1 /****************************************************************************
2 * Copyright (c) 2003-2006,2007 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.24 2007/07/21 17:45:09 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);
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 for (pg = 0; pg < 4; ++pg) {
339 sprintf(label, "Sample Form Page %d", pg + 1);
340 f[n++] = make_label(0, 15, label);
341 set_new_page(f[n - 1], TRUE);
345 f[n++] = make_label(2, 0, "Last Name");
346 f[n++] = make_field(3, 0, 1, 18);
347 set_field_type(f[n - 1], TYPE_ALPHA, 1);
349 f[n++] = make_label(2, 20, "First Name");
350 f[n++] = make_field(3, 20, 1, 12);
351 set_field_type(f[n - 1], TYPE_ALPHA, 1);
353 f[n++] = make_label(2, 34, "Middle Name");
354 f[n++] = make_field(3, 34, 1, 12);
355 set_field_type(f[n - 1], TYPE_ALPHA, 1);
358 f[n++] = make_label(2, 0, "Last Name");
359 f[n++] = make_field(3, 0, 1, 18);
360 set_field_type(f[n - 1], TYPE_ALPHA, 1);
362 f[n++] = make_label(2, 20, "First Name");
363 f[n++] = make_field(3, 20, 1, 12);
364 set_field_type(f[n - 1], TYPE_ALPHA, 1);
366 f[n++] = make_label(2, 34, "MI");
367 f[n++] = make_field(3, 34, 1, 1);
368 set_field_type(f[n - 1], TYPE_ALPHA, 1);
371 f[n++] = make_label(2, 0, "Host Name");
372 f[n++] = make_field(3, 0, 1, 18);
373 set_field_type(f[n - 1], TYPE_ALNUM, 1);
375 #ifdef NCURSES_VERSION
376 f[n++] = make_label(2, 20, "IP Address");
377 f[n++] = make_field(3, 20, 1, 12);
378 set_field_type(f[n - 1], TYPE_IPV4, 1);
384 f[n++] = make_label(2, 0, "Four digits");
385 f[n++] = make_field(3, 0, 1, 18);
386 set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0);
388 f[n++] = make_label(2, 20, "Numeric");
389 f[n++] = make_field(3, 20, 1, 12);
390 set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0);
395 f[n++] = make_label(5, 0, "Comments");
396 f[n++] = make_field(6, 0, 4, 46);
399 f[n++] = (FIELD *) 0;
406 also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0);
407 show_current_field(also, form);
410 switch (edit_field(form, &c)) {
413 case E_UNKNOWN_COMMAND:
414 finished = my_form_driver(form, c);
420 show_current_field(also, form);
426 for (c = 0; f[c] != 0; c++)
431 #ifdef NCURSES_MOUSE_VERSION
432 mousemask(0, (mmask_t *) 0);
439 static const char *tbl[] =
441 "Usage: demo_forms [options]"
443 ," -d make fields dynamic"
444 ," -j value justify (1=left, 2=center, 3=right)"
445 ," -m value set maximum size of dynamic fields"
446 ," -o value specify number of offscreen rows in new_field()"
447 ," -t value specify text to fill fields initially"
450 for (j = 0; j < SIZEOF(tbl); ++j)
451 fprintf(stderr, "%s\n", tbl[j]);
456 main(int argc, char *argv[])
460 setlocale(LC_ALL, "");
462 while ((ch = getopt(argc, argv, "dj:m:o:t:")) != -1) {
468 j_value = atoi(optarg);
469 if (j_value < NO_JUSTIFICATION
470 || j_value > JUSTIFY_RIGHT)
474 m_value = atoi(optarg);
477 o_value = atoi(optarg);
492 nonl(); /* lets us read ^M's */
493 intrflush(stdscr, FALSE);
494 keypad(stdscr, TRUE);
498 init_pair(1, COLOR_WHITE, COLOR_BLUE);
499 init_pair(2, COLOR_GREEN, COLOR_BLACK);
507 ExitProgram(EXIT_SUCCESS);
513 printf("This program requires the curses form library\n");
514 ExitProgram(EXIT_FAILURE);