sample-explanation.adb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- Sample.Explanation --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 1998-2004,2006 Free Software Foundation, Inc. --
  11. -- --
  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: --
  19. -- --
  20. -- The above copyright notice and this permission notice shall be included --
  21. -- in all copies or substantial portions of the Software. --
  22. -- --
  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. --
  30. -- --
  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 --
  34. -- authorization. --
  35. ------------------------------------------------------------------------------
  36. -- Author: Juergen Pfeifer, 1996
  37. -- Version Control
  38. -- $Revision: 1.20 $
  39. -- $Date: 2006/06/25 14:30:22 $
  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.
  45. --
  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;
  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;
  54. package body Sample.Explanation is
  55. Help_Keys : constant String := "HELPKEYS";
  56. In_Help : constant String := "INHELP";
  57. File_Name : constant String := "explain.msg";
  58. F : File_Type;
  59. type Help_Line;
  60. type Help_Line_Access is access Help_Line;
  61. pragma Controlled (Help_Line_Access);
  62. type String_Access is access String;
  63. pragma Controlled (String_Access);
  64. type Help_Line is
  65. record
  66. Prev, Next : Help_Line_Access;
  67. Line : String_Access;
  68. end record;
  69. procedure Explain (Key : in String;
  70. Win : in Window);
  71. procedure Release_String is
  72. new Ada.Unchecked_Deallocation (String,
  73. String_Access);
  74. procedure Release_Help_Line is
  75. new Ada.Unchecked_Deallocation (Help_Line,
  76. Help_Line_Access);
  77. function Search (Key : String) return Help_Line_Access;
  78. procedure Release_Help (Root : in out Help_Line_Access);
  79. procedure Explain (Key : in String)
  80. is
  81. begin
  82. Explain (Key, Null_Window);
  83. end Explain;
  84. procedure Explain (Key : in String;
  85. Win : in Window)
  86. is
  87. -- Retrieve the text associated with this key and display it in this
  88. -- window. If no window argument is passed, the routine will create
  89. -- a temporary window and use it.
  90. function Filter_Key return Real_Key_Code;
  91. procedure Unknown_Key;
  92. procedure Redo;
  93. procedure To_Window (C : in out Help_Line_Access;
  94. More : in out Boolean);
  95. Frame : Window := Null_Window;
  96. W : Window := Win;
  97. K : Real_Key_Code;
  98. P : Panel;
  99. Height : Line_Count;
  100. Width : Column_Count;
  101. Help : Help_Line_Access := Search (Key);
  102. Current : Help_Line_Access;
  103. Top_Line : Help_Line_Access;
  104. Has_More : Boolean := True;
  105. procedure Unknown_Key
  106. is
  107. begin
  108. Add (W, "Help message with ID ");
  109. Add (W, Key);
  110. Add (W, " not found.");
  111. Add (W, Character'Val (10));
  112. Add (W, "Press the Function key labelled 'Quit' key to continue.");
  113. end Unknown_Key;
  114. procedure Redo
  115. is
  116. H : Help_Line_Access := Top_Line;
  117. begin
  118. if Top_Line /= null then
  119. for L in 0 .. (Height - 1) loop
  120. Add (W, L, 0, H.Line.all);
  121. exit when H.Next = null;
  122. H := H.Next;
  123. end loop;
  124. else
  125. Unknown_Key;
  126. end if;
  127. end Redo;
  128. function Filter_Key return Real_Key_Code
  129. is
  130. K : Real_Key_Code;
  131. begin
  132. loop
  133. K := Get_Key (W);
  134. if K in Special_Key_Code'Range then
  135. case K is
  136. when HELP_CODE =>
  137. if not Find_Context (In_Help) then
  138. Push_Environment (In_Help, False);
  139. Explain (In_Help, W);
  140. Pop_Environment;
  141. Redo;
  142. end if;
  143. when EXPLAIN_CODE =>
  144. if not Find_Context (Help_Keys) then
  145. Push_Environment (Help_Keys, False);
  146. Explain (Help_Keys, W);
  147. Pop_Environment;
  148. Redo;
  149. end if;
  150. when others => exit;
  151. end case;
  152. else
  153. exit;
  154. end if;
  155. end loop;
  156. return K;
  157. end Filter_Key;
  158. procedure To_Window (C : in out Help_Line_Access;
  159. More : in out Boolean)
  160. is
  161. L : Line_Position := 0;
  162. begin
  163. loop
  164. Add (W, L, 0, C.Line.all);
  165. L := L + 1;
  166. exit when C.Next = null or else L = Height;
  167. C := C.Next;
  168. end loop;
  169. if C.Next /= null then
  170. pragma Assert (L = Height);
  171. More := True;
  172. else
  173. More := False;
  174. end if;
  175. end To_Window;
  176. begin
  177. if W = Null_Window then
  178. Push_Environment ("HELP");
  179. Default_Labels;
  180. Frame := New_Window (Lines - 2, Columns, 0, 0);
  181. if Has_Colors then
  182. Set_Background (Win => Frame,
  183. Ch => (Ch => ' ',
  184. Color => Help_Color,
  185. Attr => Normal_Video));
  186. Set_Character_Attributes (Win => Frame,
  187. Attr => Normal_Video,
  188. Color => Help_Color);
  189. Erase (Frame);
  190. end if;
  191. Box (Frame);
  192. Set_Character_Attributes (Frame, (Reverse_Video => True,
  193. others => False));
  194. Add (Frame, Lines - 3, 2, "Cursor Up/Down scrolls");
  195. Set_Character_Attributes (Frame); -- Back to default.
  196. Window_Title (Frame, "Explanation");
  197. W := Derived_Window (Frame, Lines - 4, Columns - 2, 1, 1);
  198. Refresh_Without_Update (Frame);
  199. Get_Size (W, Height, Width);
  200. Set_Meta_Mode (W);
  201. Set_KeyPad_Mode (W);
  202. Allow_Scrolling (W, True);
  203. Set_Echo_Mode (False);
  204. P := Create (Frame);
  205. Top (P);
  206. Update_Panels;
  207. else
  208. Clear (W);
  209. Refresh_Without_Update (W);
  210. end if;
  211. Current := Help; Top_Line := Help;
  212. if null = Help then
  213. Unknown_Key;
  214. loop
  215. K := Filter_Key;
  216. exit when K = QUIT_CODE;
  217. end loop;
  218. else
  219. To_Window (Current, Has_More);
  220. if Has_More then
  221. -- This means there are more lines available, so we have to go
  222. -- into a scroll manager.
  223. loop
  224. K := Filter_Key;
  225. if K in Special_Key_Code'Range then
  226. case K is
  227. when Key_Cursor_Down =>
  228. if Current.Next /= null then
  229. Move_Cursor (W, Height - 1, 0);
  230. Scroll (W, 1);
  231. Current := Current.Next;
  232. Top_Line := Top_Line.Next;
  233. Add (W, Current.Line.all);
  234. end if;
  235. when Key_Cursor_Up =>
  236. if Top_Line.Prev /= null then
  237. Move_Cursor (W, 0, 0);
  238. Scroll (W, -1);
  239. Top_Line := Top_Line.Prev;
  240. Current := Current.Prev;
  241. Add (W, Top_Line.Line.all);
  242. end if;
  243. when QUIT_CODE => exit;
  244. when others => null;
  245. end case;
  246. end if;
  247. end loop;
  248. else
  249. loop
  250. K := Filter_Key;
  251. exit when K = QUIT_CODE;
  252. end loop;
  253. end if;
  254. end if;
  255. Clear (W);
  256. if Frame /= Null_Window then
  257. Clear (Frame);
  258. Delete (P);
  259. Delete (W);
  260. Delete (Frame);
  261. Pop_Environment;
  262. end if;
  263. Update_Panels;
  264. Update_Screen;
  265. Release_Help (Help);
  266. end Explain;
  267. function Search (Key : String) return Help_Line_Access
  268. is
  269. Last : Natural;
  270. Buffer : String (1 .. 256);
  271. Root : Help_Line_Access := null;
  272. Current : Help_Line_Access;
  273. Tail : Help_Line_Access := null;
  274. function Next_Line return Boolean;
  275. function Next_Line return Boolean
  276. is
  277. H_End : constant String := "#END";
  278. begin
  279. Get_Line (F, Buffer, Last);
  280. if Last = H_End'Length and then H_End = Buffer (1 .. Last) then
  281. return False;
  282. else
  283. return True;
  284. end if;
  285. end Next_Line;
  286. begin
  287. Reset (F);
  288. Outer :
  289. loop
  290. exit Outer when not Next_Line;
  291. if Last = (1 + Key'Length) and then Key = Buffer (2 .. Last)
  292. and then Buffer (1) = '#' then
  293. loop
  294. exit when not Next_Line;
  295. exit when Buffer (1) = '#';
  296. Current := new Help_Line'(null, null,
  297. new String'(Buffer (1 .. Last)));
  298. if Tail = null then
  299. Release_Help (Root);
  300. Root := Current;
  301. else
  302. Tail.Next := Current;
  303. Current.Prev := Tail;
  304. end if;
  305. Tail := Current;
  306. end loop;
  307. exit Outer;
  308. end if;
  309. end loop Outer;
  310. return Root;
  311. end Search;
  312. procedure Release_Help (Root : in out Help_Line_Access)
  313. is
  314. Next : Help_Line_Access;
  315. begin
  316. loop
  317. exit when Root = null;
  318. Next := Root.Next;
  319. Release_String (Root.Line);
  320. Release_Help_Line (Root);
  321. Root := Next;
  322. end loop;
  323. end Release_Help;
  324. procedure Explain_Context
  325. is
  326. begin
  327. Explain (Context);
  328. end Explain_Context;
  329. procedure Notepad (Key : in String)
  330. is
  331. H : constant Help_Line_Access := Search (Key);
  332. T : Help_Line_Access := H;
  333. N : Line_Count := 1;
  334. L : Line_Position := 0;
  335. W : Window;
  336. P : Panel;
  337. begin
  338. if H /= null then
  339. loop
  340. T := T.Next;
  341. exit when T = null;
  342. N := N + 1;
  343. end loop;
  344. W := New_Window (N + 2, Columns, Lines - N - 2, 0);
  345. if Has_Colors then
  346. Set_Background (Win => W,
  347. Ch => (Ch => ' ',
  348. Color => Notepad_Color,
  349. Attr => Normal_Video));
  350. Set_Character_Attributes (Win => W,
  351. Attr => Normal_Video,
  352. Color => Notepad_Color);
  353. Erase (W);
  354. end if;
  355. Box (W);
  356. Window_Title (W, "Notepad");
  357. P := New_Panel (W);
  358. T := H;
  359. loop
  360. Add (W, L + 1, 1, T.Line.all, Integer (Columns - 2));
  361. L := L + 1;
  362. T := T.Next;
  363. exit when T = null;
  364. end loop;
  365. T := H;
  366. Release_Help (T);
  367. Refresh_Without_Update (W);
  368. Notepad_To_Context (P);
  369. end if;
  370. end Notepad;
  371. begin
  372. Open (F, In_File, File_Name);
  373. end Sample.Explanation;