]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/form_driver_w.c
ncurses 6.4 - patch 20240414
[ncurses.git] / test / form_driver_w.c
1 /****************************************************************************
2  * Copyright 2020,2022 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.17 2022/12/10 23:31:31 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 static void
49 usage(int ok)
50 {
51     static const char *msg[] =
52     {
53         "Usage: form_driver_w [options]"
54         ,""
55         ,USAGE_COMMON
56     };
57     size_t n;
58
59     for (n = 0; n < SIZEOF(msg); n++)
60         fprintf(stderr, "%s\n", msg[n]);
61
62     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
63 }
64 /* *INDENT-OFF* */
65 VERSION_COMMON()
66 /* *INDENT-ON* */
67
68 int
69 main(int argc, char *argv[])
70 {
71     static const char *help[] =
72     {
73         "Commands:",
74         "  ^D,^Q,ESC           - quit program",
75         "  <Tab>,<Down>        - move to next field",
76         "  <BackTab>,<Up>      - move to previous field",
77         0
78     };
79
80 #define NUM_FIELDS 3
81 #define MyRow(n) (4 + (n) * 2)
82 #define MyCol(n) 10
83     FIELD *field[NUM_FIELDS + 1];
84     FORM *my_form;
85     bool done = FALSE;
86     int n;
87     int ch;
88
89     while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
90         switch (ch) {
91         case OPTS_VERSION:
92             show_version(argv);
93             ExitProgram(EXIT_SUCCESS);
94         default:
95             usage(ch == OPTS_USAGE);
96             /* NOTREACHED */
97         }
98     }
99     if (optind < argc)
100         usage(FALSE);
101
102     setlocale(LC_ALL, "");
103
104     /* Initialize curses */
105     initscr();
106     cbreak();
107     noecho();
108     keypad(stdscr, TRUE);
109
110     /* Initialize the fields */
111     for (n = 0; n < NUM_FIELDS; ++n) {
112         field[n] = new_field(1, 10, MyRow(n), 18, 0, 0);
113         set_field_back(field[n], A_UNDERLINE);
114         /* Print a line for the option  */
115         field_opts_off(field[n], O_AUTOSKIP);
116         /* Don't go to next field when this is filled */
117     }
118     field[n] = NULL;
119
120     /* Create the form and post it */
121     my_form = new_form(field);
122     post_form(my_form);
123     refresh();
124
125     for (n = 0; n < NUM_FIELDS; ++n) {
126         mvprintw(MyRow(n), MyCol(n), "Value %d:", n + 1);
127     }
128
129     /* Loop through to get user requests */
130     while (!done) {
131         wint_t c2;
132         int ret = get_wch(&c2);
133
134         mvprintw(MyRow(NUM_FIELDS),
135                  MyCol(NUM_FIELDS),
136                  "Got %d (%#x), type: %s",
137                  (int) c2,
138                  (int) c2,
139                  (ret == KEY_CODE_YES)
140                  ? "KEY_CODE_YES"
141                  : ((ret == OK)
142                     ? "OK"
143                     : ((ret == ERR)
144                        ? "ERR"
145                        : "?")));
146         clrtoeol();
147
148         switch (ret) {
149         case KEY_CODE_YES:
150             switch (c2) {
151             case KEY_DOWN:
152                 /* Go to next field */
153                 form_driver_w(my_form, KEY_CODE_YES, REQ_NEXT_FIELD);
154                 /* Go to the end of the present buffer */
155                 /* Leaves nicely at the last character */
156                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
157                 break;
158             case KEY_BTAB:
159             case KEY_UP:
160                 /* Go to previous field */
161                 form_driver_w(my_form, KEY_CODE_YES, REQ_PREV_FIELD);
162                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
163                 break;
164             default:
165                 break;
166             }
167             break;
168         case OK:
169             switch (c2) {
170             case CTRL('D'):
171             case QUIT:
172             case ESCAPE:
173                 done = TRUE;
174                 break;
175             case '\t':
176                 form_driver_w(my_form, KEY_CODE_YES, REQ_NEXT_FIELD);
177                 form_driver_w(my_form, KEY_CODE_YES, REQ_END_LINE);
178                 break;
179             case HELP_KEY_1:
180                 popup_msg(form_win(my_form), help);
181                 break;
182             default:
183                 form_driver_w(my_form, OK, (wchar_t) c2);
184                 break;
185             }
186             break;
187         }
188     }
189
190     /* Un post form and free the memory */
191     unpost_form(my_form);
192     free_form(my_form);
193     for (n = 0; n < NUM_FIELDS; ++n) {
194         free_field(field[n]);
195     }
196
197     endwin();
198     ExitProgram(EXIT_SUCCESS);
199 }
200
201 #else
202 int
203 main(void)
204 {
205     printf("This program requires the wide-ncurses and forms library\n");
206     ExitProgram(EXIT_FAILURE);
207 }
208 #endif /* USE_WIDEC_SUPPORT */