1 /****************************************************************************
2 * Copyright (c) 2007 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.6 2007/07/28 19:46:43 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>
61 return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
65 Remainder(WINDOW *txtwin)
67 int result = getmaxx(txtwin) - getcurx(txtwin);
68 return (result > 0) ? result : 0;
72 * Show a highlighted line in the place where input will happen.
75 ShowPrompt(WINDOW *txtwin, int limit)
77 wchgat(txtwin, limit, A_REVERSE, 0, NULL);
82 MovePrompt(WINDOW *txtwin, int limit, int y, int x)
84 wchgat(txtwin, Remainder(txtwin), A_NORMAL, 0, NULL);
86 ShowPrompt(txtwin, limit);
90 ShowFlavor(WINDOW *strwin, WINDOW *txtwin, Flavors flavor, int limit)
92 const char *name = "?";
94 bool wins = (txtwin != stdscr);
99 name = wins ? "wgetstr" : "getstr";
103 name = wins ? "wgetnstr" : "getnstr";
106 name = wins ? "mvwgetstr" : "mvgetstr";
110 name = wins ? "mvwgetnstr" : "mvgetnstr";
120 wprintw(strwin, "%s(%d):", name, limit);
122 wprintw(strwin, "%s:", name);
124 result = limited ? limit : Remainder(txtwin);
125 ShowPrompt(txtwin, result);
127 wnoutrefresh(strwin);
132 test_getstr(int level, char **argv, WINDOW *strwin)
139 int txt_x = 0, txt_y = 0;
142 int limit = getmaxx(strwin) - 5;
145 char buffer[MAX_COLS];
147 if (argv[level] == 0) {
153 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
155 wnoutrefresh(txtbox);
157 txtwin = derwin(txtbox,
167 keypad(txtwin, TRUE); /* enable keyboard mapping */
168 (void) cbreak(); /* take input chars one at a time, no wait for \n */
169 (void) noecho(); /* don't echo input */
173 wmove(txtwin, txt_y, txt_x);
175 if ((fp = fopen(argv[level], "r")) != 0) {
176 while ((ch = fgetc(fp)) != EOF) {
177 if (waddch(txtwin, UChar(ch)) != OK) {
183 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
186 wmove(txtwin, txt_y, txt_x);
187 actual = ShowFlavor(strwin, txtwin, flavor, limit);
188 while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
192 if (txt_y < getmaxy(txtwin) - 1) {
193 MovePrompt(txtwin, actual, ++txt_y, txt_x);
200 if (txt_y > base_y) {
201 MovePrompt(txtwin, actual, --txt_y, txt_x);
209 MovePrompt(txtwin, actual, txt_y, --txt_x);
216 if (txt_x < getmaxx(txtwin) - 1) {
217 MovePrompt(txtwin, actual, txt_y, ++txt_x);
224 test_getstr(level + 1, argv, strwin);
227 wnoutrefresh(txtbox);
230 wnoutrefresh(txtwin);
236 actual = ShowFlavor(strwin, txtwin, flavor, --limit);
237 MovePrompt(txtwin, actual, txt_y, txt_x);
244 actual = ShowFlavor(strwin, txtwin, flavor, ++limit);
245 MovePrompt(txtwin, actual, txt_y, txt_x);
250 actual = ShowFlavor(strwin, txtwin, --flavor, limit);
251 MovePrompt(txtwin, actual, txt_y, txt_x);
258 if (flavor + 1 < eMaxFlavor) {
259 actual = ShowFlavor(strwin, txtwin, ++flavor, limit);
260 MovePrompt(txtwin, actual, txt_y, txt_x);
267 actual = ShowFlavor(strwin, txtwin, flavor, limit);
271 wattrset(txtwin, A_REVERSE);
274 if (txtwin != stdscr) {
275 wmove(txtwin, txt_y, txt_x);
276 rc = wgetstr(txtwin, buffer);
283 if (txtwin != stdscr) {
284 wmove(txtwin, txt_y, txt_x);
285 rc = wgetnstr(txtwin, buffer, limit);
288 rc = getnstr(buffer, limit);
292 if (txtwin != stdscr) {
293 rc = mvwgetstr(txtwin, txt_y, txt_x, buffer);
295 rc = mvgetstr(txt_y, txt_x, buffer);
299 if (txtwin != stdscr) {
300 rc = mvwgetnstr(txtwin, txt_y, txt_x, buffer, limit);
302 rc = mvgetnstr(txt_y, txt_x, buffer, limit);
309 wattrset(txtwin, A_NORMAL);
310 wprintw(strwin, "%d:%s", rc, buffer);
311 wnoutrefresh(strwin);
327 main(int argc, char *argv[])
332 setlocale(LC_ALL, "");
335 fprintf(stderr, "usage: %s file\n", argv[0]);
341 chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
343 wnoutrefresh(chrbox);
345 strwin = derwin(chrbox, 4, COLS - 2, 1, 1);
347 test_getstr(1, argv, strwin);
350 ExitProgram(EXIT_SUCCESS);