]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_get_wstr.c
ncurses 5.9 - patch 20110716
[ncurses.git] / test / test_get_wstr.c
1 /****************************************************************************
2  * Copyright (c) 2007-2009,2011 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  * $Id: test_get_wstr.c,v 1.8 2011/01/15 18:15:11 tom Exp $
30  *
31  * Author: Thomas E Dickey
32  *
33  * Demonstrate the get_wstr functions from the curses library.
34
35        int get_wstr(wint_t *wstr);
36        int getn_wstr(wint_t *wstr, int n);
37        int wget_wstr(WINDOW *win, wint_t *wstr);
38        int wgetn_wstr(WINDOW *win, wint_t *wstr, int n);
39        int mvget_wstr(int y, int x, wint_t *wstr);
40        int mvgetn_wstr(int y, int x, wint_t *wstr, int n);
41        int mvwget_wstr(WINDOW *win, int y, int x, wint_t *wstr);
42        int mvwgetn_wstr(WINDOW *win, int y, int x, wint_t *wstr, int n);
43  */
44
45 #include <test.priv.h>
46
47 #if HAVE_CHGAT
48 /* NetBSD curses wchgat */
49
50 #if USE_WIDEC_SUPPORT
51
52 #define BASE_Y 6
53 #define MAX_COLS 1024
54
55 typedef enum {
56     eGetStr = 0,
57     eGetNStr,
58     eMvGetStr,
59     eMvGetNStr,
60     eMaxFlavor
61 } Flavors;
62
63 static bool
64 Quit(int ch)
65 {
66     return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
67 }
68
69 static int
70 Remainder(WINDOW *txtwin)
71 {
72     int result = getmaxx(txtwin) - getcurx(txtwin);
73     return (result > 0) ? result : 0;
74 }
75
76 /*
77  * Show a highlighted line in the place where input will happen.
78  */
79 static void
80 ShowPrompt(WINDOW *txtwin, int limit)
81 {
82     wchgat(txtwin, limit, A_REVERSE, 0, NULL);
83     wnoutrefresh(txtwin);
84 }
85
86 static void
87 MovePrompt(WINDOW *txtwin, int limit, int y, int x)
88 {
89     wchgat(txtwin, Remainder(txtwin), A_NORMAL, 0, NULL);
90     wmove(txtwin, y, x);
91     ShowPrompt(txtwin, limit);
92 }
93
94 static int
95 ShowFlavor(WINDOW *strwin, WINDOW *txtwin, int flavor, int limit)
96 {
97     const char *name = "?";
98     bool limited = FALSE;
99     bool wins = (txtwin != stdscr);
100     int result;
101
102     switch (flavor) {
103     case eGetStr:
104         name = wins ? "wget_wstr" : "get_wstr";
105         break;
106     case eGetNStr:
107         limited = TRUE;
108         name = wins ? "wgetn_wstr" : "getn_wstr";
109         break;
110     case eMvGetStr:
111         name = wins ? "mvwget_wstr" : "mvget_wstr";
112         break;
113     case eMvGetNStr:
114         limited = TRUE;
115         name = wins ? "mvwgetn_wstr" : "mvgetn_wstr";
116         break;
117     case eMaxFlavor:
118         break;
119     }
120
121     wmove(strwin, 0, 0);
122     werase(strwin);
123
124     if (limited) {
125         wprintw(strwin, "%s(%d):", name, limit);
126     } else {
127         wprintw(strwin, "%s:", name);
128     }
129     result = limited ? limit : Remainder(txtwin);
130     ShowPrompt(txtwin, result);
131
132     wnoutrefresh(strwin);
133     return result;
134 }
135
136 static int
137 test_get_wstr(int level, char **argv, WINDOW *strwin)
138 {
139     WINDOW *txtbox = 0;
140     WINDOW *txtwin = 0;
141     FILE *fp;
142     int ch;
143     int rc;
144     int txt_x = 0, txt_y = 0;
145     int base_y;
146     int flavor = 0;
147     int limit = getmaxx(strwin) - 5;
148     int actual;
149     wint_t buffer[MAX_COLS];
150
151     if (argv[level] == 0) {
152         beep();
153         return FALSE;
154     }
155
156     if (level > 1) {
157         txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
158         box(txtbox, 0, 0);
159         wnoutrefresh(txtbox);
160
161         txtwin = derwin(txtbox,
162                         getmaxy(txtbox) - 2,
163                         getmaxx(txtbox) - 2,
164                         1, 1);
165         base_y = 0;
166     } else {
167         txtwin = stdscr;
168         base_y = BASE_Y;
169     }
170
171     keypad(txtwin, TRUE);       /* enable keyboard mapping */
172     (void) cbreak();            /* take input chars one at a time, no wait for \n */
173     (void) noecho();            /* don't echo input */
174
175     txt_y = base_y;
176     txt_x = 0;
177     wmove(txtwin, txt_y, txt_x);
178
179     if ((fp = fopen(argv[level], "r")) != 0) {
180         while ((ch = fgetc(fp)) != EOF) {
181             if (waddch(txtwin, UChar(ch)) != OK) {
182                 break;
183             }
184         }
185         fclose(fp);
186     } else {
187         wprintw(txtwin, "Cannot open:\n%s", argv[1]);
188     }
189
190     wmove(txtwin, txt_y, txt_x);
191     actual = ShowFlavor(strwin, txtwin, flavor, limit);
192     while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
193         switch (ch) {
194         case KEY_DOWN:
195         case 'j':
196             if (txt_y < getmaxy(txtwin) - 1) {
197                 MovePrompt(txtwin, actual, ++txt_y, txt_x);
198             } else {
199                 beep();
200             }
201             break;
202         case KEY_UP:
203         case 'k':
204             if (txt_y > base_y) {
205                 MovePrompt(txtwin, actual, --txt_y, txt_x);
206             } else {
207                 beep();
208             }
209             break;
210         case KEY_LEFT:
211         case 'h':
212             if (txt_x > 0) {
213                 MovePrompt(txtwin, actual, txt_y, --txt_x);
214             } else {
215                 beep();
216             }
217             break;
218         case KEY_RIGHT:
219         case 'l':
220             if (txt_x < getmaxx(txtwin) - 1) {
221                 MovePrompt(txtwin, actual, txt_y, ++txt_x);
222             } else {
223                 beep();
224             }
225             break;
226
227         case 'w':
228             test_get_wstr(level + 1, argv, strwin);
229             if (txtbox != 0) {
230                 touchwin(txtbox);
231                 wnoutrefresh(txtbox);
232             } else {
233                 touchwin(txtwin);
234                 wnoutrefresh(txtwin);
235             }
236             break;
237
238         case '-':
239             if (limit > 0) {
240                 actual = ShowFlavor(strwin, txtwin, flavor, --limit);
241                 MovePrompt(txtwin, actual, txt_y, txt_x);
242             } else {
243                 beep();
244             }
245             break;
246
247         case '+':
248             actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
249             MovePrompt(txtwin, actual, txt_y, txt_x);
250             break;
251
252         case '<':
253             if (flavor > 0) {
254                 actual = ShowFlavor(strwin, txtwin, --flavor, limit);
255                 MovePrompt(txtwin, actual, txt_y, txt_x);
256             } else {
257                 beep();
258             }
259             break;
260
261         case '>':
262             if (flavor + 1 < eMaxFlavor) {
263                 actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
264                 MovePrompt(txtwin, actual, txt_y, txt_x);
265             } else {
266                 beep();
267             }
268             break;
269
270         case ':':
271             actual = ShowFlavor(strwin, txtwin, flavor, limit);
272             *buffer = '\0';
273             rc = ERR;
274             echo();
275             (void) wattrset(txtwin, A_REVERSE);
276             switch (flavor) {
277             case eGetStr:
278                 if (txtwin != stdscr) {
279                     wmove(txtwin, txt_y, txt_x);
280                     rc = wget_wstr(txtwin, buffer);
281                 } else {
282                     move(txt_y, txt_x);
283                     rc = get_wstr(buffer);
284                 }
285                 break;
286             case eGetNStr:
287                 if (txtwin != stdscr) {
288                     wmove(txtwin, txt_y, txt_x);
289                     rc = wgetn_wstr(txtwin, buffer, limit);
290                 } else {
291                     move(txt_y, txt_x);
292                     rc = getn_wstr(buffer, limit);
293                 }
294                 break;
295             case eMvGetStr:
296                 if (txtwin != stdscr) {
297                     rc = mvwget_wstr(txtwin, txt_y, txt_x, buffer);
298                 } else {
299                     rc = mvget_wstr(txt_y, txt_x, buffer);
300                 }
301                 break;
302             case eMvGetNStr:
303                 if (txtwin != stdscr) {
304                     rc = mvwgetn_wstr(txtwin, txt_y, txt_x, buffer, limit);
305                 } else {
306                     rc = mvgetn_wstr(txt_y, txt_x, buffer, limit);
307                 }
308                 break;
309             case eMaxFlavor:
310                 break;
311             }
312             noecho();
313             (void) wattrset(txtwin, A_NORMAL);
314             wprintw(strwin, "%d", rc);
315             (void) waddwstr(strwin, (wchar_t *) buffer);
316             wnoutrefresh(strwin);
317             break;
318         default:
319             beep();
320             break;
321         }
322         doupdate();
323     }
324     if (level > 1) {
325         delwin(txtwin);
326         delwin(txtbox);
327     }
328     return TRUE;
329 }
330
331 int
332 main(int argc, char *argv[])
333 {
334     WINDOW *chrbox;
335     WINDOW *strwin;
336
337     setlocale(LC_ALL, "");
338
339     if (argc < 2) {
340         fprintf(stderr, "usage: %s file\n", argv[0]);
341         return EXIT_FAILURE;
342     }
343
344     initscr();
345
346     chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
347     box(chrbox, 0, 0);
348     wnoutrefresh(chrbox);
349
350     strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
351
352     test_get_wstr(1, argv, strwin);
353
354     endwin();
355     ExitProgram(EXIT_SUCCESS);
356 }
357 #else
358 int
359 main(void)
360 {
361     printf("This program requires the wide-ncurses library\n");
362     ExitProgram(EXIT_FAILURE);
363 }
364 #endif
365 #else
366 int
367 main(void)
368 {
369     printf("This program requires the curses chgat function\n");
370     ExitProgram(EXIT_FAILURE);
371 }
372 #endif