1 /****************************************************************************
2 * Copyright (c) 2003-2012,2013 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.46 2013/09/28 21:55:54 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);
153 strcat(value, buffer);
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");
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 | 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)
345 int field_rows, field_cols, field_max;
349 wbkgd(win, (chtype) COLOR_PAIR(1));
352 form_getyx(form, currow, curcol);
353 wprintw(win, "Cursor: %d,%d", currow, curcol);
354 if (data_ahead(form))
355 waddstr(win, " ahead");
356 if (data_behind(form))
357 waddstr(win, " behind");
359 if ((field = current_field(form)) != 0) {
360 wprintw(win, "Page %d%s, Field %d/%d%s:",
362 new_page(field) ? "*" : "",
363 field_index(field), field_count(form),
364 field_arg(field) ? "(arg)" : "");
365 if ((type = field_type(field)) != 0) {
366 if (type == TYPE_ALNUM)
367 waddstr(win, "ALNUM");
368 else if (type == TYPE_ALPHA)
369 waddstr(win, "ALPHA");
370 else if (type == TYPE_ENUM)
371 waddstr(win, "ENUM");
372 else if (type == TYPE_INTEGER)
373 waddstr(win, "INTEGER");
374 #ifdef NCURSES_VERSION
375 else if (type == TYPE_IPV4)
376 waddstr(win, "IPV4");
378 else if (type == TYPE_NUMERIC)
379 waddstr(win, "NUMERIC");
380 else if (type == TYPE_REGEXP)
381 waddstr(win, "REGEXP");
383 waddstr(win, "other");
386 if ((unsigned) field_opts(field) & O_EDIT)
387 waddstr(win, " editable");
389 waddstr(win, " readonly");
391 if (field_status(field))
392 waddstr(win, " modified");
394 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
396 wprintw(win, " size %dx%d (max %d)",
397 field_rows, field_cols, field_max);
401 (void) wattrset(win, (int) field_fore(field));
402 waddstr(win, "fore");
403 wattroff(win, (int) field_fore(field));
407 (void) wattrset(win, (int) field_back(field));
408 waddstr(win, "back");
409 wattroff(win, (int) field_back(field));
411 wprintw(win, ", pad '%c'", field_pad(field));
414 for (nbuf = 0; nbuf <= 2; ++nbuf) {
415 if ((buffer = field_buffer(field, nbuf)) != 0) {
416 wprintw(win, "buffer %d:", nbuf);
417 (void) wattrset(win, A_REVERSE);
419 waddnstr(win, buffer, trimmed(buffer));
421 waddstr(win, buffer);
423 wattroff(win, A_REVERSE);
436 FIELD *f[100]; /* will memset to zero */
443 #ifdef NCURSES_MOUSE_VERSION
444 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
449 MvAddStr(4, 57, "Forms Entry Test");
450 show_insert_mode(TRUE);
454 /* describe the form */
455 memset(f, 0, sizeof(f));
456 for (pg = 0; pg < 4; ++pg) {
458 sprintf(label, "Sample Form Page %d", pg + 1);
459 f[n++] = make_label(label, 0, 15);
460 set_new_page(f[n - 1], TRUE);
465 f[n++] = make_label(fname, 2, 0);
466 f[n++] = make_field(fname, 3, 0, 1, 18);
467 set_field_type(f[n - 1], TYPE_ALPHA, 1);
469 fname = "First Name";
470 f[n++] = make_label(fname, 2, 20);
471 f[n++] = make_field(fname, 3, 20, 1, 12);
472 set_field_type(f[n - 1], TYPE_ALPHA, 1);
474 fname = "Middle Name";
475 f[n++] = make_label(fname, 2, 34);
476 f[n++] = make_field(fname, 3, 34, 1, 12);
477 set_field_type(f[n - 1], TYPE_ALPHA, 1);
481 f[n++] = make_label(fname, 2, 0);
482 f[n++] = make_field(fname, 3, 0, 1, 18);
483 set_field_type(f[n - 1], TYPE_ALPHA, 1);
485 fname = "First Name";
486 f[n++] = make_label(fname, 2, 20);
487 f[n++] = make_field(fname, 3, 20, 1, 12);
488 set_field_type(f[n - 1], TYPE_ALPHA, 1);
491 f[n++] = make_label(fname, 2, 34);
492 f[n++] = make_field(fname, 3, 34, 1, 1);
493 set_field_pad(f[n - 1], '?');
494 set_field_type(f[n - 1], TYPE_ALPHA, 1);
498 f[n++] = make_label(fname, 2, 0);
499 f[n++] = make_field(fname, 3, 0, 1, 24);
500 set_field_type(f[n - 1], TYPE_ALNUM, 1);
502 #ifdef NCURSES_VERSION
503 fname = "IP Address";
504 f[n++] = make_label(fname, 2, 26);
505 f[n++] = make_field(fname, 3, 26, 1, 16);
506 set_field_type(f[n - 1], TYPE_IPV4, 1);
512 fname = "Four digits";
513 f[n++] = make_label(fname, 2, 0);
514 f[n++] = make_field(fname, 3, 0, 1, 18);
515 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
518 f[n++] = make_label(fname, 2, 20);
519 f[n++] = make_field(fname, 3, 20, 1, 12);
520 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
526 f[n++] = make_label(fname, 5, 0);
527 f[n++] = make_field(fname, 6, 0, 4, 46);
528 init_edit_field(f[n - 1], get_data(fname));
533 if ((form = new_form(f)) != 0) {
538 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
539 show_current_field(also, form);
542 switch (edit_field(form, &c)) {
545 case E_UNKNOWN_COMMAND:
546 finished = my_form_driver(form, c);
552 show_current_field(also, form);
559 for (c = 0; f[c] != 0; c++) {
560 void *ptr = field_userptr(f[c]);
567 #ifdef NCURSES_MOUSE_VERSION
568 mousemask(0, (mmask_t *) 0);
575 static const char *tbl[] =
577 "Usage: demo_forms [options] [data file]"
579 ," -d make fields dynamic"
580 ," -j value justify (1=left, 2=center, 3=right)"
581 ," -m value set maximum size of dynamic fields"
582 ," -o value specify number of offscreen rows in new_field()"
583 ," -t value specify text to fill fields initially"
586 for (j = 0; j < SIZEOF(tbl); ++j)
587 fprintf(stderr, "%s\n", tbl[j]);
592 main(int argc, char *argv[])
596 setlocale(LC_ALL, "");
598 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
604 j_value = atoi(optarg);
605 if (j_value < NO_JUSTIFICATION
606 || j_value > JUSTIFY_RIGHT)
610 m_value = atoi(optarg);
613 o_value = atoi(optarg);
623 while (optind < argc) {
624 read_data(argv[optind++]);
631 nonl(); /* lets us read ^M's */
632 intrflush(stdscr, FALSE);
633 keypad(stdscr, TRUE);
637 init_pair(1, COLOR_WHITE, COLOR_BLUE);
638 init_pair(2, COLOR_GREEN, COLOR_BLACK);
639 init_pair(3, COLOR_CYAN, COLOR_BLACK);
640 bkgd((chtype) COLOR_PAIR(1));
647 ExitProgram(EXIT_SUCCESS);
654 printf("This program requires the curses form library\n");
655 ExitProgram(EXIT_FAILURE);