1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2003-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: demo_forms.c,v 1.57 2020/02/02 23:34:34 tom Exp $
32 * Demonstrate a variety of functions from the form library.
33 * Thomas Dickey - 2003/4/26
44 form_request_by_name -
56 set_fieldtype_choice -
66 #include <test.priv.h>
70 #include <edit_field.h>
77 static MY_DATA *my_data;
79 static int d_option = 0;
80 static int j_value = 0;
81 static int m_value = 0;
82 static int o_value = 0;
83 static char *t_value = 0;
89 ExitProgram(EXIT_FAILURE);
95 size_t have = strlen(value);
96 while (have != 0 && (value[have - 1] == '\n' || value[have - 1] == '\r')) {
102 trimmed(const char *value)
104 int result = (int) strlen(value);
105 while (result > 0 && isspace(UChar(value[result - 1]))) {
112 get_data(const char *name)
114 char *result = t_value;
117 for (n = 0; my_data[n].name != 0; ++n) {
118 if (!strcmp(name, my_data[n].name)) {
119 result = my_data[n].value;
128 * Read (possibly) multi-line data with name+value pairs.
131 read_data(const char *filename)
133 FILE *fp = fopen(filename, "r");
141 my_data = typeCalloc(MY_DATA, (size_t) 100); /* FIXME */
142 while (fgets(buffer, sizeof(buffer), fp) != 0) {
145 if (strcmp(buffer, ".")) {
146 char *prior = my_data[more - 1].value;
147 size_t need = strlen(buffer) + 2 + strlen(prior);
148 char *value = typeRealloc(char, need, prior);
151 _nc_STRCAT(value, "\n", need);
152 _nc_STRCAT(value, buffer, need);
153 my_data[more - 1].value = value;
157 } else if (*buffer == '#') {
159 } else if ((colon = strchr(buffer, ':')) != 0) {
163 name = strdup(buffer);
164 value = strdup(colon);
165 if (name == 0 || value == 0)
167 my_data[item].name = name;
168 my_data[item].value = value;
171 failed("expected a colon");
181 make_label(const char *label, int frow, int fcol)
183 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
186 set_field_buffer(f, 0, label);
187 set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
193 * Define each field with an extra one, for reflecting "actual" text.
196 make_field(const char *label, int frow, int fcol, int rows, int cols)
198 FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
201 set_field_back(f, A_UNDERLINE);
203 * If -j and -d options are combined, -j loses. It is documented in
204 * "Character User Interface Programming", page 12-15 that setting
205 * O_STATIC off makes the form library ignore justification.
207 set_field_just(f, j_value);
210 set_field_fore(f, (chtype) COLOR_PAIR(2));
211 set_field_back(f, (A_UNDERLINE | (chtype) COLOR_PAIR(3)));
213 set_field_fore(f, A_BOLD);
216 * The field_opts_off() call dumps core with Solaris curses,
217 * but that is a known bug in Solaris' form library -TD
219 field_opts_off(f, O_STATIC);
220 set_max_field(f, m_value);
223 init_edit_field(f, get_data(label));
229 display_form(FORM *f)
234 scale_form(f, &rows, &cols);
237 * Put the form at the upper-left corner of the display, with just a box
240 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
242 set_form_sub(f, derwin(w, rows, cols, 1, 2));
246 if (post_form(f) != E_OK)
254 WINDOW *w = form_win(f);
255 WINDOW *s = form_sub(f);
265 show_insert_mode(bool insert_mode)
267 MvAddStr(5, 57, (insert_mode
268 ? "form_status: insert "
269 : "form_status: overlay"));
272 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
275 another_field(FORM *form, FIELD *field)
277 FIELD **f = form_fields(form);
281 for (n = 0; f[n] != 0; ++n) {
284 field_opts_on(result, O_SELECTABLE);
292 my_form_driver(FORM *form, int c)
294 static bool insert_mode = TRUE;
299 if (form_driver(form, REQ_VALIDATION) == E_OK)
306 if ((field = current_field(form)) != 0) {
307 set_current_field(form, another_field(form, field));
308 if ((unsigned) field_opts(field) & O_EDIT) {
309 field_opts_off(field, O_EDIT);
310 set_field_status(field, 0);
312 field_opts_on(field, O_EDIT);
314 set_current_field(form, field);
318 /* there should be a form_status() function, but there is none */
320 if (form_driver(form, REQ_INS_MODE) == E_OK) {
324 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
328 show_insert_mode(insert_mode);
339 show_current_field(WINDOW *win, FORM *form)
342 int field_rows, field_cols, field_max;
346 wbkgd(win, (chtype) COLOR_PAIR(1));
349 form_getyx(form, currow, curcol);
350 wprintw(win, "Cursor: %d,%d", currow, curcol);
351 if (data_ahead(form))
352 waddstr(win, " ahead");
353 if (data_behind(form))
354 waddstr(win, " behind");
357 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) {
417 if ((buffer = field_buffer(field, nbuf)) != 0) {
418 wprintw(win, "buffer %d:", nbuf);
419 (void) wattrset(win, A_REVERSE);
421 waddnstr(win, buffer, trimmed(buffer));
423 waddstr(win, buffer);
425 wattroff(win, A_REVERSE);
437 FIELD *f[100]; /* will memset to zero */
442 static const char *my_enum[] =
443 {"first", "second", "third", 0};
445 #ifdef NCURSES_MOUSE_VERSION
446 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
451 MvAddStr(4, 57, "Forms Entry Test");
452 show_insert_mode(TRUE);
456 /* describe the form */
457 memset(f, 0, sizeof(f));
458 for (pg = 0; pg < 4; ++pg) {
460 _nc_SPRINTF(label, _nc_SLIMIT(sizeof(label))
461 "Sample Form Page %d", pg + 1);
462 f[n++] = make_label(label, 0, 15);
463 set_new_page(f[n - 1], TRUE);
468 f[n++] = make_label(fname, 2, 0);
469 f[n++] = make_field(fname, 3, 0, 1, 18);
470 set_field_type(f[n - 1], TYPE_ALPHA, 1);
472 fname = "First Name";
473 f[n++] = make_label(fname, 2, 20);
474 f[n++] = make_field(fname, 3, 20, 1, 12);
475 set_field_type(f[n - 1], TYPE_ALPHA, 1);
477 fname = "Middle Name";
478 f[n++] = make_label(fname, 2, 34);
479 f[n++] = make_field(fname, 3, 34, 1, 12);
480 set_field_type(f[n - 1], TYPE_ALPHA, 1);
485 f[n++] = make_label(fname, 2, 0);
486 f[n++] = make_field(fname, 3, 0, 1, 12);
487 set_field_type(f[n - 1], TYPE_ALPHA, 1);
489 fname = "First Name";
490 f[n++] = make_label(fname, 2, 14);
491 f[n++] = make_field(fname, 3, 14, 1, 12);
492 set_field_type(f[n - 1], TYPE_ALPHA, 1);
495 f[n++] = make_label(fname, 2, 28);
496 f[n++] = make_field(fname, 3, 28, 1, 1);
497 set_field_pad(f[n - 1], '?');
498 set_field_type(f[n - 1], TYPE_ALPHA, 1);
500 fname = "First/Second/Third";
501 f[n++] = make_label(fname, 2, 32);
502 f[n++] = make_field(fname, 3, 32, 1, 12);
503 set_field_type(f[n - 1], TYPE_ENUM, my_enum, 0, 0);
508 f[n++] = make_label(fname, 2, 0);
509 f[n++] = make_field(fname, 3, 0, 1, 24);
510 set_field_type(f[n - 1], TYPE_ALNUM, 1);
512 #ifdef NCURSES_VERSION
513 fname = "IP Address";
514 f[n++] = make_label(fname, 2, 26);
515 f[n++] = make_field(fname, 3, 26, 1, 16);
516 set_field_type(f[n - 1], TYPE_IPV4, 1);
521 fname = "Four digits";
522 f[n++] = make_label(fname, 2, 0);
523 f[n++] = make_field(fname, 3, 0, 1, 10);
524 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
527 f[n++] = make_label(fname, 2, 13);
528 f[n++] = make_field(fname, 3, 13, 1, 12);
529 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
531 fname = "Phone number";
532 f[n++] = make_label(fname, 2, 27);
533 f[n++] = make_field(fname, 3, 27, 1, 16);
534 set_field_type(f[n - 1], TYPE_REGEXP,
535 "^([0-9]-)?[0-9]{3}-[0-9]{3}-[0-9]{4} *$");;
540 f[n++] = make_label(fname, 5, 0);
541 f[n++] = make_field(fname, 6, 0, 4, 46);
542 init_edit_field(f[n - 1], get_data(fname));
547 if ((form = new_form(f)) != 0) {
555 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
556 show_current_field(also, form);
559 switch (edit_field(form, &c)) {
562 case E_UNKNOWN_COMMAND:
563 finished = my_form_driver(form, c);
569 show_current_field(also, form);
576 for (c = 0; f[c] != 0; c++) {
577 free_edit_field(f[c]);
583 #ifdef NCURSES_MOUSE_VERSION
584 mousemask(0, (mmask_t *) 0);
591 static const char *tbl[] =
593 "Usage: demo_forms [options] [data file]"
595 ," -d make fields dynamic"
596 ," -j value justify (1=left, 2=center, 3=right)"
597 ," -m value set maximum size of dynamic fields"
598 ," -o value specify number of offscreen rows in new_field()"
599 ," -t value specify text to fill fields initially"
602 for (j = 0; j < SIZEOF(tbl); ++j)
603 fprintf(stderr, "%s\n", tbl[j]);
608 main(int argc, char *argv[])
612 setlocale(LC_ALL, "");
614 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
620 j_value = atoi(optarg);
621 if (j_value < NO_JUSTIFICATION
622 || j_value > JUSTIFY_RIGHT)
626 m_value = atoi(optarg);
629 o_value = atoi(optarg);
639 while (optind < argc) {
640 read_data(argv[optind++]);
647 nonl(); /* lets us read ^M's */
648 intrflush(stdscr, FALSE);
649 keypad(stdscr, TRUE);
653 init_pair(1, COLOR_WHITE, COLOR_BLUE);
654 init_pair(2, COLOR_GREEN, COLOR_BLACK);
655 init_pair(3, COLOR_CYAN, COLOR_BLACK);
656 bkgd((chtype) COLOR_PAIR(1));
663 ExitProgram(EXIT_SUCCESS);
670 printf("This program requires the curses form library\n");
671 ExitProgram(EXIT_FAILURE);