1 /****************************************************************************
2 * Copyright (c) 1998,2000 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer, 1995,1997 *
31 * Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en *
32 ****************************************************************************/
34 #include "form.priv.h"
36 MODULE_ID("$Id: fld_def.c,v 1.14 2002/07/06 15:33:27 juergen Exp $")
38 /* this can't be readonly */
39 static FIELD default_field = {
50 NO_JUSTIFICATION, /* just */
56 ALL_FIELD_OPTS, /* opts */
57 (FIELD *)0, /* snext */
58 (FIELD *)0, /* sprev */
59 (FIELD *)0, /* link */
61 (FIELDTYPE *)0, /* type */
64 (char *)0 /* usrptr */
67 NCURSES_EXPORT_VAR(FIELD *) _nc_Default_Field = &default_field;
69 /*---------------------------------------------------------------------------
71 | Function : TypeArgument *_nc_Make_Argument(
72 | const FIELDTYPE *typ,
76 | Description : Create an argument structure for the specified type.
77 | Use the type-dependant argument list to construct
80 | Return Values : Pointer to argument structure. Maybe NULL.
81 | In case of an error in *err an errorcounter is increased.
82 +--------------------------------------------------------------------------*/
83 NCURSES_EXPORT(TypeArgument*)
85 (const FIELDTYPE *typ, va_list *ap, int *err)
87 TypeArgument *res = (TypeArgument *)0;
90 if (typ && (typ->status & _HAS_ARGS))
93 if (typ->status & _LINKED_TYPE)
95 p = (TypeArgument *)malloc(sizeof(TypeArgument));
98 p->left = _nc_Make_Argument(typ->left ,ap,err);
99 p->right = _nc_Make_Argument(typ->right,ap,err);
106 assert(typ->makearg);
107 if ( !(res=(TypeArgument *)typ->makearg(ap)) )
114 /*---------------------------------------------------------------------------
115 | Facility : libnform
116 | Function : TypeArgument *_nc_Copy_Argument(const FIELDTYPE *typ,
117 | const TypeArgument *argp,
120 | Description : Create a copy of an argument structure for the specified
123 | Return Values : Pointer to argument structure. Maybe NULL.
124 | In case of an error in *err an errorcounter is increased.
125 +--------------------------------------------------------------------------*/
126 NCURSES_EXPORT(TypeArgument*)
128 (const FIELDTYPE *typ,
129 const TypeArgument *argp, int *err)
131 TypeArgument *res = (TypeArgument *)0;
134 if ( typ && (typ->status & _HAS_ARGS) )
137 if (typ->status & _LINKED_TYPE)
139 p = (TypeArgument *)malloc(sizeof(TypeArgument));
142 p->left = _nc_Copy_Argument(typ,argp->left ,err);
143 p->right = _nc_Copy_Argument(typ,argp->right,err);
152 if (!(res = (TypeArgument *)(typ->copyarg((const void *)argp))))
156 res = (TypeArgument *)argp;
162 /*---------------------------------------------------------------------------
163 | Facility : libnform
164 | Function : void _nc_Free_Argument(const FIELDTYPE *typ,
165 | TypeArgument * argp )
167 | Description : Release memory associated with the argument structure
168 | for the given fieldtype.
171 +--------------------------------------------------------------------------*/
174 (const FIELDTYPE * typ, TypeArgument * argp)
176 if (!typ || !(typ->status & _HAS_ARGS))
179 if (typ->status & _LINKED_TYPE)
182 _nc_Free_Argument(typ->left ,argp->left );
183 _nc_Free_Argument(typ->right,argp->right);
189 typ->freearg((void *)argp);
193 /*---------------------------------------------------------------------------
194 | Facility : libnform
195 | Function : bool _nc_Copy_Type( FIELD *dst, FIELD const *src )
197 | Description : Copy argument structure of field src to field dst
199 | Return Values : TRUE - copy worked
200 | FALSE - error occured
201 +--------------------------------------------------------------------------*/
204 (FIELD *dst, FIELD const *src)
210 dst->type = src->type;
211 dst->arg = (void *)_nc_Copy_Argument(src->type,(TypeArgument *)(src->arg),&err);
215 _nc_Free_Argument(dst->type,(TypeArgument *)(dst->arg));
216 dst->type = (FIELDTYPE *)0;
217 dst->arg = (void *)0;
228 /*---------------------------------------------------------------------------
229 | Facility : libnform
230 | Function : void _nc_Free_Type( FIELD *field )
232 | Description : Release Argument structure for this field
235 +--------------------------------------------------------------------------*/
237 _nc_Free_Type (FIELD *field)
242 _nc_Free_Argument(field->type,(TypeArgument *)(field->arg));
245 /*---------------------------------------------------------------------------
246 | Facility : libnform
247 | Function : FIELD *new_field( int rows, int cols,
248 | int frow, int fcol,
249 | int nrow, int nbuf )
251 | Description : Create a new field with this many 'rows' and 'cols',
252 | starting at 'frow/fcol' in the subwindow of the form.
253 | Allocate 'nrow' off-screen rows and 'nbuf' additional
254 | buffers. If an error occurs, errno is set to
256 | E_BAD_ARGUMENT - invalid argument
257 | E_SYSTEM_ERROR - system error
259 | Return Values : Pointer to the new field or NULL if failure.
260 +--------------------------------------------------------------------------*/
261 NCURSES_EXPORT(FIELD *)
263 (int rows, int cols, int frow, int fcol, int nrow, int nbuf)
265 FIELD *New_Field = (FIELD *)0;
266 int err = E_BAD_ARGUMENT;
274 ((err = E_SYSTEM_ERROR) != 0) && /* trick: this resets the default error */
275 (New_Field=(FIELD *)malloc(sizeof(FIELD))) )
277 *New_Field = default_field;
278 New_Field->rows = rows;
279 New_Field->cols = cols;
280 New_Field->drows = rows + nrow;
281 New_Field->dcols = cols;
282 New_Field->frow = frow;
283 New_Field->fcol = fcol;
284 New_Field->nrow = nrow;
285 New_Field->nbuf = nbuf;
286 New_Field->link = New_Field;
288 if (_nc_Copy_Type(New_Field,&default_field))
292 len = Total_Buffer_Size(New_Field);
293 if ((New_Field->buf = (char *)malloc(len)))
295 /* Prefill buffers with blanks and insert terminating zeroes
299 memset(New_Field->buf,' ',len);
300 for(i=0;i<=New_Field->nbuf;i++)
302 New_Field->buf[(New_Field->drows*New_Field->cols+1)*(i+1)-1]
311 free_field(New_Field);
317 /*---------------------------------------------------------------------------
318 | Facility : libnform
319 | Function : int free_field( FIELD *field )
321 | Description : Frees the storage allocated for the field.
323 | Return Values : E_OK - success
324 | E_BAD_ARGUMENT - invalid field pointer
325 | E_CONNECTED - field is connected
326 +--------------------------------------------------------------------------*/
328 free_field (FIELD * field)
331 RETURN(E_BAD_ARGUMENT);
336 if (field == field->link)
345 for(f=field;f->link != field;f = f->link)
347 f->link = field->link;
349 _nc_Free_Type(field);
354 /* fld_def.c ends here */