1 /****************************************************************************
2 * Copyright (c) 2007-2012,2017 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.13 2017/09/28 23:11:12 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>
46 #include <popup_msg.h>
49 /* Solaris SVr4 curses lacks wchgat, mvgetnstr, mvwgetnstr */
63 * Return-code is OK/ERR or a keyname.
68 return ((code == OK) ? "OK" : ((code == ERR) ? "ERR" : keyname(code)));
74 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
78 Remainder(WINDOW *txtwin)
80 int result = getmaxx(txtwin) - getcurx(txtwin);
81 return (result > 0) ? result : 0;
85 * Show a highlighted line in the place where input will happen.
88 ShowPrompt(WINDOW *txtwin, int limit)
90 wchgat(txtwin, limit, WA_REVERSE, 0, NULL);
95 MovePrompt(WINDOW *txtwin, int limit, int y, int x)
97 wchgat(txtwin, Remainder(txtwin), WA_NORMAL, 0, NULL);
99 ShowPrompt(txtwin, limit);
103 ShowFlavor(WINDOW *strwin, WINDOW *txtwin, int flavor, int limit)
105 const char *name = "?";
106 bool limited = FALSE;
107 bool wins = (txtwin != stdscr);
112 name = wins ? "wgetstr" : "getstr";
116 name = wins ? "wgetnstr" : "getnstr";
119 name = wins ? "mvwgetstr" : "mvgetstr";
123 name = wins ? "mvwgetnstr" : "mvgetnstr";
133 wprintw(strwin, "%s(%d):", name, limit);
135 wprintw(strwin, "%s:", name);
137 result = limited ? limit : Remainder(txtwin);
138 ShowPrompt(txtwin, result);
140 wnoutrefresh(strwin);
145 recursive_test(int level, char **argv, WINDOW *strwin)
147 static const char *help[] =
150 " q,^Q,ESC - quit this program",
151 " ^Q,ESC - quit help-screen",
153 " p,<Up> - move beginning of prompt one up row",
154 " j,<Down> - move beginning of prompt one down row",
155 " h,<Left> - move beginning of prompt one left column",
156 " l,<Right> - move beginning of prompt one right column",
158 " - - reduce getnstr buffer-size one column",
159 " + - increase getnstr buffer-size one column",
160 " : - prompt for input-text",
162 " < - scroll \"left\" through getstr-functions",
163 " > - scroll \"right\" through getstr-functions",
165 " w - recur to subwindow",
166 " ?,<F1> - show help-screen",
174 int txt_x = 0, txt_y = 0;
177 int limit = getmaxx(strwin) - 5;
180 char buffer[MAX_COLS];
182 if (argv[level] == 0) {
188 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
190 wnoutrefresh(txtbox);
192 txtwin = derwin(txtbox,
202 keypad(txtwin, TRUE); /* enable keyboard mapping */
203 (void) cbreak(); /* take input chars one at a time, no wait for \n */
204 (void) noecho(); /* don't echo input */
208 wmove(txtwin, txt_y, txt_x);
210 if ((fp = fopen(argv[level], "r")) != 0) {
211 while ((ch = fgetc(fp)) != EOF) {
212 if (waddch(txtwin, UChar(ch)) != OK) {
218 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
221 wmove(txtwin, txt_y, txt_x);
222 actual = ShowFlavor(strwin, txtwin, flavor, limit);
223 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
227 if (txt_y < getmaxy(txtwin) - 1) {
228 MovePrompt(txtwin, actual, ++txt_y, txt_x);
235 if (txt_y > base_y) {
236 MovePrompt(txtwin, actual, --txt_y, txt_x);
244 MovePrompt(txtwin, actual, txt_y, --txt_x);
251 if (txt_x < getmaxx(txtwin) - 1) {
252 MovePrompt(txtwin, actual, txt_y, ++txt_x);
259 recursive_test(level + 1, argv, strwin);
262 wnoutrefresh(txtbox);
265 wnoutrefresh(txtwin);
271 actual = ShowFlavor(strwin, txtwin, flavor, --limit);
272 MovePrompt(txtwin, actual, txt_y, txt_x);
279 actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
280 MovePrompt(txtwin, actual, txt_y, txt_x);
285 actual = ShowFlavor(strwin, txtwin, --flavor, limit);
286 MovePrompt(txtwin, actual, txt_y, txt_x);
293 if (flavor + 1 < eMaxFlavor) {
294 actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
295 MovePrompt(txtwin, actual, txt_y, txt_x);
302 actual = ShowFlavor(strwin, txtwin, flavor, limit);
306 (void) wattrset(txtwin, A_REVERSE);
309 if (txtwin != stdscr) {
310 wmove(txtwin, txt_y, txt_x);
311 rc = wgetstr(txtwin, buffer);
318 if (txtwin != stdscr) {
319 wmove(txtwin, txt_y, txt_x);
320 rc = wgetnstr(txtwin, buffer, limit);
323 rc = getnstr(buffer, limit);
327 if (txtwin != stdscr) {
328 rc = mvwgetstr(txtwin, txt_y, txt_x, buffer);
330 rc = mvgetstr(txt_y, txt_x, buffer);
334 if (txtwin != stdscr) {
335 rc = mvwgetnstr(txtwin, txt_y, txt_x, buffer, limit);
337 rc = mvgetnstr(txt_y, txt_x, buffer, limit);
344 (void) wattrset(txtwin, A_NORMAL);
345 wprintw(strwin, "%s:%s", ok_keyname(rc), buffer);
346 wnoutrefresh(strwin);
349 popup_msg(stdscr, help);
365 main(int argc, char *argv[])
370 setlocale(LC_ALL, "");
373 fprintf(stderr, "usage: %s file\n", argv[0]);
379 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
381 wnoutrefresh(chrbox);
383 strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
385 recursive_test(1, argv, strwin);
388 ExitProgram(EXIT_SUCCESS);
395 printf("This program requires the curses chgat function\n");
396 ExitProgram(EXIT_FAILURE);