]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/chgat.c
ncurses 5.6 - patch 20070505
[ncurses.git] / test / chgat.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: chgat.c,v 1.6 2007/01/06 23:28:46 tom Exp $
30  *
31  * test-driver for chgat/wchgat/mvchgat/mvwchgat
32  */
33
34 #include <test.priv.h>
35
36 #ifdef HAVE_CHGAT
37
38 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
39 #define COLOR_DEFAULT (-1)
40
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
46
47 typedef struct {
48     unsigned c;
49     unsigned v;
50     int pair, attr;
51     int count;
52     int ch;
53     const char *c_msg;
54     const char *v_msg;
55     int y_val;
56     int x_val;
57     int y_beg, x_beg;
58     int y_max, x_max;
59 } STATUS;
60
61 static const char *
62 color_params(unsigned state, int *pair)
63 {
64     /* *INDENT-OFF* */
65     static struct {
66         int pair;
67         int fg, bg;
68         const char *msg;
69     } table[] = {
70         { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
71         { 1, COLOR_RED,     COLOR_BLACK,   "red/black" },
72         { 2, COLOR_WHITE,   COLOR_BLUE,    "white/blue" },
73     };
74     /* *INDENT-ON* */
75
76     static bool first = TRUE;
77     const char *result = 0;
78
79     if (has_colors()) {
80         if (first) {
81             unsigned n;
82
83             start_color();
84             for (n = 0; n < SIZEOF(table); ++n) {
85                 init_pair(table[n].pair, table[n].fg, table[n].bg);
86             }
87         }
88         if (state < SIZEOF(table)) {
89             *pair = table[state].pair;
90             result = table[state].msg;
91         }
92     }
93     return result;
94 }
95
96 static const char *
97 video_params(unsigned state, int *attr)
98 {
99     /* *INDENT-OFF* */
100     static struct {
101         int attr;
102         const char *msg;
103     } table[] = {
104         { A_NORMAL,     "normal" },
105         { A_BOLD,       "bold" },
106         { A_REVERSE,    "reverse" },
107         { A_UNDERLINE,  "underline" },
108         { A_BLINK,      "blink" },
109     };
110     /* *INDENT-ON* */
111
112     const char *result = 0;
113
114     if (state < SIZEOF(table)) {
115         *attr = table[state].attr;
116         result = table[state].msg;
117     }
118     return result;
119 }
120
121 /* fill the window with a test-pattern */
122 static void
123 fill_window(WINDOW *win)
124 {
125     int y, x;
126
127     getyx(win, y, x);
128     wmove(win, 0, 0);
129     while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
130     }
131     wmove(win, y, x);
132 }
133
134 static void
135 show_status(WINDOW *win, STATUS * sp)
136 {
137     int y, x;
138
139     getyx(win, y, x);
140     wmove(win, 0, 0);
141     wprintw(win, "Count %d", sp->count);
142     if (sp->v_msg != 0)
143         wprintw(win, " Video %s", sp->v_msg);
144     if (sp->c_msg != 0)
145         wprintw(win, " Color %s", sp->c_msg);
146     wclrtoeol(win);
147     wmove(win, y, x);
148 }
149
150 static void
151 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
152 {
153     WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
154                           sp->y_beg + 1, sp->x_beg + 1);
155
156     if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
157         WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1);
158
159         if (win2 != 0) {
160             box(win1, 0, 0);
161             wrefresh(win1);
162             func(win2);
163
164             delwin(win2);
165         } else {
166             beep();
167         }
168         delwin(win1);
169         touchwin(win);
170     } else {
171         beep();
172     }
173 }
174
175 static void
176 init_status(WINDOW *win, STATUS * sp)
177 {
178     memset(sp, 0, sizeof(*sp));
179     sp->c = 99;
180     sp->v = 99;
181     sp->ch = ' ';
182
183     keypad(win, TRUE);
184     fill_window(win);
185
186     getbegyx(win, sp->y_beg, sp->x_beg);
187     getmaxyx(win, sp->y_max, sp->x_max);
188 }
189
190 static void
191 show_help(WINDOW *win)
192 {
193     static const char *table[] =
194     {
195         "Basic commands:"
196         ,"Use h/j/k/l or arrow keys to move the cursor."
197         ,"Set the count parameter for chgat by entering digits 0-9."
198         ,""
199         ,"Other commands:"
200         ,"space toggles through the set of video attributes and colors."
201         ,"t     touches (forces repaint) of the current line."
202         ,".     calls *chgat at the current position with the given count."
203         ,",     calls *chgat at the window beginning with the given count."
204         ,"=     resets count to zero."
205         ,"-     negates count."
206         ,"?     shows this help-window"
207         ,""
208         ,""
209     };
210
211     int y_max, x_max;
212     int row;
213
214     getmaxyx(win, y_max, x_max);
215     for (row = 0; row < (int) SIZEOF(table) && row < y_max; ++row) {
216         mvwprintw(win, row, 0, "%.*s", x_max, table[row]);
217     }
218     while (wgetch(win) != 'q')
219         beep();
220 }
221
222 static void
223 update_status(WINDOW *win, STATUS * sp)
224 {
225     switch (sp->ch) {
226     case ' ':                   /* next test-iteration */
227         if (has_colors()) {
228             if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
229                 sp->c_msg = color_params(sp->c = 0, &(sp->pair));
230                 if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
231                     sp->v_msg = video_params(sp->v = 0, &(sp->attr));
232                 }
233             }
234         } else {
235             if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
236                 sp->v_msg = video_params(sp->v = 0, &(sp->attr));
237             }
238         }
239         sp->count = 0;
240         show_status(win, sp);
241         break;
242     case KEY_LEFT:
243     case 'h':
244         if (sp->x_val > 0)
245             wmove(win, sp->y_val, --(sp->x_val));
246         break;
247     case KEY_DOWN:
248     case 'j':
249         if (sp->y_val < sp->y_max)
250             wmove(win, ++(sp->y_val), sp->x_val);
251         break;
252     case KEY_UP:
253     case 'k':
254         if (sp->y_val > 0)
255             wmove(win, --(sp->y_val), sp->x_val);
256         break;
257     case KEY_RIGHT:
258     case 'l':
259         if (sp->x_val < sp->x_max)
260             wmove(win, sp->y_val, ++(sp->x_val));
261         break;
262     case 't':
263         touchline(win, sp->y_val, 1);
264         break;
265     case '=':
266         sp->count = 0;
267         show_status(win, sp);
268         break;
269     case '-':
270         sp->count = -(sp->count);
271         show_status(win, sp);
272         break;
273     case '?':
274         do_subwindow(win, sp, show_help);
275         break;
276     default:
277         if (isdigit(sp->ch)) {
278             sp->count = (sp->count * 10) + (sp->ch - '0');
279             show_status(win, sp);
280         } else {
281             beep();
282         }
283         break;
284     }
285 }
286
287 static void
288 test_wchgat(WINDOW *win)
289 {
290     STATUS st;
291
292     init_status(win, &st);
293
294     do {
295         switch (st.ch) {
296         case '.':               /* change from current position */
297             wchgat(win, st.count, st.attr, st.pair, (void *) 0);
298             touch_if_needed(win, st.y_val);
299             break;
300         case ',':               /* change from beginning of window */
301             mvwchgat(win, 0, 0, st.count, st.attr, st.pair, (void *) 0);
302             touch_if_needed(win, 0);
303             wmove(win, st.y_val, st.x_val);
304             break;
305         case 'w':
306             do_subwindow(win, &st, test_wchgat);
307             break;
308         case 'q':
309             return;
310         default:
311             update_status(win, &st);
312             break;
313         }
314     } while ((st.ch = wgetch(win)) != ERR);
315 }
316
317 static void
318 test_chgat(void)
319 {
320     STATUS st;
321
322     init_status(stdscr, &st);
323
324     do {
325         switch (st.ch) {
326         case '.':               /* change from current position */
327             chgat(st.count, st.attr, st.pair, (void *) 0);
328             touch_if_needed(stdscr, st.y_val);
329             break;
330         case ',':               /* change from beginning of window */
331             mvchgat(0, 0, st.count, st.attr, st.pair, (void *) 0);
332             touch_if_needed(stdscr, 0);
333             move(st.y_val, st.x_val);
334             break;
335         case 'w':
336             do_subwindow(stdscr, &st, test_wchgat);
337             break;
338         case 'q':
339             return;
340         default:
341             update_status(stdscr, &st);
342             break;
343         }
344     } while ((st.ch = getch()) != ERR);
345 }
346
347 int
348 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
349 {
350     initscr();
351     cbreak();
352     noecho();
353
354     test_chgat();
355     endwin();
356
357     ExitProgram(EXIT_SUCCESS);
358 }
359
360 #else
361 int
362 main(void)
363 {
364     printf("This program requires the curses chgat function\n");
365     ExitProgram(EXIT_FAILURE);
366 }
367 #endif