1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
5 -- Sample.Explanation --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 1998 Free Software Foundation, Inc. --
12 -- Permission is hereby granted, free of charge, to any person obtaining a --
13 -- copy of this software and associated documentation files (the --
14 -- "Software"), to deal in the Software without restriction, including --
15 -- without limitation the rights to use, copy, modify, merge, publish, --
16 -- distribute, distribute with modifications, sublicense, and/or sell --
17 -- copies of the Software, and to permit persons to whom the Software is --
18 -- furnished to do so, subject to the following conditions: --
20 -- The above copyright notice and this permission notice shall be included --
21 -- in all copies or substantial portions of the Software. --
23 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
24 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
25 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
26 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
27 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
28 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
29 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
31 -- Except as contained in this notice, the name(s) of the above copyright --
32 -- holders shall not be used in advertising or otherwise to promote the --
33 -- sale, use or other dealings in this Software without prior written --
35 ------------------------------------------------------------------------------
36 -- Author: Juergen Pfeifer, 1996
37 -- Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en
40 -- Binding Version 01.00
41 ------------------------------------------------------------------------------
42 -- Poor mans help system. This scans a sequential file for key lines and
43 -- then reads the lines up to the next key. Those lines are presented in
44 -- a window as help or explanation.
46 with Ada.Text_IO; use Ada.Text_IO;
47 with Ada.Unchecked_Deallocation;
48 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
49 with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
51 with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
52 with Sample.Manifest; use Sample.Manifest;
53 with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
54 with Sample.Helpers; use Sample.Helpers;
56 package body Sample.Explanation is
58 Help_Keys : constant String := "HELPKEYS";
59 In_Help : constant String := "INHELP";
61 File_Name : String := "explain.msg";
65 type Help_Line_Access is access Help_Line;
66 pragma Controlled (Help_Line_Access);
67 type String_Access is access String;
68 pragma Controlled (String_Access);
72 Prev, Next : Help_Line_Access;
76 procedure Explain (Key : in String;
79 procedure Release_String is
80 new Ada.Unchecked_Deallocation (String,
82 procedure Release_Help_Line is
83 new Ada.Unchecked_Deallocation (Help_Line,
86 function Search (Key : String) return Help_Line_Access;
87 procedure Release_Help (Root : in out Help_Line_Access);
89 procedure Explain (Key : in String)
92 Explain (Key, Null_Window);
95 procedure Explain (Key : in String;
98 -- Retrieve the text associated with this key and display it in this
99 -- window. If no window argument is passed, the routine will create
100 -- a temporary window and use it.
102 function Filter_Key return Real_Key_Code;
103 procedure Unknown_Key;
105 procedure To_Window (C : in out Help_Line_Access;
106 More : in out Boolean);
108 Frame : Window := Null_Window;
115 Width : Column_Count;
116 Help : Help_Line_Access := Search (Key);
117 Current : Help_Line_Access;
118 Top_Line : Help_Line_Access;
122 procedure Unknown_Key
125 Add (W, "Help message with ID ");
127 Add (W, " not found.");
128 Add (W, Character'Val (10));
129 Add (W, "Press the Function key labelled 'Quit' key to continue.");
134 H : Help_Line_Access := Top_Line;
136 if Top_Line /= null then
137 for L in 0 .. (Height - 1) loop
138 Add (W, L, 0, H.Line.all);
139 exit when H.Next = null;
147 function Filter_Key return Real_Key_Code
153 if K in Special_Key_Code'Range then
156 if not Find_Context (In_Help) then
157 Push_Environment (In_Help, False);
158 Explain (In_Help, W);
163 if not Find_Context (Help_Keys) then
164 Push_Environment (Help_Keys, False);
165 Explain (Help_Keys, W);
178 procedure To_Window (C : in out Help_Line_Access;
179 More : in out Boolean)
181 L : Line_Position := 0;
184 Add (W, L, 0, C.Line.all);
186 exit when C.Next = null or else L = Height;
189 if C.Next /= null then
190 pragma Assert (L = Height);
198 if W = Null_Window then
199 Push_Environment ("HELP");
201 Frame := New_Window (Lines - 2, Columns, 0, 0);
203 Set_Background (Win => Frame,
206 Attr => Normal_Video));
207 Set_Character_Attributes (Win => Frame,
208 Attr => Normal_Video,
209 Color => Help_Color);
213 Set_Character_Attributes (Frame, (Reverse_Video => True,
215 Add (Frame, Lines - 3, 2, "Cursor Up/Down scrolls");
216 Set_Character_Attributes (Frame); -- Back to default.
217 Window_Title (Frame, "Explanation");
218 W := Derived_Window (Frame, Lines - 4, Columns - 2, 1, 1);
219 Refresh_Without_Update (Frame);
220 Get_Size (W, Height, Width);
223 Allow_Scrolling (W, True);
224 Set_Echo_Mode (False);
230 Refresh_Without_Update (W);
233 Current := Help; Top_Line := Help;
239 exit when K = QUIT_CODE;
242 To_Window (Current, Has_More);
244 -- This means there are more lines available, so we have to go
245 -- into a scroll manager.
248 if K in Special_Key_Code'Range then
250 when Key_Cursor_Down =>
251 if Current.Next /= null then
252 Move_Cursor (W, Height - 1, 0);
254 Current := Current.Next;
255 Top_Line := Top_Line.Next;
256 Add (W, Current.Line.all);
258 when Key_Cursor_Up =>
259 if Top_Line.Prev /= null then
260 Move_Cursor (W, 0, 0);
262 Top_Line := Top_Line.Prev;
263 Current := Current.Prev;
264 Add (W, Top_Line.Line.all);
266 when QUIT_CODE => exit;
274 exit when K = QUIT_CODE;
281 if Frame /= Null_Window then
296 function Search (Key : String) return Help_Line_Access
299 Buffer : String (1 .. 256);
300 Root : Help_Line_Access := null;
301 Current : Help_Line_Access;
302 Tail : Help_Line_Access := null;
304 function Next_Line return Boolean;
306 function Next_Line return Boolean
308 H_End : constant String := "#END";
310 Get_Line (F, Buffer, Last);
311 if Last = H_End'Length and then H_End = Buffer (1 .. Last) then
321 exit Outer when not Next_Line;
322 if Last = (1 + Key'Length) and then Key = Buffer (2 .. Last)
323 and then Buffer (1) = '#' then
325 exit when not Next_Line;
326 exit when Buffer (1) = '#';
327 Current := new Help_Line'(null, null,
328 new String'(Buffer (1 .. Last)));
333 Tail.Next := Current;
334 Current.Prev := Tail;
344 procedure Release_Help (Root : in out Help_Line_Access)
346 Next : Help_Line_Access;
349 exit when Root = null;
351 Release_String (Root.Line);
352 Release_Help_Line (Root);
357 procedure Explain_Context
363 procedure Notepad (Key : in String)
365 H : constant Help_Line_Access := Search (Key);
366 T : Help_Line_Access := H;
368 L : Line_Position := 0;
378 W := New_Window (N + 2, Columns, Lines - N - 2, 0);
380 Set_Background (Win => W,
382 Color => Notepad_Color,
383 Attr => Normal_Video));
384 Set_Character_Attributes (Win => W,
385 Attr => Normal_Video,
386 Color => Notepad_Color);
390 Window_Title (W, "Notepad");
394 Add (W, L + 1, 1, T.Line.all, Integer (Columns - 2));
401 Refresh_Without_Update (W);
402 Notepad_To_Context (P);
407 Open (F, In_File, File_Name);
408 end Sample.Explanation;