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