]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/ncurses2-slk_test.adb
ncurses 5.3
[ncurses.git] / Ada95 / samples / ncurses2-slk_test.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                                 ncurses                                  --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000 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.1 $
39 --  Binding Version 01.00
40 ------------------------------------------------------------------------------
41 with ncurses2.util; use ncurses2.util;
42 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
43
44 with Ada.Strings.Unbounded;
45 with Interfaces.C;
46 with Terminal_Interface.Curses.Aux;
47
48 procedure ncurses2.slk_test is
49    procedure myGet (Win : in  Window := Standard_Window;
50                     Str : out Ada.Strings.Unbounded.Unbounded_String;
51                     Len : in  Integer := -1);
52
53    procedure myGet (Win : in  Window := Standard_Window;
54                     Str : out Ada.Strings.Unbounded.Unbounded_String;
55                     Len : in  Integer := -1)
56    is
57       use Ada.Strings.Unbounded;
58       use Interfaces.C;
59       use Terminal_Interface.Curses.Aux;
60
61       function Wgetnstr (Win : Window;
62                          Str : char_array;
63                          Len : int) return int;
64       pragma Import (C, Wgetnstr, "wgetnstr");
65
66       Txt : char_array (0 .. 10);
67    begin
68       Txt (0) := Interfaces.C.char'First;
69       if Wgetnstr (Win, Txt, 8) = Curses_Err then
70          raise Curses_Exception;
71       end if;
72       Str := To_Unbounded_String (To_Ada (Txt, True));
73    end myGet;
74
75
76    use Int_IO;
77
78    use Ada.Strings.Unbounded;
79
80    c : Key_Code;
81    buf : Unbounded_String;
82    c2 : Character;
83    fmt : Label_Justification := Centered;
84    tmp : Integer;
85
86 begin
87    c := CTRL ('l');
88    loop
89       Move_Cursor (Line => 0, Column => 0);
90       c2 := Code_To_Char (c);
91       case c2 is
92          when Character'Val (Character'Pos ('l') mod 16#20#) => --  CTRL('l')
93             Erase;
94             Switch_Character_Attribute (Attr => (Bold_Character => True,
95                                                  others => False));
96             Add (Line => 0, Column => 20,
97                  Str => "Soft Key Exerciser");
98             Switch_Character_Attribute (On => False,
99                                         Attr => (Bold_Character => True,
100                                                  others => False));
101
102             Move_Cursor (Line => 2, Column => 0);
103             P ("Available commands are:");
104             P ("");
105             P ("^L         -- refresh screen");
106             P ("a          -- activate or restore soft keys");
107             P ("d          -- disable soft keys");
108             P ("c          -- set centered format for labels");
109             P ("l          -- set left-justified format for labels");
110             P ("r          -- set right-justified format for labels");
111             P ("[12345678] -- set label; labels are numbered 1 through 8");
112             P ("e          -- erase stdscr (should not erase labels)");
113             P ("s          -- test scrolling of shortened screen");
114             P ("x, q       -- return to main menu");
115             P ("");
116             P ("Note: if activating the soft keys causes your terminal to");
117             P ("scroll up one line, your terminal auto-scrolls when anything");
118             P ("is written to the last screen position.  The ncurses code");
119             P ("does not yet handle this gracefully.");
120             Refresh;
121             Restore_Soft_Label_Keys;
122
123          when 'a' =>
124             Restore_Soft_Label_Keys;
125          when 'e' =>
126             Clear;
127          when 's' =>
128             Add (Line => 20, Column => 0,
129                 Str => "Press Q to stop the scrolling-test: ");
130             loop
131                c := Getchar;
132                c2 := Code_To_Char (c);
133                exit when c2 = 'Q';
134                --  c = ERR?
135                --  TODO when c is not a character (arrow key)
136                --  the behavior is different from the C version.
137                Add (Ch => c2);
138             end loop;
139          when 'd' =>
140             Clear_Soft_Label_Keys;
141          when 'l' =>
142             fmt := Left;
143          when 'c' =>
144             fmt := Centered;
145          when 'r' =>
146             fmt := Right;
147          when '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8'  =>
148             Add (Line => 20, Column => 0,
149                  Str => "Please enter the label value: ");
150             Set_Echo_Mode (SwitchOn => True);
151             myGet (Str => buf);
152             Set_Echo_Mode (SwitchOn => False);
153             tmp := ctoi (c2);
154             Set_Soft_Label_Key (Label_Number (tmp), To_String (buf), fmt);
155             Refresh_Soft_Label_Keys;
156             Move_Cursor (Line => 20, Column => 0);
157             Clear_To_End_Of_Line;
158          when 'x' | 'q' =>
159             exit;
160             --  the C version needed a goto, ha ha
161             --  breaks exit the case not the loop because fall-throuh
162             --  happens in C!
163          when others =>
164             Beep;
165       end case;
166       c := Getchar;
167       --  TODO exit when c = EOF
168    end loop;
169    Erase;
170    End_Windows;
171 end ncurses2.slk_test;