]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/src/terminal_interface-curses-forms-field_types.adb
ncurses 5.0
[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 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 <juergen.pfeifer@gmx.net> 1996
37 --  Version Control:
38 --  $Revision: 1.12 $
39 --  Binding Version 01.00
40 ------------------------------------------------------------------------------
41 with Interfaces.C;
42 with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
43 with Ada.Unchecked_Deallocation;
44 with Ada.Unchecked_Conversion;
45 --  |
46 --  |=====================================================================
47 --  | man page form_fieldtype.3x
48 --  |=====================================================================
49 --  |
50 package body Terminal_Interface.Curses.Forms.Field_Types is
51
52    use type Interfaces.C.int;
53    use type System.Address;
54
55    function To_Argument_Access is new Ada.Unchecked_Conversion
56      (System.Address, Argument_Access);
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 : in 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 := To_Argument_Access (Get_Arg (Fld));
83             if Arg = null then
84                raise Form_Exception;
85             else
86                return Arg.Typ;
87             end if;
88          else
89             raise Form_Exception;
90          end if;
91       end if;
92    end Get_Type;
93
94    function Make_Arg (Args : System.Address) return System.Address
95    is
96       --  Actually args is a double indirected pointer to the arguments
97       --  of a C variable argument list. In theory it is now quite
98       --  complicated to write portable routine that reads the arguments,
99       --  because one has to know the growth direction of the stack and
100       --  the sizes of the individual arguments.
101       --  Fortunately we are only interested in the first argument (#0),
102       --  we know its size and for the first arg we don't care about
103       --  into which stack direction we have to proceed. We simply
104       --  resolve the double indirection and thats it.
105       type V is access all System.Address;
106       function To_Access is new Ada.Unchecked_Conversion (System.Address,
107                                                           V);
108    begin
109       return To_Access (To_Access (Args).all).all;
110    end Make_Arg;
111
112    function Copy_Arg (Usr : System.Address) return System.Address
113    is
114    begin
115       return Usr;
116    end Copy_Arg;
117
118    procedure Free_Arg (Usr : in System.Address)
119    is
120       procedure Free_Type is new Ada.Unchecked_Deallocation
121         (Field_Type'Class, Field_Type_Access);
122       procedure Freeargs is new Ada.Unchecked_Deallocation
123         (Argument, Argument_Access);
124
125       To_Be_Free : Argument_Access := To_Argument_Access (Usr);
126       Low_Level  : C_Field_Type;
127    begin
128       if To_Be_Free /= null then
129          if To_Be_Free.Usr /= System.Null_Address then
130             Low_Level := To_Be_Free.Cft;
131             if Low_Level.Freearg /= null then
132                Low_Level.Freearg (To_Be_Free.Usr);
133             end if;
134          end if;
135          if To_Be_Free.Typ /= null then
136             Free_Type (To_Be_Free.Typ);
137          end if;
138          Freeargs (To_Be_Free);
139       end if;
140    end Free_Arg;
141
142
143    procedure Wrap_Builtin (Fld : Field;
144                            Typ : Field_Type'Class;
145                            Cft : C_Field_Type := C_Builtin_Router)
146    is
147       Usr_Arg   : System.Address := Get_Arg (Fld);
148       Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
149       Arg : Argument_Access;
150       Res : Eti_Error;
151       function Set_Fld_Type (F    : Field := Fld;
152                              Cf   : C_Field_Type := Cft;
153                              Arg1 : Argument_Access) return C_Int;
154       pragma Import (C, Set_Fld_Type, "set_field_type");
155
156    begin
157       pragma Assert (Low_Level /= Null_Field_Type);
158       if Cft /= C_Builtin_Router and then Cft /= C_Choice_Router then
159          raise Form_Exception;
160       else
161          Arg := new Argument'(Usr => System.Null_Address,
162                               Typ => new Field_Type'Class'(Typ),
163                               Cft => Get_Fieldtype (Fld));
164          if Usr_Arg /= System.Null_Address then
165             if Low_Level.Copyarg /= null then
166                Arg.Usr := Low_Level.Copyarg (Usr_Arg);
167             else
168                Arg.Usr := Usr_Arg;
169             end if;
170          end if;
171
172          Res := Set_Fld_Type (Arg1 => Arg);
173          if Res /= E_Ok then
174             Eti_Exception (Res);
175          end if;
176       end if;
177    end Wrap_Builtin;
178
179    function Field_Check_Router (Fld : Field;
180                                 Usr : System.Address) return C_Int
181    is
182       Arg  : constant Argument_Access := To_Argument_Access (Usr);
183    begin
184       pragma Assert (Arg /= null and then Arg.Cft /= Null_Field_Type
185                      and then Arg.Typ /= null);
186       if Arg.Cft.Fcheck /= null then
187          return Arg.Cft.Fcheck (Fld, Arg.Usr);
188       else
189          return 1;
190       end if;
191    end Field_Check_Router;
192
193    function Char_Check_Router (Ch  : C_Int;
194                                Usr : System.Address) return C_Int
195    is
196       Arg  : constant Argument_Access := To_Argument_Access (Usr);
197    begin
198       pragma Assert (Arg /= null and then Arg.Cft /= Null_Field_Type
199                      and then Arg.Typ /= null);
200       if Arg.Cft.Ccheck /= null then
201          return Arg.Cft.Ccheck (Ch, Arg.Usr);
202       else
203          return 1;
204       end if;
205    end Char_Check_Router;
206
207    function Next_Router (Fld : Field;
208                          Usr : System.Address) return C_Int
209    is
210       Arg  : constant Argument_Access := To_Argument_Access (Usr);
211    begin
212       pragma Assert (Arg /= null and then Arg.Cft /= Null_Field_Type
213                      and then Arg.Typ /= null);
214       if Arg.Cft.Next /= null then
215          return Arg.Cft.Next (Fld, Arg.Usr);
216       else
217          return 1;
218       end if;
219    end Next_Router;
220
221    function Prev_Router (Fld : Field;
222                          Usr : System.Address) return C_Int
223    is
224       Arg  : constant Argument_Access := To_Argument_Access (Usr);
225    begin
226       pragma Assert (Arg /= null and then Arg.Cft /= Null_Field_Type
227                      and then Arg.Typ /= null);
228       if Arg.Cft.Prev /= null then
229          return Arg.Cft.Prev (Fld, Arg.Usr);
230       else
231          return 1;
232       end if;
233    end Prev_Router;
234
235    --  -----------------------------------------------------------------------
236    --
237    function C_Builtin_Router return C_Field_Type
238    is
239       Res : Eti_Error;
240       T   : C_Field_Type;
241    begin
242       if M_Builtin_Router = Null_Field_Type then
243          T := New_Fieldtype (Field_Check_Router'Access,
244                              Char_Check_Router'Access);
245          if T = Null_Field_Type then
246             raise Form_Exception;
247          else
248             Res := Set_Fieldtype_Arg (T,
249                                       Make_Arg'Access,
250                                       Copy_Arg'Access,
251                                       Free_Arg'Access);
252             if Res /= E_Ok then
253                Eti_Exception (Res);
254             end if;
255          end if;
256          M_Builtin_Router := T;
257       end if;
258       pragma Assert (M_Builtin_Router /= Null_Field_Type);
259       return M_Builtin_Router;
260    end C_Builtin_Router;
261
262    --  -----------------------------------------------------------------------
263    --
264    function C_Choice_Router return C_Field_Type
265    is
266       Res : Eti_Error;
267       T   : C_Field_Type;
268    begin
269       if M_Choice_Router = Null_Field_Type then
270          T := New_Fieldtype (Field_Check_Router'Access,
271                              Char_Check_Router'Access);
272          if T = Null_Field_Type then
273             raise Form_Exception;
274          else
275             Res := Set_Fieldtype_Arg (T,
276                                       Make_Arg'Access,
277                                       Copy_Arg'Access,
278                                       Free_Arg'Access);
279             if Res /= E_Ok then
280                Eti_Exception (Res);
281             end if;
282
283             Res := Set_Fieldtype_Choice (T,
284                                          Next_Router'Access,
285                                          Prev_Router'Access);
286             if Res /= E_Ok then
287                Eti_Exception (Res);
288             end if;
289          end if;
290          M_Choice_Router := T;
291       end if;
292       pragma Assert (M_Choice_Router /= Null_Field_Type);
293       return M_Choice_Router;
294    end C_Choice_Router;
295
296 end Terminal_Interface.Curses.Forms.Field_Types;