]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/fty_generic.c
ncurses 5.7 - patch 20090905
[ncurses.git] / form / fty_generic.c
1 /****************************************************************************
2  * Copyright (c) 2008 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /***************************************************************************
30 *                                                                          *
31 *  Author : Juergen Pfeifer                                                *
32 *                                                                          *
33 ***************************************************************************/
34
35 #include "form.priv.h"
36
37 MODULE_ID("$Id: fty_generic.c,v 1.1 2008/11/18 08:50:04 juergen Exp $")
38
39 /*
40  * This is not a full implementation of a field type, but adds some
41  * support for higher level languages with some restrictions to interop
42  * with C language. Especially the collection of arguments for the
43  * various fieldtypes is not based on the vararg C mechanism, but on a
44  * iterator based callback mechanism that allowes the high level language
45  * to provide the arguments as a structure. Most languages have mechanisms
46  * to layout structures so that they can be passed to C.
47  * The languages can register a new generic fieldtype dynamically and store
48  * a handle (key) to the calling object as an argument. Together with that
49  * it can register a freearg callback, so that the high level language
50  * remains in control of the memory management of the arguments they pass.
51  * The design idea is, that the high-level language - typically a OO
52  * language like C# or Java, uses it's own dispatching mechanisms
53  * (polymorphism) to call the proper check routines responsible for the
54  * argument type. So these language implement typically only one generic
55  * fieldtype they register with the forms library using this call.
56  *
57  * For that purpose we have extended the fieldtype struc by a new element
58  * that gets the arguments from a single struct passed by the caller. 
59  * 
60  */
61
62
63 /*---------------------------------------------------------------------------
64 |   Facility      :  libnform  
65 |   Function      :  static void *Generic_This_Type( void * arg )
66 |   
67 |   Description   :  We interpret the passed arg just as a handle the
68 |                    calling language uses to keep track of its allocated
69 |                    argument structures. We can simply copy it back.
70 |
71 |   Return Values :  Pointer to argument structure
72 +--------------------------------------------------------------------------*/
73 static void *
74 Generic_This_Type(void *arg)
75 {
76   return (arg);
77 }
78
79 /*---------------------------------------------------------------------------
80 |   Facility      :  libnform
81 |   Function      :  FIELDTYPE *_nc_generic_fieldtype(
82 |                       bool (* const field_check)(FIELD *,const void *),
83 |                       bool (* const char_check) (int, const void *),
84 |                       bool (*const next)(FORM*,FIELD*,const void*),
85 |                       bool (*const prev)(FORM*,FIELD*,const void*),
86 |                       void (*freecallback)(void*))
87 |
88 |   Description   :  Create a new fieldtype. The application programmer must
89 |                    write a field_check and a char_check function and give
90 |                    them as input to this call. A callback to allow the
91 |                    release of the allocated memory must also be provided.
92 |                    For generic field types, we provide some more 
93 |                    information about the field as parameters.
94 |
95 |                    If an error occurs, errno is set to
96 |                       E_BAD_ARGUMENT  - invalid arguments
97 |                       E_SYSTEM_ERROR  - system error (no memory)
98 |
99 |   Return Values :  Fieldtype pointer or NULL if error occurred
100 +--------------------------------------------------------------------------*/
101 NCURSES_EXPORT(FIELDTYPE *)
102 _nc_generic_fieldtype(bool (*const field_check) (FORM*, FIELD *, const void *),
103                       bool (*const char_check)  (int, FORM*, FIELD*, const void *),
104                       bool (*const next)(FORM*, FIELD*,const void*),
105                       bool (*const prev)(FORM*, FIELD*,const void*),
106                       void (*freecallback)(void*))
107 {
108     int code = E_SYSTEM_ERROR;
109     FIELDTYPE* res = (FIELDTYPE*)0;
110
111     T((T_CALLED("_nc_generic_fieldtype(%p,%p,%p,%p,%p)"), 
112        field_check, char_check, next, prev, freecallback));
113
114     if (field_check || char_check)
115     {
116       res = typeMalloc(FIELDTYPE,1);
117       if (res)
118       {
119         *res = *_nc_Default_FieldType; 
120         res->status |= (_HAS_ARGS | _GENERIC);
121         res->fieldcheck.gfcheck = field_check;
122         res->charcheck.gccheck  = char_check;
123         res->genericarg = Generic_This_Type;
124         res->freearg = freecallback;
125         res->enum_next.gnext = next;
126         res->enum_prev.gprev = prev;
127         code = E_OK;
128       }
129     }
130     else
131       code = E_BAD_ARGUMENT;
132
133     if (E_OK != code)
134       SET_ERROR(code);
135
136     returnFieldType(res);
137 }
138
139
140 /*---------------------------------------------------------------------------
141 |   Facility      :  libnform  
142 |   Function      :  static TypeArgument *GenericArgument(
143 |                      const FIELDTYPE* typ,
144 |                      int (*argiterator)(void**),
145 |                      int* err)
146 |   
147 |   Description   :  The iterator callback must browse through all fieldtype
148 |                    parameters that have an argument associated with the
149 |                    type. The iterator returns 1 if the operation to get
150 |                    the next element was successfull, 0 otherwise. If the
151 |                    iterator could move to the next argument, it fills
152 |                    the void* pointer representing the argument into the
153 |                    location provided as argument to the iterator.
154 |                    The err reference is used to keep track of errors.
155 |
156 |   Return Values :  Pointer to argument structure
157 +--------------------------------------------------------------------------*/
158 static TypeArgument*
159 GenericArgument(const FIELDTYPE *typ, 
160                 int (*argiterator)(void**), int *err)
161 {
162   TypeArgument *res = (TypeArgument *)0;
163
164   if (typ!=0 && (typ->status & _HAS_ARGS) != 0 && err!=0 && argiterator != 0)
165   {
166     if (typ->status & _LINKED_TYPE)
167     {
168       /* Composite fieldtypes keep track internally of their own memory */
169       TypeArgument* p = typeMalloc(TypeArgument,1);
170       if (p)
171       {
172         p->left  = GenericArgument(typ->left,  argiterator, err);
173         p->right = GenericArgument(typ->right, argiterator, err);
174         return p;
175       }
176       else
177         *err += 1;
178     }
179     else
180     {
181       assert(typ->genericarg != (void*)0);
182       if (typ->genericarg == 0)
183         *err += 1;
184       else
185         {
186           void* argp;
187           int valid = argiterator(&argp);
188           if (valid==0 || argp==0 || 
189               !(res = (TypeArgument*)typ->genericarg(argp)))
190             {
191               *err += 1;
192             }
193         }
194     }
195   }
196   return res;
197 }
198
199 /*---------------------------------------------------------------------------
200 |   Facility      :  libnform  
201 |   Function      :  int _nc_set_generic_fieldtype(
202 |                      FIELD* field,
203 |                      FIELDTYPE* ftyp,
204 |                      int (*argiterator)(void**))
205 |   
206 |   Description   :  Assign the fieldtype to the field and use the iterator
207 |                    mechanism to get the arguments when a check is 
208 |                    performed.
209 |
210 |   Return Values :  E_OK if all went well
211 |                    E_SYSTEM_ERROR if an error occurred
212 +--------------------------------------------------------------------------*/
213 NCURSES_EXPORT(int)
214 _nc_set_generic_fieldtype(FIELD *field,
215                           FIELDTYPE* ftyp,
216                           int (*argiterator)(void**))
217 {
218   int code = E_SYSTEM_ERROR;
219   int err = 0;
220
221   if (field)
222   {
223       if (field && field->type)
224         _nc_Free_Type(field);
225
226       field->type = ftyp;
227       if (ftyp)
228       {
229         if (argiterator)
230         {
231           /* The precondition is that the iterator is reset */
232           field->arg  = (void*)GenericArgument(field->type,argiterator,&err);
233           
234           if (err)
235             {
236               _nc_Free_Argument(field->type, (TypeArgument*)(field->arg));
237               field->type = (FIELDTYPE*)0;
238               field->arg  = (void*)0;
239             }
240           else
241             {
242               code = E_OK;
243               if (field->type)
244                 field->type->ref++;
245             }
246         }
247       }
248       else
249       {
250         field->arg = (void*)0;
251         code = E_OK;
252       }
253     }
254     return code;
255 }
256
257 /*---------------------------------------------------------------------------
258 |   Facility      :  libnform  
259 |   Function      :  WINDOW* _nc_form_cursor(
260 |                      FORM* form,
261 |                      int *pRow, int *pCol)
262 |   
263 |   Description   :  Get the current position of the form cursor position
264 |                    We also return the field window
265 |
266 |   Return Values :  The fields Window or NULL on error
267 +--------------------------------------------------------------------------*/
268 NCURSES_EXPORT(WINDOW*) 
269 _nc_form_cursor(const FORM* form, int* pRow, int* pCol)
270 {
271   int code = E_SYSTEM_ERROR;
272   WINDOW* res = (WINDOW*)0;
273
274   if (!(form==0 || pRow==0 || pCol==0))
275   {
276       *pRow = form->currow;
277       *pCol = form->curcol;
278       res   = form->w;
279       code  = E_OK;
280   }
281   if (code != E_OK)
282     SET_ERROR(code);
283   return res;
284 }
285
286
287 /* fty_generic.c ends here */