1 /****************************************************************************
2 * Copyright (c) 2003-2009,2010 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: demo_forms.c,v 1.36 2010/11/13 20:49:50 tom Exp $
31 * Demonstrate a variety of functions from the form library.
32 * Thomas Dickey - 2003/4/26
45 form_request_by_name -
57 set_fieldtype_choice -
67 #include <test.priv.h>
71 #include <edit_field.h>
73 static int d_option = 0;
74 static int j_value = 0;
75 static int m_value = 0;
76 static int o_value = 0;
77 static char *t_value = 0;
80 make_label(int frow, int fcol, NCURSES_CONST char *label)
82 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
85 set_field_buffer(f, 0, label);
86 set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
92 * Define each field with an extra one, for reflecting "actual" text.
95 make_field(int frow, int fcol, int rows, int cols)
97 FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
102 set_field_back(f, A_UNDERLINE);
104 * If -j and -d options are combined, -j loses. It is documented in
105 * "Character User Interface Programming", page 12-15 that setting
106 * O_STATIC off makes the form library ignore justification.
108 set_field_just(f, j_value);
111 set_field_fore(f, COLOR_PAIR(2));
112 set_field_back(f, A_UNDERLINE | COLOR_PAIR(3));
114 set_field_fore(f, A_BOLD);
117 * The field_opts_off() call dumps core with Solaris curses,
118 * but that is a known bug in Solaris' form library -TD
120 field_opts_off(f, O_STATIC);
121 set_max_field(f, m_value);
125 * The userptr is used in edit_field.c's inactive_field().
127 ptr = (FieldAttrs *) field_userptr(f);
129 ptr = typeCalloc(FieldAttrs, 1);
130 ptr->background = field_back(f);
132 set_field_userptr(f, (void *) ptr);
134 set_field_buffer(f, 0, t_value);
140 display_form(FORM * f)
145 scale_form(f, &rows, &cols);
148 * Put the form at the upper-left corner of the display, with just a box
151 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
153 set_form_sub(f, derwin(w, rows, cols, 1, 2));
158 if (post_form(f) != E_OK)
165 WINDOW *w = form_win(f);
166 WINDOW *s = form_sub(f);
176 show_insert_mode(bool insert_mode)
178 MvAddStr(5, 57, (insert_mode
179 ? "form_status: insert "
180 : "form_status: overlay"));
183 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
186 another_field(FORM * form, FIELD * field)
188 FIELD **f = form_fields(form);
192 for (n = 0; f[n] != 0; ++n) {
195 field_opts_on(result, O_SELECTABLE);
203 my_form_driver(FORM * form, int c)
205 static bool insert_mode = TRUE;
210 if (form_driver(form, REQ_VALIDATION) == E_OK)
217 if ((field = current_field(form)) != 0) {
218 set_current_field(form, another_field(form, field));
219 if ((unsigned) field_opts(field) & O_EDIT) {
220 field_opts_off(field, O_EDIT);
221 set_field_status(field, 0);
223 field_opts_on(field, O_EDIT);
225 set_current_field(form, field);
229 /* there should be a form_status() function, but there is none */
231 if (form_driver(form, REQ_INS_MODE) == E_OK) {
235 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
239 show_insert_mode(insert_mode);
250 show_current_field(WINDOW *win, FORM * form)
256 int field_rows, field_cols, field_max;
259 wbkgd(win, COLOR_PAIR(1));
262 wprintw(win, "Cursor: %d,%d", form->currow, form->curcol);
263 if (data_ahead(form))
264 waddstr(win, " ahead");
265 if (data_behind(form))
266 waddstr(win, " behind");
268 if ((field = current_field(form)) != 0) {
269 wprintw(win, "Page %d%s, Field %d/%d%s:",
271 new_page(field) ? "*" : "",
272 field_index(field), field_count(form),
273 field_arg(field) ? "(arg)" : "");
274 if ((type = field_type(field)) != 0) {
275 if (type == TYPE_ALNUM)
276 waddstr(win, "ALNUM");
277 else if (type == TYPE_ALPHA)
278 waddstr(win, "ALPHA");
279 else if (type == TYPE_ENUM)
280 waddstr(win, "ENUM");
281 else if (type == TYPE_INTEGER)
282 waddstr(win, "INTEGER");
283 #ifdef NCURSES_VERSION
284 else if (type == TYPE_IPV4)
285 waddstr(win, "IPV4");
287 else if (type == TYPE_NUMERIC)
288 waddstr(win, "NUMERIC");
289 else if (type == TYPE_REGEXP)
290 waddstr(win, "REGEXP");
292 waddstr(win, "other");
295 if ((unsigned) field_opts(field) & O_EDIT)
296 waddstr(win, " editable");
298 waddstr(win, " readonly");
300 if (field_status(field))
301 waddstr(win, " modified");
303 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
305 wprintw(win, " size %dx%d (max %d)",
306 field_rows, field_cols, field_max);
310 (void) wattrset(win, field_fore(field));
311 waddstr(win, "fore");
312 wattroff(win, field_fore(field));
316 (void) wattrset(win, field_back(field));
317 waddstr(win, "back");
318 wattroff(win, field_back(field));
320 wprintw(win, ", pad '%c'",
324 for (nbuf = 0; nbuf <= 2; ++nbuf) {
325 if ((buffer = field_buffer(field, nbuf)) != 0) {
326 wprintw(win, "buffer %d:", nbuf);
327 (void) wattrset(win, A_REVERSE);
328 waddstr(win, buffer);
329 wattroff(win, A_REVERSE);
342 FIELD *f[100]; /* FIXME memset to zero */
348 #ifdef NCURSES_MOUSE_VERSION
349 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
354 MvAddStr(4, 57, "Forms Entry Test");
355 show_insert_mode(TRUE);
359 /* describe the form */
360 memset(f, 0, sizeof(f));
361 for (pg = 0; pg < 4; ++pg) {
363 sprintf(label, "Sample Form Page %d", pg + 1);
364 f[n++] = make_label(0, 15, label);
365 set_new_page(f[n - 1], TRUE);
369 f[n++] = make_label(2, 0, "Last Name");
370 f[n++] = make_field(3, 0, 1, 18);
371 set_field_type(f[n - 1], TYPE_ALPHA, 1);
373 f[n++] = make_label(2, 20, "First Name");
374 f[n++] = make_field(3, 20, 1, 12);
375 set_field_type(f[n - 1], TYPE_ALPHA, 1);
377 f[n++] = make_label(2, 34, "Middle Name");
378 f[n++] = make_field(3, 34, 1, 12);
379 set_field_type(f[n - 1], TYPE_ALPHA, 1);
382 f[n++] = make_label(2, 0, "Last Name");
383 f[n++] = make_field(3, 0, 1, 18);
384 set_field_type(f[n - 1], TYPE_ALPHA, 1);
386 f[n++] = make_label(2, 20, "First Name");
387 f[n++] = make_field(3, 20, 1, 12);
388 set_field_type(f[n - 1], TYPE_ALPHA, 1);
390 f[n++] = make_label(2, 34, "MI");
391 f[n++] = make_field(3, 34, 1, 1);
392 set_field_pad(f[n - 1], '?');
393 set_field_type(f[n - 1], TYPE_ALPHA, 1);
396 f[n++] = make_label(2, 0, "Host Name");
397 f[n++] = make_field(3, 0, 1, 18);
398 set_field_type(f[n - 1], TYPE_ALNUM, 1);
400 #ifdef NCURSES_VERSION
401 f[n++] = make_label(2, 20, "IP Address");
402 f[n++] = make_field(3, 20, 1, 12);
403 set_field_type(f[n - 1], TYPE_IPV4, 1);
409 f[n++] = make_label(2, 0, "Four digits");
410 f[n++] = make_field(3, 0, 1, 18);
411 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
413 f[n++] = make_label(2, 20, "Numeric");
414 f[n++] = make_field(3, 20, 1, 12);
415 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
420 f[n++] = make_label(5, 0, "Comments");
421 f[n++] = make_field(6, 0, 4, 46);
422 set_field_buffer(f[n - 1], 0, "HELLO\nWORLD!");
423 set_field_buffer(f[n - 1], 1, "Hello\nWorld!");
428 if ((form = new_form(f)) != 0) {
433 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
434 show_current_field(also, form);
437 switch (edit_field(form, &c)) {
440 case E_UNKNOWN_COMMAND:
441 finished = my_form_driver(form, c);
447 show_current_field(also, form);
454 for (c = 0; f[c] != 0; c++) {
455 void *ptr = field_userptr(f[c]);
462 #ifdef NCURSES_MOUSE_VERSION
463 mousemask(0, (mmask_t *) 0);
470 static const char *tbl[] =
472 "Usage: demo_forms [options]"
474 ," -d make fields dynamic"
475 ," -j value justify (1=left, 2=center, 3=right)"
476 ," -m value set maximum size of dynamic fields"
477 ," -o value specify number of offscreen rows in new_field()"
478 ," -t value specify text to fill fields initially"
481 for (j = 0; j < SIZEOF(tbl); ++j)
482 fprintf(stderr, "%s\n", tbl[j]);
487 main(int argc, char *argv[])
491 setlocale(LC_ALL, "");
493 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
499 j_value = atoi(optarg);
500 if (j_value < NO_JUSTIFICATION
501 || j_value > JUSTIFY_RIGHT)
505 m_value = atoi(optarg);
508 o_value = atoi(optarg);
523 nonl(); /* lets us read ^M's */
524 intrflush(stdscr, FALSE);
525 keypad(stdscr, TRUE);
529 init_pair(1, COLOR_WHITE, COLOR_BLUE);
530 init_pair(2, COLOR_GREEN, COLOR_BLACK);
531 init_pair(3, COLOR_CYAN, COLOR_BLACK);
539 ExitProgram(EXIT_SUCCESS);
545 printf("This program requires the curses form library\n");
546 ExitProgram(EXIT_FAILURE);