]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/form_driver_w.c
276209f49440fef83762c2d41568b0b21954ab9d
[ncurses.git] / test / form_driver_w.c
1 /****************************************************************************
2  * Copyright (c) 2013 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.9 2013/12/07 20:35:54 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
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", ch, 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(my_form, REQ_NEXT_FIELD);
105                 /* Go to the end of the present buffer */
106                 /* Leaves nicely at the last character */
107                 form_driver(my_form, REQ_END_LINE);
108                 break;
109             case KEY_UP:
110                 /* Go to previous field */
111                 form_driver(my_form, REQ_PREV_FIELD);
112                 form_driver(my_form, REQ_END_LINE);
113                 break;
114             default:
115 #if 0
116                 /* If this is a normal character, it gets printed */
117                 form_driver(my_form, ch);
118                 wadd_wch(my_form->current->working, ch);
119 #endif
120                 break;
121             }
122             break;
123         case OK:
124             switch (ch) {
125             case CTRL('D'):
126             case QUIT:
127             case ESCAPE:
128                 done = TRUE;
129                 break;
130             default:
131                 form_driver_w(my_form, OK, ch);
132                 break;
133             }
134             break;
135         }
136     }
137
138     /* Un post form and free the memory */
139     unpost_form(my_form);
140     free_form(my_form);
141     free_field(field[0]);
142     free_field(field[1]);
143
144     endwin();
145     ExitProgram(EXIT_SUCCESS);
146 }
147
148 #else
149 int
150 main(void)
151 {
152     printf("This program requires the wide-ncurses and forms library\n");
153     ExitProgram(EXIT_FAILURE);
154 }
155 #endif /* USE_WIDEC_SUPPORT */