1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright 2018,2020 Thomas E. Dickey --
11 -- Copyright 2000-2007,2008 Free Software Foundation, Inc. --
13 -- Permission is hereby granted, free of charge, to any person obtaining a --
14 -- copy of this software and associated documentation files (the --
15 -- "Software"), to deal in the Software without restriction, including --
16 -- without limitation the rights to use, copy, modify, merge, publish, --
17 -- distribute, distribute with modifications, sublicense, and/or sell --
18 -- copies of the Software, and to permit persons to whom the Software is --
19 -- furnished to do so, subject to the following conditions: --
21 -- The above copyright notice and this permission notice shall be included --
22 -- in all copies or substantial portions of the Software. --
24 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
25 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
26 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
27 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
28 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
29 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
30 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
32 -- Except as contained in this notice, the name(s) of the above copyright --
33 -- holders shall not be used in advertising or otherwise to promote the --
34 -- sale, use or other dealings in this Software without prior written --
36 ------------------------------------------------------------------------------
37 -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
40 -- $Date: 2020/02/02 23:34:34 $
41 -- Binding Version 01.00
42 ------------------------------------------------------------------------------
43 -- TODO use Default_Character where appropriate
45 -- This is an Ada version of ncurses
46 -- I translated this because it tests the most features.
48 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
49 with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
51 with Ada.Text_IO; use Ada.Text_IO;
53 with Ada.Characters.Latin_1;
55 with Ada.Command_Line; use Ada.Command_Line;
57 with Ada.Strings.Unbounded;
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;
76 with ncurses2.getopt; use ncurses2.getopt;
78 package body ncurses2.m is
80 function To_trace (n : Integer) return Trace_Attribute_Set;
82 procedure Set_Terminal_Modes;
83 function Do_Single_Test (c : Character) return Boolean;
85 function To_trace (n : Integer) return Trace_Attribute_Set is
86 a : Trace_Attribute_Set := (others => False);
108 a.Cursor_Move := True;
113 a.Character_Output := True;
123 a.Virtual_Puts := True;
128 a.Input_Events := True;
138 a.Internal_Calls := True;
143 a.Character_Calls := True;
148 a.Termcap_TermInfo := True;
154 -- these are type Stdscr_Init_Proc;
156 function rip_footer (
158 Columns : Column_Count) return Integer;
159 pragma Convention (C, rip_footer);
161 function rip_footer (
163 Columns : Column_Count) return Integer is
165 Set_Background (Win, (Ch => ' ',
166 Attr => (Reverse_Video => True, others => False),
169 Move_Cursor (Win, 0, 0);
170 Add (Win, "footer:" & Columns'Img & " columns");
171 Refresh_Without_Update (Win);
172 return 0; -- Curses_OK;
175 function rip_header (
177 Columns : Column_Count) return Integer;
178 pragma Convention (C, rip_header);
180 function rip_header (
182 Columns : Column_Count) return Integer is
184 Set_Background (Win, (Ch => ' ',
185 Attr => (Reverse_Video => True, others => False),
188 Move_Cursor (Win, 0, 0);
189 Add (Win, "header:" & Columns'Img & " columns");
190 -- 'Img is a GNAT extension
191 Refresh_Without_Update (Win);
192 return 0; -- Curses_OK;
196 -- type Stringa is access String;
197 use Ada.Strings.Unbounded;
198 -- tbl : constant array (Positive range <>) of Stringa := (
199 tbl : constant array (Positive range <>) of Unbounded_String
201 To_Unbounded_String ("Usage: ncurses [options]"),
202 To_Unbounded_String (""),
203 To_Unbounded_String ("Options:"),
204 To_Unbounded_String (" -a f,b set default-colors " &
205 "(assumed white-on-black)"),
206 To_Unbounded_String (" -d use default-colors if terminal " &
208 To_Unbounded_String (" -e fmt specify format for soft-keys " &
210 To_Unbounded_String (" -f rip-off footer line " &
212 To_Unbounded_String (" -h rip-off header line " &
214 To_Unbounded_String (" -s msec specify nominal time for " &
215 "panel-demo (default: 1, to hold)"),
216 To_Unbounded_String (" -t mask specify default trace-level " &
217 "(may toggle with ^T)")
220 for n in tbl'Range loop
221 Put_Line (Standard_Error, To_String (tbl (n)));
223 -- exit(EXIT_FAILURE);
224 -- TODO should we use Set_Exit_Status and throw and exception?
227 procedure Set_Terminal_Modes is begin
228 Set_Raw_Mode (SwitchOn => False);
229 Set_Cbreak_Mode (SwitchOn => True);
230 Set_Echo_Mode (SwitchOn => False);
231 Allow_Scrolling (Mode => True);
232 Use_Insert_Delete_Line (Do_Idl => True);
233 Set_KeyPad_Mode (SwitchOn => True);
234 end Set_Terminal_Modes;
236 nap_msec : Integer := 1;
238 function Do_Single_Test (c : Character) return Boolean is
246 if not Has_Colors then
247 Cannot ("does not support color.");
252 if not Has_Colors then
253 Cannot ("does not support color.");
254 elsif not Can_Change_Color then
255 Cannot ("has hardwired color values.");
264 demo_panels (nap_msec);
268 flushinp_test (Standard_Window);
283 when others => return False;
289 my_e_param : Soft_Label_Key_Format := Four_Four;
290 assumed_colors : Boolean := False;
291 default_colors : Boolean := False;
292 default_fg : Color_Number := White;
293 default_bg : Color_Number := Black;
294 -- nap_msec was an unsigned long integer in the C version,
295 -- yet napms only takes an int!
299 optind : Integer := 1; -- must be initialized to one.
300 optarg : getopt.stringa;
305 package myio is new Ada.Text_IO.Integer_IO (Integer);
307 save_trace : Integer := 0;
308 save_trace_set : Trace_Attribute_Set;
310 function main return Integer is
313 Qgetopt (c, Argument_Count, Argument'Access,
314 "a:de:fhs:t:", optind, optarg);
316 c2 := Character'Val (c);
319 -- Ada doesn't have scanf, it doesn't even have a
320 -- regular expression library.
321 assumed_colors := True;
322 myio.Get (optarg.all, Integer (default_fg), length);
323 myio.Get (optarg.all (length + 2 .. optarg.all'Length),
324 Integer (default_bg), length);
326 default_colors := True;
328 myio.Get (optarg.all, tmpi, length);
333 my_e_param := Soft_Label_Key_Format'Val (tmpi);
335 Rip_Off_Lines (-1, rip_footer'Access);
337 Rip_Off_Lines (1, rip_header'Access);
339 myio.Get (optarg.all, nap_msec, length);
341 myio.Get (optarg.all, save_trace, length);
348 -- the C version had a bunch of macros here.
350 -- if (!isatty(fileno(stdin)))
351 -- isatty is not available in the standard Ada so skip it.
352 save_trace_set := To_trace (save_trace);
353 Trace_On (save_trace_set);
355 Init_Soft_Label_Keys (my_e_param);
358 Set_Background (Ch => (Ch => Blank,
359 Attr => Normal_Video,
360 Color => Color_Pair'First));
364 if default_colors then
366 elsif assumed_colors then
367 Assume_Default_Colors (default_fg, default_bg);
372 Save_Curses_Mode (Curses);
376 -- TODO add macro #if blocks.
377 Put_Line ("Welcome to " & Curses_Version & ". Press ? for help.");
380 Put_Line ("This is the ncurses main menu");
381 Put_Line ("a = keyboard and mouse input test");
382 Put_Line ("b = character attribute test");
383 Put_Line ("c = color test pattern");
384 Put_Line ("d = edit RGB color values");
385 Put_Line ("e = exercise soft keys");
386 Put_Line ("f = display ACS characters");
387 Put_Line ("g = display windows and scrolling");
388 Put_Line ("i = test of flushinp()");
389 Put_Line ("k = display character attributes");
390 Put_Line ("m = menu code test");
391 Put_Line ("o = exercise panels library");
392 Put_Line ("p = exercise pad features");
393 Put_Line ("q = quit");
394 Put_Line ("r = exercise forms code");
395 Put_Line ("s = overlapping-refresh test");
396 Put_Line ("t = set trace level");
397 Put_Line ("? = repeat this command summary");
402 command := Ada.Characters.Latin_1.NUL;
409 -- TODO if read(ch) <= 0
410 -- TODO ada doesn't have an Is_Space function
412 -- TODO if ch = '\n' or '\r' are these in Ada?
414 -- end loop get_input;
418 if Do_Single_Test (command) then
421 Reset_Curses_Mode (Curses);
425 if command = '?' then
426 Put_Line ("This is the ncurses capability tester.");
427 Put_Line ("You may select a test from the main menu by " &
429 Put_Line ("key letter of the choice (the letter to left " &
431 Put_Line ("at the > prompt. The commands `x' or `q' will " &
434 -- continue; --why continue in the C version?
437 when Curses_Exception => End_Windows;
440 exit when command = 'q';
443 return 0; -- TODO ExitProgram(EXIT_SUCCESS);