]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_forms.c
ncurses 6.0 - patch 20171125
[ncurses.git] / test / demo_forms.c
1 /****************************************************************************
2  * Copyright (c) 2003-2016,2017 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.53 2017/04/10 00:28:54 tom Exp $
30  *
31  * Demonstrate a variety of functions from the form library.
32  * Thomas Dickey - 2003/4/26
33  */
34 /*
35 TYPE_ENUM                       -
36 TYPE_REGEXP                     -
37 dup_field                       -
38 field_init                      -
39 field_just                      -
40 field_term                      -
41 form_init                       -
42 form_opts                       -
43 form_opts_off                   -
44 form_opts_on                    -
45 form_request_by_name            -
46 form_term                       -
47 form_userptr                    -
48 free_fieldtype                  -
49 link_field                      -
50 link_fieldtype                  -
51 move_field                      -
52 new_page                        -
53 pos_form_cursor                 -
54 set_field_init                  -
55 set_field_term                  -
56 set_fieldtype_arg               -
57 set_fieldtype_choice            -
58 set_form_fields                 -
59 set_form_init                   -
60 set_form_opts                   -
61 set_form_page                   -
62 set_form_term                   -
63 set_form_userptr                -
64 set_max_field                   -
65 */
66
67 #include <test.priv.h>
68
69 #if USE_LIBFORM
70
71 #include <edit_field.h>
72
73 typedef struct {
74     char *name;
75     char *value;
76 } MY_DATA;
77
78 static MY_DATA *my_data;
79
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;
85
86 static void
87 failed(const char *s)
88 {
89     perror(s);
90     ExitProgram(EXIT_FAILURE);
91 }
92
93 static void
94 chomp(char *value)
95 {
96     size_t have = strlen(value);
97     while (have != 0 && (value[have - 1] == '\n' || value[have - 1] == '\r')) {
98         value[--have] = '\0';
99     }
100 }
101
102 static int
103 trimmed(const char *value)
104 {
105     int result = (int) strlen(value);
106     while (result > 0 && isspace(UChar(value[result - 1]))) {
107         --result;
108     }
109     return result;
110 }
111
112 static char *
113 get_data(const char *name)
114 {
115     char *result = t_value;
116     if (my_data != 0) {
117         int n;
118         for (n = 0; my_data[n].name != 0; ++n) {
119             if (!strcmp(name, my_data[n].name)) {
120                 result = my_data[n].value;
121                 break;
122             }
123         }
124     }
125     return result;
126 }
127
128 /*
129  * Read (possibly) multi-line data with name+value pairs.
130  */
131 static void
132 read_data(const char *filename)
133 {
134     FILE *fp = fopen(filename, "r");
135
136     if (fp != 0) {
137         char buffer[BUFSIZ];
138         char *colon;
139         int more = 0;
140         int item = 0;
141
142         my_data = typeCalloc(MY_DATA, (size_t) 100);    /* FIXME */
143         while (fgets(buffer, sizeof(buffer), fp) != 0) {
144             chomp(buffer);
145             if (more) {
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);
150                     if (value == 0)
151                         failed("realloc");
152                     _nc_STRCAT(value, "\n", need);
153                     _nc_STRCAT(value, buffer, need);
154                     my_data[more - 1].value = value;
155                 } else {
156                     more = 0;
157                 }
158             } else if (*buffer == '#') {
159                 continue;
160             } else if ((colon = strchr(buffer, ':')) != 0) {
161                 char *name;
162                 char *value;
163                 *colon++ = '\0';
164                 name = strdup(buffer);
165                 value = strdup(colon);
166                 if (name == 0 || value == 0)
167                     failed("strdup");
168                 my_data[item].name = name;
169                 my_data[item].value = value;
170                 more = ++item;
171             } else {
172                 failed("expected a colon");
173             }
174         }
175         fclose(fp);
176     } else {
177         failed(filename);
178     }
179 }
180
181 static FIELD *
182 make_label(const char *label, int frow, int fcol)
183 {
184     FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
185
186     if (f) {
187         set_field_buffer(f, 0, label);
188         set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
189     }
190     return (f);
191 }
192
193 /*
194  * Define each field with an extra one, for reflecting "actual" text.
195  */
196 static FIELD *
197 make_field(const char *label, int frow, int fcol, int rows, int cols)
198 {
199     FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
200
201     if (f) {
202         set_field_back(f, A_UNDERLINE);
203         /*
204          * If -j and -d options are combined, -j loses.  It is documented in
205          * "Character User Interface Programming", page 12-15 that setting
206          * O_STATIC off makes the form library ignore justification.
207          */
208         set_field_just(f, j_value);
209         if (d_option) {
210             if (has_colors()) {
211                 set_field_fore(f, (chtype) COLOR_PAIR(2));
212                 set_field_back(f, (A_UNDERLINE | (chtype) COLOR_PAIR(3)));
213             } else {
214                 set_field_fore(f, A_BOLD);
215             }
216             /*
217              * The field_opts_off() call dumps core with Solaris curses,
218              * but that is a known bug in Solaris' form library -TD
219              */
220             field_opts_off(f, O_STATIC);
221             set_max_field(f, m_value);
222         }
223
224         init_edit_field(f, get_data(label));
225     }
226     return (f);
227 }
228
229 static void
230 display_form(FORM *f)
231 {
232     WINDOW *w;
233     int rows, cols;
234
235     scale_form(f, &rows, &cols);
236
237     /*
238      * Put the form at the upper-left corner of the display, with just a box
239      * around it.
240      */
241     if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
242         set_form_win(f, w);
243         set_form_sub(f, derwin(w, rows, cols, 1, 2));
244         box(w, 0, 0);
245         keypad(w, TRUE);
246
247         if (post_form(f) != E_OK)
248             wrefresh(w);
249     }
250 }
251
252 static void
253 erase_form(FORM *f)
254 {
255     WINDOW *w = form_win(f);
256     WINDOW *s = form_sub(f);
257
258     unpost_form(f);
259     werase(w);
260     wrefresh(w);
261     delwin(s);
262     delwin(w);
263 }
264
265 static void
266 show_insert_mode(bool insert_mode)
267 {
268     MvAddStr(5, 57, (insert_mode
269                      ? "form_status: insert "
270                      : "form_status: overlay"));
271 }
272
273 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
274
275 static FIELD *
276 another_field(FORM *form, FIELD *field)
277 {
278     FIELD **f = form_fields(form);
279     FIELD *result = 0;
280     int n;
281
282     for (n = 0; f[n] != 0; ++n) {
283         if (f[n] != field) {
284             result = f[n];
285             field_opts_on(result, O_SELECTABLE);
286             break;
287         }
288     }
289     return result;
290 }
291
292 static int
293 my_form_driver(FORM *form, int c)
294 {
295     static bool insert_mode = TRUE;
296     FIELD *field;
297
298     switch (c) {
299     case MY_QUIT:
300         if (form_driver(form, REQ_VALIDATION) == E_OK)
301             return (TRUE);
302         break;
303     case MY_HELP:
304         help_edit_field();
305         break;
306     case MY_EDT_MODE:
307         if ((field = current_field(form)) != 0) {
308             set_current_field(form, another_field(form, field));
309             if ((unsigned) field_opts(field) & O_EDIT) {
310                 field_opts_off(field, O_EDIT);
311                 set_field_status(field, 0);
312             } else {
313                 field_opts_on(field, O_EDIT);
314             }
315             set_current_field(form, field);
316         }
317         break;
318     case MY_INS_MODE:
319         /* there should be a form_status() function, but there is none */
320         if (!insert_mode) {
321             if (form_driver(form, REQ_INS_MODE) == E_OK) {
322                 insert_mode = TRUE;
323             }
324         } else {
325             if (form_driver(form, REQ_OVL_MODE) == E_OK) {
326                 insert_mode = FALSE;
327             }
328         }
329         show_insert_mode(insert_mode);
330         refresh();
331         break;
332     default:
333         beep();
334         break;
335     }
336     return (FALSE);
337 }
338
339 static void
340 show_current_field(WINDOW *win, FORM *form)
341 {
342     FIELD *field;
343     FIELDTYPE *type;
344     char *buffer;
345     int nbuf;
346     int field_rows, field_cols, field_max;
347     int currow, curcol;
348
349     if (has_colors()) {
350         wbkgd(win, (chtype) COLOR_PAIR(1));
351     }
352     werase(win);
353     form_getyx(form, currow, curcol);
354     wprintw(win, "Cursor: %d,%d", currow, curcol);
355     if (data_ahead(form))
356         waddstr(win, " ahead");
357     if (data_behind(form))
358         waddstr(win, " behind");
359     waddch(win, '\n');
360     if ((field = current_field(form)) != 0) {
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             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     WINDOW *w;
436     FORM *form;
437     FIELD *f[100];              /* will memset to zero */
438     int finished = 0, c;
439     unsigned n = 0;
440     int pg;
441     WINDOW *also;
442     const char *fname;
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         case 1:
482             fname = "Last Name";
483             f[n++] = make_label(fname, 2, 0);
484             f[n++] = make_field(fname, 3, 0, 1, 18);
485             set_field_type(f[n - 1], TYPE_ALPHA, 1);
486
487             fname = "First Name";
488             f[n++] = make_label(fname, 2, 20);
489             f[n++] = make_field(fname, 3, 20, 1, 12);
490             set_field_type(f[n - 1], TYPE_ALPHA, 1);
491
492             fname = "MI";
493             f[n++] = make_label(fname, 2, 34);
494             f[n++] = make_field(fname, 3, 34, 1, 1);
495             set_field_pad(f[n - 1], '?');
496             set_field_type(f[n - 1], TYPE_ALPHA, 1);
497             break;
498         case 2:
499             fname = "Host Name";
500             f[n++] = make_label(fname, 2, 0);
501             f[n++] = make_field(fname, 3, 0, 1, 24);
502             set_field_type(f[n - 1], TYPE_ALNUM, 1);
503
504 #ifdef NCURSES_VERSION
505             fname = "IP Address";
506             f[n++] = make_label(fname, 2, 26);
507             f[n++] = make_field(fname, 3, 26, 1, 16);
508             set_field_type(f[n - 1], TYPE_IPV4, 1);
509 #endif
510
511             break;
512
513         case 3:
514             fname = "Four digits";
515             f[n++] = make_label(fname, 2, 0);
516             f[n++] = make_field(fname, 3, 0, 1, 18);
517             set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
518
519             fname = "Numeric";
520             f[n++] = make_label(fname, 2, 20);
521             f[n++] = make_field(fname, 3, 20, 1, 12);
522             set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
523
524             break;
525         }
526
527         fname = "Comments";
528         f[n++] = make_label(fname, 5, 0);
529         f[n++] = make_field(fname, 6, 0, 4, 46);
530         init_edit_field(f[n - 1], get_data(fname));
531     }
532
533     f[n] = (FIELD *) 0;
534
535     if ((form = new_form(f)) != 0) {
536
537         display_form(form);
538
539         w = form_win(form);
540         also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
541         show_current_field(also, form);
542
543         while (!finished) {
544             switch (edit_field(form, &c)) {
545             case E_OK:
546                 break;
547             case E_UNKNOWN_COMMAND:
548                 finished = my_form_driver(form, c);
549                 break;
550             default:
551                 beep();
552                 break;
553             }
554             show_current_field(also, form);
555         }
556
557         erase_form(form);
558
559         free_form(form);
560     }
561     for (c = 0; f[c] != 0; c++) {
562         free_edit_field(f[c]);
563         free_field(f[c]);
564     }
565     noraw();
566     nl();
567
568 #ifdef NCURSES_MOUSE_VERSION
569     mousemask(0, (mmask_t *) 0);
570 #endif
571 }
572
573 static void
574 usage(void)
575 {
576     static const char *tbl[] =
577     {
578         "Usage: demo_forms [options] [data file]"
579         ,""
580         ," -d        make fields dynamic"
581         ," -j value  justify (1=left, 2=center, 3=right)"
582         ," -m value  set maximum size of dynamic fields"
583         ," -o value  specify number of offscreen rows in new_field()"
584         ," -t value  specify text to fill fields initially"
585     };
586     unsigned int j;
587     for (j = 0; j < SIZEOF(tbl); ++j)
588         fprintf(stderr, "%s\n", tbl[j]);
589     exit(EXIT_FAILURE);
590 }
591
592 int
593 main(int argc, char *argv[])
594 {
595     int ch;
596
597     setlocale(LC_ALL, "");
598
599     while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
600         switch (ch) {
601         case 'd':
602             d_option = TRUE;
603             break;
604         case 'j':
605             j_value = atoi(optarg);
606             if (j_value < NO_JUSTIFICATION
607                 || j_value > JUSTIFY_RIGHT)
608                 usage();
609             break;
610         case 'm':
611             m_value = atoi(optarg);
612             break;
613         case 'o':
614             o_value = atoi(optarg);
615             break;
616         case 't':
617             t_value = optarg;
618             break;
619         default:
620             usage();
621
622         }
623     }
624     while (optind < argc) {
625         read_data(argv[optind++]);
626     }
627
628     initscr();
629     cbreak();
630     noecho();
631     raw();
632     nonl();                     /* lets us read ^M's */
633     intrflush(stdscr, FALSE);
634     keypad(stdscr, TRUE);
635
636     if (has_colors()) {
637         start_color();
638         init_pair(1, COLOR_WHITE, COLOR_BLUE);
639         init_pair(2, COLOR_GREEN, COLOR_BLACK);
640         init_pair(3, COLOR_CYAN, COLOR_BLACK);
641         bkgd((chtype) COLOR_PAIR(1));
642         refresh();
643     }
644
645     demo_forms();
646
647     endwin();
648     ExitProgram(EXIT_SUCCESS);
649 }
650
651 #else
652 int
653 main(void)
654 {
655     printf("This program requires the curses form library\n");
656     ExitProgram(EXIT_FAILURE);
657 }
658 #endif