sample-menu_demo.adb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- Sample.Menu_Demo --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 1998-2004,2008 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.18 $
  39. -- $Date: 2008/07/26 18:48:30 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  43. with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
  44. with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
  45. with Terminal_Interface.Curses.Menus.Menu_User_Data;
  46. with Terminal_Interface.Curses.Menus.Item_User_Data;
  47. with Sample.Manifest; use Sample.Manifest;
  48. with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
  49. with Sample.Menu_Demo.Handler;
  50. with Sample.Helpers; use Sample.Helpers;
  51. with Sample.Explanation; use Sample.Explanation;
  52. package body Sample.Menu_Demo is
  53. package Spacing_Demo is
  54. procedure Spacing_Test;
  55. end Spacing_Demo;
  56. package body Spacing_Demo is
  57. procedure Spacing_Test
  58. is
  59. function My_Driver (M : Menu;
  60. K : Key_Code;
  61. P : Panel) return Boolean;
  62. procedure Set_Option_Key;
  63. procedure Set_Select_Key;
  64. procedure Set_Description_Key;
  65. procedure Set_Hide_Key;
  66. package Mh is new Sample.Menu_Demo.Handler (My_Driver);
  67. I : Item_Array_Access := new Item_Array'
  68. (New_Item ("January", "31 Days"),
  69. New_Item ("February", "28/29 Days"),
  70. New_Item ("March", "31 Days"),
  71. New_Item ("April", "30 Days"),
  72. New_Item ("May", "31 Days"),
  73. New_Item ("June", "30 Days"),
  74. New_Item ("July", "31 Days"),
  75. New_Item ("August", "31 Days"),
  76. New_Item ("September", "30 Days"),
  77. New_Item ("October", "31 Days"),
  78. New_Item ("November", "30 Days"),
  79. New_Item ("December", "31 Days"),
  80. Null_Item);
  81. M : Menu := New_Menu (I);
  82. Flip_State : Boolean := True;
  83. Hide_Long : Boolean := False;
  84. type Format_Code is (Four_By_1, Four_By_2, Four_By_3);
  85. type Operations is (Flip, Reorder, Reformat, Reselect, Describe);
  86. type Change is array (Operations) of Boolean;
  87. pragma Pack (Change);
  88. No_Change : constant Change := Change'(others => False);
  89. Current_Format : Format_Code := Four_By_1;
  90. To_Change : Change := No_Change;
  91. function My_Driver (M : Menu;
  92. K : Key_Code;
  93. P : Panel) return Boolean
  94. is
  95. begin
  96. if M = Null_Menu then
  97. raise Menu_Exception;
  98. end if;
  99. if P = Null_Panel then
  100. raise Panel_Exception;
  101. end if;
  102. To_Change := No_Change;
  103. if K in User_Key_Code'Range then
  104. if K = QUIT then
  105. return True;
  106. end if;
  107. end if;
  108. if K in Special_Key_Code'Range then
  109. case K is
  110. when Key_F4 =>
  111. To_Change (Flip) := True;
  112. return True;
  113. when Key_F5 =>
  114. To_Change (Reformat) := True;
  115. Current_Format := Four_By_1;
  116. return True;
  117. when Key_F6 =>
  118. To_Change (Reformat) := True;
  119. Current_Format := Four_By_2;
  120. return True;
  121. when Key_F7 =>
  122. To_Change (Reformat) := True;
  123. Current_Format := Four_By_3;
  124. return True;
  125. when Key_F8 =>
  126. To_Change (Reorder) := True;
  127. return True;
  128. when Key_F9 =>
  129. To_Change (Reselect) := True;
  130. return True;
  131. when Key_F10 =>
  132. if Current_Format /= Four_By_3 then
  133. To_Change (Describe) := True;
  134. return True;
  135. else
  136. return False;
  137. end if;
  138. when Key_F11 =>
  139. Hide_Long := not Hide_Long;
  140. declare
  141. O : Item_Option_Set;
  142. begin
  143. for J in I'Range loop
  144. Get_Options (I (J), O);
  145. O.Selectable := True;
  146. if Hide_Long then
  147. case J is
  148. when 1 | 3 | 5 | 7 | 8 | 10 | 12 =>
  149. O.Selectable := False;
  150. when others => null;
  151. end case;
  152. end if;
  153. Set_Options (I (J), O);
  154. end loop;
  155. end;
  156. return False;
  157. when others => null;
  158. end case;
  159. end if;
  160. return False;
  161. end My_Driver;
  162. procedure Set_Option_Key
  163. is
  164. O : Menu_Option_Set;
  165. begin
  166. if Current_Format = Four_By_1 then
  167. Set_Soft_Label_Key (8, "");
  168. else
  169. Get_Options (M, O);
  170. if O.Row_Major_Order then
  171. Set_Soft_Label_Key (8, "O-Col");
  172. else
  173. Set_Soft_Label_Key (8, "O-Row");
  174. end if;
  175. end if;
  176. Refresh_Soft_Label_Keys_Without_Update;
  177. end Set_Option_Key;
  178. procedure Set_Select_Key
  179. is
  180. O : Menu_Option_Set;
  181. begin
  182. Get_Options (M, O);
  183. if O.One_Valued then
  184. Set_Soft_Label_Key (9, "Multi");
  185. else
  186. Set_Soft_Label_Key (9, "Singl");
  187. end if;
  188. Refresh_Soft_Label_Keys_Without_Update;
  189. end Set_Select_Key;
  190. procedure Set_Description_Key
  191. is
  192. O : Menu_Option_Set;
  193. begin
  194. if Current_Format = Four_By_3 then
  195. Set_Soft_Label_Key (10, "");
  196. else
  197. Get_Options (M, O);
  198. if O.Show_Descriptions then
  199. Set_Soft_Label_Key (10, "-Desc");
  200. else
  201. Set_Soft_Label_Key (10, "+Desc");
  202. end if;
  203. end if;
  204. Refresh_Soft_Label_Keys_Without_Update;
  205. end Set_Description_Key;
  206. procedure Set_Hide_Key
  207. is
  208. begin
  209. if Hide_Long then
  210. Set_Soft_Label_Key (11, "Enab");
  211. else
  212. Set_Soft_Label_Key (11, "Disab");
  213. end if;
  214. Refresh_Soft_Label_Keys_Without_Update;
  215. end Set_Hide_Key;
  216. begin
  217. Push_Environment ("MENU01");
  218. Notepad ("MENU-PAD01");
  219. Default_Labels;
  220. Set_Soft_Label_Key (4, "Flip");
  221. Set_Soft_Label_Key (5, "4x1");
  222. Set_Soft_Label_Key (6, "4x2");
  223. Set_Soft_Label_Key (7, "4x3");
  224. Set_Option_Key;
  225. Set_Select_Key;
  226. Set_Description_Key;
  227. Set_Hide_Key;
  228. Set_Format (M, 4, 1);
  229. loop
  230. Mh.Drive_Me (M);
  231. exit when To_Change = No_Change;
  232. if To_Change (Flip) then
  233. if Flip_State then
  234. Flip_State := False;
  235. Set_Spacing (M, 3, 2, 0);
  236. else
  237. Flip_State := True;
  238. Set_Spacing (M);
  239. end if;
  240. elsif To_Change (Reformat) then
  241. case Current_Format is
  242. when Four_By_1 => Set_Format (M, 4, 1);
  243. when Four_By_2 => Set_Format (M, 4, 2);
  244. when Four_By_3 =>
  245. declare
  246. O : Menu_Option_Set;
  247. begin
  248. Get_Options (M, O);
  249. O.Show_Descriptions := False;
  250. Set_Options (M, O);
  251. Set_Format (M, 4, 3);
  252. end;
  253. end case;
  254. Set_Option_Key;
  255. Set_Description_Key;
  256. elsif To_Change (Reorder) then
  257. declare
  258. O : Menu_Option_Set;
  259. begin
  260. Get_Options (M, O);
  261. O.Row_Major_Order := not O.Row_Major_Order;
  262. Set_Options (M, O);
  263. Set_Option_Key;
  264. end;
  265. elsif To_Change (Reselect) then
  266. declare
  267. O : Menu_Option_Set;
  268. begin
  269. Get_Options (M, O);
  270. O.One_Valued := not O.One_Valued;
  271. Set_Options (M, O);
  272. Set_Select_Key;
  273. end;
  274. elsif To_Change (Describe) then
  275. declare
  276. O : Menu_Option_Set;
  277. begin
  278. Get_Options (M, O);
  279. O.Show_Descriptions := not O.Show_Descriptions;
  280. Set_Options (M, O);
  281. Set_Description_Key;
  282. end;
  283. else
  284. null;
  285. end if;
  286. end loop;
  287. Set_Spacing (M);
  288. Pop_Environment;
  289. pragma Assert (Get_Index (Items (M, 1)) = Get_Index (I (1)));
  290. Delete (M);
  291. Free (I, True);
  292. end Spacing_Test;
  293. end Spacing_Demo;
  294. procedure Demo
  295. is
  296. -- We use this datatype only to test the instantiation of
  297. -- the Menu_User_Data generic package. No functionality
  298. -- behind it.
  299. type User_Data is new Integer;
  300. type User_Data_Access is access User_Data;
  301. -- Those packages are only instantiated to test the usability.
  302. -- No real functionality is shown in the demo.
  303. package MUD is new Menu_User_Data (User_Data, User_Data_Access);
  304. package IUD is new Item_User_Data (User_Data, User_Data_Access);
  305. function My_Driver (M : Menu;
  306. K : Key_Code;
  307. P : Panel) return Boolean;
  308. package Mh is new Sample.Menu_Demo.Handler (My_Driver);
  309. Itm : Item_Array_Access := new Item_Array'
  310. (New_Item ("Menu Layout Options"),
  311. New_Item ("Demo of Hook functions"),
  312. Null_Item);
  313. M : Menu := New_Menu (Itm);
  314. U1 : constant User_Data_Access := new User_Data'(4711);
  315. U2 : User_Data_Access;
  316. U3 : constant User_Data_Access := new User_Data'(4712);
  317. U4 : User_Data_Access;
  318. function My_Driver (M : Menu;
  319. K : Key_Code;
  320. P : Panel) return Boolean
  321. is
  322. Idx : constant Positive := Get_Index (Current (M));
  323. begin
  324. if K in User_Key_Code'Range then
  325. if K = QUIT then
  326. return True;
  327. elsif K = SELECT_ITEM then
  328. if Idx in Itm'Range then
  329. Hide (P);
  330. Update_Panels;
  331. end if;
  332. case Idx is
  333. when 1 => Spacing_Demo.Spacing_Test;
  334. when others => Not_Implemented;
  335. end case;
  336. if Idx in Itm'Range then
  337. Top (P);
  338. Show (P);
  339. Update_Panels;
  340. Update_Screen;
  341. end if;
  342. end if;
  343. end if;
  344. return False;
  345. end My_Driver;
  346. begin
  347. Push_Environment ("MENU00");
  348. Notepad ("MENU-PAD00");
  349. Default_Labels;
  350. Refresh_Soft_Label_Keys_Without_Update;
  351. Set_Pad_Character (M, '|');
  352. MUD.Set_User_Data (M, U1);
  353. IUD.Set_User_Data (Itm (1), U3);
  354. Mh.Drive_Me (M);
  355. MUD.Get_User_Data (M, U2);
  356. pragma Assert (U1 = U2 and U1.all = 4711);
  357. IUD.Get_User_Data (Itm (1), U4);
  358. pragma Assert (U3 = U4 and U3.all = 4712);
  359. Pop_Environment;
  360. Delete (M);
  361. Free (Itm, True);
  362. end Demo;
  363. end Sample.Menu_Demo;