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