1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000-2008,2018 Free Software Foundation, Inc. --
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: --
20 -- The above copyright notice and this permission notice shall be included --
21 -- in all copies or substantial portions of the Software. --
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. --
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 --
35 ------------------------------------------------------------------------------
36 -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
39 -- $Date: 2018/07/07 23:33:16 $
40 -- Binding Version 01.00
41 ------------------------------------------------------------------------------
42 -- TODO use Default_Character where appropriate
44 -- This is an Ada version of ncurses
45 -- I translated this because it tests the most features.
47 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
48 with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
50 with Ada.Text_IO; use Ada.Text_IO;
52 with Ada.Characters.Latin_1;
54 with Ada.Command_Line; use Ada.Command_Line;
56 with Ada.Strings.Unbounded;
58 with ncurses2.util; use ncurses2.util;
59 with ncurses2.getch_test;
60 with ncurses2.attr_test;
61 with ncurses2.color_test;
62 with ncurses2.demo_panels;
63 with ncurses2.color_edit;
64 with ncurses2.slk_test;
65 with ncurses2.acs_display;
66 with ncurses2.acs_and_scroll;
67 with ncurses2.flushinp_test;
68 with ncurses2.test_sgr_attributes;
69 with ncurses2.menu_test;
70 with ncurses2.demo_pad;
71 with ncurses2.demo_forms;
72 with ncurses2.overlap_test;
73 with ncurses2.trace_set;
75 with ncurses2.getopt; use ncurses2.getopt;
77 package body ncurses2.m is
79 function To_trace (n : Integer) return Trace_Attribute_Set;
81 procedure Set_Terminal_Modes;
82 function Do_Single_Test (c : Character) return Boolean;
84 function To_trace (n : Integer) return Trace_Attribute_Set is
85 a : Trace_Attribute_Set := (others => False);
107 a.Cursor_Move := True;
112 a.Character_Output := True;
122 a.Virtual_Puts := True;
127 a.Input_Events := True;
137 a.Internal_Calls := True;
142 a.Character_Calls := True;
147 a.Termcap_TermInfo := True;
153 -- these are type Stdscr_Init_Proc;
155 function rip_footer (
157 Columns : Column_Count) return Integer;
158 pragma Convention (C, rip_footer);
160 function rip_footer (
162 Columns : Column_Count) return Integer is
164 Set_Background (Win, (Ch => ' ',
165 Attr => (Reverse_Video => True, others => False),
168 Move_Cursor (Win, 0, 0);
169 Add (Win, "footer:" & Columns'Img & " columns");
170 Refresh_Without_Update (Win);
171 return 0; -- Curses_OK;
174 function rip_header (
176 Columns : Column_Count) return Integer;
177 pragma Convention (C, rip_header);
179 function rip_header (
181 Columns : Column_Count) return Integer is
183 Set_Background (Win, (Ch => ' ',
184 Attr => (Reverse_Video => True, others => False),
187 Move_Cursor (Win, 0, 0);
188 Add (Win, "header:" & Columns'Img & " columns");
189 -- 'Img is a GNAT extention
190 Refresh_Without_Update (Win);
191 return 0; -- Curses_OK;
195 -- type Stringa is access String;
196 use Ada.Strings.Unbounded;
197 -- tbl : constant array (Positive range <>) of Stringa := (
198 tbl : constant array (Positive range <>) of Unbounded_String
200 To_Unbounded_String ("Usage: ncurses [options]"),
201 To_Unbounded_String (""),
202 To_Unbounded_String ("Options:"),
203 To_Unbounded_String (" -a f,b set default-colors " &
204 "(assumed white-on-black)"),
205 To_Unbounded_String (" -d use default-colors if terminal " &
207 To_Unbounded_String (" -e fmt specify format for soft-keys " &
209 To_Unbounded_String (" -f rip-off footer line " &
211 To_Unbounded_String (" -h rip-off header line " &
213 To_Unbounded_String (" -s msec specify nominal time for " &
214 "panel-demo (default: 1, to hold)"),
215 To_Unbounded_String (" -t mask specify default trace-level " &
216 "(may toggle with ^T)")
219 for n in tbl'Range loop
220 Put_Line (Standard_Error, To_String (tbl (n)));
222 -- exit(EXIT_FAILURE);
223 -- TODO should we use Set_Exit_Status and throw and exception?
226 procedure Set_Terminal_Modes is begin
227 Set_Raw_Mode (SwitchOn => False);
228 Set_Cbreak_Mode (SwitchOn => True);
229 Set_Echo_Mode (SwitchOn => False);
230 Allow_Scrolling (Mode => True);
231 Use_Insert_Delete_Line (Do_Idl => True);
232 Set_KeyPad_Mode (SwitchOn => True);
233 end Set_Terminal_Modes;
235 nap_msec : Integer := 1;
237 function Do_Single_Test (c : Character) return Boolean is
245 if not Has_Colors then
246 Cannot ("does not support color.");
251 if not Has_Colors then
252 Cannot ("does not support color.");
253 elsif not Can_Change_Color then
254 Cannot ("has hardwired color values.");
263 demo_panels (nap_msec);
267 flushinp_test (Standard_Window);
282 when others => return False;
288 my_e_param : Soft_Label_Key_Format := Four_Four;
289 assumed_colors : Boolean := False;
290 default_colors : Boolean := False;
291 default_fg : Color_Number := White;
292 default_bg : Color_Number := Black;
293 -- nap_msec was an unsigned long integer in the C version,
294 -- yet napms only takes an int!
298 optind : Integer := 1; -- must be initialized to one.
299 optarg : getopt.stringa;
304 package myio is new Ada.Text_IO.Integer_IO (Integer);
306 save_trace : Integer := 0;
307 save_trace_set : Trace_Attribute_Set;
309 function main return Integer is
312 Qgetopt (c, Argument_Count, Argument'Access,
313 "a:de:fhs:t:", optind, optarg);
315 c2 := Character'Val (c);
318 -- Ada doesn't have scanf, it doesn't even have a
319 -- regular expression library.
320 assumed_colors := True;
321 myio.Get (optarg.all, Integer (default_fg), length);
322 myio.Get (optarg.all (length + 2 .. optarg.all'Length),
323 Integer (default_bg), length);
325 default_colors := True;
327 myio.Get (optarg.all, tmpi, length);
332 my_e_param := Soft_Label_Key_Format'Val (tmpi);
334 Rip_Off_Lines (-1, rip_footer'Access);
336 Rip_Off_Lines (1, rip_header'Access);
338 myio.Get (optarg.all, nap_msec, length);
340 myio.Get (optarg.all, save_trace, length);
347 -- the C version had a bunch of macros here.
349 -- if (!isatty(fileno(stdin)))
350 -- isatty is not available in the standard Ada so skip it.
351 save_trace_set := To_trace (save_trace);
352 Trace_On (save_trace_set);
354 Init_Soft_Label_Keys (my_e_param);
357 Set_Background (Ch => (Ch => Blank,
358 Attr => Normal_Video,
359 Color => Color_Pair'First));
363 if default_colors then
365 elsif assumed_colors then
366 Assume_Default_Colors (default_fg, default_bg);
371 Save_Curses_Mode (Curses);
375 -- TODO add macro #if blocks.
376 Put_Line ("Welcome to " & Curses_Version & ". Press ? for help.");
379 Put_Line ("This is the ncurses main menu");
380 Put_Line ("a = keyboard and mouse input test");
381 Put_Line ("b = character attribute test");
382 Put_Line ("c = color test pattern");
383 Put_Line ("d = edit RGB color values");
384 Put_Line ("e = exercise soft keys");
385 Put_Line ("f = display ACS characters");
386 Put_Line ("g = display windows and scrolling");
387 Put_Line ("i = test of flushinp()");
388 Put_Line ("k = display character attributes");
389 Put_Line ("m = menu code test");
390 Put_Line ("o = exercise panels library");
391 Put_Line ("p = exercise pad features");
392 Put_Line ("q = quit");
393 Put_Line ("r = exercise forms code");
394 Put_Line ("s = overlapping-refresh test");
395 Put_Line ("t = set trace level");
396 Put_Line ("? = repeat this command summary");
401 command := Ada.Characters.Latin_1.NUL;
408 -- TODO if read(ch) <= 0
409 -- TODO ada doesn't have an Is_Space function
411 -- TODO if ch = '\n' or '\r' are these in Ada?
413 -- end loop get_input;
417 if Do_Single_Test (command) then
420 Reset_Curses_Mode (Curses);
424 if command = '?' then
425 Put_Line ("This is the ncurses capability tester.");
426 Put_Line ("You may select a test from the main menu by " &
428 Put_Line ("key letter of the choice (the letter to left " &
430 Put_Line ("at the > prompt. The commands `x' or `q' will " &
433 -- continue; --why continue in the C version?
436 when Curses_Exception => End_Windows;
439 exit when command = 'q';
442 return 0; -- TODO ExitProgram(EXIT_SUCCESS);