]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_forms.c
ncurses 6.2 - patch 20200212
[ncurses.git] / test / demo_forms.c
1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 2003-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * $Id: demo_forms.c,v 1.57 2020/02/02 23:34:34 tom Exp $
31  *
32  * Demonstrate a variety of functions from the form library.
33  * Thomas Dickey - 2003/4/26
34  */
35 /*
36 dup_field                       -
37 field_init                      -
38 field_just                      -
39 field_term                      -
40 form_init                       -
41 form_opts                       -
42 form_opts_off                   -
43 form_opts_on                    -
44 form_request_by_name            -
45 form_term                       -
46 form_userptr                    -
47 free_fieldtype                  -
48 link_field                      -
49 link_fieldtype                  -
50 move_field                      -
51 new_page                        -
52 pos_form_cursor                 -
53 set_field_init                  -
54 set_field_term                  -
55 set_fieldtype_arg               -
56 set_fieldtype_choice            -
57 set_form_fields                 -
58 set_form_init                   -
59 set_form_opts                   -
60 set_form_page                   -
61 set_form_term                   -
62 set_form_userptr                -
63 set_max_field                   -
64 */
65
66 #include <test.priv.h>
67
68 #if USE_LIBFORM
69
70 #include <edit_field.h>
71
72 typedef struct {
73     char *name;
74     char *value;
75 } MY_DATA;
76
77 static MY_DATA *my_data;
78
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;
84
85 static void
86 failed(const char *s)
87 {
88     perror(s);
89     ExitProgram(EXIT_FAILURE);
90 }
91
92 static void
93 chomp(char *value)
94 {
95     size_t have = strlen(value);
96     while (have != 0 && (value[have - 1] == '\n' || value[have - 1] == '\r')) {
97         value[--have] = '\0';
98     }
99 }
100
101 static int
102 trimmed(const char *value)
103 {
104     int result = (int) strlen(value);
105     while (result > 0 && isspace(UChar(value[result - 1]))) {
106         --result;
107     }
108     return result;
109 }
110
111 static char *
112 get_data(const char *name)
113 {
114     char *result = t_value;
115     if (my_data != 0) {
116         int n;
117         for (n = 0; my_data[n].name != 0; ++n) {
118             if (!strcmp(name, my_data[n].name)) {
119                 result = my_data[n].value;
120                 break;
121             }
122         }
123     }
124     return result;
125 }
126
127 /*
128  * Read (possibly) multi-line data with name+value pairs.
129  */
130 static void
131 read_data(const char *filename)
132 {
133     FILE *fp = fopen(filename, "r");
134
135     if (fp != 0) {
136         char buffer[BUFSIZ];
137         char *colon;
138         int more = 0;
139         int item = 0;
140
141         my_data = typeCalloc(MY_DATA, (size_t) 100);    /* FIXME */
142         while (fgets(buffer, sizeof(buffer), fp) != 0) {
143             chomp(buffer);
144             if (more) {
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);
149                     if (value == 0)
150                         failed("realloc");
151                     _nc_STRCAT(value, "\n", need);
152                     _nc_STRCAT(value, buffer, need);
153                     my_data[more - 1].value = value;
154                 } else {
155                     more = 0;
156                 }
157             } else if (*buffer == '#') {
158                 continue;
159             } else if ((colon = strchr(buffer, ':')) != 0) {
160                 char *name;
161                 char *value;
162                 *colon++ = '\0';
163                 name = strdup(buffer);
164                 value = strdup(colon);
165                 if (name == 0 || value == 0)
166                     failed("strdup");
167                 my_data[item].name = name;
168                 my_data[item].value = value;
169                 more = ++item;
170             } else {
171                 failed("expected a colon");
172             }
173         }
174         fclose(fp);
175     } else {
176         failed(filename);
177     }
178 }
179
180 static FIELD *
181 make_label(const char *label, int frow, int fcol)
182 {
183     FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
184
185     if (f) {
186         set_field_buffer(f, 0, label);
187         set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
188     }
189     return (f);
190 }
191
192 /*
193  * Define each field with an extra one, for reflecting "actual" text.
194  */
195 static FIELD *
196 make_field(const char *label, int frow, int fcol, int rows, int cols)
197 {
198     FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
199
200     if (f) {
201         set_field_back(f, A_UNDERLINE);
202         /*
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.
206          */
207         set_field_just(f, j_value);
208         if (d_option) {
209             if (has_colors()) {
210                 set_field_fore(f, (chtype) COLOR_PAIR(2));
211                 set_field_back(f, (A_UNDERLINE | (chtype) COLOR_PAIR(3)));
212             } else {
213                 set_field_fore(f, A_BOLD);
214             }
215             /*
216              * The field_opts_off() call dumps core with Solaris curses,
217              * but that is a known bug in Solaris' form library -TD
218              */
219             field_opts_off(f, O_STATIC);
220             set_max_field(f, m_value);
221         }
222
223         init_edit_field(f, get_data(label));
224     }
225     return (f);
226 }
227
228 static void
229 display_form(FORM *f)
230 {
231     WINDOW *w;
232     int rows, cols;
233
234     scale_form(f, &rows, &cols);
235
236     /*
237      * Put the form at the upper-left corner of the display, with just a box
238      * around it.
239      */
240     if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
241         set_form_win(f, w);
242         set_form_sub(f, derwin(w, rows, cols, 1, 2));
243         box(w, 0, 0);
244         keypad(w, TRUE);
245
246         if (post_form(f) != E_OK)
247             wrefresh(w);
248     }
249 }
250
251 static void
252 erase_form(FORM *f)
253 {
254     WINDOW *w = form_win(f);
255     WINDOW *s = form_sub(f);
256
257     unpost_form(f);
258     werase(w);
259     wrefresh(w);
260     delwin(s);
261     delwin(w);
262 }
263
264 static void
265 show_insert_mode(bool insert_mode)
266 {
267     MvAddStr(5, 57, (insert_mode
268                      ? "form_status: insert "
269                      : "form_status: overlay"));
270 }
271
272 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
273
274 static FIELD *
275 another_field(FORM *form, FIELD *field)
276 {
277     FIELD **f = form_fields(form);
278     FIELD *result = 0;
279     int n;
280
281     for (n = 0; f[n] != 0; ++n) {
282         if (f[n] != field) {
283             result = f[n];
284             field_opts_on(result, O_SELECTABLE);
285             break;
286         }
287     }
288     return result;
289 }
290
291 static int
292 my_form_driver(FORM *form, int c)
293 {
294     static bool insert_mode = TRUE;
295     FIELD *field;
296
297     switch (c) {
298     case MY_QUIT:
299         if (form_driver(form, REQ_VALIDATION) == E_OK)
300             return (TRUE);
301         break;
302     case MY_HELP:
303         help_edit_field();
304         break;
305     case MY_EDT_MODE:
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);
311             } else {
312                 field_opts_on(field, O_EDIT);
313             }
314             set_current_field(form, field);
315         }
316         break;
317     case MY_INS_MODE:
318         /* there should be a form_status() function, but there is none */
319         if (!insert_mode) {
320             if (form_driver(form, REQ_INS_MODE) == E_OK) {
321                 insert_mode = TRUE;
322             }
323         } else {
324             if (form_driver(form, REQ_OVL_MODE) == E_OK) {
325                 insert_mode = FALSE;
326             }
327         }
328         show_insert_mode(insert_mode);
329         refresh();
330         break;
331     default:
332         beep();
333         break;
334     }
335     return (FALSE);
336 }
337
338 static void
339 show_current_field(WINDOW *win, FORM *form)
340 {
341     FIELD *field;
342     int field_rows, field_cols, field_max;
343     int currow, curcol;
344
345     if (has_colors()) {
346         wbkgd(win, (chtype) COLOR_PAIR(1));
347     }
348     werase(win);
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");
355     waddch(win, '\n');
356
357     if ((field = current_field(form)) != 0) {
358         FIELDTYPE *type;
359         int nbuf;
360
361         wprintw(win, "Page %d%s, Field %d/%d%s:",
362                 form_page(form),
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");
378 #endif
379             else if (type == TYPE_NUMERIC)
380                 waddstr(win, "NUMERIC");
381             else if (type == TYPE_REGEXP)
382                 waddstr(win, "REGEXP");
383             else
384                 waddstr(win, "other");
385         }
386
387         if ((unsigned) field_opts(field) & O_EDIT)
388             waddstr(win, " editable");
389         else
390             waddstr(win, " readonly");
391
392         if (field_status(field))
393             waddstr(win, " modified");
394
395         if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
396             != ERR) {
397             wprintw(win, " size %dx%d (max %d)",
398                     field_rows, field_cols, field_max);
399         }
400
401         waddch(win, ' ');
402         (void) wattrset(win, AttrArg(field_fore(field), 0));
403         waddstr(win, "fore");
404         wattroff(win, (int) field_fore(field));
405
406         waddch(win, '/');
407
408         (void) wattrset(win, AttrArg(field_back(field), 0));
409         waddstr(win, "back");
410         wattroff(win, (int) field_back(field));
411
412         wprintw(win, ", pad '%c'", field_pad(field));
413
414         waddstr(win, "\n");
415         for (nbuf = 0; nbuf <= 2; ++nbuf) {
416             char *buffer;
417             if ((buffer = field_buffer(field, nbuf)) != 0) {
418                 wprintw(win, "buffer %d:", nbuf);
419                 (void) wattrset(win, A_REVERSE);
420                 if (nbuf) {
421                     waddnstr(win, buffer, trimmed(buffer));
422                 } else {
423                     waddstr(win, buffer);
424                 }
425                 wattroff(win, A_REVERSE);
426                 waddstr(win, "\n");
427             }
428         }
429     }
430     wrefresh(win);
431 }
432
433 static void
434 demo_forms(void)
435 {
436     FORM *form;
437     FIELD *f[100];              /* will memset to zero */
438     int c;
439     unsigned n = 0;
440     int pg;
441     const char *fname;
442     static const char *my_enum[] =
443     {"first", "second", "third", 0};
444
445 #ifdef NCURSES_MOUSE_VERSION
446     mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
447 #endif
448
449     help_edit_field();
450
451     MvAddStr(4, 57, "Forms Entry Test");
452     show_insert_mode(TRUE);
453
454     refresh();
455
456     /* describe the form */
457     memset(f, 0, sizeof(f));
458     for (pg = 0; pg < 4; ++pg) {
459         char label[80];
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);
464
465         switch (pg) {
466         default:
467             fname = "Last Name";
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);
471
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);
476
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);
481             break;
482
483         case 1:
484             fname = "Last Name";
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);
488
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);
493
494             fname = "MI";
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);
499
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);
504             break;
505
506         case 2:
507             fname = "Host Name";
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);
511
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);
517 #endif
518             break;
519
520         case 3:
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);
525
526             fname = "Numeric";
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);
530
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} *$");;
536             break;
537         }
538
539         fname = "Comments";
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));
543     }
544
545     f[n] = (FIELD *) 0;
546
547     if ((form = new_form(f)) != 0) {
548         WINDOW *w;
549         WINDOW *also;
550         int finished = 0;
551
552         display_form(form);
553
554         w = form_win(form);
555         also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
556         show_current_field(also, form);
557
558         while (!finished) {
559             switch (edit_field(form, &c)) {
560             case E_OK:
561                 break;
562             case E_UNKNOWN_COMMAND:
563                 finished = my_form_driver(form, c);
564                 break;
565             default:
566                 beep();
567                 break;
568             }
569             show_current_field(also, form);
570         }
571
572         erase_form(form);
573
574         free_form(form);
575     }
576     for (c = 0; f[c] != 0; c++) {
577         free_edit_field(f[c]);
578         free_field(f[c]);
579     }
580     noraw();
581     nl();
582
583 #ifdef NCURSES_MOUSE_VERSION
584     mousemask(0, (mmask_t *) 0);
585 #endif
586 }
587
588 static void
589 usage(void)
590 {
591     static const char *tbl[] =
592     {
593         "Usage: demo_forms [options] [data file]"
594         ,""
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"
600     };
601     unsigned int j;
602     for (j = 0; j < SIZEOF(tbl); ++j)
603         fprintf(stderr, "%s\n", tbl[j]);
604     exit(EXIT_FAILURE);
605 }
606
607 int
608 main(int argc, char *argv[])
609 {
610     int ch;
611
612     setlocale(LC_ALL, "");
613
614     while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
615         switch (ch) {
616         case 'd':
617             d_option = TRUE;
618             break;
619         case 'j':
620             j_value = atoi(optarg);
621             if (j_value < NO_JUSTIFICATION
622                 || j_value > JUSTIFY_RIGHT)
623                 usage();
624             break;
625         case 'm':
626             m_value = atoi(optarg);
627             break;
628         case 'o':
629             o_value = atoi(optarg);
630             break;
631         case 't':
632             t_value = optarg;
633             break;
634         default:
635             usage();
636
637         }
638     }
639     while (optind < argc) {
640         read_data(argv[optind++]);
641     }
642
643     initscr();
644     cbreak();
645     noecho();
646     raw();
647     nonl();                     /* lets us read ^M's */
648     intrflush(stdscr, FALSE);
649     keypad(stdscr, TRUE);
650
651     if (has_colors()) {
652         start_color();
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));
657         refresh();
658     }
659
660     demo_forms();
661
662     endwin();
663     ExitProgram(EXIT_SUCCESS);
664 }
665
666 #else
667 int
668 main(void)
669 {
670     printf("This program requires the curses form library\n");
671     ExitProgram(EXIT_FAILURE);
672 }
673 #endif