]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_forms.c
ncurses 6.1 - patch 20190302
[ncurses.git] / test / demo_forms.c
1 /****************************************************************************
2  * Copyright (c) 2003-2017,2018 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.55 2018/07/14 23:26:02 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     FIELDTYPE *type;
342     char *buffer;
343     int nbuf;
344     int field_rows, field_cols, field_max;
345     int currow, curcol;
346
347     if (has_colors()) {
348         wbkgd(win, (chtype) COLOR_PAIR(1));
349     }
350     werase(win);
351     form_getyx(form, currow, curcol);
352     wprintw(win, "Cursor: %d,%d", currow, curcol);
353     if (data_ahead(form))
354         waddstr(win, " ahead");
355     if (data_behind(form))
356         waddstr(win, " behind");
357     waddch(win, '\n');
358     if ((field = current_field(form)) != 0) {
359         wprintw(win, "Page %d%s, Field %d/%d%s:",
360                 form_page(form),
361                 new_page(field) ? "*" : "",
362                 field_index(field), field_count(form),
363                 field_arg(field) ? "(arg)" : "");
364         if ((type = field_type(field)) != 0) {
365             if (type == TYPE_ALNUM)
366                 waddstr(win, "ALNUM");
367             else if (type == TYPE_ALPHA)
368                 waddstr(win, "ALPHA");
369             else if (type == TYPE_ENUM)
370                 waddstr(win, "ENUM");
371             else if (type == TYPE_INTEGER)
372                 waddstr(win, "INTEGER");
373 #ifdef NCURSES_VERSION
374             else if (type == TYPE_IPV4)
375                 waddstr(win, "IPV4");
376 #endif
377             else if (type == TYPE_NUMERIC)
378                 waddstr(win, "NUMERIC");
379             else if (type == TYPE_REGEXP)
380                 waddstr(win, "REGEXP");
381             else
382                 waddstr(win, "other");
383         }
384
385         if ((unsigned) field_opts(field) & O_EDIT)
386             waddstr(win, " editable");
387         else
388             waddstr(win, " readonly");
389
390         if (field_status(field))
391             waddstr(win, " modified");
392
393         if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
394             != ERR) {
395             wprintw(win, " size %dx%d (max %d)",
396                     field_rows, field_cols, field_max);
397         }
398
399         waddch(win, ' ');
400         (void) wattrset(win, AttrArg(field_fore(field), 0));
401         waddstr(win, "fore");
402         wattroff(win, (int) field_fore(field));
403
404         waddch(win, '/');
405
406         (void) wattrset(win, AttrArg(field_back(field), 0));
407         waddstr(win, "back");
408         wattroff(win, (int) field_back(field));
409
410         wprintw(win, ", pad '%c'", field_pad(field));
411
412         waddstr(win, "\n");
413         for (nbuf = 0; nbuf <= 2; ++nbuf) {
414             if ((buffer = field_buffer(field, nbuf)) != 0) {
415                 wprintw(win, "buffer %d:", nbuf);
416                 (void) wattrset(win, A_REVERSE);
417                 if (nbuf) {
418                     waddnstr(win, buffer, trimmed(buffer));
419                 } else {
420                     waddstr(win, buffer);
421                 }
422                 wattroff(win, A_REVERSE);
423                 waddstr(win, "\n");
424             }
425         }
426     }
427     wrefresh(win);
428 }
429
430 static void
431 demo_forms(void)
432 {
433     WINDOW *w;
434     FORM *form;
435     FIELD *f[100];              /* will memset to zero */
436     int finished = 0, c;
437     unsigned n = 0;
438     int pg;
439     WINDOW *also;
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
548         display_form(form);
549
550         w = form_win(form);
551         also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
552         show_current_field(also, form);
553
554         while (!finished) {
555             switch (edit_field(form, &c)) {
556             case E_OK:
557                 break;
558             case E_UNKNOWN_COMMAND:
559                 finished = my_form_driver(form, c);
560                 break;
561             default:
562                 beep();
563                 break;
564             }
565             show_current_field(also, form);
566         }
567
568         erase_form(form);
569
570         free_form(form);
571     }
572     for (c = 0; f[c] != 0; c++) {
573         free_edit_field(f[c]);
574         free_field(f[c]);
575     }
576     noraw();
577     nl();
578
579 #ifdef NCURSES_MOUSE_VERSION
580     mousemask(0, (mmask_t *) 0);
581 #endif
582 }
583
584 static void
585 usage(void)
586 {
587     static const char *tbl[] =
588     {
589         "Usage: demo_forms [options] [data file]"
590         ,""
591         ," -d        make fields dynamic"
592         ," -j value  justify (1=left, 2=center, 3=right)"
593         ," -m value  set maximum size of dynamic fields"
594         ," -o value  specify number of offscreen rows in new_field()"
595         ," -t value  specify text to fill fields initially"
596     };
597     unsigned int j;
598     for (j = 0; j < SIZEOF(tbl); ++j)
599         fprintf(stderr, "%s\n", tbl[j]);
600     exit(EXIT_FAILURE);
601 }
602
603 int
604 main(int argc, char *argv[])
605 {
606     int ch;
607
608     setlocale(LC_ALL, "");
609
610     while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
611         switch (ch) {
612         case 'd':
613             d_option = TRUE;
614             break;
615         case 'j':
616             j_value = atoi(optarg);
617             if (j_value < NO_JUSTIFICATION
618                 || j_value > JUSTIFY_RIGHT)
619                 usage();
620             break;
621         case 'm':
622             m_value = atoi(optarg);
623             break;
624         case 'o':
625             o_value = atoi(optarg);
626             break;
627         case 't':
628             t_value = optarg;
629             break;
630         default:
631             usage();
632
633         }
634     }
635     while (optind < argc) {
636         read_data(argv[optind++]);
637     }
638
639     initscr();
640     cbreak();
641     noecho();
642     raw();
643     nonl();                     /* lets us read ^M's */
644     intrflush(stdscr, FALSE);
645     keypad(stdscr, TRUE);
646
647     if (has_colors()) {
648         start_color();
649         init_pair(1, COLOR_WHITE, COLOR_BLUE);
650         init_pair(2, COLOR_GREEN, COLOR_BLACK);
651         init_pair(3, COLOR_CYAN, COLOR_BLACK);
652         bkgd((chtype) COLOR_PAIR(1));
653         refresh();
654     }
655
656     demo_forms();
657
658     endwin();
659     ExitProgram(EXIT_SUCCESS);
660 }
661
662 #else
663 int
664 main(void)
665 {
666     printf("This program requires the curses form library\n");
667     ExitProgram(EXIT_FAILURE);
668 }
669 #endif