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