1 /****************************************************************************
2 * Copyright (c) 2003-2007,2008 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.30 2008/10/18 20:38:20 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) (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);
100 set_field_back(f, A_UNDERLINE);
102 * If -j and -d options are combined, -j loses. It is documented in
103 * "Character User Interface Programming", page 12-15 that setting
104 * O_STATIC off makes the form library ignore justification.
106 set_field_just(f, j_value);
109 set_field_fore(f, COLOR_PAIR(2));
110 set_field_back(f, A_UNDERLINE | COLOR_PAIR(3));
112 set_field_fore(f, A_BOLD);
115 * The field_opts_off() call dumps core with Solaris curses,
116 * but that is a known bug in Solaris' form library -TD
118 field_opts_off(f, O_STATIC);
119 set_max_field(f, m_value);
123 * The userptr is used in edit_field.c's inactive_field().
125 set_field_userptr(f, (void *) (long) field_back(f));
127 set_field_buffer(f, 0, t_value);
133 display_form(FORM * f)
138 scale_form(f, &rows, &cols);
141 * Put the form at the upper-left corner of the display, with just a box
144 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
146 set_form_sub(f, derwin(w, rows, cols, 1, 2));
151 if (post_form(f) != E_OK)
158 WINDOW *w = form_win(f);
159 WINDOW *s = form_sub(f);
169 show_insert_mode(bool insert_mode)
171 mvaddstr(5, 57, (insert_mode
172 ? "form_status: insert "
173 : "form_status: overlay"));
176 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
179 another_field(FORM * form, FIELD * field)
181 FIELD **f = form_fields(form);
185 for (n = 0; f[n] != 0; ++n) {
188 field_opts_on(result, O_SELECTABLE);
196 my_form_driver(FORM * form, int c)
198 static bool insert_mode = TRUE;
203 if (form_driver(form, REQ_VALIDATION) == E_OK)
210 if ((field = current_field(form)) != 0) {
211 set_current_field(form, another_field(form, field));
212 if (field_opts(field) & O_EDIT) {
213 field_opts_off(field, O_EDIT);
214 set_field_status(field, 0);
216 field_opts_on(field, O_EDIT);
218 set_current_field(form, field);
222 /* there should be a form_status() function, but there is none */
224 if (form_driver(form, REQ_INS_MODE) == E_OK) {
228 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
232 show_insert_mode(insert_mode);
243 show_current_field(WINDOW *win, FORM * form)
249 int field_rows, field_cols, field_max;
252 wbkgd(win, COLOR_PAIR(1));
255 wprintw(win, "Cursor: %d,%d", form->currow, form->curcol);
256 if (data_ahead(form))
257 waddstr(win, " ahead");
258 if (data_behind(form))
259 waddstr(win, " behind");
261 if ((field = current_field(form)) != 0) {
262 wprintw(win, "Page %d%s, Field %d/%d%s:",
264 new_page(field) ? "*" : "",
265 field_index(field), field_count(form),
266 field_arg(field) ? "(arg)" : "");
267 if ((type = field_type(field)) != 0) {
268 if (type == TYPE_ALNUM)
269 waddstr(win, "ALNUM");
270 else if (type == TYPE_ALPHA)
271 waddstr(win, "ALPHA");
272 else if (type == TYPE_ENUM)
273 waddstr(win, "ENUM");
274 else if (type == TYPE_INTEGER)
275 waddstr(win, "INTEGER");
276 #ifdef NCURSES_VERSION
277 else if (type == TYPE_IPV4)
278 waddstr(win, "IPV4");
280 else if (type == TYPE_NUMERIC)
281 waddstr(win, "NUMERIC");
282 else if (type == TYPE_REGEXP)
283 waddstr(win, "REGEXP");
285 waddstr(win, "other");
288 if (field_opts(field) & O_EDIT)
289 waddstr(win, " editable");
291 waddstr(win, " readonly");
293 if (field_status(field))
294 waddstr(win, " modified");
296 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
298 wprintw(win, " size %dx%d (max %d)",
299 field_rows, field_cols, field_max);
303 wattrset(win, field_fore(field));
304 waddstr(win, "fore");
305 wattroff(win, field_fore(field));
309 wattrset(win, field_back(field));
310 waddstr(win, "back");
311 wattroff(win, field_back(field));
313 wprintw(win, ", pad '%c'",
317 for (nbuf = 0; nbuf <= 2; ++nbuf) {
318 if ((buffer = field_buffer(field, nbuf)) != 0) {
319 wprintw(win, "buffer %d:", nbuf);
320 wattrset(win, A_REVERSE);
321 waddstr(win, buffer);
322 wattroff(win, A_REVERSE);
335 FIELD *f[100]; /* FIXME memset to zero */
341 #ifdef NCURSES_MOUSE_VERSION
342 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
347 mvaddstr(4, 57, "Forms Entry Test");
348 show_insert_mode(TRUE);
352 /* describe the form */
353 memset(f, 0, sizeof(f));
354 for (pg = 0; pg < 4; ++pg) {
356 sprintf(label, "Sample Form Page %d", pg + 1);
357 f[n++] = make_label(0, 15, label);
358 set_new_page(f[n - 1], TRUE);
362 f[n++] = make_label(2, 0, "Last Name");
363 f[n++] = make_field(3, 0, 1, 18);
364 set_field_type(f[n - 1], TYPE_ALPHA, 1);
366 f[n++] = make_label(2, 20, "First Name");
367 f[n++] = make_field(3, 20, 1, 12);
368 set_field_type(f[n - 1], TYPE_ALPHA, 1);
370 f[n++] = make_label(2, 34, "Middle Name");
371 f[n++] = make_field(3, 34, 1, 12);
372 set_field_type(f[n - 1], TYPE_ALPHA, 1);
375 f[n++] = make_label(2, 0, "Last Name");
376 f[n++] = make_field(3, 0, 1, 18);
377 set_field_type(f[n - 1], TYPE_ALPHA, 1);
379 f[n++] = make_label(2, 20, "First Name");
380 f[n++] = make_field(3, 20, 1, 12);
381 set_field_type(f[n - 1], TYPE_ALPHA, 1);
383 f[n++] = make_label(2, 34, "MI");
384 f[n++] = make_field(3, 34, 1, 1);
385 set_field_pad(f[n - 1], '?');
386 set_field_type(f[n - 1], TYPE_ALPHA, 1);
389 f[n++] = make_label(2, 0, "Host Name");
390 f[n++] = make_field(3, 0, 1, 18);
391 set_field_type(f[n - 1], TYPE_ALNUM, 1);
393 #ifdef NCURSES_VERSION
394 f[n++] = make_label(2, 20, "IP Address");
395 f[n++] = make_field(3, 20, 1, 12);
396 set_field_type(f[n - 1], TYPE_IPV4, 1);
402 f[n++] = make_label(2, 0, "Four digits");
403 f[n++] = make_field(3, 0, 1, 18);
404 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
406 f[n++] = make_label(2, 20, "Numeric");
407 f[n++] = make_field(3, 20, 1, 12);
408 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
413 f[n++] = make_label(5, 0, "Comments");
414 f[n++] = make_field(6, 0, 4, 46);
415 set_field_buffer(f[n - 1], 0, "HELLO\nWORLD!");
416 set_field_buffer(f[n - 1], 1, "Hello\nWorld!");
419 f[n++] = (FIELD *) 0;
421 if ((form = new_form(f)) != 0) {
426 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
427 show_current_field(also, form);
430 switch (edit_field(form, &c)) {
433 case E_UNKNOWN_COMMAND:
434 finished = my_form_driver(form, c);
440 show_current_field(also, form);
447 for (c = 0; f[c] != 0; c++)
452 #ifdef NCURSES_MOUSE_VERSION
453 mousemask(0, (mmask_t *) 0);
460 static const char *tbl[] =
462 "Usage: demo_forms [options]"
464 ," -d make fields dynamic"
465 ," -j value justify (1=left, 2=center, 3=right)"
466 ," -m value set maximum size of dynamic fields"
467 ," -o value specify number of offscreen rows in new_field()"
468 ," -t value specify text to fill fields initially"
471 for (j = 0; j < SIZEOF(tbl); ++j)
472 fprintf(stderr, "%s\n", tbl[j]);
477 main(int argc, char *argv[])
481 setlocale(LC_ALL, "");
483 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
489 j_value = atoi(optarg);
490 if (j_value < NO_JUSTIFICATION
491 || j_value > JUSTIFY_RIGHT)
495 m_value = atoi(optarg);
498 o_value = atoi(optarg);
513 nonl(); /* lets us read ^M's */
514 intrflush(stdscr, FALSE);
515 keypad(stdscr, TRUE);
519 init_pair(1, COLOR_WHITE, COLOR_BLUE);
520 init_pair(2, COLOR_GREEN, COLOR_BLACK);
521 init_pair(3, COLOR_CYAN, COLOR_BLACK);
529 ExitProgram(EXIT_SUCCESS);
535 printf("This program requires the curses form library\n");
536 ExitProgram(EXIT_FAILURE);