1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding --
5 -- Terminal_Interface.Curses.Forms.Field_Types --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 1998-2009,2011 Free Software Foundation, Inc. --
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: --
20 -- The above copyright notice and this permission notice shall be included --
21 -- in all copies or substantial portions of the Software. --
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. --
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 --
35 ------------------------------------------------------------------------------
36 -- Author: Juergen Pfeifer, 1996
39 -- $Date: 2011/03/22 23:22:27 $
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;
47 -- |=====================================================================
48 -- | man page form_fieldtype.3x
49 -- |=====================================================================
51 package body Terminal_Interface.Curses.Forms.Field_Types is
53 use type System.Address;
55 package Argument_Conversions is
56 new System.Address_To_Access_Conversions (Argument);
58 function Get_Fieldtype (F : Field) return C_Field_Type;
59 pragma Import (C, Get_Fieldtype, "field_type");
61 function Get_Arg (F : Field) return System.Address;
62 pragma Import (C, Get_Arg, "field_arg");
64 -- |=====================================================================
65 -- | man page form_field_validation.3x
66 -- |=====================================================================
70 function Get_Type (Fld : Field) return Field_Type_Access
72 Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
73 Arg : Argument_Access;
75 if Low_Level = Null_Field_Type then
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)));
95 function Copy_Arg (Usr : System.Address) return System.Address
101 procedure Free_Arg (Usr : System.Address)
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);
108 To_Be_Free : Argument_Access
109 := Argument_Access (Argument_Conversions.To_Pointer (Usr));
110 Low_Level : C_Field_Type;
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);
119 if To_Be_Free.all.Typ /= null then
120 Free_Type (To_Be_Free.all.Typ);
122 Freeargs (To_Be_Free);
126 procedure Wrap_Builtin (Fld : Field;
127 Typ : Field_Type'Class;
128 Cft : C_Field_Type := C_Builtin_Router)
130 Usr_Arg : constant System.Address := Get_Arg (Fld);
131 Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
132 Arg : Argument_Access;
134 function Set_Fld_Type (F : Field := Fld;
135 Cf : C_Field_Type := Cft;
136 Arg1 : Argument_Access) return C_Int;
137 pragma Import (C, Set_Fld_Type, "set_field_type_user");
140 pragma Assert (Low_Level /= Null_Field_Type);
141 if Cft /= C_Builtin_Router and then Cft /= C_Choice_Router then
142 raise Form_Exception;
144 Arg := new Argument'(Usr => System.Null_Address,
145 Typ => new Field_Type'Class'(Typ),
146 Cft => Get_Fieldtype (Fld));
147 if Usr_Arg /= System.Null_Address then
148 if Low_Level.all.Copyarg /= null then
149 Arg.all.Usr := Low_Level.all.Copyarg (Usr_Arg);
151 Arg.all.Usr := Usr_Arg;
155 Res := Set_Fld_Type (Arg1 => Arg);
162 function Field_Check_Router (Fld : Field;
163 Usr : System.Address) return Curses_Bool
165 Arg : constant Argument_Access
166 := Argument_Access (Argument_Conversions.To_Pointer (Usr));
168 pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
169 and then Arg.all.Typ /= null);
170 if Arg.all.Cft.all.Fcheck /= null then
171 return Arg.all.Cft.all.Fcheck (Fld, Arg.all.Usr);
175 end Field_Check_Router;
177 function Char_Check_Router (Ch : C_Int;
178 Usr : System.Address) return Curses_Bool
180 Arg : constant Argument_Access
181 := Argument_Access (Argument_Conversions.To_Pointer (Usr));
183 pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
184 and then Arg.all.Typ /= null);
185 if Arg.all.Cft.all.Ccheck /= null then
186 return Arg.all.Cft.all.Ccheck (Ch, Arg.all.Usr);
190 end Char_Check_Router;
192 function Next_Router (Fld : Field;
193 Usr : System.Address) return Curses_Bool
195 Arg : constant Argument_Access
196 := Argument_Access (Argument_Conversions.To_Pointer (Usr));
198 pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
199 and then Arg.all.Typ /= null);
200 if Arg.all.Cft.all.Next /= null then
201 return Arg.all.Cft.all.Next (Fld, Arg.all.Usr);
207 function Prev_Router (Fld : Field;
208 Usr : System.Address) return Curses_Bool
210 Arg : constant Argument_Access :=
211 Argument_Access (Argument_Conversions.To_Pointer (Usr));
213 pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
214 and then Arg.all.Typ /= null);
215 if Arg.all.Cft.all.Prev /= null then
216 return Arg.all.Cft.all.Prev (Fld, Arg.all.Usr);
222 -- -----------------------------------------------------------------------
224 function C_Builtin_Router return C_Field_Type
229 if M_Builtin_Router = Null_Field_Type then
230 T := New_Fieldtype (Field_Check_Router'Access,
231 Char_Check_Router'Access);
232 if T = Null_Field_Type then
233 raise Form_Exception;
235 Res := Set_Fieldtype_Arg (T,
243 M_Builtin_Router := T;
245 pragma Assert (M_Builtin_Router /= Null_Field_Type);
246 return M_Builtin_Router;
247 end C_Builtin_Router;
249 -- -----------------------------------------------------------------------
251 function C_Choice_Router return C_Field_Type
256 if M_Choice_Router = Null_Field_Type then
257 T := New_Fieldtype (Field_Check_Router'Access,
258 Char_Check_Router'Access);
259 if T = Null_Field_Type then
260 raise Form_Exception;
262 Res := Set_Fieldtype_Arg (T,
270 Res := Set_Fieldtype_Choice (T,
277 M_Choice_Router := T;
279 pragma Assert (M_Choice_Router /= Null_Field_Type);
280 return M_Choice_Router;
283 end Terminal_Interface.Curses.Forms.Field_Types;