1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding --
5 -- Terminal_Interface.Curses.Forms.Field_Types --
9 ------------------------------------------------------------------------------
10 -- Copyright 2020 Thomas E. Dickey --
11 -- Copyright 1999-2011,2014 Free Software Foundation, Inc. --
13 -- Permission is hereby granted, free of charge, to any person obtaining a --
14 -- copy of this software and associated documentation files (the --
15 -- "Software"), to deal in the Software without restriction, including --
16 -- without limitation the rights to use, copy, modify, merge, publish, --
17 -- distribute, distribute with modifications, sublicense, and/or sell --
18 -- copies of the Software, and to permit persons to whom the Software is --
19 -- furnished to do so, subject to the following conditions: --
21 -- The above copyright notice and this permission notice shall be included --
22 -- in all copies or substantial portions of the Software. --
24 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
25 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
26 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
27 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
28 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
29 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
30 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
32 -- Except as contained in this notice, the name(s) of the above copyright --
33 -- holders shall not be used in advertising or otherwise to promote the --
34 -- sale, use or other dealings in this Software without prior written --
36 ------------------------------------------------------------------------------
37 -- Author: Juergen Pfeifer, 1996
40 -- $Date: 2020/02/02 23:34:34 $
41 -- Binding Version 01.00
42 ------------------------------------------------------------------------------
43 with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
44 with Ada.Unchecked_Deallocation;
45 with System.Address_To_Access_Conversions;
48 -- |=====================================================================
49 -- | man page form_fieldtype.3x
50 -- |=====================================================================
52 package body Terminal_Interface.Curses.Forms.Field_Types is
54 use type System.Address;
56 package Argument_Conversions is
57 new System.Address_To_Access_Conversions (Argument);
59 function Get_Fieldtype (F : Field) return C_Field_Type;
60 pragma Import (C, Get_Fieldtype, "field_type");
62 function Get_Arg (F : Field) return System.Address;
63 pragma Import (C, Get_Arg, "field_arg");
65 -- |=====================================================================
66 -- | man page form_field_validation.3x
67 -- |=====================================================================
71 function Get_Type (Fld : Field) return Field_Type_Access
73 Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
74 Arg : Argument_Access;
76 if Low_Level = Null_Field_Type then
79 if Low_Level = M_Builtin_Router or else
80 Low_Level = M_Generic_Type or else
81 Low_Level = M_Choice_Router or else
82 Low_Level = M_Generic_Choice
84 Arg := Argument_Access
85 (Argument_Conversions.To_Pointer (Get_Arg (Fld)));
97 function Copy_Arg (Usr : System.Address) return System.Address
103 procedure Free_Arg (Usr : System.Address)
105 procedure Free_Type is new Ada.Unchecked_Deallocation
106 (Field_Type'Class, Field_Type_Access);
107 procedure Freeargs is new Ada.Unchecked_Deallocation
108 (Argument, Argument_Access);
110 To_Be_Free : Argument_Access
111 := Argument_Access (Argument_Conversions.To_Pointer (Usr));
112 Low_Level : C_Field_Type;
114 if To_Be_Free /= null then
115 if To_Be_Free.all.Usr /= System.Null_Address then
116 Low_Level := To_Be_Free.all.Cft;
117 if Low_Level.all.Freearg /= null then
118 Low_Level.all.Freearg (To_Be_Free.all.Usr);
121 if To_Be_Free.all.Typ /= null then
122 Free_Type (To_Be_Free.all.Typ);
124 Freeargs (To_Be_Free);
128 procedure Wrap_Builtin (Fld : Field;
129 Typ : Field_Type'Class;
130 Cft : C_Field_Type := C_Builtin_Router)
132 Usr_Arg : constant System.Address := Get_Arg (Fld);
133 Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
134 Arg : Argument_Access;
135 function Set_Fld_Type (F : Field := Fld;
136 Cf : C_Field_Type := Cft;
137 Arg1 : Argument_Access) return Eti_Error;
138 pragma Import (C, Set_Fld_Type, "set_field_type_user");
141 pragma Assert (Low_Level /= Null_Field_Type);
142 if Cft /= C_Builtin_Router and then Cft /= C_Choice_Router then
143 raise Form_Exception;
145 Arg := new Argument'(Usr => System.Null_Address,
146 Typ => new Field_Type'Class'(Typ),
147 Cft => Get_Fieldtype (Fld));
148 if Usr_Arg /= System.Null_Address then
149 if Low_Level.all.Copyarg /= null then
150 Arg.all.Usr := Low_Level.all.Copyarg (Usr_Arg);
152 Arg.all.Usr := Usr_Arg;
156 Eti_Exception (Set_Fld_Type (Arg1 => Arg));
160 function Field_Check_Router (Fld : Field;
161 Usr : System.Address) return Curses_Bool
163 Arg : constant Argument_Access
164 := Argument_Access (Argument_Conversions.To_Pointer (Usr));
166 pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
167 and then Arg.all.Typ /= null);
168 if Arg.all.Cft.all.Fcheck /= null then
169 return Arg.all.Cft.all.Fcheck (Fld, Arg.all.Usr);
173 end Field_Check_Router;
175 function Char_Check_Router (Ch : C_Int;
176 Usr : System.Address) return Curses_Bool
178 Arg : constant Argument_Access
179 := Argument_Access (Argument_Conversions.To_Pointer (Usr));
181 pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
182 and then Arg.all.Typ /= null);
183 if Arg.all.Cft.all.Ccheck /= null then
184 return Arg.all.Cft.all.Ccheck (Ch, Arg.all.Usr);
188 end Char_Check_Router;
190 function Next_Router (Fld : Field;
191 Usr : System.Address) return Curses_Bool
193 Arg : constant Argument_Access
194 := Argument_Access (Argument_Conversions.To_Pointer (Usr));
196 pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
197 and then Arg.all.Typ /= null);
198 if Arg.all.Cft.all.Next /= null then
199 return Arg.all.Cft.all.Next (Fld, Arg.all.Usr);
205 function Prev_Router (Fld : Field;
206 Usr : System.Address) return Curses_Bool
208 Arg : constant Argument_Access :=
209 Argument_Access (Argument_Conversions.To_Pointer (Usr));
211 pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
212 and then Arg.all.Typ /= null);
213 if Arg.all.Cft.all.Prev /= null then
214 return Arg.all.Cft.all.Prev (Fld, Arg.all.Usr);
220 -- -----------------------------------------------------------------------
222 function C_Builtin_Router return C_Field_Type
226 if M_Builtin_Router = Null_Field_Type then
227 T := New_Fieldtype (Field_Check_Router'Access,
228 Char_Check_Router'Access);
229 if T = Null_Field_Type then
230 raise Form_Exception;
232 Eti_Exception (Set_Fieldtype_Arg (T,
237 M_Builtin_Router := T;
239 pragma Assert (M_Builtin_Router /= Null_Field_Type);
240 return M_Builtin_Router;
241 end C_Builtin_Router;
243 -- -----------------------------------------------------------------------
245 function C_Choice_Router return C_Field_Type
249 if M_Choice_Router = Null_Field_Type then
250 T := New_Fieldtype (Field_Check_Router'Access,
251 Char_Check_Router'Access);
252 if T = Null_Field_Type then
253 raise Form_Exception;
255 Eti_Exception (Set_Fieldtype_Arg (T,
260 Eti_Exception (Set_Fieldtype_Choice (T,
262 Prev_Router'Access));
264 M_Choice_Router := T;
266 pragma Assert (M_Choice_Router /= Null_Field_Type);
267 return M_Choice_Router;
270 end Terminal_Interface.Curses.Forms.Field_Types;