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