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