xref: /netbsd/lib/libform/form.h (revision c4a72b64)
1 /*	$NetBSD: form.h,v 1.17 2002/08/09 14:15:13 blymn Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998-1999 Brett Lymn
5  *               (blymn@baea.com.au, brett_lymn@yahoo.com.au)
6  * All rights reserved.
7  *
8  * This code has been donated to The NetBSD Foundation by the Author.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *
30  */
31 
32 #ifndef FORM_H
33 #define FORM_H 1
34 #include <sys/queue.h>
35 #include <stdarg.h>
36 #include <curses.h>
37 #include <eti.h>
38 
39 /* Define the types of field justification that can be used. */
40 #define NO_JUSTIFICATION  (0)
41 #define JUSTIFY_RIGHT     (1)
42 #define JUSTIFY_LEFT      (2)
43 #define JUSTIFY_CENTER    (3)
44 
45 /* Define the max and min valid justification styles for range checking */
46 #define MIN_JUST_STYLE    NO_JUSTIFICATION
47 #define MAX_JUST_STYLE    JUSTIFY_CENTER
48 
49 /* Options for the fields */
50 typedef unsigned int Form_Options;
51 
52 /* form options */
53 #define O_BS_OVERLOAD (0x001)
54 #define O_NL_OVERLOAD (0x002)
55 
56 /* field options */
57 #define O_VISIBLE  (0x001)  /* Field is visible */
58 #define O_ACTIVE   (0x002)  /* Field is active in the form */
59 #define O_PUBLIC   (0x004)  /* The contents entered into the field is echoed */
60 #define O_EDIT     (0x008)  /* Can edit the field */
61 #define O_WRAP     (0x010)  /* The field contents can line wrap */
62 #define O_BLANK    (0x020)  /* Blank the field on modification */
63 #define O_AUTOSKIP (0x040)  /* Skip to next field when current is full */
64 #define O_NULLOK   (0x080)  /* Field is allowed to contain no data */
65 #define O_STATIC   (0x100)  /* Field is not dynamic */
66 #define O_PASSOK   (0x200)  /* An umodified field is OK */
67 
68 /*
69  * Form driver requests - be VERY careful about changing the ordering
70  * of the requests below.  The form driver code depends on a particular
71  * order for the requests.
72  */
73 #define REQ_MIN_REQUEST   (KEY_MAX + 0x101) /* must equal value of the
74                                                first request */
75 
76 #define REQ_NEXT_PAGE     (KEY_MAX + 0x101) /* next page in form */
77 #define REQ_PREV_PAGE     (KEY_MAX + 0x102) /* previous page in form */
78 #define REQ_FIRST_PAGE    (KEY_MAX + 0x103) /* goto first page in form */
79 #define REQ_LAST_PAGE     (KEY_MAX + 0x104) /* goto last page in form */
80 
81 #define REQ_NEXT_FIELD    (KEY_MAX + 0x105) /* move to the next field */
82 #define REQ_PREV_FIELD    (KEY_MAX + 0x106) /* move to the previous field */
83 #define REQ_FIRST_FIELD   (KEY_MAX + 0x107) /* goto the first field */
84 #define REQ_LAST_FIELD    (KEY_MAX + 0x108) /* goto the last field */
85 
86 #define REQ_SNEXT_FIELD   (KEY_MAX + 0x109) /* move to the next field
87                                                in sorted order */
88 #define REQ_SPREV_FIELD   (KEY_MAX + 0x10a) /* move to the prev field
89                                                in sorted order */
90 #define REQ_SFIRST_FIELD  (KEY_MAX + 0x10b) /* move to the first
91                                                sorted field */
92 #define REQ_SLAST_FIELD   (KEY_MAX + 0x10c) /* move to the last sorted
93                                                field */
94 
95 #define REQ_LEFT_FIELD    (KEY_MAX + 0x10d) /* go left one field */
96 #define REQ_RIGHT_FIELD   (KEY_MAX + 0x10e) /* go right one field */
97 #define REQ_UP_FIELD      (KEY_MAX + 0x10f) /* go up one field */
98 #define REQ_DOWN_FIELD    (KEY_MAX + 0x110) /* go down one field */
99 
100 #define REQ_NEXT_CHAR     (KEY_MAX + 0x111) /* move to the next char
101                                                in field */
102 #define REQ_PREV_CHAR     (KEY_MAX + 0x112) /* move to the previous
103                                                char in field */
104 #define REQ_NEXT_LINE     (KEY_MAX + 0x113) /* go to the next line in
105                                                the field */
106 #define REQ_PREV_LINE     (KEY_MAX + 0x114) /* go to the previous line
107                                                in the field */
108 #define REQ_NEXT_WORD     (KEY_MAX + 0x115) /* go to the next word in
109                                                the field */
110 #define REQ_PREV_WORD     (KEY_MAX + 0x116) /* go to the previous word
111                                                in the field */
112 #define REQ_BEG_FIELD     (KEY_MAX + 0x117) /* go to the beginning of
113                                                the field */
114 #define REQ_END_FIELD     (KEY_MAX + 0x118) /* go to the end of the field */
115 #define REQ_BEG_LINE      (KEY_MAX + 0x119) /* go to the beginning of
116                                                the line */
117 #define REQ_END_LINE      (KEY_MAX + 0x11a) /* go to the end of the
118                                                line */
119 #define REQ_LEFT_CHAR     (KEY_MAX + 0x11b) /* move left in the field */
120 #define REQ_RIGHT_CHAR    (KEY_MAX + 0x11c) /* move right in the field */
121 #define REQ_UP_CHAR       (KEY_MAX + 0x11d) /* move up in the field */
122 #define REQ_DOWN_CHAR     (KEY_MAX + 0x11e) /* move down in the field */
123 
124 #define REQ_NEW_LINE      (KEY_MAX + 0x11f) /* insert/overlay a new line */
125 #define REQ_INS_CHAR      (KEY_MAX + 0x120) /* insert a blank char at
126                                                the cursor */
127 #define REQ_INS_LINE      (KEY_MAX + 0x121) /* insert a blank line at
128                                                the cursor */
129 
130 #define REQ_DEL_CHAR      (KEY_MAX + 0x122) /* delete the current character */
131 #define REQ_DEL_PREV      (KEY_MAX + 0x123) /* delete the character
132                                                before the current */
133 #define REQ_DEL_LINE      (KEY_MAX + 0x124) /* delete the current line */
134 #define REQ_DEL_WORD      (KEY_MAX + 0x125) /* delete the word at the cursor */
135 #define REQ_CLR_EOL       (KEY_MAX + 0x126) /* clear to the end of the line */
136 #define REQ_CLR_EOF       (KEY_MAX + 0x127) /* clear to the end of the field */
137 #define REQ_CLR_FIELD     (KEY_MAX + 0x128) /* clear the field */
138 
139 #define REQ_OVL_MODE      (KEY_MAX + 0x129) /* overlay mode */
140 #define REQ_INS_MODE      (KEY_MAX + 0x12a) /* insert mode */
141 
142 #define REQ_SCR_FLINE     (KEY_MAX + 0x12b) /* scroll field forward one line */
143 #define REQ_SCR_BLINE     (KEY_MAX + 0x12c) /* scroll field backward
144                                                one line */
145 #define REQ_SCR_FPAGE     (KEY_MAX + 0x12d) /* scroll field forward one page */
146 #define REQ_SCR_BPAGE     (KEY_MAX + 0x12e) /* scroll field backward
147                                                one page */
148 #define REQ_SCR_FHPAGE    (KEY_MAX + 0x12f) /* scroll field forward
149                                                half a page */
150 #define REQ_SCR_BHPAGE    (KEY_MAX + 0x130) /* scroll field backward
151                                                half a page */
152 
153 #define REQ_SCR_FCHAR     (KEY_MAX + 0x131) /* horizontal scroll
154                                                forward a character */
155 #define REQ_SCR_BCHAR     (KEY_MAX + 0x132) /* horizontal scroll
156                                                backward a character */
157 #define REQ_SCR_HFLINE    (KEY_MAX + 0x133) /* horizontal scroll
158                                                forward a line */
159 #define REQ_SCR_HBLINE    (KEY_MAX + 0x134) /* horizontal scroll
160                                                backward a line */
161 #define REQ_SCR_HFHALF    (KEY_MAX + 0x135) /* horizontal scroll
162                                                forward half a line */
163 #define REQ_SCR_HBHALF    (KEY_MAX + 0x136) /* horizontal scroll
164                                                backward half a line */
165 
166 #define REQ_VALIDATION    (KEY_MAX + 0x137) /* validate the field */
167 #define REQ_PREV_CHOICE   (KEY_MAX + 0x138) /* display previous field choice */
168 #define REQ_NEXT_CHOICE   (KEY_MAX + 0x139) /* display next field choice */
169 
170 #define REQ_MAX_COMMAND   (KEY_MAX + 0x139) /* must match the last
171                                                driver command */
172 
173 typedef struct _form_string {
174 	size_t allocated;
175 	unsigned int length;
176 	char *string;
177 } FORM_STR;
178 
179 typedef struct _form_field FIELD;
180 typedef struct _form_struct FORM;
181 typedef struct _form_fieldtype FIELDTYPE;
182 
183 typedef struct _formi_page_struct _FORMI_PAGE_START;
184 typedef struct formi_type_link_struct _FORMI_TYPE_LINK;
185 typedef struct _formi_field_lines _FORMI_FIELD_LINES;
186 
187 
188 typedef void (*Form_Hook)(FORM *);
189 
190 /* definition of a field in the form */
191 struct _form_field {
192 	unsigned int rows; /* rows in the field */
193 	unsigned int cols; /* columns in the field */
194 	unsigned int drows; /* dynamic rows */
195 	unsigned int dcols; /* dynamic columns */
196 	unsigned int max; /* maximum growth */
197 	unsigned int form_row; /* starting row in the form subwindow */
198 	unsigned int form_col; /* starting column in the form subwindow */
199 	unsigned int nrows; /* number of off screen rows */
200 	int index; /* index of this field in form fields array. */
201 	int nbuf; /* number of buffers associated with this field */
202 	int buf0_status; /* set to true if buffer 0 has changed. */
203 	int justification; /* justification style of the field */
204 	int overlay; /* set to true if field is in overlay mode */
205 	unsigned int start_char; /* starting char in string (horiz scroll) */
206 	unsigned int start_line; /* starting line in field (vert scroll) */
207 	unsigned int row_count; /* number of rows actually used in field */
208 	unsigned int row_xpos; /* char offset of cursor in field, not same
209 				  as cursor_xpos due to tab expansion */
210 	unsigned int cursor_xpos; /* x pos of cursor in field */
211 	unsigned int cursor_ypos; /* y pos of cursor in field */
212 	short page_break; /* start of a new page on the form if 1 */
213 	short page; /* number of the page this field is on */
214 	chtype fore; /* character attributes for the foreground */
215 	chtype back; /* character attributes for the background */
216 	int pad; /* padding character */
217 	Form_Options opts; /* options for the field */
218 	FORM *parent; /* the form this field is bound to, if any */
219 	FIELD *up; /* field above this one */
220 	FIELD *down; /* field below this one */
221 	FIELD *left; /* field to the left of this one */
222 	FIELD *right; /* field to the right of this one */
223 	void *userptr;  /* user defined pointer. */
224 	FIELD *link; /* used if fields are linked */
225 	FIELDTYPE *type; /* type struct for the field */
226 	CIRCLEQ_ENTRY(_form_field) glue; /* circle queue glue for sorting fields */
227 	char *args; /* args for field type. */
228 	unsigned int lines_alloced; /* number of slots allocated in lines
229 				       array */
230 	_FORMI_FIELD_LINES *lines; /* array of the starts and ends of lines */
231 	FORM_STR *buffers; /* array of buffers for the field */
232 };
233 
234 /* define the types of fields we can have */
235 extern FIELDTYPE *TYPE_ALNUM;
236 extern FIELDTYPE *TYPE_ALPHA;
237 extern FIELDTYPE *TYPE_ENUM;
238 extern FIELDTYPE *TYPE_INTEGER;
239 extern FIELDTYPE *TYPE_NUMERIC;
240 extern FIELDTYPE *TYPE_REGEXP;
241 extern FIELDTYPE *TYPE_IPV4;
242 extern FIELDTYPE *TYPE_IPV6;
243 extern FIELDTYPE *TYPE_USER;
244 
245 /* definition of a field type. */
246 struct _form_fieldtype {
247 	unsigned flags; /* status of the type */
248 	unsigned refcount; /* in use if > 0 */
249 	_FORMI_TYPE_LINK *link; /* set if this type is linked */
250 
251 	char * (*make_args)(va_list *); /* make the args for the type */
252 	char * (*copy_args)(char *); /* copy the args for the type */
253 	void (*free_args)(char *); /* free storage used by the args */
254 	int (*field_check)(FIELD *, char *); /* field validation routine */
255 	int (*char_check)(int, char *); /* char validation routine */
256 	int (*next_choice)(FIELD *, char *); /* function to select next
257 						choice */
258 	int (*prev_choice)(FIELD *, char *); /* function to select prev
259 						choice */
260 };
261 
262 /*definition of a form */
263 
264 struct _form_struct {
265 	int in_init; /* true if performing a init or term function */
266 	int posted; /* the form is posted */
267 	int wrap; /* wrap from last field to first field if true */
268 	WINDOW *win; /* window for the form */
269 	WINDOW *subwin; /* subwindow for the form */
270 	WINDOW *scrwin; /* this is the window to use for output */
271 	void *userptr; /* user defined pointer */
272 	Form_Options opts; /* options for the form */
273 	Form_Hook form_init; /* function called when form posted and
274 				after page change */
275 	Form_Hook form_term; /* function called when form is unposted and
276 				before page change */
277 	Form_Hook field_init; /* function called when form posted and after
278 				 current field changes */
279 	Form_Hook field_term; /* function called when form unposted and
280 				 before current field changes */
281 	int field_count; /* number of fields attached */
282 	int cur_field; /* current field */
283 	int page; /* current page of form */
284 	int max_page; /* number of pages in the form */
285 	_FORMI_PAGE_START *page_starts; /* dynamic array of fields that start
286 					   the pages */
287 	CIRCLEQ_HEAD(_formi_sort_head, _form_field) sorted_fields; /* sorted field
288 								list */
289 	FIELD **fields; /* array of fields attached to this form. */
290 };
291 
292 /* Public function prototypes. */
293 __BEGIN_DECLS
294 
295 FIELD       *current_field(FORM *);
296 int          data_ahead(FORM *);
297 int          data_behind(FORM *);
298 FIELD       *dup_field(FIELD *, int, int);
299 int          dynamic_field_info(FIELD *, int *, int *, int *);
300 char        *field_arg(FIELD *);
301 chtype       field_back(FIELD *);
302 char        *field_buffer(FIELD *, int);
303 int          field_count(FORM *);
304 chtype       field_fore(FIELD *);
305 int          field_index(FIELD *);
306 int          field_info(FIELD *, int *, int *, int *, int *, int *, int *);
307 Form_Hook    field_init(FORM *);
308 int          field_just(FIELD *);
309 Form_Options field_opts(FIELD *);
310 int          field_opts_off(FIELD *, Form_Options);
311 int          field_opts_on(FIELD *, Form_Options);
312 int          field_pad(FIELD *);
313 int          field_status(FIELD *);
314 Form_Hook    field_term(FORM *);
315 FIELDTYPE   *field_type(FIELD *);
316 void        *field_userptr(FIELD *);
317 int          form_driver(FORM *, int);
318 FIELD      **form_fields(FORM *);
319 Form_Hook    form_init(FORM *);
320 int          form_max_page(FORM *);
321 Form_Options form_opts(FORM *);
322 int          form_opts_off(FORM *, Form_Options);
323 int          form_opts_on(FORM *, Form_Options);
324 int          form_page(FORM *);
325 WINDOW      *form_sub(FORM *);
326 Form_Hook    form_term(FORM *);
327 void        *form_userptr(FORM *);
328 WINDOW      *form_win(FORM *);
329 int          free_field(FIELD *);
330 int          free_fieldtype(FIELDTYPE *);
331 int          free_form(FORM *);
332 FIELD       *link_field(FIELD *, int, int);
333 FIELDTYPE   *link_fieldtype(FIELDTYPE *, FIELDTYPE *);
334 int          move_field(FIELD *, int, int);
335 FIELD       *new_field(int, int, int, int, int, int);
336 FIELDTYPE   *new_fieldtype(int (* field_check)(FIELD *, char *),
337 			   int (* char_check)(int, char *));
338 FORM        *new_form(FIELD **);
339 int          new_page(FIELD *);
340 int          pos_form_cursor(FORM *);
341 int          post_form(FORM *);
342 int          scale_form(FORM *, int *, int *);
343 int          set_current_field(FORM *, FIELD *);
344 int          set_field_back(FIELD *, chtype);
345 int          set_field_buffer(FIELD *, int, char *);
346 int          set_field_fore(FIELD *, chtype);
347 int          set_field_init(FORM *, Form_Hook);
348 int          set_field_just(FIELD *, int);
349 int          set_field_opts(FIELD *, Form_Options);
350 int          set_field_pad(FIELD *, int);
351 int          set_field_printf(FIELD *, int, char *, ...)
352 				__attribute__((__format__(__printf__, 3, 4)));
353 int          set_field_status(FIELD *, int);
354 int          set_field_term(FORM *, Form_Hook);
355 int          set_field_type(FIELD *, FIELDTYPE *, ...);
356 int          set_field_userptr(FIELD *, void *);
357 int          set_fieldtype_arg(FIELDTYPE *, char *(*)(va_list *),
358 			       char *(*)(char *),
359 			       void (*)(char *));
360 int          set_fieldtype_choice(FIELDTYPE *, int (*)(FIELD *, char *),
361 				  int (*)(FIELD *, char *));
362 int          set_form_fields(FORM *, FIELD **);
363 int          set_form_init(FORM *, Form_Hook);
364 int          set_form_opts(FORM *, Form_Options);
365 int          set_form_page(FORM *, int);
366 int          set_form_sub(FORM *, WINDOW *);
367 int          set_form_term(FORM *, Form_Hook);
368 int          set_form_userptr(FORM *, void *);
369 int          set_form_win(FORM *, WINDOW *);
370 int          set_max_field(FIELD *, int);
371 int          set_new_page(FIELD *, int);
372 int          unpost_form(FORM *);
373 
374 __END_DECLS
375 
376 #endif /* FORM_H */
377