summaryrefslogtreecommitdiff
path: root/Ada95/src/terminal_interface-curses-forms-field_types.adb
blob: bda6e51b088f73a80485207208ff61f09698f2c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
------------------------------------------------------------------------------
--                                                                          --
--                           GNAT ncurses Binding                           --
--                                                                          --
--                 Terminal_Interface.Curses.Forms.Field_Types              --
--                                                                          --
--                                 B O D Y                                  --
--                                                                          --
------------------------------------------------------------------------------
-- Copyright (c) 1998-2011,2014 Free Software Foundation, Inc.              --
--                                                                          --
-- Permission is hereby granted, free of charge, to any person obtaining a  --
-- copy of this software and associated documentation files (the            --
-- "Software"), to deal in the Software without restriction, including      --
-- without limitation the rights to use, copy, modify, merge, publish,      --
-- distribute, distribute with modifications, sublicense, and/or sell       --
-- copies of the Software, and to permit persons to whom the Software is    --
-- furnished to do so, subject to the following conditions:                 --
--                                                                          --
-- The above copyright notice and this permission notice shall be included  --
-- in all copies or substantial portions of the Software.                   --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
-- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
-- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
--                                                                          --
-- Except as contained in this notice, the name(s) of the above copyright   --
-- holders shall not be used in advertising or otherwise to promote the     --
-- sale, use or other dealings in this Software without prior written       --
-- authorization.                                                           --
------------------------------------------------------------------------------
--  Author:  Juergen Pfeifer, 1996
--  Version Control:
--  $Revision: 1.28 $
--  $Date: 2014/09/13 19:00:47 $
--  Binding Version 01.00
------------------------------------------------------------------------------
with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
with Ada.Unchecked_Deallocation;
with System.Address_To_Access_Conversions;

--  |
--  |=====================================================================
--  | man page form_fieldtype.3x
--  |=====================================================================
--  |
package body Terminal_Interface.Curses.Forms.Field_Types is

   use type System.Address;

   package Argument_Conversions is
      new System.Address_To_Access_Conversions (Argument);

   function Get_Fieldtype (F : Field) return C_Field_Type;
   pragma Import (C, Get_Fieldtype, "field_type");

   function Get_Arg (F : Field) return System.Address;
   pragma Import (C, Get_Arg, "field_arg");
   --  |
   --  |=====================================================================
   --  | man page form_field_validation.3x
   --  |=====================================================================
   --  |
   --  |
   --  |
   function Get_Type (Fld : Field) return Field_Type_Access
   is
      Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
      Arg : Argument_Access;
   begin
      if Low_Level = Null_Field_Type then
         return null;
      else
         if Low_Level = M_Builtin_Router or else
            Low_Level = M_Generic_Type or else
            Low_Level = M_Choice_Router or else
            Low_Level = M_Generic_Choice
         then
            Arg := Argument_Access
         (Argument_Conversions.To_Pointer (Get_Arg (Fld)));
            if Arg = null then
               raise Form_Exception;
            else
               return Arg.all.Typ;
            end if;
         else
            raise Form_Exception;
         end if;
      end if;
   end Get_Type;

   function Copy_Arg (Usr : System.Address) return System.Address
   is
   begin
      return Usr;
   end Copy_Arg;

   procedure Free_Arg (Usr : System.Address)
   is
      procedure Free_Type is new Ada.Unchecked_Deallocation
        (Field_Type'Class, Field_Type_Access);
      procedure Freeargs is new Ada.Unchecked_Deallocation
        (Argument, Argument_Access);

      To_Be_Free : Argument_Access
   := Argument_Access (Argument_Conversions.To_Pointer (Usr));
      Low_Level  : C_Field_Type;
   begin
      if To_Be_Free /= null then
         if To_Be_Free.all.Usr /= System.Null_Address then
            Low_Level := To_Be_Free.all.Cft;
            if Low_Level.all.Freearg /= null then
               Low_Level.all.Freearg (To_Be_Free.all.Usr);
            end if;
         end if;
         if To_Be_Free.all.Typ /= null then
            Free_Type (To_Be_Free.all.Typ);
         end if;
         Freeargs (To_Be_Free);
      end if;
   end Free_Arg;

   procedure Wrap_Builtin (Fld : Field;
                           Typ : Field_Type'Class;
                           Cft : C_Field_Type := C_Builtin_Router)
   is
      Usr_Arg   : constant System.Address := Get_Arg (Fld);
      Low_Level : constant C_Field_Type := Get_Fieldtype (Fld);
      Arg : Argument_Access;
      function Set_Fld_Type (F    : Field := Fld;
                             Cf   : C_Field_Type := Cft;
                             Arg1 : Argument_Access) return Eti_Error;
      pragma Import (C, Set_Fld_Type, "set_field_type_user");

   begin
      pragma Assert (Low_Level /= Null_Field_Type);
      if Cft /= C_Builtin_Router and then Cft /= C_Choice_Router then
         raise Form_Exception;
      else
         Arg := new Argument'(Usr => System.Null_Address,
                              Typ => new Field_Type'Class'(Typ),
                              Cft => Get_Fieldtype (Fld));
         if Usr_Arg /= System.Null_Address then
            if Low_Level.all.Copyarg /= null then
               Arg.all.Usr := Low_Level.all.Copyarg (Usr_Arg);
            else
               Arg.all.Usr := Usr_Arg;
            end if;
         end if;

         Eti_Exception (Set_Fld_Type (Arg1 => Arg));
      end if;
   end Wrap_Builtin;

   function Field_Check_Router (Fld : Field;
                                Usr : System.Address) return Curses_Bool
   is
      Arg  : constant Argument_Access
   := Argument_Access (Argument_Conversions.To_Pointer (Usr));
   begin
      pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
                     and then Arg.all.Typ /= null);
      if Arg.all.Cft.all.Fcheck /= null then
         return Arg.all.Cft.all.Fcheck (Fld, Arg.all.Usr);
      else
         return 1;
      end if;
   end Field_Check_Router;

   function Char_Check_Router (Ch  : C_Int;
                               Usr : System.Address) return Curses_Bool
   is
      Arg  : constant Argument_Access
   := Argument_Access (Argument_Conversions.To_Pointer (Usr));
   begin
      pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
                     and then Arg.all.Typ /= null);
      if Arg.all.Cft.all.Ccheck /= null then
         return Arg.all.Cft.all.Ccheck (Ch, Arg.all.Usr);
      else
         return 1;
      end if;
   end Char_Check_Router;

   function Next_Router (Fld : Field;
                         Usr : System.Address) return Curses_Bool
   is
      Arg  : constant Argument_Access
   := Argument_Access (Argument_Conversions.To_Pointer (Usr));
   begin
      pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
                     and then Arg.all.Typ /= null);
      if Arg.all.Cft.all.Next /= null then
         return Arg.all.Cft.all.Next (Fld, Arg.all.Usr);
      else
         return 1;
      end if;
   end Next_Router;

   function Prev_Router (Fld : Field;
                         Usr : System.Address) return Curses_Bool
   is
      Arg  : constant Argument_Access :=
               Argument_Access (Argument_Conversions.To_Pointer (Usr));
   begin
      pragma Assert (Arg /= null and then Arg.all.Cft /= Null_Field_Type
                     and then Arg.all.Typ /= null);
      if Arg.all.Cft.all.Prev /= null then
         return Arg.all.Cft.all.Prev (Fld, Arg.all.Usr);
      else
         return 1;
      end if;
   end Prev_Router;

   --  -----------------------------------------------------------------------
   --
   function C_Builtin_Router return C_Field_Type
   is
      T   : C_Field_Type;
   begin
      if M_Builtin_Router = Null_Field_Type then
         T := New_Fieldtype (Field_Check_Router'Access,
                             Char_Check_Router'Access);
         if T = Null_Field_Type then
            raise Form_Exception;
         else
            Eti_Exception (Set_Fieldtype_Arg (T,
                                              Make_Arg'Access,
                                              Copy_Arg'Access,
                                              Free_Arg'Access));
         end if;
         M_Builtin_Router := T;
      end if;
      pragma Assert (M_Builtin_Router /= Null_Field_Type);
      return M_Builtin_Router;
   end C_Builtin_Router;

   --  -----------------------------------------------------------------------
   --
   function C_Choice_Router return C_Field_Type
   is
      T   : C_Field_Type;
   begin
      if M_Choice_Router = Null_Field_Type then
         T := New_Fieldtype (Field_Check_Router'Access,
                             Char_Check_Router'Access);
         if T = Null_Field_Type then
            raise Form_Exception;
         else
            Eti_Exception (Set_Fieldtype_Arg (T,
                                              Make_Arg'Access,
                                              Copy_Arg'Access,
                                              Free_Arg'Access));

            Eti_Exception (Set_Fieldtype_Choice (T,
                                                 Next_Router'Access,
                                                 Prev_Router'Access));
         end if;
         M_Choice_Router := T;
      end if;
      pragma Assert (M_Choice_Router /= Null_Field_Type);
      return M_Choice_Router;
   end C_Choice_Router;

end Terminal_Interface.Curses.Forms.Field_Types;