]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/ncurses2-util.adb
3e88ea8b001ccf49583ec77faf982c222ef6a398
[ncurses.git] / Ada95 / samples / ncurses2-util.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                               ncurses2.util                              --
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 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
43
44 with Ada.Text_IO;
45
46 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
47 pragma Warnings (Off);
48 with Terminal_Interface.Curses.Aux;
49 pragma Warnings (On);
50
51 with Terminal_Interface.Curses.Trace; use Terminal_Interface.Curses.Trace;
52
53 with Ada.Text_IO; use Ada.Text_IO;
54
55 with Interfaces.C;
56 with Interfaces.C.Strings;
57
58 with Ada.Characters.Handling;
59
60 with ncurses2.genericPuts;
61
62
63 package body ncurses2.util is
64
65    --  #defines from C
66    --  #define CTRL(x)         ((x) & 0x1f)
67    function CTRL (c : Character) return Key_Code is
68    begin
69       return Character'Pos (c) mod 16#20#;
70       --  uses a property of ASCII
71       --  A = 16#41#; a = 16#61#; ^A = 1 or 16#1#
72    end CTRL;
73
74    function CTRL (c : Character) return Character is
75    begin
76       return Character'Val (Character'Pos (c) mod 16#20#);
77       --  uses a property of ASCII
78       --  A = 16#41#; a = 16#61#; ^A = 1 or 16#1#
79    end CTRL;
80
81    save_trace : Trace_Attribute_Set;
82    --  Common function to allow ^T to toggle trace-mode in the middle of a test
83    --  so that trace-files can be made smaller.
84    function Getchar (win : Window := Standard_Window) return Key_Code is
85       c : Key_Code;
86    begin
87       --  #ifdef TRACE
88       c := Get_Keystroke (win);
89       while c = CTRL ('T') loop
90          --  if _nc_tracing  in C
91          if Current_Trace_Setting /= Trace_Disable then
92             save_trace := Current_Trace_Setting;
93             Trace_Put ("TOGGLE-TRACING OFF");
94             Current_Trace_Setting := Trace_Disable;
95          else
96             Current_Trace_Setting := save_trace;
97          end if;
98          Trace_On (Current_Trace_Setting);
99          if Current_Trace_Setting /= Trace_Disable then
100             Trace_Put ("TOGGLE-TRACING ON");
101          end if;
102       end loop;
103       --  #else c := Get_Keystroke;
104       return c;
105    end Getchar;
106
107    procedure Getchar (win : Window := Standard_Window) is
108    begin
109       if Getchar (win) < 0 then
110          Beep;
111       end if;
112    end Getchar;
113
114
115    procedure Pause is
116    begin
117       Move_Cursor (Line => Lines - 1, Column => 0);
118       Add (Str => "Press any key to continue... ");
119       Getchar;
120    end Pause;
121
122
123    procedure Cannot (s : String) is
124       use Interfaces.C;
125       use Interfaces.C.Strings;
126       use Terminal_Interface.Curses.Aux;
127       function getenv (x : char_array)  return chars_ptr;
128       pragma Import (C, getenv, "getenv");
129       tmp1 : char_array (0 .. 10);
130       package p is new ncurses2.genericPuts (1024);
131       use p;
132       use p.BS;
133
134       tmpb : BS.Bounded_String;
135
136       Length : size_t;
137    begin
138       To_C ("TERM", tmp1, Length);
139       Fill_String (getenv (tmp1), tmpb);
140       Add (Ch => newl);
141       myAdd (Str => "This " & tmpb & " terminal " & s);
142       Pause;
143    end Cannot;
144
145    procedure ShellOut (message : Boolean) is
146       use Interfaces.C;
147       Txt : char_array (0 .. 10);
148       Length : size_t;
149       procedure system (x : char_array);
150       pragma Import (C, system, "system");
151    begin
152       To_C ("sh", Txt,  Length);
153       if message then
154          Add (Str => "Shelling out...");
155       end if;
156       Save_Curses_Mode (Mode => Curses);
157       End_Windows;
158       system (Txt);
159       if message then
160          Add (Str => "returned from shellout.");
161          Add (Ch => newl);
162       end if;
163       Refresh;
164    end ShellOut;
165
166
167
168    function Is_Digit (c : Key_Code) return Boolean is
169    begin
170       if c >= 16#100# then
171          return False;
172       else
173          return Ada.Characters.Handling.Is_Digit (Character'Val (c));
174       end if;
175    end Is_Digit;
176
177    procedure P (s : String) is
178    begin
179       Add (Str => s);
180       Add (Ch => newl);
181    end P;
182
183
184    function Code_To_Char (c : Key_Code) return Character is
185    begin
186       if c > Character'Pos (Character'Last) then
187          return Character'Val (0);
188          --  maybe raise exception?
189       else
190          return Character'Val (c);
191       end if;
192    end Code_To_Char;
193
194    --  This was untestable due to a bug in GNAT (3.12p)
195    --  Hmm, what bug? I don't remember.
196    function ctoi (c : Character) return Integer is
197    begin
198       return Character'Pos (c) - Character'Pos ('0');
199    end ctoi;
200
201 end ncurses2.util;