sample-keyboard_handler.adb 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- Sample.Keyboard_Handler --
  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.14 $
  39. -- $Date: 2006/06/25 14:30:22 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Ada.Strings; use Ada.Strings;
  43. with Ada.Strings.Fixed; use Ada.Strings.Fixed;
  44. with Ada.Strings.Maps.Constants; use Ada.Strings.Maps.Constants;
  45. with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
  46. with Ada.Characters.Handling; use Ada.Characters.Handling;
  47. with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
  48. with Terminal_Interface.Curses.Forms; use Terminal_Interface.Curses.Forms;
  49. with Terminal_Interface.Curses.Forms.Field_Types.Enumeration;
  50. use Terminal_Interface.Curses.Forms.Field_Types.Enumeration;
  51. with Sample.Header_Handler; use Sample.Header_Handler;
  52. with Sample.Form_Demo.Aux; use Sample.Form_Demo.Aux;
  53. with Sample.Manifest; use Sample.Manifest;
  54. with Sample.Form_Demo.Handler;
  55. -- This package contains a centralized keyboard handler used throughout
  56. -- this example. The handler establishes a timeout mechanism that provides
  57. -- periodical updates of the common header lines used in this example.
  58. --
  59. package body Sample.Keyboard_Handler is
  60. In_Command : Boolean := False;
  61. function Get_Key (Win : Window := Standard_Window) return Real_Key_Code
  62. is
  63. K : Real_Key_Code;
  64. function Command return Real_Key_Code;
  65. function Command return Real_Key_Code
  66. is
  67. function My_Driver (F : Form;
  68. C : Key_Code;
  69. P : Panel) return Boolean;
  70. package Fh is new Sample.Form_Demo.Handler (My_Driver);
  71. type Label_Array is array (Label_Number) of String (1 .. 8);
  72. Labels : Label_Array;
  73. FA : Field_Array_Access := new Field_Array'
  74. (Make (0, 0, "Command:"),
  75. Make (Top => 0, Left => 9, Width => Columns - 11),
  76. Null_Field);
  77. K : Real_Key_Code := Key_None;
  78. N : Natural := 0;
  79. function My_Driver (F : Form;
  80. C : Key_Code;
  81. P : Panel) return Boolean
  82. is
  83. Ch : Character;
  84. begin
  85. if P = Null_Panel then
  86. raise Panel_Exception;
  87. end if;
  88. if C in User_Key_Code'Range and then C = QUIT then
  89. if Driver (F, F_Validate_Field) = Form_Ok then
  90. K := Key_None;
  91. return True;
  92. end if;
  93. elsif C in Normal_Key_Code'Range then
  94. Ch := Character'Val (C);
  95. if Ch = LF or else Ch = CR then
  96. if Driver (F, F_Validate_Field) = Form_Ok then
  97. declare
  98. Buffer : String (1 .. Positive (Columns - 11));
  99. Cmdc : String (1 .. 8);
  100. begin
  101. Get_Buffer (Fld => FA (2), Str => Buffer);
  102. Trim (Buffer, Left);
  103. if Buffer (1) /= ' ' then
  104. Cmdc := To_Upper (Buffer (Cmdc'Range));
  105. for I in Labels'Range loop
  106. if Cmdc = Labels (I) then
  107. K := Function_Key_Code
  108. (Function_Key_Number (I));
  109. exit;
  110. end if;
  111. end loop;
  112. end if;
  113. return True;
  114. end;
  115. end if;
  116. end if;
  117. end if;
  118. return False;
  119. end My_Driver;
  120. begin
  121. In_Command := True;
  122. for I in Label_Number'Range loop
  123. Get_Soft_Label_Key (I, Labels (I));
  124. Trim (Labels (I), Left);
  125. Translate (Labels (I), Upper_Case_Map);
  126. if Labels (I) (1) /= ' ' then
  127. N := N + 1;
  128. end if;
  129. end loop;
  130. if N > 0 then -- some labels were really set
  131. declare
  132. Enum_Info : Enumeration_Info (N);
  133. Enum_Field : Enumeration_Field;
  134. J : Positive := Enum_Info.Names'First;
  135. Frm : Form := Create (FA);
  136. begin
  137. for I in Label_Number'Range loop
  138. if Labels (I) (1) /= ' ' then
  139. Enum_Info.Names (J) := new String'(Labels (I));
  140. J := J + 1;
  141. end if;
  142. end loop;
  143. Enum_Field := Create (Enum_Info, True);
  144. Set_Field_Type (FA (2), Enum_Field);
  145. Set_Background (FA (2), Normal_Video);
  146. Fh.Drive_Me (Frm, Lines - 3, 0);
  147. Delete (Frm);
  148. Update_Panels; Update_Screen;
  149. end;
  150. end if;
  151. Free (FA, True);
  152. In_Command := False;
  153. return K;
  154. end Command;
  155. begin
  156. Set_Timeout_Mode (Win, Delayed, 30000);
  157. loop
  158. K := Get_Keystroke (Win);
  159. if K = Key_None then -- a timeout occured
  160. Update_Header_Window;
  161. elsif K = 3 and then not In_Command then -- CTRL-C
  162. K := Command;
  163. exit when K /= Key_None;
  164. else
  165. exit;
  166. end if;
  167. end loop;
  168. return K;
  169. end Get_Key;
  170. procedure Init_Keyboard_Handler is
  171. begin
  172. null;
  173. end Init_Keyboard_Handler;
  174. end Sample.Keyboard_Handler;