1 /****************************************************************************
2 * Copyright (c) 2007-2009,2012 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.10 2012/07/07 18:22:49 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 */
62 * Return-code is OK/ERR or a keyname.
67 return ((code == OK) ? "OK" : ((code == ERR) ? "ERR" : keyname(code)));
73 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
77 Remainder(WINDOW *txtwin)
79 int result = getmaxx(txtwin) - getcurx(txtwin);
80 return (result > 0) ? result : 0;
84 * Show a highlighted line in the place where input will happen.
87 ShowPrompt(WINDOW *txtwin, int limit)
89 wchgat(txtwin, limit, A_REVERSE, 0, NULL);
94 MovePrompt(WINDOW *txtwin, int limit, int y, int x)
96 wchgat(txtwin, Remainder(txtwin), A_NORMAL, 0, NULL);
98 ShowPrompt(txtwin, limit);
102 ShowFlavor(WINDOW *strwin, WINDOW *txtwin, int flavor, int limit)
104 const char *name = "?";
105 bool limited = FALSE;
106 bool wins = (txtwin != stdscr);
111 name = wins ? "wgetstr" : "getstr";
115 name = wins ? "wgetnstr" : "getnstr";
118 name = wins ? "mvwgetstr" : "mvgetstr";
122 name = wins ? "mvwgetnstr" : "mvgetnstr";
132 wprintw(strwin, "%s(%d):", name, limit);
134 wprintw(strwin, "%s:", name);
136 result = limited ? limit : Remainder(txtwin);
137 ShowPrompt(txtwin, result);
139 wnoutrefresh(strwin);
144 test_getstr(int level, char **argv, WINDOW *strwin)
151 int txt_x = 0, txt_y = 0;
154 int limit = getmaxx(strwin) - 5;
157 char buffer[MAX_COLS];
159 if (argv[level] == 0) {
165 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
167 wnoutrefresh(txtbox);
169 txtwin = derwin(txtbox,
179 keypad(txtwin, TRUE); /* enable keyboard mapping */
180 (void) cbreak(); /* take input chars one at a time, no wait for \n */
181 (void) noecho(); /* don't echo input */
185 wmove(txtwin, txt_y, txt_x);
187 if ((fp = fopen(argv[level], "r")) != 0) {
188 while ((ch = fgetc(fp)) != EOF) {
189 if (waddch(txtwin, UChar(ch)) != OK) {
195 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
198 wmove(txtwin, txt_y, txt_x);
199 actual = ShowFlavor(strwin, txtwin, flavor, limit);
200 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
204 if (txt_y < getmaxy(txtwin) - 1) {
205 MovePrompt(txtwin, actual, ++txt_y, txt_x);
212 if (txt_y > base_y) {
213 MovePrompt(txtwin, actual, --txt_y, txt_x);
221 MovePrompt(txtwin, actual, txt_y, --txt_x);
228 if (txt_x < getmaxx(txtwin) - 1) {
229 MovePrompt(txtwin, actual, txt_y, ++txt_x);
236 test_getstr(level + 1, argv, strwin);
239 wnoutrefresh(txtbox);
242 wnoutrefresh(txtwin);
248 actual = ShowFlavor(strwin, txtwin, flavor, --limit);
249 MovePrompt(txtwin, actual, txt_y, txt_x);
256 actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
257 MovePrompt(txtwin, actual, txt_y, txt_x);
262 actual = ShowFlavor(strwin, txtwin, --flavor, limit);
263 MovePrompt(txtwin, actual, txt_y, txt_x);
270 if (flavor + 1 < eMaxFlavor) {
271 actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
272 MovePrompt(txtwin, actual, txt_y, txt_x);
279 actual = ShowFlavor(strwin, txtwin, flavor, limit);
283 (void) wattrset(txtwin, A_REVERSE);
286 if (txtwin != stdscr) {
287 wmove(txtwin, txt_y, txt_x);
288 rc = wgetstr(txtwin, buffer);
295 if (txtwin != stdscr) {
296 wmove(txtwin, txt_y, txt_x);
297 rc = wgetnstr(txtwin, buffer, limit);
300 rc = getnstr(buffer, limit);
304 if (txtwin != stdscr) {
305 rc = mvwgetstr(txtwin, txt_y, txt_x, buffer);
307 rc = mvgetstr(txt_y, txt_x, buffer);
311 if (txtwin != stdscr) {
312 rc = mvwgetnstr(txtwin, txt_y, txt_x, buffer, limit);
314 rc = mvgetnstr(txt_y, txt_x, buffer, limit);
321 (void) wattrset(txtwin, A_NORMAL);
322 wprintw(strwin, "%s:%s", ok_keyname(rc), buffer);
323 wnoutrefresh(strwin);
339 main(int argc, char *argv[])
344 setlocale(LC_ALL, "");
347 fprintf(stderr, "usage: %s file\n", argv[0]);
353 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
355 wnoutrefresh(chrbox);
357 strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
359 test_getstr(1, argv, strwin);
362 ExitProgram(EXIT_SUCCESS);
369 printf("This program requires the curses chgat function\n");
370 ExitProgram(EXIT_FAILURE);