ncurses 5.5
[ncurses.git] / form / frm_def.c
1 /****************************************************************************
2  * Copyright (c) 1998-2003,2004 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  *   Author:  Juergen Pfeifer, 1995,1997                                    *
31  ****************************************************************************/
32
33 #include "form.priv.h"
34
35 MODULE_ID("$Id: frm_def.c,v 1.17 2004/12/25 22:26:01 tom Exp $")
36
37 /* this can't be readonly */
38 static FORM default_form =
39 {
40   0,                            /* status     */
41   0,                            /* rows       */
42   0,                            /* cols       */
43   0,                            /* currow     */
44   0,                            /* curcol     */
45   0,                            /* toprow     */
46   0,                            /* begincol   */
47   -1,                           /* maxfield   */
48   -1,                           /* maxpage    */
49   -1,                           /* curpage    */
50   ALL_FORM_OPTS,                /* opts       */
51   (WINDOW *)0,                  /* win        */
52   (WINDOW *)0,                  /* sub        */
53   (WINDOW *)0,                  /* w          */
54   (FIELD **)0,                  /* field      */
55   (FIELD *)0,                   /* current    */
56   (_PAGE *) 0,                  /* page       */
57   (char *)0,                    /* usrptr     */
58   NULL,                         /* forminit   */
59   NULL,                         /* formterm   */
60   NULL,                         /* fieldinit  */
61   NULL                          /* fieldterm  */
62 };
63
64 NCURSES_EXPORT_VAR(FORM *) _nc_Default_Form = &default_form;
65 \f
66 /*---------------------------------------------------------------------------
67 |   Facility      :  libnform  
68 |   Function      :  static FIELD *Insert_Field_By_Position(
69 |                                     FIELD *new_field, 
70 |                                     FIELD *head )
71 |   
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.
75 |
76 |   Return Values :  New head of sorted fieldlist
77 +--------------------------------------------------------------------------*/
78 static FIELD *
79 Insert_Field_By_Position(FIELD *newfield, FIELD *head)
80 {
81   FIELD *current, *newhead;
82
83   assert(newfield);
84
85   if (!head)
86     {                           /* empty list is trivial */
87       newhead = newfield->snext = newfield->sprev = newfield;
88     }
89   else
90     {
91       newhead = current = head;
92       while ((current->frow < newfield->frow) ||
93              ((current->frow == newfield->frow) &&
94               (current->fcol < newfield->fcol)))
95         {
96           current = current->snext;
97           if (current == head)
98             {                   /* We cycled through. Reset head to indicate that */
99               head = (FIELD *)0;
100               break;
101             }
102         }
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;
108       if (current == head)
109         newhead = newfield;
110     }
111   return (newhead);
112 }
113
114 /*---------------------------------------------------------------------------
115 |   Facility      :  libnform  
116 |   Function      :  static void Disconnect_Fields(FORM *form)
117 |   
118 |   Description   :  Break association between form and array of fields.
119 |
120 |   Return Values :  -
121 +--------------------------------------------------------------------------*/
122 static void
123 Disconnect_Fields(FORM *form)
124 {
125   if (form->field)
126     {
127       FIELD **fields;
128
129       for (fields = form->field; *fields; fields++)
130         {
131           if (form == (*fields)->form)
132             (*fields)->form = (FORM *)0;
133         }
134
135       form->rows = form->cols = 0;
136       form->maxfield = form->maxpage = -1;
137       form->field = (FIELD **)0;
138       if (form->page)
139         free(form->page);
140       form->page = (_PAGE *) 0;
141     }
142 }
143
144 /*---------------------------------------------------------------------------
145 |   Facility      :  libnform  
146 |   Function      :  static int Connect_Fields(FORM *form, FIELD **fields)
147 |   
148 |   Description   :  Set association between form and array of fields.
149 |
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 +--------------------------------------------------------------------------*/
155 static int
156 Connect_Fields(FORM *form, FIELD **fields)
157 {
158   int field_cnt, j;
159   int page_nr;
160   int maximum_row_in_field, maximum_col_in_field;
161   _PAGE *pg;
162
163   T((T_CALLED("Connect_Fields(%p,%p)"), form, fields));
164
165   assert(form);
166
167   form->field = fields;
168   form->maxfield = 0;
169   form->maxpage = 0;
170
171   if (!fields)
172     RETURN(E_OK);
173
174   page_nr = 0;
175   /* store formpointer in fields and count pages */
176   for (field_cnt = 0; fields[field_cnt]; field_cnt++)
177     {
178       if (fields[field_cnt]->form)
179         RETURN(E_CONNECTED);
180       if (field_cnt == 0 ||
181           (fields[field_cnt]->status & _NEWPAGE))
182         page_nr++;
183       fields[field_cnt]->form = form;
184     }
185   if (field_cnt == 0)
186     RETURN(E_BAD_ARGUMENT);
187
188   /* allocate page structures */
189   if ((pg = (_PAGE *) malloc(page_nr * sizeof(_PAGE))) != (_PAGE *) 0)
190     {
191       form->page = pg;
192     }
193   else
194     RETURN(E_SYSTEM_ERROR);
195
196   /* Cycle through fields and calculate page boundaries as well as
197      size of the form */
198   for (j = 0; j < field_cnt; j++)
199     {
200       if (j == 0)
201         pg->pmin = j;
202       else
203         {
204           if (fields[j]->status & _NEWPAGE)
205             {
206               pg->pmax = j - 1;
207               pg++;
208               pg->pmin = j;
209             }
210         }
211
212       maximum_row_in_field = fields[j]->frow + fields[j]->rows;
213       maximum_col_in_field = fields[j]->fcol + fields[j]->cols;
214
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;
219     }
220
221   pg->pmax = field_cnt - 1;
222   form->maxfield = field_cnt;
223   form->maxpage = page_nr;
224
225   /* Sort fields on form pages */
226   for (page_nr = 0; page_nr < form->maxpage; page_nr++)
227     {
228       FIELD *fld = (FIELD *)0;
229
230       for (j = form->page[page_nr].pmin; j <= form->page[page_nr].pmax; j++)
231         {
232           fields[j]->index = j;
233           fields[j]->page = page_nr;
234           fld = Insert_Field_By_Position(fields[j], fld);
235         }
236       form->page[page_nr].smin = fld->index;
237       form->page[page_nr].smax = fld->sprev->index;
238     }
239   RETURN(E_OK);
240 }
241
242 /*---------------------------------------------------------------------------
243 |   Facility      :  libnform  
244 |   Function      :  static int Associate_Fields(FORM *form, FIELD **fields)
245 |   
246 |   Description   :  Set association between form and array of fields. 
247 |                    If there are fields, position to first active field.
248 |
249 |   Return Values :  E_OK            - success
250 |                    any other       - error occurred
251 +--------------------------------------------------------------------------*/
252 INLINE static int
253 Associate_Fields(FORM *form, FIELD **fields)
254 {
255   int res = Connect_Fields(form, fields);
256
257   if (res == E_OK)
258     {
259       if (form->maxpage > 0)
260         {
261           form->curpage = 0;
262           form_driver(form, FIRST_ACTIVE_MAGIC);
263         }
264       else
265         {
266           form->curpage = -1;
267           form->current = (FIELD *)0;
268         }
269     }
270   return (res);
271 }
272
273 /*---------------------------------------------------------------------------
274 |   Facility      :  libnform  
275 |   Function      :  FORM *new_form( FIELD **fields )
276 |   
277 |   Description   :  Create new form with given array of fields.
278 |
279 |   Return Values :  Pointer to form. NULL if error occurred.
280 +--------------------------------------------------------------------------*/
281 NCURSES_EXPORT(FORM *)
282 new_form(FIELD **fields)
283 {
284   int err = E_SYSTEM_ERROR;
285
286   FORM *form = (FORM *)malloc(sizeof(FORM));
287
288   T((T_CALLED("new_form(%p)"), fields));
289   if (form)
290     {
291       *form = *_nc_Default_Form;
292       if ((err = Associate_Fields(form, fields)) != E_OK)
293         {
294           free_form(form);
295           form = (FORM *)0;
296         }
297     }
298
299   if (!form)
300     SET_ERROR(err);
301
302   returnForm(form);
303 }
304
305 /*---------------------------------------------------------------------------
306 |   Facility      :  libnform  
307 |   Function      :  int free_form( FORM *form )
308 |   
309 |   Description   :  Release internal memory associated with form.
310 |
311 |   Return Values :  E_OK           - no error
312 |                    E_BAD_ARGUMENT - invalid form pointer
313 |                    E_POSTED       - form is posted
314 +--------------------------------------------------------------------------*/
315 NCURSES_EXPORT(int)
316 free_form(FORM *form)
317 {
318   T((T_CALLED("free_form(%p)"), form));
319
320   if (!form)
321     RETURN(E_BAD_ARGUMENT);
322
323   if (form->status & _POSTED)
324     RETURN(E_POSTED);
325
326   Disconnect_Fields(form);
327   if (form->page)
328     free(form->page);
329   free(form);
330
331   RETURN(E_OK);
332 }
333
334 /*---------------------------------------------------------------------------
335 |   Facility      :  libnform  
336 |   Function      :  int set_form_fields( FORM *form, FIELD **fields )
337 |   
338 |   Description   :  Set a new association of an array of fields to a form
339 |
340 |   Return Values :  E_OK              - no error
341 |                    E_BAD_ARGUMENT    - invalid form pointer
342 |                    E_POSTED          - form is posted
343 +--------------------------------------------------------------------------*/
344 NCURSES_EXPORT(int)
345 set_form_fields(FORM *form, FIELD **fields)
346 {
347   FIELD **old;
348   int res;
349
350   T((T_CALLED("set_form_fields(%p,%p)"), form, fields));
351
352   if (!form)
353     RETURN(E_BAD_ARGUMENT);
354
355   if (form->status & _POSTED)
356     RETURN(E_POSTED);
357
358   old = form->field;
359   Disconnect_Fields(form);
360
361   if ((res = Associate_Fields(form, fields)) != E_OK)
362     Connect_Fields(form, old);
363
364   RETURN(res);
365 }
366
367 /*---------------------------------------------------------------------------
368 |   Facility      :  libnform  
369 |   Function      :  FIELD **form_fields( const FORM *form )
370 |   
371 |   Description   :  Retrieve array of fields
372 |
373 |   Return Values :  Pointer to field array
374 +--------------------------------------------------------------------------*/
375 NCURSES_EXPORT(FIELD **)
376 form_fields(const FORM *form)
377 {
378   T((T_CALLED("form_field(%p)"), form));
379   returnFieldPtr(Normalize_Form(form)->field);
380 }
381
382 /*---------------------------------------------------------------------------
383 |   Facility      :  libnform  
384 |   Function      :  int field_count( const FORM *form )
385 |   
386 |   Description   :  Retrieve number of fields
387 |
388 |   Return Values :  Number of fields, -1 if none are defined
389 +--------------------------------------------------------------------------*/
390 NCURSES_EXPORT(int)
391 field_count(const FORM *form)
392 {
393   T((T_CALLED("field_count(%p)"), form));
394
395   returnCode(Normalize_Form(form)->maxfield);
396 }
397
398 /* frm_def.c ends here */