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