1 /****************************************************************************
2 * Copyright (c) 2006-2010,2012 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: chgat.c,v 1.12 2012/11/18 01:55:35 tom Exp $
31 * test-driver for chgat/wchgat/mvchgat/mvwchgat
34 #include <test.priv.h>
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)
44 #define touch_if_needed(win, row) /* nothing */
63 color_params(unsigned state, short *pair)
71 { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
72 { 1, COLOR_RED, COLOR_BLACK, "red/black" },
73 { 2, COLOR_WHITE, COLOR_BLUE, "white/blue" },
77 static bool first = TRUE;
78 const char *result = 0;
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;
98 video_params(unsigned state, unsigned *attr)
105 { A_NORMAL, "normal" },
107 { A_REVERSE, "reverse" },
108 { A_UNDERLINE, "underline" },
109 { A_BLINK, "blink" },
113 const char *result = 0;
115 if (state < SIZEOF(table)) {
116 *attr = table[state].attr;
117 result = table[state].msg;
122 /* fill the window with a test-pattern */
124 fill_window(WINDOW *win)
127 int y0 = -1, x0 = -1;
131 while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
134 if (y1 == y0 && x1 == x0)
143 show_status(WINDOW *win, STATUS * sp)
149 wprintw(win, "Count %d", sp->count);
151 wprintw(win, " Video %s", sp->v_msg);
153 wprintw(win, " Color %s", sp->c_msg);
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);
186 init_status(WINDOW *win, STATUS * sp)
188 memset(sp, 0, sizeof(*sp));
196 getbegyx(win, sp->y_beg, sp->x_beg);
197 getmaxyx(win, sp->y_max, sp->x_max);
201 show_help(WINDOW *win)
203 static const char *table[] =
206 ,"Use h/j/k/l or arrow keys to move the cursor."
207 ,"Set the count parameter for chgat by entering digits 0-9."
210 ,"space toggles through the set of video attributes and colors."
211 ,"t touches (forces repaint) of the current line."
212 ,". calls *chgat at the current position with the given count."
213 ,", calls *chgat at the window beginning with the given count."
214 ,"= resets count to zero."
216 ,"? shows this help-window"
224 getmaxyx(win, y_max, x_max);
225 for (row = 0; row < (int) SIZEOF(table) && row < y_max; ++row) {
226 MvWPrintw(win, row, 0, "%.*s", x_max, table[row]);
228 while (wgetch(win) != 'q')
233 update_status(WINDOW *win, STATUS * sp)
236 case ' ': /* next test-iteration */
238 if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
239 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
240 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
241 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
245 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
246 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
250 show_status(win, sp);
255 wmove(win, sp->y_val, --(sp->x_val));
259 if (sp->y_val < sp->y_max)
260 wmove(win, ++(sp->y_val), sp->x_val);
265 wmove(win, --(sp->y_val), sp->x_val);
269 if (sp->x_val < sp->x_max)
270 wmove(win, sp->y_val, ++(sp->x_val));
273 touchline(win, sp->y_val, 1);
277 show_status(win, sp);
280 sp->count = -(sp->count);
281 show_status(win, sp);
284 do_subwindow(win, sp, show_help);
287 if (isdigit(sp->ch)) {
288 sp->count = (sp->count * 10) + (sp->ch - '0');
289 show_status(win, sp);
298 test_wchgat(WINDOW *win)
302 init_status(win, &st);
306 case '.': /* change from current position */
307 wchgat(win, st.count, st.attr, st.pair, (void *) 0);
308 touch_if_needed(win, st.y_val);
310 case ',': /* change from beginning of window */
311 mvwchgat(win, 0, 0, st.count, st.attr, st.pair, (void *) 0);
312 touch_if_needed(win, 0);
313 wmove(win, st.y_val, st.x_val);
316 do_subwindow(win, &st, test_wchgat);
321 update_status(win, &st);
324 } while ((st.ch = wgetch(win)) != ERR);
332 init_status(stdscr, &st);
336 case '.': /* change from current position */
337 chgat(st.count, st.attr, st.pair, (void *) 0);
338 touch_if_needed(stdscr, st.y_val);
340 case ',': /* change from beginning of window */
341 mvchgat(0, 0, st.count, st.attr, st.pair, (void *) 0);
342 touch_if_needed(stdscr, 0);
343 move(st.y_val, st.x_val);
346 do_subwindow(stdscr, &st, test_wchgat);
351 update_status(stdscr, &st);
354 } while ((st.ch = getch()) != ERR);
358 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
367 ExitProgram(EXIT_SUCCESS);
374 printf("This program requires the curses chgat function\n");
375 ExitProgram(EXIT_FAILURE);