]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/frm_driver.c
ncurses 5.7 - patch 20090829
[ncurses.git] / form / frm_driver.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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.90 2009/08/29 19:02:25 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)->opts & 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)->dcols == (field)->cols)   && \
183     ((field)->opts & O_STATIC))             )
184
185 /* Logic to determine whether or not a dynamic field may still grow */
186 #define Growable(field) ((field)->status & _MAY_GROW)
187
188 /* Macro to set the attributes for a fields window */
189 #define Set_Field_Window_Attributes(field,win) \
190 (  wbkgdset((win),(chtype)((field)->pad | (field)->back)), \
191    (void) wattrset((win),(field)->fore) )
192
193 /* Logic to decide whether or not a field really appears on the form */
194 #define Field_Really_Appears(field)         \
195   ((field->form)                          &&\
196    (field->form->status & _POSTED)        &&\
197    (field->opts & O_VISIBLE)              &&\
198    (field->page == field->form->curpage))
199
200 /* Logic to determine whether or not we are on the first position in the
201    current field */
202 #define First_Position_In_Current_Field(form) \
203   (((form)->currow==0) && ((form)->curcol==0))
204
205 #define Minimum(a,b) (((a)<=(b)) ? (a) : (b))
206 #define Maximum(a,b) (((a)>=(b)) ? (a) : (b))
207
208 /*----------------------------------------------------------------------------
209   Useful constants
210   --------------------------------------------------------------------------*/
211 static FIELD_CELL myBLANK = BLANK;
212 static FIELD_CELL myZEROS;
213
214 #ifdef TRACE
215 static void
216 check_pos(FORM *form, int lineno)
217 {
218   int y, x;
219
220   if (form && form->w)
221     {
222       getyx(form->w, y, x);
223       if (y != form->currow || x != form->curcol)
224         {
225           T(("CHECKPOS %s@%d have position %d,%d vs want %d,%d",
226              __FILE__, lineno,
227              y, x,
228              form->currow, form->curcol));
229         }
230     }
231 }
232 #define CHECKPOS(form) check_pos(form, __LINE__)
233 #else
234 #define CHECKPOS(form)          /* nothing */
235 #endif
236
237 /*----------------------------------------------------------------------------
238   Wide-character special functions
239   --------------------------------------------------------------------------*/
240 #if USE_WIDEC_SUPPORT
241 /* like winsnstr */
242 static int
243 wins_wchnstr(WINDOW *w, cchar_t *s, int n)
244 {
245   int code = ERR;
246   int y, x;
247
248   while (n-- > 0)
249     {
250       getyx(w, y, x);
251       if ((code = wins_wch(w, s++)) != OK)
252         break;
253       if ((code = wmove(w, y, x + 1)) != OK)
254         break;
255     }
256   return code;
257 }
258
259 /* win_wchnstr is inconsistent with winnstr, since it returns OK rather than
260  * the number of items transferred.
261  */
262 static int
263 fix_wchnstr(WINDOW *w, cchar_t *s, int n)
264 {
265   int x;
266
267   win_wchnstr(w, s, n);
268   /*
269    * This function is used to extract the text only from the window.
270    * Strip attributes and color from the string so they will not be added
271    * back when copying the string to the window.
272    */
273   for (x = 0; x < n; ++x)
274     {
275       RemAttr(s[x], A_ATTRIBUTES);
276       SetPair(s[x], 0);
277     }
278   return n;
279 }
280
281 /*
282  * Returns the column of the base of the given cell.
283  */
284 static int
285 cell_base(WINDOW *win, int y, int x)
286 {
287   int result = x;
288
289   while (LEGALYX(win, y, x))
290     {
291       cchar_t *data = &(win->_line[y].text[x]);
292
293       if (isWidecBase(CHDEREF(data)) || !isWidecExt(CHDEREF(data)))
294         {
295           result = x;
296           break;
297         }
298       --x;
299     }
300   return result;
301 }
302
303 /*
304  * Returns the number of columns needed for the given cell in a window.
305  */
306 static int
307 cell_width(WINDOW *win, int y, int x)
308 {
309   int result = 1;
310
311   if (LEGALYX(win, y, x))
312     {
313       cchar_t *data = &(win->_line[y].text[x]);
314
315       if (isWidecExt(CHDEREF(data)))
316         {
317           /* recur, providing the number of columns to the next character */
318           result = cell_width(win, y, x - 1);
319         }
320       else
321         {
322           result = wcwidth(CharOf(CHDEREF(data)));
323         }
324     }
325   return result;
326 }
327
328 /*
329  * There is no wide-character function such as wdel_wch(), so we must find
330  * all of the cells that comprise a multi-column character and delete them
331  * one-by-one.
332  */
333 static void
334 delete_char(FORM *form)
335 {
336   int cells = cell_width(form->w, form->currow, form->curcol);
337
338   form->curcol = cell_base(form->w, form->currow, form->curcol);
339   wmove(form->w, form->currow, form->curcol);
340   while (cells-- > 0)
341     {
342       wdelch(form->w);
343     }
344 }
345 #define DeleteChar(form) delete_char(form)
346 #else
347 #define DeleteChar(form) \
348           wmove((form)->w, (form)->currow, (form)->curcol), \
349           wdelch((form)->w)
350 #endif
351
352 /*---------------------------------------------------------------------------
353 |   Facility      :  libnform
354 |   Function      :  static char *Get_Start_Of_Data(char * buf, int blen)
355 |
356 |   Description   :  Return pointer to first non-blank position in buffer.
357 |                    If buffer is empty return pointer to buffer itself.
358 |
359 |   Return Values :  Pointer to first non-blank position in buffer
360 +--------------------------------------------------------------------------*/
361 NCURSES_INLINE static FIELD_CELL *
362 Get_Start_Of_Data(FIELD_CELL *buf, int blen)
363 {
364   FIELD_CELL *p = buf;
365   FIELD_CELL *end = &buf[blen];
366
367   assert(buf && blen >= 0);
368   while ((p < end) && ISBLANK(*p))
369     p++;
370   return ((p == end) ? buf : p);
371 }
372
373 /*---------------------------------------------------------------------------
374 |   Facility      :  libnform
375 |   Function      :  static char *After_End_Of_Data(char * buf, int blen)
376 |
377 |   Description   :  Return pointer after last non-blank position in buffer.
378 |                    If buffer is empty, return pointer to buffer itself.
379 |
380 |   Return Values :  Pointer to position after last non-blank position in
381 |                    buffer.
382 +--------------------------------------------------------------------------*/
383 NCURSES_INLINE static FIELD_CELL *
384 After_End_Of_Data(FIELD_CELL *buf, int blen)
385 {
386   FIELD_CELL *p = &buf[blen];
387
388   assert(buf && blen >= 0);
389   while ((p > buf) && ISBLANK(p[-1]))
390     p--;
391   return (p);
392 }
393
394 /*---------------------------------------------------------------------------
395 |   Facility      :  libnform
396 |   Function      :  static char *Get_First_Whitespace_Character(
397 |                                     char * buf, int   blen)
398 |
399 |   Description   :  Position to the first whitespace character.
400 |
401 |   Return Values :  Pointer to first whitespace character in buffer.
402 +--------------------------------------------------------------------------*/
403 NCURSES_INLINE static FIELD_CELL *
404 Get_First_Whitespace_Character(FIELD_CELL *buf, int blen)
405 {
406   FIELD_CELL *p = buf;
407   FIELD_CELL *end = &p[blen];
408
409   assert(buf && blen >= 0);
410   while ((p < end) && !ISBLANK(*p))
411     p++;
412   return ((p == end) ? buf : p);
413 }
414
415 /*---------------------------------------------------------------------------
416 |   Facility      :  libnform
417 |   Function      :  static char *After_Last_Whitespace_Character(
418 |                                     char * buf, int blen)
419 |
420 |   Description   :  Get the position after the last whitespace character.
421 |
422 |   Return Values :  Pointer to position after last whitespace character in
423 |                    buffer.
424 +--------------------------------------------------------------------------*/
425 NCURSES_INLINE static FIELD_CELL *
426 After_Last_Whitespace_Character(FIELD_CELL *buf, int blen)
427 {
428   FIELD_CELL *p = &buf[blen];
429
430   assert(buf && blen >= 0);
431   while ((p > buf) && !ISBLANK(p[-1]))
432     p--;
433   return (p);
434 }
435
436 /* Set this to 1 to use the div_t version. This is a good idea if your
437    compiler has an intrinsic div() support. Unfortunately GNU-C has it
438    not yet.
439    N.B.: This only works if form->curcol follows immediately form->currow
440          and both are of type int.
441 */
442 #define USE_DIV_T (0)
443
444 /*---------------------------------------------------------------------------
445 |   Facility      :  libnform
446 |   Function      :  static void Adjust_Cursor_Position(
447 |                                       FORM * form, const char * pos)
448 |
449 |   Description   :  Set current row and column of the form to values
450 |                    corresponding to the buffer position.
451 |
452 |   Return Values :  -
453 +--------------------------------------------------------------------------*/
454 NCURSES_INLINE static void
455 Adjust_Cursor_Position(FORM *form, const FIELD_CELL *pos)
456 {
457   FIELD *field;
458   int idx;
459
460   field = form->current;
461   assert(pos >= field->buf && field->dcols > 0);
462   idx = (int)(pos - field->buf);
463 #if USE_DIV_T
464   *((div_t *) & (form->currow)) = div(idx, field->dcols);
465 #else
466   form->currow = idx / field->dcols;
467   form->curcol = idx - field->cols * form->currow;
468 #endif
469   if (field->drows < form->currow)
470     form->currow = 0;
471 }
472
473 /*---------------------------------------------------------------------------
474 |   Facility      :  libnform
475 |   Function      :  static void Buffer_To_Window(
476 |                                      const FIELD  * field,
477 |                                      WINDOW * win)
478 |
479 |   Description   :  Copy the buffer to the window. If it is a multi-line
480 |                    field, the buffer is split to the lines of the
481 |                    window without any editing.
482 |
483 |   Return Values :  -
484 +--------------------------------------------------------------------------*/
485 static void
486 Buffer_To_Window(const FIELD *field, WINDOW *win)
487 {
488   int width, height;
489   int y, x;
490   int len;
491   int row;
492   FIELD_CELL *pBuffer;
493
494   assert(win && field);
495
496   getyx(win, y, x);
497   width = getmaxx(win);
498   height = getmaxy(win);
499
500   for (row = 0, pBuffer = field->buf;
501        row < height;
502        row++, pBuffer += width)
503     {
504       if ((len = (int)(After_End_Of_Data(pBuffer, width) - pBuffer)) > 0)
505         {
506           wmove(win, row, 0);
507           myADDNSTR(win, pBuffer, len);
508         }
509     }
510   wmove(win, y, x);
511 }
512
513 /*---------------------------------------------------------------------------
514 |   Facility      :  libnform
515 |   Function      :  static void Window_To_Buffer(
516 |                                          WINDOW * win,
517 |                                          FIELD  * field)
518 |
519 |   Description   :  Copy the content of the window into the buffer.
520 |                    The multiple lines of a window are simply
521 |                    concatenated into the buffer. Pad characters in
522 |                    the window will be replaced by blanks in the buffer.
523 |
524 |   Return Values :  -
525 +--------------------------------------------------------------------------*/
526 static void
527 Window_To_Buffer(WINDOW *win, FIELD *field)
528 {
529   int pad;
530   int len = 0;
531   FIELD_CELL *p;
532   int row, height;
533
534   assert(win && field && field->buf);
535
536   pad = field->pad;
537   p = field->buf;
538   height = getmaxy(win);
539
540   for (row = 0; (row < height) && (row < field->drows); row++)
541     {
542       wmove(win, row, 0);
543       len += myINNSTR(win, p + len, field->dcols);
544     }
545   p[len] = myZEROS;
546
547   /* replace visual padding character by blanks in buffer */
548   if (pad != C_BLANK)
549     {
550       int i;
551
552       for (i = 0; i < len; i++, p++)
553         {
554           if ((unsigned long)CharOf(*p) == ChCharOf(pad)
555 #if USE_WIDEC_SUPPORT
556               && p->chars[1] == 0
557 #endif
558             )
559             *p = myBLANK;
560         }
561     }
562 }
563
564 /*---------------------------------------------------------------------------
565 |   Facility      :  libnform
566 |   Function      :  static void Synchronize_Buffer(FORM * form)
567 |
568 |   Description   :  If there was a change, copy the content of the
569 |                    window into the buffer, so the buffer is synchronized
570 |                    with the windows content. We have to indicate that the
571 |                    buffer needs validation due to the change.
572 |
573 |   Return Values :  -
574 +--------------------------------------------------------------------------*/
575 NCURSES_INLINE static void
576 Synchronize_Buffer(FORM *form)
577 {
578   if (form->status & _WINDOW_MODIFIED)
579     {
580       form->status &= ~_WINDOW_MODIFIED;
581       form->status |= _FCHECK_REQUIRED;
582       Window_To_Buffer(form->w, form->current);
583       wmove(form->w, form->currow, form->curcol);
584     }
585 }
586
587 /*---------------------------------------------------------------------------
588 |   Facility      :  libnform
589 |   Function      :  static bool Field_Grown( FIELD *field, int amount)
590 |
591 |   Description   :  This function is called for growable dynamic fields
592 |                    only. It has to increase the buffers and to allocate
593 |                    a new window for this field.
594 |                    This function has the side effect to set a new
595 |                    field-buffer pointer, the dcols and drows values
596 |                    as well as a new current Window for the field.
597 |
598 |   Return Values :  TRUE     - field successfully increased
599 |                    FALSE    - there was some error
600 +--------------------------------------------------------------------------*/
601 static bool
602 Field_Grown(FIELD *field, int amount)
603 {
604   bool result = FALSE;
605
606   if (field && Growable(field))
607     {
608       bool single_line_field = Single_Line_Field(field);
609       int old_buflen = Buffer_Length(field);
610       int new_buflen;
611       int old_dcols = field->dcols;
612       int old_drows = field->drows;
613       FIELD_CELL *oldbuf = field->buf;
614       FIELD_CELL *newbuf;
615
616       int growth;
617       FORM *form = field->form;
618       bool need_visual_update = ((form != (FORM *)0) &&
619                                  (form->status & _POSTED) &&
620                                  (form->current == field));
621
622       if (need_visual_update)
623         Synchronize_Buffer(form);
624
625       if (single_line_field)
626         {
627           growth = field->cols * amount;
628           if (field->maxgrow)
629             growth = Minimum(field->maxgrow - field->dcols, growth);
630           field->dcols += growth;
631           if (field->dcols == field->maxgrow)
632             field->status &= ~_MAY_GROW;
633         }
634       else
635         {
636           growth = (field->rows + field->nrow) * amount;
637           if (field->maxgrow)
638             growth = Minimum(field->maxgrow - field->drows, growth);
639           field->drows += growth;
640           if (field->drows == field->maxgrow)
641             field->status &= ~_MAY_GROW;
642         }
643       /* drows, dcols changed, so we get really the new buffer length */
644       new_buflen = Buffer_Length(field);
645       newbuf = (FIELD_CELL *)malloc(Total_Buffer_Size(field));
646       if (!newbuf)
647         {
648           /* restore to previous state */
649           field->dcols = old_dcols;
650           field->drows = old_drows;
651           if ((single_line_field && (field->dcols != field->maxgrow)) ||
652               (!single_line_field && (field->drows != field->maxgrow)))
653             field->status |= _MAY_GROW;
654         }
655       else
656         {
657           /* Copy all the buffers.  This is the reason why we can't just use
658            * realloc().
659            */
660           int i, j;
661           FIELD_CELL *old_bp;
662           FIELD_CELL *new_bp;
663
664           result = TRUE;        /* allow sharing of recovery on failure */
665
666           T((T_CREATE("fieldcell %p"), newbuf));
667           field->buf = newbuf;
668           for (i = 0; i <= field->nbuf; i++)
669             {
670               new_bp = Address_Of_Nth_Buffer(field, i);
671               old_bp = oldbuf + i * (1 + old_buflen);
672               for (j = 0; j < old_buflen; ++j)
673                 new_bp[j] = old_bp[j];
674               while (j < new_buflen)
675                 new_bp[j++] = myBLANK;
676               new_bp[new_buflen] = myZEROS;
677             }
678
679 #if USE_WIDEC_SUPPORT && NCURSES_EXT_FUNCS
680           if (wresize(field->working, 1, Buffer_Length(field) + 1) == ERR)
681             result = FALSE;
682 #endif
683
684           if (need_visual_update && result)
685             {
686               WINDOW *new_window = newpad(field->drows, field->dcols);
687
688               if (new_window != 0)
689                 {
690                   assert(form != (FORM *)0);
691                   if (form->w)
692                     delwin(form->w);
693                   form->w = new_window;
694                   Set_Field_Window_Attributes(field, form->w);
695                   werase(form->w);
696                   Buffer_To_Window(field, form->w);
697                   untouchwin(form->w);
698                   wmove(form->w, form->currow, form->curcol);
699                 }
700               else
701                 result = FALSE;
702             }
703
704           if (result)
705             {
706               free(oldbuf);
707               /* reflect changes in linked fields */
708               if (field != field->link)
709                 {
710                   FIELD *linked_field;
711
712                   for (linked_field = field->link;
713                        linked_field != field;
714                        linked_field = linked_field->link)
715                     {
716                       linked_field->buf = field->buf;
717                       linked_field->drows = field->drows;
718                       linked_field->dcols = field->dcols;
719                     }
720                 }
721             }
722           else
723             {
724               /* restore old state */
725               field->dcols = old_dcols;
726               field->drows = old_drows;
727               field->buf = oldbuf;
728               if ((single_line_field &&
729                    (field->dcols != field->maxgrow)) ||
730                   (!single_line_field &&
731                    (field->drows != field->maxgrow)))
732                 field->status |= _MAY_GROW;
733               free(newbuf);
734             }
735         }
736     }
737   return (result);
738 }
739
740 #ifdef NCURSES_MOUSE_VERSION
741 /*---------------------------------------------------------------------------
742 |   Facility      :  libnform
743 |   Function      :  int Field_encloses(FIELD *field, int ry, int rx)
744 |
745 |   Description   :  Check if the given coordinates lie within the given field.
746 |
747 |   Return Values :  E_OK              - success
748 |                    E_BAD_ARGUMENT    - invalid form pointer
749 |                    E_SYSTEM_ERROR    - form has no current field or
750 |                                        field-window
751 +--------------------------------------------------------------------------*/
752 static int
753 Field_encloses(FIELD *field, int ry, int rx)
754 {
755   T((T_CALLED("Field_encloses(%p)"), field));
756   if (field != 0
757       && field->frow <= ry
758       && (field->frow + field->rows) > ry
759       && field->fcol <= rx
760       && (field->fcol + field->cols) > rx)
761     {
762       RETURN(E_OK);
763     }
764   RETURN(E_INVALID_FIELD);
765 }
766 #endif
767
768 /*---------------------------------------------------------------------------
769 |   Facility      :  libnform
770 |   Function      :  int _nc_Position_Form_Cursor(FORM * form)
771 |
772 |   Description   :  Position the cursor in the window for the current
773 |                    field to be in sync. with the currow and curcol
774 |                    values.
775 |
776 |   Return Values :  E_OK              - success
777 |                    E_BAD_ARGUMENT    - invalid form pointer
778 |                    E_SYSTEM_ERROR    - form has no current field or
779 |                                        field-window
780 +--------------------------------------------------------------------------*/
781 NCURSES_EXPORT(int)
782 _nc_Position_Form_Cursor(FORM *form)
783 {
784   FIELD *field;
785   WINDOW *formwin;
786
787   if (!form)
788     return (E_BAD_ARGUMENT);
789
790   if (!form->w || !form->current)
791     return (E_SYSTEM_ERROR);
792
793   field = form->current;
794   formwin = Get_Form_Window(form);
795
796   wmove(form->w, form->currow, form->curcol);
797   if (Has_Invisible_Parts(field))
798     {
799       /* in this case fieldwin isn't derived from formwin, so we have
800          to move the cursor in formwin by hand... */
801       wmove(formwin,
802             field->frow + form->currow - form->toprow,
803             field->fcol + form->curcol - form->begincol);
804       wcursyncup(formwin);
805     }
806   else
807     wcursyncup(form->w);
808   return (E_OK);
809 }
810
811 /*---------------------------------------------------------------------------
812 |   Facility      :  libnform
813 |   Function      :  int _nc_Refresh_Current_Field(FORM * form)
814 |
815 |   Description   :  Propagate the changes in the fields window to the
816 |                    window of the form.
817 |
818 |   Return Values :  E_OK              - on success
819 |                    E_BAD_ARGUMENT    - invalid form pointer
820 |                    E_SYSTEM_ERROR    - general error
821 +--------------------------------------------------------------------------*/
822 NCURSES_EXPORT(int)
823 _nc_Refresh_Current_Field(FORM *form)
824 {
825   WINDOW *formwin;
826   FIELD *field;
827
828   T((T_CALLED("_nc_Refresh_Current_Field(%p)"), form));
829
830   if (!form)
831     RETURN(E_BAD_ARGUMENT);
832
833   if (!form->w || !form->current)
834     RETURN(E_SYSTEM_ERROR);
835
836   field = form->current;
837   formwin = Get_Form_Window(form);
838
839   if (field->opts & O_PUBLIC)
840     {
841       if (Is_Scroll_Field(field))
842         {
843           /* Again, in this case the fieldwin isn't derived from formwin,
844              so we have to perform a copy operation. */
845           if (Single_Line_Field(field))
846             {
847               /* horizontal scrolling */
848               if (form->curcol < form->begincol)
849                 form->begincol = form->curcol;
850               else
851                 {
852                   if (form->curcol >= (form->begincol + field->cols))
853                     form->begincol = form->curcol - field->cols + 1;
854                 }
855               copywin(form->w,
856                       formwin,
857                       0,
858                       form->begincol,
859                       field->frow,
860                       field->fcol,
861                       field->frow,
862                       field->cols + field->fcol - 1,
863                       0);
864             }
865           else
866             {
867               /* A multi-line, i.e. vertical scrolling field */
868               int row_after_bottom, first_modified_row, first_unmodified_row;
869
870               if (field->drows > field->rows)
871                 {
872                   row_after_bottom = form->toprow + field->rows;
873                   if (form->currow < form->toprow)
874                     {
875                       form->toprow = form->currow;
876                       field->status |= _NEWTOP;
877                     }
878                   if (form->currow >= row_after_bottom)
879                     {
880                       form->toprow = form->currow - field->rows + 1;
881                       field->status |= _NEWTOP;
882                     }
883                   if (field->status & _NEWTOP)
884                     {
885                       /* means we have to copy whole range */
886                       first_modified_row = form->toprow;
887                       first_unmodified_row = first_modified_row + field->rows;
888                       field->status &= ~_NEWTOP;
889                     }
890                   else
891                     {
892                       /* we try to optimize : finding the range of touched
893                          lines */
894                       first_modified_row = form->toprow;
895                       while (first_modified_row < row_after_bottom)
896                         {
897                           if (is_linetouched(form->w, first_modified_row))
898                             break;
899                           first_modified_row++;
900                         }
901                       first_unmodified_row = first_modified_row;
902                       while (first_unmodified_row < row_after_bottom)
903                         {
904                           if (!is_linetouched(form->w, first_unmodified_row))
905                             break;
906                           first_unmodified_row++;
907                         }
908                     }
909                 }
910               else
911                 {
912                   first_modified_row = form->toprow;
913                   first_unmodified_row = first_modified_row + field->rows;
914                 }
915               if (first_unmodified_row != first_modified_row)
916                 copywin(form->w,
917                         formwin,
918                         first_modified_row,
919                         0,
920                         field->frow + first_modified_row - form->toprow,
921                         field->fcol,
922                         field->frow + first_unmodified_row - form->toprow - 1,
923                         field->cols + field->fcol - 1,
924                         0);
925             }
926           wsyncup(formwin);
927         }
928       else
929         {
930           /* if the field-window is simply a derived window, i.e. contains no
931            * invisible parts, the whole thing is trivial
932            */
933           wsyncup(form->w);
934         }
935     }
936   untouchwin(form->w);
937   returnCode(_nc_Position_Form_Cursor(form));
938 }
939
940 /*---------------------------------------------------------------------------
941 |   Facility      :  libnform
942 |   Function      :  static void Perform_Justification(
943 |                                        FIELD  * field,
944 |                                        WINDOW * win)
945 |
946 |   Description   :  Output field with requested justification
947 |
948 |   Return Values :  -
949 +--------------------------------------------------------------------------*/
950 static void
951 Perform_Justification(FIELD *field, WINDOW *win)
952 {
953   FIELD_CELL *bp;
954   int len;
955   int col = 0;
956
957   bp = Get_Start_Of_Data(field->buf, Buffer_Length(field));
958   len = (int)(After_End_Of_Data(field->buf, Buffer_Length(field)) - bp);
959
960   if (len > 0)
961     {
962       assert(win && (field->drows == 1) && (field->dcols == field->cols));
963
964       switch (field->just)
965         {
966         case JUSTIFY_LEFT:
967           break;
968         case JUSTIFY_CENTER:
969           col = (field->cols - len) / 2;
970           break;
971         case JUSTIFY_RIGHT:
972           col = field->cols - len;
973           break;
974         default:
975           break;
976         }
977
978       wmove(win, 0, col);
979       myADDNSTR(win, bp, len);
980     }
981 }
982
983 /*---------------------------------------------------------------------------
984 |   Facility      :  libnform
985 |   Function      :  static void Undo_Justification(
986 |                                     FIELD  * field,
987 |                                     WINDOW * win)
988 |
989 |   Description   :  Display field without any justification, i.e.
990 |                    left justified
991 |
992 |   Return Values :  -
993 +--------------------------------------------------------------------------*/
994 static void
995 Undo_Justification(FIELD *field, WINDOW *win)
996 {
997   FIELD_CELL *bp;
998   int len;
999
1000   bp = Get_Start_Of_Data(field->buf, Buffer_Length(field));
1001   len = (int)(After_End_Of_Data(field->buf, Buffer_Length(field)) - bp);
1002
1003   if (len > 0)
1004     {
1005       assert(win);
1006       wmove(win, 0, 0);
1007       myADDNSTR(win, bp, len);
1008     }
1009 }
1010
1011 /*---------------------------------------------------------------------------
1012 |   Facility      :  libnform
1013 |   Function      :  static bool Check_Char(
1014 |                                           FIELDTYPE * typ,
1015 |                                           int ch,
1016 |                                           TypeArgument *argp)
1017 |
1018 |   Description   :  Perform a single character check for character ch
1019 |                    according to the fieldtype instance.
1020 |
1021 |   Return Values :  TRUE             - Character is valid
1022 |                    FALSE            - Character is invalid
1023 +--------------------------------------------------------------------------*/
1024 static bool
1025 Check_Char(FIELDTYPE *typ, int ch, TypeArgument *argp)
1026 {
1027   if (typ)
1028     {
1029       if (typ->status & _LINKED_TYPE)
1030         {
1031           assert(argp);
1032           return (
1033                    Check_Char(typ->left, ch, argp->left) ||
1034                    Check_Char(typ->right, ch, argp->right));
1035         }
1036       else
1037         {
1038           if (typ->ccheck)
1039             return typ->ccheck(ch, (void *)argp);
1040         }
1041     }
1042   return (!iscntrl(UChar(ch)) ? TRUE : FALSE);
1043 }
1044
1045 /*---------------------------------------------------------------------------
1046 |   Facility      :  libnform
1047 |   Function      :  static int Display_Or_Erase_Field(
1048 |                                           FIELD * field,
1049 |                                           bool bEraseFlag)
1050 |
1051 |   Description   :  Create a subwindow for the field and display the
1052 |                    buffer contents (apply justification if required)
1053 |                    or simply erase the field.
1054 |
1055 |   Return Values :  E_OK           - on success
1056 |                    E_SYSTEM_ERROR - some error (typical no memory)
1057 +--------------------------------------------------------------------------*/
1058 static int
1059 Display_Or_Erase_Field(FIELD *field, bool bEraseFlag)
1060 {
1061   WINDOW *win;
1062   WINDOW *fwin;
1063
1064   if (!field)
1065     return E_SYSTEM_ERROR;
1066
1067   fwin = Get_Form_Window(field->form);
1068   win = derwin(fwin,
1069                field->rows, field->cols, field->frow, field->fcol);
1070
1071   if (!win)
1072     return E_SYSTEM_ERROR;
1073   else
1074     {
1075       if (field->opts & O_VISIBLE)
1076         {
1077           Set_Field_Window_Attributes(field, win);
1078         }
1079       else
1080         {
1081           (void)wattrset(win, WINDOW_ATTRS(fwin));
1082         }
1083       werase(win);
1084     }
1085
1086   if (!bEraseFlag)
1087     {
1088       if (field->opts & O_PUBLIC)
1089         {
1090           if (Justification_Allowed(field))
1091             Perform_Justification(field, win);
1092           else
1093             Buffer_To_Window(field, win);
1094         }
1095       field->status &= ~_NEWTOP;
1096     }
1097   wsyncup(win);
1098   delwin(win);
1099   return E_OK;
1100 }
1101
1102 /* Macros to preset the bEraseFlag */
1103 #define Display_Field(field) Display_Or_Erase_Field(field,FALSE)
1104 #define Erase_Field(field)   Display_Or_Erase_Field(field,TRUE)
1105
1106 /*---------------------------------------------------------------------------
1107 |   Facility      :  libnform
1108 |   Function      :  static int Synchronize_Field(FIELD * field)
1109 |
1110 |   Description   :  Synchronize the windows content with the value in
1111 |                    the buffer.
1112 |
1113 |   Return Values :  E_OK                - success
1114 |                    E_BAD_ARGUMENT      - invalid field pointer
1115 |                    E_SYSTEM_ERROR      - some severe basic error
1116 +--------------------------------------------------------------------------*/
1117 static int
1118 Synchronize_Field(FIELD *field)
1119 {
1120   FORM *form;
1121   int res = E_OK;
1122
1123   if (!field)
1124     return (E_BAD_ARGUMENT);
1125
1126   if (((form = field->form) != (FORM *)0)
1127       && Field_Really_Appears(field))
1128     {
1129       if (field == form->current)
1130         {
1131           form->currow = form->curcol = form->toprow = form->begincol = 0;
1132           werase(form->w);
1133
1134           if ((field->opts & O_PUBLIC) && Justification_Allowed(field))
1135             Undo_Justification(field, form->w);
1136           else
1137             Buffer_To_Window(field, form->w);
1138
1139           field->status |= _NEWTOP;
1140           res = _nc_Refresh_Current_Field(form);
1141         }
1142       else
1143         res = Display_Field(field);
1144     }
1145   field->status |= _CHANGED;
1146   return (res);
1147 }
1148
1149 /*---------------------------------------------------------------------------
1150 |   Facility      :  libnform
1151 |   Function      :  static int Synchronize_Linked_Fields(FIELD * field)
1152 |
1153 |   Description   :  Propagate the Synchronize_Field function to all linked
1154 |                    fields. The first error that occurs in the sequence
1155 |                    of updates is the return value.
1156 |
1157 |   Return Values :  E_OK                - success
1158 |                    E_BAD_ARGUMENT      - invalid field pointer
1159 |                    E_SYSTEM_ERROR      - some severe basic error
1160 +--------------------------------------------------------------------------*/
1161 static int
1162 Synchronize_Linked_Fields(FIELD *field)
1163 {
1164   FIELD *linked_field;
1165   int res = E_OK;
1166   int syncres;
1167
1168   if (!field)
1169     return (E_BAD_ARGUMENT);
1170
1171   if (!field->link)
1172     return (E_SYSTEM_ERROR);
1173
1174   for (linked_field = field->link;
1175        linked_field != field;
1176        linked_field = linked_field->link)
1177     {
1178       if (((syncres = Synchronize_Field(linked_field)) != E_OK) &&
1179           (res == E_OK))
1180         res = syncres;
1181     }
1182   return (res);
1183 }
1184
1185 /*---------------------------------------------------------------------------
1186 |   Facility      :  libnform
1187 |   Function      :  int _nc_Synchronize_Attributes(FIELD * field)
1188 |
1189 |   Description   :  If a fields visual attributes have changed, this
1190 |                    routine is called to propagate those changes to the
1191 |                    screen.
1192 |
1193 |   Return Values :  E_OK             - success
1194 |                    E_BAD_ARGUMENT   - invalid field pointer
1195 |                    E_SYSTEM_ERROR   - some severe basic error
1196 +--------------------------------------------------------------------------*/
1197 NCURSES_EXPORT(int)
1198 _nc_Synchronize_Attributes(FIELD *field)
1199 {
1200   FORM *form;
1201   int res = E_OK;
1202   WINDOW *formwin;
1203
1204   T((T_CALLED("_nc_Synchronize_Attributes(%p)"), field));
1205
1206   if (!field)
1207     returnCode(E_BAD_ARGUMENT);
1208
1209   CHECKPOS(field->form);
1210   if (((form = field->form) != (FORM *)0)
1211       && Field_Really_Appears(field))
1212     {
1213       if (form->current == field)
1214         {
1215           Synchronize_Buffer(form);
1216           Set_Field_Window_Attributes(field, form->w);
1217           werase(form->w);
1218           wmove(form->w, form->currow, form->curcol);
1219
1220           if (field->opts & O_PUBLIC)
1221             {
1222               if (Justification_Allowed(field))
1223                 Undo_Justification(field, form->w);
1224               else
1225                 Buffer_To_Window(field, form->w);
1226             }
1227           else
1228             {
1229               formwin = Get_Form_Window(form);
1230               copywin(form->w, formwin,
1231                       0, 0,
1232                       field->frow, field->fcol,
1233                       field->rows - 1, field->cols - 1, 0);
1234               wsyncup(formwin);
1235               Buffer_To_Window(field, form->w);
1236               field->status |= _NEWTOP;         /* fake refresh to paint all */
1237               _nc_Refresh_Current_Field(form);
1238             }
1239         }
1240       else
1241         {
1242           res = Display_Field(field);
1243         }
1244     }
1245   CHECKPOS(form);
1246   returnCode(res);
1247 }
1248
1249 /*---------------------------------------------------------------------------
1250 |   Facility      :  libnform
1251 |   Function      :  int _nc_Synchronize_Options(FIELD * field,
1252 |                                                Field_Options newopts)
1253 |
1254 |   Description   :  If a fields options have changed, this routine is
1255 |                    called to propagate these changes to the screen and
1256 |                    to really change the behavior of the field.
1257 |
1258 |   Return Values :  E_OK                - success
1259 |                    E_BAD_ARGUMENT      - invalid field pointer
1260 |                    E_CURRENT           - field is the current one
1261 |                    E_SYSTEM_ERROR      - some severe basic error
1262 +--------------------------------------------------------------------------*/
1263 NCURSES_EXPORT(int)
1264 _nc_Synchronize_Options(FIELD *field, Field_Options newopts)
1265 {
1266   Field_Options oldopts;
1267   Field_Options changed_opts;
1268   FORM *form;
1269   int res = E_OK;
1270
1271   T((T_CALLED("_nc_Synchronize_Options(%p,%#x)"), field, newopts));
1272
1273   if (!field)
1274     returnCode(E_BAD_ARGUMENT);
1275
1276   oldopts = field->opts;
1277   changed_opts = oldopts ^ newopts;
1278   field->opts = newopts;
1279   form = field->form;
1280
1281   if (form)
1282     {
1283       if (form->current == field)
1284         {
1285           field->opts = oldopts;
1286           returnCode(E_CURRENT);
1287         }
1288
1289       if (form->status & _POSTED)
1290         {
1291           if ((form->curpage == field->page))
1292             {
1293               if (changed_opts & O_VISIBLE)
1294                 {
1295                   if (newopts & O_VISIBLE)
1296                     res = Display_Field(field);
1297                   else
1298                     res = Erase_Field(field);
1299                 }
1300               else
1301                 {
1302                   if ((changed_opts & O_PUBLIC) &&
1303                       (newopts & O_VISIBLE))
1304                     res = Display_Field(field);
1305                 }
1306             }
1307         }
1308     }
1309
1310   if (changed_opts & O_STATIC)
1311     {
1312       bool single_line_field = Single_Line_Field(field);
1313       int res2 = E_OK;
1314
1315       if (newopts & O_STATIC)
1316         {
1317           /* the field becomes now static */
1318           field->status &= ~_MAY_GROW;
1319           /* if actually we have no hidden columns, justification may
1320              occur again */
1321           if (single_line_field &&
1322               (field->cols == field->dcols) &&
1323               (field->just != NO_JUSTIFICATION) &&
1324               Field_Really_Appears(field))
1325             {
1326               res2 = Display_Field(field);
1327             }
1328         }
1329       else
1330         {
1331           /* field is no longer static */
1332           if ((field->maxgrow == 0) ||
1333               (single_line_field && (field->dcols < field->maxgrow)) ||
1334               (!single_line_field && (field->drows < field->maxgrow)))
1335             {
1336               field->status |= _MAY_GROW;
1337               /* a field with justification now changes its behavior,
1338                  so we must redisplay it */
1339               if (single_line_field &&
1340                   (field->just != NO_JUSTIFICATION) &&
1341                   Field_Really_Appears(field))
1342                 {
1343                   res2 = Display_Field(field);
1344                 }
1345             }
1346         }
1347       if (res2 != E_OK)
1348         res = res2;
1349     }
1350
1351   returnCode(res);
1352 }
1353
1354 /*---------------------------------------------------------------------------
1355 |   Facility      :  libnform
1356 |   Function      :  int _nc_Set_Current_Field(FORM  * form,
1357 |                                              FIELD * newfield)
1358 |
1359 |   Description   :  Make the newfield the new current field.
1360 |
1361 |   Return Values :  E_OK              - success
1362 |                    E_BAD_ARGUMENT    - invalid form or field pointer
1363 |                    E_SYSTEM_ERROR    - some severe basic error
1364 |                    E_NOT_CONNECTED   - no fields are connected to the form
1365 +--------------------------------------------------------------------------*/
1366 NCURSES_EXPORT(int)
1367 _nc_Set_Current_Field(FORM *form, FIELD *newfield)
1368 {
1369   FIELD *field;
1370   WINDOW *new_window;
1371
1372   T((T_CALLED("_nc_Set_Current_Field(%p,%p)"), form, newfield));
1373
1374   if (!form || !newfield || !form->current || (newfield->form != form))
1375     returnCode(E_BAD_ARGUMENT);
1376
1377   if ((form->status & _IN_DRIVER))
1378     returnCode(E_BAD_STATE);
1379
1380   if (!(form->field))
1381     returnCode(E_NOT_CONNECTED);
1382
1383   field = form->current;
1384
1385   if ((field != newfield) ||
1386       !(form->status & _POSTED))
1387     {
1388       if ((form->w) &&
1389           (field->opts & O_VISIBLE) &&
1390           (field->form->curpage == field->page))
1391         {
1392           _nc_Refresh_Current_Field(form);
1393           if (field->opts & O_PUBLIC)
1394             {
1395               if (field->drows > field->rows)
1396                 {
1397                   if (form->toprow == 0)
1398                     field->status &= ~_NEWTOP;
1399                   else
1400                     field->status |= _NEWTOP;
1401                 }
1402               else
1403                 {
1404                   if (Justification_Allowed(field))
1405                     {
1406                       Window_To_Buffer(form->w, field);
1407                       werase(form->w);
1408                       Perform_Justification(field, form->w);
1409                       wsyncup(form->w);
1410                     }
1411                 }
1412             }
1413           delwin(form->w);
1414           form->w = (WINDOW *)0;
1415         }
1416
1417       field = newfield;
1418
1419       if (Has_Invisible_Parts(field))
1420         new_window = newpad(field->drows, field->dcols);
1421       else
1422         new_window = derwin(Get_Form_Window(form),
1423                             field->rows, field->cols, field->frow, field->fcol);
1424
1425       if (!new_window)
1426         returnCode(E_SYSTEM_ERROR);
1427
1428       form->current = field;
1429
1430       if (form->w)
1431         delwin(form->w);
1432       form->w = new_window;
1433
1434       form->status &= ~_WINDOW_MODIFIED;
1435       Set_Field_Window_Attributes(field, form->w);
1436
1437       if (Has_Invisible_Parts(field))
1438         {
1439           werase(form->w);
1440           Buffer_To_Window(field, form->w);
1441         }
1442       else
1443         {
1444           if (Justification_Allowed(field))
1445             {
1446               werase(form->w);
1447               Undo_Justification(field, form->w);
1448               wsyncup(form->w);
1449             }
1450         }
1451
1452       untouchwin(form->w);
1453     }
1454
1455   form->currow = form->curcol = form->toprow = form->begincol = 0;
1456   returnCode(E_OK);
1457 }
1458
1459 /*----------------------------------------------------------------------------
1460   Intra-Field Navigation routines
1461   --------------------------------------------------------------------------*/
1462
1463 /*---------------------------------------------------------------------------
1464 |   Facility      :  libnform
1465 |   Function      :  static int IFN_Next_Character(FORM * form)
1466 |
1467 |   Description   :  Move to the next character in the field. In a multi-line
1468 |                    field this wraps at the end of the line.
1469 |
1470 |   Return Values :  E_OK                - success
1471 |                    E_REQUEST_DENIED    - at the rightmost position
1472 +--------------------------------------------------------------------------*/
1473 static int
1474 IFN_Next_Character(FORM *form)
1475 {
1476   FIELD *field = form->current;
1477   int step = myWCWIDTH(form->w, form->currow, form->curcol);
1478
1479   T((T_CALLED("IFN_Next_Character(%p)"), form));
1480   if ((form->curcol += step) == field->dcols)
1481     {
1482       if ((++(form->currow)) == field->drows)
1483         {
1484 #if GROW_IF_NAVIGATE
1485           if (!Single_Line_Field(field) && Field_Grown(field, 1))
1486             {
1487               form->curcol = 0;
1488               returnCode(E_OK);
1489             }
1490 #endif
1491           form->currow--;
1492 #if GROW_IF_NAVIGATE
1493           if (Single_Line_Field(field) && Field_Grown(field, 1))
1494             returnCode(E_OK);
1495 #endif
1496           form->curcol -= step;
1497           returnCode(E_REQUEST_DENIED);
1498         }
1499       form->curcol = 0;
1500     }
1501   returnCode(E_OK);
1502 }
1503
1504 /*---------------------------------------------------------------------------
1505 |   Facility      :  libnform
1506 |   Function      :  static int IFN_Previous_Character(FORM * form)
1507 |
1508 |   Description   :  Move to the previous character in the field. In a
1509 |                    multi-line field this wraps and the beginning of the
1510 |                    line.
1511 |
1512 |   Return Values :  E_OK                - success
1513 |                    E_REQUEST_DENIED    - at the leftmost position
1514 +--------------------------------------------------------------------------*/
1515 static int
1516 IFN_Previous_Character(FORM *form)
1517 {
1518   int amount = myWCWIDTH(form->w, form->currow, form->curcol - 1);
1519   int oldcol = form->curcol;
1520
1521   T((T_CALLED("IFN_Previous_Character(%p)"), form));
1522   if ((form->curcol -= amount) < 0)
1523     {
1524       if ((--(form->currow)) < 0)
1525         {
1526           form->currow++;
1527           form->curcol = oldcol;
1528           returnCode(E_REQUEST_DENIED);
1529         }
1530       form->curcol = form->current->dcols - 1;
1531     }
1532   returnCode(E_OK);
1533 }
1534
1535 /*---------------------------------------------------------------------------
1536 |   Facility      :  libnform
1537 |   Function      :  static int IFN_Next_Line(FORM * form)
1538 |
1539 |   Description   :  Move to the beginning of the next line in the field
1540 |
1541 |   Return Values :  E_OK                - success
1542 |                    E_REQUEST_DENIED    - at the last line
1543 +--------------------------------------------------------------------------*/
1544 static int
1545 IFN_Next_Line(FORM *form)
1546 {
1547   FIELD *field = form->current;
1548
1549   T((T_CALLED("IFN_Next_Line(%p)"), form));
1550   if ((++(form->currow)) == field->drows)
1551     {
1552 #if GROW_IF_NAVIGATE
1553       if (!Single_Line_Field(field) && Field_Grown(field, 1))
1554         returnCode(E_OK);
1555 #endif
1556       form->currow--;
1557       returnCode(E_REQUEST_DENIED);
1558     }
1559   form->curcol = 0;
1560   returnCode(E_OK);
1561 }
1562
1563 /*---------------------------------------------------------------------------
1564 |   Facility      :  libnform
1565 |   Function      :  static int IFN_Previous_Line(FORM * form)
1566 |
1567 |   Description   :  Move to the beginning of the previous line in the field
1568 |
1569 |   Return Values :  E_OK                - success
1570 |                    E_REQUEST_DENIED    - at the first line
1571 +--------------------------------------------------------------------------*/
1572 static int
1573 IFN_Previous_Line(FORM *form)
1574 {
1575   T((T_CALLED("IFN_Previous_Line(%p)"), form));
1576   if ((--(form->currow)) < 0)
1577     {
1578       form->currow++;
1579       returnCode(E_REQUEST_DENIED);
1580     }
1581   form->curcol = 0;
1582   returnCode(E_OK);
1583 }
1584
1585 /*---------------------------------------------------------------------------
1586 |   Facility      :  libnform
1587 |   Function      :  static int IFN_Next_Word(FORM * form)
1588 |
1589 |   Description   :  Move to the beginning of the next word in the field.
1590 |
1591 |   Return Values :  E_OK             - success
1592 |                    E_REQUEST_DENIED - there is no next word
1593 +--------------------------------------------------------------------------*/
1594 static int
1595 IFN_Next_Word(FORM *form)
1596 {
1597   FIELD *field = form->current;
1598   FIELD_CELL *bp = Address_Of_Current_Position_In_Buffer(form);
1599   FIELD_CELL *s;
1600   FIELD_CELL *t;
1601
1602   T((T_CALLED("IFN_Next_Word(%p)"), form));
1603
1604   /* We really need access to the data, so we have to synchronize */
1605   Synchronize_Buffer(form);
1606
1607   /* Go to the first whitespace after the current position (including
1608      current position). This is then the starting point to look for the
1609      next non-blank data */
1610   s = Get_First_Whitespace_Character(bp, Buffer_Length(field) -
1611                                      (int)(bp - field->buf));
1612
1613   /* Find the start of the next word */
1614   t = Get_Start_Of_Data(s, Buffer_Length(field) -
1615                         (int)(s - field->buf));
1616 #if !FRIENDLY_PREV_NEXT_WORD
1617   if (s == t)
1618     returnCode(E_REQUEST_DENIED);
1619   else
1620 #endif
1621     {
1622       Adjust_Cursor_Position(form, t);
1623       returnCode(E_OK);
1624     }
1625 }
1626
1627 /*---------------------------------------------------------------------------
1628 |   Facility      :  libnform
1629 |   Function      :  static int IFN_Previous_Word(FORM * form)
1630 |
1631 |   Description   :  Move to the beginning of the previous word in the field.
1632 |
1633 |   Return Values :  E_OK             - success
1634 |                    E_REQUEST_DENIED - there is no previous word
1635 +--------------------------------------------------------------------------*/
1636 static int
1637 IFN_Previous_Word(FORM *form)
1638 {
1639   FIELD *field = form->current;
1640   FIELD_CELL *bp = Address_Of_Current_Position_In_Buffer(form);
1641   FIELD_CELL *s;
1642   FIELD_CELL *t;
1643   bool again = FALSE;
1644
1645   T((T_CALLED("IFN_Previous_Word(%p)"), form));
1646
1647   /* We really need access to the data, so we have to synchronize */
1648   Synchronize_Buffer(form);
1649
1650   s = After_End_Of_Data(field->buf, (int)(bp - field->buf));
1651   /* s points now right after the last non-blank in the buffer before bp.
1652      If bp was in a word, s equals bp. In this case we must find the last
1653      whitespace in the buffer before bp and repeat the game to really find
1654      the previous word! */
1655   if (s == bp)
1656     again = TRUE;
1657
1658   /* And next call now goes backward to look for the last whitespace
1659      before that, pointing right after this, so it points to the begin
1660      of the previous word.
1661    */
1662   t = After_Last_Whitespace_Character(field->buf, (int)(s - field->buf));
1663 #if !FRIENDLY_PREV_NEXT_WORD
1664   if (s == t)
1665     returnCode(E_REQUEST_DENIED);
1666 #endif
1667   if (again)
1668     {
1669       /* and do it again, replacing bp by t */
1670       s = After_End_Of_Data(field->buf, (int)(t - field->buf));
1671       t = After_Last_Whitespace_Character(field->buf, (int)(s - field->buf));
1672 #if !FRIENDLY_PREV_NEXT_WORD
1673       if (s == t)
1674         returnCode(E_REQUEST_DENIED);
1675 #endif
1676     }
1677   Adjust_Cursor_Position(form, t);
1678   returnCode(E_OK);
1679 }
1680
1681 /*---------------------------------------------------------------------------
1682 |   Facility      :  libnform
1683 |   Function      :  static int IFN_Beginning_Of_Field(FORM * form)
1684 |
1685 |   Description   :  Place the cursor at the first non-pad character in
1686 |                    the field.
1687 |
1688 |   Return Values :  E_OK             - success
1689 +--------------------------------------------------------------------------*/
1690 static int
1691 IFN_Beginning_Of_Field(FORM *form)
1692 {
1693   FIELD *field = form->current;
1694
1695   T((T_CALLED("IFN_Beginning_Of_Field(%p)"), form));
1696   Synchronize_Buffer(form);
1697   Adjust_Cursor_Position(form,
1698                          Get_Start_Of_Data(field->buf, Buffer_Length(field)));
1699   returnCode(E_OK);
1700 }
1701
1702 /*---------------------------------------------------------------------------
1703 |   Facility      :  libnform
1704 |   Function      :  static int IFN_End_Of_Field(FORM * form)
1705 |
1706 |   Description   :  Place the cursor after the last non-pad character in
1707 |                    the field. If the field occupies the last position in
1708 |                    the buffer, the cursor is positioned on the last
1709 |                    character.
1710 |
1711 |   Return Values :  E_OK              - success
1712 +--------------------------------------------------------------------------*/
1713 static int
1714 IFN_End_Of_Field(FORM *form)
1715 {
1716   FIELD *field = form->current;
1717   FIELD_CELL *pos;
1718
1719   T((T_CALLED("IFN_End_Of_Field(%p)"), form));
1720   Synchronize_Buffer(form);
1721   pos = After_End_Of_Data(field->buf, Buffer_Length(field));
1722   if (pos == (field->buf + Buffer_Length(field)))
1723     pos--;
1724   Adjust_Cursor_Position(form, pos);
1725   returnCode(E_OK);
1726 }
1727
1728 /*---------------------------------------------------------------------------
1729 |   Facility      :  libnform
1730 |   Function      :  static int IFN_Beginning_Of_Line(FORM * form)
1731 |
1732 |   Description   :  Place the cursor on the first non-pad character in
1733 |                    the current line of the field.
1734 |
1735 |   Return Values :  E_OK         - success
1736 +--------------------------------------------------------------------------*/
1737 static int
1738 IFN_Beginning_Of_Line(FORM *form)
1739 {
1740   FIELD *field = form->current;
1741
1742   T((T_CALLED("IFN_Beginning_Of_Line(%p)"), form));
1743   Synchronize_Buffer(form);
1744   Adjust_Cursor_Position(form,
1745                          Get_Start_Of_Data(Address_Of_Current_Row_In_Buffer(form),
1746                                            field->dcols));
1747   returnCode(E_OK);
1748 }
1749
1750 /*---------------------------------------------------------------------------
1751 |   Facility      :  libnform
1752 |   Function      :  static int IFN_End_Of_Line(FORM * form)
1753 |
1754 |   Description   :  Place the cursor after the last non-pad character in the
1755 |                    current line of the field. If the field occupies the
1756 |                    last column in the line, the cursor is positioned on the
1757 |                    last character of the line.
1758 |
1759 |   Return Values :  E_OK        - success
1760 +--------------------------------------------------------------------------*/
1761 static int
1762 IFN_End_Of_Line(FORM *form)
1763 {
1764   FIELD *field = form->current;
1765   FIELD_CELL *pos;
1766   FIELD_CELL *bp;
1767
1768   T((T_CALLED("IFN_End_Of_Line(%p)"), form));
1769   Synchronize_Buffer(form);
1770   bp = Address_Of_Current_Row_In_Buffer(form);
1771   pos = After_End_Of_Data(bp, field->dcols);
1772   if (pos == (bp + field->dcols))
1773     pos--;
1774   Adjust_Cursor_Position(form, pos);
1775   returnCode(E_OK);
1776 }
1777
1778 /*---------------------------------------------------------------------------
1779 |   Facility      :  libnform
1780 |   Function      :  static int IFN_Left_Character(FORM * form)
1781 |
1782 |   Description   :  Move one character to the left in the current line.
1783 |                    This doesn't cycle.
1784 |
1785 |   Return Values :  E_OK             - success
1786 |                    E_REQUEST_DENIED - already in first column
1787 +--------------------------------------------------------------------------*/
1788 static int
1789 IFN_Left_Character(FORM *form)
1790 {
1791   int amount = myWCWIDTH(form->w, form->currow, form->curcol - 1);
1792   int oldcol = form->curcol;
1793
1794   T((T_CALLED("IFN_Left_Character(%p)"), form));
1795   if ((form->curcol -= amount) < 0)
1796     {
1797       form->curcol = oldcol;
1798       returnCode(E_REQUEST_DENIED);
1799     }
1800   returnCode(E_OK);
1801 }
1802
1803 /*---------------------------------------------------------------------------
1804 |   Facility      :  libnform
1805 |   Function      :  static int IFN_Right_Character(FORM * form)
1806 |
1807 |   Description   :  Move one character to the right in the current line.
1808 |                    This doesn't cycle.
1809 |
1810 |   Return Values :  E_OK              - success
1811 |                    E_REQUEST_DENIED  - already in last column
1812 +--------------------------------------------------------------------------*/
1813 static int
1814 IFN_Right_Character(FORM *form)
1815 {
1816   int amount = myWCWIDTH(form->w, form->currow, form->curcol);
1817   int oldcol = form->curcol;
1818
1819   T((T_CALLED("IFN_Right_Character(%p)"), form));
1820   if ((form->curcol += amount) >= form->current->dcols)
1821     {
1822 #if GROW_IF_NAVIGATE
1823       FIELD *field = form->current;
1824
1825       if (Single_Line_Field(field) && Field_Grown(field, 1))
1826         returnCode(E_OK);
1827 #endif
1828       form->curcol = oldcol;
1829       returnCode(E_REQUEST_DENIED);
1830     }
1831   returnCode(E_OK);
1832 }
1833
1834 /*---------------------------------------------------------------------------
1835 |   Facility      :  libnform
1836 |   Function      :  static int IFN_Up_Character(FORM * form)
1837 |
1838 |   Description   :  Move one line up. This doesn't cycle through the lines
1839 |                    of the field.
1840 |
1841 |   Return Values :  E_OK              - success
1842 |                    E_REQUEST_DENIED  - already in last column
1843 +--------------------------------------------------------------------------*/
1844 static int
1845 IFN_Up_Character(FORM *form)
1846 {
1847   T((T_CALLED("IFN_Up_Character(%p)"), form));
1848   if ((--(form->currow)) < 0)
1849     {
1850       form->currow++;
1851       returnCode(E_REQUEST_DENIED);
1852     }
1853   returnCode(E_OK);
1854 }
1855
1856 /*---------------------------------------------------------------------------
1857 |   Facility      :  libnform
1858 |   Function      :  static int IFN_Down_Character(FORM * form)
1859 |
1860 |   Description   :  Move one line down. This doesn't cycle through the
1861 |                    lines of the field.
1862 |
1863 |   Return Values :  E_OK              - success
1864 |                    E_REQUEST_DENIED  - already in last column
1865 +--------------------------------------------------------------------------*/
1866 static int
1867 IFN_Down_Character(FORM *form)
1868 {
1869   FIELD *field = form->current;
1870
1871   T((T_CALLED("IFN_Down_Character(%p)"), form));
1872   if ((++(form->currow)) == field->drows)
1873     {
1874 #if GROW_IF_NAVIGATE
1875       if (!Single_Line_Field(field) && Field_Grown(field, 1))
1876         returnCode(E_OK);
1877 #endif
1878       --(form->currow);
1879       returnCode(E_REQUEST_DENIED);
1880     }
1881   returnCode(E_OK);
1882 }
1883 /*----------------------------------------------------------------------------
1884   END of Intra-Field Navigation routines
1885   --------------------------------------------------------------------------*/
1886
1887 /*----------------------------------------------------------------------------
1888   Vertical scrolling helper routines
1889   --------------------------------------------------------------------------*/
1890
1891 /*---------------------------------------------------------------------------
1892 |   Facility      :  libnform
1893 |   Function      :  static int VSC_Generic(FORM *form, int nlines)
1894 |
1895 |   Description   :  Scroll multi-line field forward (nlines>0) or
1896 |                    backward (nlines<0) this many lines.
1897 |
1898 |   Return Values :  E_OK              - success
1899 |                    E_REQUEST_DENIED  - can't scroll
1900 +--------------------------------------------------------------------------*/
1901 static int
1902 VSC_Generic(FORM *form, int nlines)
1903 {
1904   FIELD *field = form->current;
1905   int res = E_REQUEST_DENIED;
1906   int rows_to_go = (nlines > 0 ? nlines : -nlines);
1907
1908   if (nlines > 0)
1909     {
1910       if ((rows_to_go + form->toprow) > (field->drows - field->rows))
1911         rows_to_go = (field->drows - field->rows - form->toprow);
1912
1913       if (rows_to_go > 0)
1914         {
1915           form->currow += rows_to_go;
1916           form->toprow += rows_to_go;
1917           res = E_OK;
1918         }
1919     }
1920   else
1921     {
1922       if (rows_to_go > form->toprow)
1923         rows_to_go = form->toprow;
1924
1925       if (rows_to_go > 0)
1926         {
1927           form->currow -= rows_to_go;
1928           form->toprow -= rows_to_go;
1929           res = E_OK;
1930         }
1931     }
1932   return (res);
1933 }
1934 /*----------------------------------------------------------------------------
1935   End of Vertical scrolling helper routines
1936   --------------------------------------------------------------------------*/
1937
1938 /*----------------------------------------------------------------------------
1939   Vertical scrolling routines
1940   --------------------------------------------------------------------------*/
1941
1942 /*---------------------------------------------------------------------------
1943 |   Facility      :  libnform
1944 |   Function      :  static int Vertical_Scrolling(
1945 |                                           int (* const fct) (FORM *),
1946 |                                           FORM * form)
1947 |
1948 |   Description   :  Performs the generic vertical scrolling routines.
1949 |                    This has to check for a multi-line field and to set
1950 |                    the _NEWTOP flag if scrolling really occurred.
1951 |
1952 |   Return Values :  Propagated error code from low-level driver calls
1953 +--------------------------------------------------------------------------*/
1954 static int
1955 Vertical_Scrolling(int (*const fct) (FORM *), FORM *form)
1956 {
1957   int res = E_REQUEST_DENIED;
1958
1959   if (!Single_Line_Field(form->current))
1960     {
1961       res = fct(form);
1962       if (res == E_OK)
1963         form->current->status |= _NEWTOP;
1964     }
1965   return (res);
1966 }
1967
1968 /*---------------------------------------------------------------------------
1969 |   Facility      :  libnform
1970 |   Function      :  static int VSC_Scroll_Line_Forward(FORM * form)
1971 |
1972 |   Description   :  Scroll multi-line field forward a line
1973 |
1974 |   Return Values :  E_OK                - success
1975 |                    E_REQUEST_DENIED    - no data ahead
1976 +--------------------------------------------------------------------------*/
1977 static int
1978 VSC_Scroll_Line_Forward(FORM *form)
1979 {
1980   T((T_CALLED("VSC_Scroll_Line_Forward(%p)"), form));
1981   returnCode(VSC_Generic(form, 1));
1982 }
1983
1984 /*---------------------------------------------------------------------------
1985 |   Facility      :  libnform
1986 |   Function      :  static int VSC_Scroll_Line_Backward(FORM * form)
1987 |
1988 |   Description   :  Scroll multi-line field backward a line
1989 |
1990 |   Return Values :  E_OK                - success
1991 |                    E_REQUEST_DENIED    - no data behind
1992 +--------------------------------------------------------------------------*/
1993 static int
1994 VSC_Scroll_Line_Backward(FORM *form)
1995 {
1996   T((T_CALLED("VSC_Scroll_Line_Backward(%p)"), form));
1997   returnCode(VSC_Generic(form, -1));
1998 }
1999
2000 /*---------------------------------------------------------------------------
2001 |   Facility      :  libnform
2002 |   Function      :  static int VSC_Scroll_Page_Forward(FORM * form)
2003 |
2004 |   Description   :  Scroll a multi-line field forward a page
2005 |
2006 |   Return Values :  E_OK              - success
2007 |                    E_REQUEST_DENIED  - no data ahead
2008 +--------------------------------------------------------------------------*/
2009 static int
2010 VSC_Scroll_Page_Forward(FORM *form)
2011 {
2012   T((T_CALLED("VSC_Scroll_Page_Forward(%p)"), form));
2013   returnCode(VSC_Generic(form, form->current->rows));
2014 }
2015
2016 /*---------------------------------------------------------------------------
2017 |   Facility      :  libnform
2018 |   Function      :  static int VSC_Scroll_Half_Page_Forward(FORM * form)
2019 |
2020 |   Description   :  Scroll a multi-line field forward half a page
2021 |
2022 |   Return Values :  E_OK              - success
2023 |                    E_REQUEST_DENIED  - no data ahead
2024 +--------------------------------------------------------------------------*/
2025 static int
2026 VSC_Scroll_Half_Page_Forward(FORM *form)
2027 {
2028   T((T_CALLED("VSC_Scroll_Half_Page_Forward(%p)"), form));
2029   returnCode(VSC_Generic(form, (form->current->rows + 1) / 2));
2030 }
2031
2032 /*---------------------------------------------------------------------------
2033 |   Facility      :  libnform
2034 |   Function      :  static int VSC_Scroll_Page_Backward(FORM * form)
2035 |
2036 |   Description   :  Scroll a multi-line field backward a page
2037 |
2038 |   Return Values :  E_OK              - success
2039 |                    E_REQUEST_DENIED  - no data behind
2040 +--------------------------------------------------------------------------*/
2041 static int
2042 VSC_Scroll_Page_Backward(FORM *form)
2043 {
2044   T((T_CALLED("VSC_Scroll_Page_Backward(%p)"), form));
2045   returnCode(VSC_Generic(form, -(form->current->rows)));
2046 }
2047
2048 /*---------------------------------------------------------------------------
2049 |   Facility      :  libnform
2050 |   Function      :  static int VSC_Scroll_Half_Page_Backward(FORM * form)
2051 |
2052 |   Description   :  Scroll a multi-line field backward half a page
2053 |
2054 |   Return Values :  E_OK              - success
2055 |                    E_REQUEST_DENIED  - no data behind
2056 +--------------------------------------------------------------------------*/
2057 static int
2058 VSC_Scroll_Half_Page_Backward(FORM *form)
2059 {
2060   T((T_CALLED("VSC_Scroll_Half_Page_Backward(%p)"), form));
2061   returnCode(VSC_Generic(form, -((form->current->rows + 1) / 2)));
2062 }
2063 /*----------------------------------------------------------------------------
2064   End of Vertical scrolling routines
2065   --------------------------------------------------------------------------*/
2066
2067 /*----------------------------------------------------------------------------
2068   Horizontal scrolling helper routines
2069   --------------------------------------------------------------------------*/
2070
2071 /*---------------------------------------------------------------------------
2072 |   Facility      :  libnform
2073 |   Function      :  static int HSC_Generic(FORM *form, int ncolumns)
2074 |
2075 |   Description   :  Scroll single-line field forward (ncolumns>0) or
2076 |                    backward (ncolumns<0) this many columns.
2077 |
2078 |   Return Values :  E_OK              - success
2079 |                    E_REQUEST_DENIED  - can't scroll
2080 +--------------------------------------------------------------------------*/
2081 static int
2082 HSC_Generic(FORM *form, int ncolumns)
2083 {
2084   FIELD *field = form->current;
2085   int res = E_REQUEST_DENIED;
2086   int cols_to_go = (ncolumns > 0 ? ncolumns : -ncolumns);
2087
2088   if (ncolumns > 0)
2089     {
2090       if ((cols_to_go + form->begincol) > (field->dcols - field->cols))
2091         cols_to_go = field->dcols - field->cols - form->begincol;
2092
2093       if (cols_to_go > 0)
2094         {
2095           form->curcol += cols_to_go;
2096           form->begincol += cols_to_go;
2097           res = E_OK;
2098         }
2099     }
2100   else
2101     {
2102       if (cols_to_go > form->begincol)
2103         cols_to_go = form->begincol;
2104
2105       if (cols_to_go > 0)
2106         {
2107           form->curcol -= cols_to_go;
2108           form->begincol -= cols_to_go;
2109           res = E_OK;
2110         }
2111     }
2112   return (res);
2113 }
2114 /*----------------------------------------------------------------------------
2115   End of Horizontal scrolling helper routines
2116   --------------------------------------------------------------------------*/
2117
2118 /*----------------------------------------------------------------------------
2119   Horizontal scrolling routines
2120   --------------------------------------------------------------------------*/
2121
2122 /*---------------------------------------------------------------------------
2123 |   Facility      :  libnform
2124 |   Function      :  static int Horizontal_Scrolling(
2125 |                                          int (* const fct) (FORM *),
2126 |                                          FORM * form)
2127 |
2128 |   Description   :  Performs the generic horizontal scrolling routines.
2129 |                    This has to check for a single-line field.
2130 |
2131 |   Return Values :  Propagated error code from low-level driver calls
2132 +--------------------------------------------------------------------------*/
2133 static int
2134 Horizontal_Scrolling(int (*const fct) (FORM *), FORM *form)
2135 {
2136   if (Single_Line_Field(form->current))
2137     return fct(form);
2138   else
2139     return (E_REQUEST_DENIED);
2140 }
2141
2142 /*---------------------------------------------------------------------------
2143 |   Facility      :  libnform
2144 |   Function      :  static int HSC_Scroll_Char_Forward(FORM * form)
2145 |
2146 |   Description   :  Scroll single-line field forward a character
2147 |
2148 |   Return Values :  E_OK                - success
2149 |                    E_REQUEST_DENIED    - no data ahead
2150 +--------------------------------------------------------------------------*/
2151 static int
2152 HSC_Scroll_Char_Forward(FORM *form)
2153 {
2154   T((T_CALLED("HSC_Scroll_Char_Forward(%p)"), form));
2155   returnCode(HSC_Generic(form, 1));
2156 }
2157
2158 /*---------------------------------------------------------------------------
2159 |   Facility      :  libnform
2160 |   Function      :  static int HSC_Scroll_Char_Backward(FORM * form)
2161 |
2162 |   Description   :  Scroll single-line field backward a character
2163 |
2164 |   Return Values :  E_OK                - success
2165 |                    E_REQUEST_DENIED    - no data behind
2166 +--------------------------------------------------------------------------*/
2167 static int
2168 HSC_Scroll_Char_Backward(FORM *form)
2169 {
2170   T((T_CALLED("HSC_Scroll_Char_Backward(%p)"), form));
2171   returnCode(HSC_Generic(form, -1));
2172 }
2173
2174 /*---------------------------------------------------------------------------
2175 |   Facility      :  libnform
2176 |   Function      :  static int HSC_Horizontal_Line_Forward(FORM* form)
2177 |
2178 |   Description   :  Scroll single-line field forward a line
2179 |
2180 |   Return Values :  E_OK                - success
2181 |                    E_REQUEST_DENIED    - no data ahead
2182 +--------------------------------------------------------------------------*/
2183 static int
2184 HSC_Horizontal_Line_Forward(FORM *form)
2185 {
2186   T((T_CALLED("HSC_Horizontal_Line_Forward(%p)"), form));
2187   returnCode(HSC_Generic(form, form->current->cols));
2188 }
2189
2190 /*---------------------------------------------------------------------------
2191 |   Facility      :  libnform
2192 |   Function      :  static int HSC_Horizontal_Half_Line_Forward(FORM* form)
2193 |
2194 |   Description   :  Scroll single-line field forward half a line
2195 |
2196 |   Return Values :  E_OK               - success
2197 |                    E_REQUEST_DENIED   - no data ahead
2198 +--------------------------------------------------------------------------*/
2199 static int
2200 HSC_Horizontal_Half_Line_Forward(FORM *form)
2201 {
2202   T((T_CALLED("HSC_Horizontal_Half_Line_Forward(%p)"), form));
2203   returnCode(HSC_Generic(form, (form->current->cols + 1) / 2));
2204 }
2205
2206 /*---------------------------------------------------------------------------
2207 |   Facility      :  libnform
2208 |   Function      :  static int HSC_Horizontal_Line_Backward(FORM* form)
2209 |
2210 |   Description   :  Scroll single-line field backward a line
2211 |
2212 |   Return Values :  E_OK                - success
2213 |                    E_REQUEST_DENIED    - no data behind
2214 +--------------------------------------------------------------------------*/
2215 static int
2216 HSC_Horizontal_Line_Backward(FORM *form)
2217 {
2218   T((T_CALLED("HSC_Horizontal_Line_Backward(%p)"), form));
2219   returnCode(HSC_Generic(form, -(form->current->cols)));
2220 }
2221
2222 /*---------------------------------------------------------------------------
2223 |   Facility      :  libnform
2224 |   Function      :  static int HSC_Horizontal_Half_Line_Backward(FORM* form)
2225 |
2226 |   Description   :  Scroll single-line field backward half a line
2227 |
2228 |   Return Values :  E_OK                - success
2229 |                    E_REQUEST_DENIED    - no data behind
2230 +--------------------------------------------------------------------------*/
2231 static int
2232 HSC_Horizontal_Half_Line_Backward(FORM *form)
2233 {
2234   T((T_CALLED("HSC_Horizontal_Half_Line_Backward(%p)"), form));
2235   returnCode(HSC_Generic(form, -((form->current->cols + 1) / 2)));
2236 }
2237
2238 /*----------------------------------------------------------------------------
2239   End of Horizontal scrolling routines
2240   --------------------------------------------------------------------------*/
2241
2242 /*----------------------------------------------------------------------------
2243   Helper routines for Field Editing
2244   --------------------------------------------------------------------------*/
2245
2246 /*---------------------------------------------------------------------------
2247 |   Facility      :  libnform
2248 |   Function      :  static bool Is_There_Room_For_A_Line(FORM * form)
2249 |
2250 |   Description   :  Check whether or not there is enough room in the
2251 |                    buffer to enter a whole line.
2252 |
2253 |   Return Values :  TRUE   - there is enough space
2254 |                    FALSE  - there is not enough space
2255 +--------------------------------------------------------------------------*/
2256 NCURSES_INLINE static bool
2257 Is_There_Room_For_A_Line(FORM *form)
2258 {
2259   FIELD *field = form->current;
2260   FIELD_CELL *begin_of_last_line, *s;
2261
2262   Synchronize_Buffer(form);
2263   begin_of_last_line = Address_Of_Row_In_Buffer(field, (field->drows - 1));
2264   s = After_End_Of_Data(begin_of_last_line, field->dcols);
2265   return ((s == begin_of_last_line) ? TRUE : FALSE);
2266 }
2267
2268 /*---------------------------------------------------------------------------
2269 |   Facility      :  libnform
2270 |   Function      :  static bool Is_There_Room_For_A_Char_In_Line(FORM * form)
2271 |
2272 |   Description   :  Checks whether or not there is room for a new character
2273 |                    in the current line.
2274 |
2275 |   Return Values :  TRUE    - there is room
2276 |                    FALSE   - there is not enough room (line full)
2277 +--------------------------------------------------------------------------*/
2278 NCURSES_INLINE static bool
2279 Is_There_Room_For_A_Char_In_Line(FORM *form)
2280 {
2281   int last_char_in_line;
2282
2283   wmove(form->w, form->currow, form->current->dcols - 1);
2284   last_char_in_line = (int)(winch(form->w) & A_CHARTEXT);
2285   wmove(form->w, form->currow, form->curcol);
2286   return (((last_char_in_line == form->current->pad) ||
2287            is_blank(last_char_in_line)) ? TRUE : FALSE);
2288 }
2289
2290 #define There_Is_No_Room_For_A_Char_In_Line(f) \
2291   !Is_There_Room_For_A_Char_In_Line(f)
2292
2293 /*---------------------------------------------------------------------------
2294 |   Facility      :  libnform
2295 |   Function      :  static int Insert_String(
2296 |                                             FORM * form,
2297 |                                             int row,
2298 |                                             char *txt,
2299 |                                             int  len )
2300 |
2301 |   Description   :  Insert the 'len' characters beginning at pointer 'txt'
2302 |                    into the 'row' of the 'form'. The insertion occurs
2303 |                    on the beginning of the row, all other characters are
2304 |                    moved to the right. After the text a pad character will
2305 |                    be inserted to separate the text from the rest. If
2306 |                    necessary the insertion moves characters on the next
2307 |                    line to make place for the requested insertion string.
2308 |
2309 |   Return Values :  E_OK              - success
2310 |                    E_REQUEST_DENIED  -
2311 |                    E_SYSTEM_ERROR    - system error
2312 +--------------------------------------------------------------------------*/
2313 static int
2314 Insert_String(FORM *form, int row, FIELD_CELL *txt, int len)
2315 {
2316   FIELD *field = form->current;
2317   FIELD_CELL *bp = Address_Of_Row_In_Buffer(field, row);
2318   int datalen = (int)(After_End_Of_Data(bp, field->dcols) - bp);
2319   int freelen = field->dcols - datalen;
2320   int requiredlen = len + 1;
2321   FIELD_CELL *split;
2322   int result = E_REQUEST_DENIED;
2323
2324   if (freelen >= requiredlen)
2325     {
2326       wmove(form->w, row, 0);
2327       myINSNSTR(form->w, txt, len);
2328       wmove(form->w, row, len);
2329       myINSNSTR(form->w, &myBLANK, 1);
2330       return E_OK;
2331     }
2332   else
2333     {
2334       /* we have to move characters on the next line. If we are on the
2335          last line this may work, if the field is growable */
2336       if ((row == (field->drows - 1)) && Growable(field))
2337         {
2338           if (!Field_Grown(field, 1))
2339             return (E_SYSTEM_ERROR);
2340           /* !!!Side-Effect : might be changed due to growth!!! */
2341           bp = Address_Of_Row_In_Buffer(field, row);
2342         }
2343
2344       if (row < (field->drows - 1))
2345         {
2346           split =
2347             After_Last_Whitespace_Character(bp,
2348                                             (int)(Get_Start_Of_Data(bp
2349                                                                     + field->dcols
2350                                                                     - requiredlen,
2351                                                                     requiredlen)
2352                                                   - bp));
2353           /* split points now to the first character of the portion of the
2354              line that must be moved to the next line */
2355           datalen = (int)(split - bp);  /* + freelen has to stay on this line   */
2356           freelen = field->dcols - (datalen + freelen);         /* for the next line */
2357
2358           if ((result = Insert_String(form, row + 1, split, freelen)) == E_OK)
2359             {
2360               wmove(form->w, row, datalen);
2361               wclrtoeol(form->w);
2362               wmove(form->w, row, 0);
2363               myINSNSTR(form->w, txt, len);
2364               wmove(form->w, row, len);
2365               myINSNSTR(form->w, &myBLANK, 1);
2366               return E_OK;
2367             }
2368         }
2369       return (result);
2370     }
2371 }
2372
2373 /*---------------------------------------------------------------------------
2374 |   Facility      :  libnform
2375 |   Function      :  static int Wrapping_Not_Necessary_Or_Wrapping_Ok(
2376 |                                             FORM * form)
2377 |
2378 |   Description   :  If a character has been entered into a field, it may
2379 |                    be that wrapping has to occur. This routine checks
2380 |                    whether or not wrapping is required and if so, performs
2381 |                    the wrapping.
2382 |
2383 |   Return Values :  E_OK              - no wrapping required or wrapping
2384 |                                        was successful
2385 |                    E_REQUEST_DENIED  -
2386 |                    E_SYSTEM_ERROR    - some system error
2387 +--------------------------------------------------------------------------*/
2388 static int
2389 Wrapping_Not_Necessary_Or_Wrapping_Ok(FORM *form)
2390 {
2391   FIELD *field = form->current;
2392   int result = E_REQUEST_DENIED;
2393   bool Last_Row = ((field->drows - 1) == form->currow);
2394
2395   if ((field->opts & O_WRAP) && /* wrapping wanted     */
2396       (!Single_Line_Field(field)) &&    /* must be multi-line  */
2397       (There_Is_No_Room_For_A_Char_In_Line(form)) &&    /* line is full        */
2398       (!Last_Row || Growable(field)))   /* there are more lines */
2399     {
2400       FIELD_CELL *bp;
2401       FIELD_CELL *split;
2402       int chars_to_be_wrapped;
2403       int chars_to_remain_on_line;
2404
2405       if (Last_Row)
2406         {
2407           /* the above logic already ensures, that in this case the field
2408              is growable */
2409           if (!Field_Grown(field, 1))
2410             return E_SYSTEM_ERROR;
2411         }
2412       bp = Address_Of_Current_Row_In_Buffer(form);
2413       Window_To_Buffer(form->w, field);
2414       split = After_Last_Whitespace_Character(bp, field->dcols);
2415       /* split points to the first character of the sequence to be brought
2416          on the next line */
2417       chars_to_remain_on_line = (int)(split - bp);
2418       chars_to_be_wrapped = field->dcols - chars_to_remain_on_line;
2419       if (chars_to_remain_on_line > 0)
2420         {
2421           if ((result = Insert_String(form, form->currow + 1, split,
2422                                       chars_to_be_wrapped)) == E_OK)
2423             {
2424               wmove(form->w, form->currow, chars_to_remain_on_line);
2425               wclrtoeol(form->w);
2426               if (form->curcol >= chars_to_remain_on_line)
2427                 {
2428                   form->currow++;
2429                   form->curcol -= chars_to_remain_on_line;
2430                 }
2431               return E_OK;
2432             }
2433         }
2434       else
2435         return E_OK;
2436       if (result != E_OK)
2437         {
2438           DeleteChar(form);
2439           Window_To_Buffer(form->w, field);
2440           result = E_REQUEST_DENIED;
2441         }
2442     }
2443   else
2444     result = E_OK;              /* wrapping was not necessary */
2445   return (result);
2446 }
2447
2448 /*----------------------------------------------------------------------------
2449   Field Editing routines
2450   --------------------------------------------------------------------------*/
2451
2452 /*---------------------------------------------------------------------------
2453 |   Facility      :  libnform
2454 |   Function      :  static int Field_Editing(
2455 |                                    int (* const fct) (FORM *),
2456 |                                    FORM * form)
2457 |
2458 |   Description   :  Generic routine for field editing requests. The driver
2459 |                    routines are only called for editable fields, the
2460 |                    _WINDOW_MODIFIED flag is set if editing occurred.
2461 |                    This is somewhat special due to the overload semantics
2462 |                    of the NEW_LINE and DEL_PREV requests.
2463 |
2464 |   Return Values :  Error code from low level drivers.
2465 +--------------------------------------------------------------------------*/
2466 static int
2467 Field_Editing(int (*const fct) (FORM *), FORM *form)
2468 {
2469   int res = E_REQUEST_DENIED;
2470
2471   /* We have to deal here with the specific case of the overloaded
2472      behavior of New_Line and Delete_Previous requests.
2473      They may end up in navigational requests if we are on the first
2474      character in a field. But navigation is also allowed on non-
2475      editable fields.
2476    */
2477   if ((fct == FE_Delete_Previous) &&
2478       (form->opts & O_BS_OVERLOAD) &&
2479       First_Position_In_Current_Field(form))
2480     {
2481       res = Inter_Field_Navigation(FN_Previous_Field, form);
2482     }
2483   else
2484     {
2485       if (fct == FE_New_Line)
2486         {
2487           if ((form->opts & O_NL_OVERLOAD) &&
2488               First_Position_In_Current_Field(form))
2489             {
2490               res = Inter_Field_Navigation(FN_Next_Field, form);
2491             }
2492           else
2493             /* FE_New_Line deals itself with the _WINDOW_MODIFIED flag */
2494             res = fct(form);
2495         }
2496       else
2497         {
2498           /* From now on, everything must be editable */
2499           if (form->current->opts & O_EDIT)
2500             {
2501               res = fct(form);
2502               if (res == E_OK)
2503                 form->status |= _WINDOW_MODIFIED;
2504             }
2505         }
2506     }
2507   return res;
2508 }
2509
2510 /*---------------------------------------------------------------------------
2511 |   Facility      :  libnform
2512 |   Function      :  static int FE_New_Line(FORM * form)
2513 |
2514 |   Description   :  Perform a new line request. This is rather complex
2515 |                    compared to other routines in this code due to the
2516 |                    rather difficult to understand description in the
2517 |                    manuals.
2518 |
2519 |   Return Values :  E_OK               - success
2520 |                    E_REQUEST_DENIED   - new line not allowed
2521 |                    E_SYSTEM_ERROR     - system error
2522 +--------------------------------------------------------------------------*/
2523 static int
2524 FE_New_Line(FORM *form)
2525 {
2526   FIELD *field = form->current;
2527   FIELD_CELL *bp, *t;
2528   bool Last_Row = ((field->drows - 1) == form->currow);
2529
2530   T((T_CALLED("FE_New_Line(%p)"), form));
2531   if (form->status & _OVLMODE)
2532     {
2533       if (Last_Row &&
2534           (!(Growable(field) && !Single_Line_Field(field))))
2535         {
2536           if (!(form->opts & O_NL_OVERLOAD))
2537             returnCode(E_REQUEST_DENIED);
2538           wmove(form->w, form->currow, form->curcol);
2539           wclrtoeol(form->w);
2540           /* we have to set this here, although it is also
2541              handled in the generic routine. The reason is,
2542              that FN_Next_Field may fail, but the form is
2543              definitively changed */
2544           form->status |= _WINDOW_MODIFIED;
2545           returnCode(Inter_Field_Navigation(FN_Next_Field, form));
2546         }
2547       else
2548         {
2549           if (Last_Row && !Field_Grown(field, 1))
2550             {
2551               /* N.B.: due to the logic in the 'if', LastRow==TRUE
2552                  means here that the field is growable and not
2553                  a single-line field */
2554               returnCode(E_SYSTEM_ERROR);
2555             }
2556           wmove(form->w, form->currow, form->curcol);
2557           wclrtoeol(form->w);
2558           form->currow++;
2559           form->curcol = 0;
2560           form->status |= _WINDOW_MODIFIED;
2561           returnCode(E_OK);
2562         }
2563     }
2564   else
2565     {
2566       /* Insert Mode */
2567       if (Last_Row &&
2568           !(Growable(field) && !Single_Line_Field(field)))
2569         {
2570           if (!(form->opts & O_NL_OVERLOAD))
2571             returnCode(E_REQUEST_DENIED);
2572           returnCode(Inter_Field_Navigation(FN_Next_Field, form));
2573         }
2574       else
2575         {
2576           bool May_Do_It = !Last_Row && Is_There_Room_For_A_Line(form);
2577
2578           if (!(May_Do_It || Growable(field)))
2579             returnCode(E_REQUEST_DENIED);
2580           if (!May_Do_It && !Field_Grown(field, 1))
2581             returnCode(E_SYSTEM_ERROR);
2582
2583           bp = Address_Of_Current_Position_In_Buffer(form);
2584           t = After_End_Of_Data(bp, field->dcols - form->curcol);
2585           wmove(form->w, form->currow, form->curcol);
2586           wclrtoeol(form->w);
2587           form->currow++;
2588           form->curcol = 0;
2589           wmove(form->w, form->currow, form->curcol);
2590           winsertln(form->w);
2591           myADDNSTR(form->w, bp, (int)(t - bp));
2592           form->status |= _WINDOW_MODIFIED;
2593           returnCode(E_OK);
2594         }
2595     }
2596 }
2597
2598 /*---------------------------------------------------------------------------
2599 |   Facility      :  libnform
2600 |   Function      :  static int FE_Insert_Character(FORM * form)
2601 |
2602 |   Description   :  Insert blank character at the cursor position
2603 |
2604 |   Return Values :  E_OK
2605 |                    E_REQUEST_DENIED
2606 +--------------------------------------------------------------------------*/
2607 static int
2608 FE_Insert_Character(FORM *form)
2609 {
2610   FIELD *field = form->current;
2611   int result = E_REQUEST_DENIED;
2612
2613   T((T_CALLED("FE_Insert_Character(%p)"), form));
2614   if (Check_Char(field->type, (int)C_BLANK, (TypeArgument *)(field->arg)))
2615     {
2616       bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);
2617
2618       if (There_Is_Room ||
2619           ((Single_Line_Field(field) && Growable(field))))
2620         {
2621           if (!There_Is_Room && !Field_Grown(field, 1))
2622             result = E_SYSTEM_ERROR;
2623           else
2624             {
2625               winsch(form->w, (chtype)C_BLANK);
2626               result = Wrapping_Not_Necessary_Or_Wrapping_Ok(form);
2627             }
2628         }
2629     }
2630   returnCode(result);
2631 }
2632
2633 /*---------------------------------------------------------------------------
2634 |   Facility      :  libnform
2635 |   Function      :  static int FE_Insert_Line(FORM * form)
2636 |
2637 |   Description   :  Insert a blank line at the cursor position
2638 |
2639 |   Return Values :  E_OK               - success
2640 |                    E_REQUEST_DENIED   - line can not be inserted
2641 +--------------------------------------------------------------------------*/
2642 static int
2643 FE_Insert_Line(FORM *form)
2644 {
2645   FIELD *field = form->current;
2646   int result = E_REQUEST_DENIED;
2647
2648   T((T_CALLED("FE_Insert_Line(%p)"), form));
2649   if (Check_Char(field->type, (int)C_BLANK, (TypeArgument *)(field->arg)))
2650     {
2651       bool Maybe_Done = (form->currow != (field->drows - 1)) &&
2652       Is_There_Room_For_A_Line(form);
2653
2654       if (!Single_Line_Field(field) &&
2655           (Maybe_Done || Growable(field)))
2656         {
2657           if (!Maybe_Done && !Field_Grown(field, 1))
2658             result = E_SYSTEM_ERROR;
2659           else
2660             {
2661               form->curcol = 0;
2662               winsertln(form->w);
2663               result = E_OK;
2664             }
2665         }
2666     }
2667   returnCode(result);
2668 }
2669
2670 /*---------------------------------------------------------------------------
2671 |   Facility      :  libnform
2672 |   Function      :  static int FE_Delete_Character(FORM * form)
2673 |
2674 |   Description   :  Delete character at the cursor position
2675 |
2676 |   Return Values :  E_OK    - success
2677 +--------------------------------------------------------------------------*/
2678 static int
2679 FE_Delete_Character(FORM *form)
2680 {
2681   T((T_CALLED("FE_Delete_Character(%p)"), form));
2682   DeleteChar(form);
2683   returnCode(E_OK);
2684 }
2685
2686 /*---------------------------------------------------------------------------
2687 |   Facility      :  libnform
2688 |   Function      :  static int FE_Delete_Previous(FORM * form)
2689 |
2690 |   Description   :  Delete character before cursor. Again this is a rather
2691 |                    difficult piece compared to others due to the overloading
2692 |                    semantics of backspace.
2693 |                    N.B.: The case of overloaded BS on first field position
2694 |                          is already handled in the generic routine.
2695 |
2696 |   Return Values :  E_OK                - success
2697 |                    E_REQUEST_DENIED    - Character can't be deleted
2698 +--------------------------------------------------------------------------*/
2699 static int
2700 FE_Delete_Previous(FORM *form)
2701 {
2702   FIELD *field = form->current;
2703
2704   T((T_CALLED("FE_Delete_Previous(%p)"), form));
2705   if (First_Position_In_Current_Field(form))
2706     returnCode(E_REQUEST_DENIED);
2707
2708   if ((--(form->curcol)) < 0)
2709     {
2710       FIELD_CELL *this_line, *prev_line, *prev_end, *this_end;
2711       int this_row = form->currow;
2712
2713       form->curcol++;
2714       if (form->status & _OVLMODE)
2715         returnCode(E_REQUEST_DENIED);
2716
2717       prev_line = Address_Of_Row_In_Buffer(field, (form->currow - 1));
2718       this_line = Address_Of_Row_In_Buffer(field, (form->currow));
2719       Synchronize_Buffer(form);
2720       prev_end = After_End_Of_Data(prev_line, field->dcols);
2721       this_end = After_End_Of_Data(this_line, field->dcols);
2722       if ((int)(this_end - this_line) >
2723           (field->cols - (int)(prev_end - prev_line)))
2724         returnCode(E_REQUEST_DENIED);
2725       wmove(form->w, form->currow, form->curcol);
2726       wdeleteln(form->w);
2727       Adjust_Cursor_Position(form, prev_end);
2728       /*
2729        * If we did not really move to the previous line, help the user a
2730        * little.  It is however a little inconsistent.  Normally, when
2731        * backspacing around the point where text wraps to a new line in a
2732        * multi-line form, we absorb one keystroke for the wrapping point.  That
2733        * is consistent with SVr4 forms.  However, SVr4 does not allow typing
2734        * into the last column of the field, and requires the user to enter a
2735        * newline to move to the next line.  Therefore it can consistently eat
2736        * that keystroke.  Since ncurses allows the last column, it wraps
2737        * automatically (given the proper options).  But we cannot eat the
2738        * keystroke to back over the wrapping point, since that would put the
2739        * cursor past the end of the form field.  In this case, just delete the
2740        * character at the end of the field.
2741        */
2742       if (form->currow == this_row && this_row > 0)
2743         {
2744           form->currow -= 1;
2745           form->curcol = field->dcols - 1;
2746           DeleteChar(form);
2747         }
2748       else
2749         {
2750           wmove(form->w, form->currow, form->curcol);
2751           myADDNSTR(form->w, this_line, (int)(this_end - this_line));
2752         }
2753     }
2754   else
2755     {
2756       DeleteChar(form);
2757     }
2758   returnCode(E_OK);
2759 }
2760
2761 /*---------------------------------------------------------------------------
2762 |   Facility      :  libnform
2763 |   Function      :  static int FE_Delete_Line(FORM * form)
2764 |
2765 |   Description   :  Delete line at cursor position.
2766 |
2767 |   Return Values :  E_OK  - success
2768 +--------------------------------------------------------------------------*/
2769 static int
2770 FE_Delete_Line(FORM *form)
2771 {
2772   T((T_CALLED("FE_Delete_Line(%p)"), form));
2773   form->curcol = 0;
2774   wdeleteln(form->w);
2775   returnCode(E_OK);
2776 }
2777
2778 /*---------------------------------------------------------------------------
2779 |   Facility      :  libnform
2780 |   Function      :  static int FE_Delete_Word(FORM * form)
2781 |
2782 |   Description   :  Delete word at cursor position
2783 |
2784 |   Return Values :  E_OK               - success
2785 |                    E_REQUEST_DENIED   - failure
2786 +--------------------------------------------------------------------------*/
2787 static int
2788 FE_Delete_Word(FORM *form)
2789 {
2790   FIELD *field = form->current;
2791   FIELD_CELL *bp = Address_Of_Current_Row_In_Buffer(form);
2792   FIELD_CELL *ep = bp + field->dcols;
2793   FIELD_CELL *cp = bp + form->curcol;
2794   FIELD_CELL *s;
2795
2796   T((T_CALLED("FE_Delete_Word(%p)"), form));
2797   Synchronize_Buffer(form);
2798   if (ISBLANK(*cp))
2799     returnCode(E_REQUEST_DENIED);       /* not in word */
2800
2801   /* move cursor to begin of word and erase to end of screen-line */
2802   Adjust_Cursor_Position(form,
2803                          After_Last_Whitespace_Character(bp, form->curcol));
2804   wmove(form->w, form->currow, form->curcol);
2805   wclrtoeol(form->w);
2806
2807   /* skip over word in buffer */
2808   s = Get_First_Whitespace_Character(cp, (int)(ep - cp));
2809   /* to begin of next word    */
2810   s = Get_Start_Of_Data(s, (int)(ep - s));
2811   if ((s != cp) && !ISBLANK(*s))
2812     {
2813       /* copy remaining line to window */
2814       myADDNSTR(form->w, s, (int)(s - After_End_Of_Data(s, (int)(ep - s))));
2815     }
2816   returnCode(E_OK);
2817 }
2818
2819 /*---------------------------------------------------------------------------
2820 |   Facility      :  libnform
2821 |   Function      :  static int FE_Clear_To_End_Of_Line(FORM * form)
2822 |
2823 |   Description   :  Clear to end of current line.
2824 |
2825 |   Return Values :  E_OK   - success
2826 +--------------------------------------------------------------------------*/
2827 static int
2828 FE_Clear_To_End_Of_Line(FORM *form)
2829 {
2830   T((T_CALLED("FE_Clear_To_End_Of_Line(%p)"), form));
2831   wmove(form->w, form->currow, form->curcol);
2832   wclrtoeol(form->w);
2833   returnCode(E_OK);
2834 }
2835
2836 /*---------------------------------------------------------------------------
2837 |   Facility      :  libnform
2838 |   Function      :  static int FE_Clear_To_End_Of_Field(FORM * form)
2839 |
2840 |   Description   :  Clear to end of field.
2841 |
2842 |   Return Values :  E_OK   - success
2843 +--------------------------------------------------------------------------*/
2844 static int
2845 FE_Clear_To_End_Of_Field(FORM *form)
2846 {
2847   T((T_CALLED("FE_Clear_To_End_Of_Field(%p)"), form));
2848   wmove(form->w, form->currow, form->curcol);
2849   wclrtobot(form->w);
2850   returnCode(E_OK);
2851 }
2852
2853 /*---------------------------------------------------------------------------
2854 |   Facility      :  libnform
2855 |   Function      :  static int FE_Clear_Field(FORM * form)
2856 |
2857 |   Description   :  Clear entire field.
2858 |
2859 |   Return Values :  E_OK   - success
2860 +--------------------------------------------------------------------------*/
2861 static int
2862 FE_Clear_Field(FORM *form)
2863 {
2864   T((T_CALLED("FE_Clear_Field(%p)"), form));
2865   form->currow = form->curcol = 0;
2866   werase(form->w);
2867   returnCode(E_OK);
2868 }
2869 /*----------------------------------------------------------------------------
2870   END of Field Editing routines
2871   --------------------------------------------------------------------------*/
2872
2873 /*----------------------------------------------------------------------------
2874   Edit Mode routines
2875   --------------------------------------------------------------------------*/
2876
2877 /*---------------------------------------------------------------------------
2878 |   Facility      :  libnform
2879 |   Function      :  static int EM_Overlay_Mode(FORM * form)
2880 |
2881 |   Description   :  Switch to overlay mode.
2882 |
2883 |   Return Values :  E_OK   - success
2884 +--------------------------------------------------------------------------*/
2885 static int
2886 EM_Overlay_Mode(FORM *form)
2887 {
2888   T((T_CALLED("EM_Overlay_Mode(%p)"), form));
2889   form->status |= _OVLMODE;
2890   returnCode(E_OK);
2891 }
2892
2893 /*---------------------------------------------------------------------------
2894 |   Facility      :  libnform
2895 |   Function      :  static int EM_Insert_Mode(FORM * form)
2896 |
2897 |   Description   :  Switch to insert mode
2898 |
2899 |   Return Values :  E_OK   - success
2900 +--------------------------------------------------------------------------*/
2901 static int
2902 EM_Insert_Mode(FORM *form)
2903 {
2904   T((T_CALLED("EM_Insert_Mode(%p)"), form));
2905   form->status &= ~_OVLMODE;
2906   returnCode(E_OK);
2907 }
2908
2909 /*----------------------------------------------------------------------------
2910   END of Edit Mode routines
2911   --------------------------------------------------------------------------*/
2912
2913 /*----------------------------------------------------------------------------
2914   Helper routines for Choice Requests
2915   --------------------------------------------------------------------------*/
2916
2917 /*---------------------------------------------------------------------------
2918 |   Facility      :  libnform
2919 |   Function      :  static bool Next_Choice(
2920 |                                            FIELDTYPE * typ,
2921 |                                            FIELD * field,
2922 |                                            TypeArgument *argp)
2923 |
2924 |   Description   :  Get the next field choice. For linked types this is
2925 |                    done recursively.
2926 |
2927 |   Return Values :  TRUE    - next choice successfully retrieved
2928 |                    FALSE   - couldn't retrieve next choice
2929 +--------------------------------------------------------------------------*/
2930 static bool
2931 Next_Choice(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
2932 {
2933   if (!typ || !(typ->status & _HAS_CHOICE))
2934     return FALSE;
2935
2936   if (typ->status & _LINKED_TYPE)
2937     {
2938       assert(argp);
2939       return (
2940                Next_Choice(typ->left, field, argp->left) ||
2941                Next_Choice(typ->right, field, argp->right));
2942     }
2943   else
2944     {
2945       assert(typ->next);
2946       return typ->next(field, (void *)argp);
2947     }
2948 }
2949
2950 /*---------------------------------------------------------------------------
2951 |   Facility      :  libnform
2952 |   Function      :  static bool Previous_Choice(
2953 |                                                FIELDTYPE * typ,
2954 |                                                FIELD * field,
2955 |                                                TypeArgument *argp)
2956 |
2957 |   Description   :  Get the previous field choice. For linked types this
2958 |                    is done recursively.
2959 |
2960 |   Return Values :  TRUE    - previous choice successfully retrieved
2961 |                    FALSE   - couldn't retrieve previous choice
2962 +--------------------------------------------------------------------------*/
2963 static bool
2964 Previous_Choice(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
2965 {
2966   if (!typ || !(typ->status & _HAS_CHOICE))
2967     return FALSE;
2968
2969   if (typ->status & _LINKED_TYPE)
2970     {
2971       assert(argp);
2972       return (
2973                Previous_Choice(typ->left, field, argp->left) ||
2974                Previous_Choice(typ->right, field, argp->right));
2975     }
2976   else
2977     {
2978       assert(typ->prev);
2979       return typ->prev(field, (void *)argp);
2980     }
2981 }
2982 /*----------------------------------------------------------------------------
2983   End of Helper routines for Choice Requests
2984   --------------------------------------------------------------------------*/
2985
2986 /*----------------------------------------------------------------------------
2987   Routines for Choice Requests
2988   --------------------------------------------------------------------------*/
2989
2990 /*---------------------------------------------------------------------------
2991 |   Facility      :  libnform
2992 |   Function      :  static int CR_Next_Choice(FORM * form)
2993 |
2994 |   Description   :  Get the next field choice.
2995 |
2996 |   Return Values :  E_OK              - success
2997 |                    E_REQUEST_DENIED  - next choice couldn't be retrieved
2998 +--------------------------------------------------------------------------*/
2999 static int
3000 CR_Next_Choice(FORM *form)
3001 {
3002   FIELD *field = form->current;
3003
3004   T((T_CALLED("CR_Next_Choice(%p)"), form));
3005   Synchronize_Buffer(form);
3006   returnCode((Next_Choice(field->type, field, (TypeArgument *)(field->arg)))
3007              ? E_OK
3008              : E_REQUEST_DENIED);
3009 }
3010
3011 /*---------------------------------------------------------------------------
3012 |   Facility      :  libnform
3013 |   Function      :  static int CR_Previous_Choice(FORM * form)
3014 |
3015 |   Description   :  Get the previous field choice.
3016 |
3017 |   Return Values :  E_OK              - success
3018 |                    E_REQUEST_DENIED  - prev. choice couldn't be retrieved
3019 +--------------------------------------------------------------------------*/
3020 static int
3021 CR_Previous_Choice(FORM *form)
3022 {
3023   FIELD *field = form->current;
3024
3025   T((T_CALLED("CR_Previous_Choice(%p)"), form));
3026   Synchronize_Buffer(form);
3027   returnCode((Previous_Choice(field->type, field, (TypeArgument *)(field->arg)))
3028              ? E_OK
3029              : E_REQUEST_DENIED);
3030 }
3031 /*----------------------------------------------------------------------------
3032   End of Routines for Choice Requests
3033   --------------------------------------------------------------------------*/
3034
3035 /*----------------------------------------------------------------------------
3036   Helper routines for Field Validations.
3037   --------------------------------------------------------------------------*/
3038
3039 /*---------------------------------------------------------------------------
3040 |   Facility      :  libnform
3041 |   Function      :  static bool Check_Field(
3042 |                                            FIELDTYPE * typ,
3043 |                                            FIELD * field,
3044 |                                            TypeArgument * argp)
3045 |
3046 |   Description   :  Check the field according to its fieldtype and its
3047 |                    actual arguments. For linked fieldtypes this is done
3048 |                    recursively.
3049 |
3050 |   Return Values :  TRUE       - field is valid
3051 |                    FALSE      - field is invalid.
3052 +--------------------------------------------------------------------------*/
3053 static bool
3054 Check_Field(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
3055 {
3056   if (typ)
3057     {
3058       if (field->opts & O_NULLOK)
3059         {
3060           FIELD_CELL *bp = field->buf;
3061
3062           assert(bp);
3063           while (ISBLANK(*bp))
3064             {
3065               bp++;
3066             }
3067           if (CharOf(*bp) == 0)
3068             return TRUE;
3069         }
3070
3071       if (typ->status & _LINKED_TYPE)
3072         {
3073           assert(argp);
3074           return (
3075                    Check_Field(typ->left, field, argp->left) ||
3076                    Check_Field(typ->right, field, argp->right));
3077         }
3078       else
3079         {
3080           if (typ->fcheck)
3081             return typ->fcheck(field, (void *)argp);
3082         }
3083     }
3084   return TRUE;
3085 }
3086
3087 /*---------------------------------------------------------------------------
3088 |   Facility      :  libnform
3089 |   Function      :  bool _nc_Internal_Validation(FORM * form )
3090 |
3091 |   Description   :  Validate the current field of the form.
3092 |
3093 |   Return Values :  TRUE  - field is valid
3094 |                    FALSE - field is invalid
3095 +--------------------------------------------------------------------------*/
3096 NCURSES_EXPORT(bool)
3097 _nc_Internal_Validation(FORM *form)
3098 {
3099   FIELD *field;
3100
3101   field = form->current;
3102
3103   Synchronize_Buffer(form);
3104   if ((form->status & _FCHECK_REQUIRED) ||
3105       (!(field->opts & O_PASSOK)))
3106     {
3107       if (!Check_Field(field->type, field, (TypeArgument *)(field->arg)))
3108         return FALSE;
3109       form->status &= ~_FCHECK_REQUIRED;
3110       field->status |= _CHANGED;
3111       Synchronize_Linked_Fields(field);
3112     }
3113   return TRUE;
3114 }
3115 /*----------------------------------------------------------------------------
3116   End of Helper routines for Field Validations.
3117   --------------------------------------------------------------------------*/
3118
3119 /*----------------------------------------------------------------------------
3120   Routines for Field Validation.
3121   --------------------------------------------------------------------------*/
3122
3123 /*---------------------------------------------------------------------------
3124 |   Facility      :  libnform
3125 |   Function      :  static int FV_Validation(FORM * form)
3126 |
3127 |   Description   :  Validate the current field of the form.
3128 |
3129 |   Return Values :  E_OK             - field valid
3130 |                    E_INVALID_FIELD  - field not valid
3131 +--------------------------------------------------------------------------*/
3132 static int
3133 FV_Validation(FORM *form)
3134 {
3135   T((T_CALLED("FV_Validation(%p)"), form));
3136   if (_nc_Internal_Validation(form))
3137     returnCode(E_OK);
3138   else
3139     returnCode(E_INVALID_FIELD);
3140 }
3141 /*----------------------------------------------------------------------------
3142   End of routines for Field Validation.
3143   --------------------------------------------------------------------------*/
3144
3145 /*----------------------------------------------------------------------------
3146   Helper routines for Inter-Field Navigation
3147   --------------------------------------------------------------------------*/
3148
3149 /*---------------------------------------------------------------------------
3150 |   Facility      :  libnform
3151 |   Function      :  static FIELD *Next_Field_On_Page(FIELD * field)
3152 |
3153 |   Description   :  Get the next field after the given field on the current
3154 |                    page. The order of fields is the one defined by the
3155 |                    fields array. Only visible and active fields are
3156 |                    counted.
3157 |
3158 |   Return Values :  Pointer to the next field.
3159 +--------------------------------------------------------------------------*/
3160 NCURSES_INLINE static FIELD *
3161 Next_Field_On_Page(FIELD *field)
3162 {
3163   FORM *form = field->form;
3164   FIELD **field_on_page = &form->field[field->index];
3165   FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
3166   FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3167
3168   do
3169     {
3170       field_on_page =
3171         (field_on_page == last_on_page) ? first_on_page : field_on_page + 1;
3172       if (Field_Is_Selectable(*field_on_page))
3173         break;
3174     }
3175   while (field != (*field_on_page));
3176   return (*field_on_page);
3177 }
3178
3179 /*---------------------------------------------------------------------------
3180 |   Facility      :  libnform
3181 |   Function      :  FIELD* _nc_First_Active_Field(FORM * form)
3182 |
3183 |   Description   :  Get the first active field on the current page,
3184 |                    if there are such. If there are none, get the first
3185 |                    visible field on the page. If there are also none,
3186 |                    we return the first field on page and hope the best.
3187 |
3188 |   Return Values :  Pointer to calculated field.
3189 +--------------------------------------------------------------------------*/
3190 NCURSES_EXPORT(FIELD *)
3191 _nc_First_Active_Field(FORM *form)
3192 {
3193   FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3194   FIELD *proposed = Next_Field_On_Page(*last_on_page);
3195
3196   if (proposed == *last_on_page)
3197     {
3198       /* there might be the special situation, where there is no
3199          active and visible field on the current page. We then select
3200          the first visible field on this readonly page
3201        */
3202       if (Field_Is_Not_Selectable(proposed))
3203         {
3204           FIELD **field = &form->field[proposed->index];
3205           FIELD **first = &form->field[form->page[form->curpage].pmin];
3206
3207           do
3208             {
3209               field = (field == last_on_page) ? first : field + 1;
3210               if (((*field)->opts & O_VISIBLE))
3211                 break;
3212             }
3213           while (proposed != (*field));
3214
3215           proposed = *field;
3216
3217           if ((proposed == *last_on_page) && !(proposed->opts & O_VISIBLE))
3218             {
3219               /* This means, there is also no visible field on the page.
3220                  So we propose the first one and hope the very best...
3221                  Some very clever user has designed a readonly and invisible
3222                  page on this form.
3223                */
3224               proposed = *first;
3225             }
3226         }
3227     }
3228   return (proposed);
3229 }
3230
3231 /*---------------------------------------------------------------------------
3232 |   Facility      :  libnform
3233 |   Function      :  static FIELD *Previous_Field_On_Page(FIELD * field)
3234 |
3235 |   Description   :  Get the previous field before the given field on the
3236 |                    current page. The order of fields is the one defined by
3237 |                    the fields array. Only visible and active fields are
3238 |                    counted.
3239 |
3240 |   Return Values :  Pointer to the previous field.
3241 +--------------------------------------------------------------------------*/
3242 NCURSES_INLINE static FIELD *
3243 Previous_Field_On_Page(FIELD *field)
3244 {
3245   FORM *form = field->form;
3246   FIELD **field_on_page = &form->field[field->index];
3247   FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
3248   FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3249
3250   do
3251     {
3252       field_on_page =
3253         (field_on_page == first_on_page) ? last_on_page : field_on_page - 1;
3254       if (Field_Is_Selectable(*field_on_page))
3255         break;
3256     }
3257   while (field != (*field_on_page));
3258
3259   return (*field_on_page);
3260 }
3261
3262 /*---------------------------------------------------------------------------
3263 |   Facility      :  libnform
3264 |   Function      :  static FIELD *Sorted_Next_Field(FIELD * field)
3265 |
3266 |   Description   :  Get the next field after the given field on the current
3267 |                    page. The order of fields is the one defined by the
3268 |                    (row,column) geometry, rows are major.
3269 |
3270 |   Return Values :  Pointer to the next field.
3271 +--------------------------------------------------------------------------*/
3272 NCURSES_INLINE static FIELD *
3273 Sorted_Next_Field(FIELD *field)
3274 {
3275   FIELD *field_on_page = field;
3276
3277   do
3278     {
3279       field_on_page = field_on_page->snext;
3280       if (Field_Is_Selectable(field_on_page))
3281         break;
3282     }
3283   while (field_on_page != field);
3284
3285   return (field_on_page);
3286 }
3287
3288 /*---------------------------------------------------------------------------
3289 |   Facility      :  libnform
3290 |   Function      :  static FIELD *Sorted_Previous_Field(FIELD * field)
3291 |
3292 |   Description   :  Get the previous field before the given field on the
3293 |                    current page. The order of fields is the one defined
3294 |                    by the (row,column) geometry, rows are major.
3295 |
3296 |   Return Values :  Pointer to the previous field.
3297 +--------------------------------------------------------------------------*/
3298 NCURSES_INLINE static FIELD *
3299 Sorted_Previous_Field(FIELD *field)
3300 {
3301   FIELD *field_on_page = field;
3302
3303   do
3304     {
3305       field_on_page = field_on_page->sprev;
3306       if (Field_Is_Selectable(field_on_page))
3307         break;
3308     }
3309   while (field_on_page != field);
3310
3311   return (field_on_page);
3312 }
3313
3314 /*---------------------------------------------------------------------------
3315 |   Facility      :  libnform
3316 |   Function      :  static FIELD *Left_Neighbor_Field(FIELD * field)
3317 |
3318 |   Description   :  Get the left neighbor of the field on the same line
3319 |                    and the same page. Cycles through the line.
3320 |
3321 |   Return Values :  Pointer to left neighbor field.
3322 +--------------------------------------------------------------------------*/
3323 NCURSES_INLINE static FIELD *
3324 Left_Neighbor_Field(FIELD *field)
3325 {
3326   FIELD *field_on_page = field;
3327
3328   /* For a field that has really a left neighbor, the while clause
3329      immediately fails and the loop is left, positioned at the right
3330      neighbor. Otherwise we cycle backwards through the sorted field list
3331      until we enter the same line (from the right end).
3332    */
3333   do
3334     {
3335       field_on_page = Sorted_Previous_Field(field_on_page);
3336     }
3337   while (field_on_page->frow != field->frow);
3338
3339   return (field_on_page);
3340 }
3341
3342 /*---------------------------------------------------------------------------
3343 |   Facility      :  libnform
3344 |   Function      :  static FIELD *Right_Neighbor_Field(FIELD * field)
3345 |
3346 |   Description   :  Get the right neighbor of the field on the same line
3347 |                    and the same page.
3348 |
3349 |   Return Values :  Pointer to right neighbor field.
3350 +--------------------------------------------------------------------------*/
3351 NCURSES_INLINE static FIELD *
3352 Right_Neighbor_Field(FIELD *field)
3353 {
3354   FIELD *field_on_page = field;
3355
3356   /* See the comments on Left_Neighbor_Field to understand how it works */
3357   do
3358     {
3359       field_on_page = Sorted_Next_Field(field_on_page);
3360     }
3361   while (field_on_page->frow != field->frow);
3362
3363   return (field_on_page);
3364 }
3365
3366 /*---------------------------------------------------------------------------
3367 |   Facility      :  libnform
3368 |   Function      :  static FIELD *Upper_Neighbor_Field(FIELD * field)
3369 |
3370 |   Description   :  Because of the row-major nature of sorting the fields,
3371 |                    it is more difficult to define whats the upper neighbor
3372 |                    field really means. We define that it must be on a
3373 |                    'previous' line (cyclic order!) and is the rightmost
3374 |                    field laying on the left side of the given field. If
3375 |                    this set is empty, we take the first field on the line.
3376 |
3377 |   Return Values :  Pointer to the upper neighbor field.
3378 +--------------------------------------------------------------------------*/
3379 static FIELD *
3380 Upper_Neighbor_Field(FIELD *field)
3381 {
3382   FIELD *field_on_page = field;
3383   int frow = field->frow;
3384   int fcol = field->fcol;
3385
3386   /* Walk back to the 'previous' line. The second term in the while clause
3387      just guarantees that we stop if we cycled through the line because
3388      there might be no 'previous' line if the page has just one line.
3389    */
3390   do
3391     {
3392       field_on_page = Sorted_Previous_Field(field_on_page);
3393     }
3394   while (field_on_page->frow == frow && field_on_page->fcol != fcol);
3395
3396   if (field_on_page->frow != frow)
3397     {
3398       /* We really found a 'previous' line. We are positioned at the
3399          rightmost field on this line */
3400       frow = field_on_page->frow;
3401
3402       /* We walk to the left as long as we are really right of the
3403          field. */
3404       while (field_on_page->frow == frow && field_on_page->fcol > fcol)
3405         field_on_page = Sorted_Previous_Field(field_on_page);
3406
3407       /* If we wrapped, just go to the right which is the first field on
3408          the row */
3409       if (field_on_page->frow != frow)
3410         field_on_page = Sorted_Next_Field(field_on_page);
3411     }
3412
3413   return (field_on_page);
3414 }
3415
3416 /*---------------------------------------------------------------------------
3417 |   Facility      :  libnform
3418 |   Function      :  static FIELD *Down_Neighbor_Field(FIELD * field)
3419 |
3420 |   Description   :  Because of the row-major nature of sorting the fields,
3421 |                    its more difficult to define whats the down neighbor
3422 |                    field really means. We define that it must be on a
3423 |                    'next' line (cyclic order!) and is the leftmost
3424 |                    field laying on the right side of the given field. If
3425 |                    this set is empty, we take the last field on the line.
3426 |
3427 |   Return Values :  Pointer to the upper neighbor field.
3428 +--------------------------------------------------------------------------*/
3429 static FIELD *
3430 Down_Neighbor_Field(FIELD *field)
3431 {
3432   FIELD *field_on_page = field;
3433   int frow = field->frow;
3434   int fcol = field->fcol;
3435
3436   /* Walk forward to the 'next' line. The second term in the while clause
3437      just guarantees that we stop if we cycled through the line because
3438      there might be no 'next' line if the page has just one line.
3439    */
3440   do
3441     {
3442       field_on_page = Sorted_Next_Field(field_on_page);
3443     }
3444   while (field_on_page->frow == frow && field_on_page->fcol != fcol);
3445
3446   if (field_on_page->frow != frow)
3447     {
3448       /* We really found a 'next' line. We are positioned at the rightmost
3449          field on this line */
3450       frow = field_on_page->frow;
3451
3452       /* We walk to the right as long as we are really left of the
3453          field. */
3454       while (field_on_page->frow == frow && field_on_page->fcol < fcol)
3455         field_on_page = Sorted_Next_Field(field_on_page);
3456
3457       /* If we wrapped, just go to the left which is the last field on
3458          the row */
3459       if (field_on_page->frow != frow)
3460         field_on_page = Sorted_Previous_Field(field_on_page);
3461     }
3462
3463   return (field_on_page);
3464 }
3465
3466 /*----------------------------------------------------------------------------
3467   Inter-Field Navigation routines
3468   --------------------------------------------------------------------------*/
3469
3470 /*---------------------------------------------------------------------------
3471 |   Facility      :  libnform
3472 |   Function      :  static int Inter_Field_Navigation(
3473 |                                           int (* const fct) (FORM *),
3474 |                                           FORM * form)
3475 |
3476 |   Description   :  Generic behavior for changing the current field, the
3477 |                    field is left and a new field is entered. So the field
3478 |                    must be validated and the field init/term hooks must
3479 |                    be called.
3480 |
3481 |   Return Values :  E_OK                - success
3482 |                    E_INVALID_FIELD     - field is invalid
3483 |                    some other          - error from subordinate call
3484 +--------------------------------------------------------------------------*/
3485 static int
3486 Inter_Field_Navigation(int (*const fct) (FORM *), FORM *form)
3487 {
3488   int res;
3489
3490   if (!_nc_Internal_Validation(form))
3491     res = E_INVALID_FIELD;
3492   else
3493     {
3494       Call_Hook(form, fieldterm);
3495       res = fct(form);
3496       Call_Hook(form, fieldinit);
3497     }
3498   return res;
3499 }
3500
3501 /*---------------------------------------------------------------------------
3502 |   Facility      :  libnform
3503 |   Function      :  static int FN_Next_Field(FORM * form)
3504 |
3505 |   Description   :  Move to the next field on the current page of the form
3506 |
3507 |   Return Values :  E_OK                 - success
3508 |                    != E_OK              - error from subordinate call
3509 +--------------------------------------------------------------------------*/
3510 static int
3511 FN_Next_Field(FORM *form)
3512 {
3513   T((T_CALLED("FN_Next_Field(%p)"), form));
3514   returnCode(_nc_Set_Current_Field(form,
3515                                    Next_Field_On_Page(form->current)));
3516 }
3517
3518 /*---------------------------------------------------------------------------
3519 |   Facility      :  libnform
3520 |   Function      :  static int FN_Previous_Field(FORM * form)
3521 |
3522 |   Description   :  Move to the previous field on the current page of the
3523 |                    form
3524 |
3525 |   Return Values :  E_OK                 - success
3526 |                    != E_OK              - error from subordinate call
3527 +--------------------------------------------------------------------------*/
3528 static int
3529 FN_Previous_Field(FORM *form)
3530 {
3531   T((T_CALLED("FN_Previous_Field(%p)"), form));
3532   returnCode(_nc_Set_Current_Field(form,
3533                                    Previous_Field_On_Page(form->current)));
3534 }
3535
3536 /*---------------------------------------------------------------------------
3537 |   Facility      :  libnform
3538 |   Function      :  static int FN_First_Field(FORM * form)
3539 |
3540 |   Description   :  Move to the first field on the current page of the form
3541 |
3542 |   Return Values :  E_OK                 - success
3543 |                    != E_OK              - error from subordinate call
3544 +--------------------------------------------------------------------------*/
3545 static int
3546 FN_First_Field(FORM *form)
3547 {
3548   T((T_CALLED("FN_First_Field(%p)"), form));
3549   returnCode(_nc_Set_Current_Field(form,
3550                                    Next_Field_On_Page(form->field[form->page[form->curpage].pmax])));
3551 }
3552
3553 /*---------------------------------------------------------------------------
3554 |   Facility      :  libnform
3555 |   Function      :  static int FN_Last_Field(FORM * form)
3556 |
3557 |   Description   :  Move to the last field on the current page of the form
3558 |
3559 |   Return Values :  E_OK                 - success
3560 |                    != E_OK              - error from subordinate call
3561 +--------------------------------------------------------------------------*/
3562 static int
3563 FN_Last_Field(FORM *form)
3564 {
3565   T((T_CALLED("FN_Last_Field(%p)"), form));
3566   returnCode(
3567               _nc_Set_Current_Field(form,
3568                                     Previous_Field_On_Page(form->field[form->page[form->curpage].pmin])));
3569 }
3570
3571 /*---------------------------------------------------------------------------
3572 |   Facility      :  libnform
3573 |   Function      :  static int FN_Sorted_Next_Field(FORM * form)
3574 |
3575 |   Description   :  Move to the sorted next field on the current page
3576 |                    of the form.
3577 |
3578 |   Return Values :  E_OK            - success
3579 |                    != E_OK         - error from subordinate call
3580 +--------------------------------------------------------------------------*/
3581 static int
3582 FN_Sorted_Next_Field(FORM *form)
3583 {
3584   T((T_CALLED("FN_Sorted_Next_Field(%p)"), form));
3585   returnCode(_nc_Set_Current_Field(form,
3586                                    Sorted_Next_Field(form->current)));
3587 }
3588
3589 /*---------------------------------------------------------------------------
3590 |   Facility      :  libnform
3591 |   Function      :  static int FN_Sorted_Previous_Field(FORM * form)
3592 |
3593 |   Description   :  Move to the sorted previous field on the current page
3594 |                    of the form.
3595 |
3596 |   Return Values :  E_OK            - success
3597 |                    != E_OK         - error from subordinate call
3598 +--------------------------------------------------------------------------*/
3599 static int
3600 FN_Sorted_Previous_Field(FORM *form)
3601 {
3602   T((T_CALLED("FN_Sorted_Previous_Field(%p)"), form));
3603   returnCode(_nc_Set_Current_Field(form,
3604                                    Sorted_Previous_Field(form->current)));
3605 }
3606
3607 /*---------------------------------------------------------------------------
3608 |   Facility      :  libnform
3609 |   Function      :  static int FN_Sorted_First_Field(FORM * form)
3610 |
3611 |   Description   :  Move to the sorted first field on the current page
3612 |                    of the form.
3613 |
3614 |   Return Values :  E_OK            - success
3615 |                    != E_OK         - error from subordinate call
3616 +--------------------------------------------------------------------------*/
3617 static int
3618 FN_Sorted_First_Field(FORM *form)
3619 {
3620   T((T_CALLED("FN_Sorted_First_Field(%p)"), form));
3621   returnCode(_nc_Set_Current_Field(form,
3622                                    Sorted_Next_Field(form->field[form->page[form->curpage].smax])));
3623 }
3624
3625 /*---------------------------------------------------------------------------
3626 |   Facility      :  libnform
3627 |   Function      :  static int FN_Sorted_Last_Field(FORM * form)
3628 |
3629 |   Description   :  Move to the sorted last field on the current page
3630 |                    of the form.
3631 |
3632 |   Return Values :  E_OK            - success
3633 |                    != E_OK         - error from subordinate call
3634 +--------------------------------------------------------------------------*/
3635 static int
3636 FN_Sorted_Last_Field(FORM *form)
3637 {
3638   T((T_CALLED("FN_Sorted_Last_Field(%p)"), form));
3639   returnCode(_nc_Set_Current_Field(form,
3640                                    Sorted_Previous_Field(form->field[form->page[form->curpage].smin])));
3641 }
3642
3643 /*---------------------------------------------------------------------------
3644 |   Facility      :  libnform
3645 |   Function      :  static int FN_Left_Field(FORM * form)
3646 |
3647 |   Description   :  Get the field on the left of the current field on the
3648 |                    same line and the same page. Cycles through the line.
3649 |
3650 |   Return Values :  E_OK            - success
3651 |                    != E_OK         - error from subordinate call
3652 +--------------------------------------------------------------------------*/
3653 static int
3654 FN_Left_Field(FORM *form)
3655 {
3656   T((T_CALLED("FN_Left_Field(%p)"), form));
3657   returnCode(_nc_Set_Current_Field(form,
3658                                    Left_Neighbor_Field(form->current)));
3659 }
3660
3661 /*---------------------------------------------------------------------------
3662 |   Facility      :  libnform
3663 |   Function      :  static int FN_Right_Field(FORM * form)
3664 |
3665 |   Description   :  Get the field on the right of the current field on the
3666 |                    same line and the same page. Cycles through the line.
3667 |
3668 |   Return Values :  E_OK            - success
3669 |                    != E_OK         - error from subordinate call
3670 +--------------------------------------------------------------------------*/
3671 static int
3672 FN_Right_Field(FORM *form)
3673 {
3674   T((T_CALLED("FN_Right_Field(%p)"), form));
3675   returnCode(_nc_Set_Current_Field(form,
3676                                    Right_Neighbor_Field(form->current)));
3677 }
3678
3679 /*---------------------------------------------------------------------------
3680 |   Facility      :  libnform
3681 |   Function      :  static int FN_Up_Field(FORM * form)
3682 |
3683 |   Description   :  Get the upper neighbor of the current field. This
3684 |                    cycles through the page. See the comments of the
3685 |                    Upper_Neighbor_Field function to understand how
3686 |                    'upper' is defined.
3687 |
3688 |   Return Values :  E_OK            - success
3689 |                    != E_OK         - error from subordinate call
3690 +--------------------------------------------------------------------------*/
3691 static int
3692 FN_Up_Field(FORM *form)
3693 {
3694   T((T_CALLED("FN_Up_Field(%p)"), form));
3695   returnCode(_nc_Set_Current_Field(form,
3696                                    Upper_Neighbor_Field(form->current)));
3697 }
3698
3699 /*---------------------------------------------------------------------------
3700 |   Facility      :  libnform
3701 |   Function      :  static int FN_Down_Field(FORM * form)
3702 |
3703 |   Description   :  Get the down neighbor of the current field. This
3704 |                    cycles through the page. See the comments of the
3705 |                    Down_Neighbor_Field function to understand how
3706 |                    'down' is defined.
3707 |
3708 |   Return Values :  E_OK            - success
3709 |                    != E_OK         - error from subordinate call
3710 +--------------------------------------------------------------------------*/
3711 static int
3712 FN_Down_Field(FORM *form)
3713 {
3714   T((T_CALLED("FN_Down_Field(%p)"), form));
3715   returnCode(_nc_Set_Current_Field(form,
3716                                    Down_Neighbor_Field(form->current)));
3717 }
3718 /*----------------------------------------------------------------------------
3719   END of Field Navigation routines
3720   --------------------------------------------------------------------------*/
3721
3722 /*----------------------------------------------------------------------------
3723   Helper routines for Page Navigation
3724   --------------------------------------------------------------------------*/
3725
3726 /*---------------------------------------------------------------------------
3727 |   Facility      :  libnform
3728 |   Function      :  int _nc_Set_Form_Page(FORM * form,
3729 |                                          int page,
3730 |                                          FIELD * field)
3731 |
3732 |   Description   :  Make the given page number the current page and make
3733 |                    the given field the current field on the page. If
3734 |                    for the field NULL is given, make the first field on
3735 |                    the page the current field. The routine acts only
3736 |                    if the requested page is not the current page.
3737 |
3738 |   Return Values :  E_OK                - success
3739 |                    != E_OK             - error from subordinate call
3740 |                    E_BAD_ARGUMENT      - invalid field pointer
3741 |                    E_SYSTEM_ERROR      - some severe basic error
3742 +--------------------------------------------------------------------------*/
3743 NCURSES_EXPORT(int)
3744 _nc_Set_Form_Page(FORM *form, int page, FIELD *field)
3745 {
3746   int res = E_OK;
3747
3748   if ((form->curpage != page))
3749     {
3750       FIELD *last_field, *field_on_page;
3751
3752       werase(Get_Form_Window(form));
3753       form->curpage = page;
3754       last_field = field_on_page = form->field[form->page[page].smin];
3755       do
3756         {
3757           if (field_on_page->opts & O_VISIBLE)
3758             if ((res = Display_Field(field_on_page)) != E_OK)
3759               return (res);
3760           field_on_page = field_on_page->snext;
3761         }
3762       while (field_on_page != last_field);
3763
3764       if (field)
3765         res = _nc_Set_Current_Field(form, field);
3766       else
3767         /* N.B.: we don't encapsulate this by Inter_Field_Navigation(),
3768            because this is already executed in a page navigation
3769            context that contains field navigation
3770          */
3771         res = FN_First_Field(form);
3772     }
3773   return (res);
3774 }
3775
3776 /*---------------------------------------------------------------------------
3777 |   Facility      :  libnform
3778 |   Function      :  static int Next_Page_Number(const FORM * form)
3779 |
3780 |   Description   :  Calculate the page number following the current page
3781 |                    number. This cycles if the highest page number is
3782 |                    reached.
3783 |
3784 |   Return Values :  The next page number
3785 +--------------------------------------------------------------------------*/
3786 NCURSES_INLINE static int
3787 Next_Page_Number(const FORM *form)
3788 {
3789   return (form->curpage + 1) % form->maxpage;
3790 }
3791
3792 /*---------------------------------------------------------------------------
3793 |   Facility      :  libnform
3794 |   Function      :  static int Previous_Page_Number(const FORM * form)
3795 |
3796 |   Description   :  Calculate the page number before the current page
3797 |                    number. This cycles if the first page number is
3798 |                    reached.
3799 |
3800 |   Return Values :  The previous page number
3801 +--------------------------------------------------------------------------*/
3802 NCURSES_INLINE static int
3803 Previous_Page_Number(const FORM *form)
3804 {
3805   return (form->curpage != 0 ? form->curpage - 1 : form->maxpage - 1);
3806 }
3807
3808 /*----------------------------------------------------------------------------
3809   Page Navigation routines
3810   --------------------------------------------------------------------------*/
3811
3812 /*---------------------------------------------------------------------------
3813 |   Facility      :  libnform
3814 |   Function      :  static int Page_Navigation(
3815 |                                               int (* const fct) (FORM *),
3816 |                                               FORM * form)
3817 |
3818 |   Description   :  Generic behavior for changing a page. This means
3819 |                    that the field is left and a new field is entered.
3820 |                    So the field must be validated and the field init/term
3821 |                    hooks must be called. Because also the page is changed,
3822 |                    the forms init/term hooks must be called also.
3823 |
3824 |   Return Values :  E_OK                - success
3825 |                    E_INVALID_FIELD     - field is invalid
3826 |                    some other          - error from subordinate call
3827 +--------------------------------------------------------------------------*/
3828 static int
3829 Page_Navigation(int (*const fct) (FORM *), FORM *form)
3830 {
3831   int res;
3832
3833   if (!_nc_Internal_Validation(form))
3834     res = E_INVALID_FIELD;
3835   else
3836     {
3837       Call_Hook(form, fieldterm);
3838       Call_Hook(form, formterm);
3839       res = fct(form);
3840       Call_Hook(form, forminit);
3841       Call_Hook(form, fieldinit);
3842     }
3843   return res;
3844 }
3845
3846 /*---------------------------------------------------------------------------
3847 |   Facility      :  libnform
3848 |   Function      :  static int PN_Next_Page(FORM * form)
3849 |
3850 |   Description   :  Move to the next page of the form
3851 |
3852 |   Return Values :  E_OK                - success
3853 |                    != E_OK             - error from subordinate call
3854 +--------------------------------------------------------------------------*/
3855 static int
3856 PN_Next_Page(FORM *form)
3857 {
3858   T((T_CALLED("PN_Next_Page(%p)"), form));
3859   returnCode(_nc_Set_Form_Page(form, Next_Page_Number(form), (FIELD *)0));
3860 }
3861
3862 /*---------------------------------------------------------------------------
3863 |   Facility      :  libnform
3864 |   Function      :  static int PN_Previous_Page(FORM * form)
3865 |
3866 |   Description   :  Move to the previous page of the form
3867 |
3868 |   Return Values :  E_OK              - success
3869 |                    != E_OK           - error from subordinate call
3870 +--------------------------------------------------------------------------*/
3871 static int
3872 PN_Previous_Page(FORM *form)
3873 {
3874   T((T_CALLED("PN_Previous_Page(%p)"), form));
3875   returnCode(_nc_Set_Form_Page(form, Previous_Page_Number(form), (FIELD *)0));
3876 }
3877
3878 /*---------------------------------------------------------------------------
3879 |   Facility      :  libnform
3880 |   Function      :  static int PN_First_Page(FORM * form)
3881 |
3882 |   Description   :  Move to the first page of the form
3883 |
3884 |   Return Values :  E_OK              - success
3885 |                    != E_OK           - error from subordinate call
3886 +--------------------------------------------------------------------------*/
3887 static int
3888 PN_First_Page(FORM *form)
3889 {
3890   T((T_CALLED("PN_First_Page(%p)"), form));
3891   returnCode(_nc_Set_Form_Page(form, 0, (FIELD *)0));
3892 }
3893
3894 /*---------------------------------------------------------------------------
3895 |   Facility      :  libnform
3896 |   Function      :  static int PN_Last_Page(FORM * form)
3897 |
3898 |   Description   :  Move to the last page of the form
3899 |
3900 |   Return Values :  E_OK              - success
3901 |                    != E_OK           - error from subordinate call
3902 +--------------------------------------------------------------------------*/
3903 static int
3904 PN_Last_Page(FORM *form)
3905 {
3906   T((T_CALLED("PN_Last_Page(%p)"), form));
3907   returnCode(_nc_Set_Form_Page(form, form->maxpage - 1, (FIELD *)0));
3908 }
3909
3910 /*----------------------------------------------------------------------------
3911   END of Field Navigation routines
3912   --------------------------------------------------------------------------*/
3913
3914 /*----------------------------------------------------------------------------
3915   Helper routines for the core form driver.
3916   --------------------------------------------------------------------------*/
3917
3918 /*---------------------------------------------------------------------------
3919 |   Facility      :  libnform
3920 |   Function      :  static int Data_Entry(FORM * form,int c)
3921 |
3922 |   Description   :  Enter character c into at the current position of the
3923 |                    current field of the form.
3924 |
3925 |   Return Values :  E_OK              - success
3926 |                    E_REQUEST_DENIED  - driver could not process the request
3927 |                    E_SYSTEM_ERROR    -
3928 +--------------------------------------------------------------------------*/
3929 static int
3930 Data_Entry(FORM *form, int c)
3931 {
3932   FIELD *field = form->current;
3933   int result = E_REQUEST_DENIED;
3934
3935   T((T_CALLED("Data_Entry(%p,%s)"), form, _tracechtype((chtype)c)));
3936   if ((field->opts & O_EDIT)
3937 #if FIX_FORM_INACTIVE_BUG
3938       && (field->opts & O_ACTIVE)
3939 #endif
3940     )
3941     {
3942       if ((field->opts & O_BLANK) &&
3943           First_Position_In_Current_Field(form) &&
3944           !(form->status & _FCHECK_REQUIRED) &&
3945           !(form->status & _WINDOW_MODIFIED))
3946         werase(form->w);
3947
3948       if (form->status & _OVLMODE)
3949         {
3950           waddch(form->w, (chtype)c);
3951         }
3952       else
3953         /* no _OVLMODE */
3954         {
3955           bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);
3956
3957           if (!(There_Is_Room ||
3958                 ((Single_Line_Field(field) && Growable(field)))))
3959             RETURN(E_REQUEST_DENIED);
3960
3961           if (!There_Is_Room && !Field_Grown(field, 1))
3962             RETURN(E_SYSTEM_ERROR);
3963
3964           winsch(form->w, (chtype)c);
3965         }
3966
3967       if ((result = Wrapping_Not_Necessary_Or_Wrapping_Ok(form)) == E_OK)
3968         {
3969           bool End_Of_Field = (((field->drows - 1) == form->currow) &&
3970                                ((field->dcols - 1) == form->curcol));
3971
3972           form->status |= _WINDOW_MODIFIED;
3973           if (End_Of_Field && !Growable(field) && (field->opts & O_AUTOSKIP))
3974             result = Inter_Field_Navigation(FN_Next_Field, form);
3975           else
3976             {
3977               if (End_Of_Field && Growable(field) && !Field_Grown(field, 1))
3978                 result = E_SYSTEM_ERROR;
3979               else
3980                 {
3981 #if USE_WIDEC_SUPPORT
3982                   /*
3983                    * We have just added a byte to the form field.  It may have
3984                    * been part of a multibyte character.  If it was, the
3985                    * addch_used field is nonzero and we should not try to move
3986                    * to a new column.
3987                    */
3988                   if (WINDOW_EXT(form->w, addch_used) == 0)
3989                     IFN_Next_Character(form);
3990 #else
3991                   IFN_Next_Character(form);
3992 #endif
3993                   result = E_OK;
3994                 }
3995             }
3996         }
3997     }
3998   RETURN(result);
3999 }
4000
4001 /* Structure to describe the binding of a request code to a function.
4002    The member keycode codes the request value as well as the generic
4003    routine to use for the request. The code for the generic routine
4004    is coded in the upper 16 Bits while the request code is coded in
4005    the lower 16 bits.
4006
4007    In terms of C++ you might think of a request as a class with a
4008    virtual method "perform". The different types of request are
4009    derived from this base class and overload (or not) the base class
4010    implementation of perform.
4011 */
4012 typedef struct
4013 {
4014   int keycode;                  /* must be at least 32 bit: hi:mode, lo: key */
4015   int (*cmd) (FORM *);          /* low level driver routine for this key     */
4016 }
4017 Binding_Info;
4018
4019 /* You may see this is the class-id of the request type class */
4020 #define ID_PN    (0x00000000)   /* Page navigation           */
4021 #define ID_FN    (0x00010000)   /* Inter-Field navigation    */
4022 #define ID_IFN   (0x00020000)   /* Intra-Field navigation    */
4023 #define ID_VSC   (0x00030000)   /* Vertical Scrolling        */
4024 #define ID_HSC   (0x00040000)   /* Horizontal Scrolling      */
4025 #define ID_FE    (0x00050000)   /* Field Editing             */
4026 #define ID_EM    (0x00060000)   /* Edit Mode                 */
4027 #define ID_FV    (0x00070000)   /* Field Validation          */
4028 #define ID_CH    (0x00080000)   /* Choice                    */
4029 #define ID_Mask  (0xffff0000)
4030 #define Key_Mask (0x0000ffff)
4031 #define ID_Shft  (16)
4032
4033 /* This array holds all the Binding Infos */
4034 /* *INDENT-OFF* */
4035 static const Binding_Info bindings[MAX_FORM_COMMAND - MIN_FORM_COMMAND + 1] =
4036 {
4037   { REQ_NEXT_PAGE    |ID_PN  ,PN_Next_Page},
4038   { REQ_PREV_PAGE    |ID_PN  ,PN_Previous_Page},
4039   { REQ_FIRST_PAGE   |ID_PN  ,PN_First_Page},
4040   { REQ_LAST_PAGE    |ID_PN  ,PN_Last_Page},
4041
4042   { REQ_NEXT_FIELD   |ID_FN  ,FN_Next_Field},
4043   { REQ_PREV_FIELD   |ID_FN  ,FN_Previous_Field},
4044   { REQ_FIRST_FIELD  |ID_FN  ,FN_First_Field},
4045   { REQ_LAST_FIELD   |ID_FN  ,FN_Last_Field},
4046   { REQ_SNEXT_FIELD  |ID_FN  ,FN_Sorted_Next_Field},
4047   { REQ_SPREV_FIELD  |ID_FN  ,FN_Sorted_Previous_Field},
4048   { REQ_SFIRST_FIELD |ID_FN  ,FN_Sorted_First_Field},
4049   { REQ_SLAST_FIELD  |ID_FN  ,FN_Sorted_Last_Field},
4050   { REQ_LEFT_FIELD   |ID_FN  ,FN_Left_Field},
4051   { REQ_RIGHT_FIELD  |ID_FN  ,FN_Right_Field},
4052   { REQ_UP_FIELD     |ID_FN  ,FN_Up_Field},
4053   { REQ_DOWN_FIELD   |ID_FN  ,FN_Down_Field},
4054
4055   { REQ_NEXT_CHAR    |ID_IFN ,IFN_Next_Character},
4056   { REQ_PREV_CHAR    |ID_IFN ,IFN_Previous_Character},
4057   { REQ_NEXT_LINE    |ID_IFN ,IFN_Next_Line},
4058   { REQ_PREV_LINE    |ID_IFN ,IFN_Previous_Line},
4059   { REQ_NEXT_WORD    |ID_IFN ,IFN_Next_Word},
4060   { REQ_PREV_WORD    |ID_IFN ,IFN_Previous_Word},
4061   { REQ_BEG_FIELD    |ID_IFN ,IFN_Beginning_Of_Field},
4062   { REQ_END_FIELD    |ID_IFN ,IFN_End_Of_Field},
4063   { REQ_BEG_LINE     |ID_IFN ,IFN_Beginning_Of_Line},
4064   { REQ_END_LINE     |ID_IFN ,IFN_End_Of_Line},
4065   { REQ_LEFT_CHAR    |ID_IFN ,IFN_Left_Character},
4066   { REQ_RIGHT_CHAR   |ID_IFN ,IFN_Right_Character},
4067   { REQ_UP_CHAR      |ID_IFN ,IFN_Up_Character},
4068   { REQ_DOWN_CHAR    |ID_IFN ,IFN_Down_Character},
4069
4070   { REQ_NEW_LINE     |ID_FE  ,FE_New_Line},
4071   { REQ_INS_CHAR     |ID_FE  ,FE_Insert_Character},
4072   { REQ_INS_LINE     |ID_FE  ,FE_Insert_Line},
4073   { REQ_DEL_CHAR     |ID_FE  ,FE_Delete_Character},
4074   { REQ_DEL_PREV     |ID_FE  ,FE_Delete_Previous},
4075   { REQ_DEL_LINE     |ID_FE  ,FE_Delete_Line},
4076   { REQ_DEL_WORD     |ID_FE  ,FE_Delete_Word},
4077   { REQ_CLR_EOL      |ID_FE  ,FE_Clear_To_End_Of_Line},
4078   { REQ_CLR_EOF      |ID_FE  ,FE_Clear_To_End_Of_Field},
4079   { REQ_CLR_FIELD    |ID_FE  ,FE_Clear_Field},
4080
4081   { REQ_OVL_MODE     |ID_EM  ,EM_Overlay_Mode},
4082   { REQ_INS_MODE     |ID_EM  ,EM_Insert_Mode},
4083
4084   { REQ_SCR_FLINE    |ID_VSC ,VSC_Scroll_Line_Forward},
4085   { REQ_SCR_BLINE    |ID_VSC ,VSC_Scroll_Line_Backward},
4086   { REQ_SCR_FPAGE    |ID_VSC ,VSC_Scroll_Page_Forward},
4087   { REQ_SCR_BPAGE    |ID_VSC ,VSC_Scroll_Page_Backward},
4088   { REQ_SCR_FHPAGE   |ID_VSC ,VSC_Scroll_Half_Page_Forward},
4089   { REQ_SCR_BHPAGE   |ID_VSC ,VSC_Scroll_Half_Page_Backward},
4090
4091   { REQ_SCR_FCHAR    |ID_HSC ,HSC_Scroll_Char_Forward},
4092   { REQ_SCR_BCHAR    |ID_HSC ,HSC_Scroll_Char_Backward},
4093   { REQ_SCR_HFLINE   |ID_HSC ,HSC_Horizontal_Line_Forward},
4094   { REQ_SCR_HBLINE   |ID_HSC ,HSC_Horizontal_Line_Backward},
4095   { REQ_SCR_HFHALF   |ID_HSC ,HSC_Horizontal_Half_Line_Forward},
4096   { REQ_SCR_HBHALF   |ID_HSC ,HSC_Horizontal_Half_Line_Backward},
4097
4098   { REQ_VALIDATION   |ID_FV  ,FV_Validation},
4099
4100   { REQ_NEXT_CHOICE  |ID_CH  ,CR_Next_Choice},
4101   { REQ_PREV_CHOICE  |ID_CH  ,CR_Previous_Choice}
4102 };
4103 /* *INDENT-ON* */
4104
4105 /*---------------------------------------------------------------------------
4106 |   Facility      :  libnform
4107 |   Function      :  int form_driver(FORM * form,int  c)
4108 |
4109 |   Description   :  This is the workhorse of the forms system. It checks
4110 |                    to determine whether the character c is a request or
4111 |                    data. If it is a request, the form driver executes
4112 |                    the request and returns the result. If it is data
4113 |                    (printable character), it enters the data into the
4114 |                    current position in the current field. If it is not
4115 |                    recognized, the form driver assumes it is an application
4116 |                    defined command and returns E_UNKNOWN_COMMAND.
4117 |                    Application defined command should be defined relative
4118 |                    to MAX_FORM_COMMAND, the maximum value of a request.
4119 |
4120 |   Return Values :  E_OK              - success
4121 |                    E_SYSTEM_ERROR    - system error
4122 |                    E_BAD_ARGUMENT    - an argument is incorrect
4123 |                    E_NOT_POSTED      - form is not posted
4124 |                    E_INVALID_FIELD   - field contents are invalid
4125 |                    E_BAD_STATE       - called from inside a hook routine
4126 |                    E_REQUEST_DENIED  - request failed
4127 |                    E_NOT_CONNECTED   - no fields are connected to the form
4128 |                    E_UNKNOWN_COMMAND - command not known
4129 +--------------------------------------------------------------------------*/
4130 NCURSES_EXPORT(int)
4131 form_driver(FORM *form, int c)
4132 {
4133   const Binding_Info *BI = (Binding_Info *) 0;
4134   int res = E_UNKNOWN_COMMAND;
4135
4136   T((T_CALLED("form_driver(%p,%d)"), form, c));
4137
4138   if (!form)
4139     RETURN(E_BAD_ARGUMENT);
4140
4141   if (!(form->field))
4142     RETURN(E_NOT_CONNECTED);
4143
4144   assert(form->page);
4145
4146   if (c == FIRST_ACTIVE_MAGIC)
4147     {
4148       form->current = _nc_First_Active_Field(form);
4149       RETURN(E_OK);
4150     }
4151
4152   assert(form->current &&
4153          form->current->buf &&
4154          (form->current->form == form)
4155     );
4156
4157   if (form->status & _IN_DRIVER)
4158     RETURN(E_BAD_STATE);
4159
4160   if (!(form->status & _POSTED))
4161     RETURN(E_NOT_POSTED);
4162
4163   if ((c >= MIN_FORM_COMMAND && c <= MAX_FORM_COMMAND) &&
4164       ((bindings[c - MIN_FORM_COMMAND].keycode & Key_Mask) == c))
4165     BI = &(bindings[c - MIN_FORM_COMMAND]);
4166
4167   if (BI)
4168     {
4169       typedef int (*Generic_Method) (int (*const) (FORM *), FORM *);
4170       static const Generic_Method Generic_Methods[] =
4171       {
4172         Page_Navigation,        /* overloaded to call field&form hooks */
4173         Inter_Field_Navigation, /* overloaded to call field hooks      */
4174         NULL,                   /* Intra-Field is generic              */
4175         Vertical_Scrolling,     /* Overloaded to check multi-line      */
4176         Horizontal_Scrolling,   /* Overloaded to check single-line     */
4177         Field_Editing,          /* Overloaded to mark modification     */
4178         NULL,                   /* Edit Mode is generic                */
4179         NULL,                   /* Field Validation is generic         */
4180         NULL                    /* Choice Request is generic           */
4181       };
4182       size_t nMethods = (sizeof(Generic_Methods) / sizeof(Generic_Methods[0]));
4183       size_t method = (BI->keycode >> ID_Shft) & 0xffff;        /* see ID_Mask */
4184
4185       if ((method >= nMethods) || !(BI->cmd))
4186         res = E_SYSTEM_ERROR;
4187       else
4188         {
4189           Generic_Method fct = Generic_Methods[method];
4190
4191           if (fct)
4192             res = fct(BI->cmd, form);
4193           else
4194             res = (BI->cmd) (form);
4195         }
4196     }
4197 #ifdef NCURSES_MOUSE_VERSION
4198   else if (KEY_MOUSE == c)
4199     {
4200       MEVENT event;
4201       WINDOW *win = form->win ? form->win : stdscr;
4202       WINDOW *sub = form->sub ? form->sub : win;
4203
4204       getmouse(&event);
4205       if ((event.bstate & (BUTTON1_CLICKED |
4206                            BUTTON1_DOUBLE_CLICKED |
4207                            BUTTON1_TRIPLE_CLICKED))
4208           && wenclose(win, event.y, event.x))
4209         {                       /* we react only if the click was in the userwin, that means
4210                                  * inside the form display area or at the decoration window.
4211                                  */
4212           int ry = event.y, rx = event.x;       /* screen coordinates */
4213
4214           res = E_REQUEST_DENIED;
4215           if (mouse_trafo(&ry, &rx, FALSE))
4216             {                   /* rx, ry are now "curses" coordinates */
4217               if (ry < sub->_begy)
4218                 {               /* we clicked above the display region; this is
4219                                  * interpreted as "scroll up" request
4220                                  */
4221                   if (event.bstate & BUTTON1_CLICKED)
4222                     res = form_driver(form, REQ_PREV_FIELD);
4223                   else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
4224                     res = form_driver(form, REQ_PREV_PAGE);
4225                   else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
4226                     res = form_driver(form, REQ_FIRST_FIELD);
4227                 }
4228               else if (ry > sub->_begy + sub->_maxy)
4229                 {               /* we clicked below the display region; this is
4230                                  * interpreted as "scroll down" request
4231                                  */
4232                   if (event.bstate & BUTTON1_CLICKED)
4233                     res = form_driver(form, REQ_NEXT_FIELD);
4234                   else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
4235                     res = form_driver(form, REQ_NEXT_PAGE);
4236                   else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
4237                     res = form_driver(form, REQ_LAST_FIELD);
4238                 }
4239               else if (wenclose(sub, event.y, event.x))
4240                 {               /* Inside the area we try to find the hit item */
4241                   int i;
4242
4243                   ry = event.y;
4244                   rx = event.x;
4245                   if (wmouse_trafo(sub, &ry, &rx, FALSE))
4246                     {
4247                       int min_field = form->page[form->curpage].pmin;
4248                       int max_field = form->page[form->curpage].pmax;
4249
4250                       for (i = min_field; i <= max_field; ++i)
4251                         {
4252                           FIELD *field = form->field[i];
4253
4254                           if (Field_Is_Selectable(field)
4255                               && Field_encloses(field, ry, rx) == E_OK)
4256                             {
4257                               res = _nc_Set_Current_Field(form, field);
4258                               if (res == E_OK)
4259                                 res = _nc_Position_Form_Cursor(form);
4260                               if (res == E_OK
4261                                   && (event.bstate & BUTTON1_DOUBLE_CLICKED))
4262                                 res = E_UNKNOWN_COMMAND;
4263                               break;
4264                             }
4265                         }
4266                     }
4267                 }
4268             }
4269         }
4270       else
4271         res = E_REQUEST_DENIED;
4272     }
4273 #endif /* NCURSES_MOUSE_VERSION */
4274   else if (!(c & (~(int)MAX_REGULAR_CHARACTER)))
4275     {
4276       /*
4277        * If we're using 8-bit characters, iscntrl+isprint cover the whole set.
4278        * But with multibyte characters, there is a third possibility, i.e.,
4279        * parts of characters that build up into printable characters which are
4280        * not considered printable.
4281        *
4282        * FIXME: the wide-character branch should also use Check_Char().
4283        */
4284 #if USE_WIDEC_SUPPORT
4285       if (!iscntrl(UChar(c)))
4286 #else
4287       if (isprint(UChar(c)) &&
4288           Check_Char(form->current->type, c,
4289                      (TypeArgument *)(form->current->arg)))
4290 #endif
4291         res = Data_Entry(form, c);
4292     }
4293   _nc_Refresh_Current_Field(form);
4294   RETURN(res);
4295 }
4296
4297 /*----------------------------------------------------------------------------
4298   Field-Buffer manipulation routines.
4299   The effects of setting a buffer are tightly coupled to the core of the form
4300   driver logic. This is especially true in the case of growable fields.
4301   So I don't separate this into a separate module.
4302   --------------------------------------------------------------------------*/
4303
4304 /*---------------------------------------------------------------------------
4305 |   Facility      :  libnform
4306 |   Function      :  int set_field_buffer(FIELD *field,
4307 |                                         int buffer, char *value)
4308 |
4309 |   Description   :  Set the given buffer of the field to the given value.
4310 |                    Buffer 0 stores the displayed content of the field.
4311 |                    For dynamic fields this may grow the fieldbuffers if
4312 |                    the length of the value exceeds the current buffer
4313 |                    length. For buffer 0 only printable values are allowed.
4314 |                    For static fields, the value needs not to be zero ter-
4315 |                    minated. It is copied up to the length of the buffer.
4316 |
4317 |   Return Values :  E_OK            - success
4318 |                    E_BAD_ARGUMENT  - invalid argument
4319 |                    E_SYSTEM_ERROR  - system error
4320 +--------------------------------------------------------------------------*/
4321 NCURSES_EXPORT(int)
4322 set_field_buffer(FIELD *field, int buffer, const char *value)
4323 {
4324   FIELD_CELL *p;
4325   int res = E_OK;
4326   unsigned int i;
4327   unsigned int len;
4328
4329 #if USE_WIDEC_SUPPORT
4330   FIELD_CELL *widevalue = 0;
4331 #endif
4332
4333   T((T_CALLED("set_field_buffer(%p,%d,%s)"), field, buffer, _nc_visbuf(value)));
4334
4335   if (!field || !value || ((buffer < 0) || (buffer > field->nbuf)))
4336     RETURN(E_BAD_ARGUMENT);
4337
4338   len = Buffer_Length(field);
4339
4340   if (Growable(field))
4341     {
4342       /* for a growable field we must assume zero terminated strings, because
4343          somehow we have to detect the length of what should be copied.
4344        */
4345       unsigned int vlen = strlen(value);
4346
4347       if (vlen > len)
4348         {
4349           if (!Field_Grown(field,
4350                            (int)(1 + (vlen - len) / ((field->rows + field->nrow)
4351                                                      * field->cols))))
4352             RETURN(E_SYSTEM_ERROR);
4353
4354           len = vlen;
4355         }
4356     }
4357
4358   p = Address_Of_Nth_Buffer(field, buffer);
4359
4360 #if USE_WIDEC_SUPPORT
4361   /*
4362    * Use addstr's logic for converting a string to an array of cchar_t's.
4363    * There should be a better way, but this handles nonspacing characters
4364    * and other special cases that we really do not want to handle here.
4365    */
4366 #if NCURSES_EXT_FUNCS
4367   if (wresize(field->working, field->drows, field->dcols) == ERR)
4368 #endif
4369     {
4370       delwin(field->working);
4371       field->working = newpad(field->drows, field->dcols);
4372     }
4373   len = Buffer_Length(field);
4374   wclear(field->working);
4375   mvwaddstr(field->working, 0, 0, value);
4376
4377   if ((widevalue = typeCalloc(FIELD_CELL, len + 1)) == 0)
4378     {
4379       RETURN(E_SYSTEM_ERROR);
4380     }
4381   else
4382     {
4383       for (i = 0; i < (unsigned)field->drows; ++i)
4384         {
4385           mvwin_wchnstr(field->working, i, 0,
4386                         widevalue + (i * field->dcols),
4387                         field->dcols);
4388         }
4389       for (i = 0; i < len; ++i)
4390         {
4391           if (CharEq(myZEROS, widevalue[i]))
4392             {
4393               while (i < len)
4394                 p[i++] = myBLANK;
4395               break;
4396             }
4397           p[i] = widevalue[i];
4398         }
4399       free(widevalue);
4400     }
4401 #else
4402   for (i = 0; i < len; ++i)
4403     {
4404       if (value[i] == '\0')
4405         {
4406           while (i < len)
4407             p[i++] = myBLANK;
4408           break;
4409         }
4410       p[i] = value[i];
4411     }
4412 #endif
4413
4414   if (buffer == 0)
4415     {
4416       int syncres;
4417
4418       if (((syncres = Synchronize_Field(field)) != E_OK) &&
4419           (res == E_OK))
4420         res = syncres;
4421       if (((syncres = Synchronize_Linked_Fields(field)) != E_OK) &&
4422           (res == E_OK))
4423         res = syncres;
4424     }
4425   RETURN(res);
4426 }
4427
4428 /*---------------------------------------------------------------------------
4429 |   Facility      :  libnform
4430 |   Function      :  char *field_buffer(const FIELD *field,int buffer)
4431 |
4432 |   Description   :  Return the address of the buffer for the field.
4433 |
4434 |   Return Values :  Pointer to buffer or NULL if arguments were invalid.
4435 +--------------------------------------------------------------------------*/
4436 NCURSES_EXPORT(char *)
4437 field_buffer(const FIELD *field, int buffer)
4438 {
4439   char *result = 0;
4440
4441   T((T_CALLED("field_buffer(%p,%d)"), field, buffer));
4442
4443   if (field && (buffer >= 0) && (buffer <= field->nbuf))
4444     {
4445 #if USE_WIDEC_SUPPORT
4446       FIELD_CELL *data = Address_Of_Nth_Buffer(field, buffer);
4447       unsigned need = 0;
4448       int size = Buffer_Length(field);
4449       int n;
4450
4451       /* determine the number of bytes needed to store the expanded string */
4452       for (n = 0; n < size; ++n)
4453         {
4454           if (!isWidecExt(data[n]))
4455             {
4456               mbstate_t state;
4457               size_t next;
4458
4459               init_mb(state);
4460               next = _nc_wcrtomb(0, data[n].chars[0], &state);
4461               if (!isEILSEQ(next))
4462                 need += next;
4463             }
4464         }
4465
4466       /* allocate a place to store the expanded string */
4467       if (field->expanded[buffer] != 0)
4468         free(field->expanded[buffer]);
4469       field->expanded[buffer] = typeMalloc(char, need + 1);
4470
4471       /* expand the multibyte data */
4472       if ((result = field->expanded[buffer]) != 0)
4473         {
4474           wclear(field->working);
4475           mvwadd_wchnstr(field->working, 0, 0, data, size);
4476           mvwinnstr(field->working, 0, 0, result, (int)need);
4477         }
4478 #else
4479       result = Address_Of_Nth_Buffer(field, buffer);
4480 #endif
4481     }
4482   returnPtr(result);
4483 }
4484
4485 #if USE_WIDEC_SUPPORT
4486
4487 /* FIXME: see lib_get_wch.c */
4488 #if HAVE_MBTOWC && HAVE_MBLEN
4489 #define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
4490 #define count_mbytes(buffer,length,state) mblen(buffer,length)
4491 #define trans_mbytes(wch,buffer,length,state) \
4492         (int) mbtowc(&wch, buffer, length)
4493 #elif HAVE_MBRTOWC && HAVE_MBRLEN
4494 #define NEED_STATE
4495 #define reset_mbytes(state) init_mb(state)
4496 #define count_mbytes(buffer,length,state) mbrlen(buffer,length,&state)
4497 #define trans_mbytes(wch,buffer,length,state) \
4498         (int) mbrtowc(&wch, buffer, length, &state)
4499 #else
4500 make an error
4501 #endif
4502
4503 /*---------------------------------------------------------------------------
4504 | Convert a multibyte string to a wide-character string.  The result must be
4505 | freed by the caller.
4506 +--------------------------------------------------------------------------*/
4507 NCURSES_EXPORT(wchar_t *)
4508 _nc_Widen_String(char *source, int *lengthp)
4509 {
4510   wchar_t *result = 0;
4511   wchar_t wch;
4512   size_t given = strlen(source);
4513   size_t tries;
4514   int pass;
4515   int status;
4516
4517 #ifdef NEED_STATE
4518   mbstate_t state;
4519 #endif
4520
4521   for (pass = 0; pass < 2; ++pass)
4522     {
4523       unsigned need = 0;
4524       size_t passed = 0;
4525
4526       while (passed < given)
4527         {
4528           bool found = FALSE;
4529
4530           for (tries = 1, status = 0; tries <= (given - passed); ++tries)
4531             {
4532               int save = source[passed + tries];
4533
4534               source[passed + tries] = 0;
4535               reset_mbytes(state);
4536               status = trans_mbytes(wch, source + passed, tries, state);
4537               source[passed + tries] = save;
4538
4539               if (status > 0)
4540                 {
4541                   found = TRUE;
4542                   break;
4543                 }
4544             }
4545           if (found)
4546             {
4547               if (pass)
4548                 {
4549                   result[need] = wch;
4550                 }
4551               passed += status;
4552               ++need;
4553             }
4554           else
4555             {
4556               if (pass)
4557                 {
4558                   result[need] = source[passed];
4559                 }
4560               ++need;
4561               ++passed;
4562             }
4563         }
4564
4565       if (!pass)
4566         {
4567           if (!need)
4568             break;
4569           result = typeCalloc(wchar_t, need);
4570
4571           *lengthp = need;
4572           if (result == 0)
4573             break;
4574         }
4575     }
4576
4577   return result;
4578 }
4579 #endif
4580
4581 /* frm_driver.c ends here */