]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_forms.c
ncurses 5.9 - patch 20130928
[ncurses.git] / test / demo_forms.c
1 /****************************************************************************
2  * Copyright (c) 2003-2012,2013 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.46 2013/09/28 21:55: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                     strcat(value, "\n");
153                     strcat(value, buffer);
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     } 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 | 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     FIELDTYPE *type;
343     char *buffer;
344     int nbuf;
345     int field_rows, field_cols, field_max;
346     int currow, curcol;
347
348     if (has_colors()) {
349         wbkgd(win, (chtype) COLOR_PAIR(1));
350     }
351     werase(win);
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");
358     waddch(win, '\n');
359     if ((field = current_field(form)) != 0) {
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, (int) field_fore(field));
402         waddstr(win, "fore");
403         wattroff(win, (int) field_fore(field));
404
405         waddch(win, '/');
406
407         (void) wattrset(win, (int) field_back(field));
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             if ((buffer = field_buffer(field, nbuf)) != 0) {
416                 wprintw(win, "buffer %d:", nbuf);
417                 (void) wattrset(win, A_REVERSE);
418                 if (nbuf) {
419                     waddnstr(win, buffer, trimmed(buffer));
420                 } else {
421                     waddstr(win, buffer);
422                 }
423                 wattroff(win, A_REVERSE);
424                 waddstr(win, "\n");
425             }
426         }
427     }
428     wrefresh(win);
429 }
430
431 static void
432 demo_forms(void)
433 {
434     WINDOW *w;
435     FORM *form;
436     FIELD *f[100];              /* will memset to zero */
437     int finished = 0, c;
438     unsigned n = 0;
439     int pg;
440     WINDOW *also;
441     const char *fname;
442
443 #ifdef NCURSES_MOUSE_VERSION
444     mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
445 #endif
446
447     help_edit_field();
448
449     MvAddStr(4, 57, "Forms Entry Test");
450     show_insert_mode(TRUE);
451
452     refresh();
453
454     /* describe the form */
455     memset(f, 0, sizeof(f));
456     for (pg = 0; pg < 4; ++pg) {
457         char label[80];
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);
461
462         switch (pg) {
463         default:
464             fname = "Last Name";
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);
468
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);
473
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);
478             break;
479         case 1:
480             fname = "Last Name";
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);
484
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);
489
490             fname = "MI";
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);
495             break;
496         case 2:
497             fname = "Host Name";
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);
501
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);
507 #endif
508
509             break;
510
511         case 3:
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);
516
517             fname = "Numeric";
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);
521
522             break;
523         }
524
525         fname = "Comments";
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));
529     }
530
531     f[n] = (FIELD *) 0;
532
533     if ((form = new_form(f)) != 0) {
534
535         display_form(form);
536
537         w = form_win(form);
538         also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
539         show_current_field(also, form);
540
541         while (!finished) {
542             switch (edit_field(form, &c)) {
543             case E_OK:
544                 break;
545             case E_UNKNOWN_COMMAND:
546                 finished = my_form_driver(form, c);
547                 break;
548             default:
549                 beep();
550                 break;
551             }
552             show_current_field(also, form);
553         }
554
555         erase_form(form);
556
557         free_form(form);
558     }
559     for (c = 0; f[c] != 0; c++) {
560         void *ptr = field_userptr(f[c]);
561         free(ptr);
562         free_field(f[c]);
563     }
564     noraw();
565     nl();
566
567 #ifdef NCURSES_MOUSE_VERSION
568     mousemask(0, (mmask_t *) 0);
569 #endif
570 }
571
572 static void
573 usage(void)
574 {
575     static const char *tbl[] =
576     {
577         "Usage: demo_forms [options] [data file]"
578         ,""
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"
584     };
585     unsigned int j;
586     for (j = 0; j < SIZEOF(tbl); ++j)
587         fprintf(stderr, "%s\n", tbl[j]);
588     exit(EXIT_FAILURE);
589 }
590
591 int
592 main(int argc, char *argv[])
593 {
594     int ch;
595
596     setlocale(LC_ALL, "");
597
598     while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
599         switch (ch) {
600         case 'd':
601             d_option = TRUE;
602             break;
603         case 'j':
604             j_value = atoi(optarg);
605             if (j_value < NO_JUSTIFICATION
606                 || j_value > JUSTIFY_RIGHT)
607                 usage();
608             break;
609         case 'm':
610             m_value = atoi(optarg);
611             break;
612         case 'o':
613             o_value = atoi(optarg);
614             break;
615         case 't':
616             t_value = optarg;
617             break;
618         default:
619             usage();
620
621         }
622     }
623     while (optind < argc) {
624         read_data(argv[optind++]);
625     }
626
627     initscr();
628     cbreak();
629     noecho();
630     raw();
631     nonl();                     /* lets us read ^M's */
632     intrflush(stdscr, FALSE);
633     keypad(stdscr, TRUE);
634
635     if (has_colors()) {
636         start_color();
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));
641         refresh();
642     }
643
644     demo_forms();
645
646     endwin();
647     ExitProgram(EXIT_SUCCESS);
648 }
649
650 #else
651 int
652 main(void)
653 {
654     printf("This program requires the curses form library\n");
655     ExitProgram(EXIT_FAILURE);
656 }
657 #endif