2 * $Id: demo_forms.c,v 1.18 2005/10/08 21:54:20 tom Exp $
4 * Demonstrate a variety of functions from the form library.
5 * Thomas Dickey - 2003/4/26
26 form_request_by_name -
39 set_fieldtype_choice -
49 #include <test.priv.h>
53 #include <edit_field.h>
55 static int d_option = 0;
56 static int j_value = 0;
57 static int m_value = 0;
58 static int o_value = 0;
59 static char *t_value = 0;
62 make_label(int frow, int fcol, NCURSES_CONST char *label)
64 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
67 set_field_buffer(f, 0, label);
68 set_field_opts(f, (int) (field_opts(f) & ~O_ACTIVE));
74 * Define each field with an extra one, for reflecting "actual" text.
77 make_field(int frow, int fcol, int rows, int cols)
79 FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
82 set_field_back(f, A_UNDERLINE);
84 * If -j and -d options are combined, -j loses. It is documented in
85 * "Character User Interface Programming", page 12-15 that setting
86 * O_STATIC off makes the form library ignore justification.
88 set_field_just(f, j_value);
89 set_field_userptr(f, (void *) 0);
92 set_field_fore(f, COLOR_PAIR(2));
94 set_field_fore(f, A_BOLD);
97 * The field_opts_off() call dumps core with Solaris curses,
98 * but that is a known bug in Solaris' form library -TD
100 field_opts_off(f, O_STATIC);
101 set_max_field(f, m_value);
104 set_field_buffer(f, 0, t_value);
110 display_form(FORM * f)
115 scale_form(f, &rows, &cols);
118 * Put the form at the upper-left corner of the display, with just a box
121 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
123 set_form_sub(f, derwin(w, rows, cols, 1, 2));
128 if (post_form(f) != E_OK)
135 WINDOW *w = form_win(f);
136 WINDOW *s = form_sub(f);
146 show_insert_mode(bool insert_mode)
148 mvaddstr(5, 57, (insert_mode
149 ? "form_status: insert "
150 : "form_status: overlay"));
153 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
156 another_field(FORM * form, FIELD * field)
158 FIELD **f = form_fields(form);
162 for (n = 0; f[n] != 0; ++n) {
165 field_opts_on(result, O_SELECTABLE);
173 my_form_driver(FORM * form, int c)
175 static bool insert_mode = TRUE;
180 if (form_driver(form, REQ_VALIDATION) == E_OK)
187 if ((field = current_field(form)) != 0) {
188 set_current_field(form, another_field(form, field));
189 if (field_opts(field) & O_EDIT) {
190 field_opts_off(field, O_EDIT);
191 set_field_status(field, 0);
193 field_opts_on(field, O_EDIT);
195 set_current_field(form, field);
199 /* there should be a form_status() function, but there is none */
201 if (form_driver(form, REQ_INS_MODE) == E_OK) {
205 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
209 show_insert_mode(insert_mode);
220 show_current_field(WINDOW *win, FORM * form)
226 int field_rows, field_cols, field_max;
229 wbkgd(win, COLOR_PAIR(1));
232 wprintw(win, "Cursor: %d,%d", form->currow, form->curcol);
233 if (data_ahead(form))
234 waddstr(win, " ahead");
235 if (data_behind(form))
236 waddstr(win, " behind");
238 if ((field = current_field(form)) != 0) {
239 wprintw(win, "Field %d:", field_index(field));
240 if ((type = field_type(field)) != 0) {
241 if (type == TYPE_ALNUM)
242 waddstr(win, "ALNUM");
243 else if (type == TYPE_ALPHA)
244 waddstr(win, "ALPHA");
245 else if (type == TYPE_ENUM)
246 waddstr(win, "ENUM");
247 else if (type == TYPE_INTEGER)
248 waddstr(win, "INTEGER");
249 #ifdef NCURSES_VERSION
250 else if (type == TYPE_IPV4)
251 waddstr(win, "IPV4");
253 else if (type == TYPE_NUMERIC)
254 waddstr(win, "NUMERIC");
255 else if (type == TYPE_REGEXP)
256 waddstr(win, "REGEXP");
258 waddstr(win, "other");
261 if (field_opts(field) & O_EDIT)
262 waddstr(win, " editable");
264 waddstr(win, " readonly");
266 if (field_status(field))
267 waddstr(win, " modified");
269 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
271 wprintw(win, " size %dx%d (max %d)",
272 field_rows, field_cols, field_max);
275 for (nbuf = 0; nbuf <= 2; ++nbuf) {
276 if ((buffer = field_buffer(field, nbuf)) != 0) {
277 wprintw(win, "buffer %d:", nbuf);
278 wattrset(win, A_REVERSE);
279 waddstr(win, buffer);
280 wattroff(win, A_REVERSE);
301 mvaddstr(4, 57, "Forms Entry Test");
302 show_insert_mode(TRUE);
306 /* describe the form */
307 for (pg = 0; pg < 4; ++pg) {
309 sprintf(label, "Sample Form Page %d", pg + 1);
310 f[n++] = make_label(0, 15, label);
311 set_new_page(f[n - 1], TRUE);
315 f[n++] = make_label(2, 0, "Last Name");
316 f[n++] = make_field(3, 0, 1, 18);
317 set_field_type(f[n - 1], TYPE_ALPHA, 1);
319 f[n++] = make_label(2, 20, "First Name");
320 f[n++] = make_field(3, 20, 1, 12);
321 set_field_type(f[n - 1], TYPE_ALPHA, 1);
323 f[n++] = make_label(2, 34, "Middle Name");
324 f[n++] = make_field(3, 34, 1, 12);
325 set_field_type(f[n - 1], TYPE_ALPHA, 1);
328 f[n++] = make_label(2, 0, "Host Name");
329 f[n++] = make_field(3, 0, 1, 18);
330 set_field_type(f[n - 1], TYPE_ALPHA, 1);
332 #ifdef NCURSES_VERSION
333 f[n++] = make_label(2, 20, "IP Address");
334 f[n++] = make_field(3, 20, 1, 12);
335 set_field_type(f[n - 1], TYPE_IPV4, 1);
341 f[n++] = make_label(2, 0, "Four digits");
342 f[n++] = make_field(3, 0, 1, 18);
343 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
345 f[n++] = make_label(2, 20, "Numeric");
346 f[n++] = make_field(3, 20, 1, 12);
347 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
352 f[n++] = make_label(5, 0, "Comments");
353 f[n++] = make_field(6, 0, 4, 46);
356 f[n++] = (FIELD *) 0;
363 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
364 show_current_field(also, form);
367 switch (edit_field(form, &c)) {
370 case E_UNKNOWN_COMMAND:
371 finished = my_form_driver(form, c);
377 show_current_field(also, form);
383 for (c = 0; f[c] != 0; c++)
392 static const char *tbl[] =
394 "Usage: demo_forms [options]"
396 ," -d make fields dynamic"
397 ," -j value justify (1=left, 2=center, 3=right)"
398 ," -m value set maximum size of dynamic fields"
399 ," -o value specify number of offscreen rows in new_field()"
400 ," -t value specify text to fill fields initially"
403 for (j = 0; j < SIZEOF(tbl); ++j)
404 fprintf(stderr, "%s\n", tbl[j]);
409 main(int argc, char *argv[])
413 setlocale(LC_ALL, "");
415 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != EOF) {
421 j_value = atoi(optarg);
422 if (j_value < NO_JUSTIFICATION
423 || j_value > JUSTIFY_RIGHT)
427 m_value = atoi(optarg);
430 o_value = atoi(optarg);
445 nonl(); /* lets us read ^M's */
446 intrflush(stdscr, FALSE);
447 keypad(stdscr, TRUE);
451 init_pair(1, COLOR_WHITE, COLOR_BLUE);
452 init_pair(2, COLOR_GREEN, COLOR_BLACK);
460 ExitProgram(EXIT_SUCCESS);
466 printf("This program requires the curses form library\n");
467 ExitProgram(EXIT_FAILURE);