]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/form_driver_w.c
ncurses 6.1 - patch 20180129
[ncurses.git] / test / form_driver_w.c
1 /****************************************************************************
2  * Copyright (c) 2013-2014,2017 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.15 2017/04/15 20:41:35 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 <test.priv.h>
41 #include <popup_msg.h>
42
43 #if USE_WIDEC_SUPPORT && USE_LIBFORM && (defined(NCURSES_VERSION_PATCH) && NCURSES_VERSION_PATCH >= 20131207)
44
45 #include <form.h>
46
47 int
48 main(void)
49 {
50     static const char *help[] =
51     {
52         "Commands:",
53         "  ^D,^Q,ESC           - quit program",
54         "  <Tab>,<Down>        - move to next field",
55         "  <BackTab>,<Up>      - move to previous field",
56         0
57     };
58
59 #define NUM_FIELDS 3
60 #define MyRow(n) (4 + (n) * 2)
61 #define MyCol(n) 10
62     FIELD *field[NUM_FIELDS + 1];
63     FORM *my_form;
64     bool done = FALSE;
65     int n;
66
67     setlocale(LC_ALL, "");
68
69     /* Initialize curses */
70     initscr();
71     cbreak();
72     noecho();
73     keypad(stdscr, TRUE);
74
75     /* Initialize the fields */
76     for (n = 0; n < NUM_FIELDS; ++n) {
77         field[n] = new_field(1, 10, MyRow(n), 18, 0, 0);
78         set_field_back(field[n], A_UNDERLINE);
79         /* Print a line for the option  */
80         field_opts_off(field[n], O_AUTOSKIP);
81         /* Don't go to next field when this is filled */
82     }
83     field[n] = NULL;
84
85     /* Create the form and post it */
86     my_form = new_form(field);
87     post_form(my_form);
88     refresh();
89
90     for (n = 0; n < NUM_FIELDS; ++n) {
91         mvprintw(MyRow(n), MyCol(n), "Value %d:", n + 1);
92     }
93
94     /* Loop through to get user requests */
95     while (!done) {
96         wint_t ch;
97         int ret = get_wch(&ch);
98
99         mvprintw(MyRow(NUM_FIELDS),
100                  MyCol(NUM_FIELDS),
101                  "Got %d (%#x), type: %s",
102                  (int) ch,
103                  (int) ch,
104                  (ret == KEY_CODE_YES)
105                  ? "KEY_CODE_YES"
106                  : ((ret == OK)
107                     ? "OK"
108                     : ((ret == ERR)
109                        ? "ERR"
110                        : "?")));
111         clrtoeol();
112
113         switch (ret) {
114         case KEY_CODE_YES:
115             switch (ch) {
116             case KEY_DOWN:
117                 /* Go to next field */
118                 form_driver_w(my_form, KEY_CODE_YES, REQ_NEXT_FIELD);
119                 /* Go to the end of the present buffer */
120                 /* Leaves nicely at the last character */
121                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
122                 break;
123             case KEY_BTAB:
124             case KEY_UP:
125                 /* Go to previous field */
126                 form_driver_w(my_form, KEY_CODE_YES, REQ_PREV_FIELD);
127                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
128                 break;
129             default:
130                 break;
131             }
132             break;
133         case OK:
134             switch (ch) {
135             case CTRL('D'):
136             case QUIT:
137             case ESCAPE:
138                 done = TRUE;
139                 break;
140             case '\t':
141                 form_driver_w(my_form, KEY_CODE_YES, REQ_NEXT_FIELD);
142                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
143                 break;
144             case HELP_KEY_1:
145                 popup_msg(form_win(my_form), help);
146                 break;
147             default:
148                 form_driver_w(my_form, OK, (wchar_t) ch);
149                 break;
150             }
151             break;
152         }
153     }
154
155     /* Un post form and free the memory */
156     unpost_form(my_form);
157     free_form(my_form);
158     for (n = 0; n < NUM_FIELDS; ++n) {
159         free_field(field[n]);
160     }
161
162     endwin();
163     ExitProgram(EXIT_SUCCESS);
164 }
165
166 #else
167 int
168 main(void)
169 {
170     printf("This program requires the wide-ncurses and forms library\n");
171     ExitProgram(EXIT_FAILURE);
172 }
173 #endif /* USE_WIDEC_SUPPORT */