1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 2007-2012,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: test_getstr.c,v 1.14 2020/02/02 23:34:34 tom Exp $
32 * Author: Thomas E Dickey
34 * Demonstrate the getstr functions from the curses library.
36 int getstr(char *str);
37 int getnstr(char *str, int n);
38 int wgetstr(WINDOW *win, char *str);
39 int wgetnstr(WINDOW *win, char *str, int n);
40 int mvgetstr(int y, int x, char *str);
41 int mvwgetstr(WINDOW *win, int y, int x, char *str);
42 int mvgetnstr(int y, int x, char *str, int n);
43 int mvwgetnstr(WINDOW *, int y, int x, char *str, int n);
46 #include <test.priv.h>
47 #include <popup_msg.h>
50 /* Solaris SVr4 curses lacks wchgat, mvgetnstr, mvwgetnstr */
64 * Return-code is OK/ERR or a keyname.
69 return ((code == OK) ? "OK" : ((code == ERR) ? "ERR" : keyname(code)));
75 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
79 Remainder(WINDOW *txtwin)
81 int result = getmaxx(txtwin) - getcurx(txtwin);
82 return (result > 0) ? result : 0;
86 * Show a highlighted line in the place where input will happen.
89 ShowPrompt(WINDOW *txtwin, int limit)
91 wchgat(txtwin, limit, WA_REVERSE, 0, NULL);
96 MovePrompt(WINDOW *txtwin, int limit, int y, int x)
98 wchgat(txtwin, Remainder(txtwin), WA_NORMAL, 0, NULL);
100 ShowPrompt(txtwin, limit);
104 ShowFlavor(WINDOW *strwin, WINDOW *txtwin, int flavor, int limit)
106 const char *name = "?";
107 bool limited = FALSE;
108 bool wins = (txtwin != stdscr);
113 name = wins ? "wgetstr" : "getstr";
117 name = wins ? "wgetnstr" : "getnstr";
120 name = wins ? "mvwgetstr" : "mvgetstr";
124 name = wins ? "mvwgetnstr" : "mvgetnstr";
134 wprintw(strwin, "%s(%d):", name, limit);
136 wprintw(strwin, "%s:", name);
138 result = limited ? limit : Remainder(txtwin);
139 ShowPrompt(txtwin, result);
141 wnoutrefresh(strwin);
146 recursive_test(int level, char **argv, WINDOW *strwin)
148 static const char *help[] =
151 " q,^Q,ESC - quit this program",
152 " ^Q,ESC - quit help-screen",
154 " p,<Up> - move beginning of prompt one up row",
155 " j,<Down> - move beginning of prompt one down row",
156 " h,<Left> - move beginning of prompt one left column",
157 " l,<Right> - move beginning of prompt one right column",
159 " - - reduce getnstr buffer-size one column",
160 " + - increase getnstr buffer-size one column",
161 " : - prompt for input-text",
163 " < - scroll \"left\" through getstr-functions",
164 " > - scroll \"right\" through getstr-functions",
166 " w - recur to subwindow",
167 " ?,<F1> - show help-screen",
175 int txt_x = 0, txt_y = 0;
178 int limit = getmaxx(strwin) - 5;
181 char buffer[MAX_COLS];
183 if (argv[level] == 0) {
189 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
191 wnoutrefresh(txtbox);
193 txtwin = derwin(txtbox,
203 keypad(txtwin, TRUE); /* enable keyboard mapping */
204 (void) cbreak(); /* take input chars one at a time, no wait for \n */
205 (void) noecho(); /* don't echo input */
209 wmove(txtwin, txt_y, txt_x);
211 if ((fp = fopen(argv[level], "r")) != 0) {
212 while ((ch = fgetc(fp)) != EOF) {
213 if (waddch(txtwin, UChar(ch)) != OK) {
219 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
222 wmove(txtwin, txt_y, txt_x);
223 actual = ShowFlavor(strwin, txtwin, flavor, limit);
224 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
228 if (txt_y < getmaxy(txtwin) - 1) {
229 MovePrompt(txtwin, actual, ++txt_y, txt_x);
236 if (txt_y > base_y) {
237 MovePrompt(txtwin, actual, --txt_y, txt_x);
245 MovePrompt(txtwin, actual, txt_y, --txt_x);
252 if (txt_x < getmaxx(txtwin) - 1) {
253 MovePrompt(txtwin, actual, txt_y, ++txt_x);
260 recursive_test(level + 1, argv, strwin);
263 wnoutrefresh(txtbox);
266 wnoutrefresh(txtwin);
272 actual = ShowFlavor(strwin, txtwin, flavor, --limit);
273 MovePrompt(txtwin, actual, txt_y, txt_x);
280 actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
281 MovePrompt(txtwin, actual, txt_y, txt_x);
286 actual = ShowFlavor(strwin, txtwin, --flavor, limit);
287 MovePrompt(txtwin, actual, txt_y, txt_x);
294 if (flavor + 1 < eMaxFlavor) {
295 actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
296 MovePrompt(txtwin, actual, txt_y, txt_x);
303 actual = ShowFlavor(strwin, txtwin, flavor, limit);
307 (void) wattrset(txtwin, A_REVERSE);
310 if (txtwin != stdscr) {
311 wmove(txtwin, txt_y, txt_x);
312 rc = wgetstr(txtwin, buffer);
319 if (txtwin != stdscr) {
320 wmove(txtwin, txt_y, txt_x);
321 rc = wgetnstr(txtwin, buffer, limit);
324 rc = getnstr(buffer, limit);
328 if (txtwin != stdscr) {
329 rc = mvwgetstr(txtwin, txt_y, txt_x, buffer);
331 rc = mvgetstr(txt_y, txt_x, buffer);
335 if (txtwin != stdscr) {
336 rc = mvwgetnstr(txtwin, txt_y, txt_x, buffer, limit);
338 rc = mvgetnstr(txt_y, txt_x, buffer, limit);
345 (void) wattrset(txtwin, A_NORMAL);
346 wprintw(strwin, "%s:%s", ok_keyname(rc), buffer);
347 wnoutrefresh(strwin);
350 popup_msg(stdscr, help);
366 main(int argc, char *argv[])
371 setlocale(LC_ALL, "");
374 fprintf(stderr, "usage: %s file\n", argv[0]);
380 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
382 wnoutrefresh(chrbox);
384 strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
386 recursive_test(1, argv, strwin);
389 ExitProgram(EXIT_SUCCESS);
396 printf("This program requires the curses chgat function\n");
397 ExitProgram(EXIT_FAILURE);