1 /****************************************************************************
2 * Copyright (c) 2003-2017,2018 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.55 2018/07/14 23:26:02 tom Exp $
31 * Demonstrate a variety of functions from the form library.
32 * Thomas Dickey - 2003/4/26
43 form_request_by_name -
55 set_fieldtype_choice -
65 #include <test.priv.h>
69 #include <edit_field.h>
76 static MY_DATA *my_data;
78 static int d_option = 0;
79 static int j_value = 0;
80 static int m_value = 0;
81 static int o_value = 0;
82 static char *t_value = 0;
88 ExitProgram(EXIT_FAILURE);
94 size_t have = strlen(value);
95 while (have != 0 && (value[have - 1] == '\n' || value[have - 1] == '\r')) {
101 trimmed(const char *value)
103 int result = (int) strlen(value);
104 while (result > 0 && isspace(UChar(value[result - 1]))) {
111 get_data(const char *name)
113 char *result = t_value;
116 for (n = 0; my_data[n].name != 0; ++n) {
117 if (!strcmp(name, my_data[n].name)) {
118 result = my_data[n].value;
127 * Read (possibly) multi-line data with name+value pairs.
130 read_data(const char *filename)
132 FILE *fp = fopen(filename, "r");
140 my_data = typeCalloc(MY_DATA, (size_t) 100); /* FIXME */
141 while (fgets(buffer, sizeof(buffer), fp) != 0) {
144 if (strcmp(buffer, ".")) {
145 char *prior = my_data[more - 1].value;
146 size_t need = strlen(buffer) + 2 + strlen(prior);
147 char *value = typeRealloc(char, need, prior);
150 _nc_STRCAT(value, "\n", need);
151 _nc_STRCAT(value, buffer, need);
152 my_data[more - 1].value = value;
156 } else if (*buffer == '#') {
158 } else if ((colon = strchr(buffer, ':')) != 0) {
162 name = strdup(buffer);
163 value = strdup(colon);
164 if (name == 0 || value == 0)
166 my_data[item].name = name;
167 my_data[item].value = value;
170 failed("expected a colon");
180 make_label(const char *label, int frow, int fcol)
182 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
185 set_field_buffer(f, 0, label);
186 set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
192 * Define each field with an extra one, for reflecting "actual" text.
195 make_field(const char *label, int frow, int fcol, int rows, int cols)
197 FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
200 set_field_back(f, A_UNDERLINE);
202 * If -j and -d options are combined, -j loses. It is documented in
203 * "Character User Interface Programming", page 12-15 that setting
204 * O_STATIC off makes the form library ignore justification.
206 set_field_just(f, j_value);
209 set_field_fore(f, (chtype) COLOR_PAIR(2));
210 set_field_back(f, (A_UNDERLINE | (chtype) COLOR_PAIR(3)));
212 set_field_fore(f, A_BOLD);
215 * The field_opts_off() call dumps core with Solaris curses,
216 * but that is a known bug in Solaris' form library -TD
218 field_opts_off(f, O_STATIC);
219 set_max_field(f, m_value);
222 init_edit_field(f, get_data(label));
228 display_form(FORM *f)
233 scale_form(f, &rows, &cols);
236 * Put the form at the upper-left corner of the display, with just a box
239 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
241 set_form_sub(f, derwin(w, rows, cols, 1, 2));
245 if (post_form(f) != E_OK)
253 WINDOW *w = form_win(f);
254 WINDOW *s = form_sub(f);
264 show_insert_mode(bool insert_mode)
266 MvAddStr(5, 57, (insert_mode
267 ? "form_status: insert "
268 : "form_status: overlay"));
271 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
274 another_field(FORM *form, FIELD *field)
276 FIELD **f = form_fields(form);
280 for (n = 0; f[n] != 0; ++n) {
283 field_opts_on(result, O_SELECTABLE);
291 my_form_driver(FORM *form, int c)
293 static bool insert_mode = TRUE;
298 if (form_driver(form, REQ_VALIDATION) == E_OK)
305 if ((field = current_field(form)) != 0) {
306 set_current_field(form, another_field(form, field));
307 if ((unsigned) field_opts(field) & O_EDIT) {
308 field_opts_off(field, O_EDIT);
309 set_field_status(field, 0);
311 field_opts_on(field, O_EDIT);
313 set_current_field(form, field);
317 /* there should be a form_status() function, but there is none */
319 if (form_driver(form, REQ_INS_MODE) == E_OK) {
323 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
327 show_insert_mode(insert_mode);
338 show_current_field(WINDOW *win, FORM *form)
344 int field_rows, field_cols, field_max;
348 wbkgd(win, (chtype) COLOR_PAIR(1));
351 form_getyx(form, currow, curcol);
352 wprintw(win, "Cursor: %d,%d", currow, curcol);
353 if (data_ahead(form))
354 waddstr(win, " ahead");
355 if (data_behind(form))
356 waddstr(win, " behind");
358 if ((field = current_field(form)) != 0) {
359 wprintw(win, "Page %d%s, Field %d/%d%s:",
361 new_page(field) ? "*" : "",
362 field_index(field), field_count(form),
363 field_arg(field) ? "(arg)" : "");
364 if ((type = field_type(field)) != 0) {
365 if (type == TYPE_ALNUM)
366 waddstr(win, "ALNUM");
367 else if (type == TYPE_ALPHA)
368 waddstr(win, "ALPHA");
369 else if (type == TYPE_ENUM)
370 waddstr(win, "ENUM");
371 else if (type == TYPE_INTEGER)
372 waddstr(win, "INTEGER");
373 #ifdef NCURSES_VERSION
374 else if (type == TYPE_IPV4)
375 waddstr(win, "IPV4");
377 else if (type == TYPE_NUMERIC)
378 waddstr(win, "NUMERIC");
379 else if (type == TYPE_REGEXP)
380 waddstr(win, "REGEXP");
382 waddstr(win, "other");
385 if ((unsigned) field_opts(field) & O_EDIT)
386 waddstr(win, " editable");
388 waddstr(win, " readonly");
390 if (field_status(field))
391 waddstr(win, " modified");
393 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
395 wprintw(win, " size %dx%d (max %d)",
396 field_rows, field_cols, field_max);
400 (void) wattrset(win, AttrArg(field_fore(field), 0));
401 waddstr(win, "fore");
402 wattroff(win, (int) field_fore(field));
406 (void) wattrset(win, AttrArg(field_back(field), 0));
407 waddstr(win, "back");
408 wattroff(win, (int) field_back(field));
410 wprintw(win, ", pad '%c'", field_pad(field));
413 for (nbuf = 0; nbuf <= 2; ++nbuf) {
414 if ((buffer = field_buffer(field, nbuf)) != 0) {
415 wprintw(win, "buffer %d:", nbuf);
416 (void) wattrset(win, A_REVERSE);
418 waddnstr(win, buffer, trimmed(buffer));
420 waddstr(win, buffer);
422 wattroff(win, A_REVERSE);
435 FIELD *f[100]; /* will memset to zero */
441 static const char *my_enum[] =
442 {"first", "second", "third", 0};
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);
484 f[n++] = make_label(fname, 2, 0);
485 f[n++] = make_field(fname, 3, 0, 1, 12);
486 set_field_type(f[n - 1], TYPE_ALPHA, 1);
488 fname = "First Name";
489 f[n++] = make_label(fname, 2, 14);
490 f[n++] = make_field(fname, 3, 14, 1, 12);
491 set_field_type(f[n - 1], TYPE_ALPHA, 1);
494 f[n++] = make_label(fname, 2, 28);
495 f[n++] = make_field(fname, 3, 28, 1, 1);
496 set_field_pad(f[n - 1], '?');
497 set_field_type(f[n - 1], TYPE_ALPHA, 1);
499 fname = "First/Second/Third";
500 f[n++] = make_label(fname, 2, 32);
501 f[n++] = make_field(fname, 3, 32, 1, 12);
502 set_field_type(f[n - 1], TYPE_ENUM, my_enum, 0, 0);
507 f[n++] = make_label(fname, 2, 0);
508 f[n++] = make_field(fname, 3, 0, 1, 24);
509 set_field_type(f[n - 1], TYPE_ALNUM, 1);
511 #ifdef NCURSES_VERSION
512 fname = "IP Address";
513 f[n++] = make_label(fname, 2, 26);
514 f[n++] = make_field(fname, 3, 26, 1, 16);
515 set_field_type(f[n - 1], TYPE_IPV4, 1);
520 fname = "Four digits";
521 f[n++] = make_label(fname, 2, 0);
522 f[n++] = make_field(fname, 3, 0, 1, 10);
523 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
526 f[n++] = make_label(fname, 2, 13);
527 f[n++] = make_field(fname, 3, 13, 1, 12);
528 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
530 fname = "Phone number";
531 f[n++] = make_label(fname, 2, 27);
532 f[n++] = make_field(fname, 3, 27, 1, 16);
533 set_field_type(f[n - 1], TYPE_REGEXP,
534 "^([0-9]-)?[0-9]{3}-[0-9]{3}-[0-9]{4} *$");;
539 f[n++] = make_label(fname, 5, 0);
540 f[n++] = make_field(fname, 6, 0, 4, 46);
541 init_edit_field(f[n - 1], get_data(fname));
546 if ((form = new_form(f)) != 0) {
551 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
552 show_current_field(also, form);
555 switch (edit_field(form, &c)) {
558 case E_UNKNOWN_COMMAND:
559 finished = my_form_driver(form, c);
565 show_current_field(also, form);
572 for (c = 0; f[c] != 0; c++) {
573 free_edit_field(f[c]);
579 #ifdef NCURSES_MOUSE_VERSION
580 mousemask(0, (mmask_t *) 0);
587 static const char *tbl[] =
589 "Usage: demo_forms [options] [data file]"
591 ," -d make fields dynamic"
592 ," -j value justify (1=left, 2=center, 3=right)"
593 ," -m value set maximum size of dynamic fields"
594 ," -o value specify number of offscreen rows in new_field()"
595 ," -t value specify text to fill fields initially"
598 for (j = 0; j < SIZEOF(tbl); ++j)
599 fprintf(stderr, "%s\n", tbl[j]);
604 main(int argc, char *argv[])
608 setlocale(LC_ALL, "");
610 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
616 j_value = atoi(optarg);
617 if (j_value < NO_JUSTIFICATION
618 || j_value > JUSTIFY_RIGHT)
622 m_value = atoi(optarg);
625 o_value = atoi(optarg);
635 while (optind < argc) {
636 read_data(argv[optind++]);
643 nonl(); /* lets us read ^M's */
644 intrflush(stdscr, FALSE);
645 keypad(stdscr, TRUE);
649 init_pair(1, COLOR_WHITE, COLOR_BLUE);
650 init_pair(2, COLOR_GREEN, COLOR_BLACK);
651 init_pair(3, COLOR_CYAN, COLOR_BLACK);
652 bkgd((chtype) COLOR_PAIR(1));
659 ExitProgram(EXIT_SUCCESS);
666 printf("This program requires the curses form library\n");
667 ExitProgram(EXIT_FAILURE);