1 /****************************************************************************
2 * Copyright (c) 2003-2014,2016 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: demo_forms.c,v 1.52 2016/09/10 21:14:55 tom Exp $
31 * Demonstrate a variety of functions from the form library.
32 * Thomas Dickey - 2003/4/26
45 form_request_by_name -
57 set_fieldtype_choice -
67 #include <test.priv.h>
71 #include <edit_field.h>
78 static MY_DATA *my_data;
80 static int d_option = 0;
81 static int j_value = 0;
82 static int m_value = 0;
83 static int o_value = 0;
84 static char *t_value = 0;
90 ExitProgram(EXIT_FAILURE);
96 size_t have = strlen(value);
97 while (have != 0 && (value[have - 1] == '\n' || value[have - 1] == '\r')) {
103 trimmed(const char *value)
105 int result = (int) strlen(value);
106 while (result > 0 && isspace(UChar(value[result - 1]))) {
113 get_data(const char *name)
115 char *result = t_value;
118 for (n = 0; my_data[n].name != 0; ++n) {
119 if (!strcmp(name, my_data[n].name)) {
120 result = my_data[n].value;
129 * Read (possibly) multi-line data with name+value pairs.
132 read_data(const char *filename)
134 FILE *fp = fopen(filename, "r");
142 my_data = typeCalloc(MY_DATA, (size_t) 100); /* FIXME */
143 while (fgets(buffer, sizeof(buffer), fp) != 0) {
146 if (strcmp(buffer, ".")) {
147 char *prior = my_data[more - 1].value;
148 size_t need = strlen(buffer) + 2 + strlen(prior);
149 char *value = typeRealloc(char, need, prior);
152 _nc_STRCAT(value, "\n", need);
153 _nc_STRCAT(value, buffer, need);
154 my_data[more - 1].value = value;
158 } else if (*buffer == '#') {
160 } else if ((colon = strchr(buffer, ':')) != 0) {
164 name = strdup(buffer);
165 value = strdup(colon);
166 if (name == 0 || value == 0)
168 my_data[item].name = name;
169 my_data[item].value = value;
172 failed("expected a colon");
182 make_label(const char *label, int frow, int fcol)
184 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
187 set_field_buffer(f, 0, label);
188 set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
194 * Define each field with an extra one, for reflecting "actual" text.
197 make_field(const char *label, int frow, int fcol, int rows, int cols)
199 FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
202 set_field_back(f, A_UNDERLINE);
204 * If -j and -d options are combined, -j loses. It is documented in
205 * "Character User Interface Programming", page 12-15 that setting
206 * O_STATIC off makes the form library ignore justification.
208 set_field_just(f, j_value);
211 set_field_fore(f, (chtype) COLOR_PAIR(2));
212 set_field_back(f, (A_UNDERLINE | (chtype) COLOR_PAIR(3)));
214 set_field_fore(f, A_BOLD);
217 * The field_opts_off() call dumps core with Solaris curses,
218 * but that is a known bug in Solaris' form library -TD
220 field_opts_off(f, O_STATIC);
221 set_max_field(f, m_value);
224 init_edit_field(f, get_data(label));
230 display_form(FORM * f)
235 scale_form(f, &rows, &cols);
238 * Put the form at the upper-left corner of the display, with just a box
241 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
243 set_form_sub(f, derwin(w, rows, cols, 1, 2));
247 if (post_form(f) != E_OK)
255 WINDOW *w = form_win(f);
256 WINDOW *s = form_sub(f);
266 show_insert_mode(bool insert_mode)
268 MvAddStr(5, 57, (insert_mode
269 ? "form_status: insert "
270 : "form_status: overlay"));
273 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
276 another_field(FORM * form, FIELD * field)
278 FIELD **f = form_fields(form);
282 for (n = 0; f[n] != 0; ++n) {
285 field_opts_on(result, O_SELECTABLE);
293 my_form_driver(FORM * form, int c)
295 static bool insert_mode = TRUE;
300 if (form_driver(form, REQ_VALIDATION) == E_OK)
307 if ((field = current_field(form)) != 0) {
308 set_current_field(form, another_field(form, field));
309 if ((unsigned) field_opts(field) & O_EDIT) {
310 field_opts_off(field, O_EDIT);
311 set_field_status(field, 0);
313 field_opts_on(field, O_EDIT);
315 set_current_field(form, field);
319 /* there should be a form_status() function, but there is none */
321 if (form_driver(form, REQ_INS_MODE) == E_OK) {
325 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
329 show_insert_mode(insert_mode);
340 show_current_field(WINDOW *win, FORM * form)
346 int field_rows, field_cols, field_max;
350 wbkgd(win, (chtype) COLOR_PAIR(1));
353 form_getyx(form, currow, curcol);
354 wprintw(win, "Cursor: %d,%d", currow, curcol);
355 if (data_ahead(form))
356 waddstr(win, " ahead");
357 if (data_behind(form))
358 waddstr(win, " behind");
360 if ((field = current_field(form)) != 0) {
361 wprintw(win, "Page %d%s, Field %d/%d%s:",
363 new_page(field) ? "*" : "",
364 field_index(field), field_count(form),
365 field_arg(field) ? "(arg)" : "");
366 if ((type = field_type(field)) != 0) {
367 if (type == TYPE_ALNUM)
368 waddstr(win, "ALNUM");
369 else if (type == TYPE_ALPHA)
370 waddstr(win, "ALPHA");
371 else if (type == TYPE_ENUM)
372 waddstr(win, "ENUM");
373 else if (type == TYPE_INTEGER)
374 waddstr(win, "INTEGER");
375 #ifdef NCURSES_VERSION
376 else if (type == TYPE_IPV4)
377 waddstr(win, "IPV4");
379 else if (type == TYPE_NUMERIC)
380 waddstr(win, "NUMERIC");
381 else if (type == TYPE_REGEXP)
382 waddstr(win, "REGEXP");
384 waddstr(win, "other");
387 if ((unsigned) field_opts(field) & O_EDIT)
388 waddstr(win, " editable");
390 waddstr(win, " readonly");
392 if (field_status(field))
393 waddstr(win, " modified");
395 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
397 wprintw(win, " size %dx%d (max %d)",
398 field_rows, field_cols, field_max);
402 (void) wattrset(win, AttrArg(field_fore(field), 0));
403 waddstr(win, "fore");
404 wattroff(win, (int) field_fore(field));
408 (void) wattrset(win, AttrArg(field_back(field), 0));
409 waddstr(win, "back");
410 wattroff(win, (int) field_back(field));
412 wprintw(win, ", pad '%c'", field_pad(field));
415 for (nbuf = 0; nbuf <= 2; ++nbuf) {
416 if ((buffer = field_buffer(field, nbuf)) != 0) {
417 wprintw(win, "buffer %d:", nbuf);
418 (void) wattrset(win, A_REVERSE);
420 waddnstr(win, buffer, trimmed(buffer));
422 waddstr(win, buffer);
424 wattroff(win, A_REVERSE);
437 FIELD *f[100]; /* will memset to zero */
444 #ifdef NCURSES_MOUSE_VERSION
445 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
450 MvAddStr(4, 57, "Forms Entry Test");
451 show_insert_mode(TRUE);
455 /* describe the form */
456 memset(f, 0, sizeof(f));
457 for (pg = 0; pg < 4; ++pg) {
459 _nc_SPRINTF(label, _nc_SLIMIT(sizeof(label))
460 "Sample Form Page %d", pg + 1);
461 f[n++] = make_label(label, 0, 15);
462 set_new_page(f[n - 1], TRUE);
467 f[n++] = make_label(fname, 2, 0);
468 f[n++] = make_field(fname, 3, 0, 1, 18);
469 set_field_type(f[n - 1], TYPE_ALPHA, 1);
471 fname = "First Name";
472 f[n++] = make_label(fname, 2, 20);
473 f[n++] = make_field(fname, 3, 20, 1, 12);
474 set_field_type(f[n - 1], TYPE_ALPHA, 1);
476 fname = "Middle Name";
477 f[n++] = make_label(fname, 2, 34);
478 f[n++] = make_field(fname, 3, 34, 1, 12);
479 set_field_type(f[n - 1], TYPE_ALPHA, 1);
483 f[n++] = make_label(fname, 2, 0);
484 f[n++] = make_field(fname, 3, 0, 1, 18);
485 set_field_type(f[n - 1], TYPE_ALPHA, 1);
487 fname = "First Name";
488 f[n++] = make_label(fname, 2, 20);
489 f[n++] = make_field(fname, 3, 20, 1, 12);
490 set_field_type(f[n - 1], TYPE_ALPHA, 1);
493 f[n++] = make_label(fname, 2, 34);
494 f[n++] = make_field(fname, 3, 34, 1, 1);
495 set_field_pad(f[n - 1], '?');
496 set_field_type(f[n - 1], TYPE_ALPHA, 1);
500 f[n++] = make_label(fname, 2, 0);
501 f[n++] = make_field(fname, 3, 0, 1, 24);
502 set_field_type(f[n - 1], TYPE_ALNUM, 1);
504 #ifdef NCURSES_VERSION
505 fname = "IP Address";
506 f[n++] = make_label(fname, 2, 26);
507 f[n++] = make_field(fname, 3, 26, 1, 16);
508 set_field_type(f[n - 1], TYPE_IPV4, 1);
514 fname = "Four digits";
515 f[n++] = make_label(fname, 2, 0);
516 f[n++] = make_field(fname, 3, 0, 1, 18);
517 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
520 f[n++] = make_label(fname, 2, 20);
521 f[n++] = make_field(fname, 3, 20, 1, 12);
522 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
528 f[n++] = make_label(fname, 5, 0);
529 f[n++] = make_field(fname, 6, 0, 4, 46);
530 init_edit_field(f[n - 1], get_data(fname));
535 if ((form = new_form(f)) != 0) {
540 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
541 show_current_field(also, form);
544 switch (edit_field(form, &c)) {
547 case E_UNKNOWN_COMMAND:
548 finished = my_form_driver(form, c);
554 show_current_field(also, form);
561 for (c = 0; f[c] != 0; c++) {
562 void *ptr = field_userptr(f[c]);
569 #ifdef NCURSES_MOUSE_VERSION
570 mousemask(0, (mmask_t *) 0);
577 static const char *tbl[] =
579 "Usage: demo_forms [options] [data file]"
581 ," -d make fields dynamic"
582 ," -j value justify (1=left, 2=center, 3=right)"
583 ," -m value set maximum size of dynamic fields"
584 ," -o value specify number of offscreen rows in new_field()"
585 ," -t value specify text to fill fields initially"
588 for (j = 0; j < SIZEOF(tbl); ++j)
589 fprintf(stderr, "%s\n", tbl[j]);
594 main(int argc, char *argv[])
598 setlocale(LC_ALL, "");
600 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
606 j_value = atoi(optarg);
607 if (j_value < NO_JUSTIFICATION
608 || j_value > JUSTIFY_RIGHT)
612 m_value = atoi(optarg);
615 o_value = atoi(optarg);
625 while (optind < argc) {
626 read_data(argv[optind++]);
633 nonl(); /* lets us read ^M's */
634 intrflush(stdscr, FALSE);
635 keypad(stdscr, TRUE);
639 init_pair(1, COLOR_WHITE, COLOR_BLUE);
640 init_pair(2, COLOR_GREEN, COLOR_BLACK);
641 init_pair(3, COLOR_CYAN, COLOR_BLACK);
642 bkgd((chtype) COLOR_PAIR(1));
649 ExitProgram(EXIT_SUCCESS);
656 printf("This program requires the curses form library\n");
657 ExitProgram(EXIT_FAILURE);