]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/form_driver_w.c
ncurses 6.0 - patch 20151024
[ncurses.git] / test / form_driver_w.c
1 /****************************************************************************
2  * Copyright (c) 2013,2014 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 /****************************************************************************
30  *   Author:  Gaute Hope, 2013                                              *
31  ****************************************************************************/
32
33 /*
34  * $Id: form_driver_w.c,v 1.13 2014/08/02 17:24:55 tom Exp $
35  *
36  * Test form_driver_w (int, int, wchar_t), a wide char aware
37  * replacement of form_driver.
38  */
39
40 #include <locale.h>
41
42 #include <test.priv.h>
43
44 #if USE_WIDEC_SUPPORT && USE_LIBFORM && (defined(NCURSES_VERSION_PATCH) && NCURSES_VERSION_PATCH >= 20131207)
45
46 #include <form.h>
47
48 int
49 main(void)
50 {
51     FIELD *field[3];
52     FORM *my_form;
53     bool done = FALSE;
54
55     setlocale(LC_ALL, "");
56
57     /* Initialize curses */
58     initscr();
59     cbreak();
60     noecho();
61     keypad(stdscr, TRUE);
62
63     /* Initialize the fields */
64     field[0] = new_field(1, 10, 4, 18, 0, 0);
65     field[1] = new_field(1, 10, 6, 18, 0, 0);
66     field[2] = NULL;
67
68     /* Set field options */
69     set_field_back(field[0], A_UNDERLINE);      /* Print a line for the option  */
70     field_opts_off(field[0], O_AUTOSKIP);       /* Don't go to next field when this */
71     /* Field is filled up           */
72     set_field_back(field[1], A_UNDERLINE);
73     field_opts_off(field[1], O_AUTOSKIP);
74
75     /* Create the form and post it */
76     my_form = new_form(field);
77     post_form(my_form);
78     refresh();
79
80     mvprintw(4, 10, "Value 1:");
81     mvprintw(6, 10, "Value 2:");
82     refresh();
83
84     /* Loop through to get user requests */
85     while (!done) {
86         wint_t ch;
87         int ret = get_wch(&ch);
88
89         mvprintw(8, 10, "Got %d (%#x), type: %s", (int) ch, (int) ch,
90                  (ret == KEY_CODE_YES)
91                  ? "KEY_CODE_YES"
92                  : ((ret == OK)
93                     ? "OK"
94                     : ((ret == ERR)
95                        ? "ERR"
96                        : "?")));
97         clrtoeol();
98
99         switch (ret) {
100         case KEY_CODE_YES:
101             switch (ch) {
102             case KEY_DOWN:
103                 /* Go to next field */
104                 form_driver_w(my_form, KEY_CODE_YES, REQ_NEXT_FIELD);
105                 /* Go to the end of the present buffer */
106                 /* Leaves nicely at the last character */
107                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
108                 break;
109             case KEY_UP:
110                 /* Go to previous field */
111                 form_driver_w(my_form, KEY_CODE_YES, REQ_PREV_FIELD);
112                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
113                 break;
114             default:
115                 break;
116             }
117             break;
118         case OK:
119             switch (ch) {
120             case CTRL('D'):
121             case QUIT:
122             case ESCAPE:
123                 done = TRUE;
124                 break;
125             default:
126                 form_driver_w(my_form, OK, (wchar_t) ch);
127                 break;
128             }
129             break;
130         }
131     }
132
133     /* Un post form and free the memory */
134     unpost_form(my_form);
135     free_form(my_form);
136     free_field(field[0]);
137     free_field(field[1]);
138
139     endwin();
140     ExitProgram(EXIT_SUCCESS);
141 }
142
143 #else
144 int
145 main(void)
146 {
147     printf("This program requires the wide-ncurses and forms library\n");
148     ExitProgram(EXIT_FAILURE);
149 }
150 #endif /* USE_WIDEC_SUPPORT */