]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/form_driver_w.c
ncurses 6.2 - patch 20210710
[ncurses.git] / test / form_driver_w.c
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 2013-2014,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *   Author:  Gaute Hope, 2013                                              *
32  ****************************************************************************/
33
34 /*
35  * $Id: form_driver_w.c,v 1.16 2020/02/02 23:34:34 tom Exp $
36  *
37  * Test form_driver_w (int, int, wchar_t), a wide char aware
38  * replacement of form_driver.
39  */
40
41 #include <test.priv.h>
42 #include <popup_msg.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     static const char *help[] =
52     {
53         "Commands:",
54         "  ^D,^Q,ESC           - quit program",
55         "  <Tab>,<Down>        - move to next field",
56         "  <BackTab>,<Up>      - move to previous field",
57         0
58     };
59
60 #define NUM_FIELDS 3
61 #define MyRow(n) (4 + (n) * 2)
62 #define MyCol(n) 10
63     FIELD *field[NUM_FIELDS + 1];
64     FORM *my_form;
65     bool done = FALSE;
66     int n;
67
68     setlocale(LC_ALL, "");
69
70     /* Initialize curses */
71     initscr();
72     cbreak();
73     noecho();
74     keypad(stdscr, TRUE);
75
76     /* Initialize the fields */
77     for (n = 0; n < NUM_FIELDS; ++n) {
78         field[n] = new_field(1, 10, MyRow(n), 18, 0, 0);
79         set_field_back(field[n], A_UNDERLINE);
80         /* Print a line for the option  */
81         field_opts_off(field[n], O_AUTOSKIP);
82         /* Don't go to next field when this is filled */
83     }
84     field[n] = NULL;
85
86     /* Create the form and post it */
87     my_form = new_form(field);
88     post_form(my_form);
89     refresh();
90
91     for (n = 0; n < NUM_FIELDS; ++n) {
92         mvprintw(MyRow(n), MyCol(n), "Value %d:", n + 1);
93     }
94
95     /* Loop through to get user requests */
96     while (!done) {
97         wint_t ch;
98         int ret = get_wch(&ch);
99
100         mvprintw(MyRow(NUM_FIELDS),
101                  MyCol(NUM_FIELDS),
102                  "Got %d (%#x), type: %s",
103                  (int) ch,
104                  (int) ch,
105                  (ret == KEY_CODE_YES)
106                  ? "KEY_CODE_YES"
107                  : ((ret == OK)
108                     ? "OK"
109                     : ((ret == ERR)
110                        ? "ERR"
111                        : "?")));
112         clrtoeol();
113
114         switch (ret) {
115         case KEY_CODE_YES:
116             switch (ch) {
117             case KEY_DOWN:
118                 /* Go to next field */
119                 form_driver_w(my_form, KEY_CODE_YES, REQ_NEXT_FIELD);
120                 /* Go to the end of the present buffer */
121                 /* Leaves nicely at the last character */
122                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
123                 break;
124             case KEY_BTAB:
125             case KEY_UP:
126                 /* Go to previous field */
127                 form_driver_w(my_form, KEY_CODE_YES, REQ_PREV_FIELD);
128                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
129                 break;
130             default:
131                 break;
132             }
133             break;
134         case OK:
135             switch (ch) {
136             case CTRL('D'):
137             case QUIT:
138             case ESCAPE:
139                 done = TRUE;
140                 break;
141             case '\t':
142                 form_driver_w(my_form, KEY_CODE_YES, REQ_NEXT_FIELD);
143                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
144                 break;
145             case HELP_KEY_1:
146                 popup_msg(form_win(my_form), help);
147                 break;
148             default:
149                 form_driver_w(my_form, OK, (wchar_t) ch);
150                 break;
151             }
152             break;
153         }
154     }
155
156     /* Un post form and free the memory */
157     unpost_form(my_form);
158     free_form(my_form);
159     for (n = 0; n < NUM_FIELDS; ++n) {
160         free_field(field[n]);
161     }
162
163     endwin();
164     ExitProgram(EXIT_SUCCESS);
165 }
166
167 #else
168 int
169 main(void)
170 {
171     printf("This program requires the wide-ncurses and forms library\n");
172     ExitProgram(EXIT_FAILURE);
173 }
174 #endif /* USE_WIDEC_SUPPORT */