]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/src/terminal_interface-curses-forms-field_types.adb
cff398a97dc668e731142ff40f58f696b2eabba9
[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-2009,2011 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.22 $
39 --  $Date: 2011/03/08 01:16:49 $
40 --  Binding Version 01.00
41 ------------------------------------------------------------------------------
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 System.Address;
53
54    pragma Warnings (Off);
55    function To_Argument_Access is new Ada.Unchecked_Conversion
56      (System.Address, Argument_Access);
57    pragma Warnings (On);
58
59    function Get_Fieldtype (F : Field) return C_Field_Type;
60    pragma Import (C, Get_Fieldtype, "field_type");
61
62    function Get_Arg (F : Field) return System.Address;
63    pragma Import (C, Get_Arg, "field_arg");
64    --  |
65    --  |=====================================================================
66    --  | man page form_field_validation.3x
67    --  |=====================================================================
68    --  |
69    --  |
70    --  |
71    function Get_Type (Fld : Field) return Field_Type_Access
72    is
73       Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
74       Arg : Argument_Access;
75    begin
76       if Low_Level = Null_Field_Type then
77          return null;
78       else
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 then
83             Arg := To_Argument_Access (Get_Arg (Fld));
84             if Arg = null then
85                raise Form_Exception;
86             else
87                return Arg.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 := To_Argument_Access (Usr);
109       Low_Level  : C_Field_Type;
110    begin
111       if To_Be_Free /= null then
112          if To_Be_Free.Usr /= System.Null_Address then
113             Low_Level := To_Be_Free.Cft;
114             if Low_Level.Freearg /= null then
115                Low_Level.Freearg (To_Be_Free.Usr);
116             end if;
117          end if;
118          if To_Be_Free.Typ /= null then
119             Free_Type (To_Be_Free.Typ);
120          end if;
121          Freeargs (To_Be_Free);
122       end if;
123    end Free_Arg;
124
125    procedure Wrap_Builtin (Fld : Field;
126                            Typ : Field_Type'Class;
127                            Cft : C_Field_Type := C_Builtin_Router)
128    is
129       Usr_Arg   : constant System.Address := Get_Arg (Fld);
130       Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
131       Arg : Argument_Access;
132       Res : Eti_Error;
133       function Set_Fld_Type (F    : Field := Fld;
134                              Cf   : C_Field_Type := Cft;
135                              Arg1 : Argument_Access) return C_Int;
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.Copyarg /= null then
148                Arg.Usr := Low_Level.Copyarg (Usr_Arg);
149             else
150                Arg.Usr := Usr_Arg;
151             end if;
152          end if;
153
154          Res := Set_Fld_Type (Arg1 => Arg);
155          if Res /= E_Ok then
156             Eti_Exception (Res);
157          end if;
158       end if;
159    end Wrap_Builtin;
160
161    function Field_Check_Router (Fld : Field;
162                                 Usr : System.Address) return Curses_Bool
163    is
164       Arg  : constant Argument_Access := To_Argument_Access (Usr);
165    begin
166       pragma Assert (Arg /= null and then Arg.Cft /= Null_Field_Type
167                      and then Arg.Typ /= null);
168       if Arg.Cft.Fcheck /= null then
169          return Arg.Cft.Fcheck (Fld, Arg.Usr);
170       else
171          return 1;
172       end if;
173    end Field_Check_Router;
174
175    function Char_Check_Router (Ch  : C_Int;
176                                Usr : System.Address) return Curses_Bool
177    is
178       Arg  : constant Argument_Access := To_Argument_Access (Usr);
179    begin
180       pragma Assert (Arg /= null and then Arg.Cft /= Null_Field_Type
181                      and then Arg.Typ /= null);
182       if Arg.Cft.Ccheck /= null then
183          return Arg.Cft.Ccheck (Ch, Arg.Usr);
184       else
185          return 1;
186       end if;
187    end Char_Check_Router;
188
189    function Next_Router (Fld : Field;
190                          Usr : System.Address) return Curses_Bool
191    is
192       Arg  : constant Argument_Access := To_Argument_Access (Usr);
193    begin
194       pragma Assert (Arg /= null and then Arg.Cft /= Null_Field_Type
195                      and then Arg.Typ /= null);
196       if Arg.Cft.Next /= null then
197          return Arg.Cft.Next (Fld, Arg.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 := To_Argument_Access (Usr);
207    begin
208       pragma Assert (Arg /= null and then Arg.Cft /= Null_Field_Type
209                      and then Arg.Typ /= null);
210       if Arg.Cft.Prev /= null then
211          return Arg.Cft.Prev (Fld, Arg.Usr);
212       else
213          return 1;
214       end if;
215    end Prev_Router;
216
217    --  -----------------------------------------------------------------------
218    --
219    function C_Builtin_Router return C_Field_Type
220    is
221       Res : Eti_Error;
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             Res := Set_Fieldtype_Arg (T,
231                                       Make_Arg'Access,
232                                       Copy_Arg'Access,
233                                       Free_Arg'Access);
234             if Res /= E_Ok then
235                Eti_Exception (Res);
236             end if;
237          end if;
238          M_Builtin_Router := T;
239       end if;
240       pragma Assert (M_Builtin_Router /= Null_Field_Type);
241       return M_Builtin_Router;
242    end C_Builtin_Router;
243
244    --  -----------------------------------------------------------------------
245    --
246    function C_Choice_Router return C_Field_Type
247    is
248       Res : Eti_Error;
249       T   : C_Field_Type;
250    begin
251       if M_Choice_Router = Null_Field_Type then
252          T := New_Fieldtype (Field_Check_Router'Access,
253                              Char_Check_Router'Access);
254          if T = Null_Field_Type then
255             raise Form_Exception;
256          else
257             Res := Set_Fieldtype_Arg (T,
258                                       Make_Arg'Access,
259                                       Copy_Arg'Access,
260                                       Free_Arg'Access);
261             if Res /= E_Ok then
262                Eti_Exception (Res);
263             end if;
264
265             Res := Set_Fieldtype_Choice (T,
266                                          Next_Router'Access,
267                                          Prev_Router'Access);
268             if Res /= E_Ok then
269                Eti_Exception (Res);
270             end if;
271          end if;
272          M_Choice_Router := T;
273       end if;
274       pragma Assert (M_Choice_Router /= Null_Field_Type);
275       return M_Choice_Router;
276    end C_Choice_Router;
277
278 end Terminal_Interface.Curses.Forms.Field_Types;