]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/frm_driver.c
ncurses 6.1 - patch 20180428
[ncurses.git] / form / frm_driver.c
1 /****************************************************************************
2  * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *   Author:  Juergen Pfeifer, 1995,1997                                    *
31  ****************************************************************************/
32
33 #include "form.priv.h"
34
35 MODULE_ID("$Id: frm_driver.c,v 1.126 2018/04/28 19:03:36 Leon.Winter Exp $")
36
37 /*----------------------------------------------------------------------------
38   This is the core module of the form library. It contains the majority
39   of the driver routines as well as the form_driver function.
40
41   Essentially this module is nearly the whole library. This is because
42   all the functions in this module depends on some others in the module,
43   so it makes no sense to split them into separate files because they
44   will always be linked together. The only acceptable concern is turnaround
45   time for this module, but now we have all Pentiums or RISCs, so what!
46
47   The driver routines are grouped into nine generic categories:
48
49    a)   Page Navigation            ( all functions prefixed by PN_ )
50         The current page of the form is left and some new page is
51         entered.
52    b)   Inter-Field Navigation     ( all functions prefixed by FN_ )
53         The current field of the form is left and some new field is
54         entered.
55    c)   Intra-Field Navigation     ( all functions prefixed by IFN_ )
56         The current position in the current field is changed.
57    d)   Vertical Scrolling         ( all functions prefixed by VSC_ )
58         Essentially this is a specialization of Intra-Field navigation.
59         It has to check for a multi-line field.
60    e)   Horizontal Scrolling       ( all functions prefixed by HSC_ )
61         Essentially this is a specialization of Intra-Field navigation.
62         It has to check for a single-line field.
63    f)   Field Editing              ( all functions prefixed by FE_ )
64         The content of the current field is changed
65    g)   Edit Mode requests         ( all functions prefixed by EM_ )
66         Switching between insert and overlay mode
67    h)   Field-Validation requests  ( all functions prefixed by FV_ )
68         Perform verifications of the field.
69    i)   Choice requests            ( all functions prefixed by CR_ )
70         Requests to enumerate possible field values
71   --------------------------------------------------------------------------*/
72
73 /*----------------------------------------------------------------------------
74   Some remarks on the placements of assert() macros :
75   I use them only on "strategic" places, i.e. top level entries where
76   I want to make sure that things are set correctly. Throughout subordinate
77   routines I omit them mostly.
78   --------------------------------------------------------------------------*/
79
80 /*
81 Some options that may effect compatibility in behavior to SVr4 forms,
82 but they are here to allow a more intuitive and user friendly behavior of
83 our form implementation. This doesn't affect the API, so we feel it is
84 uncritical.
85
86 The initial implementation tries to stay very close with the behavior
87 of the original SVr4 implementation, although in some areas it is quite
88 clear that this isn't the most appropriate way. As far as possible this
89 sources will allow you to build a forms lib that behaves quite similar
90 to SVr4, but now and in the future we will give you better options.
91 Perhaps at some time we will make this configurable at runtime.
92 */
93
94 /* Implement a more user-friendly previous/next word behavior */
95 #define FRIENDLY_PREV_NEXT_WORD (1)
96 /* Fix the wrong behavior for forms with all fields inactive */
97 #define FIX_FORM_INACTIVE_BUG (1)
98 /* Allow dynamic field growth also when navigating past the end */
99 #define GROW_IF_NAVIGATE (1)
100
101 #if USE_WIDEC_SUPPORT
102 #define myADDNSTR(w, s, n) wide_waddnstr(w, s, n)
103 #define myINSNSTR(w, s, n) wide_winsnstr(w, s, n)
104 #define myINNSTR(w, s, n)  wide_winnstr(w, s, n)
105 #define myWCWIDTH(w, y, x) cell_width(w, y, x)
106 #else
107 #define myADDNSTR(w, s, n) waddnstr(w, s, n)
108 #define myINSNSTR(w, s, n) winsnstr(w, s, n)
109 #define myINNSTR(w, s, n)  winnstr(w, s, n)
110 #define myWCWIDTH(w, y, x) 1
111 #endif
112
113 /*----------------------------------------------------------------------------
114   Forward references to some internally used static functions
115   --------------------------------------------------------------------------*/
116 static int Inter_Field_Navigation(int (*const fct) (FORM *), FORM *form);
117 static int FN_Next_Field(FORM *form);
118 static int FN_Previous_Field(FORM *form);
119 static int FE_New_Line(FORM *);
120 static int FE_Delete_Previous(FORM *);
121
122 /*----------------------------------------------------------------------------
123   Macro Definitions.
124
125   Some Remarks on that: I use the convention to use UPPERCASE for constants
126   defined by Macros. If I provide a macro as a kind of inline routine to
127   provide some logic, I use my Upper_Lower case style.
128   --------------------------------------------------------------------------*/
129
130 /* Calculate the position of a single row in a field buffer */
131 #define Position_Of_Row_In_Buffer(field,row) ((row)*(field)->dcols)
132
133 /* Calculate start address for the fields buffer# N */
134 #define Address_Of_Nth_Buffer(field,N) \
135   ((field)->buf + (N)*(1+Buffer_Length(field)))
136
137 /* Calculate the start address of the row in the fields specified buffer# N */
138 #define Address_Of_Row_In_Nth_Buffer(field,N,row) \
139   (Address_Of_Nth_Buffer(field,N) + Position_Of_Row_In_Buffer(field,row))
140
141 /* Calculate the start address of the row in the fields primary buffer */
142 #define Address_Of_Row_In_Buffer(field,row) \
143   Address_Of_Row_In_Nth_Buffer(field,0,row)
144
145 /* Calculate the start address of the row in the forms current field
146    buffer# N */
147 #define Address_Of_Current_Row_In_Nth_Buffer(form,N) \
148    Address_Of_Row_In_Nth_Buffer((form)->current,N,(form)->currow)
149
150 /* Calculate the start address of the row in the forms current field
151    primary buffer */
152 #define Address_Of_Current_Row_In_Buffer(form) \
153    Address_Of_Current_Row_In_Nth_Buffer(form,0)
154
155 /* Calculate the address of the cursor in the forms current field
156    primary buffer */
157 #define Address_Of_Current_Position_In_Nth_Buffer(form,N) \
158    (Address_Of_Current_Row_In_Nth_Buffer(form,N) + (form)->curcol)
159
160 /* Calculate the address of the cursor in the forms current field
161    buffer# N */
162 #define Address_Of_Current_Position_In_Buffer(form) \
163   Address_Of_Current_Position_In_Nth_Buffer(form,0)
164
165 /* Logic to decide whether or not a field is actually a field with
166    vertical or horizontal scrolling */
167 #define Is_Scroll_Field(field)          \
168    (((field)->drows > (field)->rows) || \
169     ((field)->dcols > (field)->cols))
170
171 /* Logic to decide whether or not a field needs to have an individual window
172    instead of a derived window because it contains invisible parts.
173    This is true for non-public fields and for scrollable fields. */
174 #define Has_Invisible_Parts(field)     \
175   (!(Field_Has_Option(field, O_PUBLIC)) || \
176    Is_Scroll_Field(field))
177
178 /* Logic to decide whether or not a field needs justification */
179 #define Justification_Allowed(field)        \
180    (((field)->just != NO_JUSTIFICATION)  && \
181     (Single_Line_Field(field))           && \
182     ((Field_Has_Option(field, O_STATIC)  && \
183      ((field)->dcols == (field)->cols))  || \
184     Field_Has_Option(field, O_DYNAMIC_JUSTIFY)))
185
186 /* Logic to determine whether or not a dynamic field may still grow */
187 #define Growable(field) ((field)->status & _MAY_GROW)
188
189 /* Macro to set the attributes for a fields window */
190 #define Set_Field_Window_Attributes(field,win) \
191 (  wbkgdset((win),(chtype)((chtype)((field)->pad) | (field)->back)), \
192    (void) wattrset((win), (int)(field)->fore) )
193
194 /* Logic to decide whether or not a field really appears on the form */
195 #define Field_Really_Appears(field)         \
196   ((field->form)                          &&\
197    (field->form->status & _POSTED)        &&\
198    (Field_Has_Option(field, O_VISIBLE))   &&\
199    (field->page == field->form->curpage))
200
201 /* Logic to determine whether or not we are on the first position in the
202    current field */
203 #define First_Position_In_Current_Field(form) \
204   (((form)->currow==0) && ((form)->curcol==0))
205
206 #define Minimum(a,b) (((a)<=(b)) ? (a) : (b))
207 #define Maximum(a,b) (((a)>=(b)) ? (a) : (b))
208
209 /*----------------------------------------------------------------------------
210   Useful constants
211   --------------------------------------------------------------------------*/
212 static FIELD_CELL myBLANK = BLANK;
213 static FIELD_CELL myZEROS;
214
215 #ifdef TRACE
216 static void
217 check_pos(FORM *form, int lineno)
218 {
219   int y, x;
220
221   if (form && form->w)
222     {
223       getyx(form->w, y, x);
224       if (y != form->currow || x != form->curcol)
225         {
226           T(("CHECKPOS %s@%d have position %d,%d vs want %d,%d",
227              __FILE__, lineno,
228              y, x,
229              form->currow, form->curcol));
230         }
231     }
232 }
233 #define CHECKPOS(form) check_pos(form, __LINE__)
234 #else
235 #define CHECKPOS(form)          /* nothing */
236 #endif
237
238 /*----------------------------------------------------------------------------
239   Wide-character special functions
240   --------------------------------------------------------------------------*/
241 #if USE_WIDEC_SUPPORT
242 /* add like waddnstr, but using cchar_t* rather than char*
243  */
244 static int
245 wide_waddnstr(WINDOW *w, const cchar_t *s, int n)
246 {
247   int rc = OK;
248
249   while (n-- > 0)
250     {
251       if ((rc = wadd_wch(w, s)) != OK)
252         break;
253       ++s;
254     }
255   return rc;
256 }
257
258 /* insert like winsnstr, but using cchar_t* rather than char*
259  *
260  * X/Open Curses has no close equivalent; inserts are done only with wchar_t
261  * strings.
262  */
263 static int
264 wide_winsnstr(WINDOW *w, const cchar_t *s, int n)
265 {
266   int code = ERR;
267   int y, x;
268
269   while (n-- > 0)
270     {
271       getyx(w, y, x);
272       if ((code = wins_wch(w, s++)) != OK)
273         break;
274       if ((code = wmove(w, y, x + 1)) != OK)
275         break;
276     }
277   return code;
278 }
279
280 /* retrieve like winnstr, but using cchar_t*, rather than char*.
281  *
282  * X/Open Curses' closest equivalent, win_wchnstr(), is inconsistent with
283  * winnstr(), since it returns OK rather than the number of items transferred.
284  */
285 static int
286 wide_winnstr(WINDOW *w, cchar_t *s, int n)
287 {
288   int x;
289
290   win_wchnstr(w, s, n);
291   /*
292    * This function is used to extract the text only from the window.
293    * Strip attributes and color from the string so they will not be added
294    * back when copying the string to the window.
295    */
296   for (x = 0; x < n; ++x)
297     {
298       RemAttr(s[x], A_ATTRIBUTES);
299       SetPair(s[x], 0);
300     }
301   return n;
302 }
303
304 /*
305  * Returns the column of the base of the given cell.
306  */
307 static int
308 cell_base(WINDOW *win, int y, int x)
309 {
310   int result = x;
311
312   while (LEGALYX(win, y, x))
313     {
314       cchar_t *data = &(win->_line[y].text[x]);
315
316       if (isWidecBase(CHDEREF(data)) || !isWidecExt(CHDEREF(data)))
317         {
318           result = x;
319           break;
320         }
321       --x;
322     }
323   return result;
324 }
325
326 /*
327  * Returns the number of columns needed for the given cell in a window.
328  */
329 static int
330 cell_width(WINDOW *win, int y, int x)
331 {
332   int result = 1;
333
334   if (LEGALYX(win, y, x))
335     {
336       cchar_t *data = &(win->_line[y].text[x]);
337
338       if (isWidecExt(CHDEREF(data)))
339         {
340           /* recur, providing the number of columns to the next character */
341           result = cell_width(win, y, x - 1);
342         }
343       else
344         {
345           result = wcwidth(CharOf(CHDEREF(data)));
346         }
347     }
348   return result;
349 }
350
351 /*
352  * There is no wide-character function such as wdel_wch(), so we must find
353  * all of the cells that comprise a multi-column character and delete them
354  * one-by-one.
355  */
356 static void
357 delete_char(FORM *form)
358 {
359   int cells = cell_width(form->w, form->currow, form->curcol);
360
361   form->curcol = cell_base(form->w, form->currow, form->curcol);
362   wmove(form->w, form->currow, form->curcol);
363   while (cells-- > 0)
364     {
365       wdelch(form->w);
366     }
367 }
368 #define DeleteChar(form) delete_char(form)
369 #else
370 #define DeleteChar(form) \
371           wmove((form)->w, (form)->currow, (form)->curcol), \
372           wdelch((form)->w)
373 #endif
374
375 /*---------------------------------------------------------------------------
376 |   Facility      :  libnform
377 |   Function      :  static char *Get_Start_Of_Data(char * buf, int blen)
378 |
379 |   Description   :  Return pointer to first non-blank position in buffer.
380 |                    If buffer is empty return pointer to buffer itself.
381 |
382 |   Return Values :  Pointer to first non-blank position in buffer
383 +--------------------------------------------------------------------------*/
384 NCURSES_INLINE static FIELD_CELL *
385 Get_Start_Of_Data(FIELD_CELL *buf, int blen)
386 {
387   FIELD_CELL *p = buf;
388   FIELD_CELL *end = &buf[blen];
389
390   assert(buf && blen >= 0);
391   while ((p < end) && ISBLANK(*p))
392     p++;
393   return ((p == end) ? buf : p);
394 }
395
396 /*---------------------------------------------------------------------------
397 |   Facility      :  libnform
398 |   Function      :  static char *After_End_Of_Data(char * buf, int blen)
399 |
400 |   Description   :  Return pointer after last non-blank position in buffer.
401 |                    If buffer is empty, return pointer to buffer itself.
402 |
403 |   Return Values :  Pointer to position after last non-blank position in
404 |                    buffer.
405 +--------------------------------------------------------------------------*/
406 NCURSES_INLINE static FIELD_CELL *
407 After_End_Of_Data(FIELD_CELL *buf, int blen)
408 {
409   FIELD_CELL *p = &buf[blen];
410
411   assert(buf && blen >= 0);
412   while ((p > buf) && ISBLANK(p[-1]))
413     p--;
414   return (p);
415 }
416
417 /*---------------------------------------------------------------------------
418 |   Facility      :  libnform
419 |   Function      :  static char *Get_First_Whitespace_Character(
420 |                                     char * buf, int   blen)
421 |
422 |   Description   :  Position to the first whitespace character.
423 |
424 |   Return Values :  Pointer to first whitespace character in buffer.
425 +--------------------------------------------------------------------------*/
426 NCURSES_INLINE static FIELD_CELL *
427 Get_First_Whitespace_Character(FIELD_CELL *buf, int blen)
428 {
429   FIELD_CELL *p = buf;
430   FIELD_CELL *end = &p[blen];
431
432   assert(buf && blen >= 0);
433   while ((p < end) && !ISBLANK(*p))
434     p++;
435   return ((p == end) ? buf : p);
436 }
437
438 /*---------------------------------------------------------------------------
439 |   Facility      :  libnform
440 |   Function      :  static char *After_Last_Whitespace_Character(
441 |                                     char * buf, int blen)
442 |
443 |   Description   :  Get the position after the last whitespace character.
444 |
445 |   Return Values :  Pointer to position after last whitespace character in
446 |                    buffer.
447 +--------------------------------------------------------------------------*/
448 NCURSES_INLINE static FIELD_CELL *
449 After_Last_Whitespace_Character(FIELD_CELL *buf, int blen)
450 {
451   FIELD_CELL *p = &buf[blen];
452
453   assert(buf && blen >= 0);
454   while ((p > buf) && !ISBLANK(p[-1]))
455     p--;
456   return (p);
457 }
458
459 /* Set this to 1 to use the div_t version. This is a good idea if your
460    compiler has an intrinsic div() support. Unfortunately GNU-C has it
461    not yet.
462    N.B.: This only works if form->curcol follows immediately form->currow
463          and both are of type int.
464 */
465 #define USE_DIV_T (0)
466
467 /*---------------------------------------------------------------------------
468 |   Facility      :  libnform
469 |   Function      :  static void Adjust_Cursor_Position(
470 |                                       FORM * form, const char * pos)
471 |
472 |   Description   :  Set current row and column of the form to values
473 |                    corresponding to the buffer position.
474 |
475 |   Return Values :  -
476 +--------------------------------------------------------------------------*/
477 NCURSES_INLINE static void
478 Adjust_Cursor_Position(FORM *form, const FIELD_CELL *pos)
479 {
480   FIELD *field;
481   int idx;
482
483   field = form->current;
484   assert(pos >= field->buf && field->dcols > 0);
485   idx = (int)(pos - field->buf);
486 #if USE_DIV_T
487   *((div_t *) & (form->currow)) = div(idx, field->dcols);
488 #else
489   form->currow = idx / field->dcols;
490   form->curcol = idx - field->cols * form->currow;
491 #endif
492   if (field->drows < form->currow)
493     form->currow = 0;
494 }
495
496 /*---------------------------------------------------------------------------
497 |   Facility      :  libnform
498 |   Function      :  static void Buffer_To_Window(
499 |                                      const FIELD  * field,
500 |                                      WINDOW * win)
501 |
502 |   Description   :  Copy the buffer to the window. If it is a multi-line
503 |                    field, the buffer is split to the lines of the
504 |                    window without any editing.
505 |
506 |   Return Values :  -
507 +--------------------------------------------------------------------------*/
508 static void
509 Buffer_To_Window(const FIELD *field, WINDOW *win)
510 {
511   int width, height;
512   int y, x;
513   int len;
514   int row;
515   FIELD_CELL *pBuffer;
516
517   assert(win && field);
518
519   getyx(win, y, x);
520   width = getmaxx(win);
521   height = getmaxy(win);
522
523   for (row = 0, pBuffer = field->buf;
524        row < height;
525        row++, pBuffer += width)
526     {
527       if ((len = (int)(After_End_Of_Data(pBuffer, width) - pBuffer)) > 0)
528         {
529           wmove(win, row, 0);
530           myADDNSTR(win, pBuffer, len);
531         }
532     }
533   wmove(win, y, x);
534 }
535
536 /*---------------------------------------------------------------------------
537 |   Facility      :  libnform
538 |   Function      :  void _nc_get_fieldbuffer(
539 |                                          WINDOW * win,
540 |                                          FIELD  * field,
541 |                                          FIELD_CELL * buf)
542 |
543 |   Description   :  Copy the content of the window into the buffer.
544 |                    The multiple lines of a window are simply
545 |                    concatenated into the buffer. Pad characters in
546 |                    the window will be replaced by blanks in the buffer.
547 |
548 |   Return Values :  -
549 +--------------------------------------------------------------------------*/
550 NCURSES_EXPORT(void)
551 _nc_get_fieldbuffer(FORM *form, FIELD *field, FIELD_CELL *buf)
552 {
553   int pad;
554   int len = 0;
555   FIELD_CELL *p;
556   int row, height;
557   WINDOW *win;
558
559   assert(form && field && buf);
560
561   win = form->w;
562   assert(win);
563
564   pad = field->pad;
565   p = buf;
566   height = getmaxy(win);
567
568   for (row = 0; (row < height) && (row < field->drows); row++)
569     {
570       wmove(win, row, 0);
571       len += myINNSTR(win, p + len, field->dcols);
572     }
573   p[len] = myZEROS;
574
575   /* replace visual padding character by blanks in buffer */
576   if (pad != C_BLANK)
577     {
578       int i;
579
580       for (i = 0; i < len; i++, p++)
581         {
582           if ((unsigned long)CharOf(*p) == ChCharOf(pad)
583 #if USE_WIDEC_SUPPORT
584               && p->chars[1] == 0
585 #endif
586             )
587             *p = myBLANK;
588         }
589     }
590 }
591
592 /*---------------------------------------------------------------------------
593 |   Facility      :  libnform
594 |   Function      :  static void Window_To_Buffer(
595 |                                          FORM   * form,
596 |                                          FIELD  * field)
597 |
598 |   Description   :  Copy the content of the window into the buffer.
599 |                    The multiple lines of a window are simply
600 |                    concatenated into the buffer. Pad characters in
601 |                    the window will be replaced by blanks in the buffer.
602 |
603 |   Return Values :  -
604 +--------------------------------------------------------------------------*/
605 static void
606 Window_To_Buffer(FORM *form, FIELD *field)
607 {
608   _nc_get_fieldbuffer(form, field, field->buf);
609 }
610
611 /*---------------------------------------------------------------------------
612 |   Facility      :  libnform
613 |   Function      :  static void Synchronize_Buffer(FORM * form)
614 |
615 |   Description   :  If there was a change, copy the content of the
616 |                    window into the buffer, so the buffer is synchronized
617 |                    with the windows content. We have to indicate that the
618 |                    buffer needs validation due to the change.
619 |
620 |   Return Values :  -
621 +--------------------------------------------------------------------------*/
622 NCURSES_INLINE static void
623 Synchronize_Buffer(FORM *form)
624 {
625   if (form->status & _WINDOW_MODIFIED)
626     {
627       ClrStatus(form, _WINDOW_MODIFIED);
628       SetStatus(form, _FCHECK_REQUIRED);
629       Window_To_Buffer(form, form->current);
630       wmove(form->w, form->currow, form->curcol);
631     }
632 }
633
634 /*---------------------------------------------------------------------------
635 |   Facility      :  libnform
636 |   Function      :  static bool Field_Grown( FIELD *field, int amount)
637 |
638 |   Description   :  This function is called for growable dynamic fields
639 |                    only. It has to increase the buffers and to allocate
640 |                    a new window for this field.
641 |                    This function has the side effect to set a new
642 |                    field-buffer pointer, the dcols and drows values
643 |                    as well as a new current Window for the field.
644 |
645 |   Return Values :  TRUE     - field successfully increased
646 |                    FALSE    - there was some error
647 +--------------------------------------------------------------------------*/
648 static bool
649 Field_Grown(FIELD *field, int amount)
650 {
651   bool result = FALSE;
652
653   if (field && Growable(field))
654     {
655       bool single_line_field = Single_Line_Field(field);
656       int old_buflen = Buffer_Length(field);
657       int new_buflen;
658       int old_dcols = field->dcols;
659       int old_drows = field->drows;
660       FIELD_CELL *oldbuf = field->buf;
661       FIELD_CELL *newbuf;
662
663       int growth;
664       FORM *form = field->form;
665       bool need_visual_update = ((form != (FORM *)0) &&
666                                  (form->status & _POSTED) &&
667                                  (form->current == field));
668
669       if (need_visual_update)
670         Synchronize_Buffer(form);
671
672       if (single_line_field)
673         {
674           growth = field->cols * amount;
675           if (field->maxgrow)
676             growth = Minimum(field->maxgrow - field->dcols, growth);
677           field->dcols += growth;
678           if (field->dcols == field->maxgrow)
679             ClrStatus(field, _MAY_GROW);
680         }
681       else
682         {
683           growth = (field->rows + field->nrow) * amount;
684           if (field->maxgrow)
685             growth = Minimum(field->maxgrow - field->drows, growth);
686           field->drows += growth;
687           if (field->drows == field->maxgrow)
688             ClrStatus(field, _MAY_GROW);
689         }
690       /* drows, dcols changed, so we get really the new buffer length */
691       new_buflen = Buffer_Length(field);
692       newbuf = (FIELD_CELL *)malloc(Total_Buffer_Size(field));
693       if (!newbuf)
694         {
695           /* restore to previous state */
696           field->dcols = old_dcols;
697           field->drows = old_drows;
698           if ((single_line_field && (field->dcols != field->maxgrow)) ||
699               (!single_line_field && (field->drows != field->maxgrow)))
700             SetStatus(field, _MAY_GROW);
701         }
702       else
703         {
704           /* Copy all the buffers.  This is the reason why we can't just use
705            * realloc().
706            */
707           int i, j;
708           FIELD_CELL *old_bp;
709           FIELD_CELL *new_bp;
710
711           result = TRUE;        /* allow sharing of recovery on failure */
712
713           T((T_CREATE("fieldcell %p"), (void *)newbuf));
714           field->buf = newbuf;
715           for (i = 0; i <= field->nbuf; i++)
716             {
717               new_bp = Address_Of_Nth_Buffer(field, i);
718               old_bp = oldbuf + i * (1 + old_buflen);
719               for (j = 0; j < old_buflen; ++j)
720                 new_bp[j] = old_bp[j];
721               while (j < new_buflen)
722                 new_bp[j++] = myBLANK;
723               new_bp[new_buflen] = myZEROS;
724             }
725
726 #if USE_WIDEC_SUPPORT && NCURSES_EXT_FUNCS
727           if (wresize(field->working, 1, Buffer_Length(field) + 1) == ERR)
728             result = FALSE;
729 #endif
730
731           if (need_visual_update && result)
732             {
733               WINDOW *new_window = newpad(field->drows, field->dcols);
734
735               if (new_window != 0)
736                 {
737                   assert(form != (FORM *)0);
738                   if (form->w)
739                     delwin(form->w);
740                   form->w = new_window;
741                   Set_Field_Window_Attributes(field, form->w);
742                   werase(form->w);
743                   Buffer_To_Window(field, form->w);
744                   untouchwin(form->w);
745                   wmove(form->w, form->currow, form->curcol);
746                 }
747               else
748                 result = FALSE;
749             }
750
751           if (result)
752             {
753               free(oldbuf);
754               /* reflect changes in linked fields */
755               if (field != field->link)
756                 {
757                   FIELD *linked_field;
758
759                   for (linked_field = field->link;
760                        linked_field != field;
761                        linked_field = linked_field->link)
762                     {
763                       linked_field->buf = field->buf;
764                       linked_field->drows = field->drows;
765                       linked_field->dcols = field->dcols;
766                     }
767                 }
768             }
769           else
770             {
771               /* restore old state */
772               field->dcols = old_dcols;
773               field->drows = old_drows;
774               field->buf = oldbuf;
775               if ((single_line_field &&
776                    (field->dcols != field->maxgrow)) ||
777                   (!single_line_field &&
778                    (field->drows != field->maxgrow)))
779                 SetStatus(field, _MAY_GROW);
780               free(newbuf);
781             }
782         }
783     }
784   return (result);
785 }
786
787 #ifdef NCURSES_MOUSE_VERSION
788 /*---------------------------------------------------------------------------
789 |   Facility      :  libnform
790 |   Function      :  int Field_encloses(FIELD *field, int ry, int rx)
791 |
792 |   Description   :  Check if the given coordinates lie within the given field.
793 |
794 |   Return Values :  E_OK              - success
795 |                    E_BAD_ARGUMENT    - invalid form pointer
796 |                    E_SYSTEM_ERROR    - form has no current field or
797 |                                        field-window
798 +--------------------------------------------------------------------------*/
799 static int
800 Field_encloses(FIELD *field, int ry, int rx)
801 {
802   T((T_CALLED("Field_encloses(%p)"), (void *)field));
803   if (field != 0
804       && field->frow <= ry
805       && (field->frow + field->rows) > ry
806       && field->fcol <= rx
807       && (field->fcol + field->cols) > rx)
808     {
809       RETURN(E_OK);
810     }
811   RETURN(E_INVALID_FIELD);
812 }
813 #endif
814
815 /*---------------------------------------------------------------------------
816 |   Facility      :  libnform
817 |   Function      :  int _nc_Position_Form_Cursor(FORM * form)
818 |
819 |   Description   :  Position the cursor in the window for the current
820 |                    field to be in sync. with the currow and curcol
821 |                    values.
822 |
823 |   Return Values :  E_OK              - success
824 |                    E_BAD_ARGUMENT    - invalid form pointer
825 |                    E_SYSTEM_ERROR    - form has no current field or
826 |                                        field-window
827 +--------------------------------------------------------------------------*/
828 NCURSES_EXPORT(int)
829 _nc_Position_Form_Cursor(FORM *form)
830 {
831   FIELD *field;
832   WINDOW *formwin;
833   int row, col;
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   col = Field_Has_Option(field, O_PUBLIC) ? form->curcol : form->begincol;
845   row = Field_Has_Option(field, O_PUBLIC) ? form->currow : form->toprow;
846
847   wmove(form->w, row, col);
848   if (Has_Invisible_Parts(field))
849     {
850       /* in this case fieldwin isn't derived from formwin, so we have
851          to move the cursor in formwin by hand... */
852       wmove(formwin,
853             field->frow + row - form->toprow,
854             field->fcol + col - form->begincol);
855       wcursyncup(formwin);
856     }
857   else
858     wcursyncup(form->w);
859   return (E_OK);
860 }
861
862 /*---------------------------------------------------------------------------
863 |   Facility      :  libnform
864 |   Function      :  int _nc_Refresh_Current_Field(FORM * form)
865 |
866 |   Description   :  Propagate the changes in the fields window to the
867 |                    window of the form.
868 |
869 |   Return Values :  E_OK              - on success
870 |                    E_BAD_ARGUMENT    - invalid form pointer
871 |                    E_SYSTEM_ERROR    - general error
872 +--------------------------------------------------------------------------*/
873 static bool move_after_insert = true;
874 NCURSES_EXPORT(int)
875 _nc_Refresh_Current_Field(FORM *form)
876 {
877   WINDOW *formwin;
878   FIELD *field;
879
880   T((T_CALLED("_nc_Refresh_Current_Field(%p)"), (void *)form));
881
882   if (!form)
883     RETURN(E_BAD_ARGUMENT);
884
885   if (!form->w || !form->current)
886     RETURN(E_SYSTEM_ERROR);
887
888   field = form->current;
889   formwin = Get_Form_Window(form);
890
891   if (Field_Has_Option(field, O_PUBLIC))
892     {
893       if (Is_Scroll_Field(field))
894         {
895           /* Again, in this case the fieldwin isn't derived from formwin,
896              so we have to perform a copy operation. */
897           if (Single_Line_Field(field))
898             {
899               /* horizontal scrolling */
900               if (form->curcol < form->begincol)
901                 form->begincol = form->curcol;
902               else
903                 {
904                   if (form->curcol >= (form->begincol + field->cols))
905                     form->begincol = form->curcol - field->cols
906                       + (move_after_insert ? 1 : 0);
907                 }
908               copywin(form->w,
909                       formwin,
910                       0,
911                       form->begincol,
912                       field->frow,
913                       field->fcol,
914                       field->frow,
915                       field->cols + field->fcol - 1,
916                       0);
917             }
918           else
919             {
920               /* A multi-line, i.e. vertical scrolling field */
921               int row_after_bottom, first_modified_row, first_unmodified_row;
922
923               if (field->drows > field->rows)
924                 {
925                   row_after_bottom = form->toprow + field->rows;
926                   if (form->currow < form->toprow)
927                     {
928                       form->toprow = form->currow;
929                       SetStatus(field, _NEWTOP);
930                     }
931                   if (form->currow >= row_after_bottom)
932                     {
933                       form->toprow = form->currow - field->rows + 1;
934                       SetStatus(field, _NEWTOP);
935                     }
936                   if (field->status & _NEWTOP)
937                     {
938                       /* means we have to copy whole range */
939                       first_modified_row = form->toprow;
940                       first_unmodified_row = first_modified_row + field->rows;
941                       ClrStatus(field, _NEWTOP);
942                     }
943                   else
944                     {
945                       /* we try to optimize : finding the range of touched
946                          lines */
947                       first_modified_row = form->toprow;
948                       while (first_modified_row < row_after_bottom)
949                         {
950                           if (is_linetouched(form->w, first_modified_row))
951                             break;
952                           first_modified_row++;
953                         }
954                       first_unmodified_row = first_modified_row;
955                       while (first_unmodified_row < row_after_bottom)
956                         {
957                           if (!is_linetouched(form->w, first_unmodified_row))
958                             break;
959                           first_unmodified_row++;
960                         }
961                     }
962                 }
963               else
964                 {
965                   first_modified_row = form->toprow;
966                   first_unmodified_row = first_modified_row + field->rows;
967                 }
968               if (first_unmodified_row != first_modified_row)
969                 copywin(form->w,
970                         formwin,
971                         first_modified_row,
972                         0,
973                         field->frow + first_modified_row - form->toprow,
974                         field->fcol,
975                         field->frow + first_unmodified_row - form->toprow - 1,
976                         field->cols + field->fcol - 1,
977                         0);
978             }
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           wsyncup(form->w);
987         }
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 fields 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 NCURSES_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 fields 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 NCURSES_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 NCURSES_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 NCURSES_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 |                    fields 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 NCURSES_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 fields 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 whats 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 laying 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 |                    its more difficult to define whats 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)