1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 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 ****************************************************************************/
33 #include "form.priv.h"
35 MODULE_ID("$Id: frm_def.c,v 1.20 2006/11/04 16:57:15 tom Exp $")
37 /* this can't be readonly */
38 static FORM default_form =
50 ALL_FORM_OPTS, /* opts */
51 (WINDOW *)0, /* win */
52 (WINDOW *)0, /* sub */
54 (FIELD **)0, /* field */
55 (FIELD *)0, /* current */
56 (_PAGE *) 0, /* page */
57 (char *)0, /* usrptr */
64 NCURSES_EXPORT_VAR(FORM *) _nc_Default_Form = &default_form;
66 /*---------------------------------------------------------------------------
68 | Function : static FIELD *Insert_Field_By_Position(
72 | Description : Insert new_field into sorted fieldlist with head "head"
73 | and return new head of sorted fieldlist. Sorting
74 | criteria is (row,column). This is a circular list.
76 | Return Values : New head of sorted fieldlist
77 +--------------------------------------------------------------------------*/
79 Insert_Field_By_Position(FIELD *newfield, FIELD *head)
81 FIELD *current, *newhead;
86 { /* empty list is trivial */
87 newhead = newfield->snext = newfield->sprev = newfield;
91 newhead = current = head;
92 while ((current->frow < newfield->frow) ||
93 ((current->frow == newfield->frow) &&
94 (current->fcol < newfield->fcol)))
96 current = current->snext;
98 { /* We cycled through. Reset head to indicate that */
103 /* we leave the loop with current pointing to the field after newfield */
104 newfield->snext = current;
105 newfield->sprev = current->sprev;
106 newfield->snext->sprev = newfield;
107 newfield->sprev->snext = newfield;
114 /*---------------------------------------------------------------------------
115 | Facility : libnform
116 | Function : static void Disconnect_Fields(FORM *form)
118 | Description : Break association between form and array of fields.
121 +--------------------------------------------------------------------------*/
123 Disconnect_Fields(FORM *form)
129 for (fields = form->field; *fields; fields++)
131 if (form == (*fields)->form)
132 (*fields)->form = (FORM *)0;
135 form->rows = form->cols = 0;
136 form->maxfield = form->maxpage = -1;
137 form->field = (FIELD **)0;
140 form->page = (_PAGE *) 0;
144 /*---------------------------------------------------------------------------
145 | Facility : libnform
146 | Function : static int Connect_Fields(FORM *form, FIELD **fields)
148 | Description : Set association between form and array of fields.
150 | Return Values : E_OK - no error
151 | E_CONNECTED - a field is already connected
152 | E_BAD_ARGUMENT - Invalid form pointer or field array
153 | E_SYSTEM_ERROR - not enough memory
154 +--------------------------------------------------------------------------*/
156 Connect_Fields(FORM *form, FIELD **fields)
160 int maximum_row_in_field, maximum_col_in_field;
163 T((T_CALLED("Connect_Fields(%p,%p)"), form, fields));
167 form->field = fields;
175 /* store formpointer in fields and count pages */
176 for (field_cnt = 0; fields[field_cnt]; field_cnt++)
178 if (fields[field_cnt]->form)
180 if (field_cnt == 0 ||
181 (fields[field_cnt]->status & _NEWPAGE))
183 fields[field_cnt]->form = form;
185 if (field_cnt == 0 || (short)field_cnt < 0)
186 RETURN(E_BAD_ARGUMENT);
188 /* allocate page structures */
189 if ((pg = (_PAGE *) malloc(page_nr * sizeof(_PAGE))) != (_PAGE *) 0)
194 RETURN(E_SYSTEM_ERROR);
196 /* Cycle through fields and calculate page boundaries as well as
198 for (j = 0; j < field_cnt; j++)
204 if (fields[j]->status & _NEWPAGE)
212 maximum_row_in_field = fields[j]->frow + fields[j]->rows;
213 maximum_col_in_field = fields[j]->fcol + fields[j]->cols;
215 if (form->rows < maximum_row_in_field)
216 form->rows = maximum_row_in_field;
217 if (form->cols < maximum_col_in_field)
218 form->cols = maximum_col_in_field;
221 pg->pmax = field_cnt - 1;
222 form->maxfield = field_cnt;
223 form->maxpage = page_nr;
225 /* Sort fields on form pages */
226 for (page_nr = 0; page_nr < form->maxpage; page_nr++)
228 FIELD *fld = (FIELD *)0;
230 for (j = form->page[page_nr].pmin; j <= form->page[page_nr].pmax; j++)
232 fields[j]->index = j;
233 fields[j]->page = page_nr;
234 fld = Insert_Field_By_Position(fields[j], fld);
236 form->page[page_nr].smin = fld->index;
237 form->page[page_nr].smax = fld->sprev->index;
242 /*---------------------------------------------------------------------------
243 | Facility : libnform
244 | Function : static int Associate_Fields(FORM *form, FIELD **fields)
246 | Description : Set association between form and array of fields.
247 | If there are fields, position to first active field.
249 | Return Values : E_OK - success
250 | E_BAD_ARGUMENT - Invalid form pointer or field array
251 | E_CONNECTED - a field is already connected
252 | E_SYSTEM_ERROR - not enough memory
253 +--------------------------------------------------------------------------*/
254 NCURSES_INLINE static int
255 Associate_Fields(FORM *form, FIELD **fields)
257 int res = Connect_Fields(form, fields);
261 if (form->maxpage > 0)
264 form_driver(form, FIRST_ACTIVE_MAGIC);
269 form->current = (FIELD *)0;
275 /*---------------------------------------------------------------------------
276 | Facility : libnform
277 | Function : FORM *new_form( FIELD **fields )
279 | Description : Create new form with given array of fields.
281 | Return Values : Pointer to form. NULL if error occurred.
284 | E_BAD_ARGUMENT - Invalid form pointer or field array
285 | E_CONNECTED - a field is already connected
286 | E_SYSTEM_ERROR - not enough memory
287 +--------------------------------------------------------------------------*/
288 NCURSES_EXPORT(FORM *)
289 new_form(FIELD **fields)
291 int err = E_SYSTEM_ERROR;
293 FORM *form = (FORM *)malloc(sizeof(FORM));
295 T((T_CALLED("new_form(%p)"), fields));
298 *form = *_nc_Default_Form;
299 if ((err = Associate_Fields(form, fields)) != E_OK)
312 /*---------------------------------------------------------------------------
313 | Facility : libnform
314 | Function : int free_form( FORM *form )
316 | Description : Release internal memory associated with form.
318 | Return Values : E_OK - no error
319 | E_BAD_ARGUMENT - invalid form pointer
320 | E_POSTED - form is posted
321 +--------------------------------------------------------------------------*/
323 free_form(FORM *form)
325 T((T_CALLED("free_form(%p)"), form));
328 RETURN(E_BAD_ARGUMENT);
330 if (form->status & _POSTED)
333 Disconnect_Fields(form);
341 /*---------------------------------------------------------------------------
342 | Facility : libnform
343 | Function : int set_form_fields( FORM *form, FIELD **fields )
345 | Description : Set a new association of an array of fields to a form
347 | Return Values : E_OK - no error
348 | E_BAD_ARGUMENT - Invalid form pointer or field array
349 | E_CONNECTED - a field is already connected
350 | E_POSTED - form is posted
351 | E_SYSTEM_ERROR - not enough memory
352 +--------------------------------------------------------------------------*/
354 set_form_fields(FORM *form, FIELD **fields)
359 T((T_CALLED("set_form_fields(%p,%p)"), form, fields));
362 RETURN(E_BAD_ARGUMENT);
364 if (form->status & _POSTED)
368 Disconnect_Fields(form);
370 if ((res = Associate_Fields(form, fields)) != E_OK)
371 Connect_Fields(form, old);
376 /*---------------------------------------------------------------------------
377 | Facility : libnform
378 | Function : FIELD **form_fields( const FORM *form )
380 | Description : Retrieve array of fields
382 | Return Values : Pointer to field array
383 +--------------------------------------------------------------------------*/
384 NCURSES_EXPORT(FIELD **)
385 form_fields(const FORM *form)
387 T((T_CALLED("form_field(%p)"), form));
388 returnFieldPtr(Normalize_Form(form)->field);
391 /*---------------------------------------------------------------------------
392 | Facility : libnform
393 | Function : int field_count( const FORM *form )
395 | Description : Retrieve number of fields
397 | Return Values : Number of fields, -1 if none are defined
398 +--------------------------------------------------------------------------*/
400 field_count(const FORM *form)
402 T((T_CALLED("field_count(%p)"), form));
404 returnCode(Normalize_Form(form)->maxfield);
407 /* frm_def.c ends here */