sample-form_demo-aux.adb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- Sample.Form_Demo.Aux --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 1998,2004 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.16 $
  39. -- $Date: 2004/08/21 21:37:00 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
  43. with Sample.Manifest; use Sample.Manifest;
  44. with Sample.Helpers; use Sample.Helpers;
  45. with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
  46. with Sample.Explanation; use Sample.Explanation;
  47. package body Sample.Form_Demo.Aux is
  48. procedure Geometry (F : in Form;
  49. L : out Line_Count; -- Lines used for menu
  50. C : out Column_Count; -- Columns used for menu
  51. Y : out Line_Position; -- Proposed Line for menu
  52. X : out Column_Position) -- Proposed Column for menu
  53. is
  54. begin
  55. Scale (F, L, C);
  56. L := L + 2; -- count for frame at top and bottom
  57. C := C + 2; -- "
  58. -- Calculate horizontal coordinate at the screen center
  59. X := (Columns - C) / 2;
  60. Y := 1; -- start always in line 1
  61. end Geometry;
  62. function Create (F : Form;
  63. Title : String;
  64. Lin : Line_Position;
  65. Col : Column_Position) return Panel
  66. is
  67. W, S : Window;
  68. L : Line_Count;
  69. C : Column_Count;
  70. Y : Line_Position;
  71. X : Column_Position;
  72. Pan : Panel;
  73. begin
  74. Geometry (F, L, C, Y, X);
  75. W := New_Window (L, C, Lin, Col);
  76. Set_Meta_Mode (W);
  77. Set_KeyPad_Mode (W);
  78. if Has_Colors then
  79. Set_Background (Win => W,
  80. Ch => (Ch => ' ',
  81. Color => Default_Colors,
  82. Attr => Normal_Video));
  83. Set_Character_Attributes (Win => W,
  84. Color => Default_Colors,
  85. Attr => Normal_Video);
  86. Erase (W);
  87. end if;
  88. S := Derived_Window (W, L - 2, C - 2, 1, 1);
  89. Set_Meta_Mode (S);
  90. Set_KeyPad_Mode (S);
  91. Box (W);
  92. Set_Window (F, W);
  93. Set_Sub_Window (F, S);
  94. if Title'Length > 0 then
  95. Window_Title (W, Title);
  96. end if;
  97. Pan := New_Panel (W);
  98. Post (F);
  99. return Pan;
  100. end Create;
  101. procedure Destroy (F : in Form;
  102. P : in out Panel)
  103. is
  104. W, S : Window;
  105. begin
  106. W := Get_Window (F);
  107. S := Get_Sub_Window (F);
  108. Post (F, False);
  109. Erase (W);
  110. Delete (P);
  111. Set_Window (F, Null_Window);
  112. Set_Sub_Window (F, Null_Window);
  113. Delete (S);
  114. Delete (W);
  115. Update_Panels;
  116. end Destroy;
  117. function Get_Request (F : Form;
  118. P : Panel;
  119. Handle_CRLF : Boolean := True) return Key_Code
  120. is
  121. W : constant Window := Get_Window (F);
  122. K : Real_Key_Code;
  123. Ch : Character;
  124. begin
  125. Top (P);
  126. loop
  127. K := Get_Key (W);
  128. if K in Special_Key_Code'Range then
  129. case K is
  130. when HELP_CODE => Explain_Context;
  131. when EXPLAIN_CODE => Explain ("FORMKEYS");
  132. when Key_Home => return F_First_Field;
  133. when Key_End => return F_Last_Field;
  134. when QUIT_CODE => return QUIT;
  135. when Key_Cursor_Down => return F_Down_Char;
  136. when Key_Cursor_Up => return F_Up_Char;
  137. when Key_Cursor_Left => return F_Previous_Char;
  138. when Key_Cursor_Right => return F_Next_Char;
  139. when Key_Next_Page => return F_Next_Page;
  140. when Key_Previous_Page => return F_Previous_Page;
  141. when Key_Backspace => return F_Delete_Previous;
  142. when Key_Clear_Screen => return F_Clear_Field;
  143. when Key_Clear_End_Of_Line => return F_Clear_EOF;
  144. when others => return K;
  145. end case;
  146. elsif K in Normal_Key_Code'Range then
  147. Ch := Character'Val (K);
  148. case Ch is
  149. when CAN => return QUIT; -- CTRL-X
  150. when ACK => return F_Next_Field; -- CTRL-F
  151. when STX => return F_Previous_Field; -- CTRL-B
  152. when FF => return F_Left_Field; -- CTRL-L
  153. when DC2 => return F_Right_Field; -- CTRL-R
  154. when NAK => return F_Up_Field; -- CTRL-U
  155. when EOT => return F_Down_Field; -- CTRL-D
  156. when ETB => return F_Next_Word; -- CTRL-W
  157. when DC4 => return F_Previous_Word; -- CTRL-T
  158. when SOH => return F_Begin_Field; -- CTRL-A
  159. when ENQ => return F_End_Field; -- CTRL-E
  160. when HT => return F_Insert_Char; -- CTRL-I
  161. when SI => return F_Insert_Line; -- CTRL-O
  162. when SYN => return F_Delete_Char; -- CTRL-V
  163. when BS => return F_Delete_Previous; -- CTRL-H
  164. when EM => return F_Delete_Line; -- CTRL-Y
  165. when BEL => return F_Delete_Word; -- CTRL-G
  166. when VT => return F_Clear_EOF; -- CTRL-K
  167. when SO => return F_Next_Choice; -- CTRL-N
  168. when DLE => return F_Previous_Choice; -- CTRL-P
  169. when CR | LF =>
  170. if Handle_CRLF then
  171. return F_New_Line;
  172. else
  173. return K;
  174. end if;
  175. when others => return K;
  176. end case;
  177. else
  178. return K;
  179. end if;
  180. end loop;
  181. end Get_Request;
  182. function Make (Top : Line_Position;
  183. Left : Column_Position;
  184. Text : String) return Field
  185. is
  186. Fld : Field;
  187. C : constant Column_Count := Column_Count (Text'Length);
  188. begin
  189. Fld := New_Field (1, C, Top, Left);
  190. Set_Buffer (Fld, 0, Text);
  191. Switch_Options (Fld, (Active => True, others => False), False);
  192. if Has_Colors then
  193. Set_Background (Fld => Fld, Color => Default_Colors);
  194. end if;
  195. return Fld;
  196. end Make;
  197. function Make (Height : Line_Count := 1;
  198. Width : Column_Count;
  199. Top : Line_Position;
  200. Left : Column_Position;
  201. Off_Screen : Natural := 0) return Field
  202. is
  203. Fld : constant Field := New_Field (Height, Width, Top, Left, Off_Screen);
  204. begin
  205. if Has_Colors then
  206. Set_Foreground (Fld => Fld, Color => Form_Fore_Color);
  207. Set_Background (Fld => Fld, Color => Form_Back_Color);
  208. else
  209. Set_Background (Fld, (Reverse_Video => True, others => False));
  210. end if;
  211. return Fld;
  212. end Make;
  213. function Default_Driver (F : Form;
  214. K : Key_Code;
  215. P : Panel) return Boolean
  216. is
  217. begin
  218. if P = Null_Panel then
  219. raise Panel_Exception;
  220. end if;
  221. if K in User_Key_Code'Range and then K = QUIT then
  222. if Driver (F, F_Validate_Field) = Form_Ok then
  223. return True;
  224. end if;
  225. end if;
  226. return False;
  227. end Default_Driver;
  228. function Count_Active (F : Form) return Natural
  229. is
  230. N : Natural := 0;
  231. O : Field_Option_Set;
  232. H : constant Natural := Field_Count (F);
  233. begin
  234. if H > 0 then
  235. for I in 1 .. H loop
  236. Get_Options (Fields (F, I), O);
  237. if O.Active then
  238. N := N + 1;
  239. end if;
  240. end loop;
  241. end if;
  242. return N;
  243. end Count_Active;
  244. end Sample.Form_Demo.Aux;