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
39 -- Binding Version 01.00
40 ------------------------------------------------------------------------------
41 -- Poor mans help system. This scans a sequential file for key lines and
42 -- then reads the lines up to the next key. Those lines are presented in
43 -- a window as help or explanation.
45 with Ada.Text_IO; use Ada.Text_IO;
46 with Ada.Unchecked_Deallocation;
47 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
48 with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
50 with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
51 with Sample.Manifest; use Sample.Manifest;
52 with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
53 with Sample.Helpers; use Sample.Helpers;
55 package body Sample.Explanation is
57 Help_Keys : constant String := "HELPKEYS";
58 In_Help : constant String := "INHELP";
60 File_Name : String := "explain.msg";
64 type Help_Line_Access is access Help_Line;
65 pragma Controlled (Help_Line_Access);
66 type String_Access is access String;
67 pragma Controlled (String_Access);
71 Prev, Next : Help_Line_Access;
75 procedure Explain (Key : in String;
78 procedure Release_String is
79 new Ada.Unchecked_Deallocation (String,
81 procedure Release_Help_Line is
82 new Ada.Unchecked_Deallocation (Help_Line,
85 function Search (Key : String) return Help_Line_Access;
86 procedure Release_Help (Root : in out Help_Line_Access);
88 procedure Explain (Key : in String)
91 Explain (Key, Null_Window);
94 procedure Explain (Key : in String;
97 -- Retrieve the text associated with this key and display it in this
98 -- window. If no window argument is passed, the routine will create
99 -- a temporary window and use it.
101 function Filter_Key return Real_Key_Code;
102 procedure Unknown_Key;
104 procedure To_Window (C : in out Help_Line_Access;
105 More : in out Boolean);
107 Frame : Window := Null_Window;
114 Width : Column_Count;
115 Help : Help_Line_Access := Search (Key);
116 Current : Help_Line_Access;
117 Top_Line : Help_Line_Access;
121 procedure Unknown_Key
124 Add (W, "Help message with ID ");
126 Add (W, " not found.");
127 Add (W, Character'Val (10));
128 Add (W, "Press the Function key labelled 'Quit' key to continue.");
133 H : Help_Line_Access := Top_Line;
135 if Top_Line /= null then
136 for L in 0 .. (Height - 1) loop
137 Add (W, L, 0, H.Line.all);
138 exit when H.Next = null;
146 function Filter_Key return Real_Key_Code
152 if K in Special_Key_Code'Range then
155 if not Find_Context (In_Help) then
156 Push_Environment (In_Help, False);
157 Explain (In_Help, W);
162 if not Find_Context (Help_Keys) then
163 Push_Environment (Help_Keys, False);
164 Explain (Help_Keys, W);
177 procedure To_Window (C : in out Help_Line_Access;
178 More : in out Boolean)
180 L : Line_Position := 0;
183 Add (W, L, 0, C.Line.all);
185 exit when C.Next = null or else L = Height;
188 if C.Next /= null then
189 pragma Assert (L = Height);
197 if W = Null_Window then
198 Push_Environment ("HELP");
200 Frame := New_Window (Lines - 2, Columns, 0, 0);
202 Set_Background (Win => Frame,
205 Attr => Normal_Video));
206 Set_Character_Attributes (Win => Frame,
207 Attr => Normal_Video,
208 Color => Help_Color);
212 Set_Character_Attributes (Frame, (Reverse_Video => True,
214 Add (Frame, Lines - 3, 2, "Cursor Up/Down scrolls");
215 Set_Character_Attributes (Frame); -- Back to default.
216 Window_Title (Frame, "Explanation");
217 W := Derived_Window (Frame, Lines - 4, Columns - 2, 1, 1);
218 Refresh_Without_Update (Frame);
219 Get_Size (W, Height, Width);
222 Allow_Scrolling (W, True);
223 Set_Echo_Mode (False);
229 Refresh_Without_Update (W);
232 Current := Help; Top_Line := Help;
238 exit when K = QUIT_CODE;
241 To_Window (Current, Has_More);
243 -- This means there are more lines available, so we have to go
244 -- into a scroll manager.
247 if K in Special_Key_Code'Range then
249 when Key_Cursor_Down =>
250 if Current.Next /= null then
251 Move_Cursor (W, Height - 1, 0);
253 Current := Current.Next;
254 Top_Line := Top_Line.Next;
255 Add (W, Current.Line.all);
257 when Key_Cursor_Up =>
258 if Top_Line.Prev /= null then
259 Move_Cursor (W, 0, 0);
261 Top_Line := Top_Line.Prev;
262 Current := Current.Prev;
263 Add (W, Top_Line.Line.all);
265 when QUIT_CODE => exit;
273 exit when K = QUIT_CODE;
280 if Frame /= Null_Window then
295 function Search (Key : String) return Help_Line_Access
298 Buffer : String (1 .. 256);
299 Root : Help_Line_Access := null;
300 Current : Help_Line_Access;
301 Tail : Help_Line_Access := null;
303 function Next_Line return Boolean;
305 function Next_Line return Boolean
307 H_End : constant String := "#END";
309 Get_Line (F, Buffer, Last);
310 if Last = H_End'Length and then H_End = Buffer (1 .. Last) then
320 exit Outer when not Next_Line;
321 if Last = (1 + Key'Length) and then Key = Buffer (2 .. Last)
322 and then Buffer (1) = '#' then
324 exit when not Next_Line;
325 exit when Buffer (1) = '#';
326 Current := new Help_Line'(null, null,
327 new String'(Buffer (1 .. Last)));
332 Tail.Next := Current;
333 Current.Prev := Tail;
343 procedure Release_Help (Root : in out Help_Line_Access)
345 Next : Help_Line_Access;
348 exit when Root = null;
350 Release_String (Root.Line);
351 Release_Help_Line (Root);
356 procedure Explain_Context
362 procedure Notepad (Key : in String)
364 H : constant Help_Line_Access := Search (Key);
365 T : Help_Line_Access := H;
367 L : Line_Position := 0;
377 W := New_Window (N + 2, Columns, Lines - N - 2, 0);
379 Set_Background (Win => W,
381 Color => Notepad_Color,
382 Attr => Normal_Video));
383 Set_Character_Attributes (Win => W,
384 Attr => Normal_Video,
385 Color => Notepad_Color);
389 Window_Title (W, "Notepad");
393 Add (W, L + 1, 1, T.Line.all, Integer (Columns - 2));
400 Refresh_Without_Update (W);
401 Notepad_To_Context (P);
406 Open (F, In_File, File_Name);
407 end Sample.Explanation;