]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/ncurses2-m.adb
ncurses 5.6 - patch 20080907
[ncurses.git] / Ada95 / samples / ncurses2-m.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                                 ncurses                                  --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000-2006,2008 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.8 $
39 --  $Date: 2008/07/26 18:47:50 $
40 --  Binding Version 01.00
41 ------------------------------------------------------------------------------
42 --  TODO use Default_Character where appropriate
43
44 --  This is an Ada version of ncurses
45 --  I translated this because it tests the most features.
46
47 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
48 with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
49
50 with Ada.Text_IO; use Ada.Text_IO;
51
52 with Ada.Characters.Latin_1;
53 --  with Ada.Characters.Handling;
54
55 with Ada.Command_Line; use Ada.Command_Line;
56
57 with Ada.Strings.Unbounded;
58
59 with ncurses2.util; use ncurses2.util;
60 with ncurses2.getch_test;
61 with ncurses2.attr_test;
62 with ncurses2.color_test;
63 with ncurses2.demo_panels;
64 with ncurses2.color_edit;
65 with ncurses2.slk_test;
66 with ncurses2.acs_display;
67 with ncurses2.acs_and_scroll;
68 with ncurses2.flushinp_test;
69 with ncurses2.test_sgr_attributes;
70 with ncurses2.menu_test;
71 with ncurses2.demo_pad;
72 with ncurses2.demo_forms;
73 with ncurses2.overlap_test;
74 with ncurses2.trace_set;
75
76 with ncurses2.getopt; use ncurses2.getopt;
77
78 package body ncurses2.m is
79    use Int_IO;
80
81    function To_trace (n : Integer) return Trace_Attribute_Set;
82    procedure usage;
83    procedure Set_Terminal_Modes;
84    function Do_Single_Test (c : Character) return Boolean;
85
86    function To_trace (n : Integer) return Trace_Attribute_Set is
87       a : Trace_Attribute_Set := (others => False);
88       m : Integer;
89       rest : Integer;
90    begin
91       m := n  mod 2;
92       if 1 = m then
93          a.Times := True;
94       end if;
95       rest := n / 2;
96
97       m := rest mod 2;
98       if 1 = m then
99          a.Tputs := True;
100       end if;
101       rest := rest / 2;
102       m := rest mod 2;
103       if 1 = m then
104          a.Update := True;
105       end if;
106       rest := rest / 2;
107       m := rest mod 2;
108       if 1 = m then
109          a.Cursor_Move := True;
110       end if;
111       rest := rest / 2;
112       m := rest mod 2;
113       if 1 = m then
114          a.Character_Output := True;
115       end if;
116       rest := rest / 2;
117       m := rest mod 2;
118       if 1 = m then
119          a.Calls := True;
120       end if;
121       rest := rest / 2;
122       m := rest mod 2;
123       if 1 = m then
124          a.Virtual_Puts := True;
125       end if;
126       rest := rest / 2;
127       m := rest mod 2;
128       if 1 = m then
129          a.Input_Events := True;
130       end if;
131       rest := rest / 2;
132       m := rest mod 2;
133       if 1 = m then
134          a.TTY_State := True;
135       end if;
136       rest := rest / 2;
137       m := rest mod 2;
138       if 1 = m then
139          a.Internal_Calls := True;
140       end if;
141       rest := rest / 2;
142       m := rest mod 2;
143       if 1 = m then
144          a.Character_Calls := True;
145       end if;
146       rest := rest / 2;
147       m := rest mod 2;
148       if 1 = m then
149          a.Termcap_TermInfo := True;
150       end if;
151
152       return a;
153    end To_trace;
154
155    --   these are type Stdscr_Init_Proc;
156
157    function rip_footer (
158                         Win : Window;
159                         Columns : Column_Count) return Integer;
160    pragma Convention (C, rip_footer);
161
162    function rip_footer (
163                         Win : Window;
164                         Columns : Column_Count) return Integer is
165    begin
166       Set_Background (Win, (Ch => ' ',
167                             Attr => (Reverse_Video => True, others => False),
168                             Color => 0));
169       Erase (Win);
170       Move_Cursor (Win, 0, 0);
171       Add (Win, "footer:"  & Columns'Img & " columns");
172       Refresh_Without_Update (Win);
173       return 0; -- Curses_OK;
174    end rip_footer;
175
176    function rip_header (
177                         Win : Window;
178                         Columns : Column_Count) return Integer;
179    pragma Convention (C, rip_header);
180
181    function rip_header (
182                         Win : Window;
183                         Columns : Column_Count) return Integer is
184    begin
185       Set_Background (Win, (Ch => ' ',
186                             Attr => (Reverse_Video => True, others => False),
187                             Color => 0));
188       Erase (Win);
189       Move_Cursor (Win, 0, 0);
190       Add (Win, "header:"  & Columns'Img & " columns");
191       --  'Img is a GNAT extention
192       Refresh_Without_Update (Win);
193       return 0; -- Curses_OK;
194    end rip_header;
195
196    procedure usage is
197       --  type Stringa is access String;
198       use Ada.Strings.Unbounded;
199       --  tbl : constant array (Positive range <>) of Stringa := (
200       tbl : constant array (Positive range <>) of Unbounded_String
201         := (
202             To_Unbounded_String ("Usage: ncurses [options]"),
203             To_Unbounded_String (""),
204             To_Unbounded_String ("Options:"),
205             To_Unbounded_String ("  -a f,b   set default-colors " &
206                                  "(assumed white-on-black)"),
207             To_Unbounded_String ("  -d       use default-colors if terminal " &
208                                  "supports them"),
209             To_Unbounded_String ("  -e fmt   specify format for soft-keys " &
210                                  "test (e)"),
211             To_Unbounded_String ("  -f       rip-off footer line " &
212                                  "(can repeat)"),
213             To_Unbounded_String ("  -h       rip-off header line " &
214                                  "(can repeat)"),
215             To_Unbounded_String ("  -s msec  specify nominal time for " &
216                                  "panel-demo (default: 1, to hold)"),
217             To_Unbounded_String ("  -t mask  specify default trace-level " &
218                                  "(may toggle with ^T)")
219             );
220    begin
221       for n in tbl'Range loop
222          Put_Line (Standard_Error, To_String (tbl (n)));
223       end loop;
224       --     exit(EXIT_FAILURE);
225       --  TODO should we use Set_Exit_Status and throw and exception?
226    end usage;
227
228    procedure Set_Terminal_Modes is begin
229       Set_Raw_Mode (SwitchOn => False);
230       Set_Cbreak_Mode (SwitchOn => True);
231       Set_Echo_Mode (SwitchOn => False);
232       Allow_Scrolling (Mode => True);
233       Use_Insert_Delete_Line (Do_Idl => True);
234       Set_KeyPad_Mode (SwitchOn => True);
235    end Set_Terminal_Modes;
236
237    nap_msec : Integer := 1;
238
239    function Do_Single_Test (c : Character) return Boolean is
240    begin
241       case c is
242          when 'a' =>
243             getch_test;
244          when 'b' =>
245             attr_test;
246          when 'c' =>
247             if not Has_Colors then
248                Cannot ("does not support color.");
249             else
250                color_test;
251             end if;
252          when 'd' =>
253             if not Has_Colors then
254                Cannot ("does not support color.");
255             elsif not Can_Change_Color then
256                Cannot ("has hardwired color values.");
257             else
258                color_edit;
259             end if;
260          when 'e' =>
261             slk_test;
262          when 'f' =>
263             acs_display;
264          when 'o' =>
265             demo_panels (nap_msec);
266          when 'g' =>
267             acs_and_scroll;
268          when 'i' =>
269             flushinp_test (Standard_Window);
270          when 'k' =>
271             test_sgr_attributes;
272          when 'm' =>
273             menu_test;
274          when 'p' =>
275             demo_pad;
276          when 'r' =>
277             demo_forms;
278          when 's' =>
279             overlap_test;
280          when 't' =>
281             trace_set;
282          when '?' =>
283             null;
284          when others => return False;
285       end case;
286       return True;
287    end Do_Single_Test;
288
289    command : Character;
290    my_e_param : Soft_Label_Key_Format := Four_Four;
291    assumed_colors : Boolean := False;
292    default_colors : Boolean := False;
293    default_fg : Color_Number := White;
294    default_bg : Color_Number := Black;
295    --  nap_msec was an unsigned long integer in the C version,
296    --  yet napms only takes an int!
297
298    c : Integer;
299    c2 : Character;
300    optind : Integer := 1; -- must be initialized to one.
301    optarg : getopt.stringa;
302
303    length : Integer;
304    tmpi : Integer;
305
306    package myio is new Ada.Text_IO.Integer_IO (Integer);
307    use myio;
308
309    save_trace : Integer := 0;
310    save_trace_set : Trace_Attribute_Set;
311
312    function main return Integer is
313    begin
314       loop
315          Qgetopt (c, Argument_Count, Argument'Access,
316                   "a:de:fhs:t:", optind, optarg);
317          exit when c = -1;
318          c2 := Character'Val (c);
319          case c2 is
320             when 'a' =>
321                --  Ada doesn't have scanf, it doesn't even have a
322                --  regular expression library.
323                assumed_colors := True;
324                myio.Get (optarg.all, Integer (default_fg), length);
325                myio.Get (optarg.all (length + 2 .. optarg.all'Length),
326                          Integer (default_bg), length);
327             when 'd' =>
328                default_colors := True;
329             when 'e' =>
330                myio.Get (optarg.all, tmpi, length);
331                if tmpi > 3 then
332                   usage;
333                   return 1;
334                end if;
335                my_e_param := Soft_Label_Key_Format'Val (tmpi);
336             when 'f' =>
337                Rip_Off_Lines (-1, rip_footer'Access);
338             when 'h' =>
339                Rip_Off_Lines (1, rip_header'Access);
340             when 's' =>
341                myio.Get (optarg.all, nap_msec, length);
342             when 't' =>
343                myio.Get (optarg.all, save_trace, length);
344             when others =>
345                usage;
346                return 1;
347          end case;
348       end loop;
349
350       --  the C version had a bunch of macros here.
351
352       --   if (!isatty(fileno(stdin)))
353       --   isatty is not available in the standard Ada so skip it.
354       save_trace_set := To_trace (save_trace);
355       Trace_On (save_trace_set);
356
357       Init_Soft_Label_Keys (my_e_param);
358
359       Init_Screen;
360       Set_Background (Ch => (Ch    => Blank,
361                              Attr  => Normal_Video,
362                              Color => Color_Pair'First));
363
364       if Has_Colors then
365          Start_Color;
366          if default_colors then
367             Use_Default_Colors;
368          elsif assumed_colors then
369             Assume_Default_Colors (default_fg, default_bg);
370          end if;
371       end if;
372
373       Set_Terminal_Modes;
374       Save_Curses_Mode (Curses);
375
376       End_Windows;
377
378       --  TODO add macro #if blocks.
379       Put_Line ("Welcome to " & Curses_Version & ".  Press ? for help.");
380
381       loop
382          Put_Line ("This is the ncurses main menu");
383          Put_Line ("a = keyboard and mouse input test");
384          Put_Line ("b = character attribute test");
385          Put_Line ("c = color test pattern");
386          Put_Line ("d = edit RGB color values");
387          Put_Line ("e = exercise soft keys");
388          Put_Line ("f = display ACS characters");
389          Put_Line ("g = display windows and scrolling");
390          Put_Line ("i = test of flushinp()");
391          Put_Line ("k = display character attributes");
392          Put_Line ("m = menu code test");
393          Put_Line ("o = exercise panels library");
394          Put_Line ("p = exercise pad features");
395          Put_Line ("q = quit");
396          Put_Line ("r = exercise forms code");
397          Put_Line ("s = overlapping-refresh test");
398          Put_Line ("t = set trace level");
399          Put_Line ("? = repeat this command summary");
400
401          Put ("> ");
402          Flush;
403
404          command := Ada.Characters.Latin_1.NUL;
405          --              get_input:
406          --              loop
407          declare
408             Ch : Character;
409          begin
410             Get (Ch);
411             --  TODO if read(ch) <= 0
412             --  TODO ada doesn't have an Is_Space function
413             command := Ch;
414             --  TODO if ch = '\n' or '\r' are these in Ada?
415          end;
416          --              end loop get_input;
417
418          declare
419          begin
420             if Do_Single_Test (command) then
421                Flush_Input;
422                Set_Terminal_Modes;
423                Reset_Curses_Mode (Curses);
424                Clear;
425                Refresh;
426                End_Windows;
427                if command = '?' then
428                   Put_Line ("This is the ncurses capability tester.");
429                   Put_Line ("You may select a test from the main menu by " &
430                             "typing the");
431                   Put_Line ("key letter of the choice (the letter to left " &
432                             "of the =)");
433                   Put_Line ("at the > prompt.  The commands `x' or `q' will " &
434                             "exit.");
435                end if;
436                --  continue; --why continue in the C version?
437             end if;
438          exception
439             when Curses_Exception => End_Windows;
440          end;
441
442          exit when command = 'q';
443       end loop;
444       Curses_Free_All;
445       return 0; -- TODO ExitProgram(EXIT_SUCCESS);
446    end main;
447
448 end ncurses2.m;