]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/src/terminal_interface-curses-forms-field_types.adb
99ee3741f97f9f7f6e8d7219a982c9f3305090e6
[ncurses.git] / Ada95 / src / terminal_interface-curses-forms-field_types.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                           GNAT ncurses Binding                           --
4 --                                                                          --
5 --                 Terminal_Interface.Curses.Forms.Field_Types              --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 1998-2011,2014 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 --  Version Control:
38 --  $Revision: 1.27 $
39 --  $Date: 2014/05/24 21:31:05 $
40 --  Binding Version 01.00
41 ------------------------------------------------------------------------------
42 with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
43 with Ada.Unchecked_Deallocation;
44 with System.Address_To_Access_Conversions;
45
46 --  |
47 --  |=====================================================================
48 --  | man page form_fieldtype.3x
49 --  |=====================================================================
50 --  |
51 package body Terminal_Interface.Curses.Forms.Field_Types is
52
53    use type System.Address;
54
55    package Argument_Conversions is
56       new System.Address_To_Access_Conversions (Argument);
57
58    function Get_Fieldtype (F : Field) return C_Field_Type;
59    pragma Import (C, Get_Fieldtype, "field_type");
60
61    function Get_Arg (F : Field) return System.Address;
62    pragma Import (C, Get_Arg, "field_arg");
63    --  |
64    --  |=====================================================================
65    --  | man page form_field_validation.3x
66    --  |=====================================================================
67    --  |
68    --  |
69    --  |
70    function Get_Type (Fld : Field) return Field_Type_Access
71    is
72       Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
73       Arg : Argument_Access;
74    begin
75       if Low_Level = Null_Field_Type then
76          return null;
77       else
78          if Low_Level = M_Builtin_Router or else
79            Low_Level = M_Generic_Type or else
80            Low_Level = M_Choice_Router or else
81            Low_Level = M_Generic_Choice then
82             Arg := Argument_Access
83          (Argument_Conversions.To_Pointer (Get_Arg (Fld)));
84             if Arg = null then
85                raise Form_Exception;
86             else
87                return Arg.all.Typ;
88             end if;
89          else
90             raise Form_Exception;
91          end if;
92       end if;
93    end Get_Type;
94
95    function Copy_Arg (Usr : System.Address) return System.Address
96    is
97    begin
98       return Usr;
99    end Copy_Arg;
100
101    procedure Free_Arg (Usr : System.Address)
102    is
103       procedure Free_Type is new Ada.Unchecked_Deallocation
104         (Field_Type'Class, Field_Type_Access);
105       procedure Freeargs is new Ada.Unchecked_Deallocation
106         (Argument, Argument_Access);
107
108       To_Be_Free : Argument_Access
109    := Argument_Access (Argument_Conversions.To_Pointer (Usr));
110       Low_Level  : C_Field_Type;
111    begin
112       if To_Be_Free /= null then
113          if To_Be_Free.all.Usr /= System.Null_Address then
114             Low_Level := To_Be_Free.all.Cft;
115             if Low_Level.all.Freearg /= null then
116                Low_Level.all.Freearg (To_Be_Free.all.Usr);
117             end if;
118          end if;
119          if To_Be_Free.all.Typ /= null then
120             Free_Type (To_Be_Free.all.Typ);
121          end if;
122          Freeargs (To_Be_Free);
123       end if;
124    end Free_Arg;
125
126    procedure Wrap_Builtin (Fld : Field;
127                            Typ : Field_Type'Class;
128                            Cft : C_Field_Type := C_Builtin_Router)
129    is
130       Usr_Arg   : constant System.Address := Get_Arg (Fld);
131       Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
132       Arg : Argument_Access;
133       function Set_Fld_Type (F    : Field := Fld;
134                              Cf   : C_Field_Type := Cft;
135                              Arg1 : Argument_Access) return Eti_Error;
136       pragma Import (C, Set_Fld_Type, "set_field_type_user");
137
138    begin
139       pragma Assert (Low_Level /= Null_Field_Type);
140       if Cft /= C_Builtin_Router and then Cft /= C_Choice_Router then
141          raise Form_Exception;
142       else
143          Arg := new Argument'(Usr => System.Null_Address,
144                               Typ => new Field_Type'Class'(Typ),
145                               Cft => Get_Fieldtype (Fld));
146          if Usr_Arg /= System.Null_Address then
147             if Low_Level.all.Copyarg /= null then
148                Arg.all.Usr := Low_Level.all.Copyarg (Usr_Arg);
149             else
150                Arg.all.Usr := Usr_Arg;
151             end if;
152          end if;
153
154          Eti_Exception (Set_Fld_Type (Arg1 => Arg));
155       end if;
156    end Wrap_Builtin;
157
158    function Field_Check_Router (Fld : Field;
159                                 Usr : System.Address) return Curses_Bool
160    is
161       Arg  : constant Argument_Access
162    := Argument_Access (Argument_Conversions.To_Pointer (Usr));
163    begin
164       pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
165                      and then Arg.all.Typ /= null);
166       if Arg.all.Cft.all.Fcheck /= null then
167          return Arg.all.Cft.all.Fcheck (Fld, Arg.all.Usr);
168       else
169          return 1;
170       end if;
171    end Field_Check_Router;
172
173    function Char_Check_Router (Ch  : C_Int;
174                                Usr : System.Address) return Curses_Bool
175    is
176       Arg  : constant Argument_Access
177    := Argument_Access (Argument_Conversions.To_Pointer (Usr));
178    begin
179       pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
180                      and then Arg.all.Typ /= null);
181       if Arg.all.Cft.all.Ccheck /= null then
182          return Arg.all.Cft.all.Ccheck (Ch, Arg.all.Usr);
183       else
184          return 1;
185       end if;
186    end Char_Check_Router;
187
188    function Next_Router (Fld : Field;
189                          Usr : System.Address) return Curses_Bool
190    is
191       Arg  : constant Argument_Access
192    := Argument_Access (Argument_Conversions.To_Pointer (Usr));
193    begin
194       pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
195                      and then Arg.all.Typ /= null);
196       if Arg.all.Cft.all.Next /= null then
197          return Arg.all.Cft.all.Next (Fld, Arg.all.Usr);
198       else
199          return 1;
200       end if;
201    end Next_Router;
202
203    function Prev_Router (Fld : Field;
204                          Usr : System.Address) return Curses_Bool
205    is
206       Arg  : constant Argument_Access :=
207                Argument_Access (Argument_Conversions.To_Pointer (Usr));
208    begin
209       pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
210                      and then Arg.all.Typ /= null);
211       if Arg.all.Cft.all.Prev /= null then
212          return Arg.all.Cft.all.Prev (Fld, Arg.all.Usr);
213       else
214          return 1;
215       end if;
216    end Prev_Router;
217
218    --  -----------------------------------------------------------------------
219    --
220    function C_Builtin_Router return C_Field_Type
221    is
222       T   : C_Field_Type;
223    begin
224       if M_Builtin_Router = Null_Field_Type then
225          T := New_Fieldtype (Field_Check_Router'Access,
226                              Char_Check_Router'Access);
227          if T = Null_Field_Type then
228             raise Form_Exception;
229          else
230             Eti_Exception (Set_Fieldtype_Arg (T,
231                                               Make_Arg'Access,
232                                               Copy_Arg'Access,
233                                               Free_Arg'Access));
234          end if;
235          M_Builtin_Router := T;
236       end if;
237       pragma Assert (M_Builtin_Router /= Null_Field_Type);
238       return M_Builtin_Router;
239    end C_Builtin_Router;
240
241    --  -----------------------------------------------------------------------
242    --
243    function C_Choice_Router return C_Field_Type
244    is
245       T   : C_Field_Type;
246    begin
247       if M_Choice_Router = Null_Field_Type then
248          T := New_Fieldtype (Field_Check_Router'Access,
249                              Char_Check_Router'Access);
250          if T = Null_Field_Type then
251             raise Form_Exception;
252          else
253             Eti_Exception (Set_Fieldtype_Arg (T,
254                                               Make_Arg'Access,
255                                               Copy_Arg'Access,
256                                               Free_Arg'Access));
257
258             Eti_Exception (Set_Fieldtype_Choice (T,
259                                                  Next_Router'Access,
260                                                  Prev_Router'Access));
261          end if;
262          M_Choice_Router := T;
263       end if;
264       pragma Assert (M_Choice_Router /= Null_Field_Type);
265       return M_Choice_Router;
266    end C_Choice_Router;
267
268 end Terminal_Interface.Curses.Forms.Field_Types;