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