]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/src/terminal_interface-curses-forms-field_types.adb
ncurses 6.2 - patch 20201003
[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 2020 Thomas E. Dickey                                          --
11 -- Copyright 1999-2011,2014 Free Software Foundation, Inc.                  --
12 --                                                                          --
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:                 --
20 --                                                                          --
21 -- The above copyright notice and this permission notice shall be included  --
22 -- in all copies or substantial portions of the Software.                   --
23 --                                                                          --
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.                               --
31 --                                                                          --
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       --
35 -- authorization.                                                           --
36 ------------------------------------------------------------------------------
37 --  Author:  Juergen Pfeifer, 1996
38 --  Version Control:
39 --  $Revision: 1.29 $
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;
46
47 --  |
48 --  |=====================================================================
49 --  | man page form_fieldtype.3x
50 --  |=====================================================================
51 --  |
52 package body Terminal_Interface.Curses.Forms.Field_Types is
53
54    use type System.Address;
55
56    package Argument_Conversions is
57       new System.Address_To_Access_Conversions (Argument);
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
83          then
84             Arg := Argument_Access
85          (Argument_Conversions.To_Pointer (Get_Arg (Fld)));
86             if Arg = null then
87                raise Form_Exception;
88             else
89                return Arg.all.Typ;
90             end if;
91          else
92             raise Form_Exception;
93          end if;
94       end if;
95    end Get_Type;
96
97    function Copy_Arg (Usr : System.Address) return System.Address
98    is
99    begin
100       return Usr;
101    end Copy_Arg;
102
103    procedure Free_Arg (Usr : System.Address)
104    is
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);
109
110       To_Be_Free : Argument_Access
111    := Argument_Access (Argument_Conversions.To_Pointer (Usr));
112       Low_Level  : C_Field_Type;
113    begin
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);
119             end if;
120          end if;
121          if To_Be_Free.all.Typ /= null then
122             Free_Type (To_Be_Free.all.Typ);
123          end if;
124          Freeargs (To_Be_Free);
125       end if;
126    end Free_Arg;
127
128    procedure Wrap_Builtin (Fld : Field;
129                            Typ : Field_Type'Class;
130                            Cft : C_Field_Type := C_Builtin_Router)
131    is
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");
139
140    begin
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;
144       else
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);
151             else
152                Arg.all.Usr := Usr_Arg;
153             end if;
154          end if;
155
156          Eti_Exception (Set_Fld_Type (Arg1 => Arg));
157       end if;
158    end Wrap_Builtin;
159
160    function Field_Check_Router (Fld : Field;
161                                 Usr : System.Address) return Curses_Bool
162    is
163       Arg  : constant Argument_Access
164    := Argument_Access (Argument_Conversions.To_Pointer (Usr));
165    begin
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);
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
179    := Argument_Access (Argument_Conversions.To_Pointer (Usr));
180    begin
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);
185       else
186          return 1;
187       end if;
188    end Char_Check_Router;
189
190    function Next_Router (Fld : Field;
191                          Usr : System.Address) return Curses_Bool
192    is
193       Arg  : constant Argument_Access
194    := Argument_Access (Argument_Conversions.To_Pointer (Usr));
195    begin
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);
200       else
201          return 1;
202       end if;
203    end Next_Router;
204
205    function Prev_Router (Fld : Field;
206                          Usr : System.Address) return Curses_Bool
207    is
208       Arg  : constant Argument_Access :=
209                Argument_Access (Argument_Conversions.To_Pointer (Usr));
210    begin
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);
215       else
216          return 1;
217       end if;
218    end Prev_Router;
219
220    --  -----------------------------------------------------------------------
221    --
222    function C_Builtin_Router return C_Field_Type
223    is
224       T   : C_Field_Type;
225    begin
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;
231          else
232             Eti_Exception (Set_Fieldtype_Arg (T,
233                                               Make_Arg'Access,
234                                               Copy_Arg'Access,
235                                               Free_Arg'Access));
236          end if;
237          M_Builtin_Router := T;
238       end if;
239       pragma Assert (M_Builtin_Router /= Null_Field_Type);
240       return M_Builtin_Router;
241    end C_Builtin_Router;
242
243    --  -----------------------------------------------------------------------
244    --
245    function C_Choice_Router return C_Field_Type
246    is
247       T   : C_Field_Type;
248    begin
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;
254          else
255             Eti_Exception (Set_Fieldtype_Arg (T,
256                                               Make_Arg'Access,
257                                               Copy_Arg'Access,
258                                               Free_Arg'Access));
259
260             Eti_Exception (Set_Fieldtype_Choice (T,
261                                                  Next_Router'Access,
262                                                  Prev_Router'Access));
263          end if;
264          M_Choice_Router := T;
265       end if;
266       pragma Assert (M_Choice_Router /= Null_Field_Type);
267       return M_Choice_Router;
268    end C_Choice_Router;
269
270 end Terminal_Interface.Curses.Forms.Field_Types;