xref: /openbsd-src/lib/libform/form.priv.h (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1*c7ef0cfcSnicm /*	$OpenBSD: form.priv.h,v 1.9 2023/10/17 09:52:10 nicm Exp $	*/
202f2426aSmillert /****************************************************************************
3*c7ef0cfcSnicm  * Copyright 2018-2020,2021 Thomas E. Dickey                                *
4*c7ef0cfcSnicm  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
502f2426aSmillert  *                                                                          *
602f2426aSmillert  * Permission is hereby granted, free of charge, to any person obtaining a  *
702f2426aSmillert  * copy of this software and associated documentation files (the            *
802f2426aSmillert  * "Software"), to deal in the Software without restriction, including      *
902f2426aSmillert  * without limitation the rights to use, copy, modify, merge, publish,      *
1002f2426aSmillert  * distribute, distribute with modifications, sublicense, and/or sell       *
1102f2426aSmillert  * copies of the Software, and to permit persons to whom the Software is    *
1202f2426aSmillert  * furnished to do so, subject to the following conditions:                 *
1302f2426aSmillert  *                                                                          *
1402f2426aSmillert  * The above copyright notice and this permission notice shall be included  *
1502f2426aSmillert  * in all copies or substantial portions of the Software.                   *
1602f2426aSmillert  *                                                                          *
1702f2426aSmillert  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1802f2426aSmillert  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1902f2426aSmillert  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
2002f2426aSmillert  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
2102f2426aSmillert  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2202f2426aSmillert  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2302f2426aSmillert  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2402f2426aSmillert  *                                                                          *
2502f2426aSmillert  * Except as contained in this notice, the name(s) of the above copyright   *
2602f2426aSmillert  * holders shall not be used in advertising or otherwise to promote the     *
2702f2426aSmillert  * sale, use or other dealings in this Software without prior written       *
2802f2426aSmillert  * authorization.                                                           *
2902f2426aSmillert  ****************************************************************************/
3002f2426aSmillert 
3102f2426aSmillert /****************************************************************************
3281d8c4e1Snicm  *   Author:  Juergen Pfeifer, 1995,1997                                    *
3302f2426aSmillert  ****************************************************************************/
34ae611fdaStholo 
35*c7ef0cfcSnicm /* $Id: form.priv.h,v 1.9 2023/10/17 09:52:10 nicm Exp $ */
3681d8c4e1Snicm 
3781d8c4e1Snicm #ifndef FORM_PRIV_H
3881d8c4e1Snicm #define FORM_PRIV_H 1
39*c7ef0cfcSnicm /* *INDENT-OFF*/
4081d8c4e1Snicm #include "curses.priv.h"
41*c7ef0cfcSnicm 
42*c7ef0cfcSnicm #define NCURSES_OPAQUE_FORM  0
43*c7ef0cfcSnicm 
44ae611fdaStholo #include "mf_common.h"
4581d8c4e1Snicm 
4681d8c4e1Snicm #if USE_WIDEC_SUPPORT
4781d8c4e1Snicm #if HAVE_WCTYPE_H
4881d8c4e1Snicm #include <wctype.h>
4981d8c4e1Snicm #endif
5081d8c4e1Snicm 
5181d8c4e1Snicm #ifndef MB_LEN_MAX
5281d8c4e1Snicm #define MB_LEN_MAX 8 /* should be >= MB_CUR_MAX, but that may be a function */
5381d8c4e1Snicm #endif
5481d8c4e1Snicm 
5581d8c4e1Snicm #define FIELD_CELL NCURSES_CH_T
5681d8c4e1Snicm 
5781d8c4e1Snicm #define NCURSES_FIELD_INTERNALS char** expanded; WINDOW *working;
5881d8c4e1Snicm #define NCURSES_FIELD_EXTENSION , (char **)0, (WINDOW *)0
5981d8c4e1Snicm 
6081d8c4e1Snicm #else
6181d8c4e1Snicm 
6281d8c4e1Snicm #define FIELD_CELL char
6381d8c4e1Snicm 
6481d8c4e1Snicm #define NCURSES_FIELD_EXTENSION /* nothing */
6581d8c4e1Snicm 
6681d8c4e1Snicm #endif
6781d8c4e1Snicm 
68ae611fdaStholo #include "form.h"
69ae611fdaStholo 
70*c7ef0cfcSnicm 	/***********************
71*c7ef0cfcSnicm 	*   Default objects    *
72*c7ef0cfcSnicm 	***********************/
73*c7ef0cfcSnicm extern FORM_EXPORT_VAR(FORM *)      _nc_Default_Form;
74*c7ef0cfcSnicm extern FORM_EXPORT_VAR(FIELD *)     _nc_Default_Field;
75*c7ef0cfcSnicm extern FORM_EXPORT_VAR(FIELDTYPE *) _nc_Default_FieldType;
76*c7ef0cfcSnicm 
77ae611fdaStholo /* form  status values */
7881d8c4e1Snicm #define _OVLMODE         (0x04U) /* Form is in overlay mode                */
7981d8c4e1Snicm #define _WINDOW_MODIFIED (0x10U) /* Current field window has been modified */
8081d8c4e1Snicm #define _FCHECK_REQUIRED (0x20U) /* Current field needs validation         */
81ae611fdaStholo 
82ae611fdaStholo /* field status values */
8381d8c4e1Snicm #define _CHANGED         (0x01U) /* Field has been changed                 */
8481d8c4e1Snicm #define _NEWTOP          (0x02U) /* Vertical scrolling occurred            */
8581d8c4e1Snicm #define _NEWPAGE         (0x04U) /* field begins new page of form          */
8681d8c4e1Snicm #define _MAY_GROW        (0x08U) /* dynamic field may still grow           */
87ae611fdaStholo 
88ae611fdaStholo /* fieldtype status values */
8981d8c4e1Snicm #define _LINKED_TYPE     (0x01U) /* Type is a linked type                  */
9081d8c4e1Snicm #define _HAS_ARGS        (0x02U) /* Type has arguments                     */
9181d8c4e1Snicm #define _HAS_CHOICE      (0x04U) /* Type has choice methods                */
9281d8c4e1Snicm #define _RESIDENT        (0x08U) /* Type is built-in                       */
93*c7ef0cfcSnicm #define _GENERIC         (0x10U) /* A generic field type                   */
94ae611fdaStholo 
958d0fca71Smillert /* This are the field options required to be a selectable field in field
968d0fca71Smillert    navigation requests */
978d0fca71Smillert #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
988d0fca71Smillert 
99ae611fdaStholo /* If form is NULL replace form argument by default-form */
10081d8c4e1Snicm #define Normalize_Form(form) \
10181d8c4e1Snicm   ((form) = (form != 0) ? (form) : _nc_Default_Form)
102ae611fdaStholo 
103ae611fdaStholo /* If field is NULL replace field argument by default-field */
10481d8c4e1Snicm #define Normalize_Field(field) \
10581d8c4e1Snicm   ((field) = (field != 0) ? (field) : _nc_Default_Field)
106ae611fdaStholo 
107*c7ef0cfcSnicm #if NCURSES_SP_FUNCS
108*c7ef0cfcSnicm #define Get_Form_Screen(form) \
109*c7ef0cfcSnicm   ((form)->win ? _nc_screen_of((form->win)):CURRENT_SCREEN)
110*c7ef0cfcSnicm #else
111*c7ef0cfcSnicm #define Get_Form_Screen(form) CURRENT_SCREEN
112*c7ef0cfcSnicm #endif
113*c7ef0cfcSnicm 
114*c7ef0cfcSnicm /* Retrieve form's window */
115ae611fdaStholo #define Get_Form_Window(form) \
116*c7ef0cfcSnicm   ((form)->sub \
117*c7ef0cfcSnicm    ? (form)->sub \
118*c7ef0cfcSnicm    : ((form)->win \
119*c7ef0cfcSnicm       ? (form)->win \
120*c7ef0cfcSnicm       : StdScreen(Get_Form_Screen(form))))
121ae611fdaStholo 
122ae611fdaStholo /* Calculate the size for a single buffer for this field */
123ae611fdaStholo #define Buffer_Length(field) ((field)->drows * (field)->dcols)
124ae611fdaStholo 
125ae611fdaStholo /* Calculate the total size of all buffers for this field */
126ae611fdaStholo #define Total_Buffer_Size(field) \
127*c7ef0cfcSnicm    ( (size_t)(Buffer_Length(field) + 1) * (size_t)(1+(field)->nbuf) * sizeof(FIELD_CELL) )
128ae611fdaStholo 
129ae611fdaStholo /* Logic to determine whether or not a field is single lined */
130ae611fdaStholo #define Single_Line_Field(field) \
131ae611fdaStholo    (((field)->rows + (field)->nrow) == 1)
132ae611fdaStholo 
133*c7ef0cfcSnicm #define Field_Has_Option(f,o)      ((((unsigned)(f)->opts) & o) != 0)
134*c7ef0cfcSnicm 
1358d0fca71Smillert /* Logic to determine whether or not a field is selectable */
13681d8c4e1Snicm #define Field_Is_Selectable(f)     (((unsigned)((f)->opts) & O_SELECTABLE)==O_SELECTABLE)
13781d8c4e1Snicm #define Field_Is_Not_Selectable(f) (((unsigned)((f)->opts) & O_SELECTABLE)!=O_SELECTABLE)
138ae611fdaStholo 
13981d8c4e1Snicm typedef struct typearg
14081d8c4e1Snicm   {
141ae611fdaStholo     struct typearg *left;
142ae611fdaStholo     struct typearg *right;
14381d8c4e1Snicm   }
14481d8c4e1Snicm TypeArgument;
145ae611fdaStholo 
146ae611fdaStholo /* This is a dummy request code (normally invalid) to be used internally
147ae611fdaStholo    with the form_driver() routine to position to the first active field
148ae611fdaStholo    on the form
149ae611fdaStholo */
150ae611fdaStholo #define FIRST_ACTIVE_MAGIC (-291056)
151ae611fdaStholo 
152ae611fdaStholo #define ALL_FORM_OPTS  (                \
153ae611fdaStholo 			O_NL_OVERLOAD  |\
154ae611fdaStholo 			O_BS_OVERLOAD   )
155ae611fdaStholo 
156*c7ef0cfcSnicm #define STD_FIELD_OPTS (Field_Options)( \
157ae611fdaStholo 			O_VISIBLE |\
158ae611fdaStholo 			O_ACTIVE  |\
159ae611fdaStholo 			O_PUBLIC  |\
160ae611fdaStholo 			O_EDIT    |\
161ae611fdaStholo 			O_WRAP    |\
162ae611fdaStholo 			O_BLANK   |\
163ae611fdaStholo 			O_AUTOSKIP|\
164ae611fdaStholo 			O_NULLOK  |\
165ae611fdaStholo 			O_PASSOK  |\
166ae611fdaStholo 			O_STATIC)
167ae611fdaStholo 
168*c7ef0cfcSnicm #define ALL_FIELD_OPTS (Field_Options)( \
169*c7ef0cfcSnicm 			STD_FIELD_OPTS |\
170*c7ef0cfcSnicm 			O_DYNAMIC_JUSTIFY |\
171*c7ef0cfcSnicm 			O_NO_LEFT_STRIP |\
172*c7ef0cfcSnicm 			O_EDGE_INSERT_STAY |\
173*c7ef0cfcSnicm 			O_INPUT_LIMIT)
174*c7ef0cfcSnicm 
175ae611fdaStholo #define C_BLANK ' '
176ae611fdaStholo #define is_blank(c) ((c)==C_BLANK)
1778d0fca71Smillert 
17881d8c4e1Snicm #define C_ZEROS '\0'
17981d8c4e1Snicm 
180*c7ef0cfcSnicm extern FORM_EXPORT(TypeArgument *) _nc_Make_Argument (const FIELDTYPE*, va_list*, int*);
181*c7ef0cfcSnicm extern FORM_EXPORT(TypeArgument *) _nc_Copy_Argument (const FIELDTYPE*, const TypeArgument*, int*);
182*c7ef0cfcSnicm extern FORM_EXPORT(void) _nc_Free_Argument (const FIELDTYPE*, TypeArgument*);
183*c7ef0cfcSnicm extern FORM_EXPORT(bool) _nc_Copy_Type (FIELD*, FIELD const *);
184*c7ef0cfcSnicm extern FORM_EXPORT(void) _nc_Free_Type (FIELD *);
1858d0fca71Smillert 
186*c7ef0cfcSnicm extern FORM_EXPORT(int) _nc_Synchronize_Attributes (FIELD*);
187*c7ef0cfcSnicm extern FORM_EXPORT(int) _nc_Synchronize_Options (FIELD*, Field_Options);
188*c7ef0cfcSnicm extern FORM_EXPORT(int) _nc_Set_Form_Page (FORM*, int, FIELD*);
189*c7ef0cfcSnicm extern FORM_EXPORT(int) _nc_Refresh_Current_Field (FORM*);
190*c7ef0cfcSnicm extern FORM_EXPORT(FIELD *) _nc_First_Active_Field (FORM*);
191*c7ef0cfcSnicm extern FORM_EXPORT(bool) _nc_Internal_Validation (FORM*);
192*c7ef0cfcSnicm extern FORM_EXPORT(int) _nc_Set_Current_Field (FORM*, FIELD*);
193*c7ef0cfcSnicm extern FORM_EXPORT(int) _nc_Position_Form_Cursor (FORM*);
194*c7ef0cfcSnicm extern FORM_EXPORT(void) _nc_Unset_Current_Field(FORM *form);
1958d0fca71Smillert 
196*c7ef0cfcSnicm #if NCURSES_INTEROP_FUNCS
197*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_INTEGER(void);
198*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ALNUM(void);
199*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ALPHA(void);
200*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ENUM(void);
201*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_NUMERIC(void);
202*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_REGEXP(void);
203*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_IPV4(void);
204*c7ef0cfcSnicm 
205*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *)
206*c7ef0cfcSnicm _nc_generic_fieldtype(bool (*const field_check) (FORM*,
207*c7ef0cfcSnicm 						 FIELD *,
208*c7ef0cfcSnicm 						 const void *),
209*c7ef0cfcSnicm 		      bool (*const char_check)  (int,
210*c7ef0cfcSnicm 						 FORM*,
211*c7ef0cfcSnicm 						 FIELD*,
212*c7ef0cfcSnicm 						 const void *),
213*c7ef0cfcSnicm 		      bool (*const next)(FORM*,FIELD*,const void*),
214*c7ef0cfcSnicm 		      bool (*const prev)(FORM*,FIELD*,const void*),
215*c7ef0cfcSnicm 		      void (*freecallback)(void*));
216*c7ef0cfcSnicm extern FORM_EXPORT(int) _nc_set_generic_fieldtype(FIELD*, FIELDTYPE*, int (*)(void**));
217*c7ef0cfcSnicm extern FORM_EXPORT(WINDOW*) _nc_form_cursor(const FORM* , int* , int* );
218*c7ef0cfcSnicm 
219*c7ef0cfcSnicm #define INIT_FT_FUNC(func) {func}
220*c7ef0cfcSnicm #else
221*c7ef0cfcSnicm #define INIT_FT_FUNC(func) func
222*c7ef0cfcSnicm #endif
223*c7ef0cfcSnicm 
224*c7ef0cfcSnicm extern FORM_EXPORT(void) _nc_get_fieldbuffer(FORM*, FIELD*, FIELD_CELL*);
22581d8c4e1Snicm 
22681d8c4e1Snicm #if USE_WIDEC_SUPPORT
227*c7ef0cfcSnicm extern FORM_EXPORT(wchar_t *) _nc_Widen_String(char *, int *);
22881d8c4e1Snicm #endif
22981d8c4e1Snicm 
23081d8c4e1Snicm #ifdef TRACE
23181d8c4e1Snicm 
232*c7ef0cfcSnicm #define returnField(code)	TRACE_RETURN1(code,field)
233*c7ef0cfcSnicm #define returnFieldPtr(code)	TRACE_RETURN1(code,field_ptr)
234*c7ef0cfcSnicm #define returnForm(code)	TRACE_RETURN1(code,form)
235*c7ef0cfcSnicm #define returnFieldType(code)	TRACE_RETURN1(code,field_type)
236*c7ef0cfcSnicm #define returnFormHook(code)	TRACE_RETURN1(code,form_hook)
23781d8c4e1Snicm 
238*c7ef0cfcSnicm extern FORM_EXPORT(FIELD **)	    _nc_retrace_field_ptr (FIELD **);
239*c7ef0cfcSnicm extern FORM_EXPORT(FIELD *)	    _nc_retrace_field (FIELD *);
240*c7ef0cfcSnicm extern FORM_EXPORT(FIELDTYPE *)  _nc_retrace_field_type (FIELDTYPE *);
241*c7ef0cfcSnicm extern FORM_EXPORT(FORM *)       _nc_retrace_form (FORM *);
242*c7ef0cfcSnicm extern FORM_EXPORT(Form_Hook)    _nc_retrace_form_hook (Form_Hook);
24381d8c4e1Snicm 
24481d8c4e1Snicm #else /* !TRACE */
24581d8c4e1Snicm 
24681d8c4e1Snicm #define returnFieldPtr(code)	return code
24781d8c4e1Snicm #define returnFieldType(code)	return code
24881d8c4e1Snicm #define returnField(code)	return code
24981d8c4e1Snicm #define returnForm(code)	return code
25081d8c4e1Snicm #define returnFormHook(code)	return code
25181d8c4e1Snicm 
25281d8c4e1Snicm #endif /* TRACE/!TRACE */
25381d8c4e1Snicm 
25481d8c4e1Snicm /*
25581d8c4e1Snicm  * Use Check_CTYPE_Field() to simplify FIELDTYPE's that use only the ccheck()
25681d8c4e1Snicm  * function.
25781d8c4e1Snicm  */
25881d8c4e1Snicm #if USE_WIDEC_SUPPORT
25981d8c4e1Snicm #define Check_CTYPE_Field(result, buffer, width, ccheck) \
26081d8c4e1Snicm   while (*buffer && *buffer == ' ') \
26181d8c4e1Snicm     buffer++; \
26281d8c4e1Snicm   if (*buffer) \
26381d8c4e1Snicm     { \
26481d8c4e1Snicm       bool blank = FALSE; \
26581d8c4e1Snicm       int len; \
26681d8c4e1Snicm       int n; \
26781d8c4e1Snicm       wchar_t *list = _nc_Widen_String((char *)buffer, &len); \
26881d8c4e1Snicm       if (list != 0) \
26981d8c4e1Snicm 	{ \
27081d8c4e1Snicm 	  result = TRUE; \
27181d8c4e1Snicm 	  for (n = 0; n < len; ++n) \
27281d8c4e1Snicm 	    { \
27381d8c4e1Snicm 	      if (blank) \
27481d8c4e1Snicm 		{ \
27581d8c4e1Snicm 		  if (list[n] != ' ') \
27681d8c4e1Snicm 		    { \
27781d8c4e1Snicm 		      result = FALSE; \
27881d8c4e1Snicm 		      break; \
27981d8c4e1Snicm 		    } \
28081d8c4e1Snicm 		} \
28181d8c4e1Snicm 	      else if (list[n] == ' ') \
28281d8c4e1Snicm 		{ \
28381d8c4e1Snicm 		  blank = TRUE; \
28481d8c4e1Snicm 		  result = (n + 1 >= width); \
28581d8c4e1Snicm 		} \
28681d8c4e1Snicm 	      else if (!ccheck(list[n], NULL)) \
28781d8c4e1Snicm 		{ \
28881d8c4e1Snicm 		  result = FALSE; \
28981d8c4e1Snicm 		  break; \
29081d8c4e1Snicm 		} \
29181d8c4e1Snicm 	    } \
29281d8c4e1Snicm 	  free(list); \
29381d8c4e1Snicm 	} \
29481d8c4e1Snicm     }
29581d8c4e1Snicm #else
29681d8c4e1Snicm #define Check_CTYPE_Field(result, buffer, width, ccheck) \
29781d8c4e1Snicm   while (*buffer && *buffer == ' ') \
29881d8c4e1Snicm     buffer++; \
29981d8c4e1Snicm   if (*buffer) \
30081d8c4e1Snicm     { \
30181d8c4e1Snicm       unsigned char *s = buffer; \
30281d8c4e1Snicm       int l = -1; \
30381d8c4e1Snicm       while (*buffer && ccheck(*buffer, NULL)) \
30481d8c4e1Snicm 	buffer++; \
30581d8c4e1Snicm       l = (int)(buffer - s); \
30681d8c4e1Snicm       while (*buffer && *buffer == ' ') \
30781d8c4e1Snicm 	buffer++; \
30881d8c4e1Snicm       result = ((*buffer || (l < width)) ? FALSE : TRUE); \
30981d8c4e1Snicm     }
31081d8c4e1Snicm #endif
311*c7ef0cfcSnicm /* *INDENT-ON*/
31281d8c4e1Snicm 
31381d8c4e1Snicm #endif /* FORM_PRIV_H */
314