xref: /openbsd/lib/libform/form.priv.h (revision 73471bf0)
1 /*	$OpenBSD: form.priv.h,v 1.8 2015/01/23 22:48:51 krw Exp $	*/
2 /****************************************************************************
3  * Copyright (c) 1998-2006,2008 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 1.8 2015/01/23 22:48:51 krw Exp $ */
35 
36 #ifndef FORM_PRIV_H
37 #define FORM_PRIV_H 1
38 
39 #include "curses.priv.h"
40 #include "mf_common.h"
41 
42 #if USE_WIDEC_SUPPORT
43 #if HAVE_WCTYPE_H
44 #include <wctype.h>
45 #endif
46 
47 #ifndef MB_LEN_MAX
48 #define MB_LEN_MAX 8 /* should be >= MB_CUR_MAX, but that may be a function */
49 #endif
50 
51 #define FIELD_CELL NCURSES_CH_T
52 
53 #define NCURSES_FIELD_INTERNALS char** expanded; WINDOW *working;
54 #define NCURSES_FIELD_EXTENSION , (char **)0, (WINDOW *)0
55 
56 #else
57 
58 #define FIELD_CELL char
59 
60 #define NCURSES_FIELD_EXTENSION /* nothing */
61 
62 #endif
63 
64 #include "form.h"
65 
66 /* form  status values */
67 #define _OVLMODE         (0x04U) /* Form is in overlay mode                */
68 #define _WINDOW_MODIFIED (0x10U) /* Current field window has been modified */
69 #define _FCHECK_REQUIRED (0x20U) /* Current field needs validation         */
70 
71 /* field status values */
72 #define _CHANGED         (0x01U) /* Field has been changed                 */
73 #define _NEWTOP          (0x02U) /* Vertical scrolling occurred            */
74 #define _NEWPAGE         (0x04U) /* field begins new page of form          */
75 #define _MAY_GROW        (0x08U) /* dynamic field may still grow           */
76 
77 /* fieldtype status values */
78 #define _LINKED_TYPE     (0x01U) /* Type is a linked type                  */
79 #define _HAS_ARGS        (0x02U) /* Type has arguments                     */
80 #define _HAS_CHOICE      (0x04U) /* Type has choice methods                */
81 #define _RESIDENT        (0x08U) /* Type is built-in                       */
82 
83 /* This are the field options required to be a selectable field in field
84    navigation requests */
85 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
86 
87 /* If form is NULL replace form argument by default-form */
88 #define Normalize_Form(form) \
89   ((form) = (form != 0) ? (form) : _nc_Default_Form)
90 
91 /* If field is NULL replace field argument by default-field */
92 #define Normalize_Field(field) \
93   ((field) = (field != 0) ? (field) : _nc_Default_Field)
94 
95 /* Retrieve forms window */
96 #define Get_Form_Window(form) \
97   ((form)->sub?(form)->sub:((form)->win?(form)->win:stdscr))
98 
99 /* Calculate the size for a single buffer for this field */
100 #define Buffer_Length(field) ((field)->drows * (field)->dcols)
101 
102 /* Calculate the total size of all buffers for this field */
103 #define Total_Buffer_Size(field) \
104    ( (Buffer_Length(field) + 1) * (1+(field)->nbuf) * sizeof(FIELD_CELL) )
105 
106 /* Logic to determine whether or not a field is single lined */
107 #define Single_Line_Field(field) \
108    (((field)->rows + (field)->nrow) == 1)
109 
110 /* Logic to determine whether or not a field is selectable */
111 #define Field_Is_Selectable(f)     (((unsigned)((f)->opts) & O_SELECTABLE)==O_SELECTABLE)
112 #define Field_Is_Not_Selectable(f) (((unsigned)((f)->opts) & O_SELECTABLE)!=O_SELECTABLE)
113 
114 typedef struct typearg
115   {
116     struct typearg *left;
117     struct typearg *right;
118   }
119 TypeArgument;
120 
121 /* This is a dummy request code (normally invalid) to be used internally
122    with the form_driver() routine to position to the first active field
123    on the form
124 */
125 #define FIRST_ACTIVE_MAGIC (-291056)
126 
127 #define ALL_FORM_OPTS  (                \
128 			O_NL_OVERLOAD  |\
129 			O_BS_OVERLOAD   )
130 
131 #define ALL_FIELD_OPTS (Field_Options)( \
132 			O_VISIBLE |\
133 			O_ACTIVE  |\
134 			O_PUBLIC  |\
135 			O_EDIT    |\
136 			O_WRAP    |\
137 			O_BLANK   |\
138 			O_AUTOSKIP|\
139 			O_NULLOK  |\
140 			O_PASSOK  |\
141 			O_STATIC   )
142 
143 #define C_BLANK ' '
144 #define is_blank(c) ((c)==C_BLANK)
145 
146 #define C_ZEROS '\0'
147 
148 extern NCURSES_EXPORT_VAR(const FIELDTYPE *) _nc_Default_FieldType;
149 
150 extern NCURSES_EXPORT(TypeArgument *) _nc_Make_Argument (const FIELDTYPE*, va_list*, int*);
151 extern NCURSES_EXPORT(TypeArgument *) _nc_Copy_Argument (const FIELDTYPE*, const TypeArgument*, int*);
152 extern NCURSES_EXPORT(void) _nc_Free_Argument (const FIELDTYPE*, TypeArgument*);
153 extern NCURSES_EXPORT(bool) _nc_Copy_Type (FIELD*, FIELD const *);
154 extern NCURSES_EXPORT(void) _nc_Free_Type (FIELD *);
155 
156 extern NCURSES_EXPORT(int) _nc_Synchronize_Attributes (FIELD*);
157 extern NCURSES_EXPORT(int) _nc_Synchronize_Options (FIELD*, Field_Options);
158 extern NCURSES_EXPORT(int) _nc_Set_Form_Page (FORM*, int, FIELD*);
159 extern NCURSES_EXPORT(int) _nc_Refresh_Current_Field (FORM*);
160 extern NCURSES_EXPORT(FIELD *) _nc_First_Active_Field (FORM*);
161 extern NCURSES_EXPORT(bool) _nc_Internal_Validation (FORM*);
162 extern NCURSES_EXPORT(int) _nc_Set_Current_Field (FORM*, FIELD*);
163 extern NCURSES_EXPORT(int) _nc_Position_Form_Cursor (FORM*);
164 
165 #if USE_WIDEC_SUPPORT
166 extern NCURSES_EXPORT(wchar_t *) _nc_Widen_String(char *, int *);
167 #endif
168 
169 #ifdef TRACE
170 
171 #define returnField(code)	TRACE_RETURN(code,field)
172 #define returnFieldPtr(code)	TRACE_RETURN(code,field_ptr)
173 #define returnForm(code)	TRACE_RETURN(code,form)
174 #define returnFieldType(code)	TRACE_RETURN(code,field_type)
175 #define returnFormHook(code)	TRACE_RETURN(code,form_hook)
176 
177 extern NCURSES_EXPORT(FIELD **)	    _nc_retrace_field_ptr (FIELD **);
178 extern NCURSES_EXPORT(FIELD *)	    _nc_retrace_field (FIELD *);
179 extern NCURSES_EXPORT(FIELDTYPE *)  _nc_retrace_field_type (FIELDTYPE *);
180 extern NCURSES_EXPORT(FORM *)  _nc_retrace_form (FORM *);
181 extern NCURSES_EXPORT(Form_Hook)  _nc_retrace_form_hook (Form_Hook);
182 
183 #else /* !TRACE */
184 
185 #define returnFieldPtr(code)	return code
186 #define returnFieldType(code)	return code
187 #define returnField(code)	return code
188 #define returnForm(code)	return code
189 #define returnFormHook(code)	return code
190 
191 #endif /* TRACE/!TRACE */
192 
193 /*
194  * Use Check_CTYPE_Field() to simplify FIELDTYPE's that use only the ccheck()
195  * function.
196  */
197 #if USE_WIDEC_SUPPORT
198 #define Check_CTYPE_Field(result, buffer, width, ccheck) \
199   while (*buffer && *buffer == ' ') \
200     buffer++; \
201   if (*buffer) \
202     { \
203       bool blank = FALSE; \
204       int len; \
205       int n; \
206       wchar_t *list = _nc_Widen_String((char *)buffer, &len); \
207       if (list != 0) \
208 	{ \
209 	  result = TRUE; \
210 	  for (n = 0; n < len; ++n) \
211 	    { \
212 	      if (blank) \
213 		{ \
214 		  if (list[n] != ' ') \
215 		    { \
216 		      result = FALSE; \
217 		      break; \
218 		    } \
219 		} \
220 	      else if (list[n] == ' ') \
221 		{ \
222 		  blank = TRUE; \
223 		  result = (n + 1 >= width); \
224 		} \
225 	      else if (!ccheck(list[n], NULL)) \
226 		{ \
227 		  result = FALSE; \
228 		  break; \
229 		} \
230 	    } \
231 	  free(list); \
232 	} \
233     }
234 #else
235 #define Check_CTYPE_Field(result, buffer, width, ccheck) \
236   while (*buffer && *buffer == ' ') \
237     buffer++; \
238   if (*buffer) \
239     { \
240       unsigned char *s = buffer; \
241       int l = -1; \
242       while (*buffer && ccheck(*buffer, NULL)) \
243 	buffer++; \
244       l = (int)(buffer - s); \
245       while (*buffer && *buffer == ' ') \
246 	buffer++; \
247       result = ((*buffer || (l < width)) ? FALSE : TRUE); \
248     }
249 #endif
250 
251 #endif /* FORM_PRIV_H */
252