missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / chgat.c
blob53ecfd7cedf4c9fdeaf7c19418278cb2691cdf60
1 /****************************************************************************
2 * Copyright (c) 2006-2009,2010 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 ****************************************************************************/
29 * $Id: chgat.c,v 1.11 2010/05/01 19:12:26 tom Exp $
31 * test-driver for chgat/wchgat/mvchgat/mvwchgat
34 #include <test.priv.h>
36 #if HAVE_CHGAT
38 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
39 #define COLOR_DEFAULT (-1)
41 #if defined(NCURSES_VERSION_PATCH) && NCURSES_VERSION_PATCH < 20060715
42 #define touch_if_needed(win, row) touchline(win, row, 1)
43 #else
44 #define touch_if_needed(win, row) /* nothing */
45 #endif
47 typedef struct {
48 unsigned c;
49 unsigned v;
50 short pair;
51 unsigned attr;
52 int count;
53 int ch;
54 const char *c_msg;
55 const char *v_msg;
56 int y_val;
57 int x_val;
58 int y_beg, x_beg;
59 int y_max, x_max;
60 } STATUS;
62 static const char *
63 color_params(unsigned state, short *pair)
65 /* *INDENT-OFF* */
66 static struct {
67 short pair;
68 short fg, bg;
69 const char *msg;
70 } table[] = {
71 { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
72 { 1, COLOR_RED, COLOR_BLACK, "red/black" },
73 { 2, COLOR_WHITE, COLOR_BLUE, "white/blue" },
75 /* *INDENT-ON* */
77 static bool first = TRUE;
78 const char *result = 0;
80 if (has_colors()) {
81 if (first) {
82 unsigned n;
84 start_color();
85 for (n = 0; n < SIZEOF(table); ++n) {
86 init_pair(table[n].pair, table[n].fg, table[n].bg);
89 if (state < SIZEOF(table)) {
90 *pair = table[state].pair;
91 result = table[state].msg;
94 return result;
97 static const char *
98 video_params(unsigned state, unsigned *attr)
100 /* *INDENT-OFF* */
101 static struct {
102 unsigned attr;
103 const char *msg;
104 } table[] = {
105 { A_NORMAL, "normal" },
106 { A_BOLD, "bold" },
107 { A_REVERSE, "reverse" },
108 { A_UNDERLINE, "underline" },
109 { A_BLINK, "blink" },
111 /* *INDENT-ON* */
113 const char *result = 0;
115 if (state < SIZEOF(table)) {
116 *attr = table[state].attr;
117 result = table[state].msg;
119 return result;
122 /* fill the window with a test-pattern */
123 static void
124 fill_window(WINDOW *win)
126 int y, x;
127 int y0 = -1, x0 = -1;
129 getyx(win, y, x);
130 wmove(win, 0, 0);
131 while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
132 int y1, x1;
133 getyx(win, y1, x1);
134 if (y1 == y0 && x1 == x0)
135 break;
136 x0 = x1;
137 y0 = y1;
139 wmove(win, y, x);
142 static void
143 show_status(WINDOW *win, STATUS * sp)
145 int y, x;
147 getyx(win, y, x);
148 wmove(win, 0, 0);
149 wprintw(win, "Count %d", sp->count);
150 if (sp->v_msg != 0)
151 wprintw(win, " Video %s", sp->v_msg);
152 if (sp->c_msg != 0)
153 wprintw(win, " Color %s", sp->c_msg);
154 wclrtoeol(win);
155 wmove(win, y, x);
158 static void
159 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
161 WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
162 sp->y_beg + 1, sp->x_beg + 1);
164 if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
165 WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1);
167 if (win2 != 0) {
168 box(win1, 0, 0);
169 wrefresh(win1);
170 func(win2);
172 delwin(win2);
173 } else {
174 beep();
176 delwin(win1);
177 touchwin(win);
178 } else {
179 beep();
183 static void
184 init_status(WINDOW *win, STATUS * sp)
186 memset(sp, 0, sizeof(*sp));
187 sp->c = 99;
188 sp->v = 99;
189 sp->ch = ' ';
191 keypad(win, TRUE);
192 fill_window(win);
194 getbegyx(win, sp->y_beg, sp->x_beg);
195 getmaxyx(win, sp->y_max, sp->x_max);
198 static void
199 show_help(WINDOW *win)
201 static const char *table[] =
203 "Basic commands:"
204 ,"Use h/j/k/l or arrow keys to move the cursor."
205 ,"Set the count parameter for chgat by entering digits 0-9."
207 ,"Other commands:"
208 ,"space toggles through the set of video attributes and colors."
209 ,"t touches (forces repaint) of the current line."
210 ,". calls *chgat at the current position with the given count."
211 ,", calls *chgat at the window beginning with the given count."
212 ,"= resets count to zero."
213 ,"- negates count."
214 ,"? shows this help-window"
219 int y_max, x_max;
220 int row;
222 getmaxyx(win, y_max, x_max);
223 for (row = 0; row < (int) SIZEOF(table) && row < y_max; ++row) {
224 MvWPrintw(win, row, 0, "%.*s", x_max, table[row]);
226 while (wgetch(win) != 'q')
227 beep();
230 static void
231 update_status(WINDOW *win, STATUS * sp)
233 switch (sp->ch) {
234 case ' ': /* next test-iteration */
235 if (has_colors()) {
236 if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
237 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
238 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
239 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
242 } else {
243 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
244 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
247 sp->count = 0;
248 show_status(win, sp);
249 break;
250 case KEY_LEFT:
251 case 'h':
252 if (sp->x_val > 0)
253 wmove(win, sp->y_val, --(sp->x_val));
254 break;
255 case KEY_DOWN:
256 case 'j':
257 if (sp->y_val < sp->y_max)
258 wmove(win, ++(sp->y_val), sp->x_val);
259 break;
260 case KEY_UP:
261 case 'k':
262 if (sp->y_val > 0)
263 wmove(win, --(sp->y_val), sp->x_val);
264 break;
265 case KEY_RIGHT:
266 case 'l':
267 if (sp->x_val < sp->x_max)
268 wmove(win, sp->y_val, ++(sp->x_val));
269 break;
270 case 't':
271 touchline(win, sp->y_val, 1);
272 break;
273 case '=':
274 sp->count = 0;
275 show_status(win, sp);
276 break;
277 case '-':
278 sp->count = -(sp->count);
279 show_status(win, sp);
280 break;
281 case '?':
282 do_subwindow(win, sp, show_help);
283 break;
284 default:
285 if (isdigit(sp->ch)) {
286 sp->count = (sp->count * 10) + (sp->ch - '0');
287 show_status(win, sp);
288 } else {
289 beep();
291 break;
295 static void
296 test_wchgat(WINDOW *win)
298 STATUS st;
300 init_status(win, &st);
302 do {
303 switch (st.ch) {
304 case '.': /* change from current position */
305 wchgat(win, st.count, st.attr, st.pair, (void *) 0);
306 touch_if_needed(win, st.y_val);
307 break;
308 case ',': /* change from beginning of window */
309 mvwchgat(win, 0, 0, st.count, st.attr, st.pair, (void *) 0);
310 touch_if_needed(win, 0);
311 wmove(win, st.y_val, st.x_val);
312 break;
313 case 'w':
314 do_subwindow(win, &st, test_wchgat);
315 break;
316 case 'q':
317 return;
318 default:
319 update_status(win, &st);
320 break;
322 } while ((st.ch = wgetch(win)) != ERR);
325 static void
326 test_chgat(void)
328 STATUS st;
330 init_status(stdscr, &st);
332 do {
333 switch (st.ch) {
334 case '.': /* change from current position */
335 chgat(st.count, st.attr, st.pair, (void *) 0);
336 touch_if_needed(stdscr, st.y_val);
337 break;
338 case ',': /* change from beginning of window */
339 mvchgat(0, 0, st.count, st.attr, st.pair, (void *) 0);
340 touch_if_needed(stdscr, 0);
341 move(st.y_val, st.x_val);
342 break;
343 case 'w':
344 do_subwindow(stdscr, &st, test_wchgat);
345 break;
346 case 'q':
347 return;
348 default:
349 update_status(stdscr, &st);
350 break;
352 } while ((st.ch = getch()) != ERR);
356 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
358 initscr();
359 cbreak();
360 noecho();
362 test_chgat();
363 endwin();
365 ExitProgram(EXIT_SUCCESS);
368 #else
370 main(void)
372 printf("This program requires the curses chgat function\n");
373 ExitProgram(EXIT_FAILURE);
375 #endif