1 /****************************************************************************
2 * Copyright 2018-2022,2023 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.63 2023/11/11 00:29:53 tom Exp $
32 * Demonstrate a variety of functions from the form library.
33 * Thomas Dickey - 2003/4/26
36 #include <test.priv.h>
40 #include <edit_field.h>
47 static MY_DATA *my_data;
49 static int d_option = 0;
50 static int j_value = 0;
51 static int m_value = 0;
52 static int o_value = 0;
53 static char *t_value = 0;
59 ExitProgram(EXIT_FAILURE);
65 size_t have = strlen(value);
66 while (have != 0 && (value[have - 1] == '\n' || value[have - 1] == '\r')) {
72 trimmed(const char *value)
74 int result = (int) strlen(value);
75 while (result > 0 && isspace(UChar(value[result - 1]))) {
82 get_data(const char *name)
84 char *result = t_value;
87 for (n = 0; my_data[n].name != 0; ++n) {
88 if (!strcmp(name, my_data[n].name)) {
89 result = my_data[n].value;
98 * Read (possibly) multi-line data with name+value pairs.
101 read_data(const char *filename)
103 FILE *fp = fopen(filename, "r");
111 my_data = typeCalloc(MY_DATA, (size_t) 100); /* FIXME */
112 while (fgets(buffer, sizeof(buffer), fp) != 0) {
115 if (strcmp(buffer, ".")) {
116 char *prior = my_data[more - 1].value;
117 size_t need = strlen(buffer) + 2 + strlen(prior);
118 char *value = typeRealloc(char, need, prior);
121 _nc_STRCAT(value, "\n", need);
122 _nc_STRCAT(value, buffer, need);
123 my_data[more - 1].value = value;
127 } else if (*buffer == '#') {
129 } else if ((colon = strchr(buffer, ':')) != 0) {
133 name = strdup(buffer);
134 value = strdup(colon);
135 if (name == 0 || value == 0)
137 my_data[item].name = name;
138 my_data[item].value = value;
141 failed("expected a colon");
151 make_label(const char *label, int frow, int fcol)
153 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
156 set_field_buffer(f, 0, label);
157 set_field_opts(f, (int) ((unsigned) field_opts(f) & (unsigned) ~O_ACTIVE));
163 * Define each field with an extra one, for reflecting "actual" text.
166 make_field(const char *label, int frow, int fcol, int rows, int cols)
168 FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
171 set_field_back(f, A_UNDERLINE);
173 * If -j and -d options are combined, -j loses. It is documented in
174 * "Character User Interface Programming", page 12-15 that setting
175 * O_STATIC off makes the form library ignore justification.
177 set_field_just(f, j_value);
180 set_field_fore(f, (chtype) COLOR_PAIR(2));
181 set_field_back(f, (A_UNDERLINE | (chtype) COLOR_PAIR(3)));
183 set_field_fore(f, A_BOLD);
186 * The field_opts_off() call dumps core with Solaris curses,
187 * but that is a known bug in Solaris' form library -TD
189 field_opts_off(f, O_STATIC);
190 set_max_field(f, m_value);
193 init_edit_field(f, get_data(label));
199 display_form(FORM *f)
204 scale_form(f, &rows, &cols);
207 * Put the form at the upper-left corner of the display, with just a box
210 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
212 set_form_sub(f, derwin(w, rows, cols, 1, 2));
216 if (post_form(f) != E_OK)
224 WINDOW *w = form_win(f);
225 WINDOW *s = form_sub(f);
235 show_insert_mode(bool insert_mode)
237 MvAddStr(5, 57, (insert_mode
238 ? "form_status: insert "
239 : "form_status: overlay"));
242 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
245 another_field(FORM *form, const FIELD *const field)
247 FIELD **f = form_fields(form);
251 for (n = 0; f[n] != 0; ++n) {
254 field_opts_on(result, O_SELECTABLE);
262 my_form_driver(FORM *form, int c)
264 static bool insert_mode = TRUE;
269 if (form_driver(form, REQ_VALIDATION) == E_OK)
276 if ((field = current_field(form)) != 0) {
277 set_current_field(form, another_field(form, field));
278 if ((unsigned) field_opts(field) & O_EDIT) {
279 field_opts_off(field, O_EDIT);
280 set_field_status(field, 0);
282 field_opts_on(field, O_EDIT);
284 set_current_field(form, field);
288 /* there should be a form_status() function, but there is none */
290 if (form_driver(form, REQ_INS_MODE) == E_OK) {
294 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
298 show_insert_mode(insert_mode);
309 show_current_field(WINDOW *win, FORM *form)
312 int field_rows, field_cols, field_max;
316 wbkgd(win, (chtype) COLOR_PAIR(1));
319 form_getyx(form, currow, curcol);
320 wprintw(win, "Cursor: %d,%d", currow, curcol);
321 if (data_ahead(form))
322 waddstr(win, " ahead");
323 if (data_behind(form))
324 waddstr(win, " behind");
327 if ((field = current_field(form)) != 0) {
331 wprintw(win, "Page %d%s, Field %d/%d%s:",
333 new_page(field) ? "*" : "",
334 field_index(field), field_count(form),
335 field_arg(field) ? "(arg)" : "");
336 if ((type = field_type(field)) != 0) {
337 if (type == TYPE_ALNUM)
338 waddstr(win, "ALNUM");
339 else if (type == TYPE_ALPHA)
340 waddstr(win, "ALPHA");
341 else if (type == TYPE_ENUM)
342 waddstr(win, "ENUM");
343 else if (type == TYPE_INTEGER)
344 waddstr(win, "INTEGER");
345 #ifdef NCURSES_VERSION
346 else if (type == TYPE_IPV4)
347 waddstr(win, "IPV4");
349 else if (type == TYPE_NUMERIC)
350 waddstr(win, "NUMERIC");
351 else if (type == TYPE_REGEXP)
352 waddstr(win, "REGEXP");
354 waddstr(win, "other");
357 if ((unsigned) field_opts(field) & O_EDIT)
358 waddstr(win, " editable");
360 waddstr(win, " readonly");
362 if (field_status(field))
363 waddstr(win, " modified");
365 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
367 wprintw(win, " size %dx%d (max %d)",
368 field_rows, field_cols, field_max);
372 (void) wattrset(win, AttrArg(field_fore(field), 0));
373 waddstr(win, "fore");
374 wattroff(win, (int) field_fore(field));
378 (void) wattrset(win, AttrArg(field_back(field), 0));
379 waddstr(win, "back");
380 wattroff(win, (int) field_back(field));
382 wprintw(win, ", pad '%c'", field_pad(field));
385 for (nbuf = 0; nbuf <= 2; ++nbuf) {
387 if ((buffer = field_buffer(field, nbuf)) != 0) {
388 wprintw(win, "buffer %d:", nbuf);
389 (void) wattrset(win, A_REVERSE);
391 waddnstr(win, buffer, trimmed(buffer));
393 waddstr(win, buffer);
395 wattroff(win, A_REVERSE);
407 FIELD *f[100]; /* will memset to zero */
412 static const char *my_enum[] =
413 {"first", "second", "third", 0};
415 #ifdef NCURSES_MOUSE_VERSION
416 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
421 MvAddStr(4, 57, "Forms Entry Test");
422 show_insert_mode(TRUE);
426 /* describe the form */
427 memset(f, 0, sizeof(f));
428 for (pg = 0; pg < 4; ++pg) {
430 _nc_SPRINTF(label, _nc_SLIMIT(sizeof(label))
431 "Sample Form Page %d", pg + 1);
432 f[n++] = make_label(label, 0, 15);
433 set_new_page(f[n - 1], TRUE);
438 f[n++] = make_label(fname, 2, 0);
439 f[n++] = make_field(fname, 3, 0, 1, 18);
440 set_field_type(f[n - 1], TYPE_ALPHA, 1);
442 fname = "First Name";
443 f[n++] = make_label(fname, 2, 20);
444 f[n++] = make_field(fname, 3, 20, 1, 12);
445 set_field_type(f[n - 1], TYPE_ALPHA, 1);
447 fname = "Middle Name";
448 f[n++] = make_label(fname, 2, 34);
449 f[n++] = make_field(fname, 3, 34, 1, 12);
450 set_field_type(f[n - 1], TYPE_ALPHA, 1);
455 f[n++] = make_label(fname, 2, 0);
456 f[n++] = make_field(fname, 3, 0, 1, 12);
457 set_field_type(f[n - 1], TYPE_ALPHA, 1);
459 fname = "First Name";
460 f[n++] = make_label(fname, 2, 14);
461 f[n++] = make_field(fname, 3, 14, 1, 12);
462 set_field_type(f[n - 1], TYPE_ALPHA, 1);
465 f[n++] = make_label(fname, 2, 28);
466 f[n++] = make_field(fname, 3, 28, 1, 1);
467 set_field_pad(f[n - 1], '?');
468 set_field_type(f[n - 1], TYPE_ALPHA, 1);
470 fname = "First/Second/Third";
471 f[n++] = make_label(fname, 2, 32);
472 f[n++] = make_field(fname, 3, 32, 1, 12);
473 set_field_type(f[n - 1], TYPE_ENUM, my_enum, 0, 0);
478 f[n++] = make_label(fname, 2, 0);
479 f[n++] = make_field(fname, 3, 0, 1, 24);
480 set_field_type(f[n - 1], TYPE_ALNUM, 1);
482 #ifdef NCURSES_VERSION
483 fname = "IP Address";
484 f[n++] = make_label(fname, 2, 26);
485 f[n++] = make_field(fname, 3, 26, 1, 16);
486 set_field_type(f[n - 1], TYPE_IPV4, 1);
491 fname = "Four digits";
492 f[n++] = make_label(fname, 2, 0);
493 f[n++] = make_field(fname, 3, 0, 1, 10);
494 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
497 f[n++] = make_label(fname, 2, 13);
498 f[n++] = make_field(fname, 3, 13, 1, 12);
499 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
501 fname = "Phone number";
502 f[n++] = make_label(fname, 2, 27);
503 f[n++] = make_field(fname, 3, 27, 1, 16);
504 set_field_type(f[n - 1], TYPE_REGEXP,
505 "^([0-9]-)?[0-9]{3}-[0-9]{3}-[0-9]{4} *$");;
510 f[n++] = make_label(fname, 5, 0);
511 f[n++] = make_field(fname, 6, 0, 4, 46);
512 init_edit_field(f[n - 1], get_data(fname));
517 if ((form = new_form(f)) != 0) {
525 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
526 show_current_field(also, form);
529 switch (edit_field(form, &c)) {
532 case E_UNKNOWN_COMMAND:
533 finished = my_form_driver(form, c);
539 show_current_field(also, form);
546 for (c = 0; f[c] != 0; c++) {
547 free_edit_field(f[c]);
553 #ifdef NCURSES_MOUSE_VERSION
554 mousemask(0, (mmask_t *) 0);
561 static const char *tbl[] =
563 "Usage: demo_forms [options] [data file]"
567 ," -d make fields dynamic"
568 ," -j NUM justify (1=left, 2=center, 3=right)"
569 ," -m NUM set maximum size of dynamic fields"
570 ," -o NUM specify number of offscreen rows in new_field()"
571 ," -t NUM specify text to fill fields initially"
574 for (j = 0; j < SIZEOF(tbl); ++j)
575 fprintf(stderr, "%s\n", tbl[j]);
576 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
583 main(int argc, char *argv[])
587 setlocale(LC_ALL, "");
589 while ((ch = getopt(argc, argv, OPTS_COMMON "dj:m:o:t:")) != -1) {
595 j_value = atoi(optarg);
596 if (j_value < NO_JUSTIFICATION
597 || j_value > JUSTIFY_RIGHT)
601 m_value = atoi(optarg);
604 o_value = atoi(optarg);
611 ExitProgram(EXIT_SUCCESS);
613 usage(ch == OPTS_USAGE);
617 while (optind < argc) {
618 read_data(argv[optind++]);
625 nonl(); /* lets us read ^M's */
626 intrflush(stdscr, FALSE);
627 keypad(stdscr, TRUE);
631 init_pair(1, COLOR_WHITE, COLOR_BLUE);
632 init_pair(2, COLOR_GREEN, COLOR_BLACK);
633 init_pair(3, COLOR_CYAN, COLOR_BLACK);
634 bkgd((chtype) COLOR_PAIR(1));
641 ExitProgram(EXIT_SUCCESS);
648 printf("This program requires the curses form library\n");
649 ExitProgram(EXIT_FAILURE);