1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 2007-2011,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_get_wstr.c,v 1.12 2020/02/02 23:34:34 tom Exp $
32 * Author: Thomas E Dickey
34 * Demonstrate the get_wstr functions from the curses library.
36 int get_wstr(wint_t *wstr);
37 int getn_wstr(wint_t *wstr, int n);
38 int wget_wstr(WINDOW *win, wint_t *wstr);
39 int wgetn_wstr(WINDOW *win, wint_t *wstr, int n);
40 int mvget_wstr(int y, int x, wint_t *wstr);
41 int mvgetn_wstr(int y, int x, wint_t *wstr, int n);
42 int mvwget_wstr(WINDOW *win, int y, int x, wint_t *wstr);
43 int mvwgetn_wstr(WINDOW *win, int y, int x, wint_t *wstr, int n);
46 #include <test.priv.h>
47 #include <popup_msg.h>
50 /* NetBSD curses wchgat */
68 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
72 Remainder(WINDOW *txtwin)
74 int result = getmaxx(txtwin) - getcurx(txtwin);
75 return (result > 0) ? result : 0;
79 * Show a highlighted line in the place where input will happen.
82 ShowPrompt(WINDOW *txtwin, int limit)
84 wchgat(txtwin, limit, WA_REVERSE, 0, NULL);
89 MovePrompt(WINDOW *txtwin, int limit, int y, int x)
91 wchgat(txtwin, Remainder(txtwin), WA_NORMAL, 0, NULL);
93 ShowPrompt(txtwin, limit);
97 ShowFlavor(WINDOW *strwin, WINDOW *txtwin, int flavor, int limit)
99 const char *name = "?";
100 bool limited = FALSE;
101 bool wins = (txtwin != stdscr);
106 name = wins ? "wget_wstr" : "get_wstr";
110 name = wins ? "wgetn_wstr" : "getn_wstr";
113 name = wins ? "mvwget_wstr" : "mvget_wstr";
117 name = wins ? "mvwgetn_wstr" : "mvgetn_wstr";
127 wprintw(strwin, "%s(%d):", name, limit);
129 wprintw(strwin, "%s:", name);
131 result = limited ? limit : Remainder(txtwin);
132 ShowPrompt(txtwin, result);
134 wnoutrefresh(strwin);
139 recursive_test(int level, char **argv, WINDOW *strwin)
141 static const char *help[] =
144 " q,^Q,ESC - quit this program",
145 " ^Q,ESC - quit help-screen",
147 " p,<Up> - move beginning of prompt one up row",
148 " j,<Down> - move beginning of prompt one down row",
149 " h,<Left> - move beginning of prompt one left column",
150 " l,<Right> - move beginning of prompt one right column",
152 " - - reduce getnstr buffer-size one column",
153 " + - increase getnstr buffer-size one column",
154 " : - prompt for input-text",
156 " < - scroll \"left\" through getstr-functions",
157 " > - scroll \"right\" through getstr-functions",
159 " w - recur to subwindow",
160 " ?,<F1> - show help-screen",
168 int txt_x = 0, txt_y = 0;
171 int limit = getmaxx(strwin) - 5;
173 wint_t buffer[MAX_COLS];
175 if (argv[level] == 0) {
181 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
183 wnoutrefresh(txtbox);
185 txtwin = derwin(txtbox,
195 keypad(txtwin, TRUE); /* enable keyboard mapping */
196 (void) cbreak(); /* take input chars one at a time, no wait for \n */
197 (void) noecho(); /* don't echo input */
201 wmove(txtwin, txt_y, txt_x);
203 if ((fp = fopen(argv[level], "r")) != 0) {
204 while ((ch = fgetc(fp)) != EOF) {
205 if (waddch(txtwin, UChar(ch)) != OK) {
211 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
214 wmove(txtwin, txt_y, txt_x);
215 actual = ShowFlavor(strwin, txtwin, flavor, limit);
216 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
220 if (txt_y < getmaxy(txtwin) - 1) {
221 MovePrompt(txtwin, actual, ++txt_y, txt_x);
228 if (txt_y > base_y) {
229 MovePrompt(txtwin, actual, --txt_y, txt_x);
237 MovePrompt(txtwin, actual, txt_y, --txt_x);
244 if (txt_x < getmaxx(txtwin) - 1) {
245 MovePrompt(txtwin, actual, txt_y, ++txt_x);
252 recursive_test(level + 1, argv, strwin);
255 wnoutrefresh(txtbox);
258 wnoutrefresh(txtwin);
264 actual = ShowFlavor(strwin, txtwin, flavor, --limit);
265 MovePrompt(txtwin, actual, txt_y, txt_x);
272 actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
273 MovePrompt(txtwin, actual, txt_y, txt_x);
278 actual = ShowFlavor(strwin, txtwin, --flavor, limit);
279 MovePrompt(txtwin, actual, txt_y, txt_x);
286 if (flavor + 1 < eMaxFlavor) {
287 actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
288 MovePrompt(txtwin, actual, txt_y, txt_x);
295 actual = ShowFlavor(strwin, txtwin, flavor, limit);
299 (void) wattrset(txtwin, A_REVERSE);
302 if (txtwin != stdscr) {
303 wmove(txtwin, txt_y, txt_x);
304 rc = wget_wstr(txtwin, buffer);
307 rc = get_wstr(buffer);
311 if (txtwin != stdscr) {
312 wmove(txtwin, txt_y, txt_x);
313 rc = wgetn_wstr(txtwin, buffer, limit);
316 rc = getn_wstr(buffer, limit);
320 if (txtwin != stdscr) {
321 rc = mvwget_wstr(txtwin, txt_y, txt_x, buffer);
323 rc = mvget_wstr(txt_y, txt_x, buffer);
327 if (txtwin != stdscr) {
328 rc = mvwgetn_wstr(txtwin, txt_y, txt_x, buffer, limit);
330 rc = mvgetn_wstr(txt_y, txt_x, buffer, limit);
337 (void) wattrset(txtwin, A_NORMAL);
338 wprintw(strwin, "%s:", (rc == OK) ? "OK" : "ERR");
339 (void) waddwstr(strwin, (wchar_t *) buffer);
340 wnoutrefresh(strwin);
343 popup_msg(stdscr, help);
359 main(int argc, char *argv[])
364 setlocale(LC_ALL, "");
367 fprintf(stderr, "usage: %s file\n", argv[0]);
373 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
375 wnoutrefresh(chrbox);
377 strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
379 recursive_test(1, argv, strwin);
382 ExitProgram(EXIT_SUCCESS);
388 printf("This program requires the wide-ncurses library\n");
389 ExitProgram(EXIT_FAILURE);
396 printf("This program requires the curses chgat function\n");
397 ExitProgram(EXIT_FAILURE);