sample.adb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- Sample --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 1998,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.17 $
  39. -- $Date: 2008/09/27 14:42:40 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Text_IO;
  43. with Ada.Exceptions; use Ada.Exceptions;
  44. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  45. with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
  46. with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
  47. with Terminal_Interface.Curses.Menus.Menu_User_Data;
  48. with Terminal_Interface.Curses.Menus.Item_User_Data;
  49. with Sample.Manifest; use Sample.Manifest;
  50. with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
  51. with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
  52. with Sample.Header_Handler; use Sample.Header_Handler;
  53. with Sample.Explanation; use Sample.Explanation;
  54. with Sample.Menu_Demo.Handler;
  55. with Sample.Curses_Demo;
  56. with Sample.Form_Demo;
  57. with Sample.Menu_Demo;
  58. with Sample.Text_IO_Demo;
  59. with GNAT.OS_Lib;
  60. package body Sample is
  61. type User_Data is
  62. record
  63. Data : Integer;
  64. end record;
  65. type User_Access is access User_Data;
  66. package Ud is new
  67. Terminal_Interface.Curses.Menus.Menu_User_Data
  68. (User_Data, User_Access);
  69. package Id is new
  70. Terminal_Interface.Curses.Menus.Item_User_Data
  71. (User_Data, User_Access);
  72. procedure Whow is
  73. procedure Main_Menu;
  74. procedure Main_Menu
  75. is
  76. function My_Driver (M : Menu;
  77. K : Key_Code;
  78. Pan : Panel) return Boolean;
  79. package Mh is new Sample.Menu_Demo.Handler (My_Driver);
  80. I : Item_Array_Access := new Item_Array'
  81. (New_Item ("Curses Core Demo"),
  82. New_Item ("Menu Demo"),
  83. New_Item ("Form Demo"),
  84. New_Item ("Text IO Demo"),
  85. Null_Item);
  86. M : Menu := New_Menu (I);
  87. D1, D2 : User_Access;
  88. I1, I2 : User_Access;
  89. function My_Driver (M : Menu;
  90. K : Key_Code;
  91. Pan : Panel) return Boolean
  92. is
  93. Idx : constant Positive := Get_Index (Current (M));
  94. begin
  95. if K in User_Key_Code'Range then
  96. if K = QUIT then
  97. return True;
  98. elsif K = SELECT_ITEM then
  99. if Idx <= 4 then
  100. Hide (Pan);
  101. Update_Panels;
  102. end if;
  103. case Idx is
  104. when 1 => Sample.Curses_Demo.Demo;
  105. when 2 => Sample.Menu_Demo.Demo;
  106. when 3 => Sample.Form_Demo.Demo;
  107. when 4 => Sample.Text_IO_Demo.Demo;
  108. when others => null;
  109. end case;
  110. if Idx <= 4 then
  111. Top (Pan);
  112. Show (Pan);
  113. Update_Panels;
  114. Update_Screen;
  115. end if;
  116. end if;
  117. end if;
  118. return False;
  119. end My_Driver;
  120. begin
  121. if (1 + Item_Count (M)) /= I'Length then
  122. raise Constraint_Error;
  123. end if;
  124. D1 := new User_Data'(Data => 4711);
  125. Ud.Set_User_Data (M, D1);
  126. I1 := new User_Data'(Data => 1174);
  127. Id.Set_User_Data (I (1), I1);
  128. Set_Spacing (Men => M, Row => 2);
  129. Default_Labels;
  130. Notepad ("MAINPAD");
  131. Mh.Drive_Me (M, " Demo ");
  132. Ud.Get_User_Data (M, D2);
  133. pragma Assert (D1 = D2);
  134. pragma Assert (D1.Data = D2.Data);
  135. Id.Get_User_Data (I (1), I2);
  136. pragma Assert (I1 = I2);
  137. pragma Assert (I1.Data = I2.Data);
  138. Delete (M);
  139. Free (I, True);
  140. end Main_Menu;
  141. begin
  142. Initialize (PC_Style_With_Index);
  143. Init_Header_Handler;
  144. Init_Screen;
  145. if Has_Colors then
  146. Start_Color;
  147. Init_Pair (Pair => Default_Colors, Fore => Black, Back => White);
  148. Init_Pair (Pair => Menu_Back_Color, Fore => Black, Back => Cyan);
  149. Init_Pair (Pair => Menu_Fore_Color, Fore => Red, Back => Cyan);
  150. Init_Pair (Pair => Menu_Grey_Color, Fore => White, Back => Cyan);
  151. Init_Pair (Pair => Notepad_Color, Fore => Black, Back => Yellow);
  152. Init_Pair (Pair => Help_Color, Fore => Blue, Back => Cyan);
  153. Init_Pair (Pair => Form_Back_Color, Fore => Black, Back => Cyan);
  154. Init_Pair (Pair => Form_Fore_Color, Fore => Red, Back => Cyan);
  155. Init_Pair (Pair => Header_Color, Fore => Black, Back => Green);
  156. Set_Background (Ch => (Color => Default_Colors,
  157. Attr => Normal_Video,
  158. Ch => ' '));
  159. Set_Character_Attributes (Attr => Normal_Video,
  160. Color => Default_Colors);
  161. Erase;
  162. Set_Soft_Label_Key_Attributes (Color => Header_Color);
  163. -- This propagates the attributes to the label window
  164. Refresh_Soft_Label_Keys;
  165. end if;
  166. Init_Keyboard_Handler;
  167. Set_Echo_Mode (False);
  168. Set_Raw_Mode;
  169. Set_Meta_Mode;
  170. Set_KeyPad_Mode;
  171. -- Initialize the Function Key Environment
  172. -- We have some fixed key throughout this sample
  173. Main_Menu;
  174. End_Windows;
  175. Curses_Free_All;
  176. exception
  177. when Event : others =>
  178. Terminal_Interface.Curses.End_Windows;
  179. Text_IO.Put ("Exception: ");
  180. Text_IO.Put (Exception_Name (Event));
  181. Text_IO.New_Line;
  182. GNAT.OS_Lib.OS_Exit (1);
  183. end Whow;
  184. end Sample;