1 /****************************************************************************
2 * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer, 1995,1997 *
31 ****************************************************************************/
33 #include "form.priv.h"
35 MODULE_ID("$Id: frm_driver.c,v 1.71 2005/10/01 19:42:40 tom Exp $")
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.
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!
47 The driver routines are grouped into nine generic categories:
49 a) Page Navigation ( all functions prefixed by PN_ )
50 The current page of the form is left and some new page is
52 b) Inter-Field Navigation ( all functions prefixed by FN_ )
53 The current field of the form is left and some new field is
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 --------------------------------------------------------------------------*/
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 --------------------------------------------------------------------------*/
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
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.
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)
101 #if USE_WIDEC_SUPPORT
102 #define myADDNSTR(w, s, n) wadd_wchnstr(w, s, n)
103 #define myINSNSTR(w, s, n) wins_wchnstr(w, s, n)
104 #define myINNSTR(w, s, n) fix_wchnstr(w, s, n)
105 #define myWCWIDTH(w, y, x) cell_width(w, y, x)
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
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 *);
122 /*----------------------------------------------------------------------------
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 --------------------------------------------------------------------------*/
130 /* Calculate the position of a single row in a field buffer */
131 #define Position_Of_Row_In_Buffer(field,row) ((row)*(field)->dcols)
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)))
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))
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)
145 /* Calculate the start address of the row in the forms current field
147 #define Address_Of_Current_Row_In_Nth_Buffer(form,N) \
148 Address_Of_Row_In_Nth_Buffer((form)->current,N,(form)->currow)
150 /* Calculate the start address of the row in the forms current field
152 #define Address_Of_Current_Row_In_Buffer(form) \
153 Address_Of_Current_Row_In_Nth_Buffer(form,0)
155 /* Calculate the address of the cursor in the forms current field
157 #define Address_Of_Current_Position_In_Nth_Buffer(form,N) \
158 (Address_Of_Current_Row_In_Nth_Buffer(form,N) + (form)->curcol)
160 /* Calculate the address of the cursor in the forms current field
162 #define Address_Of_Current_Position_In_Buffer(form) \
163 Address_Of_Current_Position_In_Nth_Buffer(form,0)
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))
171 /* Logic to decide whether or not a field needs to have an individual window
172 instead of a derived window because it contains invisible parts.
173 This is true for non-public fields and for scrollable fields. */
174 #define Has_Invisible_Parts(field) \
175 (!((field)->opts & O_PUBLIC) || \
176 Is_Scroll_Field(field))
178 /* Logic to decide whether or not a field needs justification */
179 #define Justification_Allowed(field) \
180 (((field)->just != NO_JUSTIFICATION) && \
181 (Single_Line_Field(field)) && \
182 (((field)->dcols == (field)->cols) && \
183 ((field)->opts & O_STATIC)) )
185 /* Logic to determine whether or not a dynamic field may still grow */
186 #define Growable(field) ((field)->status & _MAY_GROW)
188 /* Macro to set the attributes for a fields window */
189 #define Set_Field_Window_Attributes(field,win) \
190 ( wbkgdset((win),(chtype)((field)->pad | (field)->back)), \
191 wattrset((win),(field)->fore) )
193 /* Logic to decide whether or not a field really appears on the form */
194 #define Field_Really_Appears(field) \
196 (field->form->status & _POSTED) &&\
197 (field->opts & O_VISIBLE) &&\
198 (field->page == field->form->curpage))
200 /* Logic to determine whether or not we are on the first position in the
202 #define First_Position_In_Current_Field(form) \
203 (((form)->currow==0) && ((form)->curcol==0))
205 #define Minimum(a,b) (((a)<=(b)) ? (a) : (b))
206 #define Maximum(a,b) (((a)>=(b)) ? (a) : (b))
208 /*----------------------------------------------------------------------------
210 --------------------------------------------------------------------------*/
211 static FIELD_CELL myBLANK = BLANK;
212 static FIELD_CELL myZEROS;
216 check_pos(FORM *form, int lineno)
222 getyx(form->w, y, x);
223 if (y != form->currow || x != form->curcol)
225 T(("CHECKPOS %s@%d have position %d,%d vs want %d,%d",
228 form->currow, form->curcol));
232 #define CHECKPOS(form) check_pos(form, __LINE__)
234 #define CHECKPOS(form) /* nothing */
237 /*----------------------------------------------------------------------------
238 Wide-character special functions
239 --------------------------------------------------------------------------*/
240 #if USE_WIDEC_SUPPORT
243 wins_wchnstr(WINDOW *w, cchar_t *s, int n)
251 if ((code = wins_wch(w, s++)) != OK)
253 if ((code = wmove(w, y, x + 1)) != OK)
259 /* win_wchnstr is inconsistent with winnstr, since it returns OK rather than
260 * the number of items transferred.
263 fix_wchnstr(WINDOW *w, cchar_t *s, int n)
265 win_wchnstr(w, s, n);
270 * Returns the column of the base of the given cell.
273 cell_base(WINDOW *win, int y, int x)
277 while (LEGALYX(win, y, x))
279 cchar_t *data = &(win->_line[y].text[x]);
281 if (isWidecBase(CHDEREF(data)) || !isWidecExt(CHDEREF(data)))
292 * Returns the number of columns needed for the given cell in a window.
295 cell_width(WINDOW *win, int y, int x)
299 if (LEGALYX(win, y, x))
301 cchar_t *data = &(win->_line[y].text[x]);
303 if (isWidecExt(CHDEREF(data)))
305 /* recur, providing the number of columns to the next character */
306 result = cell_width(win, y, x - 1);
310 result = wcwidth(CharOf(CHDEREF(data)));
317 * There is no wide-character function such as wdel_wch(), so we must find
318 * all of the cells that comprise a multi-column character and delete them
322 delete_char(FORM *form)
324 int cells = cell_width(form->w, form->currow, form->curcol);
326 form->curcol = cell_base(form->w, form->currow, form->curcol);
327 wmove(form->w, form->currow, form->curcol);
333 #define DeleteChar(form) delete_char(form)
335 #define DeleteChar(form) \
336 wmove((form)->w, (form)->currow, (form)->curcol), \
340 /*---------------------------------------------------------------------------
341 | Facility : libnform
342 | Function : static char *Get_Start_Of_Data(char * buf, int blen)
344 | Description : Return pointer to first non-blank position in buffer.
345 | If buffer is empty return pointer to buffer itself.
347 | Return Values : Pointer to first non-blank position in buffer
348 +--------------------------------------------------------------------------*/
349 INLINE static FIELD_CELL *
350 Get_Start_Of_Data(FIELD_CELL *buf, int blen)
353 FIELD_CELL *end = &buf[blen];
355 assert(buf && blen >= 0);
356 while ((p < end) && ISBLANK(*p))
358 return ((p == end) ? buf : p);
361 /*---------------------------------------------------------------------------
362 | Facility : libnform
363 | Function : static char *After_End_Of_Data(char * buf, int blen)
365 | Description : Return pointer after last non-blank position in buffer.
366 | If buffer is empty, return pointer to buffer itself.
368 | Return Values : Pointer to position after last non-blank position in
370 +--------------------------------------------------------------------------*/
371 INLINE static FIELD_CELL *
372 After_End_Of_Data(FIELD_CELL *buf, int blen)
374 FIELD_CELL *p = &buf[blen];
376 assert(buf && blen >= 0);
377 while ((p > buf) && ISBLANK(p[-1]))
382 /*---------------------------------------------------------------------------
383 | Facility : libnform
384 | Function : static char *Get_First_Whitespace_Character(
385 | char * buf, int blen)
387 | Description : Position to the first whitespace character.
389 | Return Values : Pointer to first whitespace character in buffer.
390 +--------------------------------------------------------------------------*/
391 INLINE static FIELD_CELL *
392 Get_First_Whitespace_Character(FIELD_CELL *buf, int blen)
395 FIELD_CELL *end = &p[blen];
397 assert(buf && blen >= 0);
398 while ((p < end) && !ISBLANK(*p))
400 return ((p == end) ? buf : p);
403 /*---------------------------------------------------------------------------
404 | Facility : libnform
405 | Function : static char *After_Last_Whitespace_Character(
406 | char * buf, int blen)
408 | Description : Get the position after the last whitespace character.
410 | Return Values : Pointer to position after last whitespace character in
412 +--------------------------------------------------------------------------*/
413 INLINE static FIELD_CELL *
414 After_Last_Whitespace_Character(FIELD_CELL *buf, int blen)
416 FIELD_CELL *p = &buf[blen];
418 assert(buf && blen >= 0);
419 while ((p > buf) && !ISBLANK(p[-1]))
424 /* Set this to 1 to use the div_t version. This is a good idea if your
425 compiler has an intrinsic div() support. Unfortunately GNU-C has it
427 N.B.: This only works if form->curcol follows immediately form->currow
428 and both are of type int.
430 #define USE_DIV_T (0)
432 /*---------------------------------------------------------------------------
433 | Facility : libnform
434 | Function : static void Adjust_Cursor_Position(
435 | FORM * form, const char * pos)
437 | Description : Set current row and column of the form to values
438 | corresponding to the buffer position.
441 +--------------------------------------------------------------------------*/
443 Adjust_Cursor_Position(FORM *form, const FIELD_CELL *pos)
448 field = form->current;
449 assert(pos >= field->buf && field->dcols > 0);
450 idx = (int)(pos - field->buf);
452 *((div_t *) & (form->currow)) = div(idx, field->dcols);
454 form->currow = idx / field->dcols;
455 form->curcol = idx - field->cols * form->currow;
457 if (field->drows < form->currow)
461 /*---------------------------------------------------------------------------
462 | Facility : libnform
463 | Function : static void Buffer_To_Window(
464 | const FIELD * field,
467 | Description : Copy the buffer to the window. If it is a multi-line
468 | field, the buffer is split to the lines of the
469 | window without any editing.
472 +--------------------------------------------------------------------------*/
474 Buffer_To_Window(const FIELD *field, WINDOW *win)
482 assert(win && field);
485 width = getmaxx(win);
486 height = getmaxy(win);
488 for (row = 0, pBuffer = field->buf;
490 row++, pBuffer += width)
492 if ((len = (int)(After_End_Of_Data(pBuffer, width) - pBuffer)) > 0)
495 myADDNSTR(win, pBuffer, len);
501 /*---------------------------------------------------------------------------
502 | Facility : libnform
503 | Function : static void Window_To_Buffer(
507 | Description : Copy the content of the window into the buffer.
508 | The multiple lines of a window are simply
509 | concatenated into the buffer. Pad characters in
510 | the window will be replaced by blanks in the buffer.
513 +--------------------------------------------------------------------------*/
515 Window_To_Buffer(WINDOW *win, FIELD *field)
522 assert(win && field && field->buf);
526 height = getmaxy(win);
528 for (row = 0; (row < height) && (row < field->drows); row++)
531 len += myINNSTR(win, p + len, field->dcols);
535 /* replace visual padding character by blanks in buffer */
540 for (i = 0; i < len; i++, p++)
542 if ((unsigned long)CharOf(*p) == ChCharOf(pad)
543 #if USE_WIDEC_SUPPORT
546 && AttrOf(*p) == ChAttrOf(pad))
552 /*---------------------------------------------------------------------------
553 | Facility : libnform
554 | Function : static void Synchronize_Buffer(FORM * form)
556 | Description : If there was a change, copy the content of the
557 | window into the buffer, so the buffer is synchronized
558 | with the windows content. We have to indicate that the
559 | buffer needs validation due to the change.
562 +--------------------------------------------------------------------------*/
564 Synchronize_Buffer(FORM *form)
566 if (form->status & _WINDOW_MODIFIED)
568 form->status &= ~_WINDOW_MODIFIED;
569 form->status |= _FCHECK_REQUIRED;
570 Window_To_Buffer(form->w, form->current);
571 wmove(form->w, form->currow, form->curcol);
575 /*---------------------------------------------------------------------------
576 | Facility : libnform
577 | Function : static bool Field_Grown( FIELD *field, int amount)
579 | Description : This function is called for growable dynamic fields
580 | only. It has to increase the buffers and to allocate
581 | a new window for this field.
582 | This function has the side effect to set a new
583 | field-buffer pointer, the dcols and drows values
584 | as well as a new current Window for the field.
586 | Return Values : TRUE - field successfully increased
587 | FALSE - there was some error
588 +--------------------------------------------------------------------------*/
590 Field_Grown(FIELD *field, int amount)
594 if (field && Growable(field))
596 bool single_line_field = Single_Line_Field(field);
597 int old_buflen = Buffer_Length(field);
599 int old_dcols = field->dcols;
600 int old_drows = field->drows;
601 FIELD_CELL *oldbuf = field->buf;
605 FORM *form = field->form;
606 bool need_visual_update = ((form != (FORM *)0) &&
607 (form->status & _POSTED) &&
608 (form->current == field));
610 if (need_visual_update)
611 Synchronize_Buffer(form);
613 if (single_line_field)
615 growth = field->cols * amount;
617 growth = Minimum(field->maxgrow - field->dcols, growth);
618 field->dcols += growth;
619 if (field->dcols == field->maxgrow)
620 field->status &= ~_MAY_GROW;
624 growth = (field->rows + field->nrow) * amount;
626 growth = Minimum(field->maxgrow - field->drows, growth);
627 field->drows += growth;
628 if (field->drows == field->maxgrow)
629 field->status &= ~_MAY_GROW;
631 /* drows, dcols changed, so we get really the new buffer length */
632 new_buflen = Buffer_Length(field);
633 newbuf = (FIELD_CELL *)malloc(Total_Buffer_Size(field));
636 /* restore to previous state */
637 field->dcols = old_dcols;
638 field->drows = old_drows;
639 if ((single_line_field && (field->dcols != field->maxgrow)) ||
640 (!single_line_field && (field->drows != field->maxgrow)))
641 field->status |= _MAY_GROW;
645 /* Copy all the buffers. This is the reason why we can't just use
652 result = TRUE; /* allow sharing of recovery on failure */
655 for (i = 0; i <= field->nbuf; i++)
657 new_bp = Address_Of_Nth_Buffer(field, i);
658 old_bp = oldbuf + i * (1 + old_buflen);
659 for (j = 0; j < old_buflen; ++j)
660 new_bp[j] = old_bp[j];
661 while (j < new_buflen)
662 new_bp[j++] = myBLANK;
663 new_bp[new_buflen] = myZEROS;
666 #if USE_WIDEC_SUPPORT
667 if (wresize(field->working, 1, Buffer_Length(field) + 1) == ERR)
671 if (need_visual_update && result)
673 WINDOW *new_window = newpad(field->drows, field->dcols);
677 assert(form != (FORM *)0);
680 form->w = new_window;
681 Set_Field_Window_Attributes(field, form->w);
683 Buffer_To_Window(field, form->w);
685 wmove(form->w, form->currow, form->curcol);
694 /* reflect changes in linked fields */
695 if (field != field->link)
699 for (linked_field = field->link;
700 linked_field != field;
701 linked_field = linked_field->link)
703 linked_field->buf = field->buf;
704 linked_field->drows = field->drows;
705 linked_field->dcols = field->dcols;
711 /* restore old state */
712 field->dcols = old_dcols;
713 field->drows = old_drows;
715 if ((single_line_field &&
716 (field->dcols != field->maxgrow)) ||
717 (!single_line_field &&
718 (field->drows != field->maxgrow)))
719 field->status |= _MAY_GROW;
727 /*---------------------------------------------------------------------------
728 | Facility : libnform
729 | Function : int _nc_Position_Form_Cursor(FORM * form)
731 | Description : Position the cursor in the window for the current
732 | field to be in sync. with the currow and curcol
735 | Return Values : E_OK - success
736 | E_BAD_ARGUMENT - invalid form pointer
737 | E_SYSTEM_ERROR - form has no current field or
739 +--------------------------------------------------------------------------*/
741 _nc_Position_Form_Cursor(FORM *form)
747 return (E_BAD_ARGUMENT);
749 if (!form->w || !form->current)
750 return (E_SYSTEM_ERROR);
752 field = form->current;
753 formwin = Get_Form_Window(form);
755 wmove(form->w, form->currow, form->curcol);
756 if (Has_Invisible_Parts(field))
758 /* in this case fieldwin isn't derived from formwin, so we have
759 to move the cursor in formwin by hand... */
761 field->frow + form->currow - form->toprow,
762 field->fcol + form->curcol - form->begincol);
770 /*---------------------------------------------------------------------------
771 | Facility : libnform
772 | Function : int _nc_Refresh_Current_Field(FORM * form)
774 | Description : Propagate the changes in the fields window to the
775 | window of the form.
777 | Return Values : E_OK - on success
778 | E_BAD_ARGUMENT - invalid form pointer
779 | E_SYSTEM_ERROR - general error
780 +--------------------------------------------------------------------------*/
782 _nc_Refresh_Current_Field(FORM *form)
787 T((T_CALLED("_nc_Refresh_Current_Field(%p)"), form));
790 RETURN(E_BAD_ARGUMENT);
792 if (!form->w || !form->current)
793 RETURN(E_SYSTEM_ERROR);
795 field = form->current;
796 formwin = Get_Form_Window(form);
798 if (field->opts & O_PUBLIC)
800 if (Is_Scroll_Field(field))
802 /* Again, in this case the fieldwin isn't derived from formwin,
803 so we have to perform a copy operation. */
804 if (Single_Line_Field(field))
806 /* horizontal scrolling */
807 if (form->curcol < form->begincol)
808 form->begincol = form->curcol;
811 if (form->curcol >= (form->begincol + field->cols))
812 form->begincol = form->curcol - field->cols + 1;
821 field->cols + field->fcol - 1,
826 /* A multi-line, i.e. vertical scrolling field */
827 int row_after_bottom, first_modified_row, first_unmodified_row;
829 if (field->drows > field->rows)
831 row_after_bottom = form->toprow + field->rows;
832 if (form->currow < form->toprow)
834 form->toprow = form->currow;
835 field->status |= _NEWTOP;
837 if (form->currow >= row_after_bottom)
839 form->toprow = form->currow - field->rows + 1;
840 field->status |= _NEWTOP;
842 if (field->status & _NEWTOP)
844 /* means we have to copy whole range */
845 first_modified_row = form->toprow;
846 first_unmodified_row = first_modified_row + field->rows;
847 field->status &= ~_NEWTOP;
851 /* we try to optimize : finding the range of touched
853 first_modified_row = form->toprow;
854 while (first_modified_row < row_after_bottom)
856 if (is_linetouched(form->w, first_modified_row))
858 first_modified_row++;
860 first_unmodified_row = first_modified_row;
861 while (first_unmodified_row < row_after_bottom)
863 if (!is_linetouched(form->w, first_unmodified_row))
865 first_unmodified_row++;
871 first_modified_row = form->toprow;
872 first_unmodified_row = first_modified_row + field->rows;
874 if (first_unmodified_row != first_modified_row)
879 field->frow + first_modified_row - form->toprow,
881 field->frow + first_unmodified_row - form->toprow - 1,
882 field->cols + field->fcol - 1,
889 /* if the field-window is simply a derived window, i.e. contains no
890 * invisible parts, the whole thing is trivial
896 returnCode(_nc_Position_Form_Cursor(form));
899 /*---------------------------------------------------------------------------
900 | Facility : libnform
901 | Function : static void Perform_Justification(
905 | Description : Output field with requested justification
908 +--------------------------------------------------------------------------*/
910 Perform_Justification(FIELD *field, WINDOW *win)
916 bp = Get_Start_Of_Data(field->buf, Buffer_Length(field));
917 len = (int)(After_End_Of_Data(field->buf, Buffer_Length(field)) - bp);
921 assert(win && (field->drows == 1) && (field->dcols == field->cols));
928 col = (field->cols - len) / 2;
931 col = field->cols - len;
938 myADDNSTR(win, bp, len);
942 /*---------------------------------------------------------------------------
943 | Facility : libnform
944 | Function : static void Undo_Justification(
948 | Description : Display field without any justification, i.e.
952 +--------------------------------------------------------------------------*/
954 Undo_Justification(FIELD *field, WINDOW *win)
959 bp = Get_Start_Of_Data(field->buf, Buffer_Length(field));
960 len = (int)(After_End_Of_Data(field->buf, Buffer_Length(field)) - bp);
966 myADDNSTR(win, bp, len);
970 /*---------------------------------------------------------------------------
971 | Facility : libnform
972 | Function : static bool Check_Char(
975 | TypeArgument *argp)
977 | Description : Perform a single character check for character ch
978 | according to the fieldtype instance.
980 | Return Values : TRUE - Character is valid
981 | FALSE - Character is invalid
982 +--------------------------------------------------------------------------*/
984 Check_Char(FIELDTYPE *typ, int ch, TypeArgument *argp)
988 if (typ->status & _LINKED_TYPE)
992 Check_Char(typ->left, ch, argp->left) ||
993 Check_Char(typ->right, ch, argp->right));
998 return typ->ccheck(ch, (void *)argp);
1001 return (!iscntrl(UChar(ch)) ? TRUE : FALSE);
1004 /*---------------------------------------------------------------------------
1005 | Facility : libnform
1006 | Function : static int Display_Or_Erase_Field(
1010 | Description : Create a subwindow for the field and display the
1011 | buffer contents (apply justification if required)
1012 | or simply erase the field.
1014 | Return Values : E_OK - on success
1015 | E_SYSTEM_ERROR - some error (typical no memory)
1016 +--------------------------------------------------------------------------*/
1018 Display_Or_Erase_Field(FIELD *field, bool bEraseFlag)
1024 return E_SYSTEM_ERROR;
1026 fwin = Get_Form_Window(field->form);
1028 field->rows, field->cols, field->frow, field->fcol);
1031 return E_SYSTEM_ERROR;
1034 if (field->opts & O_VISIBLE)
1035 Set_Field_Window_Attributes(field, win);
1037 wattrset(win, getattrs(fwin));
1043 if (field->opts & O_PUBLIC)
1045 if (Justification_Allowed(field))
1046 Perform_Justification(field, win);
1048 Buffer_To_Window(field, win);
1050 field->status &= ~_NEWTOP;
1057 /* Macros to preset the bEraseFlag */
1058 #define Display_Field(field) Display_Or_Erase_Field(field,FALSE)
1059 #define Erase_Field(field) Display_Or_Erase_Field(field,TRUE)
1061 /*---------------------------------------------------------------------------
1062 | Facility : libnform
1063 | Function : static int Synchronize_Field(FIELD * field)
1065 | Description : Synchronize the windows content with the value in
1068 | Return Values : E_OK - success
1069 | E_BAD_ARGUMENT - invalid field pointer
1070 | E_SYSTEM_ERROR - some severe basic error
1071 +--------------------------------------------------------------------------*/
1073 Synchronize_Field(FIELD *field)
1079 return (E_BAD_ARGUMENT);
1081 if (((form = field->form) != (FORM *)0)
1082 && Field_Really_Appears(field))
1084 if (field == form->current)
1086 form->currow = form->curcol = form->toprow = form->begincol = 0;
1089 if ((field->opts & O_PUBLIC) && Justification_Allowed(field))
1090 Undo_Justification(field, form->w);
1092 Buffer_To_Window(field, form->w);
1094 field->status |= _NEWTOP;
1095 res = _nc_Refresh_Current_Field(form);
1098 res = Display_Field(field);
1100 field->status |= _CHANGED;
1104 /*---------------------------------------------------------------------------
1105 | Facility : libnform
1106 | Function : static int Synchronize_Linked_Fields(FIELD * field)
1108 | Description : Propagate the Synchronize_Field function to all linked
1109 | fields. The first error that occurs in the sequence
1110 | of updates is the return value.
1112 | Return Values : E_OK - success
1113 | E_BAD_ARGUMENT - invalid field pointer
1114 | E_SYSTEM_ERROR - some severe basic error
1115 +--------------------------------------------------------------------------*/
1117 Synchronize_Linked_Fields(FIELD *field)
1119 FIELD *linked_field;
1124 return (E_BAD_ARGUMENT);
1127 return (E_SYSTEM_ERROR);
1129 for (linked_field = field->link;
1130 linked_field != field;
1131 linked_field = linked_field->link)
1133 if (((syncres = Synchronize_Field(linked_field)) != E_OK) &&
1140 /*---------------------------------------------------------------------------
1141 | Facility : libnform
1142 | Function : int _nc_Synchronize_Attributes(FIELD * field)
1144 | Description : If a fields visual attributes have changed, this
1145 | routine is called to propagate those changes to the
1148 | Return Values : E_OK - success
1149 | E_BAD_ARGUMENT - invalid field pointer
1150 | E_SYSTEM_ERROR - some severe basic error
1151 +--------------------------------------------------------------------------*/
1153 _nc_Synchronize_Attributes(FIELD *field)
1159 T((T_CALLED("_nc_Synchronize_Attributes(%p)"), field));
1162 returnCode(E_BAD_ARGUMENT);
1164 CHECKPOS(field->form);
1165 if (((form = field->form) != (FORM *)0)
1166 && Field_Really_Appears(field))
1168 if (form->current == field)
1170 Synchronize_Buffer(form);
1171 Set_Field_Window_Attributes(field, form->w);
1173 wmove(form->w, form->currow, form->curcol);
1175 if (field->opts & O_PUBLIC)
1177 if (Justification_Allowed(field))
1178 Undo_Justification(field, form->w);
1180 Buffer_To_Window(field, form->w);
1184 formwin = Get_Form_Window(form);
1185 copywin(form->w, formwin,
1187 field->frow, field->fcol,
1188 field->rows - 1, field->cols - 1, 0);
1190 Buffer_To_Window(field, form->w);
1191 field->status |= _NEWTOP; /* fake refresh to paint all */
1192 _nc_Refresh_Current_Field(form);
1197 res = Display_Field(field);
1204 /*---------------------------------------------------------------------------
1205 | Facility : libnform
1206 | Function : int _nc_Synchronize_Options(FIELD * field,
1207 | Field_Options newopts)
1209 | Description : If a fields options have changed, this routine is
1210 | called to propagate these changes to the screen and
1211 | to really change the behavior of the field.
1213 | Return Values : E_OK - success
1214 | E_BAD_ARGUMENT - invalid field pointer
1215 | E_SYSTEM_ERROR - some severe basic error
1216 +--------------------------------------------------------------------------*/
1218 _nc_Synchronize_Options(FIELD *field, Field_Options newopts)
1220 Field_Options oldopts;
1221 Field_Options changed_opts;
1225 T((T_CALLED("_nc_Synchronize_Options(%p,%#x)"), field, newopts));
1228 returnCode(E_BAD_ARGUMENT);
1230 oldopts = field->opts;
1231 changed_opts = oldopts ^ newopts;
1232 field->opts = newopts;
1237 if (form->current == field)
1239 field->opts = oldopts;
1240 returnCode(E_CURRENT);
1243 if (form->status & _POSTED)
1245 if ((form->curpage == field->page))
1247 if (changed_opts & O_VISIBLE)
1249 if (newopts & O_VISIBLE)
1250 res = Display_Field(field);
1252 res = Erase_Field(field);
1256 if ((changed_opts & O_PUBLIC) &&
1257 (newopts & O_VISIBLE))
1258 res = Display_Field(field);
1264 if (changed_opts & O_STATIC)
1266 bool single_line_field = Single_Line_Field(field);
1269 if (newopts & O_STATIC)
1271 /* the field becomes now static */
1272 field->status &= ~_MAY_GROW;
1273 /* if actually we have no hidden columns, justification may
1275 if (single_line_field &&
1276 (field->cols == field->dcols) &&
1277 (field->just != NO_JUSTIFICATION) &&
1278 Field_Really_Appears(field))
1280 res2 = Display_Field(field);
1285 /* field is no longer static */
1286 if ((field->maxgrow == 0) ||
1287 (single_line_field && (field->dcols < field->maxgrow)) ||
1288 (!single_line_field && (field->drows < field->maxgrow)))
1290 field->status |= _MAY_GROW;
1291 /* a field with justification now changes its behavior,
1292 so we must redisplay it */
1293 if (single_line_field &&
1294 (field->just != NO_JUSTIFICATION) &&
1295 Field_Really_Appears(field))
1297 res2 = Display_Field(field);
1308 /*---------------------------------------------------------------------------
1309 | Facility : libnform
1310 | Function : int _nc_Set_Current_Field(FORM * form,
1313 | Description : Make the newfield the new current field.
1315 | Return Values : E_OK - success
1316 | E_BAD_ARGUMENT - invalid form or field pointer
1317 | E_SYSTEM_ERROR - some severe basic error
1318 +--------------------------------------------------------------------------*/
1320 _nc_Set_Current_Field(FORM *form, FIELD *newfield)
1325 T((T_CALLED("_nc_Set_Current_Field(%p,%p)"), form, newfield));
1327 if (!form || !newfield || !form->current || (newfield->form != form))
1328 returnCode(E_BAD_ARGUMENT);
1330 if ((form->status & _IN_DRIVER))
1331 returnCode(E_BAD_STATE);
1334 returnCode(E_NOT_CONNECTED);
1336 field = form->current;
1338 if ((field != newfield) ||
1339 !(form->status & _POSTED))
1342 (field->opts & O_VISIBLE) &&
1343 (field->form->curpage == field->page))
1345 _nc_Refresh_Current_Field(form);
1346 if (field->opts & O_PUBLIC)
1348 if (field->drows > field->rows)
1350 if (form->toprow == 0)
1351 field->status &= ~_NEWTOP;
1353 field->status |= _NEWTOP;
1357 if (Justification_Allowed(field))
1359 Window_To_Buffer(form->w, field);
1361 Perform_Justification(field, form->w);
1367 form->w = (WINDOW *)0;
1372 if (Has_Invisible_Parts(field))
1373 new_window = newpad(field->drows, field->dcols);
1375 new_window = derwin(Get_Form_Window(form),
1376 field->rows, field->cols, field->frow, field->fcol);
1379 returnCode(E_SYSTEM_ERROR);
1381 form->current = field;
1385 form->w = new_window;
1387 form->status &= ~_WINDOW_MODIFIED;
1388 Set_Field_Window_Attributes(field, form->w);
1390 if (Has_Invisible_Parts(field))
1393 Buffer_To_Window(field, form->w);
1397 if (Justification_Allowed(field))
1400 Undo_Justification(field, form->w);
1405 untouchwin(form->w);
1408 form->currow = form->curcol = form->toprow = form->begincol = 0;
1412 /*----------------------------------------------------------------------------
1413 Intra-Field Navigation routines
1414 --------------------------------------------------------------------------*/
1416 /*---------------------------------------------------------------------------
1417 | Facility : libnform
1418 | Function : static int IFN_Next_Character(FORM * form)
1420 | Description : Move to the next character in the field. In a multi-line
1421 | field this wraps at the end of the line.
1423 | Return Values : E_OK - success
1424 | E_REQUEST_DENIED - at the rightmost position
1425 +--------------------------------------------------------------------------*/
1427 IFN_Next_Character(FORM *form)
1429 FIELD *field = form->current;
1430 int step = myWCWIDTH(form->w, form->currow, form->curcol);
1432 T((T_CALLED("IFN_Next_Character(%p)"), form));
1433 if ((form->curcol += step) == field->dcols)
1435 if ((++(form->currow)) == field->drows)
1437 #if GROW_IF_NAVIGATE
1438 if (!Single_Line_Field(field) && Field_Grown(field, 1))
1445 #if GROW_IF_NAVIGATE
1446 if (Single_Line_Field(field) && Field_Grown(field, 1))
1449 form->curcol -= step;
1450 returnCode(E_REQUEST_DENIED);
1457 /*---------------------------------------------------------------------------
1458 | Facility : libnform
1459 | Function : static int IFN_Previous_Character(FORM * form)
1461 | Description : Move to the previous character in the field. In a
1462 | multi-line field this wraps and the beginning of the
1465 | Return Values : E_OK - success
1466 | E_REQUEST_DENIED - at the leftmost position
1467 +--------------------------------------------------------------------------*/
1469 IFN_Previous_Character(FORM *form)
1471 int amount = myWCWIDTH(form->w, form->currow, form->curcol - 1);
1472 int oldcol = form->curcol;
1474 T((T_CALLED("IFN_Previous_Character(%p)"), form));
1475 if ((form->curcol -= amount) < 0)
1477 if ((--(form->currow)) < 0)
1480 form->curcol = oldcol;
1481 returnCode(E_REQUEST_DENIED);
1483 form->curcol = form->current->dcols - 1;
1488 /*---------------------------------------------------------------------------
1489 | Facility : libnform
1490 | Function : static int IFN_Next_Line(FORM * form)
1492 | Description : Move to the beginning of the next line in the field
1494 | Return Values : E_OK - success
1495 | E_REQUEST_DENIED - at the last line
1496 +--------------------------------------------------------------------------*/
1498 IFN_Next_Line(FORM *form)
1500 FIELD *field = form->current;
1502 T((T_CALLED("IFN_Next_Line(%p)"), form));
1503 if ((++(form->currow)) == field->drows)
1505 #if GROW_IF_NAVIGATE
1506 if (!Single_Line_Field(field) && Field_Grown(field, 1))
1510 returnCode(E_REQUEST_DENIED);
1516 /*---------------------------------------------------------------------------
1517 | Facility : libnform
1518 | Function : static int IFN_Previous_Line(FORM * form)
1520 | Description : Move to the beginning of the previous line in the field
1522 | Return Values : E_OK - success
1523 | E_REQUEST_DENIED - at the first line
1524 +--------------------------------------------------------------------------*/
1526 IFN_Previous_Line(FORM *form)
1528 T((T_CALLED("IFN_Previous_Line(%p)"), form));
1529 if ((--(form->currow)) < 0)
1532 returnCode(E_REQUEST_DENIED);
1538 /*---------------------------------------------------------------------------
1539 | Facility : libnform
1540 | Function : static int IFN_Next_Word(FORM * form)
1542 | Description : Move to the beginning of the next word in the field.
1544 | Return Values : E_OK - success
1545 | E_REQUEST_DENIED - there is no next word
1546 +--------------------------------------------------------------------------*/
1548 IFN_Next_Word(FORM *form)
1550 FIELD *field = form->current;
1551 FIELD_CELL *bp = Address_Of_Current_Position_In_Buffer(form);
1555 T((T_CALLED("IFN_Next_Word(%p)"), form));
1557 /* We really need access to the data, so we have to synchronize */
1558 Synchronize_Buffer(form);
1560 /* Go to the first whitespace after the current position (including
1561 current position). This is then the starting point to look for the
1562 next non-blank data */
1563 s = Get_First_Whitespace_Character(bp, Buffer_Length(field) -
1564 (int)(bp - field->buf));
1566 /* Find the start of the next word */
1567 t = Get_Start_Of_Data(s, Buffer_Length(field) -
1568 (int)(s - field->buf));
1569 #if !FRIENDLY_PREV_NEXT_WORD
1571 returnCode(E_REQUEST_DENIED);
1575 Adjust_Cursor_Position(form, t);
1580 /*---------------------------------------------------------------------------
1581 | Facility : libnform
1582 | Function : static int IFN_Previous_Word(FORM * form)
1584 | Description : Move to the beginning of the previous word in the field.
1586 | Return Values : E_OK - success
1587 | E_REQUEST_DENIED - there is no previous word
1588 +--------------------------------------------------------------------------*/
1590 IFN_Previous_Word(FORM *form)
1592 FIELD *field = form->current;
1593 FIELD_CELL *bp = Address_Of_Current_Position_In_Buffer(form);
1598 T((T_CALLED("IFN_Previous_Word(%p)"), form));
1600 /* We really need access to the data, so we have to synchronize */
1601 Synchronize_Buffer(form);
1603 s = After_End_Of_Data(field->buf, (int)(bp - field->buf));
1604 /* s points now right after the last non-blank in the buffer before bp.
1605 If bp was in a word, s equals bp. In this case we must find the last
1606 whitespace in the buffer before bp and repeat the game to really find
1607 the previous word! */
1611 /* And next call now goes backward to look for the last whitespace
1612 before that, pointing right after this, so it points to the begin
1613 of the previous word.
1615 t = After_Last_Whitespace_Character(field->buf, (int)(s - field->buf));
1616 #if !FRIENDLY_PREV_NEXT_WORD
1618 returnCode(E_REQUEST_DENIED);
1622 /* and do it again, replacing bp by t */
1623 s = After_End_Of_Data(field->buf, (int)(t - field->buf));
1624 t = After_Last_Whitespace_Character(field->buf, (int)(s - field->buf));
1625 #if !FRIENDLY_PREV_NEXT_WORD
1627 returnCode(E_REQUEST_DENIED);
1630 Adjust_Cursor_Position(form, t);
1634 /*---------------------------------------------------------------------------
1635 | Facility : libnform
1636 | Function : static int IFN_Beginning_Of_Field(FORM * form)
1638 | Description : Place the cursor at the first non-pad character in
1641 | Return Values : E_OK - success
1642 +--------------------------------------------------------------------------*/
1644 IFN_Beginning_Of_Field(FORM *form)
1646 FIELD *field = form->current;
1648 T((T_CALLED("IFN_Beginning_Of_Field(%p)"), form));
1649 Synchronize_Buffer(form);
1650 Adjust_Cursor_Position(form,
1651 Get_Start_Of_Data(field->buf, Buffer_Length(field)));
1655 /*---------------------------------------------------------------------------
1656 | Facility : libnform
1657 | Function : static int IFN_End_Of_Field(FORM * form)
1659 | Description : Place the cursor after the last non-pad character in
1660 | the field. If the field occupies the last position in
1661 | the buffer, the cursor is positioned on the last
1664 | Return Values : E_OK - success
1665 +--------------------------------------------------------------------------*/
1667 IFN_End_Of_Field(FORM *form)
1669 FIELD *field = form->current;
1672 T((T_CALLED("IFN_End_Of_Field(%p)"), form));
1673 Synchronize_Buffer(form);
1674 pos = After_End_Of_Data(field->buf, Buffer_Length(field));
1675 if (pos == (field->buf + Buffer_Length(field)))
1677 Adjust_Cursor_Position(form, pos);
1681 /*---------------------------------------------------------------------------
1682 | Facility : libnform
1683 | Function : static int IFN_Beginning_Of_Line(FORM * form)
1685 | Description : Place the cursor on the first non-pad character in
1686 | the current line of the field.
1688 | Return Values : E_OK - success
1689 +--------------------------------------------------------------------------*/
1691 IFN_Beginning_Of_Line(FORM *form)
1693 FIELD *field = form->current;
1695 T((T_CALLED("IFN_Beginning_Of_Line(%p)"), form));
1696 Synchronize_Buffer(form);
1697 Adjust_Cursor_Position(form,
1698 Get_Start_Of_Data(Address_Of_Current_Row_In_Buffer(form),
1703 /*---------------------------------------------------------------------------
1704 | Facility : libnform
1705 | Function : static int IFN_End_Of_Line(FORM * form)
1707 | Description : Place the cursor after the last non-pad character in the
1708 | current line of the field. If the field occupies the
1709 | last column in the line, the cursor is positioned on the
1710 | last character of the line.
1712 | Return Values : E_OK - success
1713 +--------------------------------------------------------------------------*/
1715 IFN_End_Of_Line(FORM *form)
1717 FIELD *field = form->current;
1721 T((T_CALLED("IFN_End_Of_Line(%p)"), form));
1722 Synchronize_Buffer(form);
1723 bp = Address_Of_Current_Row_In_Buffer(form);
1724 pos = After_End_Of_Data(bp, field->dcols);
1725 if (pos == (bp + field->dcols))
1727 Adjust_Cursor_Position(form, pos);
1731 /*---------------------------------------------------------------------------
1732 | Facility : libnform
1733 | Function : static int IFN_Left_Character(FORM * form)
1735 | Description : Move one character to the left in the current line.
1736 | This doesn't cycle.
1738 | Return Values : E_OK - success
1739 | E_REQUEST_DENIED - already in first column
1740 +--------------------------------------------------------------------------*/
1742 IFN_Left_Character(FORM *form)
1744 int amount = myWCWIDTH(form->w, form->currow, form->curcol - 1);
1745 int oldcol = form->curcol;
1747 T((T_CALLED("IFN_Left_Character(%p)"), form));
1748 if ((form->curcol -= amount) < 0)
1750 form->curcol = oldcol;
1751 returnCode(E_REQUEST_DENIED);
1756 /*---------------------------------------------------------------------------
1757 | Facility : libnform
1758 | Function : static int IFN_Right_Character(FORM * form)
1760 | Description : Move one character to the right in the current line.
1761 | This doesn't cycle.
1763 | Return Values : E_OK - success
1764 | E_REQUEST_DENIED - already in last column
1765 +--------------------------------------------------------------------------*/
1767 IFN_Right_Character(FORM *form)
1769 int amount = myWCWIDTH(form->w, form->currow, form->curcol);
1770 int oldcol = form->curcol;
1772 T((T_CALLED("IFN_Right_Character(%p)"), form));
1773 if ((form->curcol += amount) >= form->current->dcols)
1775 #if GROW_IF_NAVIGATE
1776 FIELD *field = form->current;
1778 if (Single_Line_Field(field) && Field_Grown(field, 1))
1781 form->curcol = oldcol;
1782 returnCode(E_REQUEST_DENIED);
1787 /*---------------------------------------------------------------------------
1788 | Facility : libnform
1789 | Function : static int IFN_Up_Character(FORM * form)
1791 | Description : Move one line up. This doesn't cycle through the lines
1794 | Return Values : E_OK - success
1795 | E_REQUEST_DENIED - already in last column
1796 +--------------------------------------------------------------------------*/
1798 IFN_Up_Character(FORM *form)
1800 T((T_CALLED("IFN_Up_Character(%p)"), form));
1801 if ((--(form->currow)) < 0)
1804 returnCode(E_REQUEST_DENIED);
1809 /*---------------------------------------------------------------------------
1810 | Facility : libnform
1811 | Function : static int IFN_Down_Character(FORM * form)
1813 | Description : Move one line down. This doesn't cycle through the
1814 | lines of the field.
1816 | Return Values : E_OK - success
1817 | E_REQUEST_DENIED - already in last column
1818 +--------------------------------------------------------------------------*/
1820 IFN_Down_Character(FORM *form)
1822 FIELD *field = form->current;
1824 T((T_CALLED("IFN_Down_Character(%p)"), form));
1825 if ((++(form->currow)) == field->drows)
1827 #if GROW_IF_NAVIGATE
1828 if (!Single_Line_Field(field) && Field_Grown(field, 1))
1832 returnCode(E_REQUEST_DENIED);
1836 /*----------------------------------------------------------------------------
1837 END of Intra-Field Navigation routines
1838 --------------------------------------------------------------------------*/
1840 /*----------------------------------------------------------------------------
1841 Vertical scrolling helper routines
1842 --------------------------------------------------------------------------*/
1844 /*---------------------------------------------------------------------------
1845 | Facility : libnform
1846 | Function : static int VSC_Generic(FORM *form, int nlines)
1848 | Description : Scroll multi-line field forward (nlines>0) or
1849 | backward (nlines<0) this many lines.
1851 | Return Values : E_OK - success
1852 | E_REQUEST_DENIED - can't scroll
1853 +--------------------------------------------------------------------------*/
1855 VSC_Generic(FORM *form, int nlines)
1857 FIELD *field = form->current;
1858 int res = E_REQUEST_DENIED;
1859 int rows_to_go = (nlines > 0 ? nlines : -nlines);
1863 if ((rows_to_go + form->toprow) > (field->drows - field->rows))
1864 rows_to_go = (field->drows - field->rows - form->toprow);
1868 form->currow += rows_to_go;
1869 form->toprow += rows_to_go;
1875 if (rows_to_go > form->toprow)
1876 rows_to_go = form->toprow;
1880 form->currow -= rows_to_go;
1881 form->toprow -= rows_to_go;
1887 /*----------------------------------------------------------------------------
1888 End of Vertical scrolling helper routines
1889 --------------------------------------------------------------------------*/
1891 /*----------------------------------------------------------------------------
1892 Vertical scrolling routines
1893 --------------------------------------------------------------------------*/
1895 /*---------------------------------------------------------------------------
1896 | Facility : libnform
1897 | Function : static int Vertical_Scrolling(
1898 | int (* const fct) (FORM *),
1901 | Description : Performs the generic vertical scrolling routines.
1902 | This has to check for a multi-line field and to set
1903 | the _NEWTOP flag if scrolling really occurred.
1905 | Return Values : Propagated error code from low-level driver calls
1906 +--------------------------------------------------------------------------*/
1908 Vertical_Scrolling(int (*const fct) (FORM *), FORM *form)
1910 int res = E_REQUEST_DENIED;
1912 if (!Single_Line_Field(form->current))
1916 form->current->status |= _NEWTOP;
1921 /*---------------------------------------------------------------------------
1922 | Facility : libnform
1923 | Function : static int VSC_Scroll_Line_Forward(FORM * form)
1925 | Description : Scroll multi-line field forward a line
1927 | Return Values : E_OK - success
1928 | E_REQUEST_DENIED - no data ahead
1929 +--------------------------------------------------------------------------*/
1931 VSC_Scroll_Line_Forward(FORM *form)
1933 T((T_CALLED("VSC_Scroll_Line_Forward(%p)"), form));
1934 returnCode(VSC_Generic(form, 1));
1937 /*---------------------------------------------------------------------------
1938 | Facility : libnform
1939 | Function : static int VSC_Scroll_Line_Backward(FORM * form)
1941 | Description : Scroll multi-line field backward a line
1943 | Return Values : E_OK - success
1944 | E_REQUEST_DENIED - no data behind
1945 +--------------------------------------------------------------------------*/
1947 VSC_Scroll_Line_Backward(FORM *form)
1949 T((T_CALLED("VSC_Scroll_Line_Backward(%p)"), form));
1950 returnCode(VSC_Generic(form, -1));
1953 /*---------------------------------------------------------------------------
1954 | Facility : libnform
1955 | Function : static int VSC_Scroll_Page_Forward(FORM * form)
1957 | Description : Scroll a multi-line field forward a page
1959 | Return Values : E_OK - success
1960 | E_REQUEST_DENIED - no data ahead
1961 +--------------------------------------------------------------------------*/
1963 VSC_Scroll_Page_Forward(FORM *form)
1965 T((T_CALLED("VSC_Scroll_Page_Forward(%p)"), form));
1966 returnCode(VSC_Generic(form, form->current->rows));
1969 /*---------------------------------------------------------------------------
1970 | Facility : libnform
1971 | Function : static int VSC_Scroll_Half_Page_Forward(FORM * form)
1973 | Description : Scroll a multi-line field forward half a page
1975 | Return Values : E_OK - success
1976 | E_REQUEST_DENIED - no data ahead
1977 +--------------------------------------------------------------------------*/
1979 VSC_Scroll_Half_Page_Forward(FORM *form)
1981 T((T_CALLED("VSC_Scroll_Half_Page_Forward(%p)"), form));
1982 returnCode(VSC_Generic(form, (form->current->rows + 1) / 2));
1985 /*---------------------------------------------------------------------------
1986 | Facility : libnform
1987 | Function : static int VSC_Scroll_Page_Backward(FORM * form)
1989 | Description : Scroll a multi-line field backward a page
1991 | Return Values : E_OK - success
1992 | E_REQUEST_DENIED - no data behind
1993 +--------------------------------------------------------------------------*/
1995 VSC_Scroll_Page_Backward(FORM *form)
1997 T((T_CALLED("VSC_Scroll_Page_Backward(%p)"), form));
1998 returnCode(VSC_Generic(form, -(form->current->rows)));
2001 /*---------------------------------------------------------------------------
2002 | Facility : libnform
2003 | Function : static int VSC_Scroll_Half_Page_Backward(FORM * form)
2005 | Description : Scroll a multi-line field backward half a page
2007 | Return Values : E_OK - success
2008 | E_REQUEST_DENIED - no data behind
2009 +--------------------------------------------------------------------------*/
2011 VSC_Scroll_Half_Page_Backward(FORM *form)
2013 T((T_CALLED("VSC_Scroll_Half_Page_Backward(%p)"), form));
2014 returnCode(VSC_Generic(form, -((form->current->rows + 1) / 2)));
2016 /*----------------------------------------------------------------------------
2017 End of Vertical scrolling routines
2018 --------------------------------------------------------------------------*/
2020 /*----------------------------------------------------------------------------
2021 Horizontal scrolling helper routines
2022 --------------------------------------------------------------------------*/
2024 /*---------------------------------------------------------------------------
2025 | Facility : libnform
2026 | Function : static int HSC_Generic(FORM *form, int ncolumns)
2028 | Description : Scroll single-line field forward (ncolumns>0) or
2029 | backward (ncolumns<0) this many columns.
2031 | Return Values : E_OK - success
2032 | E_REQUEST_DENIED - can't scroll
2033 +--------------------------------------------------------------------------*/
2035 HSC_Generic(FORM *form, int ncolumns)
2037 FIELD *field = form->current;
2038 int res = E_REQUEST_DENIED;
2039 int cols_to_go = (ncolumns > 0 ? ncolumns : -ncolumns);
2043 if ((cols_to_go + form->begincol) > (field->dcols - field->cols))
2044 cols_to_go = field->dcols - field->cols - form->begincol;
2048 form->curcol += cols_to_go;
2049 form->begincol += cols_to_go;
2055 if (cols_to_go > form->begincol)
2056 cols_to_go = form->begincol;
2060 form->curcol -= cols_to_go;
2061 form->begincol -= cols_to_go;
2067 /*----------------------------------------------------------------------------
2068 End of Horizontal scrolling helper routines
2069 --------------------------------------------------------------------------*/
2071 /*----------------------------------------------------------------------------
2072 Horizontal scrolling routines
2073 --------------------------------------------------------------------------*/
2075 /*---------------------------------------------------------------------------
2076 | Facility : libnform
2077 | Function : static int Horizontal_Scrolling(
2078 | int (* const fct) (FORM *),
2081 | Description : Performs the generic horizontal scrolling routines.
2082 | This has to check for a single-line field.
2084 | Return Values : Propagated error code from low-level driver calls
2085 +--------------------------------------------------------------------------*/
2087 Horizontal_Scrolling(int (*const fct) (FORM *), FORM *form)
2089 if (Single_Line_Field(form->current))
2092 return (E_REQUEST_DENIED);
2095 /*---------------------------------------------------------------------------
2096 | Facility : libnform
2097 | Function : static int HSC_Scroll_Char_Forward(FORM * form)
2099 | Description : Scroll single-line field forward a character
2101 | Return Values : E_OK - success
2102 | E_REQUEST_DENIED - no data ahead
2103 +--------------------------------------------------------------------------*/
2105 HSC_Scroll_Char_Forward(FORM *form)
2107 T((T_CALLED("HSC_Scroll_Char_Forward(%p)"), form));
2108 returnCode(HSC_Generic(form, 1));
2111 /*---------------------------------------------------------------------------
2112 | Facility : libnform
2113 | Function : static int HSC_Scroll_Char_Backward(FORM * form)
2115 | Description : Scroll single-line field backward a character
2117 | Return Values : E_OK - success
2118 | E_REQUEST_DENIED - no data behind
2119 +--------------------------------------------------------------------------*/
2121 HSC_Scroll_Char_Backward(FORM *form)
2123 T((T_CALLED("HSC_Scroll_Char_Backward(%p)"), form));
2124 returnCode(HSC_Generic(form, -1));
2127 /*---------------------------------------------------------------------------
2128 | Facility : libnform
2129 | Function : static int HSC_Horizontal_Line_Forward(FORM* form)
2131 | Description : Scroll single-line field forward a line
2133 | Return Values : E_OK - success
2134 | E_REQUEST_DENIED - no data ahead
2135 +--------------------------------------------------------------------------*/
2137 HSC_Horizontal_Line_Forward(FORM *form)
2139 T((T_CALLED("HSC_Horizontal_Line_Forward(%p)"), form));
2140 returnCode(HSC_Generic(form, form->current->cols));
2143 /*---------------------------------------------------------------------------
2144 | Facility : libnform
2145 | Function : static int HSC_Horizontal_Half_Line_Forward(FORM* form)
2147 | Description : Scroll single-line field forward half a line
2149 | Return Values : E_OK - success
2150 | E_REQUEST_DENIED - no data ahead
2151 +--------------------------------------------------------------------------*/
2153 HSC_Horizontal_Half_Line_Forward(FORM *form)
2155 T((T_CALLED("HSC_Horizontal_Half_Line_Forward(%p)"), form));
2156 returnCode(HSC_Generic(form, (form->current->cols + 1) / 2));
2159 /*---------------------------------------------------------------------------
2160 | Facility : libnform
2161 | Function : static int HSC_Horizontal_Line_Backward(FORM* form)
2163 | Description : Scroll single-line field backward a line
2165 | Return Values : E_OK - success
2166 | E_REQUEST_DENIED - no data behind
2167 +--------------------------------------------------------------------------*/
2169 HSC_Horizontal_Line_Backward(FORM *form)
2171 T((T_CALLED("HSC_Horizontal_Line_Backward(%p)"), form));
2172 returnCode(HSC_Generic(form, -(form->current->cols)));
2175 /*---------------------------------------------------------------------------
2176 | Facility : libnform
2177 | Function : static int HSC_Horizontal_Half_Line_Backward(FORM* form)
2179 | Description : Scroll single-line field backward half a line
2181 | Return Values : E_OK - success
2182 | E_REQUEST_DENIED - no data behind
2183 +--------------------------------------------------------------------------*/
2185 HSC_Horizontal_Half_Line_Backward(FORM *form)
2187 T((T_CALLED("HSC_Horizontal_Half_Line_Backward(%p)"), form));
2188 returnCode(HSC_Generic(form, -((form->current->cols + 1) / 2)));
2191 /*----------------------------------------------------------------------------
2192 End of Horizontal scrolling routines
2193 --------------------------------------------------------------------------*/
2195 /*----------------------------------------------------------------------------
2196 Helper routines for Field Editing
2197 --------------------------------------------------------------------------*/
2199 /*---------------------------------------------------------------------------
2200 | Facility : libnform
2201 | Function : static bool Is_There_Room_For_A_Line(FORM * form)
2203 | Description : Check whether or not there is enough room in the
2204 | buffer to enter a whole line.
2206 | Return Values : TRUE - there is enough space
2207 | FALSE - there is not enough space
2208 +--------------------------------------------------------------------------*/
2210 Is_There_Room_For_A_Line(FORM *form)
2212 FIELD *field = form->current;
2213 FIELD_CELL *begin_of_last_line, *s;
2215 Synchronize_Buffer(form);
2216 begin_of_last_line = Address_Of_Row_In_Buffer(field, (field->drows - 1));
2217 s = After_End_Of_Data(begin_of_last_line, field->dcols);
2218 return ((s == begin_of_last_line) ? TRUE : FALSE);
2221 /*---------------------------------------------------------------------------
2222 | Facility : libnform
2223 | Function : static bool Is_There_Room_For_A_Char_In_Line(FORM * form)
2225 | Description : Checks whether or not there is room for a new character
2226 | in the current line.
2228 | Return Values : TRUE - there is room
2229 | FALSE - there is not enough room (line full)
2230 +--------------------------------------------------------------------------*/
2232 Is_There_Room_For_A_Char_In_Line(FORM *form)
2234 int last_char_in_line;
2236 wmove(form->w, form->currow, form->current->dcols - 1);
2237 last_char_in_line = (int)(winch(form->w) & A_CHARTEXT);
2238 wmove(form->w, form->currow, form->curcol);
2239 return (((last_char_in_line == form->current->pad) ||
2240 is_blank(last_char_in_line)) ? TRUE : FALSE);
2243 #define There_Is_No_Room_For_A_Char_In_Line(f) \
2244 !Is_There_Room_For_A_Char_In_Line(f)
2246 /*---------------------------------------------------------------------------
2247 | Facility : libnform
2248 | Function : static int Insert_String(
2254 | Description : Insert the 'len' characters beginning at pointer 'txt'
2255 | into the 'row' of the 'form'. The insertion occurs
2256 | on the beginning of the row, all other characters are
2257 | moved to the right. After the text a pad character will
2258 | be inserted to separate the text from the rest. If
2259 | necessary the insertion moves characters on the next
2260 | line to make place for the requested insertion string.
2262 | Return Values : E_OK - success
2263 | E_REQUEST_DENIED -
2264 | E_SYSTEM_ERROR - system error
2265 +--------------------------------------------------------------------------*/
2267 Insert_String(FORM *form, int row, FIELD_CELL *txt, int len)
2269 FIELD *field = form->current;
2270 FIELD_CELL *bp = Address_Of_Row_In_Buffer(field, row);
2271 int datalen = (int)(After_End_Of_Data(bp, field->dcols) - bp);
2272 int freelen = field->dcols - datalen;
2273 int requiredlen = len + 1;
2275 int result = E_REQUEST_DENIED;
2277 if (freelen >= requiredlen)
2279 wmove(form->w, row, 0);
2280 myINSNSTR(form->w, txt, len);
2281 wmove(form->w, row, len);
2282 myINSNSTR(form->w, &myBLANK, 1);
2287 /* we have to move characters on the next line. If we are on the
2288 last line this may work, if the field is growable */
2289 if ((row == (field->drows - 1)) && Growable(field))
2291 if (!Field_Grown(field, 1))
2292 return (E_SYSTEM_ERROR);
2293 /* !!!Side-Effect : might be changed due to growth!!! */
2294 bp = Address_Of_Row_In_Buffer(field, row);
2297 if (row < (field->drows - 1))
2300 After_Last_Whitespace_Character(bp,
2301 (int)(Get_Start_Of_Data(bp
2306 /* split points now to the first character of the portion of the
2307 line that must be moved to the next line */
2308 datalen = (int)(split - bp); /* + freelen has to stay on this line */
2309 freelen = field->dcols - (datalen + freelen); /* for the next line */
2311 if ((result = Insert_String(form, row + 1, split, freelen)) == E_OK)
2313 wmove(form->w, row, datalen);
2315 wmove(form->w, row, 0);
2316 myINSNSTR(form->w, txt, len);
2317 wmove(form->w, row, len);
2318 myINSNSTR(form->w, &myBLANK, 1);
2326 /*---------------------------------------------------------------------------
2327 | Facility : libnform
2328 | Function : static int Wrapping_Not_Necessary_Or_Wrapping_Ok(
2331 | Description : If a character has been entered into a field, it may
2332 | be that wrapping has to occur. This routine checks
2333 | whether or not wrapping is required and if so, performs
2336 | Return Values : E_OK - no wrapping required or wrapping
2338 | E_REQUEST_DENIED -
2339 | E_SYSTEM_ERROR - some system error
2340 +--------------------------------------------------------------------------*/
2342 Wrapping_Not_Necessary_Or_Wrapping_Ok(FORM *form)
2344 FIELD *field = form->current;
2345 int result = E_REQUEST_DENIED;
2346 bool Last_Row = ((field->drows - 1) == form->currow);
2348 if ((field->opts & O_WRAP) && /* wrapping wanted */
2349 (!Single_Line_Field(field)) && /* must be multi-line */
2350 (There_Is_No_Room_For_A_Char_In_Line(form)) && /* line is full */
2351 (!Last_Row || Growable(field))) /* there are more lines */
2355 int chars_to_be_wrapped;
2356 int chars_to_remain_on_line;
2360 /* the above logic already ensures, that in this case the field
2362 if (!Field_Grown(field, 1))
2363 return E_SYSTEM_ERROR;
2365 bp = Address_Of_Current_Row_In_Buffer(form);
2366 Window_To_Buffer(form->w, field);
2367 split = After_Last_Whitespace_Character(bp, field->dcols);
2368 /* split points to the first character of the sequence to be brought
2370 chars_to_remain_on_line = (int)(split - bp);
2371 chars_to_be_wrapped = field->dcols - chars_to_remain_on_line;
2372 if (chars_to_remain_on_line > 0)
2374 if ((result = Insert_String(form, form->currow + 1, split,
2375 chars_to_be_wrapped)) == E_OK)
2377 wmove(form->w, form->currow, chars_to_remain_on_line);
2379 if (form->curcol >= chars_to_remain_on_line)
2382 form->curcol -= chars_to_remain_on_line;
2392 Window_To_Buffer(form->w, field);
2393 result = E_REQUEST_DENIED;
2397 result = E_OK; /* wrapping was not necessary */
2401 /*----------------------------------------------------------------------------
2402 Field Editing routines
2403 --------------------------------------------------------------------------*/
2405 /*---------------------------------------------------------------------------
2406 | Facility : libnform
2407 | Function : static int Field_Editing(
2408 | int (* const fct) (FORM *),
2411 | Description : Generic routine for field editing requests. The driver
2412 | routines are only called for editable fields, the
2413 | _WINDOW_MODIFIED flag is set if editing occurred.
2414 | This is somewhat special due to the overload semantics
2415 | of the NEW_LINE and DEL_PREV requests.
2417 | Return Values : Error code from low level drivers.
2418 +--------------------------------------------------------------------------*/
2420 Field_Editing(int (*const fct) (FORM *), FORM *form)
2422 int res = E_REQUEST_DENIED;
2424 /* We have to deal here with the specific case of the overloaded
2425 behavior of New_Line and Delete_Previous requests.
2426 They may end up in navigational requests if we are on the first
2427 character in a field. But navigation is also allowed on non-
2430 if ((fct == FE_Delete_Previous) &&
2431 (form->opts & O_BS_OVERLOAD) &&
2432 First_Position_In_Current_Field(form))
2434 res = Inter_Field_Navigation(FN_Previous_Field, form);
2438 if (fct == FE_New_Line)
2440 if ((form->opts & O_NL_OVERLOAD) &&
2441 First_Position_In_Current_Field(form))
2443 res = Inter_Field_Navigation(FN_Next_Field, form);
2446 /* FE_New_Line deals itself with the _WINDOW_MODIFIED flag */
2451 /* From now on, everything must be editable */
2452 if (form->current->opts & O_EDIT)
2456 form->status |= _WINDOW_MODIFIED;
2463 /*---------------------------------------------------------------------------
2464 | Facility : libnform
2465 | Function : static int FE_New_Line(FORM * form)
2467 | Description : Perform a new line request. This is rather complex
2468 | compared to other routines in this code due to the
2469 | rather difficult to understand description in the
2472 | Return Values : E_OK - success
2473 | E_REQUEST_DENIED - new line not allowed
2474 | E_SYSTEM_ERROR - system error
2475 +--------------------------------------------------------------------------*/
2477 FE_New_Line(FORM *form)
2479 FIELD *field = form->current;
2481 bool Last_Row = ((field->drows - 1) == form->currow);
2483 T((T_CALLED("FE_New_Line(%p)"), form));
2484 if (form->status & _OVLMODE)
2487 (!(Growable(field) && !Single_Line_Field(field))))
2489 if (!(form->opts & O_NL_OVERLOAD))
2490 returnCode(E_REQUEST_DENIED);
2491 wmove(form->w, form->currow, form->curcol);
2493 /* we have to set this here, although it is also
2494 handled in the generic routine. The reason is,
2495 that FN_Next_Field may fail, but the form is
2496 definitively changed */
2497 form->status |= _WINDOW_MODIFIED;
2498 returnCode(Inter_Field_Navigation(FN_Next_Field, form));
2502 if (Last_Row && !Field_Grown(field, 1))
2504 /* N.B.: due to the logic in the 'if', LastRow==TRUE
2505 means here that the field is growable and not
2506 a single-line field */
2507 returnCode(E_SYSTEM_ERROR);
2509 wmove(form->w, form->currow, form->curcol);
2513 form->status |= _WINDOW_MODIFIED;
2521 !(Growable(field) && !Single_Line_Field(field)))
2523 if (!(form->opts & O_NL_OVERLOAD))
2524 returnCode(E_REQUEST_DENIED);
2525 returnCode(Inter_Field_Navigation(FN_Next_Field, form));
2529 bool May_Do_It = !Last_Row && Is_There_Room_For_A_Line(form);
2531 if (!(May_Do_It || Growable(field)))
2532 returnCode(E_REQUEST_DENIED);
2533 if (!May_Do_It && !Field_Grown(field, 1))
2534 returnCode(E_SYSTEM_ERROR);
2536 bp = Address_Of_Current_Position_In_Buffer(form);
2537 t = After_End_Of_Data(bp, field->dcols - form->curcol);
2538 wmove(form->w, form->currow, form->curcol);
2542 wmove(form->w, form->currow, form->curcol);
2544 myADDNSTR(form->w, bp, (int)(t - bp));
2545 form->status |= _WINDOW_MODIFIED;
2551 /*---------------------------------------------------------------------------
2552 | Facility : libnform
2553 | Function : static int FE_Insert_Character(FORM * form)
2555 | Description : Insert blank character at the cursor position
2557 | Return Values : E_OK
2559 +--------------------------------------------------------------------------*/
2561 FE_Insert_Character(FORM *form)
2563 FIELD *field = form->current;
2564 int result = E_REQUEST_DENIED;
2566 T((T_CALLED("FE_Insert_Character(%p)"), form));
2567 if (Check_Char(field->type, (int)C_BLANK, (TypeArgument *)(field->arg)))
2569 bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);
2571 if (There_Is_Room ||
2572 ((Single_Line_Field(field) && Growable(field))))
2574 if (!There_Is_Room && !Field_Grown(field, 1))
2575 result = E_SYSTEM_ERROR;
2578 winsch(form->w, (chtype)C_BLANK);
2579 result = Wrapping_Not_Necessary_Or_Wrapping_Ok(form);
2586 /*---------------------------------------------------------------------------
2587 | Facility : libnform
2588 | Function : static int FE_Insert_Line(FORM * form)
2590 | Description : Insert a blank line at the cursor position
2592 | Return Values : E_OK - success
2593 | E_REQUEST_DENIED - line can not be inserted
2594 +--------------------------------------------------------------------------*/
2596 FE_Insert_Line(FORM *form)
2598 FIELD *field = form->current;
2599 int result = E_REQUEST_DENIED;
2601 T((T_CALLED("FE_Insert_Line(%p)"), form));
2602 if (Check_Char(field->type, (int)C_BLANK, (TypeArgument *)(field->arg)))
2604 bool Maybe_Done = (form->currow != (field->drows - 1)) &&
2605 Is_There_Room_For_A_Line(form);
2607 if (!Single_Line_Field(field) &&
2608 (Maybe_Done || Growable(field)))
2610 if (!Maybe_Done && !Field_Grown(field, 1))
2611 result = E_SYSTEM_ERROR;
2623 /*---------------------------------------------------------------------------
2624 | Facility : libnform
2625 | Function : static int FE_Delete_Character(FORM * form)
2627 | Description : Delete character at the cursor position
2629 | Return Values : E_OK - success
2630 +--------------------------------------------------------------------------*/
2632 FE_Delete_Character(FORM *form)
2634 T((T_CALLED("FE_Delete_Character(%p)"), form));
2639 /*---------------------------------------------------------------------------
2640 | Facility : libnform
2641 | Function : static int FE_Delete_Previous(FORM * form)
2643 | Description : Delete character before cursor. Again this is a rather
2644 | difficult piece compared to others due to the overloading
2645 | semantics of backspace.
2646 | N.B.: The case of overloaded BS on first field position
2647 | is already handled in the generic routine.
2649 | Return Values : E_OK - success
2650 | E_REQUEST_DENIED - Character can't be deleted
2651 +--------------------------------------------------------------------------*/
2653 FE_Delete_Previous(FORM *form)
2655 FIELD *field = form->current;
2657 T((T_CALLED("FE_Delete_Previous(%p)"), form));
2658 if (First_Position_In_Current_Field(form))
2659 returnCode(E_REQUEST_DENIED);
2661 if ((--(form->curcol)) < 0)
2663 FIELD_CELL *this_line, *prev_line, *prev_end, *this_end;
2664 int this_row = form->currow;
2667 if (form->status & _OVLMODE)
2668 returnCode(E_REQUEST_DENIED);
2670 prev_line = Address_Of_Row_In_Buffer(field, (form->currow - 1));
2671 this_line = Address_Of_Row_In_Buffer(field, (form->currow));
2672 Synchronize_Buffer(form);
2673 prev_end = After_End_Of_Data(prev_line, field->dcols);
2674 this_end = After_End_Of_Data(this_line, field->dcols);
2675 if ((int)(this_end - this_line) >
2676 (field->cols - (int)(prev_end - prev_line)))
2677 returnCode(E_REQUEST_DENIED);
2678 wmove(form->w, form->currow, form->curcol);
2680 Adjust_Cursor_Position(form, prev_end);
2682 * If we did not really move to the previous line, help the user a
2683 * little. It is however a little inconsistent. Normally, when
2684 * backspacing around the point where text wraps to a new line in a
2685 * multi-line form, we absorb one keystroke for the wrapping point. That
2686 * is consistent with SVr4 forms. However, SVr4 does not allow typing
2687 * into the last column of the field, and requires the user to enter a
2688 * newline to move to the next line. Therefore it can consistently eat
2689 * that keystroke. Since ncurses allows the last column, it wraps
2690 * automatically (given the proper options). But we cannot eat the
2691 * keystroke to back over the wrapping point, since that would put the
2692 * cursor past the end of the form field. In this case, just delete the
2693 * character at the end of the field.
2695 if (form->currow == this_row && this_row > 0)
2698 form->curcol = field->dcols - 1;
2703 wmove(form->w, form->currow, form->curcol);
2704 myADDNSTR(form->w, this_line, (int)(this_end - this_line));
2714 /*---------------------------------------------------------------------------
2715 | Facility : libnform
2716 | Function : static int FE_Delete_Line(FORM * form)
2718 | Description : Delete line at cursor position.
2720 | Return Values : E_OK - success
2721 +--------------------------------------------------------------------------*/
2723 FE_Delete_Line(FORM *form)
2725 T((T_CALLED("FE_Delete_Line(%p)"), form));
2731 /*---------------------------------------------------------------------------
2732 | Facility : libnform
2733 | Function : static int FE_Delete_Word(FORM * form)
2735 | Description : Delete word at cursor position
2737 | Return Values : E_OK - success
2738 | E_REQUEST_DENIED - failure
2739 +--------------------------------------------------------------------------*/
2741 FE_Delete_Word(FORM *form)
2743 FIELD *field = form->current;
2744 FIELD_CELL *bp = Address_Of_Current_Row_In_Buffer(form);
2745 FIELD_CELL *ep = bp + field->dcols;
2746 FIELD_CELL *cp = bp + form->curcol;
2749 T((T_CALLED("FE_Delete_Word(%p)"), form));
2750 Synchronize_Buffer(form);
2752 returnCode(E_REQUEST_DENIED); /* not in word */
2754 /* move cursor to begin of word and erase to end of screen-line */
2755 Adjust_Cursor_Position(form,
2756 After_Last_Whitespace_Character(bp, form->curcol));
2757 wmove(form->w, form->currow, form->curcol);
2760 /* skip over word in buffer */
2761 s = Get_First_Whitespace_Character(cp, (int)(ep - cp));
2762 /* to begin of next word */
2763 s = Get_Start_Of_Data(s, (int)(ep - s));
2764 if ((s != cp) && !ISBLANK(*s))
2766 /* copy remaining line to window */
2767 myADDNSTR(form->w, s, (int)(s - After_End_Of_Data(s, (int)(ep - s))));
2772 /*---------------------------------------------------------------------------
2773 | Facility : libnform
2774 | Function : static int FE_Clear_To_End_Of_Line(FORM * form)
2776 | Description : Clear to end of current line.
2778 | Return Values : E_OK - success
2779 +--------------------------------------------------------------------------*/
2781 FE_Clear_To_End_Of_Line(FORM *form)
2783 T((T_CALLED("FE_Clear_To_End_Of_Line(%p)"), form));
2784 wmove(form->w, form->currow, form->curcol);
2789 /*---------------------------------------------------------------------------
2790 | Facility : libnform
2791 | Function : static int FE_Clear_To_End_Of_Field(FORM * form)
2793 | Description : Clear to end of field.
2795 | Return Values : E_OK - success
2796 +--------------------------------------------------------------------------*/
2798 FE_Clear_To_End_Of_Field(FORM *form)
2800 T((T_CALLED("FE_Clear_To_End_Of_Field(%p)"), form));
2801 wmove(form->w, form->currow, form->curcol);
2806 /*---------------------------------------------------------------------------
2807 | Facility : libnform
2808 | Function : static int FE_Clear_Field(FORM * form)
2810 | Description : Clear entire field.
2812 | Return Values : E_OK - success
2813 +--------------------------------------------------------------------------*/
2815 FE_Clear_Field(FORM *form)
2817 T((T_CALLED("FE_Clear_Field(%p)"), form));
2818 form->currow = form->curcol = 0;
2822 /*----------------------------------------------------------------------------
2823 END of Field Editing routines
2824 --------------------------------------------------------------------------*/
2826 /*----------------------------------------------------------------------------
2828 --------------------------------------------------------------------------*/
2830 /*---------------------------------------------------------------------------
2831 | Facility : libnform
2832 | Function : static int EM_Overlay_Mode(FORM * form)
2834 | Description : Switch to overlay mode.
2836 | Return Values : E_OK - success
2837 +--------------------------------------------------------------------------*/
2839 EM_Overlay_Mode(FORM *form)
2841 T((T_CALLED("EM_Overlay_Mode(%p)"), form));
2842 form->status |= _OVLMODE;
2846 /*---------------------------------------------------------------------------
2847 | Facility : libnform
2848 | Function : static int EM_Insert_Mode(FORM * form)
2850 | Description : Switch to insert mode
2852 | Return Values : E_OK - success
2853 +--------------------------------------------------------------------------*/
2855 EM_Insert_Mode(FORM *form)
2857 T((T_CALLED("EM_Insert_Mode(%p)"), form));
2858 form->status &= ~_OVLMODE;
2862 /*----------------------------------------------------------------------------
2863 END of Edit Mode routines
2864 --------------------------------------------------------------------------*/
2866 /*----------------------------------------------------------------------------
2867 Helper routines for Choice Requests
2868 --------------------------------------------------------------------------*/
2870 /*---------------------------------------------------------------------------
2871 | Facility : libnform
2872 | Function : static bool Next_Choice(
2875 | TypeArgument *argp)
2877 | Description : Get the next field choice. For linked types this is
2880 | Return Values : TRUE - next choice successfully retrieved
2881 | FALSE - couldn't retrieve next choice
2882 +--------------------------------------------------------------------------*/
2884 Next_Choice(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
2886 if (!typ || !(typ->status & _HAS_CHOICE))
2889 if (typ->status & _LINKED_TYPE)
2893 Next_Choice(typ->left, field, argp->left) ||
2894 Next_Choice(typ->right, field, argp->right));
2899 return typ->next(field, (void *)argp);
2903 /*---------------------------------------------------------------------------
2904 | Facility : libnform
2905 | Function : static bool Previous_Choice(
2908 | TypeArgument *argp)
2910 | Description : Get the previous field choice. For linked types this
2911 | is done recursively.
2913 | Return Values : TRUE - previous choice successfully retrieved
2914 | FALSE - couldn't retrieve previous choice
2915 +--------------------------------------------------------------------------*/
2917 Previous_Choice(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
2919 if (!typ || !(typ->status & _HAS_CHOICE))
2922 if (typ->status & _LINKED_TYPE)
2926 Previous_Choice(typ->left, field, argp->left) ||
2927 Previous_Choice(typ->right, field, argp->right));
2932 return typ->prev(field, (void *)argp);
2935 /*----------------------------------------------------------------------------
2936 End of Helper routines for Choice Requests
2937 --------------------------------------------------------------------------*/
2939 /*----------------------------------------------------------------------------
2940 Routines for Choice Requests
2941 --------------------------------------------------------------------------*/
2943 /*---------------------------------------------------------------------------
2944 | Facility : libnform
2945 | Function : static int CR_Next_Choice(FORM * form)
2947 | Description : Get the next field choice.
2949 | Return Values : E_OK - success
2950 | E_REQUEST_DENIED - next choice couldn't be retrieved
2951 +--------------------------------------------------------------------------*/
2953 CR_Next_Choice(FORM *form)
2955 FIELD *field = form->current;
2957 T((T_CALLED("CR_Next_Choice(%p)"), form));
2958 Synchronize_Buffer(form);
2959 returnCode((Next_Choice(field->type, field, (TypeArgument *)(field->arg)))
2961 : E_REQUEST_DENIED);
2964 /*---------------------------------------------------------------------------
2965 | Facility : libnform
2966 | Function : static int CR_Previous_Choice(FORM * form)
2968 | Description : Get the previous field choice.
2970 | Return Values : E_OK - success
2971 | E_REQUEST_DENIED - prev. choice couldn't be retrieved
2972 +--------------------------------------------------------------------------*/
2974 CR_Previous_Choice(FORM *form)
2976 FIELD *field = form->current;
2978 T((T_CALLED("CR_Previous_Choice(%p)"), form));
2979 Synchronize_Buffer(form);
2980 returnCode((Previous_Choice(field->type, field, (TypeArgument *)(field->arg)))
2982 : E_REQUEST_DENIED);
2984 /*----------------------------------------------------------------------------
2985 End of Routines for Choice Requests
2986 --------------------------------------------------------------------------*/
2988 /*----------------------------------------------------------------------------
2989 Helper routines for Field Validations.
2990 --------------------------------------------------------------------------*/
2992 /*---------------------------------------------------------------------------
2993 | Facility : libnform
2994 | Function : static bool Check_Field(
2997 | TypeArgument * argp)
2999 | Description : Check the field according to its fieldtype and its
3000 | actual arguments. For linked fieldtypes this is done
3003 | Return Values : TRUE - field is valid
3004 | FALSE - field is invalid.
3005 +--------------------------------------------------------------------------*/
3007 Check_Field(FIELDTYPE *typ, FIELD *field, TypeArgument *argp)
3011 if (field->opts & O_NULLOK)
3013 FIELD_CELL *bp = field->buf;
3016 while (ISBLANK(*bp))
3020 if (CharOf(*bp) == 0)
3024 if (typ->status & _LINKED_TYPE)
3028 Check_Field(typ->left, field, argp->left) ||
3029 Check_Field(typ->right, field, argp->right));
3034 return typ->fcheck(field, (void *)argp);
3040 /*---------------------------------------------------------------------------
3041 | Facility : libnform
3042 | Function : bool _nc_Internal_Validation(FORM * form )
3044 | Description : Validate the current field of the form.
3046 | Return Values : TRUE - field is valid
3047 | FALSE - field is invalid
3048 +--------------------------------------------------------------------------*/
3049 NCURSES_EXPORT(bool)
3050 _nc_Internal_Validation(FORM *form)
3054 field = form->current;
3056 Synchronize_Buffer(form);
3057 if ((form->status & _FCHECK_REQUIRED) ||
3058 (!(field->opts & O_PASSOK)))
3060 if (!Check_Field(field->type, field, (TypeArgument *)(field->arg)))
3062 form->status &= ~_FCHECK_REQUIRED;
3063 field->status |= _CHANGED;
3064 Synchronize_Linked_Fields(field);
3068 /*----------------------------------------------------------------------------
3069 End of Helper routines for Field Validations.
3070 --------------------------------------------------------------------------*/
3072 /*----------------------------------------------------------------------------
3073 Routines for Field Validation.
3074 --------------------------------------------------------------------------*/
3076 /*---------------------------------------------------------------------------
3077 | Facility : libnform
3078 | Function : static int FV_Validation(FORM * form)
3080 | Description : Validate the current field of the form.
3082 | Return Values : E_OK - field valid
3083 | E_INVALID_FIELD - field not valid
3084 +--------------------------------------------------------------------------*/
3086 FV_Validation(FORM *form)
3088 T((T_CALLED("FV_Validation(%p)"), form));
3089 if (_nc_Internal_Validation(form))
3092 returnCode(E_INVALID_FIELD);
3094 /*----------------------------------------------------------------------------
3095 End of routines for Field Validation.
3096 --------------------------------------------------------------------------*/
3098 /*----------------------------------------------------------------------------
3099 Helper routines for Inter-Field Navigation
3100 --------------------------------------------------------------------------*/
3102 /*---------------------------------------------------------------------------
3103 | Facility : libnform
3104 | Function : static FIELD *Next_Field_On_Page(FIELD * field)
3106 | Description : Get the next field after the given field on the current
3107 | page. The order of fields is the one defined by the
3108 | fields array. Only visible and active fields are
3111 | Return Values : Pointer to the next field.
3112 +--------------------------------------------------------------------------*/
3113 INLINE static FIELD *
3114 Next_Field_On_Page(FIELD *field)
3116 FORM *form = field->form;
3117 FIELD **field_on_page = &form->field[field->index];
3118 FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
3119 FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3124 (field_on_page == last_on_page) ? first_on_page : field_on_page + 1;
3125 if (Field_Is_Selectable(*field_on_page))
3128 while (field != (*field_on_page));
3129 return (*field_on_page);
3132 /*---------------------------------------------------------------------------
3133 | Facility : libnform
3134 | Function : FIELD* _nc_First_Active_Field(FORM * form)
3136 | Description : Get the first active field on the current page,
3137 | if there are such. If there are none, get the first
3138 | visible field on the page. If there are also none,
3139 | we return the first field on page and hope the best.
3141 | Return Values : Pointer to calculated field.
3142 +--------------------------------------------------------------------------*/
3143 NCURSES_EXPORT(FIELD *)
3144 _nc_First_Active_Field(FORM *form)
3146 FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3147 FIELD *proposed = Next_Field_On_Page(*last_on_page);
3149 if (proposed == *last_on_page)
3151 /* there might be the special situation, where there is no
3152 active and visible field on the current page. We then select
3153 the first visible field on this readonly page
3155 if (Field_Is_Not_Selectable(proposed))
3157 FIELD **field = &form->field[proposed->index];
3158 FIELD **first = &form->field[form->page[form->curpage].pmin];
3162 field = (field == last_on_page) ? first : field + 1;
3163 if (((*field)->opts & O_VISIBLE))
3166 while (proposed != (*field));
3170 if ((proposed == *last_on_page) && !(proposed->opts & O_VISIBLE))
3172 /* This means, there is also no visible field on the page.
3173 So we propose the first one and hope the very best...
3174 Some very clever user has designed a readonly and invisible
3184 /*---------------------------------------------------------------------------
3185 | Facility : libnform
3186 | Function : static FIELD *Previous_Field_On_Page(FIELD * field)
3188 | Description : Get the previous field before the given field on the
3189 | current page. The order of fields is the one defined by
3190 | the fields array. Only visible and active fields are
3193 | Return Values : Pointer to the previous field.
3194 +--------------------------------------------------------------------------*/
3195 INLINE static FIELD *
3196 Previous_Field_On_Page(FIELD *field)
3198 FORM *form = field->form;
3199 FIELD **field_on_page = &form->field[field->index];
3200 FIELD **first_on_page = &form->field[form->page[form->curpage].pmin];
3201 FIELD **last_on_page = &form->field[form->page[form->curpage].pmax];
3206 (field_on_page == first_on_page) ? last_on_page : field_on_page - 1;
3207 if (Field_Is_Selectable(*field_on_page))
3210 while (field != (*field_on_page));
3212 return (*field_on_page);
3215 /*---------------------------------------------------------------------------
3216 | Facility : libnform
3217 | Function : static FIELD *Sorted_Next_Field(FIELD * field)
3219 | Description : Get the next field after the given field on the current
3220 | page. The order of fields is the one defined by the
3221 | (row,column) geometry, rows are major.
3223 | Return Values : Pointer to the next field.
3224 +--------------------------------------------------------------------------*/
3225 INLINE static FIELD *
3226 Sorted_Next_Field(FIELD *field)
3228 FIELD *field_on_page = field;
3232 field_on_page = field_on_page->snext;
3233 if (Field_Is_Selectable(field_on_page))
3236 while (field_on_page != field);
3238 return (field_on_page);
3241 /*---------------------------------------------------------------------------
3242 | Facility : libnform
3243 | Function : static FIELD *Sorted_Previous_Field(FIELD * field)
3245 | Description : Get the previous field before the given field on the
3246 | current page. The order of fields is the one defined
3247 | by the (row,column) geometry, rows are major.
3249 | Return Values : Pointer to the previous field.
3250 +--------------------------------------------------------------------------*/
3251 INLINE static FIELD *
3252 Sorted_Previous_Field(FIELD *field)
3254 FIELD *field_on_page = field;
3258 field_on_page = field_on_page->sprev;
3259 if (Field_Is_Selectable(field_on_page))
3262 while (field_on_page != field);
3264 return (field_on_page);
3267 /*---------------------------------------------------------------------------
3268 | Facility : libnform
3269 | Function : static FIELD *Left_Neighbor_Field(FIELD * field)
3271 | Description : Get the left neighbor of the field on the same line
3272 | and the same page. Cycles through the line.
3274 | Return Values : Pointer to left neighbor field.
3275 +--------------------------------------------------------------------------*/
3276 INLINE static FIELD *
3277 Left_Neighbor_Field(FIELD *field)
3279 FIELD *field_on_page = field;
3281 /* For a field that has really a left neighbor, the while clause
3282 immediately fails and the loop is left, positioned at the right
3283 neighbor. Otherwise we cycle backwards through the sorted field list
3284 until we enter the same line (from the right end).
3288 field_on_page = Sorted_Previous_Field(field_on_page);
3290 while (field_on_page->frow != field->frow);
3292 return (field_on_page);
3295 /*---------------------------------------------------------------------------
3296 | Facility : libnform
3297 | Function : static FIELD *Right_Neighbor_Field(FIELD * field)
3299 | Description : Get the right neighbor of the field on the same line
3300 | and the same page.
3302 | Return Values : Pointer to right neighbor field.
3303 +--------------------------------------------------------------------------*/
3304 INLINE static FIELD *
3305 Right_Neighbor_Field(FIELD *field)
3307 FIELD *field_on_page = field;
3309 /* See the comments on Left_Neighbor_Field to understand how it works */
3312 field_on_page = Sorted_Next_Field(field_on_page);
3314 while (field_on_page->frow != field->frow);
3316 return (field_on_page);
3319 /*---------------------------------------------------------------------------
3320 | Facility : libnform
3321 | Function : static FIELD *Upper_Neighbor_Field(FIELD * field)
3323 | Description : Because of the row-major nature of sorting the fields,
3324 | it is more difficult to define whats the upper neighbor
3325 | field really means. We define that it must be on a
3326 | 'previous' line (cyclic order!) and is the rightmost
3327 | field laying on the left side of the given field. If
3328 | this set is empty, we take the first field on the line.
3330 | Return Values : Pointer to the upper neighbor field.
3331 +--------------------------------------------------------------------------*/
3333 Upper_Neighbor_Field(FIELD *field)
3335 FIELD *field_on_page = field;
3336 int frow = field->frow;
3337 int fcol = field->fcol;
3339 /* Walk back to the 'previous' line. The second term in the while clause
3340 just guarantees that we stop if we cycled through the line because
3341 there might be no 'previous' line if the page has just one line.
3345 field_on_page = Sorted_Previous_Field(field_on_page);
3347 while (field_on_page->frow == frow && field_on_page->fcol != fcol);
3349 if (field_on_page->frow != frow)
3351 /* We really found a 'previous' line. We are positioned at the
3352 rightmost field on this line */
3353 frow = field_on_page->frow;
3355 /* We walk to the left as long as we are really right of the
3357 while (field_on_page->frow == frow && field_on_page->fcol > fcol)
3358 field_on_page = Sorted_Previous_Field(field_on_page);
3360 /* If we wrapped, just go to the right which is the first field on
3362 if (field_on_page->frow != frow)
3363 field_on_page = Sorted_Next_Field(field_on_page);
3366 return (field_on_page);
3369 /*---------------------------------------------------------------------------
3370 | Facility : libnform
3371 | Function : static FIELD *Down_Neighbor_Field(FIELD * field)
3373 | Description : Because of the row-major nature of sorting the fields,
3374 | its more difficult to define whats the down neighbor
3375 | field really means. We define that it must be on a
3376 | 'next' line (cyclic order!) and is the leftmost
3377 | field laying on the right side of the given field. If
3378 | this set is empty, we take the last field on the line.
3380 | Return Values : Pointer to the upper neighbor field.
3381 +--------------------------------------------------------------------------*/
3383 Down_Neighbor_Field(FIELD *field)
3385 FIELD *field_on_page = field;
3386 int frow = field->frow;
3387 int fcol = field->fcol;
3389 /* Walk forward to the 'next' line. The second term in the while clause
3390 just guarantees that we stop if we cycled through the line because
3391 there might be no 'next' line if the page has just one line.
3395 field_on_page = Sorted_Next_Field(field_on_page);
3397 while (field_on_page->frow == frow && field_on_page->fcol != fcol);
3399 if (field_on_page->frow != frow)
3401 /* We really found a 'next' line. We are positioned at the rightmost
3402 field on this line */
3403 frow = field_on_page->frow;
3405 /* We walk to the right as long as we are really left of the
3407 while (field_on_page->frow == frow && field_on_page->fcol < fcol)
3408 field_on_page = Sorted_Next_Field(field_on_page);
3410 /* If we wrapped, just go to the left which is the last field on
3412 if (field_on_page->frow != frow)
3413 field_on_page = Sorted_Previous_Field(field_on_page);
3416 return (field_on_page);
3419 /*----------------------------------------------------------------------------
3420 Inter-Field Navigation routines
3421 --------------------------------------------------------------------------*/
3423 /*---------------------------------------------------------------------------
3424 | Facility : libnform
3425 | Function : static int Inter_Field_Navigation(
3426 | int (* const fct) (FORM *),
3429 | Description : Generic behavior for changing the current field, the
3430 | field is left and a new field is entered. So the field
3431 | must be validated and the field init/term hooks must
3434 | Return Values : E_OK - success
3435 | E_INVALID_FIELD - field is invalid
3436 | some other - error from subordinate call
3437 +--------------------------------------------------------------------------*/
3439 Inter_Field_Navigation(int (*const fct) (FORM *), FORM *form)
3443 if (!_nc_Internal_Validation(form))
3444 res = E_INVALID_FIELD;
3447 Call_Hook(form, fieldterm);
3449 Call_Hook(form, fieldinit);
3454 /*---------------------------------------------------------------------------
3455 | Facility : libnform
3456 | Function : static int FN_Next_Field(FORM * form)
3458 | Description : Move to the next field on the current page of the form
3460 | Return Values : E_OK - success
3461 | != E_OK - error from subordinate call
3462 +--------------------------------------------------------------------------*/
3464 FN_Next_Field(FORM *form)
3466 T((T_CALLED("FN_Next_Field(%p)"), form));
3467 returnCode(_nc_Set_Current_Field(form,
3468 Next_Field_On_Page(form->current)));
3471 /*---------------------------------------------------------------------------
3472 | Facility : libnform
3473 | Function : static int FN_Previous_Field(FORM * form)
3475 | Description : Move to the previous field on the current page of the
3478 | Return Values : E_OK - success
3479 | != E_OK - error from subordinate call
3480 +--------------------------------------------------------------------------*/
3482 FN_Previous_Field(FORM *form)
3484 T((T_CALLED("FN_Previous_Field(%p)"), form));
3485 returnCode(_nc_Set_Current_Field(form,
3486 Previous_Field_On_Page(form->current)));
3489 /*---------------------------------------------------------------------------
3490 | Facility : libnform
3491 | Function : static int FN_First_Field(FORM * form)
3493 | Description : Move to the first field on the current page of the form
3495 | Return Values : E_OK - success
3496 | != E_OK - error from subordinate call
3497 +--------------------------------------------------------------------------*/
3499 FN_First_Field(FORM *form)
3501 T((T_CALLED("FN_First_Field(%p)"), form));
3502 returnCode(_nc_Set_Current_Field(form,
3503 Next_Field_On_Page(form->field[form->page[form->curpage].pmax])));
3506 /*---------------------------------------------------------------------------
3507 | Facility : libnform
3508 | Function : static int FN_Last_Field(FORM * form)
3510 | Description : Move to the last field on the current page of the form
3512 | Return Values : E_OK - success
3513 | != E_OK - error from subordinate call
3514 +--------------------------------------------------------------------------*/
3516 FN_Last_Field(FORM *form)
3518 T((T_CALLED("FN_Last_Field(%p)"), form));
3520 _nc_Set_Current_Field(form,
3521 Previous_Field_On_Page(form->field[form->page[form->curpage].pmin])));
3524 /*---------------------------------------------------------------------------
3525 | Facility : libnform
3526 | Function : static int FN_Sorted_Next_Field(FORM * form)
3528 | Description : Move to the sorted next field on the current page
3531 | Return Values : E_OK - success
3532 | != E_OK - error from subordinate call
3533 +--------------------------------------------------------------------------*/
3535 FN_Sorted_Next_Field(FORM *form)
3537 T((T_CALLED("FN_Sorted_Next_Field(%p)"), form));
3538 returnCode(_nc_Set_Current_Field(form,
3539 Sorted_Next_Field(form->current)));
3542 /*---------------------------------------------------------------------------
3543 | Facility : libnform
3544 | Function : static int FN_Sorted_Previous_Field(FORM * form)
3546 | Description : Move to the sorted previous field on the current page
3549 | Return Values : E_OK - success
3550 | != E_OK - error from subordinate call
3551 +--------------------------------------------------------------------------*/
3553 FN_Sorted_Previous_Field(FORM *form)
3555 T((T_CALLED("FN_Sorted_Previous_Field(%p)"), form));
3556 returnCode(_nc_Set_Current_Field(form,
3557 Sorted_Previous_Field(form->current)));
3560 /*---------------------------------------------------------------------------
3561 | Facility : libnform
3562 | Function : static int FN_Sorted_First_Field(FORM * form)
3564 | Description : Move to the sorted first field on the current page
3567 | Return Values : E_OK - success
3568 | != E_OK - error from subordinate call
3569 +--------------------------------------------------------------------------*/
3571 FN_Sorted_First_Field(FORM *form)
3573 T((T_CALLED("FN_Sorted_First_Field(%p)"), form));
3574 returnCode(_nc_Set_Current_Field(form,
3575 Sorted_Next_Field(form->field[form->page[form->curpage].smax])));
3578 /*---------------------------------------------------------------------------
3579 | Facility : libnform
3580 | Function : static int FN_Sorted_Last_Field(FORM * form)
3582 | Description : Move to the sorted last field on the current page
3585 | Return Values : E_OK - success
3586 | != E_OK - error from subordinate call
3587 +--------------------------------------------------------------------------*/
3589 FN_Sorted_Last_Field(FORM *form)
3591 T((T_CALLED("FN_Sorted_Last_Field(%p)"), form));
3592 returnCode(_nc_Set_Current_Field(form,
3593 Sorted_Previous_Field(form->field[form->page[form->curpage].smin])));
3596 /*---------------------------------------------------------------------------
3597 | Facility : libnform
3598 | Function : static int FN_Left_Field(FORM * form)
3600 | Description : Get the field on the left of the current field on the
3601 | same line and the same page. Cycles through the line.
3603 | Return Values : E_OK - success
3604 | != E_OK - error from subordinate call
3605 +--------------------------------------------------------------------------*/
3607 FN_Left_Field(FORM *form)
3609 T((T_CALLED("FN_Left_Field(%p)"), form));
3610 returnCode(_nc_Set_Current_Field(form,
3611 Left_Neighbor_Field(form->current)));
3614 /*---------------------------------------------------------------------------
3615 | Facility : libnform
3616 | Function : static int FN_Right_Field(FORM * form)
3618 | Description : Get the field on the right of the current field on the
3619 | same line and the same page. Cycles through the line.
3621 | Return Values : E_OK - success
3622 | != E_OK - error from subordinate call
3623 +--------------------------------------------------------------------------*/
3625 FN_Right_Field(FORM *form)
3627 T((T_CALLED("FN_Right_Field(%p)"), form));
3628 returnCode(_nc_Set_Current_Field(form,
3629 Right_Neighbor_Field(form->current)));
3632 /*---------------------------------------------------------------------------
3633 | Facility : libnform
3634 | Function : static int FN_Up_Field(FORM * form)
3636 | Description : Get the upper neighbor of the current field. This
3637 | cycles through the page. See the comments of the
3638 | Upper_Neighbor_Field function to understand how
3639 | 'upper' is defined.
3641 | Return Values : E_OK - success
3642 | != E_OK - error from subordinate call
3643 +--------------------------------------------------------------------------*/
3645 FN_Up_Field(FORM *form)
3647 T((T_CALLED("FN_Up_Field(%p)"), form));
3648 returnCode(_nc_Set_Current_Field(form,
3649 Upper_Neighbor_Field(form->current)));
3652 /*---------------------------------------------------------------------------
3653 | Facility : libnform
3654 | Function : static int FN_Down_Field(FORM * form)
3656 | Description : Get the down neighbor of the current field. This
3657 | cycles through the page. See the comments of the
3658 | Down_Neighbor_Field function to understand how
3659 | 'down' is defined.
3661 | Return Values : E_OK - success
3662 | != E_OK - error from subordinate call
3663 +--------------------------------------------------------------------------*/
3665 FN_Down_Field(FORM *form)
3667 T((T_CALLED("FN_Down_Field(%p)"), form));
3668 returnCode(_nc_Set_Current_Field(form,
3669 Down_Neighbor_Field(form->current)));
3671 /*----------------------------------------------------------------------------
3672 END of Field Navigation routines
3673 --------------------------------------------------------------------------*/
3675 /*----------------------------------------------------------------------------
3676 Helper routines for Page Navigation
3677 --------------------------------------------------------------------------*/
3679 /*---------------------------------------------------------------------------
3680 | Facility : libnform
3681 | Function : int _nc_Set_Form_Page(FORM * form,
3685 | Description : Make the given page number the current page and make
3686 | the given field the current field on the page. If
3687 | for the field NULL is given, make the first field on
3688 | the page the current field. The routine acts only
3689 | if the requested page is not the current page.
3691 | Return Values : E_OK - success
3692 | != E_OK - error from subordinate call
3693 +--------------------------------------------------------------------------*/
3695 _nc_Set_Form_Page(FORM *form, int page, FIELD *field)
3699 if ((form->curpage != page))
3701 FIELD *last_field, *field_on_page;
3703 werase(Get_Form_Window(form));
3704 form->curpage = page;
3705 last_field = field_on_page = form->field[form->page[page].smin];
3708 if (field_on_page->opts & O_VISIBLE)
3709 if ((res = Display_Field(field_on_page)) != E_OK)
3711 field_on_page = field_on_page->snext;
3713 while (field_on_page != last_field);
3716 res = _nc_Set_Current_Field(form, field);
3718 /* N.B.: we don't encapsulate this by Inter_Field_Navigation(),
3719 because this is already executed in a page navigation
3720 context that contains field navigation
3722 res = FN_First_Field(form);
3727 /*---------------------------------------------------------------------------
3728 | Facility : libnform
3729 | Function : static int Next_Page_Number(const FORM * form)
3731 | Description : Calculate the page number following the current page
3732 | number. This cycles if the highest page number is
3735 | Return Values : The next page number
3736 +--------------------------------------------------------------------------*/
3738 Next_Page_Number(const FORM *form)
3740 return (form->curpage + 1) % form->maxpage;
3743 /*---------------------------------------------------------------------------
3744 | Facility : libnform
3745 | Function : static int Previous_Page_Number(const FORM * form)
3747 | Description : Calculate the page number before the current page
3748 | number. This cycles if the first page number is
3751 | Return Values : The previous page number
3752 +--------------------------------------------------------------------------*/
3754 Previous_Page_Number(const FORM *form)
3756 return (form->curpage != 0 ? form->curpage - 1 : form->maxpage - 1);
3759 /*----------------------------------------------------------------------------
3760 Page Navigation routines
3761 --------------------------------------------------------------------------*/
3763 /*---------------------------------------------------------------------------
3764 | Facility : libnform
3765 | Function : static int Page_Navigation(
3766 | int (* const fct) (FORM *),
3769 | Description : Generic behavior for changing a page. This means
3770 | that the field is left and a new field is entered.
3771 | So the field must be validated and the field init/term
3772 | hooks must be called. Because also the page is changed,
3773 | the forms init/term hooks must be called also.
3775 | Return Values : E_OK - success
3776 | E_INVALID_FIELD - field is invalid
3777 | some other - error from subordinate call
3778 +--------------------------------------------------------------------------*/
3780 Page_Navigation(int (*const fct) (FORM *), FORM *form)
3784 if (!_nc_Internal_Validation(form))
3785 res = E_INVALID_FIELD;
3788 Call_Hook(form, fieldterm);
3789 Call_Hook(form, formterm);
3791 Call_Hook(form, forminit);
3792 Call_Hook(form, fieldinit);
3797 /*---------------------------------------------------------------------------
3798 | Facility : libnform
3799 | Function : static int PN_Next_Page(FORM * form)
3801 | Description : Move to the next page of the form
3803 | Return Values : E_OK - success
3804 | != E_OK - error from subordinate call
3805 +--------------------------------------------------------------------------*/
3807 PN_Next_Page(FORM *form)
3809 T((T_CALLED("PN_Next_Page(%p)"), form));
3810 returnCode(_nc_Set_Form_Page(form, Next_Page_Number(form), (FIELD *)0));
3813 /*---------------------------------------------------------------------------
3814 | Facility : libnform
3815 | Function : static int PN_Previous_Page(FORM * form)
3817 | Description : Move to the previous page of the form
3819 | Return Values : E_OK - success
3820 | != E_OK - error from subordinate call
3821 +--------------------------------------------------------------------------*/
3823 PN_Previous_Page(FORM *form)
3825 T((T_CALLED("PN_Previous_Page(%p)"), form));
3826 returnCode(_nc_Set_Form_Page(form, Previous_Page_Number(form), (FIELD *)0));
3829 /*---------------------------------------------------------------------------
3830 | Facility : libnform
3831 | Function : static int PN_First_Page(FORM * form)
3833 | Description : Move to the first page of the form
3835 | Return Values : E_OK - success
3836 | != E_OK - error from subordinate call
3837 +--------------------------------------------------------------------------*/
3839 PN_First_Page(FORM *form)
3841 T((T_CALLED("PN_First_Page(%p)"), form));
3842 returnCode(_nc_Set_Form_Page(form, 0, (FIELD *)0));
3845 /*---------------------------------------------------------------------------
3846 | Facility : libnform
3847 | Function : static int PN_Last_Page(FORM * form)
3849 | Description : Move to the last page of the form
3851 | Return Values : E_OK - success
3852 | != E_OK - error from subordinate call
3853 +--------------------------------------------------------------------------*/
3855 PN_Last_Page(FORM *form)
3857 T((T_CALLED("PN_Last_Page(%p)"), form));
3858 returnCode(_nc_Set_Form_Page(form, form->maxpage - 1, (FIELD *)0));
3861 /*----------------------------------------------------------------------------
3862 END of Field Navigation routines
3863 --------------------------------------------------------------------------*/
3865 /*----------------------------------------------------------------------------
3866 Helper routines for the core form driver.
3867 --------------------------------------------------------------------------*/
3869 /*---------------------------------------------------------------------------
3870 | Facility : libnform
3871 | Function : static int Data_Entry(FORM * form,int c)
3873 | Description : Enter character c into at the current position of the
3874 | current field of the form.
3876 | Return Values : E_OK -
3877 | E_REQUEST_DENIED -
3879 +--------------------------------------------------------------------------*/
3881 Data_Entry(FORM *form, int c)
3883 FIELD *field = form->current;
3884 int result = E_REQUEST_DENIED;
3886 T((T_CALLED("Data_Entry(%p,%s)"), form, _tracechtype(c)));
3887 if ((field->opts & O_EDIT)
3888 #if FIX_FORM_INACTIVE_BUG
3889 && (field->opts & O_ACTIVE)
3893 if ((field->opts & O_BLANK) &&
3894 First_Position_In_Current_Field(form) &&
3895 !(form->status & _FCHECK_REQUIRED) &&
3896 !(form->status & _WINDOW_MODIFIED))
3899 if (form->status & _OVLMODE)
3901 waddch(form->w, (chtype)c);
3906 bool There_Is_Room = Is_There_Room_For_A_Char_In_Line(form);
3908 if (!(There_Is_Room ||
3909 ((Single_Line_Field(field) && Growable(field)))))
3910 RETURN(E_REQUEST_DENIED);
3912 if (!There_Is_Room && !Field_Grown(field, 1))
3913 RETURN(E_SYSTEM_ERROR);
3915 winsch(form->w, (chtype)c);
3918 if ((result = Wrapping_Not_Necessary_Or_Wrapping_Ok(form)) == E_OK)
3920 bool End_Of_Field = (((field->drows - 1) == form->currow) &&
3921 ((field->dcols - 1) == form->curcol));
3923 form->status |= _WINDOW_MODIFIED;
3924 if (End_Of_Field && !Growable(field) && (field->opts & O_AUTOSKIP))
3925 result = Inter_Field_Navigation(FN_Next_Field, form);
3928 if (End_Of_Field && Growable(field) && !Field_Grown(field, 1))
3929 result = E_SYSTEM_ERROR;
3932 #if USE_WIDEC_SUPPORT
3934 * We have just added a byte to the form field. It may have
3935 * been part of a multibyte character. If it was, the
3936 * addch_used field is nonzero and we should not try to move
3939 if (WINDOW_EXT(form->w, addch_used) == 0)
3940 IFN_Next_Character(form);
3942 IFN_Next_Character(form);
3952 /* Structure to describe the binding of a request code to a function.
3953 The member keycode codes the request value as well as the generic
3954 routine to use for the request. The code for the generic routine
3955 is coded in the upper 16 Bits while the request code is coded in
3958 In terms of C++ you might think of a request as a class with a
3959 virtual method "perform". The different types of request are
3960 derived from this base class and overload (or not) the base class
3961 implementation of perform.
3965 int keycode; /* must be at least 32 bit: hi:mode, lo: key */
3966 int (*cmd) (FORM *); /* low level driver routine for this key */
3970 /* You may see this is the class-id of the request type class */
3971 #define ID_PN (0x00000000) /* Page navigation */
3972 #define ID_FN (0x00010000) /* Inter-Field navigation */
3973 #define ID_IFN (0x00020000) /* Intra-Field navigation */
3974 #define ID_VSC (0x00030000) /* Vertical Scrolling */
3975 #define ID_HSC (0x00040000) /* Horizontal Scrolling */
3976 #define ID_FE (0x00050000) /* Field Editing */
3977 #define ID_EM (0x00060000) /* Edit Mode */
3978 #define ID_FV (0x00070000) /* Field Validation */
3979 #define ID_CH (0x00080000) /* Choice */
3980 #define ID_Mask (0xffff0000)
3981 #define Key_Mask (0x0000ffff)
3982 #define ID_Shft (16)
3984 /* This array holds all the Binding Infos */
3986 static const Binding_Info bindings[MAX_FORM_COMMAND - MIN_FORM_COMMAND + 1] =
3988 { REQ_NEXT_PAGE |ID_PN ,PN_Next_Page},
3989 { REQ_PREV_PAGE |ID_PN ,PN_Previous_Page},
3990 { REQ_FIRST_PAGE |ID_PN ,PN_First_Page},
3991 { REQ_LAST_PAGE |ID_PN ,PN_Last_Page},
3993 { REQ_NEXT_FIELD |ID_FN ,FN_Next_Field},
3994 { REQ_PREV_FIELD |ID_FN ,FN_Previous_Field},
3995 { REQ_FIRST_FIELD |ID_FN ,FN_First_Field},
3996 { REQ_LAST_FIELD |ID_FN ,FN_Last_Field},
3997 { REQ_SNEXT_FIELD |ID_FN ,FN_Sorted_Next_Field},
3998 { REQ_SPREV_FIELD |ID_FN ,FN_Sorted_Previous_Field},
3999 { REQ_SFIRST_FIELD |ID_FN ,FN_Sorted_First_Field},
4000 { REQ_SLAST_FIELD |ID_FN ,FN_Sorted_Last_Field},
4001 { REQ_LEFT_FIELD |ID_FN ,FN_Left_Field},
4002 { REQ_RIGHT_FIELD |ID_FN ,FN_Right_Field},
4003 { REQ_UP_FIELD |ID_FN ,FN_Up_Field},
4004 { REQ_DOWN_FIELD |ID_FN ,FN_Down_Field},
4006 { REQ_NEXT_CHAR |ID_IFN ,IFN_Next_Character},
4007 { REQ_PREV_CHAR |ID_IFN ,IFN_Previous_Character},
4008 { REQ_NEXT_LINE |ID_IFN ,IFN_Next_Line},
4009 { REQ_PREV_LINE |ID_IFN ,IFN_Previous_Line},
4010 { REQ_NEXT_WORD |ID_IFN ,IFN_Next_Word},
4011 { REQ_PREV_WORD |ID_IFN ,IFN_Previous_Word},
4012 { REQ_BEG_FIELD |ID_IFN ,IFN_Beginning_Of_Field},
4013 { REQ_END_FIELD |ID_IFN ,IFN_End_Of_Field},
4014 { REQ_BEG_LINE |ID_IFN ,IFN_Beginning_Of_Line},
4015 { REQ_END_LINE |ID_IFN ,IFN_End_Of_Line},
4016 { REQ_LEFT_CHAR |ID_IFN ,IFN_Left_Character},
4017 { REQ_RIGHT_CHAR |ID_IFN ,IFN_Right_Character},
4018 { REQ_UP_CHAR |ID_IFN ,IFN_Up_Character},
4019 { REQ_DOWN_CHAR |ID_IFN ,IFN_Down_Character},
4021 { REQ_NEW_LINE |ID_FE ,FE_New_Line},
4022 { REQ_INS_CHAR |ID_FE ,FE_Insert_Character},
4023 { REQ_INS_LINE |ID_FE ,FE_Insert_Line},
4024 { REQ_DEL_CHAR |ID_FE ,FE_Delete_Character},
4025 { REQ_DEL_PREV |ID_FE ,FE_Delete_Previous},
4026 { REQ_DEL_LINE |ID_FE ,FE_Delete_Line},
4027 { REQ_DEL_WORD |ID_FE ,FE_Delete_Word},
4028 { REQ_CLR_EOL |ID_FE ,FE_Clear_To_End_Of_Line},
4029 { REQ_CLR_EOF |ID_FE ,FE_Clear_To_End_Of_Field},
4030 { REQ_CLR_FIELD |ID_FE ,FE_Clear_Field},
4032 { REQ_OVL_MODE |ID_EM ,EM_Overlay_Mode},
4033 { REQ_INS_MODE |ID_EM ,EM_Insert_Mode},
4035 { REQ_SCR_FLINE |ID_VSC ,VSC_Scroll_Line_Forward},
4036 { REQ_SCR_BLINE |ID_VSC ,VSC_Scroll_Line_Backward},
4037 { REQ_SCR_FPAGE |ID_VSC ,VSC_Scroll_Page_Forward},
4038 { REQ_SCR_BPAGE |ID_VSC ,VSC_Scroll_Page_Backward},
4039 { REQ_SCR_FHPAGE |ID_VSC ,VSC_Scroll_Half_Page_Forward},
4040 { REQ_SCR_BHPAGE |ID_VSC ,VSC_Scroll_Half_Page_Backward},
4042 { REQ_SCR_FCHAR |ID_HSC ,HSC_Scroll_Char_Forward},
4043 { REQ_SCR_BCHAR |ID_HSC ,HSC_Scroll_Char_Backward},
4044 { REQ_SCR_HFLINE |ID_HSC ,HSC_Horizontal_Line_Forward},
4045 { REQ_SCR_HBLINE |ID_HSC ,HSC_Horizontal_Line_Backward},
4046 { REQ_SCR_HFHALF |ID_HSC ,HSC_Horizontal_Half_Line_Forward},
4047 { REQ_SCR_HBHALF |ID_HSC ,HSC_Horizontal_Half_Line_Backward},
4049 { REQ_VALIDATION |ID_FV ,FV_Validation},
4051 { REQ_NEXT_CHOICE |ID_CH ,CR_Next_Choice},
4052 { REQ_PREV_CHOICE |ID_CH ,CR_Previous_Choice}
4056 /*---------------------------------------------------------------------------
4057 | Facility : libnform
4058 | Function : int form_driver(FORM * form,int c)
4060 | Description : This is the workhorse of the forms system. It checks
4061 | to determine whether the character c is a request or
4062 | data. If it is a request, the form driver executes
4063 | the request and returns the result. If it is data
4064 | (printable character), it enters the data into the
4065 | current position in the current field. If it is not
4066 | recognized, the form driver assumes it is an application
4067 | defined command and returns E_UNKNOWN_COMMAND.
4068 | Application defined command should be defined relative
4069 | to MAX_FORM_COMMAND, the maximum value of a request.
4071 | Return Values : E_OK - success
4072 | E_SYSTEM_ERROR - system error
4073 | E_BAD_ARGUMENT - an argument is incorrect
4074 | E_NOT_POSTED - form is not posted
4075 | E_INVALID_FIELD - field contents are invalid
4076 | E_BAD_STATE - called from inside a hook routine
4077 | E_REQUEST_DENIED - request failed
4078 | E_UNKNOWN_COMMAND - command not known
4079 +--------------------------------------------------------------------------*/
4081 form_driver(FORM *form, int c)
4083 const Binding_Info *BI = (Binding_Info *) 0;
4084 int res = E_UNKNOWN_COMMAND;
4086 T((T_CALLED("form_driver(%p,%d)"), form, c));
4089 RETURN(E_BAD_ARGUMENT);
4092 RETURN(E_NOT_CONNECTED);
4096 if (c == FIRST_ACTIVE_MAGIC)
4098 form->current = _nc_First_Active_Field(form);
4102 assert(form->current &&
4103 form->current->buf &&
4104 (form->current->form == form)
4107 if (form->status & _IN_DRIVER)
4108 RETURN(E_BAD_STATE);
4110 if (!(form->status & _POSTED))
4111 RETURN(E_NOT_POSTED);
4113 if ((c >= MIN_FORM_COMMAND && c <= MAX_FORM_COMMAND) &&
4114 ((bindings[c - MIN_FORM_COMMAND].keycode & Key_Mask) == c))
4115 BI = &(bindings[c - MIN_FORM_COMMAND]);
4119 typedef int (*Generic_Method) (int (*const) (FORM *), FORM *);
4120 static const Generic_Method Generic_Methods[] =
4122 Page_Navigation, /* overloaded to call field&form hooks */
4123 Inter_Field_Navigation, /* overloaded to call field hooks */
4124 NULL, /* Intra-Field is generic */
4125 Vertical_Scrolling, /* Overloaded to check multi-line */
4126 Horizontal_Scrolling, /* Overloaded to check single-line */
4127 Field_Editing, /* Overloaded to mark modification */
4128 NULL, /* Edit Mode is generic */
4129 NULL, /* Field Validation is generic */
4130 NULL /* Choice Request is generic */
4132 size_t nMethods = (sizeof(Generic_Methods) / sizeof(Generic_Methods[0]));
4133 size_t method = ((BI->keycode & ID_Mask) >> ID_Shft) & 0xffff;
4135 if ((method >= nMethods) || !(BI->cmd))
4136 res = E_SYSTEM_ERROR;
4139 Generic_Method fct = Generic_Methods[method];
4142 res = fct(BI->cmd, form);
4144 res = (BI->cmd) (form);
4147 else if (!(c & (~(int)MAX_REGULAR_CHARACTER)))
4150 * If we're using 8-bit characters, iscntrl+isprint cover the whole set.
4151 * But with multibyte characters, there is a third possibility, i.e.,
4152 * parts of characters that build up into printable characters which are
4153 * not considered printable.
4155 * FIXME: the wide-character branch should also use Check_Char().
4157 #if USE_WIDEC_SUPPORT
4158 if (!iscntrl(UChar(c)))
4160 if (isprint(UChar(c)) &&
4161 Check_Char(form->current->type, c,
4162 (TypeArgument *)(form->current->arg)))
4164 res = Data_Entry(form, c);
4166 _nc_Refresh_Current_Field(form);
4170 /*----------------------------------------------------------------------------
4171 Field-Buffer manipulation routines.
4172 The effects of setting a buffer are tightly coupled to the core of the form
4173 driver logic. This is especially true in the case of growable fields.
4174 So I don't separate this into a separate module.
4175 --------------------------------------------------------------------------*/
4177 /*---------------------------------------------------------------------------
4178 | Facility : libnform
4179 | Function : int set_field_buffer(FIELD *field,
4180 | int buffer, char *value)
4182 | Description : Set the given buffer of the field to the given value.
4183 | Buffer 0 stores the displayed content of the field.
4184 | For dynamic fields this may grow the fieldbuffers if
4185 | the length of the value exceeds the current buffer
4186 | length. For buffer 0 only printable values are allowed.
4187 | For static fields, the value needs not to be zero ter-
4188 | minated. It is copied up to the length of the buffer.
4190 | Return Values : E_OK - success
4191 | E_BAD_ARGUMENT - invalid argument
4192 | E_SYSTEM_ERROR - system error
4193 +--------------------------------------------------------------------------*/
4195 set_field_buffer(FIELD *field, int buffer, const char *value)
4202 #if USE_WIDEC_SUPPORT
4203 FIELD_CELL *widevalue = 0;
4206 T((T_CALLED("set_field_buffer(%p,%d,%s)"), field, buffer, _nc_visbuf(value)));
4208 if (!field || !value || ((buffer < 0) || (buffer > field->nbuf)))
4209 RETURN(E_BAD_ARGUMENT);
4211 len = Buffer_Length(field);
4215 for (i = 0; (value[i] != '\0') && (i < len); ++i)
4217 if (iscntrl(UChar(value[i])))
4218 RETURN(E_BAD_ARGUMENT);
4222 if (Growable(field))
4224 /* for a growable field we must assume zero terminated strings, because
4225 somehow we have to detect the length of what should be copied.
4227 unsigned int vlen = strlen(value);
4231 if (!Field_Grown(field,
4232 (int)(1 + (vlen - len) / ((field->rows + field->nrow)
4234 RETURN(E_SYSTEM_ERROR);
4236 /* in this case we also have to check, whether or not the remaining
4237 characters in value are also printable for buffer 0. */
4240 for (i = len; i < vlen; i++)
4241 if (iscntrl(UChar(value[i])))
4242 RETURN(E_BAD_ARGUMENT);
4248 p = Address_Of_Nth_Buffer(field, buffer);
4250 #if USE_WIDEC_SUPPORT
4252 * Use addstr's logic for converting a string to an array of cchar_t's.
4253 * There should be a better way, but this handles nonspacing characters
4254 * and other special cases that we really do not want to handle here.
4256 wclear(field->working);
4257 mvwaddstr(field->working, 0, 0, value);
4259 if ((widevalue = (FIELD_CELL *)calloc(len, sizeof(FIELD_CELL))) == 0)
4261 RETURN(E_SYSTEM_ERROR);
4265 mvwin_wchnstr(field->working, 0, 0, widevalue, (int)len);
4266 for (i = 0; i < len; ++i)
4268 if (CharEq(myZEROS, widevalue[i]))
4274 p[i] = widevalue[i];
4279 for (i = 0; i < len; ++i)
4281 if (value[i] == '\0')
4295 if (((syncres = Synchronize_Field(field)) != E_OK) &&
4298 if (((syncres = Synchronize_Linked_Fields(field)) != E_OK) &&
4305 /*---------------------------------------------------------------------------
4306 | Facility : libnform
4307 | Function : char *field_buffer(const FIELD *field,int buffer)
4309 | Description : Return the address of the buffer for the field.
4311 | Return Values : Pointer to buffer or NULL if arguments were invalid.
4312 +--------------------------------------------------------------------------*/
4313 NCURSES_EXPORT(char *)
4314 field_buffer(const FIELD *field, int buffer)
4318 T((T_CALLED("field_buffer(%p,%d)"), field, buffer));
4320 if (field && (buffer >= 0) && (buffer <= field->nbuf))
4322 #if USE_WIDEC_SUPPORT
4323 FIELD_CELL *data = Address_Of_Nth_Buffer(field, buffer);
4325 int size = Buffer_Length(field);
4328 /* determine the number of bytes needed to store the expanded string */
4329 for (n = 0; n < size; ++n)
4331 if (!isWidecExt(data[n]))
4337 next = _nc_wcrtomb(0, data[n].chars[0], &state);
4338 if (!isEILSEQ(next))
4346 /* allocate a place to store the expanded string */
4347 if (field->expanded[buffer] != 0)
4348 free(field->expanded[buffer]);
4349 field->expanded[buffer] = typeMalloc(char, need + 1);
4351 /* expand the multibyte data */
4352 if ((result = field->expanded[buffer]) != 0)