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.26 2008/08/23 23:22:55 tom Exp $
31 * Demonstrate a variety of functions from the form library.
32 * Thomas Dickey - 2003/4/26
52 form_request_by_name -
65 set_fieldtype_choice -
75 #include <test.priv.h>
79 #include <edit_field.h>
81 static int d_option = 0;
82 static int j_value = 0;
83 static int m_value = 0;
84 static int o_value = 0;
85 static char *t_value = 0;
88 make_label(int frow, int fcol, NCURSES_CONST char *label)
90 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
93 set_field_buffer(f, 0, label);
94 set_field_opts(f, (int) (field_opts(f) & ~O_ACTIVE));
100 * Define each field with an extra one, for reflecting "actual" text.
103 make_field(int frow, int fcol, int rows, int cols)
105 FIELD *f = new_field(rows, cols, frow, fcol, o_value, 1);
108 set_field_back(f, A_UNDERLINE);
110 * If -j and -d options are combined, -j loses. It is documented in
111 * "Character User Interface Programming", page 12-15 that setting
112 * O_STATIC off makes the form library ignore justification.
114 set_field_just(f, j_value);
115 set_field_userptr(f, (void *) 0);
118 set_field_fore(f, COLOR_PAIR(2));
120 set_field_fore(f, A_BOLD);
123 * The field_opts_off() call dumps core with Solaris curses,
124 * but that is a known bug in Solaris' form library -TD
126 field_opts_off(f, O_STATIC);
127 set_max_field(f, m_value);
130 set_field_buffer(f, 0, t_value);
136 display_form(FORM * f)
141 scale_form(f, &rows, &cols);
144 * Put the form at the upper-left corner of the display, with just a box
147 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
149 set_form_sub(f, derwin(w, rows, cols, 1, 2));
154 if (post_form(f) != E_OK)
161 WINDOW *w = form_win(f);
162 WINDOW *s = form_sub(f);
172 show_insert_mode(bool insert_mode)
174 mvaddstr(5, 57, (insert_mode
175 ? "form_status: insert "
176 : "form_status: overlay"));
179 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
182 another_field(FORM * form, FIELD * field)
184 FIELD **f = form_fields(form);
188 for (n = 0; f[n] != 0; ++n) {
191 field_opts_on(result, O_SELECTABLE);
199 my_form_driver(FORM * form, int c)
201 static bool insert_mode = TRUE;
206 if (form_driver(form, REQ_VALIDATION) == E_OK)
213 if ((field = current_field(form)) != 0) {
214 set_current_field(form, another_field(form, field));
215 if (field_opts(field) & O_EDIT) {
216 field_opts_off(field, O_EDIT);
217 set_field_status(field, 0);
219 field_opts_on(field, O_EDIT);
221 set_current_field(form, field);
225 /* there should be a form_status() function, but there is none */
227 if (form_driver(form, REQ_INS_MODE) == E_OK) {
231 if (form_driver(form, REQ_OVL_MODE) == E_OK) {
235 show_insert_mode(insert_mode);
246 show_current_field(WINDOW *win, FORM * form)
252 int field_rows, field_cols, field_max;
255 wbkgd(win, COLOR_PAIR(1));
258 wprintw(win, "Cursor: %d,%d", form->currow, form->curcol);
259 if (data_ahead(form))
260 waddstr(win, " ahead");
261 if (data_behind(form))
262 waddstr(win, " behind");
264 if ((field = current_field(form)) != 0) {
265 wprintw(win, "Field %d:", field_index(field));
266 if ((type = field_type(field)) != 0) {
267 if (type == TYPE_ALNUM)
268 waddstr(win, "ALNUM");
269 else if (type == TYPE_ALPHA)
270 waddstr(win, "ALPHA");
271 else if (type == TYPE_ENUM)
272 waddstr(win, "ENUM");
273 else if (type == TYPE_INTEGER)
274 waddstr(win, "INTEGER");
275 #ifdef NCURSES_VERSION
276 else if (type == TYPE_IPV4)
277 waddstr(win, "IPV4");
279 else if (type == TYPE_NUMERIC)
280 waddstr(win, "NUMERIC");
281 else if (type == TYPE_REGEXP)
282 waddstr(win, "REGEXP");
284 waddstr(win, "other");
287 if (field_opts(field) & O_EDIT)
288 waddstr(win, " editable");
290 waddstr(win, " readonly");
292 if (field_status(field))
293 waddstr(win, " modified");
295 if (dynamic_field_info(field, &field_rows, &field_cols, &field_max)
297 wprintw(win, " size %dx%d (max %d)",
298 field_rows, field_cols, field_max);
301 for (nbuf = 0; nbuf <= 2; ++nbuf) {
302 if ((buffer = field_buffer(field, nbuf)) != 0) {
303 wprintw(win, "buffer %d:", nbuf);
304 wattrset(win, A_REVERSE);
305 waddstr(win, buffer);
306 wattroff(win, A_REVERSE);
319 FIELD *f[100]; /* FIXME memset to zero */
325 #ifdef NCURSES_MOUSE_VERSION
326 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
331 mvaddstr(4, 57, "Forms Entry Test");
332 show_insert_mode(TRUE);
336 /* describe the form */
337 memset(f, 0, sizeof(f));
338 for (pg = 0; pg < 4; ++pg) {
340 sprintf(label, "Sample Form Page %d", pg + 1);
341 f[n++] = make_label(0, 15, label);
342 set_new_page(f[n - 1], TRUE);
346 f[n++] = make_label(2, 0, "Last Name");
347 f[n++] = make_field(3, 0, 1, 18);
348 set_field_type(f[n - 1], TYPE_ALPHA, 1);
350 f[n++] = make_label(2, 20, "First Name");
351 f[n++] = make_field(3, 20, 1, 12);
352 set_field_type(f[n - 1], TYPE_ALPHA, 1);
354 f[n++] = make_label(2, 34, "Middle Name");
355 f[n++] = make_field(3, 34, 1, 12);
356 set_field_type(f[n - 1], TYPE_ALPHA, 1);
359 f[n++] = make_label(2, 0, "Last Name");
360 f[n++] = make_field(3, 0, 1, 18);
361 set_field_type(f[n - 1], TYPE_ALPHA, 1);
363 f[n++] = make_label(2, 20, "First Name");
364 f[n++] = make_field(3, 20, 1, 12);
365 set_field_type(f[n - 1], TYPE_ALPHA, 1);
367 f[n++] = make_label(2, 34, "MI");
368 f[n++] = make_field(3, 34, 1, 1);
369 set_field_type(f[n - 1], TYPE_ALPHA, 1);
372 f[n++] = make_label(2, 0, "Host Name");
373 f[n++] = make_field(3, 0, 1, 18);
374 set_field_type(f[n - 1], TYPE_ALNUM, 1);
376 #ifdef NCURSES_VERSION
377 f[n++] = make_label(2, 20, "IP Address");
378 f[n++] = make_field(3, 20, 1, 12);
379 set_field_type(f[n - 1], TYPE_IPV4, 1);
385 f[n++] = make_label(2, 0, "Four digits");
386 f[n++] = make_field(3, 0, 1, 18);
387 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
389 f[n++] = make_label(2, 20, "Numeric");
390 f[n++] = make_field(3, 20, 1, 12);
391 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
396 f[n++] = make_label(5, 0, "Comments");
397 f[n++] = make_field(6, 0, 4, 46);
400 f[n++] = (FIELD *) 0;
402 if ((form = new_form(f)) != 0) {
407 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
408 show_current_field(also, form);
411 switch (edit_field(form, &c)) {
414 case E_UNKNOWN_COMMAND:
415 finished = my_form_driver(form, c);
421 show_current_field(also, form);
428 for (c = 0; f[c] != 0; c++)
433 #ifdef NCURSES_MOUSE_VERSION
434 mousemask(0, (mmask_t *) 0);
441 static const char *tbl[] =
443 "Usage: demo_forms [options]"
445 ," -d make fields dynamic"
446 ," -j value justify (1=left, 2=center, 3=right)"
447 ," -m value set maximum size of dynamic fields"
448 ," -o value specify number of offscreen rows in new_field()"
449 ," -t value specify text to fill fields initially"
452 for (j = 0; j < SIZEOF(tbl); ++j)
453 fprintf(stderr, "%s\n", tbl[j]);
458 main(int argc, char *argv[])
462 setlocale(LC_ALL, "");
464 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
470 j_value = atoi(optarg);
471 if (j_value < NO_JUSTIFICATION
472 || j_value > JUSTIFY_RIGHT)
476 m_value = atoi(optarg);
479 o_value = atoi(optarg);
494 nonl(); /* lets us read ^M's */
495 intrflush(stdscr, FALSE);
496 keypad(stdscr, TRUE);
500 init_pair(1, COLOR_WHITE, COLOR_BLUE);
501 init_pair(2, COLOR_GREEN, COLOR_BLACK);
509 ExitProgram(EXIT_SUCCESS);
515 printf("This program requires the curses form library\n");
516 ExitProgram(EXIT_FAILURE);