1 /****************************************************************************
2 * Copyright (c) 2006-2009,2010 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.11 2010/05/01 19:12:26 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);
184 init_status(WINDOW
*win
, STATUS
* sp
)
186 memset(sp
, 0, sizeof(*sp
));
194 getbegyx(win
, sp
->y_beg
, sp
->x_beg
);
195 getmaxyx(win
, sp
->y_max
, sp
->x_max
);
199 show_help(WINDOW
*win
)
201 static const char *table
[] =
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."
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."
214 ,"? shows this help-window"
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')
231 update_status(WINDOW
*win
, STATUS
* sp
)
234 case ' ': /* next test-iteration */
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
));
243 if ((sp
->v_msg
= video_params(++(sp
->v
), &(sp
->attr
))) == 0) {
244 sp
->v_msg
= video_params(sp
->v
= 0, &(sp
->attr
));
248 show_status(win
, sp
);
253 wmove(win
, sp
->y_val
, --(sp
->x_val
));
257 if (sp
->y_val
< sp
->y_max
)
258 wmove(win
, ++(sp
->y_val
), sp
->x_val
);
263 wmove(win
, --(sp
->y_val
), sp
->x_val
);
267 if (sp
->x_val
< sp
->x_max
)
268 wmove(win
, sp
->y_val
, ++(sp
->x_val
));
271 touchline(win
, sp
->y_val
, 1);
275 show_status(win
, sp
);
278 sp
->count
= -(sp
->count
);
279 show_status(win
, sp
);
282 do_subwindow(win
, sp
, show_help
);
285 if (isdigit(sp
->ch
)) {
286 sp
->count
= (sp
->count
* 10) + (sp
->ch
- '0');
287 show_status(win
, sp
);
296 test_wchgat(WINDOW
*win
)
300 init_status(win
, &st
);
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
);
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
);
314 do_subwindow(win
, &st
, test_wchgat
);
319 update_status(win
, &st
);
322 } while ((st
.ch
= wgetch(win
)) != ERR
);
330 init_status(stdscr
, &st
);
334 case '.': /* change from current position */
335 chgat(st
.count
, st
.attr
, st
.pair
, (void *) 0);
336 touch_if_needed(stdscr
, st
.y_val
);
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
);
344 do_subwindow(stdscr
, &st
, test_wchgat
);
349 update_status(stdscr
, &st
);
352 } while ((st
.ch
= getch()) != ERR
);
356 main(int argc GCC_UNUSED
, char *argv
[]GCC_UNUSED
)
365 ExitProgram(EXIT_SUCCESS
);
372 printf("This program requires the curses chgat function\n");
373 ExitProgram(EXIT_FAILURE
);