]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/ncurses2-getch_test.adb
57ceb961dc6281ed29e958f9115904ddde57e9fc
[ncurses.git] / Ada95 / samples / ncurses2-getch_test.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                                 ncurses                                  --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000,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: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
37 --  Version Control
38 --  $Revision: 1.5 $
39 --  $Date: 2004/08/21 21:37:00 $
40 --  Binding Version 01.00
41 ------------------------------------------------------------------------------
42 --  Character input test
43 --  test the keypad feature
44
45 with ncurses2.util; use ncurses2.util;
46
47 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
48 with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
49 with Ada.Characters.Handling;
50 with Ada.Strings.Bounded;
51
52 with ncurses2.genericPuts;
53
54 procedure ncurses2.getch_test is
55    use Int_IO;
56
57    function mouse_decode (ep : Mouse_Event) return String;
58
59    function mouse_decode (ep : Mouse_Event) return String is
60       Y      : Line_Position;
61       X      : Column_Position;
62       Button : Mouse_Button;
63       State  : Button_State;
64       package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (200);
65       use BS;
66       buf : Bounded_String := To_Bounded_String ("");
67    begin
68       --  Note that these bindings do not allow
69       --  two button states,
70       --  The C version can print {click-1, click-3} for example.
71       --  They also don't have the 'id' or z coordinate.
72       Get_Event (ep, Y, X, Button, State);
73
74       --  TODO Append (buf, "id "); from C version
75       Append (buf, "at (");
76       Append (buf, Column_Position'Image (X));
77       Append (buf, ", ");
78       Append (buf, Line_Position'Image (Y));
79       Append (buf, ") state");
80       Append (buf, Mouse_Button'Image (Button));
81
82       Append (buf, " = ");
83       Append (buf, Button_State'Image (State));
84       return To_String (buf);
85    end mouse_decode;
86
87
88    buf : String (1 .. 1024); --  TODO was BUFSIZE
89    n : Integer;
90    c : Key_Code;
91    blockflag : Timeout_Mode := Blocking;
92    firsttime : Boolean := True;
93    tmp2  : Event_Mask;
94    tmp6 : String (1 .. 6);
95    tmp20 : String (1 .. 20);
96    x : Column_Position;
97    y : Line_Position;
98    tmpx : Integer;
99    incount : Integer := 0;
100
101 begin
102    Refresh;
103    tmp2 := Start_Mouse (All_Events);
104    Add (Str => "Delay in 10ths of a second (<CR> for blocking input)? ");
105    Set_Echo_Mode (SwitchOn => True);
106    Get (Str => buf);
107
108    Set_Echo_Mode (SwitchOn => False);
109    Set_NL_Mode (SwitchOn => False);
110
111    if Ada.Characters.Handling.Is_Digit (buf (1)) then
112       Get (Item => n, From => buf, Last => tmpx);
113       Set_Timeout_Mode (Mode => Delayed, Amount => n * 100);
114       blockflag := Delayed;
115    end if;
116
117    c := Character'Pos ('?');
118    Set_Raw_Mode (SwitchOn => True);
119    loop
120       if not firsttime then
121          Add (Str => "Key pressed: ");
122          Put (tmp6, Integer (c), 8);
123          Add (Str => tmp6);
124          Add (Ch => ' ');
125          if c = Key_Mouse then declare
126             event : Mouse_Event;
127          begin
128             event := Get_Mouse;
129             Add (Str => "KEY_MOUSE, ");
130             Add (Str => mouse_decode (event));
131             Add (Ch => newl);
132          end;
133          elsif c >= Key_Min then
134             Key_Name (c, tmp20);
135             Add (Str => tmp20);
136             --  I used tmp and got bitten by the length problem:->
137             Add (Ch => newl);
138          elsif c > 16#80# then --  TODO fix, use constant if possible
139             declare
140                c2 : constant Character := Character'Val (c mod 16#80#);
141             begin
142                if Ada.Characters.Handling.Is_Graphic (c2) then
143                   Add (Str => "M-");
144                   Add (Ch => c2);
145                else
146                   Add (Str => "M-");
147                   Add (Str => Un_Control ((Ch => c2,
148                                            Color => Color_Pair'First,
149                                            Attr => Normal_Video)));
150                end if;
151                Add (Str => " (high-half character)");
152                Add (Ch => newl);
153             end;
154          else declare
155             c2 : constant Character := Character'Val (c mod 16#80#);
156          begin
157             if Ada.Characters.Handling.Is_Graphic (c2) then
158                Add (Ch => c2);
159                Add (Str => " (ASCII printable character)");
160                Add (Ch => newl);
161             else
162                Add (Str => Un_Control ((Ch => c2,
163                                        Color => Color_Pair'First,
164                                        Attr => Normal_Video)));
165                Add (Str => " (ASCII control character)");
166                Add (Ch => newl);
167             end if;
168          end;
169          end if;
170          --  TODO I am not sure why this was in the C version
171          --  the delay statement scroll anyway.
172          Get_Cursor_Position (Line => y, Column => x);
173          if y >= Lines - 1 then
174             Move_Cursor (Line => 0, Column => 0);
175          end if;
176          Clear_To_End_Of_Line;
177       end if;
178
179       firsttime := False;
180       if c = Character'Pos ('g') then
181          declare
182             package p is new ncurses2.genericPuts (1024);
183             use p;
184             use p.BS;
185             timedout : Boolean := False;
186             boundedbuf : Bounded_String;
187          begin
188             Add (Str => "getstr test: ");
189             Set_Echo_Mode (SwitchOn => True);
190             --  Note that if delay mode is set
191             --  Get can raise an exception.
192             --  The C version would print the string it had so far
193             --  also TODO get longer length string, like the C version
194             declare begin
195                myGet (Str => boundedbuf);
196             exception when Curses_Exception =>
197                Add (Str => "Timed out.");
198                Add (Ch => newl);
199                timedout := True;
200             end;
201             --  note that the Ada Get will stop reading at 1024.
202             if not timedout then
203                Set_Echo_Mode (SwitchOn => False);
204                Add (Str => " I saw '");
205                myAdd (Str => boundedbuf);
206                Add (Str => "'.");
207                Add (ch => newl);
208             end if;
209          end;
210       elsif c = Character'Pos ('s') then
211          ShellOut (True);
212       elsif c = Character'Pos ('x') or c = Character'Pos ('q') or
213         (c = Key_None and blockflag = Blocking) then
214          exit;
215       elsif c = Character'Pos ('?') then
216          Add (Str => "Type any key to see its keypad value.  Also:");
217          Add (Ch => newl);
218          Add (Str => "g -- triggers a getstr test");
219          Add (Ch => newl);
220          Add (Str => "s -- shell out");
221          Add (Ch => newl);
222          Add (Str => "q -- quit");
223          Add (Ch => newl);
224          Add (Str => "? -- repeats this help message");
225          Add (Ch => newl);
226       end if;
227
228       loop
229          c := Getchar;
230          exit when c /= Key_None;
231          if blockflag /= Blocking then
232             Put (tmp6, incount); --  argh string length!
233             Add (Str => tmp6);
234             Add (Str => ": input timed out");
235             Add (Ch => newl);
236          else
237             Put (tmp6, incount);
238             Add (Str => tmp6);
239             Add (Str => ": input error");
240             Add (Ch => newl);
241             exit;
242          end if;
243          incount := incount + 1;
244       end loop;
245    end loop;
246
247    End_Mouse (tmp2);
248    Set_Timeout_Mode (Mode => Blocking, Amount => 0); --  amount is ignored
249    Set_Raw_Mode (SwitchOn => False);
250    Set_NL_Mode (SwitchOn => True);
251    Erase;
252    End_Windows;
253 end ncurses2.getch_test;