]> ncurses.scripts.mit.edu Git - ncurses.git/blob - terminal_interface-curses.adb
f196f3b5e1d3b70037b4e8485305184a1e0f4aeb
[ncurses.git] / terminal_interface-curses.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                           GNAT ncurses Binding                           --
4 --                                                                          --
5 --                        Terminal_Interface.Curses                         --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 1998 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: Juergen Pfeifer, 1996
37 --  Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en
38 --  Version Control:
39 --  $Revision: 1.28 $
40 --  Binding Version 01.00
41 ------------------------------------------------------------------------------
42 with System;
43
44 with Terminal_Interface.Curses.Aux;
45 with Interfaces.C;                  use Interfaces.C;
46 with Interfaces.C.Strings;          use Interfaces.C.Strings;
47 with Interfaces.C.Pointers;
48 with Ada.Characters.Handling;       use Ada.Characters.Handling;
49 with Ada.Strings.Fixed;
50 with Ada.Unchecked_Conversion;
51
52 package body Terminal_Interface.Curses is
53
54    use Aux;
55    use type System.Bit_Order;
56
57    package ASF renames Ada.Strings.Fixed;
58
59    type chtype_array is array (size_t range <>)
60       of aliased Attributed_Character;
61    pragma Convention (C, chtype_array);
62
63 ------------------------------------------------------------------------------
64    generic
65       type Element is (<>);
66    function W_Get_Element (Win    : in Window;
67                            Offset : in Natural) return Element;
68
69    function W_Get_Element (Win    : in Window;
70                            Offset : in Natural) return Element is
71       type E_Array is array (Natural range <>) of aliased Element;
72       package C_E_Array is new
73         Interfaces.C.Pointers (Natural, Element, E_Array, Element'Val (0));
74       use C_E_Array;
75
76       function To_Pointer is new
77         Ada.Unchecked_Conversion (Window, Pointer);
78
79       P : Pointer := To_Pointer (Win);
80    begin
81       if Win = Null_Window then
82          raise Curses_Exception;
83       else
84          P := P + ptrdiff_t (Offset);
85          return P.all;
86       end if;
87    end W_Get_Element;
88
89    function W_Get_Int   is new W_Get_Element (C_Int);
90    function W_Get_Short is new W_Get_Element (C_Short);
91    function W_Get_Byte  is new W_Get_Element (Interfaces.C.unsigned_char);
92
93    function Get_Flag (Win    : Window;
94                       Offset : Natural) return Boolean;
95
96    function Get_Flag (Win    : Window;
97                       Offset : Natural) return Boolean
98    is
99       Res : C_Int;
100    begin
101       case Sizeof_bool is
102          when 1 => Res := C_Int (W_Get_Byte  (Win, Offset));
103          when 2 => Res := C_Int (W_Get_Short (Win, Offset));
104          when 4 => Res := C_Int (W_Get_Int   (Win, Offset));
105          when others => raise Curses_Exception;
106       end case;
107
108       case Res is
109          when 0       => return False;
110          when others  => return True;
111       end case;
112    end Get_Flag;
113
114 ------------------------------------------------------------------------------
115    function Key_Name (Key : in Real_Key_Code) return String
116    is
117       function Keyname (K : C_Int) return chars_ptr;
118       pragma Import (C, Keyname, "keyname");
119
120       Ch : Character;
121    begin
122       if Key <= Character'Pos (Character'Last) then
123          Ch := Character'Val (Key);
124          if Is_Control (Ch) then
125             return Un_Control (Attributed_Character'(Ch    => Ch,
126                                                      Color => Color_Pair'First,
127                                                      Attr  => Normal_Video));
128          elsif Is_Graphic (Ch) then
129             declare
130                S : String (1 .. 1);
131             begin
132                S (1) := Ch;
133                return S;
134             end;
135          else
136             return "";
137          end if;
138       else
139          return Fill_String (Keyname (C_Int (Key)));
140       end if;
141    end Key_Name;
142
143    procedure Key_Name (Key  : in  Real_Key_Code;
144                        Name : out String)
145    is
146    begin
147       ASF.Move (Key_Name (Key), Name);
148    end Key_Name;
149
150 ------------------------------------------------------------------------------
151    procedure Init_Screen
152    is
153       function Initscr return Window;
154       pragma Import (C, Initscr, "initscr");
155
156       W : Window;
157    begin
158       W := Initscr;
159       if W = Null_Window then
160          raise Curses_Exception;
161       end if;
162    end Init_Screen;
163
164    procedure End_Windows
165    is
166       function Endwin return C_Int;
167       pragma Import (C, Endwin, "endwin");
168    begin
169       if Endwin = Curses_Err then
170          raise Curses_Exception;
171       end if;
172    end End_Windows;
173
174    function Is_End_Window return Boolean
175    is
176       function Isendwin return Curses_Bool;
177       pragma Import (C, Isendwin, "isendwin");
178    begin
179       if Isendwin = Curses_Bool_False then
180          return False;
181       else
182          return True;
183       end if;
184    end Is_End_Window;
185 ------------------------------------------------------------------------------
186    procedure Move_Cursor (Win    : in Window := Standard_Window;
187                           Line   : in Line_Position;
188                           Column : in Column_Position)
189    is
190       function Wmove (Win    : Window;
191                       Line   : C_Int;
192                       Column : C_Int
193                      ) return C_Int;
194       pragma Import (C, Wmove, "wmove");
195    begin
196       if Wmove (Win, C_Int (Line), C_Int (Column)) = Curses_Err then
197          raise Curses_Exception;
198       end if;
199    end Move_Cursor;
200 ------------------------------------------------------------------------------
201    procedure Add (Win : in Window := Standard_Window;
202                   Ch  : in Attributed_Character)
203    is
204       function Waddch (W  : Window;
205                        Ch : C_Chtype) return C_Int;
206       pragma Import (C, Waddch, "waddch");
207    begin
208       if Waddch (Win, AttrChar_To_Chtype (Ch)) = Curses_Err then
209          raise Curses_Exception;
210       end if;
211    end Add;
212
213    procedure Add (Win : in Window := Standard_Window;
214                   Ch  : in Character)
215    is
216    begin
217       Add (Win,
218            Attributed_Character'(Ch    => Ch,
219                                  Color => Color_Pair'First,
220                                  Attr  => Normal_Video));
221    end Add;
222
223    procedure Add
224      (Win    : in Window := Standard_Window;
225       Line   : in Line_Position;
226       Column : in Column_Position;
227       Ch     : in Attributed_Character)
228    is
229       function mvwaddch (W  : Window;
230                          Y  : C_Int;
231                          X  : C_Int;
232                          Ch : C_Chtype) return C_Int;
233       pragma Import (C, mvwaddch, "mvwaddch");
234    begin
235       if mvwaddch (Win, C_Int (Line),
236                    C_Int (Column),
237                    AttrChar_To_Chtype (Ch)) = Curses_Err then
238          raise Curses_Exception;
239       end if;
240    end Add;
241
242    procedure Add
243      (Win    : in Window := Standard_Window;
244       Line   : in Line_Position;
245       Column : in Column_Position;
246       Ch     : in Character)
247    is
248    begin
249       Add (Win,
250            Line,
251            Column,
252            Attributed_Character'(Ch    => Ch,
253                                  Color => Color_Pair'First,
254                                  Attr  => Normal_Video));
255    end Add;
256
257    procedure Add_With_Immediate_Echo
258      (Win : in Window := Standard_Window;
259       Ch  : in Attributed_Character)
260    is
261       function Wechochar (W  : Window;
262                           Ch : C_Chtype) return C_Int;
263       pragma Import (C, Wechochar, "wechochar");
264    begin
265       if Wechochar (Win, AttrChar_To_Chtype (Ch)) = Curses_Err then
266          raise Curses_Exception;
267       end if;
268    end Add_With_Immediate_Echo;
269
270    procedure Add_With_Immediate_Echo
271      (Win : in Window := Standard_Window;
272       Ch  : in Character)
273    is
274    begin
275       Add_With_Immediate_Echo
276         (Win,
277          Attributed_Character'(Ch    => Ch,
278                                Color => Color_Pair'First,
279                                Attr  => Normal_Video));
280    end Add_With_Immediate_Echo;
281 ------------------------------------------------------------------------------
282    function Create (Number_Of_Lines       : Line_Count;
283                     Number_Of_Columns     : Column_Count;
284                     First_Line_Position   : Line_Position;
285                     First_Column_Position : Column_Position) return Window
286    is
287       function Newwin (Number_Of_Lines       : C_Int;
288                        Number_Of_Columns     : C_Int;
289                        First_Line_Position   : C_Int;
290                        First_Column_Position : C_Int) return Window;
291       pragma Import (C, Newwin, "newwin");
292
293       W : Window;
294    begin
295       W := Newwin (C_Int (Number_Of_Lines),
296                    C_Int (Number_Of_Columns),
297                    C_Int (First_Line_Position),
298                    C_Int (First_Column_Position));
299       if W = Null_Window then
300          raise Curses_Exception;
301       end if;
302       return W;
303    end Create;
304
305    procedure Delete (Win : in out Window)
306    is
307       function Wdelwin (W : Window) return C_Int;
308       pragma Import (C, Wdelwin, "delwin");
309    begin
310       if Wdelwin (Win) = Curses_Err then
311          raise Curses_Exception;
312       end if;
313       Win := Null_Window;
314    end Delete;
315
316    function Sub_Window
317      (Win                   : Window := Standard_Window;
318       Number_Of_Lines       : Line_Count;
319       Number_Of_Columns     : Column_Count;
320       First_Line_Position   : Line_Position;
321       First_Column_Position : Column_Position) return Window
322    is
323       function Subwin
324         (Win                   : Window;
325          Number_Of_Lines       : C_Int;
326          Number_Of_Columns     : C_Int;
327          First_Line_Position   : C_Int;
328          First_Column_Position : C_Int) return Window;
329       pragma Import (C, Subwin, "subwin");
330
331       W : Window;
332    begin
333       W := Subwin (Win,
334                    C_Int (Number_Of_Lines),
335                    C_Int (Number_Of_Columns),
336                    C_Int (First_Line_Position),
337                    C_Int (First_Column_Position));
338       if W = Null_Window then
339          raise Curses_Exception;
340       end if;
341       return W;
342    end Sub_Window;
343
344    function Derived_Window
345      (Win                   : Window := Standard_Window;
346       Number_Of_Lines       : Line_Count;
347       Number_Of_Columns     : Column_Count;
348       First_Line_Position   : Line_Position;
349       First_Column_Position : Column_Position) return Window
350    is
351       function Derwin
352         (Win                   : Window;
353          Number_Of_Lines       : C_Int;
354          Number_Of_Columns     : C_Int;
355          First_Line_Position   : C_Int;
356          First_Column_Position : C_Int) return Window;
357       pragma Import (C, Derwin, "derwin");
358
359       W : Window;
360    begin
361       W := Derwin (Win,
362                    C_Int (Number_Of_Lines),
363                    C_Int (Number_Of_Columns),
364                    C_Int (First_Line_Position),
365                    C_Int (First_Column_Position));
366       if W = Null_Window then
367          raise Curses_Exception;
368       end if;
369       return W;
370    end Derived_Window;
371
372    function Duplicate (Win : Window) return Window
373    is
374       function Dupwin (Win : Window) return Window;
375       pragma Import (C, Dupwin, "dupwin");
376
377       W : Window := Dupwin (Win);
378    begin
379       if W = Null_Window then
380          raise Curses_Exception;
381       end if;
382       return W;
383    end Duplicate;
384
385    procedure Move_Window (Win    : in Window;
386                           Line   : in Line_Position;
387                           Column : in Column_Position)
388    is
389       function Mvwin (Win    : Window;
390                       Line   : C_Int;
391                       Column : C_Int) return C_Int;
392       pragma Import (C, Mvwin, "mvwin");
393    begin
394       if Mvwin (Win, C_Int (Line), C_Int (Column)) = Curses_Err then
395          raise Curses_Exception;
396       end if;
397    end Move_Window;
398
399    procedure Move_Derived_Window (Win    : in Window;
400                                   Line   : in Line_Position;
401                                   Column : in Column_Position)
402    is
403       function Mvderwin (Win    : Window;
404                          Line   : C_Int;
405                          Column : C_Int) return C_Int;
406       pragma Import (C, Mvderwin, "mvderwin");
407    begin
408       if Mvderwin (Win, C_Int (Line), C_Int (Column)) = Curses_Err then
409          raise Curses_Exception;
410       end if;
411    end Move_Derived_Window;
412
413    procedure Set_Synch_Mode (Win  : in Window  := Standard_Window;
414                              Mode : in Boolean := False)
415    is
416       function Syncok (Win  : Window;
417                        Mode : Curses_Bool) return C_Int;
418       pragma Import (C, Syncok, "syncok");
419    begin
420       if Syncok (Win, Curses_Bool (Boolean'Pos (Mode))) = Curses_Err then
421          raise Curses_Exception;
422       end if;
423    end Set_Synch_Mode;
424 ------------------------------------------------------------------------------
425    procedure Add (Win : in Window := Standard_Window;
426                   Str : in String;
427                   Len : in Integer := -1)
428    is
429       function Waddnstr (Win : Window;
430                          Str : char_array;
431                          Len : C_Int := -1) return C_Int;
432       pragma Import (C, Waddnstr, "waddnstr");
433
434       Txt    : char_array (0 .. Str'Length);
435       Length : size_t;
436    begin
437       To_C (Str, Txt, Length);
438       if Waddnstr (Win, Txt, C_Int (Len)) = Curses_Err then
439          raise Curses_Exception;
440       end if;
441    end Add;
442
443    procedure Add
444      (Win    : in Window := Standard_Window;
445       Line   : in Line_Position;
446       Column : in Column_Position;
447       Str    : in String;
448       Len    : in Integer := -1)
449    is
450    begin
451       Move_Cursor (Win, Line, Column);
452       Add (Win, Str, Len);
453    end Add;
454 ------------------------------------------------------------------------------
455    procedure Add
456      (Win : in Window := Standard_Window;
457       Str : in Attributed_String;
458       Len : in Integer := -1)
459    is
460       function Waddchnstr (Win : Window;
461                            Str : chtype_array;
462                            Len : C_Int := -1) return C_Int;
463       pragma Import (C, Waddchnstr, "waddchnstr");
464
465       Txt : chtype_array (0 .. Str'Length);
466    begin
467       for Length in 1 .. size_t (Str'Length) loop
468          Txt (Length - 1) := Str (Natural (Length));
469       end loop;
470       Txt (Str'Length) := Default_Character;
471       if Waddchnstr (Win,
472                      Txt,
473                      C_Int (Len)) = Curses_Err then
474          raise Curses_Exception;
475       end if;
476    end Add;
477
478    procedure Add
479      (Win    : in Window := Standard_Window;
480       Line   : in Line_Position;
481       Column : in Column_Position;
482       Str    : in Attributed_String;
483       Len    : in Integer := -1)
484    is
485    begin
486       Move_Cursor (Win, Line, Column);
487       Add (Win, Str, Len);
488    end Add;
489 ------------------------------------------------------------------------------
490    procedure Border
491      (Win                       : in Window := Standard_Window;
492       Left_Side_Symbol          : in Attributed_Character := Default_Character;
493       Right_Side_Symbol         : in Attributed_Character := Default_Character;
494       Top_Side_Symbol           : in Attributed_Character := Default_Character;
495       Bottom_Side_Symbol        : in Attributed_Character := Default_Character;
496       Upper_Left_Corner_Symbol  : in Attributed_Character := Default_Character;
497       Upper_Right_Corner_Symbol : in Attributed_Character := Default_Character;
498       Lower_Left_Corner_Symbol  : in Attributed_Character := Default_Character;
499       Lower_Right_Corner_Symbol : in Attributed_Character := Default_Character)
500    is
501       function Wborder (W   : Window;
502                         LS  : C_Chtype;
503                         RS  : C_Chtype;
504                         TS  : C_Chtype;
505                         BS  : C_Chtype;
506                         ULC : C_Chtype;
507                         URC : C_Chtype;
508                         LLC : C_Chtype;
509                         LRC : C_Chtype) return C_Int;
510       pragma Import (C, Wborder, "wborder");
511    begin
512       if Wborder (Win,
513                   AttrChar_To_Chtype (Left_Side_Symbol),
514                   AttrChar_To_Chtype (Right_Side_Symbol),
515                   AttrChar_To_Chtype (Top_Side_Symbol),
516                   AttrChar_To_Chtype (Bottom_Side_Symbol),
517                   AttrChar_To_Chtype (Upper_Left_Corner_Symbol),
518                   AttrChar_To_Chtype (Upper_Right_Corner_Symbol),
519                   AttrChar_To_Chtype (Lower_Left_Corner_Symbol),
520                   AttrChar_To_Chtype (Lower_Right_Corner_Symbol)
521                   ) = Curses_Err
522       then
523          raise Curses_Exception;
524       end if;
525    end Border;
526
527    procedure Box
528      (Win               : in Window := Standard_Window;
529       Vertical_Symbol   : in Attributed_Character := Default_Character;
530       Horizontal_Symbol : in Attributed_Character := Default_Character)
531    is
532    begin
533       Border (Win,
534               Vertical_Symbol, Vertical_Symbol,
535               Horizontal_Symbol, Horizontal_Symbol);
536    end Box;
537
538    procedure Horizontal_Line
539      (Win         : in Window := Standard_Window;
540       Line_Size   : in Natural;
541       Line_Symbol : in Attributed_Character := Default_Character)
542    is
543       function Whline (W   : Window;
544                        Ch  : C_Chtype;
545                        Len : C_Int) return C_Int;
546       pragma Import (C, Whline, "whline");
547    begin
548       if Whline (Win,
549                  AttrChar_To_Chtype (Line_Symbol),
550                  C_Int (Line_Size)) = Curses_Err then
551          raise Curses_Exception;
552       end if;
553    end Horizontal_Line;
554
555    procedure Vertical_Line
556      (Win         : in Window := Standard_Window;
557       Line_Size   : in Natural;
558       Line_Symbol : in Attributed_Character := Default_Character)
559    is
560       function Wvline (W   : Window;
561                        Ch  : C_Chtype;
562                        Len : C_Int) return C_Int;
563       pragma Import (C, Wvline, "wvline");
564    begin
565       if Wvline (Win,
566                  AttrChar_To_Chtype (Line_Symbol),
567                  C_Int (Line_Size)) = Curses_Err then
568          raise Curses_Exception;
569       end if;
570    end Vertical_Line;
571
572 ------------------------------------------------------------------------------
573    function Get_Keystroke (Win : Window := Standard_Window)
574      return Real_Key_Code
575    is
576       function Wgetch (W : Window) return C_Int;
577       pragma Import (C, Wgetch, "wgetch");
578
579       C : constant C_Int := Wgetch (Win);
580    begin
581       if C = Curses_Err then
582          return Key_None;
583       else
584          return Real_Key_Code (C);
585       end if;
586    end Get_Keystroke;
587
588    procedure Undo_Keystroke (Key : in Real_Key_Code)
589    is
590       function Ungetch (Ch : C_Int) return C_Int;
591       pragma Import (C, Ungetch, "ungetch");
592    begin
593       if Ungetch (C_Int (Key)) = Curses_Err then
594          raise Curses_Exception;
595       end if;
596    end Undo_Keystroke;
597
598    function Has_Key (Key : Special_Key_Code) return Boolean
599    is
600       function Haskey (Key : C_Int) return C_Int;
601       pragma Import (C, Haskey, "has_key");
602    begin
603       if Haskey (C_Int (Key)) = Curses_False then
604          return False;
605       else
606          return True;
607       end if;
608    end Has_Key;
609
610    function Is_Function_Key (Key : Special_Key_Code) return Boolean
611    is
612       L : constant Special_Key_Code  := Special_Key_Code (Natural (Key_F0) +
613         Natural (Function_Key_Number'Last));
614    begin
615       if (Key >= Key_F0) and then (Key <= L) then
616          return True;
617       else
618          return False;
619       end if;
620    end Is_Function_Key;
621
622    function Function_Key (Key : Real_Key_Code)
623                           return Function_Key_Number
624    is
625    begin
626       if Is_Function_Key (Key) then
627          return Function_Key_Number (Key - Key_F0);
628       else
629          raise Constraint_Error;
630       end if;
631    end Function_Key;
632
633    function Function_Key_Code (Key : Function_Key_Number) return Real_Key_Code
634    is
635    begin
636       return Real_Key_Code (Natural (Key_F0) + Natural (Key));
637    end Function_Key_Code;
638 ------------------------------------------------------------------------------
639    procedure Standout (Win : Window  := Standard_Window;
640                        On  : Boolean := True)
641    is
642       function wstandout (Win : Window) return C_Int;
643       pragma Import (C, wstandout, "wstandout");
644       function wstandend (Win : Window) return C_Int;
645       pragma Import (C, wstandend, "wstandend");
646
647       Err : C_Int;
648    begin
649       if On then
650          Err := wstandout (Win);
651       else
652          Err := wstandend (Win);
653       end if;
654       if Err = Curses_Err then
655          raise Curses_Exception;
656       end if;
657    end Standout;
658
659    procedure Switch_Character_Attribute
660      (Win  : in Window := Standard_Window;
661       Attr : in Character_Attribute_Set := Normal_Video;
662       On   : in Boolean := True)
663    is
664       function Wattron (Win    : Window;
665                         C_Attr : C_AttrType) return C_Int;
666       pragma Import (C, Wattron, "wattr_on");
667       function Wattroff (Win    : Window;
668                          C_Attr : C_AttrType) return C_Int;
669       pragma Import (C, Wattroff, "wattr_off");
670       --  In Ada we use the On Boolean to control whether or not we want to
671       --  switch on or off the attributes in the set.
672       Err : C_Int;
673       AC  : constant Attributed_Character := (Ch    => Character'First,
674                                               Color => Color_Pair'First,
675                                               Attr  => Attr);
676    begin
677       if On then
678          Err := Wattron  (Win, AttrChar_To_AttrType (AC));
679       else
680          Err := Wattroff (Win, AttrChar_To_AttrType (AC));
681       end if;
682       if Err = Curses_Err then
683          raise Curses_Exception;
684       end if;
685    end Switch_Character_Attribute;
686
687    procedure Set_Character_Attributes
688      (Win   : in Window := Standard_Window;
689       Attr  : in Character_Attribute_Set := Normal_Video;
690       Color : in Color_Pair := Color_Pair'First)
691    is
692       function Wattrset (Win    : Window;
693                          C_Attr : C_AttrType) return C_Int;
694       pragma Import (C, Wattrset, "wattrset"); -- ??? wattr_set
695    begin
696       if Wattrset (Win,
697                    AttrChar_To_AttrType (Attributed_Character'
698                                          (Ch    => Character'First,
699                                           Color => Color,
700                                           Attr  => Attr))) = Curses_Err then
701          raise Curses_Exception;
702       end if;
703    end Set_Character_Attributes;
704
705    function Get_Character_Attribute (Win : Window := Standard_Window)
706                                      return Character_Attribute_Set
707    is
708       function Wattrget (Win : Window;
709                          Atr : access C_AttrType;
710                          Col : access C_Short;
711                          Opt : System.Address) return C_Int;
712       pragma Import (C, Wattrget, "wattr_get");
713
714       Attr : aliased C_AttrType;
715       Col  : aliased C_Short;
716       Res  : constant C_Int := Wattrget (Win, Attr'Access, Col'Access,
717                                          System.Null_Address);
718       Ch   : Attributed_Character;
719    begin
720       if Res = Curses_Ok then
721          Ch := AttrType_To_AttrChar (Attr);
722          return Ch.Attr;
723       else
724          raise Curses_Exception;
725       end if;
726    end Get_Character_Attribute;
727
728    function Get_Character_Attribute (Win : Window := Standard_Window)
729                                      return Color_Pair
730    is
731       function Wattrget (Win : Window;
732                          Atr : access C_AttrType;
733                          Col : access C_Short;
734                          Opt : System.Address) return C_Int;
735       pragma Import (C, Wattrget, "wattr_get");
736
737       Attr : aliased C_AttrType;
738       Col  : aliased C_Short;
739       Res  : constant C_Int := Wattrget (Win, Attr'Access, Col'Access,
740                                          System.Null_Address);
741       Ch   : Attributed_Character;
742    begin
743       if Res = Curses_Ok then
744          Ch := AttrType_To_AttrChar (Attr);
745          return Ch.Color;
746       else
747          raise Curses_Exception;
748       end if;
749    end Get_Character_Attribute;
750
751    procedure Set_Color (Win  : in Window := Standard_Window;
752                         Pair : in Color_Pair)
753    is
754       function Wset_Color (Win   : Window;
755                            Color : C_Short;
756                            Opts  : C_Void_Ptr) return C_Int;
757       pragma Import (C, Wset_Color, "wcolor_set");
758    begin
759       if Wset_Color (Win,
760                      C_Short (Pair),
761                      C_Void_Ptr (System.Null_Address)) = Curses_Err then
762          raise Curses_Exception;
763       end if;
764    end Set_Color;
765
766    procedure Change_Attributes
767      (Win   : in Window := Standard_Window;
768       Count : in Integer := -1;
769       Attr  : in Character_Attribute_Set := Normal_Video;
770       Color : in Color_Pair := Color_Pair'First)
771    is
772       function Wchgat (Win   : Window;
773                        Cnt   : C_Int;
774                        Attr  : C_AttrType;
775                        Color : C_Short;
776                        Opts  : System.Address := System.Null_Address)
777                        return C_Int;
778       pragma Import (C, Wchgat, "wchgat");
779
780       Ch : constant Attributed_Character :=
781         (Ch => Character'First, Color => Color_Pair'First, Attr => Attr);
782    begin
783       if Wchgat (Win, C_Int (Count), AttrChar_To_AttrType (Ch),
784                  C_Short (Color)) = Curses_Err then
785          raise Curses_Exception;
786       end if;
787    end Change_Attributes;
788
789    procedure Change_Attributes
790      (Win    : in Window := Standard_Window;
791       Line   : in Line_Position := Line_Position'First;
792       Column : in Column_Position := Column_Position'First;
793       Count  : in Integer := -1;
794       Attr   : in Character_Attribute_Set := Normal_Video;
795       Color  : in Color_Pair := Color_Pair'First)
796    is
797    begin
798       Move_Cursor (Win, Line, Column);
799       Change_Attributes (Win, Count, Attr, Color);
800    end Change_Attributes;
801 ------------------------------------------------------------------------------
802    procedure Beep
803    is
804       function Beeper return C_Int;
805       pragma Import (C, Beeper, "beep");
806    begin
807       if Beeper = Curses_Err then
808          raise Curses_Exception;
809       end if;
810    end Beep;
811
812    procedure Flash_Screen
813    is
814       function Flash return C_Int;
815       pragma Import (C, Flash, "flash");
816    begin
817       if Flash = Curses_Err then
818          raise Curses_Exception;
819       end if;
820    end Flash_Screen;
821 ------------------------------------------------------------------------------
822    procedure Set_Cbreak_Mode (SwitchOn : in Boolean := True)
823    is
824       function Cbreak return C_Int;
825       pragma Import (C, Cbreak, "cbreak");
826       function NoCbreak return C_Int;
827       pragma Import (C, NoCbreak, "nocbreak");
828
829       Err : C_Int;
830    begin
831       if SwitchOn then
832          Err := Cbreak;
833       else
834          Err := NoCbreak;
835       end if;
836       if Err = Curses_Err then
837          raise Curses_Exception;
838       end if;
839    end Set_Cbreak_Mode;
840
841    procedure Set_Raw_Mode (SwitchOn : in Boolean := True)
842    is
843       function Raw return C_Int;
844       pragma Import (C, Raw, "raw");
845       function NoRaw return C_Int;
846       pragma Import (C, NoRaw, "noraw");
847
848       Err : C_Int;
849    begin
850       if SwitchOn then
851          Err := Raw;
852       else
853          Err := NoRaw;
854       end if;
855       if Err = Curses_Err then
856          raise Curses_Exception;
857       end if;
858    end Set_Raw_Mode;
859
860    procedure Set_Echo_Mode (SwitchOn : in Boolean := True)
861    is
862       function Echo return C_Int;
863       pragma Import (C, Echo, "echo");
864       function NoEcho return C_Int;
865       pragma Import (C, NoEcho, "noecho");
866
867       Err : C_Int;
868    begin
869       if SwitchOn then
870          Err := Echo;
871       else
872          Err := NoEcho;
873       end if;
874       if Err = Curses_Err then
875          raise Curses_Exception;
876       end if;
877    end Set_Echo_Mode;
878
879    procedure Set_Meta_Mode (Win      : in Window := Standard_Window;
880                             SwitchOn : in Boolean := True)
881    is
882       function Meta (W : Window; Mode : Curses_Bool) return C_Int;
883       pragma Import (C, Meta, "meta");
884    begin
885       if Meta (Win, Curses_Bool (Boolean'Pos (SwitchOn))) = Curses_Err then
886          raise Curses_Exception;
887       end if;
888    end Set_Meta_Mode;
889
890    procedure Set_KeyPad_Mode (Win      : in Window := Standard_Window;
891                               SwitchOn : in Boolean := True)
892    is
893       function Keypad (W : Window; Mode : Curses_Bool) return C_Int;
894       pragma Import (C, Keypad, "keypad");
895    begin
896       if Keypad (Win, Curses_Bool (Boolean'Pos (SwitchOn))) = Curses_Err then
897          raise Curses_Exception;
898       end if;
899    end Set_KeyPad_Mode;
900
901    function Get_KeyPad_Mode (Win : in Window := Standard_Window)
902                              return Boolean
903    is
904    begin
905       return Get_Flag (Win, Offset_use_keypad);
906    end Get_KeyPad_Mode;
907
908    procedure Half_Delay (Amount : in Half_Delay_Amount)
909    is
910       function Halfdelay (Amount : C_Int) return C_Int;
911       pragma Import (C, Halfdelay, "halfdelay");
912    begin
913       if Halfdelay (C_Int (Amount)) = Curses_Err then
914          raise Curses_Exception;
915       end if;
916    end Half_Delay;
917
918    procedure Set_Flush_On_Interrupt_Mode
919      (Win  : in Window := Standard_Window;
920       Mode : in Boolean := True)
921    is
922       function Intrflush (Win : Window; Mode : Curses_Bool) return C_Int;
923       pragma Import (C, Intrflush, "intrflush");
924    begin
925       if Intrflush (Win, Curses_Bool (Boolean'Pos (Mode))) = Curses_Err then
926          raise Curses_Exception;
927       end if;
928    end Set_Flush_On_Interrupt_Mode;
929
930    procedure Set_Queue_Interrupt_Mode
931      (Win   : in Window := Standard_Window;
932       Flush : in Boolean := True)
933    is
934       procedure Qiflush;
935       pragma Import (C, Qiflush, "qiflush");
936       procedure No_Qiflush;
937       pragma Import (C, No_Qiflush, "noqiflush");
938    begin
939       if Flush then
940          Qiflush;
941       else
942          No_Qiflush;
943       end if;
944    end Set_Queue_Interrupt_Mode;
945
946    procedure Set_NoDelay_Mode
947      (Win  : in Window := Standard_Window;
948       Mode : in Boolean := False)
949    is
950       function Nodelay (Win : Window; Mode : Curses_Bool) return C_Int;
951       pragma Import (C, Nodelay, "nodelay");
952    begin
953       if Nodelay (Win, Curses_Bool (Boolean'Pos (Mode))) = Curses_Err then
954          raise Curses_Exception;
955       end if;
956    end Set_NoDelay_Mode;
957
958    procedure Set_Timeout_Mode (Win    : in Window := Standard_Window;
959                                Mode   : in Timeout_Mode;
960                                Amount : in Natural)
961    is
962       function Wtimeout (Win : Window; Amount : C_Int) return C_Int;
963       pragma Import (C, Wtimeout, "wtimeout");
964
965       Time : C_Int;
966    begin
967       case Mode is
968          when Blocking     => Time := -1;
969          when Non_Blocking => Time := 0;
970          when Delayed      =>
971             if Amount = 0 then
972                raise Constraint_Error;
973             end if;
974             Time := C_Int (Amount);
975       end case;
976       if Wtimeout (Win, Time) = Curses_Err then
977          raise Curses_Exception;
978       end if;
979    end Set_Timeout_Mode;
980
981    procedure Set_Escape_Timer_Mode
982      (Win       : in Window := Standard_Window;
983       Timer_Off : in Boolean := False)
984    is
985       function Notimeout (Win : Window; Mode : Curses_Bool) return C_Int;
986       pragma Import (C, Notimeout, "notimeout");
987    begin
988       if Notimeout (Win, Curses_Bool (Boolean'Pos (Timer_Off)))
989         = Curses_Err then
990          raise Curses_Exception;
991       end if;
992    end Set_Escape_Timer_Mode;
993
994 ------------------------------------------------------------------------------
995    procedure Set_NL_Mode (SwitchOn : in Boolean := True)
996    is
997       function NL return C_Int;
998       pragma Import (C, NL, "nl");
999       function NoNL return C_Int;
1000       pragma Import (C, NoNL, "nonl");
1001
1002       Err : C_Int;
1003    begin
1004       if SwitchOn then
1005          Err := NL;
1006       else
1007          Err := NoNL;
1008       end if;
1009       if Err = Curses_Err then
1010          raise Curses_Exception;
1011       end if;
1012    end Set_NL_Mode;
1013
1014    procedure Clear_On_Next_Update
1015      (Win      : in Window := Standard_Window;
1016       Do_Clear : in Boolean := True)
1017    is
1018       function Clear_Ok (W : Window; Flag : Curses_Bool) return C_Int;
1019       pragma Import (C, Clear_Ok, "clearok");
1020    begin
1021       if Clear_Ok (Win, Curses_Bool (Boolean'Pos (Do_Clear))) = Curses_Err then
1022          raise Curses_Exception;
1023       end if;
1024    end Clear_On_Next_Update;
1025
1026    procedure Use_Insert_Delete_Line
1027      (Win    : in Window := Standard_Window;
1028       Do_Idl : in Boolean := True)
1029    is
1030       function IDL_Ok (W : Window; Flag : Curses_Bool) return C_Int;
1031       pragma Import (C, IDL_Ok, "idlok");
1032    begin
1033       if IDL_Ok (Win, Curses_Bool (Boolean'Pos (Do_Idl))) = Curses_Err then
1034          raise Curses_Exception;
1035       end if;
1036    end Use_Insert_Delete_Line;
1037
1038    procedure Use_Insert_Delete_Character
1039      (Win    : in Window := Standard_Window;
1040       Do_Idc : in Boolean := True)
1041    is
1042       function IDC_Ok (W : Window; Flag : Curses_Bool) return C_Int;
1043       pragma Import (C, IDC_Ok, "idcok");
1044    begin
1045       if IDC_Ok (Win, Curses_Bool (Boolean'Pos (Do_Idc))) = Curses_Err then
1046          raise Curses_Exception;
1047       end if;
1048    end Use_Insert_Delete_Character;
1049
1050    procedure Leave_Cursor_After_Update
1051      (Win      : in Window := Standard_Window;
1052       Do_Leave : in Boolean := True)
1053    is
1054       function Leave_Ok (W : Window; Flag : Curses_Bool) return C_Int;
1055       pragma Import (C, Leave_Ok, "leaveok");
1056    begin
1057       if Leave_Ok (Win, Curses_Bool (Boolean'Pos (Do_Leave))) = Curses_Err then
1058          raise Curses_Exception;
1059       end if;
1060    end Leave_Cursor_After_Update;
1061
1062    procedure Immediate_Update_Mode
1063      (Win  : in Window := Standard_Window;
1064       Mode : in Boolean := False)
1065    is
1066       function Immedok (Win : Window; Mode : Curses_Bool) return C_Int;
1067       pragma Import (C, Immedok, "immedok");
1068    begin
1069       if Immedok (Win, Curses_Bool (Boolean'Pos (Mode))) = Curses_Err then
1070          raise Curses_Exception;
1071       end if;
1072    end Immediate_Update_Mode;
1073
1074    procedure Allow_Scrolling
1075      (Win  : in Window  := Standard_Window;
1076       Mode : in Boolean := False)
1077    is
1078       function Scrollok (Win : Window; Mode : Curses_Bool) return C_Int;
1079       pragma Import (C, Scrollok, "scrollok");
1080    begin
1081       if Scrollok (Win, Curses_Bool (Boolean'Pos (Mode))) = Curses_Err then
1082          raise Curses_Exception;
1083       end if;
1084    end Allow_Scrolling;
1085
1086    function Scrolling_Allowed (Win : Window := Standard_Window)
1087                                return Boolean
1088    is
1089    begin
1090       return Get_Flag (Win, Offset_scroll);
1091    end Scrolling_Allowed;
1092
1093    procedure Set_Scroll_Region
1094      (Win         : in Window := Standard_Window;
1095       Top_Line    : in Line_Position;
1096       Bottom_Line : in Line_Position)
1097    is
1098       function Wsetscrreg (Win : Window;
1099                            Lin : C_Int;
1100                            Col : C_Int) return C_Int;
1101       pragma Import (C, Wsetscrreg, "wsetscrreg");
1102    begin
1103       if Wsetscrreg (Win, C_Int (Top_Line), C_Int (Bottom_Line))
1104         = Curses_Err then
1105          raise Curses_Exception;
1106       end if;
1107    end Set_Scroll_Region;
1108 ------------------------------------------------------------------------------
1109    procedure Update_Screen
1110    is
1111       function Do_Update return C_Int;
1112       pragma Import (C, Do_Update, "doupdate");
1113    begin
1114       if Do_Update = Curses_Err then
1115          raise Curses_Exception;
1116       end if;
1117    end Update_Screen;
1118
1119    procedure Refresh (Win : in Window := Standard_Window)
1120    is
1121       function Wrefresh (W : Window) return C_Int;
1122       pragma Import (C, Wrefresh, "wrefresh");
1123    begin
1124       if Wrefresh (Win) = Curses_Err then
1125          raise Curses_Exception;
1126       end if;
1127    end Refresh;
1128
1129    procedure Refresh_Without_Update
1130      (Win : in Window := Standard_Window)
1131    is
1132       function Wnoutrefresh (W : Window) return C_Int;
1133       pragma Import (C, Wnoutrefresh, "wnoutrefresh");
1134    begin
1135       if Wnoutrefresh (Win) = Curses_Err then
1136          raise Curses_Exception;
1137       end if;
1138    end Refresh_Without_Update;
1139
1140    procedure Redraw (Win : in Window := Standard_Window)
1141    is
1142       function Redrawwin (Win : Window) return C_Int;
1143       pragma Import (C, Redrawwin, "redrawwin");
1144    begin
1145       if Redrawwin (Win) = Curses_Err then
1146          raise Curses_Exception;
1147       end if;
1148    end Redraw;
1149
1150    procedure Redraw
1151      (Win        : in Window := Standard_Window;
1152       Begin_Line : in Line_Position;
1153       Line_Count : in Positive)
1154    is
1155       function Wredrawln (Win : Window; First : C_Int; Cnt : C_Int)
1156                           return C_Int;
1157       pragma Import (C, Wredrawln, "wredrawln");
1158    begin
1159       if Wredrawln (Win,
1160                     C_Int (Begin_Line),
1161                     C_Int (Line_Count)) = Curses_Err then
1162          raise Curses_Exception;
1163       end if;
1164    end Redraw;
1165
1166 ------------------------------------------------------------------------------
1167    procedure Erase (Win : in Window := Standard_Window)
1168    is
1169       function Werase (W : Window) return C_Int;
1170       pragma Import (C, Werase, "werase");
1171    begin
1172       if Werase (Win) = Curses_Err then
1173          raise Curses_Exception;
1174       end if;
1175    end Erase;
1176
1177    procedure Clear (Win : in Window := Standard_Window)
1178    is
1179       function Wclear (W : Window) return C_Int;
1180       pragma Import (C, Wclear, "wclear");
1181    begin
1182       if Wclear (Win) = Curses_Err then
1183          raise Curses_Exception;
1184       end if;
1185    end Clear;
1186
1187    procedure Clear_To_End_Of_Screen (Win : in Window := Standard_Window)
1188    is
1189       function Wclearbot (W : Window) return C_Int;
1190       pragma Import (C, Wclearbot, "wclrtobot");
1191    begin
1192       if Wclearbot (Win) = Curses_Err then
1193          raise Curses_Exception;
1194       end if;
1195    end Clear_To_End_Of_Screen;
1196
1197    procedure Clear_To_End_Of_Line (Win : in Window := Standard_Window)
1198    is
1199       function Wcleareol (W : Window) return C_Int;
1200       pragma Import (C, Wcleareol, "wclrtoeol");
1201    begin
1202       if Wcleareol (Win) = Curses_Err then
1203          raise Curses_Exception;
1204       end if;
1205    end Clear_To_End_Of_Line;
1206 ------------------------------------------------------------------------------
1207    procedure Set_Background
1208      (Win : in Window := Standard_Window;
1209       Ch  : in Attributed_Character)
1210    is
1211       procedure WBackground (W : in Window; Ch : in C_Chtype);
1212       pragma Import (C, WBackground, "wbkgdset");
1213    begin
1214       WBackground (Win, AttrChar_To_Chtype (Ch));
1215    end Set_Background;
1216
1217    procedure Change_Background
1218      (Win : in Window := Standard_Window;
1219       Ch  : in Attributed_Character)
1220    is
1221       function WChangeBkgd (W : Window; Ch : C_Chtype) return C_Int;
1222       pragma Import (C, WChangeBkgd, "wbkgd");
1223    begin
1224       if WChangeBkgd (Win, AttrChar_To_Chtype (Ch)) = Curses_Err then
1225          raise Curses_Exception;
1226       end if;
1227    end Change_Background;
1228
1229    function Get_Background (Win : Window := Standard_Window)
1230      return Attributed_Character
1231    is
1232       function Wgetbkgd (Win : Window) return C_Chtype;
1233       pragma Import (C, Wgetbkgd, "getbkgd");
1234    begin
1235       return Chtype_To_AttrChar (Wgetbkgd (Win));
1236    end Get_Background;
1237 ------------------------------------------------------------------------------
1238    procedure Change_Lines_Status (Win   : in Window := Standard_Window;
1239                                   Start : in Line_Position;
1240                                   Count : in Positive;
1241                                   State : in Boolean)
1242    is
1243       function Wtouchln (Win : Window;
1244                          Sta : C_Int;
1245                          Cnt : C_Int;
1246                          Chg : C_Int) return C_Int;
1247       pragma Import (C, Wtouchln, "wtouchln");
1248    begin
1249       if Wtouchln (Win, C_Int (Start), C_Int (Count),
1250                    C_Int (Boolean'Pos (State))) = Curses_Err then
1251          raise Curses_Exception;
1252       end if;
1253    end Change_Lines_Status;
1254
1255    procedure Touch (Win : in Window := Standard_Window)
1256    is
1257       Y : Line_Position;
1258       X : Column_Position;
1259    begin
1260       Get_Size (Win, Y, X);
1261       Change_Lines_Status (Win, 0, Positive (Y), True);
1262    end Touch;
1263
1264    procedure Untouch (Win : in Window := Standard_Window)
1265    is
1266       Y : Line_Position;
1267       X : Column_Position;
1268    begin
1269       Get_Size (Win, Y, X);
1270       Change_Lines_Status (Win, 0, Positive (Y), False);
1271    end Untouch;
1272
1273    procedure Touch (Win   : in Window := Standard_Window;
1274                     Start : in Line_Position;
1275                     Count : in Positive)
1276    is
1277    begin
1278       Change_Lines_Status (Win, Start, Count, True);
1279    end Touch;
1280
1281    function Is_Touched
1282      (Win  : Window := Standard_Window;
1283       Line : Line_Position) return Boolean
1284    is
1285       function WLineTouched (W : Window; L : C_Int) return Curses_Bool;
1286       pragma Import (C, WLineTouched, "is_linetouched");
1287    begin
1288       if WLineTouched (Win, C_Int (Line)) = Curses_Bool_False then
1289          return False;
1290       else
1291          return True;
1292       end if;
1293    end Is_Touched;
1294
1295    function Is_Touched
1296      (Win : Window := Standard_Window) return Boolean
1297    is
1298       function WWinTouched (W : Window) return Curses_Bool;
1299       pragma Import (C, WWinTouched, "is_wintouched");
1300    begin
1301       if WWinTouched (Win) = Curses_Bool_False then
1302          return False;
1303       else
1304          return True;
1305       end if;
1306    end Is_Touched;
1307 ------------------------------------------------------------------------------
1308    procedure Copy
1309      (Source_Window            : in Window;
1310       Destination_Window       : in Window;
1311       Source_Top_Row           : in Line_Position;
1312       Source_Left_Column       : in Column_Position;
1313       Destination_Top_Row      : in Line_Position;
1314       Destination_Left_Column  : in Column_Position;
1315       Destination_Bottom_Row   : in Line_Position;
1316       Destination_Right_Column : in Column_Position;
1317       Non_Destructive_Mode     : in Boolean := True)
1318    is
1319       function Copywin (Src : Window;
1320                         Dst : Window;
1321                         Str : C_Int;
1322                         Slc : C_Int;
1323                         Dtr : C_Int;
1324                         Dlc : C_Int;
1325                         Dbr : C_Int;
1326                         Drc : C_Int;
1327                         Ndm : C_Int) return C_Int;
1328       pragma Import (C, Copywin, "copywin");
1329    begin
1330       if Copywin (Source_Window,
1331                   Destination_Window,
1332                   C_Int (Source_Top_Row),
1333                   C_Int (Source_Left_Column),
1334                   C_Int (Destination_Top_Row),
1335                   C_Int (Destination_Left_Column),
1336                   C_Int (Destination_Bottom_Row),
1337                   C_Int (Destination_Right_Column),
1338                   Boolean'Pos (Non_Destructive_Mode)
1339                 ) = Curses_Err then
1340          raise Curses_Exception;
1341       end if;
1342    end Copy;
1343
1344    procedure Overwrite
1345      (Source_Window      : in Window;
1346       Destination_Window : in Window)
1347    is
1348       function Overwrite (Src : Window; Dst : Window) return C_Int;
1349       pragma Import (C, Overwrite, "overwrite");
1350    begin
1351       if Overwrite (Source_Window, Destination_Window) = Curses_Err then
1352          raise Curses_Exception;
1353       end if;
1354    end Overwrite;
1355
1356    procedure Overlay
1357      (Source_Window      : in Window;
1358       Destination_Window : in Window)
1359    is
1360       function Overlay (Src : Window; Dst : Window) return C_Int;
1361       pragma Import (C, Overlay, "overlay");
1362    begin
1363       if Overlay (Source_Window, Destination_Window) = Curses_Err then
1364          raise Curses_Exception;
1365       end if;
1366    end Overlay;
1367
1368 ------------------------------------------------------------------------------
1369    procedure Insert_Delete_Lines
1370      (Win   : in Window := Standard_Window;
1371       Lines : in Integer       := 1) -- default is to insert one line above
1372    is
1373       function Winsdelln (W : Window; N : C_Int) return C_Int;
1374       pragma Import (C, Winsdelln, "winsdelln");
1375    begin
1376       if Winsdelln (Win, C_Int (Lines)) = Curses_Err then
1377          raise Curses_Exception;
1378       end if;
1379    end Insert_Delete_Lines;
1380
1381    procedure Delete_Line (Win : in Window := Standard_Window)
1382    is
1383    begin
1384       Insert_Delete_Lines (Win, -1);
1385    end Delete_Line;
1386
1387    procedure Insert_Line (Win : in Window := Standard_Window)
1388    is
1389    begin
1390       Insert_Delete_Lines (Win, 1);
1391    end Insert_Line;
1392 ------------------------------------------------------------------------------
1393
1394
1395    procedure Get_Size
1396      (Win               : in Window := Standard_Window;
1397       Number_Of_Lines   : out Line_Count;
1398       Number_Of_Columns : out Column_Count)
1399    is
1400       --  Please note: in ncurses they are one off.
1401       --  This might be different in other implementations of curses
1402       Y : C_Int := C_Int (W_Get_Short (Win, Offset_maxy)) + C_Int (Offset_XY);
1403       X : C_Int := C_Int (W_Get_Short (Win, Offset_maxx)) + C_Int (Offset_XY);
1404    begin
1405       Number_Of_Lines   := Line_Count (Y);
1406       Number_Of_Columns := Column_Count (X);
1407    end Get_Size;
1408
1409    procedure Get_Window_Position
1410      (Win             : in Window := Standard_Window;
1411       Top_Left_Line   : out Line_Position;
1412       Top_Left_Column : out Column_Position)
1413    is
1414       Y : C_Short := W_Get_Short (Win, Offset_begy);
1415       X : C_Short := W_Get_Short (Win, Offset_begx);
1416    begin
1417       Top_Left_Line   := Line_Position (Y);
1418       Top_Left_Column := Column_Position (X);
1419    end Get_Window_Position;
1420
1421    procedure Get_Cursor_Position
1422      (Win    : in  Window := Standard_Window;
1423       Line   : out Line_Position;
1424       Column : out Column_Position)
1425    is
1426       Y : C_Short := W_Get_Short (Win, Offset_cury);
1427       X : C_Short := W_Get_Short (Win, Offset_curx);
1428    begin
1429       Line   := Line_Position (Y);
1430       Column := Column_Position (X);
1431    end Get_Cursor_Position;
1432
1433    procedure Get_Origin_Relative_To_Parent
1434      (Win                : in  Window;
1435       Top_Left_Line      : out Line_Position;
1436       Top_Left_Column    : out Column_Position;
1437       Is_Not_A_Subwindow : out Boolean)
1438    is
1439       Y : C_Int := W_Get_Int (Win, Offset_pary);
1440       X : C_Int := W_Get_Int (Win, Offset_parx);
1441    begin
1442       if Y = -1 then
1443          Top_Left_Line   := Line_Position'Last;
1444          Top_Left_Column := Column_Position'Last;
1445          Is_Not_A_Subwindow := True;
1446       else
1447          Top_Left_Line   := Line_Position (Y);
1448          Top_Left_Column := Column_Position (X);
1449          Is_Not_A_Subwindow := False;
1450       end if;
1451    end Get_Origin_Relative_To_Parent;
1452 ------------------------------------------------------------------------------
1453    function New_Pad (Lines   : Line_Count;
1454                      Columns : Column_Count) return Window
1455    is
1456       function Newpad (Lines : C_Int; Columns : C_Int) return Window;
1457       pragma Import (C, Newpad, "newpad");
1458
1459       W : Window;
1460    begin
1461       W := Newpad (C_Int (Lines), C_Int (Columns));
1462       if W = Null_Window then
1463          raise Curses_Exception;
1464       end if;
1465       return W;
1466    end New_Pad;
1467
1468    function Sub_Pad
1469      (Pad                   : Window;
1470       Number_Of_Lines       : Line_Count;
1471       Number_Of_Columns     : Column_Count;
1472       First_Line_Position   : Line_Position;
1473       First_Column_Position : Column_Position) return Window
1474    is
1475       function Subpad
1476         (Pad                   : Window;
1477          Number_Of_Lines       : C_Int;
1478          Number_Of_Columns     : C_Int;
1479          First_Line_Position   : C_Int;
1480          First_Column_Position : C_Int) return Window;
1481       pragma Import (C, Subpad, "subpad");
1482
1483       W : Window;
1484    begin
1485       W := Subpad (Pad,
1486                    C_Int (Number_Of_Lines),
1487                    C_Int (Number_Of_Columns),
1488                    C_Int (First_Line_Position),
1489                    C_Int (First_Column_Position));
1490       if W = Null_Window then
1491          raise Curses_Exception;
1492       end if;
1493       return W;
1494    end Sub_Pad;
1495
1496    procedure Refresh
1497      (Pad                      : in Window;
1498       Source_Top_Row           : in Line_Position;
1499       Source_Left_Column       : in Column_Position;
1500       Destination_Top_Row      : in Line_Position;
1501       Destination_Left_Column  : in Column_Position;
1502       Destination_Bottom_Row   : in Line_Position;
1503       Destination_Right_Column : in Column_Position)
1504    is
1505       function Prefresh
1506         (Pad                      : Window;
1507          Source_Top_Row           : C_Int;
1508          Source_Left_Column       : C_Int;
1509          Destination_Top_Row      : C_Int;
1510          Destination_Left_Column  : C_Int;
1511          Destination_Bottom_Row   : C_Int;
1512          Destination_Right_Column : C_Int) return C_Int;
1513       pragma Import (C, Prefresh, "prefresh");
1514    begin
1515       if Prefresh (Pad,
1516                    C_Int (Source_Top_Row),
1517                    C_Int (Source_Left_Column),
1518                    C_Int (Destination_Top_Row),
1519                    C_Int (Destination_Left_Column),
1520                    C_Int (Destination_Bottom_Row),
1521                    C_Int (Destination_Right_Column)) = Curses_Err then
1522          raise Curses_Exception;
1523       end if;
1524    end Refresh;
1525
1526    procedure Refresh_Without_Update
1527      (Pad                      : in Window;
1528       Source_Top_Row           : in Line_Position;
1529       Source_Left_Column       : in Column_Position;
1530       Destination_Top_Row      : in Line_Position;
1531       Destination_Left_Column  : in Column_Position;
1532       Destination_Bottom_Row   : in Line_Position;
1533       Destination_Right_Column : in Column_Position)
1534    is
1535       function Pnoutrefresh
1536         (Pad                      : Window;
1537          Source_Top_Row           : C_Int;
1538          Source_Left_Column       : C_Int;
1539          Destination_Top_Row      : C_Int;
1540          Destination_Left_Column  : C_Int;
1541          Destination_Bottom_Row   : C_Int;
1542          Destination_Right_Column : C_Int) return C_Int;
1543       pragma Import (C, Pnoutrefresh, "pnoutrefresh");
1544    begin
1545       if Pnoutrefresh (Pad,
1546                        C_Int (Source_Top_Row),
1547                        C_Int (Source_Left_Column),
1548                        C_Int (Destination_Top_Row),
1549                        C_Int (Destination_Left_Column),
1550                        C_Int (Destination_Bottom_Row),
1551                        C_Int (Destination_Right_Column)) = Curses_Err then
1552          raise Curses_Exception;
1553       end if;
1554    end Refresh_Without_Update;
1555
1556    procedure Add_Character_To_Pad_And_Echo_It
1557      (Pad : in Window;
1558       Ch  : in Attributed_Character)
1559    is
1560       function Pechochar (Pad : Window; Ch : C_Chtype)
1561                           return C_Int;
1562       pragma Import (C, Pechochar, "pechochar");
1563    begin
1564       if Pechochar (Pad, AttrChar_To_Chtype (Ch)) = Curses_Err then
1565          raise Curses_Exception;
1566       end if;
1567    end Add_Character_To_Pad_And_Echo_It;
1568
1569    procedure Add_Character_To_Pad_And_Echo_It
1570      (Pad : in Window;
1571       Ch  : in Character)
1572    is
1573    begin
1574       Add_Character_To_Pad_And_Echo_It
1575         (Pad,
1576          Attributed_Character'(Ch    => Ch,
1577                                Color => Color_Pair'First,
1578                                Attr  => Normal_Video));
1579    end Add_Character_To_Pad_And_Echo_It;
1580 ------------------------------------------------------------------------------
1581    procedure Scroll (Win    : in Window := Standard_Window;
1582                      Amount : in Integer := 1)
1583    is
1584       function Wscrl (Win : Window; N : C_Int) return C_Int;
1585       pragma Import (C, Wscrl, "wscrl");
1586
1587    begin
1588       if Wscrl (Win, C_Int (Amount)) = Curses_Err then
1589          raise Curses_Exception;
1590       end if;
1591    end Scroll;
1592
1593 ------------------------------------------------------------------------------
1594    procedure Delete_Character (Win : in Window := Standard_Window)
1595    is
1596       function Wdelch (Win : Window) return C_Int;
1597       pragma Import (C, Wdelch, "wdelch");
1598    begin
1599       if Wdelch (Win) = Curses_Err then
1600          raise Curses_Exception;
1601       end if;
1602    end Delete_Character;
1603
1604    procedure Delete_Character
1605      (Win    : in Window := Standard_Window;
1606       Line   : in Line_Position;
1607       Column : in Column_Position)
1608    is
1609       function Mvwdelch (Win : Window;
1610                          Lin : C_Int;
1611                          Col : C_Int) return C_Int;
1612       pragma Import (C, Mvwdelch, "mvwdelch");
1613    begin
1614       if Mvwdelch (Win, C_Int (Line), C_Int (Column)) = Curses_Err then
1615          raise Curses_Exception;
1616       end if;
1617    end Delete_Character;
1618 ------------------------------------------------------------------------------
1619    function Peek (Win : Window := Standard_Window)
1620      return Attributed_Character
1621    is
1622       function Winch (Win : Window) return C_Chtype;
1623       pragma Import (C, Winch, "winch");
1624    begin
1625       return Chtype_To_AttrChar (Winch (Win));
1626    end Peek;
1627
1628    function Peek
1629      (Win    : Window := Standard_Window;
1630       Line   : Line_Position;
1631       Column : Column_Position) return Attributed_Character
1632    is
1633       function Mvwinch (Win : Window;
1634                         Lin : C_Int;
1635                         Col : C_Int) return C_Chtype;
1636       pragma Import (C, Mvwinch, "mvwinch");
1637    begin
1638       return Chtype_To_AttrChar (Mvwinch (Win, C_Int (Line), C_Int (Column)));
1639    end Peek;
1640 ------------------------------------------------------------------------------
1641    procedure Insert (Win : in Window := Standard_Window;
1642                      Ch  : in Attributed_Character)
1643    is
1644       function Winsch (Win : Window; Ch : C_Chtype) return C_Int;
1645       pragma Import (C, Winsch, "winsch");
1646    begin
1647       if Winsch (Win, AttrChar_To_Chtype (Ch)) = Curses_Err then
1648          raise Curses_Exception;
1649       end if;
1650    end Insert;
1651
1652    procedure Insert
1653      (Win    : in Window := Standard_Window;
1654       Line   : in Line_Position;
1655       Column : in Column_Position;
1656       Ch     : in Attributed_Character)
1657    is
1658       function Mvwinsch (Win : Window;
1659                          Lin : C_Int;
1660                          Col : C_Int;
1661                          Ch  : C_Chtype) return C_Int;
1662       pragma Import (C, Mvwinsch, "mvwinsch");
1663    begin
1664       if Mvwinsch (Win,
1665                    C_Int (Line),
1666                    C_Int (Column),
1667                    AttrChar_To_Chtype (Ch)) = Curses_Err then
1668          raise Curses_Exception;
1669       end if;
1670    end Insert;
1671 ------------------------------------------------------------------------------
1672    procedure Insert (Win : in Window := Standard_Window;
1673                      Str : in String;
1674                      Len : in Integer := -1)
1675    is
1676       function Winsnstr (Win : Window;
1677                          Str : char_array;
1678                          Len : Integer := -1) return C_Int;
1679       pragma Import (C, Winsnstr, "winsnstr");
1680
1681       Txt    : char_array (0 .. Str'Length);
1682       Length : size_t;
1683    begin
1684       To_C (Str, Txt, Length);
1685       if Winsnstr (Win, Txt, Len) = Curses_Err then
1686          raise Curses_Exception;
1687       end if;
1688    end Insert;
1689
1690    procedure Insert
1691      (Win    : in Window := Standard_Window;
1692       Line   : in Line_Position;
1693       Column : in Column_Position;
1694       Str    : in String;
1695       Len    : in Integer := -1)
1696    is
1697       function Mvwinsnstr (Win    : Window;
1698                            Line   : C_Int;
1699                            Column : C_Int;
1700                            Str    : char_array;
1701                            Len    : C_Int) return C_Int;
1702       pragma Import (C, Mvwinsnstr, "mvwinsnstr");
1703
1704       Txt    : char_array (0 .. Str'Length);
1705       Length : size_t;
1706    begin
1707       To_C (Str, Txt, Length);
1708       if Mvwinsnstr (Win, C_Int (Line), C_Int (Column), Txt, C_Int (Len))
1709         = Curses_Err then
1710          raise Curses_Exception;
1711       end if;
1712    end Insert;
1713 ------------------------------------------------------------------------------
1714    procedure Peek (Win : in  Window := Standard_Window;
1715                    Str : out String;
1716                    Len : in  Integer := -1)
1717    is
1718       function Winnstr (Win : Window;
1719                         Str : char_array;
1720                         Len : C_Int) return C_Int;
1721       pragma Import (C, Winnstr, "winnstr");
1722
1723       N   : Integer := Len;
1724       Txt : char_array (0 .. Str'Length);
1725       Cnt : Natural;
1726    begin
1727       if N < 0 then
1728          N := Str'Length;
1729       end if;
1730       if N > Str'Length then
1731          raise Constraint_Error;
1732       end if;
1733       Txt (0) := Interfaces.C.char'First;
1734       if Winnstr (Win, Txt, C_Int (N)) = Curses_Err then
1735          raise Curses_Exception;
1736       end if;
1737       To_Ada (Txt, Str, Cnt, True);
1738       if Cnt < Str'Length then
1739          Str ((Str'First + Cnt) .. Str'Last) := (others => ' ');
1740       end if;
1741    end Peek;
1742
1743    procedure Peek
1744      (Win    : in  Window := Standard_Window;
1745       Line   : in  Line_Position;
1746       Column : in  Column_Position;
1747       Str    : out String;
1748       Len    : in  Integer := -1)
1749    is
1750    begin
1751       Move_Cursor (Win, Line, Column);
1752       Peek (Win, Str, Len);
1753    end Peek;
1754 ------------------------------------------------------------------------------
1755    procedure Peek
1756      (Win : in  Window := Standard_Window;
1757       Str : out Attributed_String;
1758       Len : in  Integer := -1)
1759    is
1760       function Winchnstr (Win : Window;
1761                           Str : chtype_array;             -- out
1762                           Len : C_Int) return C_Int;
1763       pragma Import (C, Winchnstr, "winchnstr");
1764
1765       N   : Integer := Len;
1766       Txt : chtype_array (0 .. Str'Length) := (0 => Default_Character);
1767       Cnt : Natural := 0;
1768    begin
1769       if N < 0 then
1770          N := Str'Length;
1771       end if;
1772       if N > Str'Length then
1773          raise Constraint_Error;
1774       end if;
1775       if Winchnstr (Win, Txt, C_Int (N)) = Curses_Err then
1776          raise Curses_Exception;
1777       end if;
1778       for To in Str'Range loop
1779          exit when Txt (size_t (Cnt)) = Default_Character;
1780          Str (To) := Txt (size_t (Cnt));
1781          Cnt := Cnt + 1;
1782       end loop;
1783       if Cnt < Str'Length then
1784          Str ((Str'First + Cnt) .. Str'Last) :=
1785            (others => (Ch => ' ',
1786                        Color => Color_Pair'First,
1787                        Attr => Normal_Video));
1788       end if;
1789    end Peek;
1790
1791    procedure Peek
1792      (Win    : in  Window := Standard_Window;
1793       Line   : in  Line_Position;
1794       Column : in  Column_Position;
1795       Str    : out Attributed_String;
1796       Len    : in Integer := -1)
1797    is
1798    begin
1799       Move_Cursor (Win, Line, Column);
1800       Peek (Win, Str, Len);
1801    end Peek;
1802 ------------------------------------------------------------------------------
1803    procedure Get (Win : in  Window := Standard_Window;
1804                   Str : out String;
1805                   Len : in  Integer := -1)
1806    is
1807       function Wgetnstr (Win : Window;
1808                          Str : char_array;
1809                          Len : C_Int) return C_Int;
1810       pragma Import (C, Wgetnstr, "wgetnstr");
1811
1812       N   : Integer := Len;
1813       Txt : char_array (0 .. Str'Length);
1814       Cnt : Natural;
1815    begin
1816       if N < 0 then
1817          N := Str'Length;
1818       end if;
1819       if N > Str'Length then
1820          raise Constraint_Error;
1821       end if;
1822       Txt (0) := Interfaces.C.char'First;
1823       if Wgetnstr (Win, Txt, C_Int (N)) = Curses_Err then
1824          raise Curses_Exception;
1825       end if;
1826       To_Ada (Txt, Str, Cnt, True);
1827       if Cnt < Str'Length then
1828          Str ((Str'First + Cnt) .. Str'Last) := (others => ' ');
1829       end if;
1830    end Get;
1831
1832    procedure Get
1833      (Win    : in  Window := Standard_Window;
1834       Line   : in  Line_Position;
1835       Column : in  Column_Position;
1836       Str    : out String;
1837       Len    : in  Integer := -1)
1838    is
1839    begin
1840       Move_Cursor (Win, Line, Column);
1841       Get (Win, Str, Len);
1842    end Get;
1843 ------------------------------------------------------------------------------
1844    procedure Init_Soft_Label_Keys
1845      (Format : in Soft_Label_Key_Format := Three_Two_Three)
1846    is
1847       function Slk_Init (Fmt : C_Int) return C_Int;
1848       pragma Import (C, Slk_Init, "slk_init");
1849    begin
1850       if Slk_Init (Soft_Label_Key_Format'Pos (Format)) = Curses_Err then
1851          raise Curses_Exception;
1852       end if;
1853    end Init_Soft_Label_Keys;
1854
1855    procedure Set_Soft_Label_Key (Label : in Label_Number;
1856                                  Text  : in String;
1857                                  Fmt   : in Label_Justification := Left)
1858    is
1859       function Slk_Set (Label : C_Int;
1860                         Txt   : char_array;
1861                         Fmt   : C_Int) return C_Int;
1862       pragma Import (C, Slk_Set, "slk_set");
1863
1864       Txt : char_array (0 .. Text'Length);
1865       Len : size_t;
1866    begin
1867       To_C (Text, Txt, Len);
1868       if Slk_Set (C_Int (Label), Txt,
1869                   C_Int (Label_Justification'Pos (Fmt))) = Curses_Err then
1870          raise Curses_Exception;
1871       end if;
1872    end Set_Soft_Label_Key;
1873
1874    procedure Refresh_Soft_Label_Keys
1875    is
1876       function Slk_Refresh return C_Int;
1877       pragma Import (C, Slk_Refresh, "slk_refresh");
1878    begin
1879       if Slk_Refresh = Curses_Err then
1880          raise Curses_Exception;
1881       end if;
1882    end Refresh_Soft_Label_Keys;
1883
1884    procedure Refresh_Soft_Label_Keys_Without_Update
1885    is
1886       function Slk_Noutrefresh return C_Int;
1887       pragma Import (C, Slk_Noutrefresh, "slk_noutrefresh");
1888    begin
1889       if Slk_Noutrefresh = Curses_Err then
1890          raise Curses_Exception;
1891       end if;
1892    end Refresh_Soft_Label_Keys_Without_Update;
1893
1894    procedure Get_Soft_Label_Key (Label : in Label_Number;
1895                                  Text  : out String)
1896    is
1897       function Slk_Label (Label : C_Int) return chars_ptr;
1898       pragma Import (C, Slk_Label, "slk_label");
1899    begin
1900       Fill_String (Slk_Label (C_Int (Label)), Text);
1901    end Get_Soft_Label_Key;
1902
1903    function Get_Soft_Label_Key (Label : in Label_Number) return String
1904    is
1905       function Slk_Label (Label : C_Int) return chars_ptr;
1906       pragma Import (C, Slk_Label, "slk_label");
1907    begin
1908       return Fill_String (Slk_Label (C_Int (Label)));
1909    end Get_Soft_Label_Key;
1910
1911    procedure Clear_Soft_Label_Keys
1912    is
1913       function Slk_Clear return C_Int;
1914       pragma Import (C, Slk_Clear, "slk_clear");
1915    begin
1916       if Slk_Clear = Curses_Err then
1917          raise Curses_Exception;
1918       end if;
1919    end Clear_Soft_Label_Keys;
1920
1921    procedure Restore_Soft_Label_Keys
1922    is
1923       function Slk_Restore return C_Int;
1924       pragma Import (C, Slk_Restore, "slk_restore");
1925    begin
1926       if Slk_Restore = Curses_Err then
1927          raise Curses_Exception;
1928       end if;
1929    end Restore_Soft_Label_Keys;
1930
1931    procedure Touch_Soft_Label_Keys
1932    is
1933       function Slk_Touch return C_Int;
1934       pragma Import (C, Slk_Touch, "slk_touch");
1935    begin
1936       if Slk_Touch = Curses_Err then
1937          raise Curses_Exception;
1938       end if;
1939    end Touch_Soft_Label_Keys;
1940
1941    procedure Switch_Soft_Label_Key_Attributes
1942      (Attr : in Character_Attribute_Set;
1943       On   : in Boolean := True)
1944    is
1945       function Slk_Attron (Ch : C_Chtype) return C_Int;
1946       pragma Import (C, Slk_Attron, "slk_attron");
1947       function Slk_Attroff (Ch : C_Chtype) return C_Int;
1948       pragma Import (C, Slk_Attroff, "slk_attroff");
1949
1950       Err : C_Int;
1951       Ch  : constant Attributed_Character := (Ch    => Character'First,
1952                                               Attr  => Attr,
1953                                               Color => Color_Pair'First);
1954    begin
1955       if On then
1956          Err := Slk_Attron  (AttrChar_To_Chtype (Ch));
1957       else
1958          Err := Slk_Attroff (AttrChar_To_Chtype (Ch));
1959       end if;
1960       if Err = Curses_Err then
1961          raise Curses_Exception;
1962       end if;
1963    end Switch_Soft_Label_Key_Attributes;
1964
1965    procedure Set_Soft_Label_Key_Attributes
1966      (Attr  : in Character_Attribute_Set := Normal_Video;
1967       Color : in Color_Pair := Color_Pair'First)
1968    is
1969       function Slk_Attrset (Ch : C_Chtype) return C_Int;
1970       pragma Import (C, Slk_Attrset, "slk_attrset");
1971
1972       Ch : constant Attributed_Character := (Ch    => Character'First,
1973                                              Attr  => Attr,
1974                                              Color => Color);
1975    begin
1976       if Slk_Attrset (AttrChar_To_Chtype (Ch)) = Curses_Err then
1977          raise Curses_Exception;
1978       end if;
1979    end Set_Soft_Label_Key_Attributes;
1980
1981    function Get_Soft_Label_Key_Attributes return Character_Attribute_Set
1982    is
1983       function Slk_Attr return C_Chtype;
1984       pragma Import (C, Slk_Attr, "slk_attr");
1985
1986       Attr : constant C_Chtype := Slk_Attr;
1987    begin
1988       return Chtype_To_AttrChar (Attr).Attr;
1989    end Get_Soft_Label_Key_Attributes;
1990
1991    function Get_Soft_Label_Key_Attributes return Color_Pair
1992    is
1993       function Slk_Attr return C_Chtype;
1994       pragma Import (C, Slk_Attr, "slk_attr");
1995
1996       Attr : constant C_Chtype := Slk_Attr;
1997    begin
1998       return Chtype_To_AttrChar (Attr).Color;
1999    end Get_Soft_Label_Key_Attributes;
2000
2001    procedure Set_Soft_Label_Key_Color (Pair : in Color_Pair)
2002    is
2003       function Slk_Color (Color : in C_Short) return C_Int;
2004       pragma Import (C, Slk_Color, "slk_color");
2005    begin
2006       if Slk_Color (C_Short (Pair)) = Curses_Err then
2007          raise Curses_Exception;
2008       end if;
2009    end Set_Soft_Label_Key_Color;
2010
2011 ------------------------------------------------------------------------------
2012    procedure Enable_Key (Key    : in Special_Key_Code;
2013                          Enable : in Boolean := True)
2014    is
2015       function Keyok (Keycode : C_Int;
2016                       On_Off  : Curses_Bool) return C_Int;
2017       pragma Import (C, Keyok, "keyok");
2018    begin
2019       if Keyok (C_Int (Key), Curses_Bool (Boolean'Pos (Enable)))
2020         = Curses_Err then
2021          raise Curses_Exception;
2022       end if;
2023    end Enable_Key;
2024 ------------------------------------------------------------------------------
2025    procedure Define_Key (Definition : in String;
2026                          Key        : in Special_Key_Code)
2027    is
2028       function Defkey (Def : char_array;
2029                        Key : C_Int) return C_Int;
2030       pragma Import (C, Defkey, "define_key");
2031
2032       Txt    : char_array (0 .. Definition'Length);
2033       Length : size_t;
2034    begin
2035       To_C (Definition, Txt, Length);
2036       if Defkey (Txt, C_Int (Key)) = Curses_Err then
2037          raise Curses_Exception;
2038       end if;
2039    end Define_Key;
2040 ------------------------------------------------------------------------------
2041    procedure Un_Control (Ch  : in Attributed_Character;
2042                          Str : out String)
2043    is
2044       function Unctrl (Ch : C_Chtype) return chars_ptr;
2045       pragma Import (C, Unctrl, "unctrl");
2046    begin
2047       Fill_String (Unctrl (AttrChar_To_Chtype (Ch)), Str);
2048    end Un_Control;
2049
2050    function Un_Control (Ch : in Attributed_Character) return String
2051    is
2052       function Unctrl (Ch : C_Chtype) return chars_ptr;
2053       pragma Import (C, Unctrl, "unctrl");
2054    begin
2055       return Fill_String (Unctrl (AttrChar_To_Chtype (Ch)));
2056    end Un_Control;
2057
2058    procedure Delay_Output (Msecs : in Natural)
2059    is
2060       function Delayoutput (Msecs : C_Int) return C_Int;
2061       pragma Import (C, Delayoutput, "delay_output");
2062    begin
2063       if Delayoutput (C_Int (Msecs)) = Curses_Err then
2064          raise Curses_Exception;
2065       end if;
2066    end Delay_Output;
2067
2068    procedure Flush_Input
2069    is
2070       function Flushinp return C_Int;
2071       pragma Import (C, Flushinp, "flushinp");
2072    begin
2073       if Flushinp = Curses_Err then  -- docu says that never happens, but...
2074          raise Curses_Exception;
2075       end if;
2076    end Flush_Input;
2077 ------------------------------------------------------------------------------
2078    function Baudrate return Natural
2079    is
2080       function Baud return C_Int;
2081       pragma Import (C, Baud, "baudrate");
2082    begin
2083       return Natural (Baud);
2084    end Baudrate;
2085
2086    function Erase_Character return Character
2087    is
2088       function Erasechar return C_Int;
2089       pragma Import (C, Erasechar, "erasechar");
2090    begin
2091       return Character'Val (Erasechar);
2092    end Erase_Character;
2093
2094    function Kill_Character return Character
2095    is
2096       function Killchar return C_Int;
2097       pragma Import (C, Killchar, "killchar");
2098    begin
2099       return Character'Val (Killchar);
2100    end Kill_Character;
2101
2102    function Has_Insert_Character return Boolean
2103    is
2104       function Has_Ic return Curses_Bool;
2105       pragma Import (C, Has_Ic, "has_ic");
2106    begin
2107       if Has_Ic = Curses_Bool_False then
2108          return False;
2109       else
2110          return True;
2111       end if;
2112    end Has_Insert_Character;
2113
2114    function Has_Insert_Line return Boolean
2115    is
2116       function Has_Il return Curses_Bool;
2117       pragma Import (C, Has_Il, "has_il");
2118    begin
2119       if Has_Il = Curses_Bool_False then
2120          return False;
2121       else
2122          return True;
2123       end if;
2124    end Has_Insert_Line;
2125
2126    function Supported_Attributes return Character_Attribute_Set
2127    is
2128       function Termattrs return C_Chtype;
2129       pragma Import (C, Termattrs, "termattrs");
2130
2131       Ch : constant Attributed_Character := Chtype_To_AttrChar (Termattrs);
2132    begin
2133       return Ch.Attr;
2134    end Supported_Attributes;
2135
2136    procedure Long_Name (Name : out String)
2137    is
2138       function Longname return chars_ptr;
2139       pragma Import (C, Longname, "longname");
2140    begin
2141       Fill_String (Longname, Name);
2142    end Long_Name;
2143
2144    function Long_Name return String
2145    is
2146       function Longname return chars_ptr;
2147       pragma Import (C, Longname, "longname");
2148    begin
2149       return Fill_String (Longname);
2150    end Long_Name;
2151
2152    procedure Terminal_Name (Name : out String)
2153    is
2154       function Termname return chars_ptr;
2155       pragma Import (C, Termname, "termname");
2156    begin
2157       Fill_String (Termname, Name);
2158    end Terminal_Name;
2159
2160    function Terminal_Name return String
2161    is
2162       function Termname return chars_ptr;
2163       pragma Import (C, Termname, "termname");
2164    begin
2165       return Fill_String (Termname);
2166    end Terminal_Name;
2167 ------------------------------------------------------------------------------
2168    procedure Init_Pair (Pair : in Redefinable_Color_Pair;
2169                         Fore : in Color_Number;
2170                         Back : in Color_Number)
2171    is
2172       function Initpair (Pair : C_Short;
2173                          Fore : C_Short;
2174                          Back : C_Short) return C_Int;
2175       pragma Import (C, Initpair, "init_pair");
2176    begin
2177       if Integer (Pair) >= Number_Of_Color_Pairs then
2178          raise Constraint_Error;
2179       end if;
2180       if Integer (Fore) >= Number_Of_Colors or else
2181         Integer (Back) >= Number_Of_Colors then raise Constraint_Error;
2182       end if;
2183       if Initpair (C_Short (Pair), C_Short (Fore), C_Short (Back))
2184         = Curses_Err then
2185          raise Curses_Exception;
2186       end if;
2187    end Init_Pair;
2188
2189    procedure Pair_Content (Pair : in Color_Pair;
2190                            Fore : out Color_Number;
2191                            Back : out Color_Number)
2192    is
2193       type C_Short_Access is access all C_Short;
2194       function Paircontent (Pair : C_Short;
2195                             Fp   : C_Short_Access;
2196                             Bp   : C_Short_Access) return C_Int;
2197       pragma Import (C, Paircontent, "pair_content");
2198
2199       F, B : aliased C_Short;
2200    begin
2201       if Paircontent (C_Short (Pair), F'Access, B'Access) = Curses_Err then
2202          raise Curses_Exception;
2203       else
2204          Fore := Color_Number (F);
2205          Back := Color_Number (B);
2206       end if;
2207    end Pair_Content;
2208
2209    function Has_Colors return Boolean
2210    is
2211       function Hascolors return Curses_Bool;
2212       pragma Import (C, Hascolors, "has_colors");
2213    begin
2214       if Hascolors = Curses_Bool_False then
2215          return False;
2216       else
2217          return True;
2218       end if;
2219    end Has_Colors;
2220
2221    procedure Init_Color (Color : in Color_Number;
2222                          Red   : in RGB_Value;
2223                          Green : in RGB_Value;
2224                          Blue  : in RGB_Value)
2225    is
2226       function Initcolor (Col   : C_Short;
2227                           Red   : C_Short;
2228                           Green : C_Short;
2229                           Blue  : C_Short) return C_Int;
2230       pragma Import (C, Initcolor, "init_color");
2231    begin
2232       if Initcolor (C_Short (Color), C_Short (Red), C_Short (Green),
2233                     C_Short (Blue)) = Curses_Err then
2234             raise Curses_Exception;
2235       end if;
2236    end Init_Color;
2237
2238    function Can_Change_Color return Boolean
2239    is
2240       function Canchangecolor return Curses_Bool;
2241       pragma Import (C, Canchangecolor, "can_change_color");
2242    begin
2243       if Canchangecolor = Curses_Bool_False then
2244          return False;
2245       else
2246          return True;
2247       end if;
2248    end Can_Change_Color;
2249
2250    procedure Color_Content (Color : in  Color_Number;
2251                             Red   : out RGB_Value;
2252                             Green : out RGB_Value;
2253                             Blue  : out RGB_Value)
2254    is
2255       type C_Short_Access is access all C_Short;
2256
2257       function Colorcontent (Color : C_Short; R, G, B : C_Short_Access)
2258                              return C_Int;
2259       pragma Import (C, Colorcontent, "color_content");
2260
2261       R, G, B : aliased C_Short;
2262    begin
2263       if Colorcontent (C_Short (Color), R'Access, G'Access, B'Access) =
2264         Curses_Err then
2265          raise Curses_Exception;
2266       else
2267          Red   := RGB_Value (R);
2268          Green := RGB_Value (G);
2269          Blue  := RGB_Value (B);
2270       end if;
2271    end Color_Content;
2272
2273 ------------------------------------------------------------------------------
2274    procedure Save_Curses_Mode (Mode : in Curses_Mode)
2275    is
2276       function Def_Prog_Mode return C_Int;
2277       pragma Import (C, Def_Prog_Mode, "def_prog_mode");
2278       function Def_Shell_Mode return C_Int;
2279       pragma Import (C, Def_Shell_Mode, "def_shell_mode");
2280
2281       Err : C_Int;
2282    begin
2283       case Mode is
2284          when Curses => Err := Def_Prog_Mode;
2285          when Shell  => Err := Def_Shell_Mode;
2286       end case;
2287       if Err = Curses_Err then
2288          raise Curses_Exception;
2289       end if;
2290    end Save_Curses_Mode;
2291
2292    procedure Reset_Curses_Mode (Mode : in Curses_Mode)
2293    is
2294       function Reset_Prog_Mode return C_Int;
2295       pragma Import (C, Reset_Prog_Mode, "reset_prog_mode");
2296       function Reset_Shell_Mode return C_Int;
2297       pragma Import (C, Reset_Shell_Mode, "reset_shell_mode");
2298
2299       Err : C_Int;
2300    begin
2301       case Mode is
2302          when Curses => Err := Reset_Prog_Mode;
2303          when Shell  => Err := Reset_Shell_Mode;
2304       end case;
2305       if Err = Curses_Err then
2306          raise Curses_Exception;
2307       end if;
2308    end Reset_Curses_Mode;
2309
2310    procedure Save_Terminal_State
2311    is
2312       function Savetty return C_Int;
2313       pragma Import (C, Savetty, "savetty");
2314    begin
2315       if Savetty = Curses_Err then
2316          raise Curses_Exception;
2317       end if;
2318    end Save_Terminal_State;
2319
2320    procedure Reset_Terminal_State
2321    is
2322       function Resetty return C_Int;
2323       pragma Import (C, Resetty, "resetty");
2324    begin
2325       if Resetty = Curses_Err then
2326          raise Curses_Exception;
2327       end if;
2328    end Reset_Terminal_State;
2329
2330    procedure Rip_Off_Lines (Lines : in Integer;
2331                             Proc  : in Stdscr_Init_Proc)
2332    is
2333       function Ripoffline (Lines : C_Int;
2334                            Proc  : Stdscr_Init_Proc) return C_Int;
2335       pragma Import (C, Ripoffline, "_nc_ripoffline");
2336    begin
2337       if Ripoffline (C_Int (Lines), Proc) = Curses_Err then
2338          raise Curses_Exception;
2339       end if;
2340    end Rip_Off_Lines;
2341
2342    procedure Set_Cursor_Visibility (Visibility : in out Cursor_Visibility)
2343    is
2344       function Curs_Set (Curs : C_Int) return C_Int;
2345       pragma Import (C, Curs_Set, "curs_set");
2346
2347       Res : C_Int;
2348    begin
2349       Res := Curs_Set (Cursor_Visibility'Pos (Visibility));
2350       if Res /= Curses_Err then
2351          Visibility := Cursor_Visibility'Val (Res);
2352       end if;
2353    end Set_Cursor_Visibility;
2354
2355    procedure Nap_Milli_Seconds (Ms : in Natural)
2356    is
2357       function Napms (Ms : C_Int) return C_Int;
2358       pragma Import (C, Napms, "napms");
2359    begin
2360       if Napms (C_Int (Ms)) = Curses_Err then
2361          raise Curses_Exception;
2362       end if;
2363    end Nap_Milli_Seconds;
2364 ------------------------------------------------------------------------------
2365
2366    function Standard_Window return Window
2367    is
2368       Stdscr : Window;
2369       pragma Import (C, Stdscr, "stdscr");
2370    begin
2371       return Stdscr;
2372    end Standard_Window;
2373
2374    function Lines return Line_Count
2375    is
2376       C_Lines : C_Int;
2377       pragma Import (C, C_Lines, "LINES");
2378    begin
2379       return Line_Count (C_Lines);
2380    end Lines;
2381
2382    function Columns return Column_Count
2383    is
2384       C_Columns : C_Int;
2385       pragma Import (C, C_Columns, "COLS");
2386    begin
2387       return Column_Count (C_Columns);
2388    end Columns;
2389
2390    function Tab_Size return Natural
2391    is
2392       C_Tab_Size : C_Int;
2393       pragma Import (C, C_Tab_Size, "TABSIZE");
2394    begin
2395       return Natural (C_Tab_Size);
2396    end Tab_Size;
2397
2398    function Number_Of_Colors return Natural
2399    is
2400       C_Number_Of_Colors : C_Int;
2401       pragma Import (C, C_Number_Of_Colors, "COLORS");
2402    begin
2403       return Natural (C_Number_Of_Colors);
2404    end Number_Of_Colors;
2405
2406    function Number_Of_Color_Pairs return Natural
2407    is
2408       C_Number_Of_Color_Pairs : C_Int;
2409       pragma Import (C, C_Number_Of_Color_Pairs, "COLOR_PAIRS");
2410    begin
2411       return Natural (C_Number_Of_Color_Pairs);
2412    end Number_Of_Color_Pairs;
2413 ------------------------------------------------------------------------------
2414    procedure Transform_Coordinates
2415      (W      : in Window := Standard_Window;
2416       Line   : in out Line_Position;
2417       Column : in out Column_Position;
2418       Dir    : in Transform_Direction := From_Screen)
2419    is
2420       type Int_Access is access all C_Int;
2421       function Transform (W    : Window;
2422                           Y, X : Int_Access;
2423                           Dir  : Curses_Bool) return C_Int;
2424       pragma Import (C, Transform, "wmouse_trafo");
2425
2426       X : aliased C_Int := C_Int (Column);
2427       Y : aliased C_Int := C_Int (Line);
2428       D : Curses_Bool := Curses_Bool_False;
2429       R : C_Int;
2430    begin
2431       if Dir = To_Screen then
2432          D := 1;
2433       end if;
2434       R := Transform (W, Y'Access, X'Access, D);
2435       if R = Curses_False then
2436          raise Curses_Exception;
2437       else
2438          Line   := Line_Position (Y);
2439          Column := Column_Position (X);
2440       end if;
2441    end Transform_Coordinates;
2442 ------------------------------------------------------------------------------
2443    procedure Use_Default_Colors is
2444       function C_Use_Default_Colors return C_Int;
2445       pragma Import (C, C_Use_Default_Colors, "use_default_colors");
2446       Err : constant C_Int := C_Use_Default_Colors;
2447    begin
2448       if Err = Curses_Err then
2449          raise Curses_Exception;
2450       end if;
2451    end Use_Default_Colors;
2452
2453    procedure Assume_Default_Colors (Fore : Color_Number := Default_Color;
2454                                     Back : Color_Number := Default_Color)
2455    is
2456       function C_Assume_Default_Colors (Fore : C_Int;
2457                                         Back : C_Int) return C_Int;
2458       pragma Import (C, C_Assume_Default_Colors, "assume_default_colors");
2459
2460       Err : constant C_Int := C_Assume_Default_Colors (C_Int (Fore),
2461                                                        C_Int (Black));
2462    begin
2463       if Err = Curses_Err then
2464          raise Curses_Exception;
2465       end if;
2466    end Assume_Default_Colors;
2467 ------------------------------------------------------------------------------
2468    function Curses_Version return String
2469    is
2470       function curses_versionC return chars_ptr;
2471       pragma Import (C, curses_versionC, "curses_version");
2472       Result : constant chars_ptr := curses_versionC;
2473    begin
2474       return Fill_String (Result);
2475    end Curses_Version;
2476 ------------------------------------------------------------------------------
2477    function Use_Extended_Names (Enable : Boolean) return Boolean
2478    is
2479       function use_extended_namesC (e : Curses_Bool) return C_Int;
2480       pragma Import (C, use_extended_namesC, "use_extended_names");
2481
2482       Res : constant C_Int :=
2483          use_extended_namesC (Curses_Bool (Boolean'Pos (Enable)));
2484    begin
2485       if Res = C_Int (Curses_Bool_False) then
2486          return False;
2487       else
2488          return True;
2489       end if;
2490    end Use_Extended_Names;
2491 ------------------------------------------------------------------------------
2492    procedure Screen_Dump_To_File (Filename : in String)
2493    is
2494       function scr_dump (f : char_array) return C_Int;
2495       pragma Import (C, scr_dump, "scr_dump");
2496       Txt    : char_array (0 .. Filename'Length);
2497       Length : size_t;
2498    begin
2499       To_C (Filename, Txt, Length);
2500       if Curses_Err = scr_dump (Txt) then
2501          raise Curses_Exception;
2502       end if;
2503    end Screen_Dump_To_File;
2504
2505    procedure Screen_Restore_From_File (Filename : in String)
2506    is
2507       function scr_restore (f : char_array) return C_Int;
2508       pragma Import (C, scr_restore, "scr_restore");
2509       Txt    : char_array (0 .. Filename'Length);
2510       Length : size_t;
2511    begin
2512       To_C (Filename, Txt, Length);
2513       if Curses_Err = scr_restore (Txt)  then
2514          raise Curses_Exception;
2515       end if;
2516    end Screen_Restore_From_File;
2517
2518    procedure Screen_Init_From_File (Filename : in String)
2519    is
2520       function scr_init (f : char_array) return C_Int;
2521       pragma Import (C, scr_init, "scr_init");
2522       Txt    : char_array (0 .. Filename'Length);
2523       Length : size_t;
2524    begin
2525       To_C (Filename, Txt, Length);
2526       if Curses_Err = scr_init (Txt) then
2527          raise Curses_Exception;
2528       end if;
2529    end Screen_Init_From_File;
2530
2531    procedure Screen_Set_File (Filename : in String)
2532    is
2533       function scr_set (f : char_array) return C_Int;
2534       pragma Import (C, scr_set, "scr_set");
2535       Txt    : char_array (0 .. Filename'Length);
2536       Length : size_t;
2537    begin
2538       To_C (Filename, Txt, Length);
2539       if Curses_Err = scr_set (Txt) then
2540          raise Curses_Exception;
2541       end if;
2542    end Screen_Set_File;
2543 ------------------------------------------------------------------------------
2544    procedure Resize (Win               : Window := Standard_Window;
2545                      Number_Of_Lines   : Line_Count;
2546                      Number_Of_Columns : Column_Count) is
2547       function wresize (win     : Window;
2548                         lines   : C_Int;
2549                         columns : C_Int) return C_Int;
2550       pragma Import (C, wresize);
2551    begin
2552       if wresize (Win,
2553                   C_Int (Number_Of_Lines),
2554                   C_Int (Number_Of_Columns)) = Curses_Err then
2555          raise Curses_Exception;
2556       end if;
2557    end Resize;
2558 ------------------------------------------------------------------------------
2559
2560 end Terminal_Interface.Curses;
2561