blob: 9cb0de95a809a3e66845e559648668dacf8f51f7 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
2 * Copyright (c) 2006-2011,2012 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.8 2012/12/08 20:46:02 tom Exp $
30 *
31 * Demonstrate the redrawwin() and wredrawln() functions.
32 * Thomas Dickey - 2006/11/4
33 */
34
35#include <test.priv.h>
36
37static void
38trash(int beg_x, int max_x, int cur_x)
39{
40 int x;
41
42 for (x = cur_x; x > beg_x; --x) {
43 putchar('\b');
44 }
45 for (x = beg_x; x < max_x; ++x) {
46 if (x < cur_x)
47 putchar('<');
48 else if (x == cur_x)
49 putchar('=');
50 else if (x > cur_x)
51 putchar('>');
52 }
53 for (x = max_x; x > cur_x; --x) {
54 putchar('\b');
55 }
56}
57
58static void
59test_redraw(WINDOW *win)
60{
61 WINDOW *win1;
62 WINDOW *win2;
63 bool done = FALSE;
64 int ch, y, x;
65 int max_y, max_x;
66 int beg_y, beg_x;
67
68 assert(win != 0);
69
70 scrollok(win, TRUE);
71 keypad(win, TRUE);
72 getmaxyx(win, max_y, max_x);
73 getbegyx(win, beg_y, beg_x);
74 while (!done) {
75 ch = wgetch(win);
76 getyx(win, y, x);
77 switch (ch) {
78 case 'q':
79 /* FALLTHRU */
80 case ESCAPE:
81 done = TRUE;
82 break;
83 case 'w':
84 win1 = newwin(max_y, max_x,
85 beg_y, beg_x);
86 win2 = newwin(max_y - 2, max_x - 2,
87 beg_y + 1, beg_x + 1);
88 box(win1, 0, 0);
89 wrefresh(win1);
90
91 test_redraw(win2);
92
93 delwin(win2);
94 delwin(win1);
95
96 touchwin(win);
97 break;
98
99 case '!':
100 /*
101 * redrawwin() and wredrawln() do not take into account the
102 * possibility that the cursor may have moved. That makes them
103 * cumbersome for using with a shell command. So we simply
104 * trash the current line of the window using backspace/overwrite.
105 */
106 trash(beg_x, max_x, x + beg_x);
107 break;
108
109#ifdef NCURSES_VERSION
110 case '@':
111 /*
112 * For a shell command, we can work around the problem noted above
113 * using mvcur(). It is ifdef'd for NCURSES, since X/Open does
114 * not define the case where the old location is unknown.
115 */
116 IGNORE_RC(system("date"));
117 mvcur(-1, -1, y, x);
118 break;
119#endif
120
121 case CTRL('W'):
122 redrawwin(win);
123 break;
124
125 case CTRL('L'):
126 wredrawln(win, y, 1);
127 break;
128
129 case KEY_UP:
130 if (y > 0)
131 wmove(win, y - 1, x);
132 break;
133
134 case KEY_DOWN:
135 if (y < max_y)
136 wmove(win, y + 1, x);
137 break;
138
139 case KEY_LEFT:
140 if (x > 0)
141 wmove(win, y, x - 1);
142 break;
143
144 case KEY_RIGHT:
145 if (x < max_x)
146 wmove(win, y, x + 1);
147 break;
148
149 default:
150 if (ch > KEY_MIN) {
151 waddstr(win, keyname(ch));
152 } else {
153 waddstr(win, unctrl(UChar(ch)));
154 }
155 break;
156 }
157 wnoutrefresh(win);
158 doupdate();
159 }
160}
161
162int
163main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
164{
165 initscr();
166 raw();
167 noecho();
168 test_redraw(stdscr);
169 endwin();
170 ExitProgram(EXIT_SUCCESS);
171}