aboutsummaryrefslogtreecommitdiff
path: root/Ada95/src/terminal_interface-curses-text_io.adb
blob: 4b29514efbf27dce216be5f43ad7cb160cd94232 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
------------------------------------------------------------------------------
--                                                                          --
--                           GNAT ncurses Binding                           --
--                                                                          --
--                     Terminal_Interface.Curses.Text_IO                    --
--                                                                          --
--                                 B O D Y                                  --
--                                                                          --
------------------------------------------------------------------------------
-- Copyright (c) 1998-2006,2009 Free Software Foundation, Inc.              --
--                                                                          --
-- Permission is hereby granted, free of charge, to any person obtaining a  --
-- copy of this software and associated documentation files (the            --
-- "Software"), to deal in the Software without restriction, including      --
-- without limitation the rights to use, copy, modify, merge, publish,      --
-- distribute, distribute with modifications, sublicense, and/or sell       --
-- copies of the Software, and to permit persons to whom the Software is    --
-- furnished to do so, subject to the following conditions:                 --
--                                                                          --
-- The above copyright notice and this permission notice shall be included  --
-- in all copies or substantial portions of the Software.                   --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
-- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
-- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
--                                                                          --
-- Except as contained in this notice, the name(s) of the above copyright   --
-- holders shall not be used in advertising or otherwise to promote the     --
-- sale, use or other dealings in this Software without prior written       --
-- authorization.                                                           --
------------------------------------------------------------------------------
--  Author:  Juergen Pfeifer, 1996
--  Version Control:
--  $Revision: 1.19 $
--  $Date: 2009/12/26 17:40:46 $
--  Binding Version 01.00
------------------------------------------------------------------------------
package body Terminal_Interface.Curses.Text_IO is

   Default_Window : Window := Null_Window;

   procedure Set_Window (Win : Window)
   is
   begin
      Default_Window := Win;
   end Set_Window;

   function Get_Window return Window
   is
   begin
      if Default_Window = Null_Window then
         return Standard_Window;
      else
         return Default_Window;
      end if;
   end Get_Window;
   pragma Inline (Get_Window);

   procedure Flush (Win : Window)
   is
   begin
      Refresh (Win);
   end Flush;

   procedure Flush
   is
   begin
      Flush (Get_Window);
   end Flush;

   --------------------------------------------
   -- Specification of line and page lengths --
   --------------------------------------------

   --  There are no set routines in this package. I assume, that you allocate
   --  the window with an appropriate size.
   --  A scroll-window is interpreted as an page with unbounded page length,
   --  i.e. it returns the conventional 0 as page length.

   function Line_Length (Win : Window) return Count
   is
      N_Lines : Line_Count;
      N_Cols  : Column_Count;
   begin
      Get_Size (Win, N_Lines, N_Cols);
      --  if Natural (N_Cols) > Natural (Count'Last) then
      --     raise Layout_Error;
      --  end if;
      return Count (N_Cols);
   end Line_Length;

   function Line_Length return Count
   is
   begin
      return Line_Length (Get_Window);
   end Line_Length;

   function Page_Length (Win : Window) return Count
   is
      N_Lines : Line_Count;
      N_Cols  : Column_Count;
   begin
      if Scrolling_Allowed (Win) then
         return 0;
      else
         Get_Size (Win, N_Lines, N_Cols);
         --  if Natural (N_Lines) > Natural (Count'Last) then
         --     raise Layout_Error;
         --  end if;
         return Count (N_Lines);
      end if;
   end Page_Length;

   function Page_Length return Count
   is
   begin
      return Page_Length (Get_Window);
   end Page_Length;

   ------------------------------------
   -- Column, Line, and Page Control --
   ------------------------------------
   procedure New_Line (Win : Window; Spacing : Positive_Count := 1)
   is
      P_Size : constant Count := Page_Length (Win);
   begin
      if not Spacing'Valid then
         raise Constraint_Error;
      end if;

      for I in 1 .. Spacing loop
         if P_Size > 0 and then Line (Win) >= P_Size then
            New_Page (Win);
         else
            Add (Win, ASCII.LF);
         end if;
      end loop;
   end New_Line;

   procedure New_Line (Spacing : Positive_Count := 1)
   is
   begin
      New_Line (Get_Window, Spacing);
   end New_Line;

   procedure New_Page (Win : Window)
   is
   begin
      Clear (Win);
   end New_Page;

   procedure New_Page
   is
   begin
      New_Page (Get_Window);
   end New_Page;

   procedure Set_Col (Win : Window;  To : Positive_Count)
   is
      Y  : Line_Position;
      X1 : Column_Position;
      X2 : Column_Position;
      N  : Natural;
   begin
      if not To'Valid then
         raise Constraint_Error;
      end if;

      Get_Cursor_Position (Win, Y, X1);
      N  := Natural (To); N := N - 1;
      X2 := Column_Position (N);
      if X1 > X2 then
         New_Line (Win, 1);
         X1 := 0;
      end if;
      if X1 < X2 then
         declare
            Filler : constant String (Integer (X1) .. (Integer (X2) - 1))
              := (others => ' ');
         begin
            Put (Win, Filler);
         end;
      end if;
   end Set_Col;

   procedure Set_Col (To : Positive_Count)
   is
   begin
      Set_Col (Get_Window, To);
   end Set_Col;

   procedure Set_Line (Win : Window; To : Positive_Count)
   is
      Y1 : Line_Position;
      Y2 : Line_Position;
      X  : Column_Position;
      N  : Natural;
   begin
      if not To'Valid then
         raise Constraint_Error;
      end if;

      Get_Cursor_Position (Win, Y1, X);
      N  := Natural (To); N := N - 1;
      Y2 := Line_Position (N);
      if Y2 < Y1 then
         New_Page (Win);
         Y1 := 0;
      end if;
      if Y1 < Y2 then
         New_Line (Win, Positive_Count (Y2 - Y1));
      end if;
   end Set_Line;

   procedure Set_Line (To : Positive_Count)
   is
   begin
      Set_Line (Get_Window, To);
   end Set_Line;

   function Col (Win : Window) return Positive_Count
   is
      Y : Line_Position;
      X : Column_Position;
      N : Natural;
   begin
      Get_Cursor_Position (Win, Y, X);
      N := Natural (X); N := N + 1;
      --  if N > Natural (Count'Last) then
      --     raise Layout_Error;
      --  end if;
      return Positive_Count (N);
   end Col;

   function Col return Positive_Count
   is
   begin
      return Col (Get_Window);
   end Col;

   function Line (Win : Window) return Positive_Count
   is
      Y : Line_Position;
      X : Column_Position;
      N : Natural;
   begin
      Get_Cursor_Position (Win, Y, X);
      N := Natural (Y); N := N + 1;
      --  if N > Natural (Count'Last) then
      --     raise Layout_Error;
      --  end if;
      return Positive_Count (N);
   end Line;

   function Line return Positive_Count
   is
   begin
      return Line (Get_Window);
   end Line;

   -----------------------
   -- Characters Output --
   -----------------------

   procedure Put (Win  : Window; Item : Character)
   is
      P_Size : constant Count := Page_Length (Win);
      Y : Line_Position;
      X : Column_Position;
      L : Line_Count;
      C : Column_Count;
   begin
      if P_Size > 0 then
         Get_Cursor_Position (Win, Y, X);
         Get_Size (Win, L, C);
         if (Y + 1) = L and then (X + 1) = C then
            New_Page (Win);
         end if;
      end if;
      Add (Win, Item);
   end Put;

   procedure Put (Item : Character)
   is
   begin
      Put (Get_Window, Item);
   end Put;

   --------------------
   -- Strings-Output --
   --------------------

   procedure Put (Win  : Window; Item : String)
   is
      P_Size : constant Count := Page_Length (Win);
      Y : Line_Position;
      X : Column_Position;
      L : Line_Count;
      C : Column_Count;
   begin
      if P_Size > 0 then
         Get_Cursor_Position (Win, Y, X);
         Get_Size (Win, L, C);
         if (Y + 1) = L and then (X + 1 + Item'Length) >= C then
            New_Page (Win);
         end if;
      end if;
      Add (Win, Item);
   end Put;

   procedure Put (Item : String)
   is
   begin
      Put (Get_Window, Item);
   end Put;

   procedure Put_Line
     (Win  : Window;
      Item : String)
   is
   begin
      Put (Win, Item);
      New_Line (Win, 1);
   end Put_Line;

   procedure Put_Line
     (Item : String)
   is
   begin
      Put_Line (Get_Window, Item);
   end Put_Line;

end Terminal_Interface.Curses.Text_IO;