1 /****************************************************************************
2 * Copyright (c) 2007-2009,2011 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: test_get_wstr.c,v 1.8 2011/01/15 18:15:11 tom Exp $
31 * Author: Thomas E Dickey
33 * Demonstrate the get_wstr functions from the curses library.
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);
45 #include <test.priv.h>
48 /* NetBSD curses wchgat */
66 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
70 Remainder(WINDOW *txtwin)
72 int result = getmaxx(txtwin) - getcurx(txtwin);
73 return (result > 0) ? result : 0;
77 * Show a highlighted line in the place where input will happen.
80 ShowPrompt(WINDOW *txtwin, int limit)
82 wchgat(txtwin, limit, A_REVERSE, 0, NULL);
87 MovePrompt(WINDOW *txtwin, int limit, int y, int x)
89 wchgat(txtwin, Remainder(txtwin), A_NORMAL, 0, NULL);
91 ShowPrompt(txtwin, limit);
95 ShowFlavor(WINDOW *strwin, WINDOW *txtwin, int flavor, int limit)
97 const char *name = "?";
99 bool wins = (txtwin != stdscr);
104 name = wins ? "wget_wstr" : "get_wstr";
108 name = wins ? "wgetn_wstr" : "getn_wstr";
111 name = wins ? "mvwget_wstr" : "mvget_wstr";
115 name = wins ? "mvwgetn_wstr" : "mvgetn_wstr";
125 wprintw(strwin, "%s(%d):", name, limit);
127 wprintw(strwin, "%s:", name);
129 result = limited ? limit : Remainder(txtwin);
130 ShowPrompt(txtwin, result);
132 wnoutrefresh(strwin);
137 test_get_wstr(int level, char **argv, WINDOW *strwin)
144 int txt_x = 0, txt_y = 0;
147 int limit = getmaxx(strwin) - 5;
149 wint_t buffer[MAX_COLS];
151 if (argv[level] == 0) {
157 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
159 wnoutrefresh(txtbox);
161 txtwin = derwin(txtbox,
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 */
177 wmove(txtwin, txt_y, txt_x);
179 if ((fp = fopen(argv[level], "r")) != 0) {
180 while ((ch = fgetc(fp)) != EOF) {
181 if (waddch(txtwin, UChar(ch)) != OK) {
187 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
190 wmove(txtwin, txt_y, txt_x);
191 actual = ShowFlavor(strwin, txtwin, flavor, limit);
192 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
196 if (txt_y < getmaxy(txtwin) - 1) {
197 MovePrompt(txtwin, actual, ++txt_y, txt_x);
204 if (txt_y > base_y) {
205 MovePrompt(txtwin, actual, --txt_y, txt_x);
213 MovePrompt(txtwin, actual, txt_y, --txt_x);
220 if (txt_x < getmaxx(txtwin) - 1) {
221 MovePrompt(txtwin, actual, txt_y, ++txt_x);
228 test_get_wstr(level + 1, argv, strwin);
231 wnoutrefresh(txtbox);
234 wnoutrefresh(txtwin);
240 actual = ShowFlavor(strwin, txtwin, flavor, --limit);
241 MovePrompt(txtwin, actual, txt_y, txt_x);
248 actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
249 MovePrompt(txtwin, actual, txt_y, txt_x);
254 actual = ShowFlavor(strwin, txtwin, --flavor, limit);
255 MovePrompt(txtwin, actual, txt_y, txt_x);
262 if (flavor + 1 < eMaxFlavor) {
263 actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
264 MovePrompt(txtwin, actual, txt_y, txt_x);
271 actual = ShowFlavor(strwin, txtwin, flavor, limit);
275 (void) wattrset(txtwin, A_REVERSE);
278 if (txtwin != stdscr) {
279 wmove(txtwin, txt_y, txt_x);
280 rc = wget_wstr(txtwin, buffer);
283 rc = get_wstr(buffer);
287 if (txtwin != stdscr) {
288 wmove(txtwin, txt_y, txt_x);
289 rc = wgetn_wstr(txtwin, buffer, limit);
292 rc = getn_wstr(buffer, limit);
296 if (txtwin != stdscr) {
297 rc = mvwget_wstr(txtwin, txt_y, txt_x, buffer);
299 rc = mvget_wstr(txt_y, txt_x, buffer);
303 if (txtwin != stdscr) {
304 rc = mvwgetn_wstr(txtwin, txt_y, txt_x, buffer, limit);
306 rc = mvgetn_wstr(txt_y, txt_x, buffer, limit);
313 (void) wattrset(txtwin, A_NORMAL);
314 wprintw(strwin, "%d", rc);
315 (void) waddwstr(strwin, (wchar_t *) buffer);
316 wnoutrefresh(strwin);
332 main(int argc, char *argv[])
337 setlocale(LC_ALL, "");
340 fprintf(stderr, "usage: %s file\n", argv[0]);
346 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
348 wnoutrefresh(chrbox);
350 strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
352 test_get_wstr(1, argv, strwin);
355 ExitProgram(EXIT_SUCCESS);
361 printf("This program requires the wide-ncurses library\n");
362 ExitProgram(EXIT_FAILURE);
369 printf("This program requires the curses chgat function\n");
370 ExitProgram(EXIT_FAILURE);