xref: /freebsd/contrib/ncurses/form/form.priv.h (revision 8881d206f4e68b564c2c5f50fc717086fc3e827a)
1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *   Author:  Juergen Pfeifer, 1995,1997                                    *
32  ****************************************************************************/
33 
34 /* $Id: form.priv.h,v 0.47 2020/12/11 23:20:11 tom Exp $ */
35 
36 #ifndef FORM_PRIV_H
37 #define FORM_PRIV_H 1
38 /* *INDENT-OFF*/
39 #include "curses.priv.h"
40 
41 #define NCURSES_OPAQUE_FORM  0
42 
43 #include "mf_common.h"
44 
45 #if USE_WIDEC_SUPPORT
46 #if HAVE_WCTYPE_H
47 #include <wctype.h>
48 #endif
49 
50 #ifndef MB_LEN_MAX
51 #define MB_LEN_MAX 8 /* should be >= MB_CUR_MAX, but that may be a function */
52 #endif
53 
54 #define FIELD_CELL NCURSES_CH_T
55 
56 #define NCURSES_FIELD_INTERNALS char** expanded; WINDOW *working;
57 #define NCURSES_FIELD_EXTENSION , (char **)0, (WINDOW *)0
58 
59 #else
60 
61 #define FIELD_CELL char
62 
63 #define NCURSES_FIELD_EXTENSION /* nothing */
64 
65 #endif
66 
67 #include "form.h"
68 
69 	/***********************
70 	*   Default objects    *
71 	***********************/
72 extern FORM_EXPORT_VAR(FORM *)      _nc_Default_Form;
73 extern FORM_EXPORT_VAR(FIELD *)     _nc_Default_Field;
74 extern FORM_EXPORT_VAR(FIELDTYPE *) _nc_Default_FieldType;
75 
76 /* form  status values */
77 #define _OVLMODE         (0x04U) /* Form is in overlay mode                */
78 #define _WINDOW_MODIFIED (0x10U) /* Current field window has been modified */
79 #define _FCHECK_REQUIRED (0x20U) /* Current field needs validation         */
80 
81 /* field status values */
82 #define _CHANGED         (0x01U) /* Field has been changed                 */
83 #define _NEWTOP          (0x02U) /* Vertical scrolling occurred            */
84 #define _NEWPAGE         (0x04U) /* field begins new page of form          */
85 #define _MAY_GROW        (0x08U) /* dynamic field may still grow           */
86 
87 /* fieldtype status values */
88 #define _LINKED_TYPE     (0x01U) /* Type is a linked type                  */
89 #define _HAS_ARGS        (0x02U) /* Type has arguments                     */
90 #define _HAS_CHOICE      (0x04U) /* Type has choice methods                */
91 #define _RESIDENT        (0x08U) /* Type is built-in                       */
92 #define _GENERIC         (0x10U) /* A generic field type                   */
93 
94 /* This are the field options required to be a selectable field in field
95    navigation requests */
96 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
97 
98 /* If form is NULL replace form argument by default-form */
99 #define Normalize_Form(form) \
100   ((form) = (form != 0) ? (form) : _nc_Default_Form)
101 
102 /* If field is NULL replace field argument by default-field */
103 #define Normalize_Field(field) \
104   ((field) = (field != 0) ? (field) : _nc_Default_Field)
105 
106 #if NCURSES_SP_FUNCS
107 #define Get_Form_Screen(form) \
108   ((form)->win ? _nc_screen_of((form->win)):CURRENT_SCREEN)
109 #else
110 #define Get_Form_Screen(form) CURRENT_SCREEN
111 #endif
112 
113 /* Retrieve form's window */
114 #define Get_Form_Window(form) \
115   ((form)->sub \
116    ? (form)->sub \
117    : ((form)->win \
118       ? (form)->win \
119       : StdScreen(Get_Form_Screen(form))))
120 
121 /* Calculate the size for a single buffer for this field */
122 #define Buffer_Length(field) ((field)->drows * (field)->dcols)
123 
124 /* Calculate the total size of all buffers for this field */
125 #define Total_Buffer_Size(field) \
126    ( (size_t)(Buffer_Length(field) + 1) * (size_t)(1+(field)->nbuf) * sizeof(FIELD_CELL) )
127 
128 /* Logic to determine whether or not a field is single lined */
129 #define Single_Line_Field(field) \
130    (((field)->rows + (field)->nrow) == 1)
131 
132 #define Field_Has_Option(f,o)      ((((unsigned)(f)->opts) & o) != 0)
133 
134 /* Logic to determine whether or not a field is selectable */
135 #define Field_Is_Selectable(f)     (((unsigned)((f)->opts) & O_SELECTABLE)==O_SELECTABLE)
136 #define Field_Is_Not_Selectable(f) (((unsigned)((f)->opts) & O_SELECTABLE)!=O_SELECTABLE)
137 
138 typedef struct typearg
139   {
140     struct typearg *left;
141     struct typearg *right;
142   }
143 TypeArgument;
144 
145 /* This is a dummy request code (normally invalid) to be used internally
146    with the form_driver() routine to position to the first active field
147    on the form
148 */
149 #define FIRST_ACTIVE_MAGIC (-291056)
150 
151 #define ALL_FORM_OPTS  (                \
152 			O_NL_OVERLOAD  |\
153 			O_BS_OVERLOAD   )
154 
155 #define STD_FIELD_OPTS (Field_Options)( \
156 			O_VISIBLE |\
157 			O_ACTIVE  |\
158 			O_PUBLIC  |\
159 			O_EDIT    |\
160 			O_WRAP    |\
161 			O_BLANK   |\
162 			O_AUTOSKIP|\
163 			O_NULLOK  |\
164 			O_PASSOK  |\
165 			O_STATIC)
166 
167 #define ALL_FIELD_OPTS (Field_Options)( \
168 			STD_FIELD_OPTS |\
169 			O_DYNAMIC_JUSTIFY |\
170 			O_NO_LEFT_STRIP |\
171 			O_EDGE_INSERT_STAY |\
172 			O_INPUT_LIMIT)
173 
174 #define C_BLANK ' '
175 #define is_blank(c) ((c)==C_BLANK)
176 
177 #define C_ZEROS '\0'
178 
179 extern FORM_EXPORT(TypeArgument *) _nc_Make_Argument (const FIELDTYPE*, va_list*, int*);
180 extern FORM_EXPORT(TypeArgument *) _nc_Copy_Argument (const FIELDTYPE*, const TypeArgument*, int*);
181 extern FORM_EXPORT(void) _nc_Free_Argument (const FIELDTYPE*, TypeArgument*);
182 extern FORM_EXPORT(bool) _nc_Copy_Type (FIELD*, FIELD const *);
183 extern FORM_EXPORT(void) _nc_Free_Type (FIELD *);
184 
185 extern FORM_EXPORT(int) _nc_Synchronize_Attributes (FIELD*);
186 extern FORM_EXPORT(int) _nc_Synchronize_Options (FIELD*, Field_Options);
187 extern FORM_EXPORT(int) _nc_Set_Form_Page (FORM*, int, FIELD*);
188 extern FORM_EXPORT(int) _nc_Refresh_Current_Field (FORM*);
189 extern FORM_EXPORT(FIELD *) _nc_First_Active_Field (FORM*);
190 extern FORM_EXPORT(bool) _nc_Internal_Validation (FORM*);
191 extern FORM_EXPORT(int) _nc_Set_Current_Field (FORM*, FIELD*);
192 extern FORM_EXPORT(int) _nc_Position_Form_Cursor (FORM*);
193 extern FORM_EXPORT(void) _nc_Unset_Current_Field(FORM *form);
194 
195 #if NCURSES_INTEROP_FUNCS
196 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_INTEGER(void);
197 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ALNUM(void);
198 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ALPHA(void);
199 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ENUM(void);
200 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_NUMERIC(void);
201 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_REGEXP(void);
202 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_IPV4(void);
203 
204 extern FORM_EXPORT(FIELDTYPE *)
205 _nc_generic_fieldtype(bool (*const field_check) (FORM*,
206 						 FIELD *,
207 						 const void *),
208 		      bool (*const char_check)  (int,
209 						 FORM*,
210 						 FIELD*,
211 						 const void *),
212 		      bool (*const next)(FORM*,FIELD*,const void*),
213 		      bool (*const prev)(FORM*,FIELD*,const void*),
214 		      void (*freecallback)(void*));
215 extern FORM_EXPORT(int) _nc_set_generic_fieldtype(FIELD*, FIELDTYPE*, int (*)(void**));
216 extern FORM_EXPORT(WINDOW*) _nc_form_cursor(const FORM* , int* , int* );
217 
218 #define INIT_FT_FUNC(func) {func}
219 #else
220 #define INIT_FT_FUNC(func) func
221 #endif
222 
223 extern FORM_EXPORT(void) _nc_get_fieldbuffer(FORM*, FIELD*, FIELD_CELL*);
224 
225 #if USE_WIDEC_SUPPORT
226 extern FORM_EXPORT(wchar_t *) _nc_Widen_String(char *, int *);
227 #endif
228 
229 #ifdef TRACE
230 
231 #define returnField(code)	TRACE_RETURN1(code,field)
232 #define returnFieldPtr(code)	TRACE_RETURN1(code,field_ptr)
233 #define returnForm(code)	TRACE_RETURN1(code,form)
234 #define returnFieldType(code)	TRACE_RETURN1(code,field_type)
235 #define returnFormHook(code)	TRACE_RETURN1(code,form_hook)
236 
237 extern FORM_EXPORT(FIELD **)	    _nc_retrace_field_ptr (FIELD **);
238 extern FORM_EXPORT(FIELD *)	    _nc_retrace_field (FIELD *);
239 extern FORM_EXPORT(FIELDTYPE *)  _nc_retrace_field_type (FIELDTYPE *);
240 extern FORM_EXPORT(FORM *)       _nc_retrace_form (FORM *);
241 extern FORM_EXPORT(Form_Hook)    _nc_retrace_form_hook (Form_Hook);
242 
243 #else /* !TRACE */
244 
245 #define returnFieldPtr(code)	return code
246 #define returnFieldType(code)	return code
247 #define returnField(code)	return code
248 #define returnForm(code)	return code
249 #define returnFormHook(code)	return code
250 
251 #endif /* TRACE/!TRACE */
252 
253 /*
254  * Use Check_CTYPE_Field() to simplify FIELDTYPE's that use only the ccheck()
255  * function.
256  */
257 #if USE_WIDEC_SUPPORT
258 #define Check_CTYPE_Field(result, buffer, width, ccheck) \
259   while (*buffer && *buffer == ' ') \
260     buffer++; \
261   if (*buffer) \
262     { \
263       bool blank = FALSE; \
264       int len; \
265       int n; \
266       wchar_t *list = _nc_Widen_String((char *)buffer, &len); \
267       if (list != 0) \
268 	{ \
269 	  result = TRUE; \
270 	  for (n = 0; n < len; ++n) \
271 	    { \
272 	      if (blank) \
273 		{ \
274 		  if (list[n] != ' ') \
275 		    { \
276 		      result = FALSE; \
277 		      break; \
278 		    } \
279 		} \
280 	      else if (list[n] == ' ') \
281 		{ \
282 		  blank = TRUE; \
283 		  result = (n + 1 >= width); \
284 		} \
285 	      else if (!ccheck(list[n], NULL)) \
286 		{ \
287 		  result = FALSE; \
288 		  break; \
289 		} \
290 	    } \
291 	  free(list); \
292 	} \
293     }
294 #else
295 #define Check_CTYPE_Field(result, buffer, width, ccheck) \
296   while (*buffer && *buffer == ' ') \
297     buffer++; \
298   if (*buffer) \
299     { \
300       unsigned char *s = buffer; \
301       int l = -1; \
302       while (*buffer && ccheck(*buffer, NULL)) \
303 	buffer++; \
304       l = (int)(buffer - s); \
305       while (*buffer && *buffer == ' ') \
306 	buffer++; \
307       result = ((*buffer || (l < width)) ? FALSE : TRUE); \
308     }
309 #endif
310 /* *INDENT-ON*/
311 
312 #endif /* FORM_PRIV_H */
313