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