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