1 /****************************************************************************
2 * Copyright (c) 2007-2008,2009 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_getstr.c,v 1.9 2009/08/29 19:02:25 tom Exp $
31 * Author: Thomas E Dickey
33 * Demonstrate the getstr functions from the curses library.
35 int getstr(char *str);
36 int getnstr(char *str, int n);
37 int wgetstr(WINDOW *win, char *str);
38 int wgetnstr(WINDOW *win, char *str, int n);
39 int mvgetstr(int y, int x, char *str);
40 int mvwgetstr(WINDOW *win, int y, int x, char *str);
41 int mvgetnstr(int y, int x, char *str, int n);
42 int mvwgetnstr(WINDOW *, int y, int x, char *str, int n);
45 #include <test.priv.h>
48 /* Solaris SVr4 curses lacks wchgat, mvgetnstr, mvwgetnstr */
64 return (ch
== ERR
|| ch
== 'q' || ch
== QUIT
|| ch
== ESCAPE
);
68 Remainder(WINDOW
*txtwin
)
70 int result
= getmaxx(txtwin
) - getcurx(txtwin
);
71 return (result
> 0) ? result
: 0;
75 * Show a highlighted line in the place where input will happen.
78 ShowPrompt(WINDOW
*txtwin
, int limit
)
80 wchgat(txtwin
, limit
, A_REVERSE
, 0, NULL
);
85 MovePrompt(WINDOW
*txtwin
, int limit
, int y
, int x
)
87 wchgat(txtwin
, Remainder(txtwin
), A_NORMAL
, 0, NULL
);
89 ShowPrompt(txtwin
, limit
);
93 ShowFlavor(WINDOW
*strwin
, WINDOW
*txtwin
, int flavor
, int limit
)
95 const char *name
= "?";
97 bool wins
= (txtwin
!= stdscr
);
102 name
= wins
? "wgetstr" : "getstr";
106 name
= wins
? "wgetnstr" : "getnstr";
109 name
= wins
? "mvwgetstr" : "mvgetstr";
113 name
= wins
? "mvwgetnstr" : "mvgetnstr";
123 wprintw(strwin
, "%s(%d):", name
, limit
);
125 wprintw(strwin
, "%s:", name
);
127 result
= limited
? limit
: Remainder(txtwin
);
128 ShowPrompt(txtwin
, result
);
130 wnoutrefresh(strwin
);
135 test_getstr(int level
, char **argv
, WINDOW
*strwin
)
142 int txt_x
= 0, txt_y
= 0;
145 int limit
= getmaxx(strwin
) - 5;
148 char buffer
[MAX_COLS
];
150 if (argv
[level
] == 0) {
156 txtbox
= newwin(LINES
- BASE_Y
, COLS
- level
, BASE_Y
, level
);
158 wnoutrefresh(txtbox
);
160 txtwin
= derwin(txtbox
,
170 keypad(txtwin
, TRUE
); /* enable keyboard mapping */
171 (void) cbreak(); /* take input chars one at a time, no wait for \n */
172 (void) noecho(); /* don't echo input */
176 wmove(txtwin
, txt_y
, txt_x
);
178 if ((fp
= fopen(argv
[level
], "r")) != 0) {
179 while ((ch
= fgetc(fp
)) != EOF
) {
180 if (waddch(txtwin
, UChar(ch
)) != OK
) {
186 wprintw(txtwin
, "Cannot open:\n%s", argv
[1]);
189 wmove(txtwin
, txt_y
, txt_x
);
190 actual
= ShowFlavor(strwin
, txtwin
, flavor
, limit
);
191 while (!Quit(ch
= mvwgetch(txtwin
, txt_y
, txt_x
))) {
195 if (txt_y
< getmaxy(txtwin
) - 1) {
196 MovePrompt(txtwin
, actual
, ++txt_y
, txt_x
);
203 if (txt_y
> base_y
) {
204 MovePrompt(txtwin
, actual
, --txt_y
, txt_x
);
212 MovePrompt(txtwin
, actual
, txt_y
, --txt_x
);
219 if (txt_x
< getmaxx(txtwin
) - 1) {
220 MovePrompt(txtwin
, actual
, txt_y
, ++txt_x
);
227 test_getstr(level
+ 1, argv
, strwin
);
230 wnoutrefresh(txtbox
);
233 wnoutrefresh(txtwin
);
239 actual
= ShowFlavor(strwin
, txtwin
, flavor
, --limit
);
240 MovePrompt(txtwin
, actual
, txt_y
, txt_x
);
247 actual
= ShowFlavor(strwin
, txtwin
, flavor
, ++limit
);
248 MovePrompt(txtwin
, actual
, txt_y
, txt_x
);
253 actual
= ShowFlavor(strwin
, txtwin
, --flavor
, limit
);
254 MovePrompt(txtwin
, actual
, txt_y
, txt_x
);
261 if (flavor
+ 1 < eMaxFlavor
) {
262 actual
= ShowFlavor(strwin
, txtwin
, ++flavor
, limit
);
263 MovePrompt(txtwin
, actual
, txt_y
, txt_x
);
270 actual
= ShowFlavor(strwin
, txtwin
, flavor
, limit
);
274 (void) wattrset(txtwin
, A_REVERSE
);
277 if (txtwin
!= stdscr
) {
278 wmove(txtwin
, txt_y
, txt_x
);
279 rc
= wgetstr(txtwin
, buffer
);
286 if (txtwin
!= stdscr
) {
287 wmove(txtwin
, txt_y
, txt_x
);
288 rc
= wgetnstr(txtwin
, buffer
, limit
);
291 rc
= getnstr(buffer
, limit
);
295 if (txtwin
!= stdscr
) {
296 rc
= mvwgetstr(txtwin
, txt_y
, txt_x
, buffer
);
298 rc
= mvgetstr(txt_y
, txt_x
, buffer
);
302 if (txtwin
!= stdscr
) {
303 rc
= mvwgetnstr(txtwin
, txt_y
, txt_x
, buffer
, limit
);
305 rc
= mvgetnstr(txt_y
, txt_x
, buffer
, limit
);
312 (void) wattrset(txtwin
, A_NORMAL
);
313 wprintw(strwin
, "%d:%s", rc
, buffer
);
314 wnoutrefresh(strwin
);
330 main(int argc
, char *argv
[])
335 setlocale(LC_ALL
, "");
338 fprintf(stderr
, "usage: %s file\n", argv
[0]);
344 chrbox
= derwin(stdscr
, BASE_Y
, COLS
, 0, 0);
346 wnoutrefresh(chrbox
);
348 strwin
= derwin(chrbox
, 4, COLS
- 2, 1, 1);
350 test_getstr(1, argv
, strwin
);
353 ExitProgram(EXIT_SUCCESS
);
360 printf("This program requires the curses chgat function\n");
361 ExitProgram(EXIT_FAILURE
);