1 /****************************************************************************
2 * Copyright (c) 2007-2011,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_get_wstr.c,v 1.11 2017/09/28 23:10:54 tom Exp $
31 * Author: Thomas E Dickey
33 * Demonstrate the get_wstr functions from the curses library.
35 int get_wstr(wint_t *wstr);
36 int getn_wstr(wint_t *wstr, int n);
37 int wget_wstr(WINDOW *win, wint_t *wstr);
38 int wgetn_wstr(WINDOW *win, wint_t *wstr, int n);
39 int mvget_wstr(int y, int x, wint_t *wstr);
40 int mvgetn_wstr(int y, int x, wint_t *wstr, int n);
41 int mvwget_wstr(WINDOW *win, int y, int x, wint_t *wstr);
42 int mvwgetn_wstr(WINDOW *win, int y, int x, wint_t *wstr, int n);
45 #include <test.priv.h>
46 #include <popup_msg.h>
49 /* NetBSD curses wchgat */
67 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
71 Remainder(WINDOW *txtwin)
73 int result = getmaxx(txtwin) - getcurx(txtwin);
74 return (result > 0) ? result : 0;
78 * Show a highlighted line in the place where input will happen.
81 ShowPrompt(WINDOW *txtwin, int limit)
83 wchgat(txtwin, limit, WA_REVERSE, 0, NULL);
88 MovePrompt(WINDOW *txtwin, int limit, int y, int x)
90 wchgat(txtwin, Remainder(txtwin), WA_NORMAL, 0, NULL);
92 ShowPrompt(txtwin, limit);
96 ShowFlavor(WINDOW *strwin, WINDOW *txtwin, int flavor, int limit)
98 const char *name = "?";
100 bool wins = (txtwin != stdscr);
105 name = wins ? "wget_wstr" : "get_wstr";
109 name = wins ? "wgetn_wstr" : "getn_wstr";
112 name = wins ? "mvwget_wstr" : "mvget_wstr";
116 name = wins ? "mvwgetn_wstr" : "mvgetn_wstr";
126 wprintw(strwin, "%s(%d):", name, limit);
128 wprintw(strwin, "%s:", name);
130 result = limited ? limit : Remainder(txtwin);
131 ShowPrompt(txtwin, result);
133 wnoutrefresh(strwin);
138 recursive_test(int level, char **argv, WINDOW *strwin)
140 static const char *help[] =
143 " q,^Q,ESC - quit this program",
144 " ^Q,ESC - quit help-screen",
146 " p,<Up> - move beginning of prompt one up row",
147 " j,<Down> - move beginning of prompt one down row",
148 " h,<Left> - move beginning of prompt one left column",
149 " l,<Right> - move beginning of prompt one right column",
151 " - - reduce getnstr buffer-size one column",
152 " + - increase getnstr buffer-size one column",
153 " : - prompt for input-text",
155 " < - scroll \"left\" through getstr-functions",
156 " > - scroll \"right\" through getstr-functions",
158 " w - recur to subwindow",
159 " ?,<F1> - show help-screen",
167 int txt_x = 0, txt_y = 0;
170 int limit = getmaxx(strwin) - 5;
172 wint_t buffer[MAX_COLS];
174 if (argv[level] == 0) {
180 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
182 wnoutrefresh(txtbox);
184 txtwin = derwin(txtbox,
194 keypad(txtwin, TRUE); /* enable keyboard mapping */
195 (void) cbreak(); /* take input chars one at a time, no wait for \n */
196 (void) noecho(); /* don't echo input */
200 wmove(txtwin, txt_y, txt_x);
202 if ((fp = fopen(argv[level], "r")) != 0) {
203 while ((ch = fgetc(fp)) != EOF) {
204 if (waddch(txtwin, UChar(ch)) != OK) {
210 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
213 wmove(txtwin, txt_y, txt_x);
214 actual = ShowFlavor(strwin, txtwin, flavor, limit);
215 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
219 if (txt_y < getmaxy(txtwin) - 1) {
220 MovePrompt(txtwin, actual, ++txt_y, txt_x);
227 if (txt_y > base_y) {
228 MovePrompt(txtwin, actual, --txt_y, txt_x);
236 MovePrompt(txtwin, actual, txt_y, --txt_x);
243 if (txt_x < getmaxx(txtwin) - 1) {
244 MovePrompt(txtwin, actual, txt_y, ++txt_x);
251 recursive_test(level + 1, argv, strwin);
254 wnoutrefresh(txtbox);
257 wnoutrefresh(txtwin);
263 actual = ShowFlavor(strwin, txtwin, flavor, --limit);
264 MovePrompt(txtwin, actual, txt_y, txt_x);
271 actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
272 MovePrompt(txtwin, actual, txt_y, txt_x);
277 actual = ShowFlavor(strwin, txtwin, --flavor, limit);
278 MovePrompt(txtwin, actual, txt_y, txt_x);
285 if (flavor + 1 < eMaxFlavor) {
286 actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
287 MovePrompt(txtwin, actual, txt_y, txt_x);
294 actual = ShowFlavor(strwin, txtwin, flavor, limit);
298 (void) wattrset(txtwin, A_REVERSE);
301 if (txtwin != stdscr) {
302 wmove(txtwin, txt_y, txt_x);
303 rc = wget_wstr(txtwin, buffer);
306 rc = get_wstr(buffer);
310 if (txtwin != stdscr) {
311 wmove(txtwin, txt_y, txt_x);
312 rc = wgetn_wstr(txtwin, buffer, limit);
315 rc = getn_wstr(buffer, limit);
319 if (txtwin != stdscr) {
320 rc = mvwget_wstr(txtwin, txt_y, txt_x, buffer);
322 rc = mvget_wstr(txt_y, txt_x, buffer);
326 if (txtwin != stdscr) {
327 rc = mvwgetn_wstr(txtwin, txt_y, txt_x, buffer, limit);
329 rc = mvgetn_wstr(txt_y, txt_x, buffer, limit);
336 (void) wattrset(txtwin, A_NORMAL);
337 wprintw(strwin, "%s:", (rc == OK) ? "OK" : "ERR");
338 (void) waddwstr(strwin, (wchar_t *) buffer);
339 wnoutrefresh(strwin);
342 popup_msg(stdscr, help);
358 main(int argc, char *argv[])
363 setlocale(LC_ALL, "");
366 fprintf(stderr, "usage: %s file\n", argv[0]);
372 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
374 wnoutrefresh(chrbox);
376 strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
378 recursive_test(1, argv, strwin);
381 ExitProgram(EXIT_SUCCESS);
387 printf("This program requires the wide-ncurses library\n");
388 ExitProgram(EXIT_FAILURE);
395 printf("This program requires the curses chgat function\n");
396 ExitProgram(EXIT_FAILURE);