]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/frm_driver.c
ncurses 6.2 - patch 20210227
[ncurses.git] / form / frm_driver.c
1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *   Author:  Juergen Pfeifer, 1995,1997                                    *
32  ****************************************************************************/
33
34 #include "form.priv.h"
35
36 MODULE_ID("$Id: frm_driver.c,v 1.133 2020/12/12 00:36:42 tom Exp $")
37
38 /*----------------------------------------------------------------------------
39   This is the core module of the form library. It contains the majority
40   of the driver routines as well as the form_driver function.
41
42   Essentially this module is nearly the whole library. This is because
43   all the functions in this module depends on some others in the module,
44   so it makes no sense to split them into separate files because they
45   will always be linked together. The only acceptable concern is turnaround
46   time for this module, but now we have all Pentiums or RISCs, so what!
47
48   The driver routines are grouped into nine generic categories:
49
50    a)   Page Navigation            ( all functions prefixed by PN_ )
51         The current page of the form is left and some new page is
52         entered.
53    b)   Inter-Field Navigation     ( all functions prefixed by FN_ )
54         The current field of the form is left and some new field is
55         entered.
56    c)   Intra-Field Navigation     ( all functions prefixed by IFN_ )
57         The current position in the current field is changed.
58    d)   Vertical Scrolling         ( all functions prefixed by VSC_ )
59         Essentially this is a specialization of Intra-Field navigation.
60         It has to check for a multi-line field.
61    e)   Horizontal Scrolling       ( all functions prefixed by HSC_ )
62         Essentially this is a specialization of Intra-Field navigation.
63         It has to check for a single-line field.
64    f)   Field Editing              ( all functions prefixed by FE_ )
65         The content of the current field is changed
66    g)   Edit Mode requests         ( all functions prefixed by EM_ )
67         Switching between insert and overlay mode
68    h)   Field-Validation requests  ( all functions prefixed by FV_ )
69         Perform verifications of the field.
70    i)   Choice requests            ( all functions prefixed by CR_ )
71         Requests to enumerate possible field values
72   --------------------------------------------------------------------------*/
73
74 /*----------------------------------------------------------------------------
75   Some remarks on the placements of assert() macros :
76   I use them only on "strategic" places, i.e. top level entries where
77   I want to make sure that things are set correctly. Throughout subordinate
78   routines I omit them mostly.
79   --------------------------------------------------------------------------*/
80
81 /*
82 Some options that may effect compatibility in behavior to SVr4 forms,
83 but they are here to allow a more intuitive and user friendly behavior of
84 our form implementation. This doesn't affect the API, so we feel it is
85 uncritical.
86
87 The initial implementation tries to stay very close with the behavior
88 of the original SVr4 implementation, although in some areas it is quite
89 clear that this isn't the most appropriate way. As far as possible this
90 sources will allow you to build a forms lib that behaves quite similar
91 to SVr4, but now and in the future we will give you better options.
92 Perhaps at some time we will make this configurable at runtime.
93 */
94
95 /* Implement a more user-friendly previous/next word behavior */
96 #define FRIENDLY_PREV_NEXT_WORD (1)
97 /* Fix the wrong behavior for forms with all fields inactive */
98 #define FIX_FORM_INACTIVE_BUG (1)
99 /* Allow dynamic field growth also when navigating past the end */
100 #define GROW_IF_NAVIGATE (1)
101
102 #if USE_WIDEC_SUPPORT
103 #define myADDNSTR(w, s, n) wide_waddnstr(w, s, n)
104 #define myINSNSTR(w, s, n) wide_winsnstr(w, s, n)
105 #define myINNSTR(w, s, n)  wide_winnstr(w, s, n)
106 #define myWCWIDTH(w, y, x) cell_width(w, y, x)
107 #else
108 #define myADDNSTR(w, s, n) waddnstr(w, s, n)
109 #define myINSNSTR(w, s, n) winsnstr(w, s, n)
110 #define myINNSTR(w, s, n)  winnstr(w, s, n)
111 #define myWCWIDTH(w, y, x) 1
112 #endif
113
114 /*----------------------------------------------------------------------------
115   Forward references to some internally used static functions
116   --------------------------------------------------------------------------*/
117 static int Inter_Field_Navigation(int (*const fct) (FORM *), FORM *form);
118 static int FN_Next_Field(FORM *form);
119 static int FN_Previous_Field(FORM *form);
120 static int FE_New_Line(FORM *);
121 static int FE_Delete_Previous(FORM *);
122
123 /*----------------------------------------------------------------------------
124   Macro Definitions.
125
126   Some Remarks on that: I use the convention to use UPPERCASE for constants
127   defined by Macros. If I provide a macro as a kind of inline routine to
128   provide some logic, I use my Upper_Lower case style.
129   --------------------------------------------------------------------------*/
130
131 /* Calculate the position of a single row in a field buffer */
132 #define Position_Of_Row_In_Buffer(field,row) ((row)*(field)->dcols)
133
134 /* Calculate start address for the field's buffer# N */
135 #define Address_Of_Nth_Buffer(field,N) \
136   ((field)->buf + (N)*(1+Buffer_Length(field)))
137
138 /* Calculate the start address of the row in the field's specified buffer# N */
139 #define Address_Of_Row_In_Nth_Buffer(field,N,row) \
140   (Address_Of_Nth_Buffer(field,N) + Position_Of_Row_In_Buffer(field,row))
141
142 /* Calculate the start address of the row in the field's primary buffer */
143 #define Address_Of_Row_In_Buffer(field,row) \
144   Address_Of_Row_In_Nth_Buffer(field,0,row)
145
146 /* Calculate the start address of the row in the form's current field
147    buffer# N */
148 #define Address_Of_Current_Row_In_Nth_Buffer(form,N) \
149    Address_Of_Row_In_Nth_Buffer((form)->current,N,(form)->currow)
150
151 /* Calculate the start address of the row in the form's current field
152    primary buffer */
153 #define Address_Of_Current_Row_In_Buffer(form) \
154    Address_Of_Current_Row_In_Nth_Buffer(form,0)
155
156 /* Calculate the address of the cursor in the form's current field
157    primary buffer */
158 #define Address_Of_Current_Position_In_Nth_Buffer(form,N) \
159    (Address_Of_Current_Row_In_Nth_Buffer(form,N) + (form)->curcol)
160
161 /* Calculate the address of the cursor in the form's current field
162    buffer# N */
163 #define Address_Of_Current_Position_In_Buffer(form) \
164   Address_Of_Current_Position_In_Nth_Buffer(form,0)
165
166 /* Logic to decide whether or not a field is actually a field with
167    vertical or horizontal scrolling */
168 #define Is_Scroll_Field(field)          \
169    (((field)->drows > (field)->rows) || \
170     ((field)->dcols > (field)->cols))
171
172 /* Logic to decide whether or not a field needs to have an individual window
173    instead of a derived window because it contains invisible parts.
174    This is true for non-public fields and for scrollable fields. */
175 #define Has_Invisible_Parts(field)     \
176   (!(Field_Has_Option(field, O_PUBLIC)) || \
177    Is_Scroll_Field(field))
178
179 /* Logic to decide whether or not a field needs justification */
180 #define Justification_Allowed(field)        \
181    (((field)->just != NO_JUSTIFICATION)  && \
182     (Single_Line_Field(field))           && \
183     ((Field_Has_Option(field, O_STATIC)  && \
184      ((field)->dcols == (field)->cols))  || \
185     Field_Has_Option(field, O_DYNAMIC_JUSTIFY)))
186
187 /* Logic to determine whether or not a dynamic field may still grow */
188 #define Growable(field) ((field)->status & _MAY_GROW)
189
190 /* Macro to set the attributes for a field's window */
191 #define Set_Field_Window_Attributes(field,win) \
192 (  wbkgdset((win),(chtype)((chtype)((field)->pad) | (field)->back)), \
193    (void) wattrset((win), (int)(field)->fore) )
194
195 /* Logic to decide whether or not a field really appears on the form */
196 #define Field_Really_Appears(field)         \
197   ((field->form)                          &&\
198    (field->form->status & _POSTED)        &&\
199    (Field_Has_Option(field, O_VISIBLE))   &&\
200    (field->page == field->form->curpage))
201
202 /* Logic to determine whether or not we are on the first position in the
203    current field */
204 #define First_Position_In_Current_Field(form) \
205   (((form)->currow==0) && ((form)->curcol==0))
206
207 #define Minimum(a,b) (((a)<=(b)) ? (a) : (b))
208 #define Maximum(a,b) (((a)>=(b)) ? (a) : (b))
209
210 /*----------------------------------------------------------------------------
211   Useful constants
212   --------------------------------------------------------------------------*/
213 static FIELD_CELL myBLANK = BLANK;
214 static FIELD_CELL myZEROS;
215
216 #ifdef TRACE
217 static void
218 check_pos(FORM *form, int lineno)
219 {
220   int y, x;
221
222   if (form && form->w)
223     {
224       getyx(form->w, y, x);
225       if (y != form->currow || x != form->curcol)
226         {
227           T(("CHECKPOS %s@%d have position %d,%d vs want %d,%d",
228              __FILE__, lineno,
229              y, x,
230              form->currow, form->curcol));
231         }
232     }
233 }
234 #define CHECKPOS(form) check_pos(form, __LINE__)
235 #else
236 #define CHECKPOS(form)          /* nothing */
237 #endif
238
239 /*----------------------------------------------------------------------------
240   Wide-character special functions
241   --------------------------------------------------------------------------*/
242 #if USE_WIDEC_SUPPORT
243 /* add like waddnstr, but using cchar_t* rather than char*
244  */
245 static int
246 wide_waddnstr(WINDOW *w, const cchar_t *s, int n)
247 {
248   int rc = OK;
249
250   while (n-- > 0)
251     {
252       if ((rc = wadd_wch(w, s)) != OK)
253         break;
254       ++s;
255     }
256   return rc;
257 }
258
259 /* insert like winsnstr, but using cchar_t* rather than char*
260  *
261  * X/Open Curses has no close equivalent; inserts are done only with wchar_t
262  * strings.
263  */
264 static int
265 wide_winsnstr(WINDOW *w, const cchar_t *s, int n)
266 {
267   int code = ERR;
268   int y, x;
269
270   while (n-- > 0)
271     {
272       getyx(w, y, x);
273       if ((code = wins_wch(w, s++)) != OK)
274         break;
275       if ((code = wmove(w, y, x + 1)) != OK)
276         break;
277     }
278   return code;
279 }
280
281 /* retrieve like winnstr, but using cchar_t*, rather than char*.
282  *
283  * X/Open Curses' closest equivalent, win_wchnstr(), is inconsistent with
284  * winnstr(), since it returns OK rather than the number of items transferred.
285  */
286 static int
287 wide_winnstr(WINDOW *w, cchar_t *s, int n)
288 {
289   int x;
290
291   win_wchnstr(w, s, n);
292   /*
293    * This function is used to extract the text only from the window.
294    * Strip attributes and color from the string so they will not be added
295    * back when copying the string to the window.
296    */
297   for (x = 0; x < n; ++x)
298     {
299       RemAttr(s[x], A_ATTRIBUTES);
300       SetPair(s[x], 0);
301     }
302   return n;
303 }
304
305 /*
306  * Returns the column of the base of the given cell.
307  */
308 static int
309 cell_base(WINDOW *win, int y, int x)
310 {
311   int result = x;
312
313   while (LEGALYX(win, y, x))
314     {
315       cchar_t *data = &(win->_line[y].text[x]);
316
317       if (isWidecBase(CHDEREF(data)) || !isWidecExt(CHDEREF(data)))
318         {
319           result = x;
320           break;
321         }
322       --x;
323     }
324   return result;
325 }
326
327 /*
328  * Returns the number of columns needed for the given cell in a window.
329  */
330 static int
331 cell_width(WINDOW *win, int y, int x)
332 {
333   int result = 1;
334
335   if (LEGALYX(win, y, x))
336     {
337       cchar_t *data = &(win->_line[y].text[x]);
338
339       if (isWidecExt(CHDEREF(data)))
340         {
341           /* recur, providing the number of columns to the next character */
342           result = cell_width(win, y, x - 1);
343         }
344       else
345         {
346           result = wcwidth(CharOf(CHDEREF(data)));
347         }
348     }
349   return result;
350 }
351
352 /*
353  * There is no wide-character function such as wdel_wch(), so we must find
354  * all of the cells that comprise a multi-column character and delete them
355  * one-by-one.
356  */
357 static void
358 delete_char(FORM *form)
359 {
360   int cells = cell_width(form->w, form->currow, form->curcol);
361
362   form->curcol = cell_base(form->w, form->currow, form->curcol);
363   wmove(form->w, form->currow, form->curcol);
364   while (cells-- > 0)
365     {
366       wdelch(form->w);
367     }
368 }
369 #define DeleteChar(form) delete_char(form)
370 #else
371 #define DeleteChar(form) \
372           wmove((form)->w, (form)->currow, (form)->curcol), \
373           wdelch((form)->w)
374 #endif
375
376 /*---------------------------------------------------------------------------
377 |   Facility      :  libnform
378 |   Function      :  static char *Get_Start_Of_Data(char * buf, int blen)
379 |
380 |   Description   :  Return pointer to first non-blank position in buffer.
381 |                    If buffer is empty return pointer to buffer itself.
382 |
383 |   Return Values :  Pointer to first non-blank position in buffer
384 +--------------------------------------------------------------------------*/
385 NCURSES_INLINE static FIELD_CELL *
386 Get_Start_Of_Data(FIELD_CELL *buf, int blen)
387 {
388   FIELD_CELL *p = buf;
389   FIELD_CELL *end = &buf[blen];
390
391   assert(buf && blen >= 0);
392   while ((p < end) && ISBLANK(*p))
393     p++;
394   return ((p == end) ? buf : p);
395 }
396
397 /*---------------------------------------------------------------------------
398 |   Facility      :  libnform
399 |   Function      :  static char *After_End_Of_Data(char * buf, int blen)
400 |
401 |   Description   :  Return pointer after last non-blank position in buffer.
402 |                    If buffer is empty, return pointer to buffer itself.
403 |
404 |   Return Values :  Pointer to position after last non-blank position in
405 |                    buffer.
406 +--------------------------------------------------------------------------*/
407 NCURSES_INLINE static FIELD_CELL *
408 After_End_Of_Data(FIELD_CELL *buf, int blen)
409 {
410   FIELD_CELL *p = &buf[blen];
411
412   assert(buf && blen >= 0);
413   while ((p > buf) && ISBLANK(p[-1]))
414     p--;
415   return (p);
416 }
417
418 /*---------------------------------------------------------------------------
419 |   Facility      :  libnform
420 |   Function      :  static char *Get_First_Whitespace_Character(
421 |                                     char * buf, int   blen)
422 |
423 |   Description   :  Position to the first whitespace character.
424 |
425 |   Return Values :  Pointer to first whitespace character in buffer.
426 +--------------------------------------------------------------------------*/
427 NCURSES_INLINE static FIELD_CELL *
428 Get_First_Whitespace_Character(FIELD_CELL *buf, int blen)
429 {
430   FIELD_CELL *p = buf;
431   FIELD_CELL *end = &p[blen];
432
433   assert(buf && blen >= 0);
434   while ((p < end) && !ISBLANK(*p))
435     p++;
436   return ((p == end) ? buf : p);
437 }
438
439 /*---------------------------------------------------------------------------
440 |   Facility      :  libnform
441 |   Function      :  static char *After_Last_Whitespace_Character(
442 |                                     char * buf, int blen)
443 |
444 |   Description   :  Get the position after the last whitespace character.
445 |
446 |   Return Values :  Pointer to position after last whitespace character in
447 |                    buffer.
448 +--------------------------------------------------------------------------*/
449 NCURSES_INLINE static FIELD_CELL *
450 After_Last_Whitespace_Character(FIELD_CELL *buf, int blen)
451 {
452   FIELD_CELL *p = &buf[blen];
453
454   assert(buf && blen >= 0);
455   while ((p > buf) && !ISBLANK(p[-1]))
456     p--;
457   return (p);
458 }
459
460 /* Set this to 1 to use the div_t version. This is a good idea if your
461    compiler has an intrinsic div() support. Unfortunately GNU-C has it
462    not yet.
463    N.B.: This only works if form->curcol follows immediately form->currow
464          and both are of type int.
465 */
466 #define USE_DIV_T (0)
467
468 /*---------------------------------------------------------------------------
469 |   Facility      :  libnform
470 |   Function      :  static void Adjust_Cursor_Position(
471 |                                       FORM * form, const char * pos)
472 |
473 |   Description   :  Set current row and column of the form to values
474 |                    corresponding to the buffer position.
475 |
476 |   Return Values :  -
477 +--------------------------------------------------------------------------*/
478 NCURSES_INLINE static void
479 Adjust_Cursor_Position(FORM *form, const FIELD_CELL *pos)
480 {
481   FIELD *field;
482   int idx;
483
484   field = form->current;
485   assert(pos >= field->buf && field->dcols > 0);
486   idx = (int)(pos - field->buf);
487 #if USE_DIV_T
488   *((div_t *) & (form->currow)) = div(idx, field->dcols);
489 #else
490   form->currow = idx / field->dcols;
491   form->curcol = idx - field->cols * form->currow;
492 #endif
493   if (field->drows < form->currow)
494     form->currow = 0;
495 }
496
497 /*---------------------------------------------------------------------------
498 |   Facility      :  libnform
499 |   Function      :  static void Buffer_To_Window(
500 |                                      const FIELD  * field,
501 |                                      WINDOW * win)
502 |
503 |   Description   :  Copy the buffer to the window. If it is a multi-line
504 |                    field, the buffer is split to the lines of the
505 |                    window without any editing.
506 |
507 |   Return Values :  -
508 +--------------------------------------------------------------------------*/
509 static void
510 Buffer_To_Window(const FIELD *field, WINDOW *win)
511 {
512   int width, height;
513   int y, x;
514   int len;
515   int row;
516   FIELD_CELL *pBuffer;
517
518   assert(win && field);
519
520   getyx(win, y, x);
521   width = getmaxx(win);
522   height = getmaxy(win);
523
524   for (row = 0, pBuffer = field->buf;
525        row < height;
526        row++, pBuffer += width)
527     {
528       if ((len = (int)(After_End_Of_Data(pBuffer, width) - pBuffer)) > 0)
529         {
530           wmove(win, row, 0);
531           myADDNSTR(win, pBuffer, len);
532         }
533     }
534   wmove(win, y, x);
535 }
536
537 /*---------------------------------------------------------------------------
538 |   Facility      :  libnform
539 |   Function      :  void _nc_get_fieldbuffer(
540 |                                          WINDOW * win,
541 |                                          FIELD  * field,
542 |                                          FIELD_CELL * buf)
543 |
544 |   Description   :  Copy the content of the window into the buffer.
545 |                    The multiple lines of a window are simply
546 |                    concatenated into the buffer. Pad characters in
547 |                    the window will be replaced by blanks in the buffer.
548 |
549 |   Return Values :  -
550 +--------------------------------------------------------------------------*/
551 FORM_EXPORT(void)
552 _nc_get_fieldbuffer(FORM *form, FIELD *field, FIELD_CELL *buf)
553 {
554   int pad;
555   int len = 0;
556   FIELD_CELL *p;
557   int row, height;
558   WINDOW *win;
559
560   assert(form && field && buf);
561
562   win = form->w;
563   assert(win);
564
565   pad = field->pad;
566   p = buf;
567   height = getmaxy(win);
568
569   for (row = 0; (row < height) && (row < field->drows); row++)
570     {
571       wmove(win, row, 0);
572       len += myINNSTR(win, p + len, field->dcols);
573     }
574   p[len] = myZEROS;
575
576   /* replace visual padding character by blanks in buffer */
577   if (pad != C_BLANK)
578     {
579       int i;
580
581       for (i = 0; i < len; i++, p++)
582         {
583           if ((unsigned long)CharOf(*p) == ChCharOf(pad)
584 #if USE_WIDEC_SUPPORT
585               && p->chars[1] == 0
586 #endif
587             )
588             *p = myBLANK;
589         }
590     }
591 }
592
593 /*---------------------------------------------------------------------------
594 |   Facility      :  libnform
595 |   Function      :  static void Window_To_Buffer(
596 |                                          FORM   * form,
597 |                                          FIELD  * field)
598 |
599 |   Description   :  Copy the content of the window into the buffer.
600 |                    The multiple lines of a window are simply
601 |                    concatenated into the buffer. Pad characters in
602 |                    the window will be replaced by blanks in the buffer.
603 |
604 |   Return Values :  -
605 +--------------------------------------------------------------------------*/
606 static void
607 Window_To_Buffer(FORM *form, FIELD *field)
608 {
609   _nc_get_fieldbuffer(form, field, field->buf);
610 }
611
612 /*---------------------------------------------------------------------------
613 |   Facility      :  libnform
614 |   Function      :  static void Synchronize_Buffer(FORM * form)
615 |
616 |   Description   :  If there was a change, copy the content of the
617 |                    window into the buffer, so the buffer is synchronized
618 |                    with the windows content. We have to indicate that the
619 |                    buffer needs validation due to the change.
620 |
621 |   Return Values :  -
622 +--------------------------------------------------------------------------*/
623 NCURSES_INLINE static void
624 Synchronize_Buffer(FORM *form)
625 {
626   if (form->status & _WINDOW_MODIFIED)
627     {
628       ClrStatus(form, _WINDOW_MODIFIED);
629       SetStatus(form, _FCHECK_REQUIRED);
630       Window_To_Buffer(form, form->current);
631       wmove(form->w, form->currow, form->curcol);
632     }
633 }
634
635 /*---------------------------------------------------------------------------
636 |   Facility      :  libnform
637 |   Function      :  static bool Field_Grown( FIELD *field, int amount)
638 |
639 |   Description   :  This function is called for growable dynamic fields
640 |                    only. It has to increase the buffers and to allocate
641 |                    a new window for this field.
642 |                    This function has the side effect to set a new
643 |                    field-buffer pointer, the dcols and drows values
644 |                    as well as a new current Window for the field.
645 |
646 |   Return Values :  TRUE     - field successfully increased
647 |                    FALSE    - there was some error
648 +--------------------------------------------------------------------------*/
649 static bool
650 Field_Grown(FIELD *field, int amount)
651 {
652   bool result = FALSE;
653
654   if (field && Growable(field))
655     {
656       bool single_line_field = Single_Line_Field(field);
657       int old_buflen = Buffer_Length(field);
658       int new_buflen;
659       int old_dcols = field->dcols;
660       int old_drows = field->drows;
661       FIELD_CELL *oldbuf = field->buf;
662       FIELD_CELL *newbuf;
663
664       int growth;
665       FORM *form = field->form;
666       bool need_visual_update = ((form != (FORM *)0) &&
667                                  (form->status & _POSTED) &&
668                                  (form->current == field));
669
670       if (need_visual_update)
671         Synchronize_Buffer(form);
672
673       if (single_line_field)
674         {
675           growth = field->cols * amount;
676           if (field->maxgrow)
677             growth = Minimum(field->maxgrow - field->dcols, growth);
678           field->dcols += growth;
679           if (field->dcols == field->maxgrow)
680             ClrStatus(field, _MAY_GROW);
681         }
682       else
683         {
684           growth = (field->rows + field->nrow) * amount;
685           if (field->maxgrow)
686             growth = Minimum(field->maxgrow - field->drows, growth);
687           field->drows += growth;
688           if (field->drows == field->maxgrow)
689             ClrStatus(field, _MAY_GROW);
690         }
691       /* drows, dcols changed, so we get really the new buffer length */
692       new_buflen = Buffer_Length(field);
693       newbuf = (FIELD_CELL *)malloc(Total_Buffer_Size(field));
694       if (!newbuf)
695         {
696           /* restore to previous state */
697           field->dcols = old_dcols;
698           field->drows = old_drows;
699           if ((single_line_field && (field->dcols != field->maxgrow)) ||
700               (!single_line_field && (field->drows != field->maxgrow)))
701             SetStatus(field, _MAY_GROW);
702         }
703       else
704         {
705           /* Copy all the buffers.  This is the reason why we can't just use
706            * realloc().
707            */
708           int i, j;
709           FIELD_CELL *old_bp;
710           FIELD_CELL *new_bp;
711
712           result = TRUE;        /* allow sharing of recovery on failure */
713
714           T((T_CREATE("fieldcell %p"), (void *)newbuf));
715           field->buf = newbuf;
716           for (i = 0; i <= field->nbuf; i++)
717             {
718               new_bp = Address_Of_Nth_Buffer(field, i);
719               old_bp = oldbuf + i * (1 + old_buflen);
720               for (j = 0; j < old_buflen; ++j)
721                 new_bp[j] = old_bp[j];
722               while (j < new_buflen)
723                 new_bp[j++] = myBLANK;
724               new_bp[new_buflen] = myZEROS;
725             }
726
727 #if USE_WIDEC_SUPPORT && NCURSES_EXT_FUNCS
728           if (wresize(field->working, 1, Buffer_Length(field) + 1) == ERR)
729             result = FALSE;
730 #endif
731
732           if (need_visual_update && result)
733             {
734               WINDOW *new_window = newpad(field->drows, field->dcols);
735
736               if (new_window != 0)
737                 {
738                   assert(form != (FORM *)0);
739                   if (form->w)
740                     delwin(form->w);
741                   form->w = new_window;
742                   Set_Field_Window_Attributes(field, form->w);
743                   werase(form->w);
744                   Buffer_To_Window(field, form->w);
745                   untouchwin(form->w);
746                   wmove(form->w, form->currow, form->curcol);
747                 }
748               else
749                 result = FALSE;
750             }
751
752           if (result)
753             {
754               free(oldbuf);
755               /* reflect changes in linked fields */
756               if (field != field->link)
757                 {
758                   FIELD *linked_field;
759
760                   for (linked_field = field->link;
761                        linked_field != field;
762                        linked_field = linked_field->link)
763                     {
764                       linked_field->buf = field->buf;
765                       linked_field->drows = field->drows;
766                       linked_field->dcols = field->dcols;
767                     }
768                 }
769             }
770           else
771             {
772               /* restore old state */
773               field->dcols = old_dcols;
774               field->drows = old_drows;
775               field->buf = oldbuf;
776               if ((single_line_field &&
777                    (field->dcols != field->maxgrow)) ||
778                   (!single_line_field &&
779                    (field->drows != field->maxgrow)))
780                 SetStatus(field, _MAY_GROW);
781               free(newbuf);
782             }
783         }
784     }
785   return (result);
786 }
787
788 #ifdef NCURSES_MOUSE_VERSION
789 /*---------------------------------------------------------------------------
790 |   Facility      :  libnform
791 |   Function      :  int Field_encloses(FIELD *field, int ry, int rx)
792 |
793 |   Description   :  Check if the given coordinates lie within the given field.
794 |
795 |   Return Values :  E_OK              - success
796 |                    E_BAD_ARGUMENT    - invalid form pointer
797 |                    E_SYSTEM_ERROR    - form has no current field or
798 |                                        field-window
799 +--------------------------------------------------------------------------*/
800 static int
801 Field_encloses(FIELD *field, int ry, int rx)
802 {
803   T((T_CALLED("Field_encloses(%p)"), (void *)field));
804   if (field != 0
805       && field->frow <= ry
806       && (field->frow + field->rows) > ry
807       && field->fcol <= rx
808       && (field->fcol + field->cols) > rx)
809     {
810       RETURN(E_OK);
811     }
812   RETURN(E_INVALID_FIELD);
813 }
814 #endif
815
816 /*---------------------------------------------------------------------------
817 |   Facility      :  libnform
818 |   Function      :  int _nc_Position_Form_Cursor(FORM * form)
819 |
820 |   Description   :  Position the cursor in the window for the current
821 |                    field to be in sync. with the currow and curcol
822 |                    values.
823 |
824 |   Return Values :  E_OK              - success
825 |                    E_BAD_ARGUMENT    - invalid form pointer
826 |                    E_SYSTEM_ERROR    - form has no current field or
827 |                                        field-window
828 +--------------------------------------------------------------------------*/
829 FORM_EXPORT(int)
830 _nc_Position_Form_Cursor(FORM *form)
831 {
832   FIELD *field;
833   WINDOW *formwin;
834
835   if (!form)
836     return (E_BAD_ARGUMENT);
837
838   if (!form->w || !form->current)
839     return (E_SYSTEM_ERROR);
840
841   field = form->current;
842   formwin = Get_Form_Window(form);
843
844   wmove(form->w, form->currow, form->curcol);
845   if (Has_Invisible_Parts(field))
846     {
847       /* in this case fieldwin isn't derived from formwin, so we have
848          to move the cursor in formwin by hand... */
849       wmove(formwin,
850             field->frow + form->currow - form->toprow,
851             field->fcol + form->curcol - form->begincol);
852       wcursyncup(formwin);
853     }
854   else
855     wcursyncup(form->w);
856   return (E_OK);
857 }
858
859 /*---------------------------------------------------------------------------
860 |   Facility      :  libnform
861 |   Function      :  int _nc_Refresh_Current_Field(FORM * form)
862 |
863 |   Description   :  Propagate the changes in the field's window to the
864 |                    window of the form.
865 |
866 |   Return Values :  E_OK              - on success
867 |                    E_BAD_ARGUMENT    - invalid form pointer
868 |                    E_SYSTEM_ERROR    - general error
869 +--------------------------------------------------------------------------*/
870 static bool move_after_insert = TRUE;
871 FORM_EXPORT(int)
872 _nc_Refresh_Current_Field(FORM *form)
873 {
874   WINDOW *formwin;
875   FIELD *field;
876   bool is_public;
877
878   T((T_CALLED("_nc_Refresh_Current_Field(%p)"), (void *)form));
879
880   if (!form)
881     RETURN(E_BAD_ARGUMENT);
882
883   if (!form->w || !form->current)
884     RETURN(E_SYSTEM_ERROR);
885
886   field = form->current;
887   formwin = Get_Form_Window(form);
888
889   is_public = Field_Has_Option(field, O_PUBLIC);
890
891   if (Is_Scroll_Field(field))
892     {
893       /* Again, in this case the fieldwin isn't derived from formwin,
894          so we have to perform a copy operation. */
895       if (Single_Line_Field(field))
896         {
897           /* horizontal scrolling */
898           if (form->curcol < form->begincol)
899             form->begincol = form->curcol;
900           else
901             {
902               if (form->curcol >= (form->begincol + field->cols))
903                 form->begincol = form->curcol - field->cols
904                   + (move_after_insert ? 1 : 0);
905             }
906           if (is_public)
907             copywin(form->w,
908                     formwin,
909                     0,
910                     form->begincol,
911                     field->frow,
912                     field->fcol,
913                     field->frow,
914                     field->cols + field->fcol - 1,
915                     0);
916         }
917       else
918         {
919           /* A multi-line, i.e. vertical scrolling field */
920           int row_after_bottom, first_modified_row, first_unmodified_row;
921
922           if (field->drows > field->rows)
923             {
924               row_after_bottom = form->toprow + field->rows;
925               if (form->currow < form->toprow)
926                 {
927                   form->toprow = form->currow;
928                   SetStatus(field, _NEWTOP);
929                 }
930               if (form->currow >= row_after_bottom)
931                 {
932                   form->toprow = form->currow - field->rows + 1;
933                   SetStatus(field, _NEWTOP);
934                 }
935               if (field->status & _NEWTOP)
936                 {
937                   /* means we have to copy whole range */
938                   first_modified_row = form->toprow;
939                   first_unmodified_row = first_modified_row + field->rows;
940                   ClrStatus(field, _NEWTOP);
941                 }
942               else
943                 {
944                   /* we try to optimize : finding the range of touched
945                      lines */
946                   first_modified_row = form->toprow;
947                   while (first_modified_row < row_after_bottom)
948                     {
949                       if (is_linetouched(form->w, first_modified_row))
950                         break;
951                       first_modified_row++;
952                     }
953                   first_unmodified_row = first_modified_row;
954                   while (first_unmodified_row < row_after_bottom)
955                     {
956                       if (!is_linetouched(form->w, first_unmodified_row))
957                         break;
958                       first_unmodified_row++;
959                     }
960                 }
961             }
962           else
963             {
964               first_modified_row = form->toprow;
965               first_unmodified_row = first_modified_row + field->rows;
966             }
967           if (first_unmodified_row != first_modified_row && is_public)
968             copywin(form->w,
969                     formwin,
970                     first_modified_row,
971                     0,
972                     field->frow + first_modified_row - form->toprow,
973                     field->fcol,
974                     field->frow + first_unmodified_row - form->toprow - 1,
975                     field->cols + field->fcol - 1,
976                     0);
977         }
978       if (is_public)
979         wsyncup(formwin);
980     }
981   else
982     {
983       /* if the field-window is simply a derived window, i.e. contains no
984        * invisible parts, the whole thing is trivial
985        */
986       if (is_public)
987         wsyncup(form->w);
988     }
989   untouchwin(form->w);
990   returnCode(_nc_Position_Form_Cursor(form));
991 }
992
993 /*---------------------------------------------------------------------------
994 |   Facility      :  libnform
995 |   Function      :  static void Perform_Justification(
996 |                                        FIELD  * field,
997 |                                        WINDOW * win)
998 |
999 |   Description   :  Output field with requested justification
1000 |
1001 |   Return Values :  -
1002 +--------------------------------------------------------------------------*/
1003 static void
1004 Perform_Justification(FIELD *field, WINDOW *win)
1005 {
1006   FIELD_CELL *bp;
1007   int len;
1008   int col = 0;
1009
1010   bp = (Field_Has_Option(field, O_NO_LEFT_STRIP)
1011         ? field->buf
1012         : Get_Start_Of_Data(field->buf, Buffer_Length(field)));
1013   len = (int)(After_End_Of_Data(field->buf, Buffer_Length(field)) - bp);
1014
1015   if (len > 0)
1016     {
1017       assert(win && (field->drows == 1));
1018
1019       if (field->cols - len >= 0)
1020         switch (field->just)
1021           {
1022           case JUSTIFY_LEFT:
1023             break;
1024           case JUSTIFY_CENTER:
1025             col = (field->cols - len) / 2;
1026             break;
1027           case JUSTIFY_RIGHT:
1028             col = field->cols - len;
1029             break;
1030           default:
1031             break;
1032           }
1033
1034       wmove(win, 0, col);
1035       myADDNSTR(win, bp, len);
1036     }
1037 }
1038
1039 /*---------------------------------------------------------------------------
1040 |   Facility      :  libnform
1041 |   Function      :  static void Undo_Justification(
1042 |                                     FIELD  * field,
1043 |                                     WINDOW * win)
1044 |
1045 |   Description   :  Display field without any justification, i.e.
1046 |                    left justified
1047 |
1048 |   Return Values :  -
1049 +--------------------------------------------------------------------------*/
1050 static void
1051 Undo_Justification(FIELD *field, WINDOW *win)
1052 {
1053   FIELD_CELL *bp;
1054   int y, x;
1055   int len;
1056
1057   getyx(win, y, x);
1058
1059   bp = (Field_Has_Option(field, O_NO_LEFT_STRIP)
1060         ? field->buf
1061         : Get_Start_Of_Data(field->buf, Buffer_Length(field)));
1062   len = (int)(After_End_Of_Data(field->buf, Buffer_Length(field)) - bp);
1063
1064   if (len > 0)
1065     {
1066       assert(win);
1067       wmove(win, 0, 0);
1068       myADDNSTR(win, bp, len);
1069     }
1070   wmove(win, y, x);
1071 }
1072
1073 /*---------------------------------------------------------------------------
1074 |   Facility      :  libnform
1075 |   Function      :  static bool Check_Char(FORM  *form,
1076 |                                           FIELD *field,
1077 |                                           FIELDTYPE * typ,
1078 |                                           int ch,
1079 |                                           TypeArgument *argp)
1080 |
1081 |   Description   :  Perform a single character check for character ch
1082 |                    according to the fieldtype instance.
1083 |
1084 |   Return Values :  TRUE             - Character is valid
1085 |                    FALSE            - Character is invalid
1086 +--------------------------------------------------------------------------*/
1087 static bool
1088 Check_Char(FORM *form,
1089            FIELD *field,
1090            FIELDTYPE *typ,
1091            int ch,
1092            TypeArgument *argp)
1093 {
1094   if (typ)
1095     {
1096       if (typ->status & _LINKED_TYPE)
1097         {
1098           assert(argp);
1099           return (
1100                    Check_Char(form, field, typ->left, ch, argp->left) ||
1101                    Check_Char(form, field, typ->right, ch, argp->right));
1102         }
1103       else
1104         {
1105 #if NCURSES_INTEROP_FUNCS
1106           if (typ->charcheck.occheck)
1107             {
1108               if (typ->status & _GENERIC)
1109                 return typ->charcheck.gccheck(ch, form, field, (void *)argp);
1110               else
1111                 return typ->charcheck.occheck(ch, (void *)argp);
1112             }
1113 #else
1114           if (typ->ccheck)
1115             return typ->ccheck(ch, (void *)argp);
1116 #endif
1117         }
1118     }
1119   return (!iscntrl(UChar(ch)) ? TRUE : FALSE);
1120 }
1121
1122 /*---------------------------------------------------------------------------
1123 |   Facility      :  libnform
1124 |   Function      :  static int Display_Or_Erase_Field(
1125 |                                           FIELD * field,
1126 |                                           bool bEraseFlag)
1127 |
1128 |   Description   :  Create a subwindow for the field and display the
1129 |                    buffer contents (apply justification if required)
1130 |                    or simply erase the field.
1131 |
1132 |   Return Values :  E_OK           - on success
1133 |                    E_SYSTEM_ERROR - some error (typical no memory)
1134 +--------------------------------------------------------------------------*/
1135 static int
1136 Display_Or_Erase_Field(FIELD *field, bool bEraseFlag)
1137 {
1138   WINDOW *win;
1139   WINDOW *fwin;
1140
1141   if (!field)
1142     return E_SYSTEM_ERROR;
1143
1144   fwin = Get_Form_Window(field->form);
1145   win = derwin(fwin,
1146                field->rows, field->cols, field->frow, field->fcol);
1147
1148   if (!win)
1149     return E_SYSTEM_ERROR;
1150   else
1151     {
1152       if (Field_Has_Option(field, O_VISIBLE))
1153         {
1154           Set_Field_Window_Attributes(field, win);
1155         }
1156       else
1157         {
1158           (void)wattrset(win, (int)WINDOW_ATTRS(fwin));
1159         }
1160       werase(win);
1161     }
1162
1163   if (!bEraseFlag)
1164     {
1165       if (Field_Has_Option(field, O_PUBLIC))
1166         {
1167           if (Justification_Allowed(field))
1168             Perform_Justification(field, win);
1169           else
1170             Buffer_To_Window(field, win);
1171         }
1172       ClrStatus(field, _NEWTOP);
1173     }
1174   wsyncup(win);
1175   delwin(win);
1176   return E_OK;
1177 }
1178
1179 /* Macros to preset the bEraseFlag */
1180 #define Display_Field(field) Display_Or_Erase_Field(field,FALSE)
1181 #define Erase_Field(field)   Display_Or_Erase_Field(field,TRUE)
1182
1183 /*---------------------------------------------------------------------------
1184 |   Facility      :  libnform
1185 |   Function      :  static int Synchronize_Field(FIELD * field)
1186 |
1187 |   Description   :  Synchronize the windows content with the value in
1188 |                    the buffer.
1189 |
1190 |   Return Values :  E_OK                - success
1191 |                    E_BAD_ARGUMENT      - invalid field pointer
1192 |                    E_SYSTEM_ERROR      - some severe basic error
1193 +--------------------------------------------------------------------------*/
1194 static int
1195 Synchronize_Field(FIELD *field)
1196 {
1197   FORM *form;
1198   int res = E_OK;
1199
1200   if (!field)
1201     return (E_BAD_ARGUMENT);
1202
1203   if (((form = field->form) != (FORM *)0)
1204       && Field_Really_Appears(field))
1205     {
1206       if (field == form->current)
1207         {
1208           form->currow = form->curcol = form->toprow = form->begincol = 0;
1209           werase(form->w);
1210
1211           if ((Field_Has_Option(field, O_PUBLIC)) && Justification_Allowed(field))
1212             Undo_Justification(field, form->w);
1213           else
1214             Buffer_To_Window(field, form->w);
1215
1216           SetStatus(field, _NEWTOP);
1217           res = _nc_Refresh_Current_Field(form);
1218         }
1219       else
1220         res = Display_Field(field);
1221     }
1222   SetStatus(field, _CHANGED);
1223   return (res);
1224 }
1225
1226 /*---------------------------------------------------------------------------
1227 |   Facility      :  libnform
1228 |   Function      :  static int Synchronize_Linked_Fields(FIELD * field)
1229 |
1230 |   Description   :  Propagate the Synchronize_Field function to all linked
1231 |                    fields. The first error that occurs in the sequence
1232 |                    of updates is the return value.
1233 |
1234 |   Return Values :  E_OK                - success
1235 |                    E_BAD_ARGUMENT      - invalid field pointer
1236 |                    E_SYSTEM_ERROR      - some severe basic error
1237 +--------------------------------------------------------------------------*/
1238 static int
1239 Synchronize_Linked_Fields(FIELD *field)
1240 {
1241   FIELD *linked_field;
1242   int res = E_OK;
1243   int syncres;
1244
1245   if (!field)
1246     return (E_BAD_ARGUMENT);
1247
1248   if (!field->link)
1249     return (E_SYSTEM_ERROR);
1250
1251   for (linked_field = field->link;
1252        (linked_field != field) && (linked_field != 0);
1253        linked_field = linked_field->link)
1254     {
1255       if (((syncres = Synchronize_Field(linked_field)) != E_OK) &&
1256           (res == E_OK))
1257         res = syncres;
1258     }
1259   return (res);
1260 }
1261
1262 /*---------------------------------------------------------------------------
1263 |   Facility      :  libnform
1264 |   Function      :  int _nc_Synchronize_Attributes(FIELD * field)
1265 |
1266 |   Description   :  If a field's visual attributes have changed, this
1267 |                    routine is called to propagate those changes to the
1268 |                    screen.
1269 |
1270 |   Return Values :  E_OK             - success
1271 |                    E_BAD_ARGUMENT   - invalid field pointer
1272 |                    E_SYSTEM_ERROR   - some severe basic error
1273 +--------------------------------------------------------------------------*/
1274 FORM_EXPORT(int)
1275 _nc_Synchronize_Attributes(FIELD *field)
1276 {
1277   FORM *form;
1278   int res = E_OK;
1279   WINDOW *formwin;
1280
1281   T((T_CALLED("_nc_Synchronize_Attributes(%p)"), (void *)field));
1282
1283   if (!field)
1284     returnCode(E_BAD_ARGUMENT);
1285
1286   CHECKPOS(field->form);
1287   if (((form = field->form) != (FORM *)0)
1288       && Field_Really_Appears(field))
1289     {
1290       if (form->current == field)
1291         {
1292           Synchronize_Buffer(form);
1293           Set_Field_Window_Attributes(field, form->w);
1294           werase(form->w);
1295           wmove(form->w, form->currow, form->curcol);
1296
1297           if (Field_Has_Option(field, O_PUBLIC))
1298             {
1299               if (Justification_Allowed(field))
1300                 Undo_Justification(field, form->w);
1301               else
1302                 Buffer_To_Window(field, form->w);
1303             }
1304           else
1305             {
1306               formwin = Get_Form_Window(form);
1307               copywin(form->w, formwin,
1308                       0, 0,
1309                       field->frow, field->fcol,
1310                       field->frow + field->rows - 1,
1311                       field->fcol + field->cols - 1, 0);
1312               wsyncup(formwin);
1313               Buffer_To_Window(field, form->w);
1314               SetStatus(field, _NEWTOP);        /* fake refresh to paint all */
1315               _nc_Refresh_Current_Field(form);
1316             }
1317         }
1318       else
1319         {
1320           res = Display_Field(field);
1321         }
1322     }
1323   CHECKPOS(form);
1324   returnCode(res);
1325 }
1326
1327 /*---------------------------------------------------------------------------
1328 |   Facility      :  libnform
1329 |   Function      :  int _nc_Synchronize_Options(FIELD * field,
1330 |                                                Field_Options newopts)
1331 |
1332 |   Description   :  If a field's options have changed, this routine is
1333 |                    called to propagate these changes to the screen and
1334 |                    to really change the behavior of the field.
1335 |
1336 |   Return Values :  E_OK                - success
1337 |                    E_BAD_ARGUMENT      - invalid field pointer
1338 |                    E_CURRENT           - field is the current one
1339 |                    E_SYSTEM_ERROR      - some severe basic error
1340 +--------------------------------------------------------------------------*/
1341 FORM_EXPORT(int)
1342 _nc_Synchronize_Options(FIELD *field, Field_Options newopts)
1343 {
1344   Field_Options oldopts;
1345   Field_Options changed_opts;
1346   FORM *form;
1347   int res = E_OK;
1348
1349   T((T_CALLED("_nc_Synchronize_Options(%p,%#x)"), (void *)field, newopts));
1350
1351   if (!field)
1352     returnCode(E_BAD_ARGUMENT);
1353
1354   oldopts = field->opts;
1355   changed_opts = oldopts ^ newopts;
1356   field->opts = newopts;
1357   form = field->form;
1358
1359   if (form)
1360     {
1361       if (form->status & _POSTED)
1362         {
1363           if (form->current == field)
1364             {
1365               field->opts = oldopts;
1366               returnCode(E_CURRENT);
1367             }
1368           if (form->curpage == field->page)
1369             {
1370               if ((unsigned)changed_opts & O_VISIBLE)
1371                 {
1372                   if ((unsigned)newopts & O_VISIBLE)
1373                     res = Display_Field(field);
1374                   else
1375                     res = Erase_Field(field);
1376                 }
1377               else
1378                 {
1379                   if (((unsigned)changed_opts & O_PUBLIC) &&
1380                       ((unsigned)newopts & O_VISIBLE))
1381                     res = Display_Field(field);
1382                 }
1383             }
1384         }
1385     }
1386
1387   if ((unsigned)changed_opts & O_STATIC)
1388     {
1389       bool single_line_field = Single_Line_Field(field);
1390       int res2 = E_OK;
1391
1392       if ((unsigned)newopts & O_STATIC)
1393         {
1394           /* the field becomes now static */
1395           ClrStatus(field, _MAY_GROW);
1396           /* if actually we have no hidden columns, justification may
1397              occur again */
1398           if (single_line_field &&
1399               (field->cols == field->dcols) &&
1400               (field->just != NO_JUSTIFICATION) &&
1401               Field_Really_Appears(field))
1402             {
1403               res2 = Display_Field(field);
1404             }
1405         }
1406       else
1407         {
1408           /* field is no longer static */
1409           if ((field->maxgrow == 0) ||
1410               (single_line_field && (field->dcols < field->maxgrow)) ||
1411               (!single_line_field && (field->drows < field->maxgrow)))
1412             {
1413               SetStatus(field, _MAY_GROW);
1414               /* a field with justification now changes its behavior,
1415                  so we must redisplay it */
1416               if (single_line_field &&
1417                   (field->just != NO_JUSTIFICATION) &&
1418                   Field_Really_Appears(field))
1419                 {
1420                   res2 = Display_Field(field);
1421                 }
1422             }
1423         }
1424       if (res2 != E_OK)
1425         res = res2;
1426     }
1427
1428   returnCode(res);
1429 }
1430
1431 /*
1432  * Removes the focus from the current field of the form.
1433  */
1434 void
1435 _nc_Unset_Current_Field(FORM *form)
1436 {
1437   FIELD *field = form->current;
1438
1439   _nc_Refresh_Current_Field(form);
1440   if (Field_Has_Option(field, O_PUBLIC))
1441     {
1442       if (field->drows > field->rows)
1443         {
1444           if (form->toprow == 0)
1445             ClrStatus(field, _NEWTOP);
1446           else
1447             SetStatus(field, _NEWTOP);
1448         }
1449       else
1450         {
1451           if (Justification_Allowed(field))
1452             {
1453               Window_To_Buffer(form, field);
1454               werase(form->w);
1455               Perform_Justification(field, form->w);
1456               if (Field_Has_Option(field, O_DYNAMIC_JUSTIFY) &&
1457                   (form->w->_parent == 0))
1458                 {
1459                   copywin(form->w,
1460                           Get_Form_Window(form),
1461                           0,
1462                           0,
1463                           field->frow,
1464                           field->fcol,
1465                           field->frow,
1466                           field->cols + field->fcol - 1,
1467                           0);
1468                   wsyncup(Get_Form_Window(form));
1469                 }
1470               else
1471                 {
1472                   wsyncup(form->w);
1473                 }
1474             }
1475         }
1476     }
1477   delwin(form->w);
1478   form->w = (WINDOW *)0;
1479   form->current = 0;
1480 }
1481
1482 /*---------------------------------------------------------------------------
1483 |   Facility      :  libnform
1484 |   Function      :  int _nc_Set_Current_Field(FORM  * form,
1485 |                                              FIELD * newfield)
1486 |
1487 |   Description   :  Make the newfield the new current field.
1488 |
1489 |   Return Values :  E_OK              - success
1490 |                    E_BAD_ARGUMENT    - invalid form or field pointer
1491 |                    E_SYSTEM_ERROR    - some severe basic error
1492 |                    E_NOT_CONNECTED   - no fields are connected to the form
1493 +--------------------------------------------------------------------------*/
1494 FORM_EXPORT(int)
1495 _nc_Set_Current_Field(FORM *form, FIELD *newfield)
1496 {
1497   FIELD *field;
1498   WINDOW *new_window;
1499
1500   T((T_CALLED("_nc_Set_Current_Field(%p,%p)"), (void *)form, (void *)newfield));
1501
1502   if (!form || !newfield || (newfield->form != form))
1503     returnCode(E_BAD_ARGUMENT);
1504
1505   if ((form->status & _IN_DRIVER))
1506     returnCode(E_BAD_STATE);
1507
1508   if (!(form->field))
1509     returnCode(E_NOT_CONNECTED);
1510
1511   field = form->current;
1512
1513   if ((field != newfield) ||
1514       !(form->status & _POSTED))
1515     {
1516       if (field && (form->w) &&
1517           (Field_Has_Option(field, O_VISIBLE)) &&
1518           (field->form->curpage == field->page))
1519         _nc_Unset_Current_Field(form);
1520
1521       field = newfield;
1522
1523       if (Has_Invisible_Parts(field))
1524         new_window = newpad(field->drows, field->dcols);
1525       else
1526         new_window = derwin(Get_Form_Window(form),
1527                             field->rows, field->cols, field->frow, field->fcol);
1528
1529       if (!new_window)
1530         returnCode(E_SYSTEM_ERROR);
1531
1532       form->current = field;
1533
1534       if (form->w)
1535         delwin(form->w);
1536       form->w = new_window;
1537
1538       ClrStatus(form, _WINDOW_MODIFIED);
1539       Set_Field_Window_Attributes(field, form->w);
1540
1541       if (Has_Invisible_Parts(field))
1542         {
1543           werase(form->w);
1544           Buffer_To_Window(field, form->w);
1545         }
1546       else
1547         {
1548           if (Justification_Allowed(field))
1549             {
1550               werase(form->w);
1551               Undo_Justification(field, form->w);
1552               wsyncup(form->w);
1553             }
1554         }
1555
1556       untouchwin(form->w);
1557     }
1558
1559   form->currow = form->curcol = form->toprow = form->begincol = 0;
1560   returnCode(E_OK);
1561 }
1562
1563 /*----------------------------------------------------------------------------
1564   Intra-Field Navigation routines
1565   --------------------------------------------------------------------------*/
1566
1567 /*---------------------------------------------------------------------------
1568 |   Facility      :  libnform
1569 |   Function      :  static int IFN_Next_Character(FORM * form)
1570 |
1571 |   Description   :  Move to the next character in the field. In a multi-line
1572 |                    field this wraps at the end of the line.
1573 |
1574 |   Return Values :  E_OK                - success
1575 |                    E_REQUEST_DENIED    - at the rightmost position
1576 +--------------------------------------------------------------------------*/
1577 static int
1578 IFN_Next_Character(FORM *form)
1579 {
1580   FIELD *field = form->current;
1581   int step = myWCWIDTH(form->w, form->currow, form->curcol);
1582
1583   T((T_CALLED("IFN_Next_Character(%p)"), (void *)form));
1584   if ((form->curcol += step) == field->dcols)
1585     {
1586       if ((++(form->currow)) == field->drows)
1587         {
1588 #if GROW_IF_NAVIGATE
1589           if (!Single_Line_Field(field) && Field_Grown(field, 1))
1590             {
1591               form->curcol = 0;
1592               returnCode(E_OK);
1593             }
1594 #endif
1595           form->currow--;
1596 #if GROW_IF_NAVIGATE
1597           if (Single_Line_Field(field) && Field_Grown(field, 1))
1598             returnCode(E_OK);
1599 #endif
1600           form->curcol -= step;
1601           returnCode(E_REQUEST_DENIED);
1602         }
1603       form->curcol = 0;
1604     }
1605   returnCode(E_OK);
1606 }
1607
1608 /*---------------------------------------------------------------------------
1609 |   Facility      :  libnform
1610 |   Function      :  static int IFN_Previous_Character(FORM * form)
1611 |
1612 |   Description   :  Move to the previous character in the field. In a
1613 |                    multi-line field this wraps and the beginning of the
1614 |                    line.
1615 |
1616 |   Return Values :  E_OK                - success
1617 |                    E_REQUEST_DENIED    - at the leftmost position
1618 +--------------------------------------------------------------------------*/
1619 static int
1620 IFN_Previous_Character(FORM *form)
1621 {
1622   int amount = myWCWIDTH(form->w, form->currow, form->curcol - 1);
1623   int oldcol = form->curcol;
1624
1625   T((T_CALLED("IFN_Previous_Character(%p)"), (void *)form));
1626   if ((form->curcol -= amount) < 0)
1627     {
1628       if ((--(form->currow)) < 0)
1629         {
1630           form->currow++;
1631           form->curcol = oldcol;
1632           returnCode(E_REQUEST_DENIED);
1633         }
1634       form->curcol = form->current->dcols - 1;
1635     }
1636   returnCode(E_OK);
1637 }
1638
1639 /*---------------------------------------------------------------------------
1640 |   Facility      :  libnform
1641 |   Function      :  static int IFN_Next_Line(FORM * form)
1642 |
1643 |   Description   :  Move to the beginning of the next line in the field
1644 |
1645 |   Return Values :  E_OK                - success
1646 |                    E_REQUEST_DENIED    - at the last line
1647 +--------------------------------------------------------------------------*/
1648 static int
1649 IFN_Next_Line(FORM *form)
1650 {
1651   FIELD *field = form->current;
1652
1653   T((T_CALLED("IFN_Next_Line(%p)"), (void *)form));
1654   if ((++(form->currow)) == field->drows)
1655     {
1656 #if GROW_IF_NAVIGATE
1657       if (!Single_Line_Field(field) && Field_Grown(field, 1))
1658         returnCode(E_OK);
1659 #endif
1660       form->currow--;
1661       returnCode(E_REQUEST_DENIED);
1662     }
1663   form->curcol = 0;
1664   returnCode(E_OK);
1665 }
1666
1667 /*---------------------------------------------------------------------------
1668 |   Facility      :  libnform
1669 |   Function      :  static int IFN_Previous_Line(FORM * form)
1670 |
1671 |   Description   :  Move to the beginning of the previous line in the field
1672 |
1673 |   Return Values :  E_OK                - success
1674 |                    E_REQUEST_DENIED    - at the first line
1675 +--------------------------------------------------------------------------*/
1676 static int
1677 IFN_Previous_Line(FORM *form)
1678 {
1679   T((T_CALLED("IFN_Previous_Line(%p)"), (void *)form));
1680   if ((--(form->currow)) < 0)
1681     {
1682       form->currow++;
1683       returnCode(E_REQUEST_DENIED);
1684     }
1685   form->curcol = 0;
1686   returnCode(E_OK);
1687 }
1688
1689 /*---------------------------------------------------------------------------
1690 |   Facility      :  libnform
1691 |   Function      :  static int IFN_Next_Word(FORM * form)
1692 |
1693 |   Description   :  Move to the beginning of the next word in the field.
1694 |
1695 |   Return Values :  E_OK             - success
1696 |                    E_REQUEST_DENIED - there is no next word
1697 +--------------------------------------------------------------------------*/
1698 static int
1699 IFN_Next_Word(FORM *form)
1700 {
1701   FIELD *field = form->current;
1702   FIELD_CELL *bp = Address_Of_Current_Position_In_Buffer(form);
1703   FIELD_CELL *s;
1704   FIELD_CELL *t;
1705
1706   T((T_CALLED("IFN_Next_Word(%p)"), (void *)form));
1707
1708   /* We really need access to the data, so we have to synchronize */
1709   Synchronize_Buffer(form);
1710
1711   /* Go to the first whitespace after the current position (including
1712      current position). This is then the starting point to look for the
1713      next non-blank data */
1714   s = Get_First_Whitespace_Character(bp, Buffer_Length(field) -
1715                                      (int)(bp - field->buf));
1716
1717   /* Find the start of the next word */
1718   t = Get_Start_Of_Data(s, Buffer_Length(field) -
1719                         (int)(s - field->buf));
1720 #if !FRIENDLY_PREV_NEXT_WORD
1721   if (s == t)
1722     returnCode(E_REQUEST_DENIED);
1723   else
1724 #endif
1725     {
1726       Adjust_Cursor_Position(form, t);
1727       returnCode(E_OK);
1728     }
1729 }
1730
1731 /*---------------------------------------------------------------------------
1732 |   Facility      :  libnform
1733 |   Function      :  static int IFN_Previous_Word(FORM * form)
1734 |
1735 |   Description   :  Move to the beginning of the previous word in the field.
1736 |
1737 |   Return Values :  E_OK             - success
1738 |                    E_REQUEST_DENIED - there is no previous word
1739 +--------------------------------------------------------------------------*/
1740 static int
1741 IFN_Previous_Word(FORM *form)
1742 {
1743   FIELD *field = form->current;
1744   FIELD_CELL *bp = Address_Of_Current_Position_In_Buffer(form);
1745   FIELD_CELL *s;
1746   FIELD_CELL *t;
1747   bool again = FALSE;
1748
1749   T((T_CALLED("IFN_Previous_Word(%p)"), (void *)form));
1750
1751   /* We really need access to the data, so we have to synchronize */
1752   Synchronize_Buffer(form);
1753
1754   s = After_End_Of_Data(field->buf, (int)(bp - field->buf));
1755   /* s points now right after the last non-blank in the buffer before bp.
1756      If bp was in a word, s equals bp. In this case we must find the last
1757      whitespace in the buffer before bp and repeat the game to really find
1758      the previous word! */
1759   if (s == bp)
1760     again = TRUE;
1761
1762   /* And next call now goes backward to look for the last whitespace
1763      before that, pointing right after this, so it points to the begin
1764      of the previous word.
1765    */
1766   t = After_Last_Whitespace_Character(field->buf, (int)(s - field->buf));
1767 #if !FRIENDLY_PREV_NEXT_WORD
1768   if (s == t)
1769     returnCode(E_REQUEST_DENIED);
1770 #endif
1771   if (again)
1772     {
1773       /* and do it again, replacing bp by t */
1774       s = After_End_Of_Data(field->buf, (int)(t - field->buf));
1775       t = After_Last_Whitespace_Character(field->buf, (int)(s - field->buf));
1776 #if !FRIENDLY_PREV_NEXT_WORD
1777       if (s == t)
1778         returnCode(E_REQUEST_DENIED);
1779 #endif
1780     }
1781   Adjust_Cursor_Position(form, t);
1782   returnCode(E_OK);
1783 }
1784
1785 /*---------------------------------------------------------------------------
1786 |   Facility      :  libnform
1787 |   Function      :  static int IFN_Beginning_Of_Field(FORM * form)
1788 |
1789 |   Description   :  Place the cursor at the first non-pad character in
1790 |                    the field.
1791 |
1792 |   Return Values :  E_OK             - success
1793 +--------------------------------------------------------------------------*/
1794 static int
1795 IFN_Beginning_Of_Field(FORM *form)
1796 {
1797   FIELD *field = form->current;
1798
1799   T((T_CALLED("IFN_Beginning_Of_Field(%p)"), (void *)form));
1800   Synchronize_Buffer(form);
1801   Adjust_Cursor_Position(form,
1802                          Get_Start_Of_Data(field->buf, Buffer_Length(field)));
1803   returnCode(E_OK);
1804 }
1805
1806 /*---------------------------------------------------------------------------
1807 |   Facility      :  libnform
1808 |   Function      :  static int IFN_End_Of_Field(FORM * form)
1809 |
1810 |   Description   :  Place the cursor after the last non-pad character in
1811 |                    the field. If the field occupies the last position in
1812 |                    the buffer, the cursor is positioned on the last
1813 |                    character.
1814 |
1815 |   Return Values :  E_OK              - success
1816 +--------------------------------------------------------------------------*/
1817 static int
1818 IFN_End_Of_Field(FORM *form)
1819 {
1820   FIELD *field = form->current;
1821   FIELD_CELL *pos;
1822
1823   T((T_CALLED("IFN_End_Of_Field(%p)"), (void *)form));
1824   Synchronize_Buffer(form);
1825   pos = After_End_Of_Data(field->buf, Buffer_Length(field));
1826   if (pos == (field->buf + Buffer_Length(field)))
1827     pos--;
1828   Adjust_Cursor_Position(form, pos);
1829   returnCode(E_OK);
1830 }
1831
1832 /*---------------------------------------------------------------------------
1833 |   Facility      :  libnform
1834 |   Function      :  static int IFN_Beginning_Of_Line(FORM * form)
1835 |
1836 |   Description   :  Place the cursor on the first non-pad character in
1837 |                    the current line of the field.
1838 |
1839 |   Return Values :  E_OK         - success
1840 +--------------------------------------------------------------------------*/
1841 static int
1842 IFN_Beginning_Of_Line(FORM *form)
1843 {
1844   FIELD *field = form->current;
1845
1846   T((T_CALLED("IFN_Beginning_Of_Line(%p)"), (void *)form));
1847   Synchronize_Buffer(form);
1848   Adjust_Cursor_Position(form,
1849                          Get_Start_Of_Data(Address_Of_Current_Row_In_Buffer(form),
1850                                            field->dcols));
1851   returnCode(E_OK);
1852 }
1853
1854 /*---------------------------------------------------------------------------
1855 |   Facility      :  libnform
1856 |   Function      :  static int IFN_End_Of_Line(FORM * form)
1857 |
1858 |   Description   :  Place the cursor after the last non-pad character in the
1859 |                    current line of the field. If the field occupies the
1860 |                    last column in the line, the cursor is positioned on the
1861 |                    last character of the line.
1862 |
1863 |   Return Values :  E_OK        - success
1864 +--------------------------------------------------------------------------*/
1865 static int
1866 IFN_End_Of_Line(FORM *form)
1867 {
1868   FIELD *field = form->current;
1869   FIELD_CELL *pos;
1870   FIELD_CELL *bp;
1871
1872   T((T_CALLED("IFN_End_Of_Line(%p)"), (void *)form));
1873   Synchronize_Buffer(form);
1874   bp = Address_Of_Current_Row_In_Buffer(form);
1875   pos = After_End_Of_Data(bp, field->dcols);
1876   if (pos == (bp + field->dcols))
1877     pos--;
1878   Adjust_Cursor_Position(form, pos);
1879   returnCode(E_OK);
1880 }
1881
1882 /*---------------------------------------------------------------------------
1883 |   Facility      :  libnform
1884 |   Function      :  static int IFN_Left_Character(FORM * form)
1885 |
1886 |   Description   :  Move one character to the left in the current line.
1887 |                    This doesn't cycle.
1888 |
1889 |   Return Values :  E_OK             - success
1890 |                    E_REQUEST_DENIED - already in first column
1891 +--------------------------------------------------------------------------*/
1892 static int
1893 IFN_Left_Character(FORM *form)
1894 {
1895   int amount = myWCWIDTH(form->w, form->currow, form->curcol - 1);
1896   int oldcol = form->curcol;
1897
1898   T((T_CALLED("IFN_Left_Character(%p)"), (void *)form));
1899   if ((form->curcol -= amount) < 0)
1900     {
1901       form->curcol = oldcol;
1902       returnCode(E_REQUEST_DENIED);
1903     }
1904   returnCode(E_OK);
1905 }
1906
1907 /*---------------------------------------------------------------------------
1908 |   Facility      :  libnform
1909 |   Function      :  static int IFN_Right_Character(FORM * form)
1910 |
1911 |   Description   :  Move one character to the right in the current line.
1912 |                    This doesn't cycle.
1913 |
1914 |   Return Values :  E_OK              - success
1915 |                    E_REQUEST_DENIED  - already in last column
1916 +--------------------------------------------------------------------------*/
1917 static int
1918 IFN_Right_Character(FORM *form)
1919 {
1920   int amount = myWCWIDTH(form->w, form->currow, form->curcol);
1921   int oldcol = form->curcol;
1922
1923   T((T_CALLED("IFN_Right_Character(%p)"), (void *)form));
1924   if ((form->curcol += amount) >= form->current->dcols)
1925     {
1926 #if GROW_IF_NAVIGATE
1927       FIELD *field = form->current;
1928
1929       if (Single_Line_Field(field) && Field_Grown(field, 1))
1930         returnCode(E_OK);
1931 #endif
1932       form->curcol = oldcol;
1933       returnCode(E_REQUEST_DENIED);
1934     }
1935   returnCode(E_OK);
1936 }
1937
1938 /*---------------------------------------------------------------------------
1939 |   Facility      :  libnform
1940 |   Function      :  static int IFN_Up_Character(FORM * form)
1941 |
1942 |   Description   :  Move one line up. This doesn't cycle through the lines
1943 |                    of the field.
1944 |
1945 |   Return Values :  E_OK              - success
1946 |                    E_REQUEST_DENIED  - already in last column
1947 +--------------------------------------------------------------------------*/
1948 static int
1949 IFN_Up_Character(FORM *form)
1950 {
1951   T((T_CALLED("IFN_Up_Character(%p)"), (void *)form));
1952   if ((--(form->currow)) < 0)
1953     {
1954       form->currow++;
1955       returnCode(E_REQUEST_DENIED);
1956     }
1957   returnCode(E_OK);
1958 }
1959
1960 /*---------------------------------------------------------------------------
1961 |   Facility      :  libnform
1962 |   Function      :  static int IFN_Down_Character(FORM * form)
1963 |
1964 |   Description   :  Move one line down. This doesn't cycle through the
1965 |                    lines of the field.
1966 |
1967 |   Return Values :  E_OK              - success
1968 |                    E_REQUEST_DENIED  - already in last column
1969 +--------------------------------------------------------------------------*/
1970 static int
1971 IFN_Down_Character(FORM *form)
1972 {
1973   FIELD *field = form->current;
1974
1975   T((T_CALLED("IFN_Down_Character(%p)"), (void *)form));
1976   if ((++(form->currow)) == field->drows)
1977     {
1978 #if GROW_IF_NAVIGATE
1979       if (!Single_Line_Field(field) && Field_Grown(field, 1))
1980         returnCode(E_OK);
1981 #endif
1982       --(form->currow);
1983       returnCode(E_REQUEST_DENIED);
1984     }
1985   returnCode(E_OK);
1986 }
1987 /*----------------------------------------------------------------------------
1988   END of Intra-Field Navigation routines
1989   --------------------------------------------------------------------------*/
1990
1991 /*----------------------------------------------------------------------------
1992   Vertical scrolling helper routines
1993   --------------------------------------------------------------------------*/
1994
1995 /*---------------------------------------------------------------------------
1996 |   Facility      :  libnform
1997 |   Function      :  static int VSC_Generic(FORM *form, int nlines)
1998 |
1999 |   Description   :  Scroll multi-line field forward (nlines>0) or
2000 |                    backward (nlines<0) this many lines.
2001 |
2002 |   Return Values :  E_OK              - success
2003 |                    E_REQUEST_DENIED  - can't scroll
2004 +--------------------------------------------------------------------------*/
2005 static int
2006 VSC_Generic(FORM *form, int nlines)
2007 {
2008   FIELD *field = form->current;
2009   int res = E_REQUEST_DENIED;
2010   int rows_to_go = (nlines > 0 ? nlines : -nlines);
2011
2012   if (nlines > 0)
2013     {
2014       if ((rows_to_go + form->toprow) > (field->drows - field->rows))
2015         rows_to_go = (field->drows - field->rows - form->toprow);
2016
2017       if (rows_to_go > 0)
2018         {
2019           form->currow += rows_to_go;
2020           form->toprow += rows_to_go;
2021           res = E_OK;
2022         }
2023     }
2024   else
2025     {
2026       if (rows_to_go > form->toprow)
2027         rows_to_go = form->toprow;
2028
2029       if (rows_to_go > 0)
2030         {
2031           form->currow -= rows_to_go;
2032           form->toprow -= rows_to_go;
2033           res = E_OK;
2034         }
2035     }
2036   return (res);
2037 }
2038 /*----------------------------------------------------------------------------
2039   End of Vertical scrolling helper routines
2040   --------------------------------------------------------------------------*/
2041
2042 /*----------------------------------------------------------------------------
2043   Vertical scrolling routines
2044   --------------------------------------------------------------------------*/
2045
2046 /*---------------------------------------------------------------------------
2047 |   Facility      :  libnform
2048 |   Function      :  static int Vertical_Scrolling(
2049 |                                           int (* const fct) (FORM *),
2050 |                                           FORM * form)
2051 |
2052 |   Description   :  Performs the generic vertical scrolling routines.
2053 |                    This has to check for a multi-line field and to set
2054 |                    the _NEWTOP flag if scrolling really occurred.
2055 |
2056 |   Return Values :  Propagated error code from low-level driver calls
2057 +--------------------------------------------------------------------------*/
2058 static int
2059 Vertical_Scrolling(int (*const fct) (FORM *), FORM *form)
2060 {
2061   int res = E_REQUEST_DENIED;
2062
2063   if (!Single_Line_Field(form->current))
2064     {
2065       res = fct(form);
2066       if (res == E_OK)
2067         SetStatus(form->current, _NEWTOP);
2068     }
2069   return (res);
2070 }
2071
2072 /*---------------------------------------------------------------------------
2073 |   Facility      :  libnform
2074 |   Function      :  static int VSC_Scroll_Line_Forward(FORM * form)
2075 |
2076 |   Description   :  Scroll multi-line field forward a line
2077 |
2078 |   Return Values :  E_OK                - success
2079 |                    E_REQUEST_DENIED    - no data ahead
2080 +--------------------------------------------------------------------------*/
2081 static int
2082 VSC_Scroll_Line_Forward(FORM *form)
2083 {
2084   T((T_CALLED("VSC_Scroll_Line_Forward(%p)"), (void *)form));
2085   returnCode(VSC_Generic(form, 1));
2086 }
2087
2088 /*---------------------------------------------------------------------------
2089 |   Facility      :  libnform
2090 |   Function      :  static int VSC_Scroll_Line_Backward(FORM * form)
2091 |
2092 |   Description   :  Scroll multi-line field backward a line
2093 |
2094 |   Return Values :  E_OK                - success
2095 |                    E_REQUEST_DENIED    - no data behind
2096 +--------------------------------------------------------------------------*/
2097 static int
2098 VSC_Scroll_Line_Backward(FORM *form)
2099 {
2100   T((T_CALLED("VSC_Scroll_Line_Backward(%p)"), (void *)form));
2101   returnCode(VSC_Generic(form, -1));
2102 }
2103
2104 /*---------------------------------------------------------------------------
2105 |   Facility      :  libnform
2106 |   Function      :  static int VSC_Scroll_Page_Forward(FORM * form)
2107 |
2108 |   Description   :  Scroll a multi-line field forward a page
2109 |
2110 |   Return Values :  E_OK              - success
2111 |                    E_REQUEST_DENIED  - no data ahead
2112 +--------------------------------------------------------------------------*/
2113 static int
2114 VSC_Scroll_Page_Forward(FORM *form)
2115 {
2116   T((T_CALLED("VSC_Scroll_Page_Forward(%p)"), (void *)form));
2117   returnCode(VSC_Generic(form, form->current->rows));
2118 }
2119
2120 /*---------------------------------------------------------------------------
2121 |   Facility      :  libnform
2122 |   Function      :  static int VSC_Scroll_Half_Page_Forward(FORM * form)
2123 |
2124 |   Description   :  Scroll a multi-line field forward half a page
2125 |
2126 |   Return Values :  E_OK              - success
2127 |                    E_REQUEST_DENIED  - no data ahead
2128 +--------------------------------------------------------------------------*/
2129 static int
2130 VSC_Scroll_Half_Page_Forward(FORM *form)
2131 {
2132   T((T_CALLED("VSC_Scroll_Half_Page_Forward(%p)"), (void *)form));
2133   returnCode(VSC_Generic(form, (form->current->rows + 1) / 2));
2134 }
2135
2136 /*---------------------------------------------------------------------------
2137 |   Facility      :  libnform
2138 |   Function      :  static int VSC_Scroll_Page_Backward(FORM * form)
2139 |
2140 |   Description   :  Scroll a multi-line field backward a page
2141 |
2142 |   Return Values :  E_OK              - success
2143 |                    E_REQUEST_DENIED  - no data behind
2144 +--------------------------------------------------------------------------*/
2145 static int
2146 VSC_Scroll_Page_Backward(FORM *form)
2147 {
2148   T((T_CALLED("VSC_Scroll_Page_Backward(%p)"), (void *)form));
2149   returnCode(VSC_Generic(form, -(form->current->rows)));
2150 }
2151
2152 /*---------------------------------------------------------------------------
2153 |   Facility      :  libnform
2154 |   Function      :  static int VSC_Scroll_Half_Page_Backward(FORM * form)
2155 |
2156 |   Description   :  Scroll a multi-line field backward half a page
2157 |
2158 |   Return Values :  E_OK              - success
2159 |                    E_REQUEST_DENIED  - no data behind
2160 +--------------------------------------------------------------------------*/
2161 static int
2162 VSC_Scroll_Half_Page_Backward(FORM *form)
2163 {
2164   T((T_CALLED("VSC_Scroll_Half_Page_Backward(%p)"), (void *)form));
2165   returnCode(VSC_Generic(form, -((form->current->rows + 1) / 2)));
2166 }
2167 /*----------------------------------------------------------------------------
2168   End of Vertical scrolling routines
2169   --------------------------------------------------------------------------*/
2170
2171 /*----------------------------------------------------------------------------
2172   Horizontal scrolling helper routines
2173   --------------------------------------------------------------------------*/
2174
2175 /*---------------------------------------------------------------------------
2176 |   Facility      :  libnform
2177 |   Function      :  static int HSC_Generic(FORM *form, int ncolumns)
2178 |
2179 |   Description   :  Scroll single-line field forward (ncolumns>0) or
2180 |                    backward (ncolumns<0) this many columns.
2181 |
2182 |   Return Values :  E_OK              - success
2183 |                    E_REQUEST_DENIED  - can't scroll
2184 +--------------------------------------------------------------------------*/
2185 static int
2186 HSC_Generic(FORM *form, int ncolumns)
2187 {
2188   FIELD *field = form->current;
2189   int res = E_REQUEST_DENIED;
2190   int cols_to_go = (ncolumns > 0 ? ncolumns : -ncolumns);
2191
2192   if (ncolumns > 0)
2193     {
2194       if ((cols_to_go + form->begincol) > (field->dcols - field->cols))
2195         cols_to_go = field->dcols - field->cols - form->begincol;
2196
2197       if (cols_to_go > 0)
2198         {
2199           form->curcol += cols_to_go;
2200           form->begincol += cols_to_go;
2201           res = E_OK;
2202         }
2203     }
2204   else
2205     {
2206       if (cols_to_go > form->begincol)
2207         cols_to_go = form->begincol;
2208
2209       if (cols_to_go > 0)
2210         {
2211           form->curcol -= cols_to_go;
2212           form->begincol -= cols_to_go;
2213           res = E_OK;
2214         }
2215     }
2216   return (res);
2217 }
2218 /*----------------------------------------------------------------------------
2219   End of Horizontal scrolling helper routines
2220   --------------------------------------------------------------------------*/
2221
2222 /*----------------------------------------------------------------------------
2223   Horizontal scrolling routines
2224   --------------------------------------------------------------------------*/
2225
2226 /*---------------------------------------------------------------------------
2227 |   Facility      :  libnform
2228 |   Function      :  static int Horizontal_Scrolling(
2229 |                                          int (* const fct) (FORM *),
2230 |                                          FORM * form)
2231 |
2232 |   Description   :  Performs the generic horizontal scrolling routines.
2233 |                    This has to check for a single-line field.
2234 |
2235 |   Return Values :  Propagated error code from low-level driver calls
2236 +--------------------------------------------------------------------------*/
2237 static int
2238 Horizontal_Scrolling(int (*const fct) (FORM *), FORM *form)
2239 {
2240   if (Single_Line_Field(form->current))
2241     return fct(form);
2242   else
2243     return (E_REQUEST_DENIED);
2244 }
2245
2246 /*---------------------------------------------------------------------------
2247 |   Facility      :  libnform
2248 |   Function      :  static int HSC_Scroll_Char_Forward(FORM * form)
2249 |
2250 |   Description   :  Scroll single-line field forward a character
2251 |
2252 |   Return Values :  E_OK                - success
2253 |                    E_REQUEST_DENIED    - no data ahead
2254 +--------------------------------------------------------------------------*/
2255 static int
2256 HSC_Scroll_Char_Forward(FORM *form)
2257 {
2258   T((T_CALLED("HSC_Scroll_Char_Forward(%p)"), (void *)form));
2259   returnCode(HSC_Generic(form, 1));
2260 }
2261
2262 /*---------------------------------------------------------------------------
2263 |   Facility      :  libnform
2264 |   Function      :  static int HSC_Scroll_Char_Backward(FORM * form)
2265 |
2266 |   Description   :  Scroll single-line field backward a character
2267 |
2268 |   Return Values :  E_OK                - success
2269 |                    E_REQUEST_DENIED    - no data behind
2270 +--------------------------------------------------------------------------*/
2271 static int
2272 HSC_Scroll_Char_Backward(FORM *form)
2273 {
2274   T((T_CALLED("HSC_Scroll_Char_Backward(%p)"), (void *)form));
2275   returnCode(HSC_Generic(form, -1));
2276 }
2277
2278 /*---------------------------------------------------------------------------
2279 |   Facility      :  libnform
2280 |   Function      :  static int HSC_Horizontal_Line_Forward(FORM* form)
2281 |
2282 |   Description   :  Scroll single-line field forward a line
2283 |
2284 |   Return Values :  E_OK                - success
2285 |                    E_REQUEST_DENIED    - no data ahead
2286 +--------------------------------------------------------------------------*/
2287 static int
2288 HSC_Horizontal_Line_Forward(FORM *form)
2289 {
2290   T((T_CALLED("HSC_Horizontal_Line_Forward(%p)"), (void *)form));
2291   returnCode(HSC_Generic(form, form->current->cols));
2292 }
2293
2294 /*---------------------------------------------------------------------------
2295 |   Facility      :  libnform
2296 |   Function      :  static int HSC_Horizontal_Half_Line_Forward(FORM* form)
2297 |
2298 |   Description   :  Scroll single-line field forward half a line
2299 |
2300 |   Return Values :  E_OK               - success
2301 |                    E_REQUEST_DENIED   - no data ahead
2302 +--------------------------------------------------------------------------*/
2303 static int
2304 HSC_Horizontal_Half_Line_Forward(FORM *form)
2305 {
2306   T((T_CALLED("HSC_Horizontal_Half_Line_Forward(%p)"), (void *)form));
2307   returnCode(HSC_Generic(form, (form->current->cols + 1) / 2));
2308 }
2309
2310 /*---------------------------------------------------------------------------
2311 |   Facility      :  libnform
2312 |   Function      :  static int HSC_Horizontal_Line_Backward(FORM* form)
2313 |
2314 |   Description   :  Scroll single-line field backward a line
2315 |
2316 |   Return Values :  E_OK                - success
2317 |                    E_REQUEST_DENIED    - no data behind
2318 +--------------------------------------------------------------------------*/
2319 static int
2320 HSC_Horizontal_Line_Backward(FORM *form)
2321 {
2322   T((T_CALLED("HSC_Horizontal_Line_Backward(%p)"), (void *)form));
2323   returnCode(HSC_Generic(form, -(form->current->cols)));
2324 }
2325
2326 /*---------------------------------------------------------------------------
2327 |   Facility      :  libnform
2328 |   Function      :  static int HSC_Horizontal_Half_Line_Backward(FORM* form)
2329 |
2330 |   Description   :  Scroll single-line field backward half a line
2331 |
2332 |   Return Values :  E_OK                - success
2333 |                    E_REQUEST_DENIED    - no data behind
2334 +--------------------------------------------------------------------------*/
2335 static int
2336 HSC_Horizontal_Half_Line_Backward(FORM *form)
2337 {
2338   T((T_CALLED("HSC_Horizontal_Half_Line_Backward(%p)"), (void *)form));
2339   returnCode(HSC_Generic(form, -((form->current->cols + 1) / 2)));
2340 }
2341
2342 /*----------------------------------------------------------------------------
2343   End of Horizontal scrolling routines
2344   --------------------------------------------------------------------------*/
2345
2346 /*----------------------------------------------------------------------------
2347   Helper routines for Field Editing
2348   --------------------------------------------------------------------------*/
2349
2350 /*---------------------------------------------------------------------------
2351 |   Facility      :  libnform
2352 |   Function      :  static bool Is_There_Room_For_A_Line(FORM * form)
2353 |
2354 |   Description   :  Check whether or not there is enough room in the
2355 |                    buffer to enter a whole line.
2356 |
2357 |   Return Values :  TRUE   - there is enough space
2358 |                    FALSE  - there is not enough space
2359 +--------------------------------------------------------------------------*/
2360 NCURSES_INLINE static bool
2361 Is_There_Room_For_A_Line(FORM *form)
2362 {
2363   FIELD *field = form->current;
2364   FIELD_CELL *begin_of_last_line, *s;
2365
2366   Synchronize_Buffer(form);
2367   begin_of_last_line = Address_Of_Row_In_Buffer(field, (field->drows - 1));
2368   s = After_End_Of_Data(begin_of_last_line, field->dcols);
2369   return ((s == begin_of_last_line) ? TRUE : FALSE);
2370 }
2371
2372 /*---------------------------------------------------------------------------
2373 |   Facility      :  libnform
2374 |   Function      :  static bool Is_There_Room_For_A_Char_In_Line(FORM * form)
2375 |
2376 |   Description   :  Checks whether or not there is room for a new character
2377 |                    in the current line.
2378 |
2379 |   Return Values :  TRUE    - there is room
2380 |                    FALSE   - there is not enough room (line full)
2381 +--------------------------------------------------------------------------*/
2382 NCURSES_INLINE static bool
2383 Is_There_Room_For_A_Char_In_Line(FORM *form)
2384 {
2385   int last_char_in_line;
2386
2387   wmove(form->w, form->currow, form->current->dcols - 1);
2388   last_char_in_line = (int)(winch(form->w) & A_CHARTEXT);
2389   wmove(form->w, form->currow, form->curcol);
2390   return (((last_char_in_line == form->current->pad) ||
2391            is_blank(last_char_in_line)) ? TRUE : FALSE);
2392 }
2393
2394 #define There_Is_No_Room_For_A_Char_In_Line(f) \
2395   !Is_There_Room_For_A_Char_In_Line(f)
2396
2397 /*---------------------------------------------------------------------------
2398 |   Facility      :  libnform
2399 |   Function      :  static int Insert_String(
2400 |                                             FORM * form,
2401 |                                             int row,
2402 |                                             char *txt,
2403 |                                             int  len )
2404 |
2405 |   Description   :  Insert the 'len' characters beginning at pointer 'txt'
2406 |                    into the 'row' of the 'form'. The insertion occurs
2407 |                    on the beginning of the row, all other characters are
2408 |                    moved to the right. After the text a pad character will
2409 |                    be inserted to separate the text from the rest. If
2410 |                    necessary the insertion moves characters on the next
2411 |                    line to make place for the requested insertion string.
2412 |
2413 |   Return Values :  E_OK              - success
2414 |                    E_REQUEST_DENIED  -
2415 |                    E_SYSTEM_ERROR    - system error
2416 +--------------------------------------------------------------------------*/
2417 static int
2418 Insert_String(FORM *form, int row, FIELD_CELL *txt, int len)
2419 {
2420   FIELD *field = form->current;
2421   FIELD_CELL *bp = Address_Of_Row_In_Buffer(field, row);
2422   int datalen = (int)(After_End_Of_Data(bp, field->dcols) - bp);
2423   int freelen = field->dcols - datalen;
2424   int requiredlen = len + 1;
2425   FIELD_CELL *split;
2426   int result = E_REQUEST_DENIED;
2427
2428   if (freelen >= requiredlen)
2429     {
2430       wmove(form->w, row, 0);
2431       myINSNSTR(form->w, txt, len);
2432       wmove(form->w, row, len);
2433       myINSNSTR(form->w, &myBLANK, 1);
2434       return E_OK;
2435     }
2436   else
2437     {
2438       /* we have to move characters on the next line. If we are on the
2439          last line this may work, if the field is growable */
2440       if ((row == (field->drows - 1)) && Growable(field))
2441         {
2442           if (!Field_Grown(field, 1))
2443             return (E_SYSTEM_ERROR);
2444           /* !!!Side-Effect : might be changed due to growth!!! */
2445           bp = Address_Of_Row_In_Buffer(field, row);
2446         }
2447
2448       if (row < (field->drows - 1))
2449         {
2450           split =
2451             After_Last_Whitespace_Character(bp,
2452                                             (int)(Get_Start_Of_Data(bp
2453                                                                     + field->dcols
2454                                                                     - requiredlen,
2455                                                                     requiredlen)
2456                                                   - bp));
2457           /* split points now to the first character of the portion of the
2458              line that must be moved to the next line */
2459           datalen = (int)(split - bp);  /* + freelen has to stay on this line   */
2460           freelen = field->dcols - (datalen + freelen);         /* for the next line */
2461
2462           if ((result = Insert_String(form, row + 1, split, freelen)) == E_OK)
2463             {
2464               wmove(form->w, row, datalen);
2465               wclrtoeol(form->w);
2466               wmove(form->w, row, 0);
2467               myINSNSTR(form->w, txt, len);
2468               wmove(form->w, row, len);
2469               myINSNSTR(form->w, &myBLANK, 1);
2470               return E_OK;
2471             }
2472         }
2473       return (result);
2474     }
2475 }
2476
2477 /*---------------------------------------------------------------------------
2478 |   Facility      :  libnform
2479 |   Function      :  static int Wrapping_Not_Necessary_Or_Wrapping_Ok(
2480 |                                             FORM * form)
2481 |
2482 |   Description   :  If a character has been entered into a field, it may
2483 |                    be that wrapping has to occur. This routine checks
2484 |                    whether or not wrapping is required and if so, performs
2485 |                    the wrapping.
2486 |
2487 |   Return Values :  E_OK              - no wrapping required or wrapping
2488 |                                        was successful
2489 |                    E_REQUEST_DENIED  -
2490 |                    E_SYSTEM_ERROR    - some system error
2491 +--------------------------------------------------------------------------*/
2492 static int
2493 Wrapping_Not_Necessary_Or_Wrapping_Ok(FORM *form)
2494 {
2495   FIELD *field = form->current;
2496   int result = E_REQUEST_DENIED;
2497   bool Last_Row = ((field->drows - 1) == form->currow);
2498
2499   if ((Field_Has_Option(field, O_WRAP)) &&      /* wrapping wanted     */
2500       (!Single_Line_Field(field)) &&    /* must be multi-line  */
2501       (There_Is_No_Room_For_A_Char_In_Line(form)) &&    /* line is full        */
2502       (!Last_Row || Growable(field)))   /* there are more lines */
2503     {
2504       FIELD_CELL *bp;
2505       FIELD_CELL *split;
2506       int chars_to_be_wrapped;
2507       int chars_to_remain_on_line;
2508
2509       if (Last_Row)
2510         {
2511           /* the above logic already ensures, that in this case the field
2512              is growable */
2513           if (!Field_Grown(field, 1))
2514             return E_SYSTEM_ERROR;
2515         }
2516       bp = Address_Of_Current_Row_In_Buffer(form);
2517       Window_To_Buffer(form, field);
2518       split = After_Last_Whitespace_Character(bp, field->dcols);
2519       /* split points to the first character of the sequence to be brought
2520          on the next line */
2521       chars_to_remain_on_line = (int)(split - bp);
2522       chars_to_be_wrapped = field->dcols - chars_to_remain_on_line;
2523       if (chars_to_remain_on_line > 0)
2524         {
2525           if ((result = Insert_String(form, form->currow + 1, split,
2526                                       chars_to_be_wrapped)) == E_OK)
2527             {
2528               wmove(form->w, form->currow, chars_to_remain_on_line);
2529               wclrtoeol(form->w);
2530               if (form->curcol >= chars_to_remain_on_line)
2531                 {
2532                   form->currow++;
2533                   form->curcol -= chars_to_remain_on_line;
2534                 }
2535               return E_OK;
2536             }
2537         }
2538       else
2539         return E_OK;
2540       if (result != E_OK)
2541         {
2542           DeleteChar(form);
2543           Window_To_Buffer(form, field);
2544           result = E_REQUEST_DENIED;
2545         }
2546     }
2547   else
2548     result = E_OK;              /* wrapping was not necessary */
2549   return (result);
2550 }
2551
2552 /*----------------------------------------------------------------------------
2553   Field Editing routines
2554   --------------------------------------------------------------------------*/
2555
2556 /*---------------------------------------------------------------------------
2557 |   Facility      :  libnform
2558 |   Function      :  static int Field_Editing(
2559 |                                    int (* const fct) (FORM *),
2560 |                                    FORM * form)
2561 |
2562 |   Description   :  Generic routine for field editing requests. The driver
2563 |                    routines are only called for editable fields, the
2564 |                    _WINDOW_MODIFIED flag is set if editing occurred.
2565 |                    This is somewhat special due to the overload semantics
2566 |                    of the NEW_LINE and DEL_PREV requests.
2567 |
2568 |   Return Values :  Error code from low level drivers.
2569 +--------------------------------------------------------------------------*/
2570 static int
2571 Field_Editing(int (*const fct) (FORM *), FORM *form)
2572 {
2573   int res = E_REQUEST_DENIED;
2574
2575   /* We have to deal here with the specific case of the overloaded
2576      behavior of New_Line and Delete_Previous requests.
2577      They may end up in navigational requests if we are on the first
2578      character in a field. But navigation is also allowed on non-
2579      editable fields.
2580    */
2581   if ((fct == FE_Delete_Previous) &&
2582       ((unsigned)form->opts & O_BS_OVERLOAD) &&
2583       First_Position_In_Current_Field(form))
2584     {
2585       res = Inter_Field_Navigation(FN_Previous_Field, form);
2586     }
2587   else
2588     {
2589       if (fct == FE_New_Line)
2590         {
2591           if (((unsigned)form->opts & O_NL_OVERLOAD) &&
2592               First_Position_In_Current_Field(form))
2593             {
2594               res = Inter_Field_Navigation(FN_Next_Field, form);
2595             }
2596           else
2597             /* FE_New_Line deals itself with the _WINDOW_MODIFIED flag */
2598             res = fct(form);
2599         }
2600       else
2601         {
2602           /* From now on, everything must be editable */
2603           if ((unsigned)form->current->opts & O_EDIT)
2604             {
2605               res = fct(form);
2606               if (res == E_OK)
2607                 SetStatus(form, _WINDOW_MODIFIED);
2608             }
2609         }
2610     }
2611   return res;
2612 }
2613
2614 /*---------------------------------------------------------------------------
2615 |   Facility      :  libnform
2616 |   Function      :  static int FE_New_Line(FORM * form)
2617 |
2618 |   Description   :  Perform a new line request. This is rather complex
2619 |                    compared to other routines in this code due to the
2620 |                    rather difficult to understand description in the
2621 |                    manuals.
2622 |
2623 |   Return Values :  E_OK               - success
2624 |                    E_REQUEST_DENIED   - new line not allowed
2625 |                    E_SYSTEM_ERROR     - system error
2626 +--------------------------------------------------------------------------*/
2627 static int
2628 FE_New_Line(FORM *form)
2629 {
2630   FIELD *field = form->current;
2631   FIELD_CELL *bp, *t;
2632   bool Last_Row = ((field->drows - 1) == form->currow);
2633
2634   T((T_CALLED("FE_New_Line(%p)"), (void *)form));
2635   if (form->status & _OVLMODE)
2636     {
2637       if (Last_Row &&
2638           (!(Growable(field) && !Single_Line_Field(field))))
2639         {
2640           if (!((unsigned)form->opts & O_NL_OVERLOAD))
2641             returnCode(E_REQUEST_DENIED);
2642           wmove(form->w, form->currow, form->curcol);
2643           wclrtoeol(form->w);
2644           /* we have to set this here, although it is also
2645              handled in the generic routine. The reason is,
2646              that FN_Next_Field may fail, but the form is
2647              definitively changed */
2648           SetStatus(form, _WINDOW_MODIFIED);
2649           returnCode(Inter_Field_Navigation(FN_Next_Field, form));
2650         }
2651       else
2652         {
2653           if (Last_Row && !Field_Grown(field, 1))
2654             {
2655               /* N.B.: due to the logic in the 'if', LastRow==TRUE
2656                  means here that the field is growable and not
2657                  a single-line field */
2658               returnCode(E_SYSTEM_ERROR);
2659             }
2660           wmove(form->w, form->currow, form->curcol);
2661           wclrtoeol(form->w);
2662           form->currow++;
2663           form->curcol = 0;
2664           SetStatus(form, _WINDOW_MODIFIED);
2665           returnCode(E_OK);
2666         }
2667     }
2668   else
2669     {
2670       /* Insert Mode */
2671       if (Last_Row &&
2672           !(Growable(field) && !Single_Line_Field(field)))
2673         {
2674           if (!((unsigned)form->opts & O_NL_OVERLOAD))
2675             returnCode(E_REQUEST_DENIED);
2676           returnCode(Inter_Field_Navigation(FN_Next_Field, form));
2677         }
2678       else
2679         {
2680           bool May_Do_It = !Last_Row && Is_There_Room_For_A_Line(form);
2681
2682           if (!(May_Do_It || Growable(field)))
2683             returnCode(E_REQUEST_DENIED);
2684           if (!May_Do_It && !Field_Grown(field, 1))
2685             returnCode(E_SYSTEM_ERROR);
2686
2687           bp = Address_Of_Current_Position_In_Buffer(form);
2688           t = After_End_Of_Data(bp, field->dcols - form->curcol);
2689           wmove(form->w, form->currow, form->curcol);
2690           wclrtoeol(form->w);
2691           form->currow++;
2692           form->curcol = 0;
2693           wmove(form->w, form->currow, form->curcol);
2694           winsertln(form->w);
2695           myADDNSTR(form->w, bp, (int)(t - bp));
2696           SetStatus(form, _WINDOW_MODIFIED);
2697           returnCode(E_OK);
2698         }
2699     }
2700 }
2701
2702 /*---------------------------------------------------------------------------
2703 |   Facility      :  libnform
2704 |   Function      :  static int FE_Insert_Character(FORM * form)
2705 |
2706 |   Description   :  Insert blank character at the cursor position
2707 |
2708 |   Return Values :  E_OK
2709 |                    E_REQUEST_DENIED
2710 +--------------------------------------------------------------------------*/
2711 static int
2712 FE_Insert_Character(FORM *form)
2713 {
2714   FIELD *field = form->current;
2715   int result = E_REQUEST_DENIED;
2716
2717   T((T_CALLED("FE_Insert_Character(%p)"), (void *)form));
2718   if (Check_Char(form, field, field->type, (int)C_BLANK,
2719                  (TypeArgument *)(field->arg)))
2720     {
2721       bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);
2722
2723       if (There_Is_Room ||
2724           ((Single_Line_Field(field) && Growable(field))))
2725         {
2726           if (!There_Is_Room && !Field_Grown(field, 1))
2727             result = E_SYSTEM_ERROR;
2728           else
2729             {
2730               winsch(form->w, (chtype)C_BLANK);
2731               result = Wrapping_Not_Necessary_Or_Wrapping_Ok(form);
2732             }
2733         }
2734     }
2735   returnCode(result);
2736 }
2737
2738 /*---------------------------------------------------------------------------
2739 |   Facility      :  libnform
2740 |   Function      :  static int FE_Insert_Line(FORM * form)
2741 |
2742 |   Description   :  Insert a blank line at the cursor position
2743 |
2744 |   Return Values :  E_OK               - success
2745 |                    E_REQUEST_DENIED   - line can not be inserted
2746 +--------------------------------------------------------------------------*/
2747 static int
2748 FE_Insert_Line(FORM *form)
2749 {
2750   FIELD *field = form->current;
2751   int result = E_REQUEST_DENIED;
2752
2753   T((T_CALLED("FE_Insert_Line(%p)"), (void *)form));
2754   if (Check_Char(form, field,
2755                  field->type, (int)C_BLANK, (TypeArgument *)(field->arg)))
2756     {
2757       bool Maybe_Done = (form->currow != (field->drows - 1)) &&
2758       Is_There_Room_For_A_Line(form);
2759
2760       if (!Single_Line_Field(field) &&
2761           (Maybe_Done || Growable(field)))
2762         {
2763           if (!Maybe_Done && !Field_Grown(field, 1))
2764             result = E_SYSTEM_ERROR;
2765           else
2766             {
2767               form->curcol = 0;
2768               winsertln(form->w);
2769               result = E_OK;
2770             }
2771         }
2772     }
2773   returnCode(result);
2774 }
2775
2776 /*---------------------------------------------------------------------------
2777 |   Facility      :  libnform
2778 |   Function      :  static int FE_Delete_Character(FORM * form)
2779 |
2780 |   Description   :  Delete character at the cursor position
2781 |
2782 |   Return Values :  E_OK    - success
2783 +--------------------------------------------------------------------------*/
2784 static int
2785 FE_Delete_Character(FORM *form)
2786 {
2787   T((T_CALLED("FE_Delete_Character(%p)"), (void *)form));
2788   DeleteChar(form);
2789   returnCode(E_OK);
2790 }
2791
2792 /*---------------------------------------------------------------------------
2793 |   Facility      :  libnform
2794 |   Function      :  static int FE_Delete_Previous(FORM * form)
2795 |
2796 |   Description   :  Delete character before cursor. Again this is a rather
2797 |                    difficult piece compared to others due to the overloading
2798 |                    semantics of backspace.
2799 |                    N.B.: The case of overloaded BS on first field position
2800 |                          is already handled in the generic routine.
2801 |
2802 |   Return Values :  E_OK                - success
2803 |                    E_REQUEST_DENIED    - Character can't be deleted
2804 +--------------------------------------------------------------------------*/
2805 static int
2806 FE_Delete_Previous(FORM *form)
2807 {
2808   FIELD *field = form->current;
2809
2810   T((T_CALLED("FE_Delete_Previous(%p)"), (void *)form));
2811   if (First_Position_In_Current_Field(form))
2812     returnCode(E_REQUEST_DENIED);
2813
2814   if ((--(form->curcol)) < 0)
2815     {
2816       FIELD_CELL *this_line, *prev_line, *prev_end, *this_end;
2817       int this_row = form->currow;
2818
2819       form->curcol++;
2820       if (form->status & _OVLMODE)
2821         returnCode(E_REQUEST_DENIED);
2822
2823       prev_line = Address_Of_Row_In_Buffer(field, (form->currow - 1));
2824       this_line = Address_Of_Row_In_Buffer(field, (form->currow));
2825       Synchronize_Buffer(form);
2826       prev_end = After_End_Of_Data(prev_line, field->dcols);
2827       this_end = After_End_Of_Data(this_line, field->dcols);
2828       if ((int)(this_end - this_line) >
2829           (field->cols - (int)(prev_end - prev_line)))
2830         returnCode(E_REQUEST_DENIED);
2831       wmove(form->w, form->currow, form->curcol);
2832       wdeleteln(form->w);
2833       Adjust_Cursor_Position(form, prev_end);
2834       /*
2835        * If we did not really move to the previous line, help the user a
2836        * little.  It is however a little inconsistent.  Normally, when
2837        * backspacing around the point where text wraps to a new line in a
2838        * multi-line form, we absorb one keystroke for the wrapping point.  That
2839        * is consistent with SVr4 forms.  However, SVr4 does not allow typing
2840        * into the last column of the field, and requires the user to enter a
2841        * newline to move to the next line.  Therefore it can consistently eat
2842        * that keystroke.  Since ncurses allows the last column, it wraps
2843        * automatically (given the proper options).  But we cannot eat the
2844        * keystroke to back over the wrapping point, since that would put the
2845        * cursor past the end of the form field.  In this case, just delete the
2846        * character at the end of the field.
2847        */
2848       if (form->currow == this_row && this_row > 0)
2849         {
2850           form->currow -= 1;
2851           form->curcol = field->dcols - 1;
2852           DeleteChar(form);
2853         }
2854       else
2855         {
2856           wmove(form->w, form->currow, form->curcol);
2857           myADDNSTR(form->w, this_line, (int)(this_end - this_line));
2858         }
2859     }
2860   else
2861     {
2862       DeleteChar(form);
2863     }
2864   returnCode(E_OK);
2865 }
2866
2867 /*---------------------------------------------------------------------------
2868 |   Facility      :  libnform
2869 |   Function      :  static int FE_Delete_Line(FORM * form)
2870 |
2871 |   Description   :  Delete line at cursor position.
2872 |
2873 |   Return Values :  E_OK  - success
2874 +--------------------------------------------------------------------------*/
2875 static int
2876 FE_Delete_Line(FORM *form)
2877 {
2878   T((T_CALLED("FE_Delete_Line(%p)"), (void *)form));
2879   form->curcol = 0;
2880   wdeleteln(form->w);
2881   returnCode(E_OK);
2882 }
2883
2884 /*---------------------------------------------------------------------------
2885 |   Facility      :  libnform
2886 |   Function      :  static int FE_Delete_Word(FORM * form)
2887 |
2888 |   Description   :  Delete word at cursor position
2889 |
2890 |   Return Values :  E_OK               - success
2891 |                    E_REQUEST_DENIED   - failure
2892 +--------------------------------------------------------------------------*/
2893 static int
2894 FE_Delete_Word(FORM *form)
2895 {
2896   FIELD *field = form->current;
2897   FIELD_CELL *bp = Address_Of_Current_Row_In_Buffer(form);
2898   FIELD_CELL *ep = bp + field->dcols;
2899   FIELD_CELL *cp = bp + form->curcol;
2900   FIELD_CELL *s;
2901
2902   T((T_CALLED("FE_Delete_Word(%p)"), (void *)form));
2903   Synchronize_Buffer(form);
2904   if (ISBLANK(*cp))
2905     returnCode(E_REQUEST_DENIED);       /* not in word */
2906
2907   /* move cursor to begin of word and erase to end of screen-line */
2908   Adjust_Cursor_Position(form,
2909                          After_Last_Whitespace_Character(bp, form->curcol));
2910   wmove(form->w, form->currow, form->curcol);
2911   wclrtoeol(form->w);
2912
2913   /* skip over word in buffer */
2914   s = Get_First_Whitespace_Character(cp, (int)(ep - cp));
2915   /* to begin of next word    */
2916   s = Get_Start_Of_Data(s, (int)(ep - s));
2917   if ((s != cp) && !ISBLANK(*s))
2918     {
2919       /* copy remaining line to window */
2920       myADDNSTR(form->w, s, (int)(s - After_End_Of_Data(s, (int)(ep - s))));
2921     }
2922   returnCode(E_OK);
2923 }
2924
2925 /*---------------------------------------------------------------------------
2926 |   Facility      :  libnform
2927 |   Function      :  static int FE_Clear_To_End_Of_Line(FORM * form)
2928 |
2929 |   Description   :  Clear to end of current line.
2930 |
2931 |   Return Values :  E_OK   - success
2932 +--------------------------------------------------------------------------*/
2933 static int
2934 FE_Clear_To_End_Of_Line(FORM *form)
2935 {
2936   T((T_CALLED("FE_Clear_To_End_Of_Line(%p)"), (void *)form));
2937   wmove(form->w, form->currow, form->curcol);
2938   wclrtoeol(form->w);
2939   returnCode(E_OK);
2940 }
2941
2942 /*---------------------------------------------------------------------------
2943 |   Facility      :  libnform
2944 |   Function      :  static int FE_Clear_To_End_Of_Field(FORM * form)
2945 |
2946 |   Description   :  Clear to end of field.
2947 |
2948 |   Return Values :  E_OK   - success
2949 +--------------------------------------------------------------------------*/
2950 static int
2951 FE_Clear_To_End_Of_Field(FORM *form)
2952 {
2953   T((T_CALLED("FE_Clear_To_End_Of_Field(%p)"), (void *)form));
2954   wmove(form->w, form->currow, form->curcol);
2955   wclrtobot(form->w);
2956   returnCode(E_OK);
2957 }
2958
2959 /*---------------------------------------------------------------------------
2960 |   Facility      :  libnform
2961 |   Function      :  static int FE_Clear_Field(FORM * form)
2962 |
2963 |   Description   :  Clear entire field.
2964 |
2965 |   Return Values :  E_OK   - success
2966 +--------------------------------------------------------------------------*/
2967 static int
2968 FE_Clear_Field(FORM *form)
2969 {
2970   T((T_CALLED("FE_Clear_Field(%p)"), (void *)form));
2971   form->currow = form->curcol = 0;
2972   werase(form->w);
2973   returnCode(E_OK);
2974 }
2975 /*----------------------------------------------------------------------------
2976   END of Field Editing routines
2977   --------------------------------------------------------------------------*/
2978
2979 /*----------------------------------------------------------------------------
2980   Edit Mode routines
2981   --------------------------------------------------------------------------*/
2982
2983 /*---------------------------------------------------------------------------
2984 |   Facility      :  libnform
2985 |   Function      :  static int EM_Overlay_Mode(FORM * form)
2986 |
2987 |   Description   :  Switch to overlay mode.
2988 |
2989 |   Return Values :  E_OK   - success
2990 +--------------------------------------------------------------------------*/
2991 static int
2992 EM_Overlay_Mode(FORM *form)
2993 {
2994   T((T_CALLED("EM_Overlay_Mode(%p)"), (void *)form));
2995   SetStatus(form, _OVLMODE);
2996   returnCode(E_OK);
2997 }
2998
2999 /*---------------------------------------------------------------------------
3000 |   Facility      :  libnform
3001 |   Function      :  static int EM_Insert_Mode(FORM * form)
3002 |
3003 |   Description   :  Switch to insert mode
3004 |
3005 |   Return Values :  E_OK   - success
3006 +--------------------------------------------------------------------------*/
3007 static int
3008 EM_Insert_Mode(FORM *form)
3009 {
3010   T((T_CALLED("EM_Insert_Mode(%p)"), (void *)form));
3011   ClrStatus(form, _OVLMODE);
3012   returnCode(E_OK);
3013 }
3014
3015 /*----------------------------------------------------------------------------
3016   END of Edit Mode routines
3017   --------------------------------------------------------------------------*/
3018
3019 /*----------------------------------------------------------------------------
3020   Helper routines for Choice Requests
3021   --------------------------------------------------------------------------*/
3022
3023 /*---------------------------------------------------------------------------
3024 |   Facility      :  libnform
3025 |   Function      :  static bool Next_Choice(FORM * form,
3026 |                                            FIELDTYPE * typ,
3027 |                                            FIELD * field,
3028 |                                            TypeArgument *argp)
3029 |
3030 |   Description   :  Get the next field choice. For linked types this is
3031 |                    done recursively.
3032 |
3033 |   Return Values :  TRUE    - next choice successfully retrieved
3034 |                    FALSE   - couldn't retrieve next choice
3035 +--------------------------------------------------------------------------*/
3036 static bool
3037 Next_Choice(FORM *form, FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
3038 {
3039   if (!typ || !(typ->status & _HAS_CHOICE))
3040     return FALSE;
3041
3042   if (typ->status & _LINKED_TYPE)
3043     {
3044       assert(argp);
3045       return (
3046                Next_Choice(form, typ->left, field, argp->left) ||
3047                Next_Choice(form, typ->right, field, argp->right));
3048     }
3049   else
3050     {
3051 #if NCURSES_INTEROP_FUNCS
3052       assert(typ->enum_next.onext);
3053       if (typ->status & _GENERIC)
3054         return typ->enum_next.gnext(form, field, (void *)argp);
3055       else
3056         return typ->enum_next.onext(field, (void *)argp);
3057 #else
3058       assert(typ->next);
3059       return typ->next(field, (void *)argp);
3060 #endif
3061     }
3062 }
3063
3064 /*---------------------------------------------------------------------------
3065 |   Facility      :  libnform
3066 |   Function      :  static bool Previous_Choice(FORM * form,
3067 |                                                FIELDTYPE * typ,
3068 |                                                FIELD * field,
3069 |                                                TypeArgument *argp)
3070 |
3071 |   Description   :  Get the previous field choice. For linked types this
3072 |                    is done recursively.
3073 |
3074 |   Return Values :  TRUE    - previous choice successfully retrieved
3075 |                    FALSE   - couldn't retrieve previous choice
3076 +--------------------------------------------------------------------------*/
3077 static bool
3078 Previous_Choice(FORM *form, FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
3079 {
3080   if (!typ || !(typ->status & _HAS_CHOICE))
3081     return FALSE;
3082
3083   if (typ->status & _LINKED_TYPE)
3084     {
3085       assert(argp);
3086       return (
3087                Previous_Choice(form, typ->left, field, argp->left) ||
3088                Previous_Choice(form, typ->right, field, argp->right));
3089     }
3090   else
3091     {
3092 #if NCURSES_INTEROP_FUNCS
3093       assert(typ->enum_prev.oprev);
3094       if (typ->status & _GENERIC)
3095         return typ->enum_prev.gprev(form, field, (void *)argp);
3096       else
3097         return typ->enum_prev.oprev(field, (void *)argp);
3098 #else
3099       assert(typ->prev);
3100       return typ->prev(field, (void *)argp);
3101 #endif
3102     }
3103 }
3104 /*----------------------------------------------------------------------------
3105   End of Helper routines for Choice Requests
3106   --------------------------------------------------------------------------*/
3107
3108 /*----------------------------------------------------------------------------
3109   Routines for Choice Requests
3110   --------------------------------------------------------------------------*/
3111
3112 /*---------------------------------------------------------------------------
3113 |   Facility      :  libnform
3114 |   Function      :  static int CR_Next_Choice(FORM * form)
3115 |
3116 |   Description   :  Get the next field choice.
3117 |
3118 |   Return Values :  E_OK              - success
3119 |                    E_REQUEST_DENIED  - next choice couldn't be retrieved
3120 +--------------------------------------------------------------------------*/
3121 static int
3122 CR_Next_Choice(FORM *form)
3123 {
3124   FIELD *field = form->current;
3125
3126   T((T_CALLED("CR_Next_Choice(%p)"), (void *)form));
3127   Synchronize_Buffer(form);
3128   returnCode((Next_Choice(form, field->type, field, (TypeArgument *)(field->arg)))
3129              ? E_OK
3130              : E_REQUEST_DENIED);
3131 }
3132
3133 /*---------------------------------------------------------------------------
3134 |   Facility      :  libnform
3135 |   Function      :  static int CR_Previous_Choice(FORM * form)
3136 |
3137 |   Description   :  Get the previous field choice.
3138 |
3139 |   Return Values :  E_OK              - success
3140 |                    E_REQUEST_DENIED  - prev. choice couldn't be retrieved
3141 +--------------------------------------------------------------------------*/
3142 static int
3143 CR_Previous_Choice(FORM *form)
3144 {
3145   FIELD *field = form->current;
3146
3147   T((T_CALLED("CR_Previous_Choice(%p)"), (void *)form));
3148   Synchronize_Buffer(form);
3149   returnCode((Previous_Choice(form, field->type, field, (TypeArgument *)(field->arg)))
3150              ? E_OK
3151              : E_REQUEST_DENIED);
3152 }
3153 /*----------------------------------------------------------------------------
3154   End of Routines for Choice Requests
3155   --------------------------------------------------------------------------*/
3156
3157 /*----------------------------------------------------------------------------
3158   Helper routines for Field Validations.
3159   --------------------------------------------------------------------------*/
3160
3161 /*---------------------------------------------------------------------------
3162 |   Facility      :  libnform
3163 |   Function      :  static bool Check_Field(FORM* form,
3164 |                                            FIELDTYPE * typ,
3165 |                                            FIELD * field,
3166 |                                            TypeArgument * argp)
3167 |
3168 |   Description   :  Check the field according to its fieldtype and its
3169 |                    actual arguments. For linked fieldtypes this is done
3170 |                    recursively.
3171 |
3172 |   Return Values :  TRUE       - field is valid
3173 |                    FALSE      - field is invalid.
3174 +--------------------------------------------------------------------------*/
3175 static bool
3176 Check_Field(FORM *form, FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
3177 {
3178   if (typ)
3179     {
3180       if (Field_Has_Option(field, O_NULLOK))
3181         {
3182           FIELD_CELL *bp = field->buf;
3183
3184           assert(bp);
3185           while (ISBLANK(*bp))
3186             {
3187               bp++;
3188             }
3189           if (CharOf(*bp) == 0)
3190             return TRUE;
3191         }
3192
3193       if (typ->status & _LINKED_TYPE)
3194         {
3195           assert(argp);
3196           return (
3197                    Check_Field(form, typ->left, field, argp->left) ||
3198                    Check_Field(form, typ->right, field, argp->right));
3199         }
3200       else
3201         {
3202 #if NCURSES_INTEROP_FUNCS
3203           if (typ->fieldcheck.ofcheck)
3204             {
3205               if (typ->status & _GENERIC)
3206                 return typ->fieldcheck.gfcheck(form, field, (void *)argp);
3207               else
3208                 return typ->fieldcheck.ofcheck(field, (void *)argp);
3209             }
3210 #else
3211           if (typ->fcheck)
3212             return typ->fcheck(field, (void *)argp);
3213 #endif
3214         }
3215     }
3216   return TRUE;
3217 }
3218
3219 /*---------------------------------------------------------------------------
3220 |   Facility      :  libnform
3221 |   Function      :  bool _nc_Internal_Validation(FORM * form )
3222 |
3223 |   Description   :  Validate the current field of the form.
3224 |
3225 |   Return Values :  TRUE  - field is valid
3226 |                    FALSE - field is invalid
3227 +--------------------------------------------------------------------------*/
3228 FORM_EXPORT(bool)
3229 _nc_Internal_Validation(FORM *form)
3230 {
3231   FIELD *field;
3232
3233   field = form->current;
3234
3235   Synchronize_Buffer(form);
3236   if ((form->status & _FCHECK_REQUIRED) ||
3237       (!(Field_Has_Option(field, O_PASSOK))))
3238     {
3239       if (!Check_Field(form, field->type, field, (TypeArgument *)(field->arg)))
3240         return FALSE;
3241       ClrStatus(form, _FCHECK_REQUIRED);
3242       SetStatus(field, _CHANGED);
3243       Synchronize_Linked_Fields(field);
3244     }
3245   return TRUE;
3246 }
3247 /*----------------------------------------------------------------------------
3248   End of Helper routines for Field Validations.
3249   --------------------------------------------------------------------------*/
3250
3251 /*----------------------------------------------------------------------------
3252   Routines for Field Validation.
3253   --------------------------------------------------------------------------*/
3254
3255 /*---------------------------------------------------------------------------
3256 |   Facility      :  libnform
3257 |   Function      :  static int FV_Validation(FORM * form)
3258 |
3259 |   Description   :  Validate the current field of the form.
3260 |
3261 |   Return Values :  E_OK             - field valid
3262 |                    E_INVALID_FIELD  - field not valid
3263 +--------------------------------------------------------------------------*/
3264 static int
3265 FV_Validation(FORM *form)
3266 {
3267   T((T_CALLED("FV_Validation(%p)"), (void *)form));
3268   if (_nc_Internal_Validation(form))
3269     returnCode(E_OK);
3270   else
3271     returnCode(E_INVALID_FIELD);
3272 }
3273 /*----------------------------------------------------------------------------
3274   End of routines for Field Validation.
3275   --------------------------------------------------------------------------*/
3276
3277 /*----------------------------------------------------------------------------
3278   Helper routines for Inter-Field Navigation
3279   --------------------------------------------------------------------------*/
3280
3281 /*---------------------------------------------------------------------------
3282 |   Facility      :  libnform
3283 |   Function      :  static FIELD *Next_Field_On_Page(FIELD * field)
3284 |
3285 |   Description   :  Get the next field after the given field on the current
3286 |                    page. The order of fields is the one defined by the
3287 |                    field's array. Only visible and active fields are
3288 |                    counted.
3289 |
3290 |   Return Values :  Pointer to the next field.
3291 +--------------------------------------------------------------------------*/
3292 NCURSES_INLINE static FIELD *
3293 Next_Field_On_Page(FIELD *field)
3294 {
3295   FORM *form = field->form;
3296   FIELD **field_on_page = &form->field[field->index];
3297   FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
3298   FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3299
3300   do
3301     {
3302       field_on_page =
3303         (field_on_page == last_on_page) ? first_on_page : field_on_page + 1;
3304       if (Field_Is_Selectable(*field_on_page))
3305         break;
3306     }
3307   while (field != (*field_on_page));
3308   return (*field_on_page);
3309 }
3310
3311 /*---------------------------------------------------------------------------
3312 |   Facility      :  libnform
3313 |   Function      :  FIELD* _nc_First_Active_Field(FORM * form)
3314 |
3315 |   Description   :  Get the first active field on the current page,
3316 |                    if there are such. If there are none, get the first
3317 |                    visible field on the page. If there are also none,
3318 |                    we return the first field on page and hope the best.
3319 |
3320 |   Return Values :  Pointer to calculated field.
3321 +--------------------------------------------------------------------------*/
3322 FORM_EXPORT(FIELD *)
3323 _nc_First_Active_Field(FORM *form)
3324 {
3325   FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3326   FIELD *proposed = Next_Field_On_Page(*last_on_page);
3327
3328   if (proposed == *last_on_page)
3329     {
3330       /* there might be the special situation, where there is no
3331          active and visible field on the current page. We then select
3332          the first visible field on this readonly page
3333        */
3334       if (Field_Is_Not_Selectable(proposed))
3335         {
3336           FIELD **field = &form->field[proposed->index];
3337           FIELD **first = &form->field[form->page[form->curpage].pmin];
3338
3339           do
3340             {
3341               field = (field == last_on_page) ? first : field + 1;
3342               if (Field_Has_Option(*field, O_VISIBLE))
3343                 break;
3344             }
3345           while (proposed != (*field));
3346
3347           proposed = *field;
3348
3349           if ((proposed == *last_on_page) &&
3350               !((unsigned)proposed->opts & O_VISIBLE))
3351             {
3352               /* This means, there is also no visible field on the page.
3353                  So we propose the first one and hope the very best...
3354                  Some very clever user has designed a readonly and invisible
3355                  page on this form.
3356                */
3357               proposed = *first;
3358             }
3359         }
3360     }
3361   return (proposed);
3362 }
3363
3364 /*---------------------------------------------------------------------------
3365 |   Facility      :  libnform
3366 |   Function      :  static FIELD *Previous_Field_On_Page(FIELD * field)
3367 |
3368 |   Description   :  Get the previous field before the given field on the
3369 |                    current page. The order of fields is the one defined by
3370 |                    the field's array. Only visible and active fields are
3371 |                    counted.
3372 |
3373 |   Return Values :  Pointer to the previous field.
3374 +--------------------------------------------------------------------------*/
3375 NCURSES_INLINE static FIELD *
3376 Previous_Field_On_Page(FIELD *field)
3377 {
3378   FORM *form = field->form;
3379   FIELD **field_on_page = &form->field[field->index];
3380   FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
3381   FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3382
3383   do
3384     {
3385       field_on_page =
3386         (field_on_page == first_on_page) ? last_on_page : field_on_page - 1;
3387       if (Field_Is_Selectable(*field_on_page))
3388         break;
3389     }
3390   while (field != (*field_on_page));
3391
3392   return (*field_on_page);
3393 }
3394
3395 /*---------------------------------------------------------------------------
3396 |   Facility      :  libnform
3397 |   Function      :  static FIELD *Sorted_Next_Field(FIELD * field)
3398 |
3399 |   Description   :  Get the next field after the given field on the current
3400 |                    page. The order of fields is the one defined by the
3401 |                    (row,column) geometry, rows are major.
3402 |
3403 |   Return Values :  Pointer to the next field.
3404 +--------------------------------------------------------------------------*/
3405 NCURSES_INLINE static FIELD *
3406 Sorted_Next_Field(FIELD *field)
3407 {
3408   FIELD *field_on_page = field;
3409
3410   do
3411     {
3412       field_on_page = field_on_page->snext;
3413       if (Field_Is_Selectable(field_on_page))
3414         break;
3415     }
3416   while (field_on_page != field);
3417
3418   return (field_on_page);
3419 }
3420
3421 /*---------------------------------------------------------------------------
3422 |   Facility      :  libnform
3423 |   Function      :  static FIELD *Sorted_Previous_Field(FIELD * field)
3424 |
3425 |   Description   :  Get the previous field before the given field on the
3426 |                    current page. The order of fields is the one defined
3427 |                    by the (row,column) geometry, rows are major.
3428 |
3429 |   Return Values :  Pointer to the previous field.
3430 +--------------------------------------------------------------------------*/
3431 NCURSES_INLINE static FIELD *
3432 Sorted_Previous_Field(FIELD *field)
3433 {
3434   FIELD *field_on_page = field;
3435
3436   do
3437     {
3438       field_on_page = field_on_page->sprev;
3439       if (Field_Is_Selectable(field_on_page))
3440         break;
3441     }
3442   while (field_on_page != field);
3443
3444   return (field_on_page);
3445 }
3446
3447 /*---------------------------------------------------------------------------
3448 |   Facility      :  libnform
3449 |   Function      :  static FIELD *Left_Neighbor_Field(FIELD * field)
3450 |
3451 |   Description   :  Get the left neighbor of the field on the same line
3452 |                    and the same page. Cycles through the line.
3453 |
3454 |   Return Values :  Pointer to left neighbor field.
3455 +--------------------------------------------------------------------------*/
3456 NCURSES_INLINE static FIELD *
3457 Left_Neighbor_Field(FIELD *field)
3458 {
3459   FIELD *field_on_page = field;
3460
3461   /* For a field that has really a left neighbor, the while clause
3462      immediately fails and the loop is left, positioned at the right
3463      neighbor. Otherwise we cycle backwards through the sorted field list
3464      until we enter the same line (from the right end).
3465    */
3466   do
3467     {
3468       field_on_page = Sorted_Previous_Field(field_on_page);
3469     }
3470   while (field_on_page->frow != field->frow);
3471
3472   return (field_on_page);
3473 }
3474
3475 /*---------------------------------------------------------------------------
3476 |   Facility      :  libnform
3477 |   Function      :  static FIELD *Right_Neighbor_Field(FIELD * field)
3478 |
3479 |   Description   :  Get the right neighbor of the field on the same line
3480 |                    and the same page.
3481 |
3482 |   Return Values :  Pointer to right neighbor field.
3483 +--------------------------------------------------------------------------*/
3484 NCURSES_INLINE static FIELD *
3485 Right_Neighbor_Field(FIELD *field)
3486 {
3487   FIELD *field_on_page = field;
3488
3489   /* See the comments on Left_Neighbor_Field to understand how it works */
3490   do
3491     {
3492       field_on_page = Sorted_Next_Field(field_on_page);
3493     }
3494   while (field_on_page->frow != field->frow);
3495
3496   return (field_on_page);
3497 }
3498
3499 /*---------------------------------------------------------------------------
3500 |   Facility      :  libnform
3501 |   Function      :  static FIELD *Upper_Neighbor_Field(FIELD * field)
3502 |
3503 |   Description   :  Because of the row-major nature of sorting the fields,
3504 |                    it is more difficult to define what the upper neighbor
3505 |                    field really means. We define that it must be on a
3506 |                    'previous' line (cyclic order!) and is the rightmost
3507 |                    field lying on the left side of the given field. If
3508 |                    this set is empty, we take the first field on the line.
3509 |
3510 |   Return Values :  Pointer to the upper neighbor field.
3511 +--------------------------------------------------------------------------*/
3512 static FIELD *
3513 Upper_Neighbor_Field(FIELD *field)
3514 {
3515   FIELD *field_on_page = field;
3516   int frow = field->frow;
3517   int fcol = field->fcol;
3518
3519   /* Walk back to the 'previous' line. The second term in the while clause
3520      just guarantees that we stop if we cycled through the line because
3521      there might be no 'previous' line if the page has just one line.
3522    */
3523   do
3524     {
3525       field_on_page = Sorted_Previous_Field(field_on_page);
3526     }
3527   while (field_on_page->frow == frow && field_on_page->fcol != fcol);
3528
3529   if (field_on_page->frow != frow)
3530     {
3531       /* We really found a 'previous' line. We are positioned at the
3532          rightmost field on this line */
3533       frow = field_on_page->frow;
3534
3535       /* We walk to the left as long as we are really right of the
3536          field. */
3537       while (field_on_page->frow == frow && field_on_page->fcol > fcol)
3538         field_on_page = Sorted_Previous_Field(field_on_page);
3539
3540       /* If we wrapped, just go to the right which is the first field on
3541          the row */
3542       if (field_on_page->frow != frow)
3543         field_on_page = Sorted_Next_Field(field_on_page);
3544     }
3545
3546   return (field_on_page);
3547 }
3548
3549 /*---------------------------------------------------------------------------
3550 |   Facility      :  libnform
3551 |   Function      :  static FIELD *Down_Neighbor_Field(FIELD * field)
3552 |
3553 |   Description   :  Because of the row-major nature of sorting the fields,
3554 |                    it's more difficult to define what the down neighbor
3555 |                    field really means. We define that it must be on a
3556 |                    'next' line (cyclic order!) and is the leftmost
3557 |                    field laying on the right side of the given field. If
3558 |                    this set is empty, we take the last field on the line.
3559 |
3560 |   Return Values :  Pointer to the upper neighbor field.
3561 +--------------------------------------------------------------------------*/
3562 static FIELD *
3563 Down_Neighbor_Field(FIELD *field)
3564 {
3565   FIELD *field_on_page = field;
3566   int frow = field->frow;
3567   int fcol = field->fcol;
3568
3569   /* Walk forward to the 'next' line. The second term in the while clause
3570      just guarantees that we stop if we cycled through the line because
3571      there might be no 'next' line if the page has just one line.
3572    */
3573   do
3574     {
3575       field_on_page = Sorted_Next_Field(field_on_page);
3576     }
3577   while (field_on_page->frow == frow && field_on_page->fcol != fcol);
3578
3579   if (field_on_page->frow != frow)
3580     {
3581       /* We really found a 'next' line. We are positioned at the rightmost
3582          field on this line */
3583       frow = field_on_page->frow;
3584
3585       /* We walk to the right as long as we are really left of the
3586          field. */
3587       while (field_on_page->frow == frow && field_on_page->fcol < fcol)
3588         field_on_page = Sorted_Next_Field(field_on_page);
3589
3590       /* If we wrapped, just go to the left which is the last field on
3591          the row */
3592       if (field_on_page->frow != frow)
3593         field_on_page = Sorted_Previous_Field(field_on_page);
3594     }
3595
3596   return (field_on_page);
3597 }
3598
3599 /*----------------------------------------------------------------------------
3600   Inter-Field Navigation routines
3601   --------------------------------------------------------------------------*/
3602
3603 /*---------------------------------------------------------------------------
3604 |   Facility      :  libnform
3605 |   Function      :  static int Inter_Field_Navigation(
3606 |                                           int (* const fct) (FORM *),
3607 |                                           FORM * form)
3608 |
3609 |   Description   :  Generic behavior for changing the current field, the
3610 |                    field is left and a new field is entered. So the field
3611 |                    must be validated and the field init/term hooks must
3612 |                    be called.
3613 |
3614 |   Return Values :  E_OK                - success
3615 |                    E_INVALID_FIELD     - field is invalid
3616 |                    some other          - error from subordinate call
3617 +--------------------------------------------------------------------------*/
3618 static int
3619 Inter_Field_Navigation(int (*const fct) (FORM *), FORM *form)
3620 {
3621   int res;
3622
3623   if (!_nc_Internal_Validation(form))
3624     res = E_INVALID_FIELD;
3625   else
3626     {
3627       Call_Hook(form, fieldterm);
3628       res = fct(form);
3629       Call_Hook(form, fieldinit);
3630     }
3631   return res;
3632 }
3633
3634 /*---------------------------------------------------------------------------
3635 |   Facility      :  libnform
3636 |   Function      :  static int FN_Next_Field(FORM * form)
3637 |
3638 |   Description   :  Move to the next field on the current page of the form
3639 |
3640 |   Return Values :  E_OK                 - success
3641 |                    != E_OK              - error from subordinate call
3642 +--------------------------------------------------------------------------*/
3643 static int
3644 FN_Next_Field(FORM *form)
3645 {
3646   T((T_CALLED("FN_Next_Field(%p)"), (void *)form));
3647   returnCode(_nc_Set_Current_Field(form,
3648                                    Next_Field_On_Page(form->current)));
3649 }
3650
3651 /*---------------------------------------------------------------------------
3652 |   Facility      :  libnform
3653 |   Function      :  static int FN_Previous_Field(FORM * form)
3654 |
3655 |   Description   :  Move to the previous field on the current page of the
3656 |                    form
3657 |
3658 |   Return Values :  E_OK                 - success
3659 |                    != E_OK              - error from subordinate call
3660 +--------------------------------------------------------------------------*/
3661 static int
3662 FN_Previous_Field(FORM *form)
3663 {
3664   T((T_CALLED("FN_Previous_Field(%p)"), (void *)form));
3665   returnCode(_nc_Set_Current_Field(form,
3666                                    Previous_Field_On_Page(form->current)));
3667 }
3668
3669 /*---------------------------------------------------------------------------
3670 |   Facility      :  libnform
3671 |   Function      :  static int FN_First_Field(FORM * form)
3672 |
3673 |   Description   :  Move to the first field on the current page of the form
3674 |
3675 |   Return Values :  E_OK                 - success
3676 |                    != E_OK              - error from subordinate call
3677 +--------------------------------------------------------------------------*/
3678 static int
3679 FN_First_Field(FORM *form)
3680 {
3681   T((T_CALLED("FN_First_Field(%p)"), (void *)form));
3682   returnCode(_nc_Set_Current_Field(form,
3683                                    Next_Field_On_Page(form->field[form->page[form->curpage].pmax])));
3684 }
3685
3686 /*---------------------------------------------------------------------------
3687 |   Facility      :  libnform
3688 |   Function      :  static int FN_Last_Field(FORM * form)
3689 |
3690 |   Description   :  Move to the last field on the current page of the form
3691 |
3692 |   Return Values :  E_OK                 - success
3693 |                    != E_OK              - error from subordinate call
3694 +--------------------------------------------------------------------------*/
3695 static int
3696 FN_Last_Field(FORM *form)
3697 {
3698   T((T_CALLED("FN_Last_Field(%p)"), (void *)form));
3699   returnCode(
3700               _nc_Set_Current_Field(form,
3701                                     Previous_Field_On_Page(form->field[form->page[form->curpage].pmin])));
3702 }
3703
3704 /*---------------------------------------------------------------------------
3705 |   Facility      :  libnform
3706 |   Function      :  static int FN_Sorted_Next_Field(FORM * form)
3707 |
3708 |   Description   :  Move to the sorted next field on the current page
3709 |                    of the form.
3710 |
3711 |   Return Values :  E_OK            - success
3712 |                    != E_OK         - error from subordinate call