form.h revision 1.9 1 /* $NetBSD: form.h,v 1.9 2001/02/16 03:21:35 blymn Exp $ */
2
3 /*-
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn (at) baea.com.au, brett_lymn (at) 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 withough 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 0x100 /* must equal value of the first request */
74
75 #define REQ_NEXT_PAGE 0x100 /* next page in form */
76 #define REQ_PREV_PAGE 0x101 /* previous page in form */
77 #define REQ_FIRST_PAGE 0x102 /* goto first page in form */
78 #define REQ_LAST_PAGE 0x103 /* goto last page in form */
79
80 #define REQ_NEXT_FIELD 0x104 /* move to the next field */
81 #define REQ_PREV_FIELD 0x105 /* move to the previous field */
82 #define REQ_FIRST_FIELD 0x106 /* goto the first field */
83 #define REQ_LAST_FIELD 0x107 /* goto the last field */
84
85 #define REQ_SNEXT_FIELD 0x108 /* move to the next field in sorted order */
86 #define REQ_SPREV_FIELD 0x109 /* move to the prev field in sorted order */
87 #define REQ_SFIRST_FIELD 0x10a /* move to the first sorted field */
88 #define REQ_SLAST_FIELD 0x10b /* move to the last sorted field */
89
90 #define REQ_LEFT_FIELD 0x10c /* go left one field */
91 #define REQ_RIGHT_FIELD 0x10d /* go right one field */
92 #define REQ_UP_FIELD 0x10e /* go up one field */
93 #define REQ_DOWN_FIELD 0x10f /* go down one field */
94
95 #define REQ_NEXT_CHAR 0x110 /* move to the next char in field */
96 #define REQ_PREV_CHAR 0x111 /* move to the previous char in field */
97 #define REQ_NEXT_LINE 0x112 /* go to the next line in the field */
98 #define REQ_PREV_LINE 0x113 /* go to the previous line in the field */
99 #define REQ_NEXT_WORD 0x114 /* go to the next word in the field */
100 #define REQ_PREV_WORD 0x115 /* go to the previous word in the field */
101 #define REQ_BEG_FIELD 0x116 /* go to the beginning of the field */
102 #define REQ_END_FIELD 0x117 /* go to the end of the field */
103 #define REQ_BEG_LINE 0x118 /* go to the beginning of the line */
104 #define REQ_END_LINE 0x119 /* go to the end of the line */
105 #define REQ_LEFT_CHAR 0x11a /* move left in the field */
106 #define REQ_RIGHT_CHAR 0x11b /* move right in the field */
107 #define REQ_UP_CHAR 0x11c /* move up in the field */
108 #define REQ_DOWN_CHAR 0x11d /* move down in the field */
109
110 #define REQ_NEW_LINE 0x11e /* insert/overlay a new line */
111 #define REQ_INS_CHAR 0x11f /* insert a blank char at the cursor */
112 #define REQ_INS_LINE 0x120 /* insert a blank line at the cursor */
113
114 #define REQ_DEL_CHAR 0x121 /* delete the current character */
115 #define REQ_DEL_PREV 0x122 /* delete the character before the current */
116 #define REQ_DEL_LINE 0x123 /* delete the current line */
117 #define REQ_DEL_WORD 0x124 /* delete the word at the cursor */
118 #define REQ_CLR_EOL 0x125 /* clear to the end of the line */
119 #define REQ_CLR_EOF 0x126 /* clear to the end of the field */
120 #define REQ_CLR_FIELD 0x127 /* clear the field */
121
122 #define REQ_OVL_MODE 0x128 /* overlay mode */
123 #define REQ_INS_MODE 0x129 /* insert mode */
124
125 #define REQ_SCR_FLINE 0x12a /* scroll field forward one line */
126 #define REQ_SCR_BLINE 0x12b /* scroll field backward one line */
127 #define REQ_SCR_FPAGE 0x12c /* scroll field forward one page */
128 #define REQ_SCR_BPAGE 0x12d /* scroll field backward one page */
129 #define REQ_SCR_FHPAGE 0x12e /* scroll field forward half a page */
130 #define REQ_SCR_BHPAGE 0x12f /* scroll field backward half a page */
131
132 #define REQ_SCR_FCHAR 0x130 /* horizontal scroll forward a character */
133 #define REQ_SCR_BCHAR 0x131 /* horizontal scroll backward a character */
134 #define REQ_SCR_HFLINE 0x132 /* horizontal scroll forward a line */
135 #define REQ_SCR_HBLINE 0x133 /* horizontal scroll backward a line */
136 #define REQ_SCR_HFHALF 0x134 /* horizontal scroll forward half a line */
137 #define REQ_SCR_HBHALF 0x135 /* horizontal scroll backward half a line */
138
139 #define REQ_VALIDATION 0x136 /* validate the field */
140 #define REQ_PREV_CHOICE 0x137 /* display previous field choice */
141 #define REQ_NEXT_CHOICE 0x138 /* display next field choice */
142
143 #define REQ_MAX_COMMAND 0x138 /* must match the last driver command */
144
145 typedef struct _form_string {
146 size_t allocated;
147 unsigned int length;
148 char *string;
149 } FORM_STR;
150
151 typedef struct _form_field FIELD;
152 typedef struct _form_struct FORM;
153 typedef struct _form_fieldtype FIELDTYPE;
154
155 typedef struct _formi_page_struct _FORMI_PAGE_START;
156 typedef struct formi_type_link_struct _FORMI_TYPE_LINK;
157
158 typedef void (*Form_Hook)(FORM *);
159
160 /* definition of a field in the form */
161 struct _form_field {
162 unsigned int rows; /* rows in the field */
163 unsigned int cols; /* columns in the field */
164 unsigned int drows; /* dynamic rows */
165 unsigned int dcols; /* dynamic columns */
166 unsigned int max; /* maximum growth */
167 unsigned int form_row; /* starting row in the form subwindow */
168 unsigned int form_col; /* starting column in the form subwindow */
169 unsigned int nrows; /* number of off screen rows */
170 int index; /* index of this field in form fields array. */
171 int nbuf; /* number of buffers associated with this field */
172 int buf0_status; /* set to true if buffer 0 has changed. */
173 int justification; /* justification style of the field */
174 int overlay; /* set to true if field is in overlay mode */
175 unsigned int start_char; /* starting char in string (horiz scroll) */
176 unsigned int start_line; /* starting line in field (vert scroll) */
177 unsigned int hscroll; /* amount of horizontal scroll... */
178 unsigned int row_count; /* number of rows actually used in field */
179 unsigned int cursor_xpos; /* x pos of cursor in field */
180 unsigned int cursor_ypos; /* y pos of cursor in field */
181 short page_break; /* start of a new page on the form if 1 */
182 short page; /* number of the page this field is on */
183 chtype fore; /* character attributes for the foreground */
184 chtype back; /* character attributes for the background */
185 int pad; /* padding character */
186 Form_Options opts; /* options for the field */
187 FORM *parent; /* the form this field is bound to, if any */
188 FIELD *up; /* field above this one */
189 FIELD *down; /* field below this one */
190 FIELD *left; /* field to the left of this one */
191 FIELD *right; /* field to the right of this one */
192 void *userptr; /* user defined pointer. */
193 FIELD *link; /* used if fields are linked */
194 FIELDTYPE *type; /* type struct for the field */
195 CIRCLEQ_ENTRY(_form_field) glue; /* circle queue glue for sorting fields */
196 char *args; /* args for field type. */
197 FORM_STR *buffers; /* array of buffers for the field */
198 };
199
200 /* define the types of fields we can have */
201 extern FIELDTYPE *TYPE_ALNUM;
202 extern FIELDTYPE *TYPE_ALPHA;
203 extern FIELDTYPE *TYPE_ENUM;
204 extern FIELDTYPE *TYPE_INTEGER;
205 extern FIELDTYPE *TYPE_NUMERIC;
206 extern FIELDTYPE *TYPE_REGEXP;
207 extern FIELDTYPE *TYPE_IPV4;
208 extern FIELDTYPE *TYPE_IPV6;
209 extern FIELDTYPE *TYPE_USER;
210
211 /* definition of a field type. */
212 struct _form_fieldtype {
213 unsigned flags; /* status of the type */
214 unsigned refcount; /* in use if > 0 */
215 _FORMI_TYPE_LINK *link; /* set if this type is linked */
216
217 char * (*make_args)(va_list *); /* make the args for the type */
218 char * (*copy_args)(char *); /* copy the args for the type */
219 void (*free_args)(char *); /* free storage used by the args */
220 int (*field_check)(FIELD *, char *); /* field validation routine */
221 int (*char_check)(int, char *); /* char validation routine */
222 int (*next_choice)(FIELD *, char *); /* function to select next
223 choice */
224 int (*prev_choice)(FIELD *, char *); /* function to select prev
225 choice */
226 };
227
228 /*definition of a form */
229
230 struct _form_struct {
231 int in_init; /* true if performing a init or term function */
232 int posted; /* the form is posted */
233 int wrap; /* wrap from last field to first field if true */
234 WINDOW *win; /* window for the form */
235 WINDOW *subwin; /* subwindow for the form */
236 void *userptr; /* user defined pointer */
237 Form_Options opts; /* options for the form */
238 Form_Hook form_init; /* function called when form posted and
239 after page change */
240 Form_Hook form_term; /* function called when form is unposted and
241 before page change */
242 Form_Hook field_init; /* function called when form posted and after
243 current field changes */
244 Form_Hook field_term; /* function called when form unposted and
245 before current field changes */
246 int field_count; /* number of fields attached */
247 int cur_field; /* current field */
248 int page; /* current page of form */
249 int max_page; /* number of pages in the form */
250 int subwin_created; /* libform made the window */
251 _FORMI_PAGE_START *page_starts; /* dynamic array of fields that start
252 the pages */
253 CIRCLEQ_HEAD(_formi_sort_head, _form_field) sorted_fields; /* sorted field
254 list */
255 FIELD **fields; /* array of fields attached to this form. */
256 };
257
258 /* Public function prototypes. */
259 __BEGIN_DECLS
260
261 FIELD *current_field(FORM *);
262 int data_ahead(FORM *);
263 int data_behind(FORM *);
264 FIELD *dup_field(FIELD *, int, int);
265 int dynamic_field_info(FIELD *, int *, int *, int *);
266 char *field_arg(FIELD *);
267 chtype field_back(FIELD *);
268 char *field_buffer(FIELD *, int);
269 int field_count(FORM *);
270 chtype field_fore(FIELD *);
271 int field_index(FIELD *);
272 int field_info __P((FIELD *, int *, int *, int *, int *,
273 int *, int *));
274 Form_Hook field_init(FORM *);
275 int field_just(FIELD *);
276 Form_Options field_opts(FIELD *);
277 int field_opts_off(FIELD *, Form_Options);
278 int field_opts_on(FIELD *, Form_Options);
279 int field_pad(FIELD *);
280 int field_status(FIELD *);
281 Form_Hook field_term(FORM *);
282 FIELDTYPE *field_type(FIELD *);
283 void *field_userptr(FIELD *);
284 int form_driver(FORM *, int);
285 FIELD **form_fields(FORM *);
286 Form_Hook form_init(FORM *);
287 int form_max_page(FORM *);
288 Form_Options form_opts(FORM *);
289 int form_opts_off(FORM *, Form_Options);
290 int form_opts_on(FORM *, Form_Options);
291 int form_page(FORM *);
292 WINDOW *form_sub(FORM *);
293 Form_Hook form_term(FORM *);
294 void *form_userptr(FORM *);
295 WINDOW *form_win(FORM *);
296 int free_field(FIELD *);
297 int free_fieldtype(FIELDTYPE *);
298 int free_form(FORM *);
299 FIELD *link_field(FIELD *, int, int);
300 FIELDTYPE *link_fieldtype(FIELDTYPE *, FIELDTYPE *);
301 int move_field(FIELD *, int, int);
302 FIELD *new_field(int, int, int, int, int, int);
303 FIELDTYPE *new_fieldtype(int (* field_check)(FIELD *, char *),
304 int (* char_check)(int, char *));
305 FORM *new_form(FIELD **);
306 int new_page(FIELD *);
307 int pos_form_cursor(FORM *);
308 int post_form(FORM *);
309 int scale_form(FORM *, int *, int *);
310 int set_current_field(FORM *, FIELD *);
311 int set_field_back(FIELD *, chtype);
312 int set_field_buffer(FIELD *, int, char *);
313 int set_field_fore(FIELD *, chtype);
314 int set_field_init(FORM *, Form_Hook);
315 int set_field_just(FIELD *, int);
316 int set_field_opts(FIELD *, Form_Options);
317 int set_field_pad(FIELD *, int);
318 int set_field_status(FIELD *, int);
319 int set_field_term(FORM *, Form_Hook);
320 int set_field_type(FIELD *, FIELDTYPE *, ...);
321 int set_field_userptr(FIELD *, void *);
322 int set_fieldtype_arg(FIELDTYPE *, char *(*)(va_list *),
323 char *(*)(char *),
324 void (*)(char *));
325 int set_fieldtype_choice(FIELDTYPE *, int (*)(FIELD *, char *),
326 int (*)(FIELD *, char *));
327 int set_form_fields(FORM *, FIELD **);
328 int set_form_init(FORM *, Form_Hook);
329 int set_form_opts(FORM *, Form_Options);
330 int set_form_page(FORM *, int);
331 int set_form_sub(FORM *, WINDOW *);
332 int set_form_term(FORM *, Form_Hook);
333 int set_form_userptr(FORM *, void *);
334 int set_form_win(FORM *, WINDOW *);
335 int set_max_field(FIELD *, int);
336 int set_new_page(FIELD *, int);
337 int unpost_form(FORM *);
338
339 __END_DECLS
340
341 #endif /* FORM_H */
342