1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 1998-2010,2012 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Juergen Pfeifer, 1995,1997 *
32 ****************************************************************************/
34 #include "form.priv.h"
36 MODULE_ID("$Id: frm_def.c,v 1.28 2020/05/24 01:40:20 anonymous.maarten Exp $")
38 /* this can't be readonly */
39 static FORM default_form =
51 ALL_FORM_OPTS, /* opts */
52 (WINDOW *)0, /* win */
53 (WINDOW *)0, /* sub */
55 (FIELD **)0, /* field */
56 (FIELD *)0, /* current */
57 (_PAGE *) 0, /* page */
58 (char *)0, /* usrptr */
65 FORM_EXPORT_VAR(FORM *) _nc_Default_Form = &default_form;
67 /*---------------------------------------------------------------------------
69 | Function : static FIELD *Insert_Field_By_Position(
73 | Description : Insert new_field into sorted fieldlist with head "head"
74 | and return new head of sorted fieldlist. Sorting
75 | criteria is (row,column). This is a circular list.
77 | Return Values : New head of sorted fieldlist
78 +--------------------------------------------------------------------------*/
80 Insert_Field_By_Position(FIELD *newfield, FIELD *head)
82 FIELD *current, *newhead;
87 { /* empty list is trivial */
88 newhead = newfield->snext = newfield->sprev = newfield;
92 newhead = current = head;
93 while ((current->frow < newfield->frow) ||
94 ((current->frow == newfield->frow) &&
95 (current->fcol < newfield->fcol)))
97 current = current->snext;
99 { /* We cycled through. Reset head to indicate that */
104 /* we leave the loop with current pointing to the field after newfield */
105 newfield->snext = current;
106 newfield->sprev = current->sprev;
107 newfield->snext->sprev = newfield;
108 newfield->sprev->snext = newfield;
115 /*---------------------------------------------------------------------------
116 | Facility : libnform
117 | Function : static void Disconnect_Fields(FORM *form)
119 | Description : Break association between form and array of fields.
122 +--------------------------------------------------------------------------*/
124 Disconnect_Fields(FORM *form)
130 for (fields = form->field; *fields; fields++)
132 if (form == (*fields)->form)
133 (*fields)->form = (FORM *)0;
136 form->rows = form->cols = 0;
137 form->maxfield = form->maxpage = -1;
138 form->field = (FIELD **)0;
141 form->page = (_PAGE *) 0;
145 /*---------------------------------------------------------------------------
146 | Facility : libnform
147 | Function : static int Connect_Fields(FORM *form, FIELD **fields)
149 | Description : Set association between form and array of fields.
151 | Return Values : E_OK - no error
152 | E_CONNECTED - a field is already connected
153 | E_BAD_ARGUMENT - Invalid form pointer or field array
154 | E_SYSTEM_ERROR - not enough memory
155 +--------------------------------------------------------------------------*/
157 Connect_Fields(FORM *form, FIELD **fields)
161 int maximum_row_in_field, maximum_col_in_field;
164 T((T_CALLED("Connect_Fields(%p,%p)"), (void *)form, (void *)fields));
168 form->field = fields;
176 /* store formpointer in fields and count pages */
177 for (field_cnt = 0; fields[field_cnt]; field_cnt++)
179 if (fields[field_cnt]->form)
181 if (field_cnt == 0 ||
182 (fields[field_cnt]->status & _NEWPAGE))
184 fields[field_cnt]->form = form;
186 if (field_cnt == 0 || (short)field_cnt < 0)
187 RETURN(E_BAD_ARGUMENT);
189 /* allocate page structures */
190 if ((pg = typeMalloc(_PAGE, page_nr)) != (_PAGE *) 0)
192 T((T_CREATE("_PAGE %p"), (void *)pg));
196 RETURN(E_SYSTEM_ERROR);
198 /* Cycle through fields and calculate page boundaries as well as
200 for (j = 0; j < field_cnt; j++)
203 pg->pmin = (short) j;
206 if (fields[j]->status & _NEWPAGE)
208 pg->pmax = (short) (j - 1);
210 pg->pmin = (short) j;
214 maximum_row_in_field = fields[j]->frow + fields[j]->rows;
215 maximum_col_in_field = fields[j]->fcol + fields[j]->cols;
217 if (form->rows < maximum_row_in_field)
218 form->rows = (short) maximum_row_in_field;
219 if (form->cols < maximum_col_in_field)
220 form->cols = (short) maximum_col_in_field;
223 pg->pmax = (short) (field_cnt - 1);
224 form->maxfield = (short) field_cnt;
225 form->maxpage = (short) page_nr;
227 /* Sort fields on form pages */
228 for (page_nr = 0; page_nr < form->maxpage; page_nr++)
230 FIELD *fld = (FIELD *)0;
232 for (j = form->page[page_nr].pmin; j <= form->page[page_nr].pmax; j++)
234 fields[j]->index = (short) j;
235 fields[j]->page = (short) page_nr;
236 fld = Insert_Field_By_Position(fields[j], fld);
240 form->page[page_nr].smin = fld->index;
241 form->page[page_nr].smax = fld->sprev->index;
245 form->page[page_nr].smin = 0;
246 form->page[page_nr].smax = 0;
252 /*---------------------------------------------------------------------------
253 | Facility : libnform
254 | Function : static int Associate_Fields(FORM *form, FIELD **fields)
256 | Description : Set association between form and array of fields.
257 | If there are fields, position to first active field.
259 | Return Values : E_OK - success
260 | E_BAD_ARGUMENT - Invalid form pointer or field array
261 | E_CONNECTED - a field is already connected
262 | E_SYSTEM_ERROR - not enough memory
263 +--------------------------------------------------------------------------*/
264 NCURSES_INLINE static int
265 Associate_Fields(FORM *form, FIELD **fields)
267 int res = Connect_Fields(form, fields);
271 if (form->maxpage > 0)
274 form_driver(form, FIRST_ACTIVE_MAGIC);
279 form->current = (FIELD *)0;
285 /*---------------------------------------------------------------------------
286 | Facility : libnform
287 | Function : FORM *new_form_sp(SCREEN* sp, FIELD** fields )
289 | Description : Create new form with given array of fields.
291 | Return Values : Pointer to form. NULL if error occurred.
294 | E_BAD_ARGUMENT - Invalid form pointer or field array
295 | E_CONNECTED - a field is already connected
296 | E_SYSTEM_ERROR - not enough memory
297 +--------------------------------------------------------------------------*/
299 NCURSES_SP_NAME(new_form) (NCURSES_SP_DCLx FIELD **fields)
301 int err = E_SYSTEM_ERROR;
302 FORM *form = (FORM *)0;
304 T((T_CALLED("new_form(%p,%p)"), (void *)SP_PARM, (void *)fields));
306 if (IsValidScreen(SP_PARM))
308 form = typeMalloc(FORM, 1);
312 T((T_CREATE("form %p"), (void *)form));
313 *form = *_nc_Default_Form;
314 /* This ensures win and sub are always non-null,
315 so we can derive always the SCREEN that this form is
317 form->win = StdScreen(SP_PARM);
318 form->sub = StdScreen(SP_PARM);
319 if ((err = Associate_Fields(form, fields)) != E_OK)
333 /*---------------------------------------------------------------------------
334 | Facility : libnform
335 | Function : FORM* new_form(FIELD** fields )
337 | Description : Create new form with given array of fields.
339 | Return Values : Pointer to form. NULL if error occurred.
342 | E_BAD_ARGUMENT - Invalid form pointer or field array
343 | E_CONNECTED - a field is already connected
344 | E_SYSTEM_ERROR - not enough memory
345 +--------------------------------------------------------------------------*/
348 new_form(FIELD **fields)
350 return NCURSES_SP_NAME(new_form) (CURRENT_SCREEN, fields);
354 /*---------------------------------------------------------------------------
355 | Facility : libnform
356 | Function : int free_form( FORM *form )
358 | Description : Release internal memory associated with form.
360 | Return Values : E_OK - no error
361 | E_BAD_ARGUMENT - invalid form pointer
362 | E_POSTED - form is posted
363 +--------------------------------------------------------------------------*/
365 free_form(FORM *form)
367 T((T_CALLED("free_form(%p)"), (void *)form));
370 RETURN(E_BAD_ARGUMENT);
372 if (form->status & _POSTED)
375 Disconnect_Fields(form);
383 /*---------------------------------------------------------------------------
384 | Facility : libnform
385 | Function : int set_form_fields( FORM *form, FIELD **fields )
387 | Description : Set a new association of an array of fields to a form
389 | Return Values : E_OK - no error
390 | E_BAD_ARGUMENT - Invalid form pointer or field array
391 | E_CONNECTED - a field is already connected
392 | E_POSTED - form is posted
393 | E_SYSTEM_ERROR - not enough memory
394 +--------------------------------------------------------------------------*/
396 set_form_fields(FORM *form, FIELD **fields)
401 T((T_CALLED("set_form_fields(%p,%p)"), (void *)form, (void *)fields));
404 RETURN(E_BAD_ARGUMENT);
406 if (form->status & _POSTED)
410 Disconnect_Fields(form);
412 if ((res = Associate_Fields(form, fields)) != E_OK)
413 Connect_Fields(form, old);
418 /*---------------------------------------------------------------------------
419 | Facility : libnform
420 | Function : FIELD **form_fields( const FORM *form )
422 | Description : Retrieve array of fields
424 | Return Values : Pointer to field array
425 +--------------------------------------------------------------------------*/
426 FORM_EXPORT(FIELD **)
427 form_fields(const FORM *form)
429 T((T_CALLED("form_field(%p)"), (const void *)form));
430 returnFieldPtr(Normalize_Form(form)->field);
433 /*---------------------------------------------------------------------------
434 | Facility : libnform
435 | Function : int field_count( const FORM *form )
437 | Description : Retrieve number of fields
439 | Return Values : Number of fields, -1 if none are defined
440 +--------------------------------------------------------------------------*/
442 field_count(const FORM *form)
444 T((T_CALLED("field_count(%p)"), (const void *)form));
446 returnCode(Normalize_Form(form)->maxfield);
449 /* frm_def.c ends here */