]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/redraw.c
ncurses 6.1 - patch 20180407
[ncurses.git] / test / redraw.c
1 /****************************************************************************
2  * Copyright (c) 2006-2012,2017 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: redraw.c,v 1.10 2017/06/24 14:04:57 tom Exp $
30  *
31  * Demonstrate the redrawwin() and wredrawln() functions.
32  * Thomas Dickey - 2006/11/4
33  */
34
35 #include <test.priv.h>
36 #include <popup_msg.h>
37
38 static void
39 trash(int beg_x, int max_x, int cur_x)
40 {
41     int x;
42
43     for (x = cur_x; x > beg_x; --x) {
44         putchar('\b');
45     }
46     for (x = beg_x; x < max_x; ++x) {
47         if (x < cur_x)
48             putchar('<');
49         else if (x == cur_x)
50             putchar('=');
51         else if (x > cur_x)
52             putchar('>');
53     }
54     for (x = max_x; x > cur_x; --x) {
55         putchar('\b');
56     }
57     fflush(stdout);
58 }
59
60 static void
61 test_redraw(WINDOW *win)
62 {
63     static const char *help[] =
64     {
65         "Commands:",
66         "  ^Q/ESC/q   - quit",
67         "  w          - recur in a new window",
68         "  !          - overwrite current line using stdio outside curses.",
69 #ifdef NCURSES_VERSION
70         "  @          - run \"date\" command, to put its output on screen.",
71 #endif
72         "  ^L         - call redrawwin() for current window.",
73         "  ^W         - call wredrawln() for current line/current window.",
74         "  arrow-keys - move cursor on the screen",
75         "",
76         "Other control characters are added to the screen in printable form.",
77         "Other printable characters are added to the screen as is.",
78         0
79     };
80
81     WINDOW *win1;
82     WINDOW *win2;
83     bool done = FALSE;
84     int ch, y, x;
85     int max_y, max_x;
86     int beg_y, beg_x;
87
88     assert(win != 0);
89
90     scrollok(win, TRUE);
91     keypad(win, TRUE);
92     getmaxyx(win, max_y, max_x);
93     getbegyx(win, beg_y, beg_x);
94     while (!done) {
95         ch = wgetch(win);
96         getyx(win, y, x);
97         switch (ch) {
98         case 'q':
99             /* FALLTHRU */
100         case QUIT:
101         case ESCAPE:
102             done = TRUE;
103             break;
104         case 'w':
105             win1 = newwin(max_y, max_x,
106                           beg_y, beg_x);
107             win2 = newwin(max_y - 2, max_x - 2,
108                           beg_y + 1, beg_x + 1);
109             box(win1, 0, 0);
110             wrefresh(win1);
111
112             test_redraw(win2);
113
114             delwin(win2);
115             delwin(win1);
116
117             touchwin(win);
118             break;
119
120         case '!':
121             /*
122              * redrawwin() and wredrawln() do not take into account the
123              * possibility that the cursor may have moved.  That makes them
124              * cumbersome for using with a shell command.  So we simply
125              * trash the current line of the window using backspace/overwrite.
126              */
127             trash(beg_x, max_x, x + beg_x);
128             break;
129
130 #ifdef NCURSES_VERSION
131         case '@':
132             /*
133              * For a shell command, we can work around the problem noted above
134              * using mvcur().  It is ifdef'd for NCURSES, since X/Open does
135              * not define the case where the old location is unknown. 
136              */
137             IGNORE_RC(system("date"));
138             mvcur(-1, -1, y, x);
139             break;
140 #endif
141
142         case CTRL('W'):
143             redrawwin(win);
144             break;
145
146         case CTRL('L'):
147             wredrawln(win, y, 1);
148             break;
149
150         case KEY_UP:
151             if (y > 0)
152                 wmove(win, y - 1, x);
153             break;
154
155         case KEY_DOWN:
156             if (y < max_y)
157                 wmove(win, y + 1, x);
158             break;
159
160         case KEY_LEFT:
161             if (x > 0)
162                 wmove(win, y, x - 1);
163             break;
164
165         case KEY_RIGHT:
166             if (x < max_x)
167                 wmove(win, y, x + 1);
168             break;
169
170         case HELP_KEY_1:
171             popup_msg(win, help);
172             break;
173
174         default:
175             if (ch > KEY_MIN) {
176                 waddstr(win, keyname(ch));
177                 waddch(win, '\n');
178             } else {
179                 waddstr(win, unctrl(UChar(ch)));
180             }
181             break;
182         }
183         wnoutrefresh(win);
184         doupdate();
185     }
186 }
187
188 static void
189 usage(void)
190 {
191     static const char *tbl[] =
192     {
193         "Usage: redraw [options]"
194         ,""
195         ,"Options:"
196         ,"  -e      use stderr (default stdout)"
197         ,"  -n      do not initialize terminal"
198     };
199     unsigned n;
200     for (n = 0; n < SIZEOF(tbl); ++n)
201         fprintf(stderr, "%s\n", tbl[n]);
202     ExitProgram(EXIT_FAILURE);
203 }
204
205 int
206 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
207 {
208     int ch;
209     bool no_init = FALSE;
210     FILE *my_fp = stdout;
211
212     while ((ch = getopt(argc, argv, "en")) != -1) {
213         switch (ch) {
214         case 'e':
215             my_fp = stderr;
216             break;
217         case 'n':
218             no_init = TRUE;
219             break;
220         default:
221             usage();
222             break;
223         }
224     }
225     if (optind < argc)
226         usage();
227
228     if (no_init) {
229         START_TRACE();
230     } else {
231         newterm((char *) 0, my_fp, stdin);
232     }
233
234     raw();
235     noecho();
236     test_redraw(stdscr);
237     endwin();
238     ExitProgram(EXIT_SUCCESS);
239 }