1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 2006-2012,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * $Id: chgat.c,v 1.19 2020/02/02 23:34:34 tom Exp $
31  *
32  * test-driver for chgat/wchgat/mvchgat/mvwchgat
33  */
34 
35 #include <test.priv.h>
36 #include <popup_msg.h>
37 
38 #if HAVE_CHGAT
39 
40 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
41 #define COLOR_DEFAULT (-1)
42 
43 #if defined(NCURSES_VERSION_PATCH) && NCURSES_VERSION_PATCH < 20060715
44 #define touch_if_needed(win, row) touchline(win, row, 1)
45 #else
46 #define touch_if_needed(win, row)	/* nothing */
47 #endif
48 
49 typedef struct {
50     size_t c;
51     size_t v;
52     short pair;
53     attr_t attr;
54     int count;
55     int ch;
56     const char *c_msg;
57     const char *v_msg;
58     int y_val;
59     int x_val;
60     int y_beg, x_beg;
61     int y_max, x_max;
62 } STATUS;
63 
64 static const char *
color_params(size_t state,short * pair)65 color_params(size_t state, short *pair)
66 {
67     /* *INDENT-OFF* */
68     static struct {
69 	short pair;
70 	short fg, bg;
71 	const char *msg;
72     } table[] = {
73 	{ 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" },
74 	{ 1, COLOR_RED,     COLOR_BLACK,   "red/black" },
75 	{ 2, COLOR_WHITE,   COLOR_BLUE,    "white/blue" },
76     };
77     /* *INDENT-ON* */
78 
79     const char *result = 0;
80 
81     if (has_colors()) {
82 	static bool first = TRUE;
83 
84 	if (first) {
85 	    size_t n;
86 
87 	    start_color();
88 	    for (n = 0; n < SIZEOF(table); ++n) {
89 		init_pair(table[n].pair, table[n].fg, table[n].bg);
90 	    }
91 	}
92 	if (state < SIZEOF(table)) {
93 	    *pair = table[state].pair;
94 	    result = table[state].msg;
95 	}
96     }
97     return result;
98 }
99 
100 static const char *
video_params(size_t state,attr_t * attr)101 video_params(size_t state, attr_t *attr)
102 {
103     /* *INDENT-OFF* */
104     static struct {
105 	attr_t attr;
106 	const char *msg;
107     } table[] = {
108 	{ WA_NORMAL,	"normal" },
109 	{ WA_BOLD,	"bold" },
110 	{ WA_REVERSE,	"reverse" },
111 	{ WA_UNDERLINE,	"underline" },
112 	{ WA_BLINK, 	"blink" },
113     };
114     /* *INDENT-ON* */
115 
116     const char *result = 0;
117 
118     if (state < SIZEOF(table)) {
119 	*attr = table[state].attr;
120 	result = table[state].msg;
121     }
122     return result;
123 }
124 
125 /* fill the window with a test-pattern */
126 static void
fill_window(WINDOW * win)127 fill_window(WINDOW *win)
128 {
129     int y, x;
130     int y0 = -1, x0 = -1;
131 
132     getyx(win, y, x);
133     wmove(win, 0, 0);
134     while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) {
135 	int y1, x1;
136 	getyx(win, y1, x1);
137 	if (y1 == y0 && x1 == x0)
138 	    break;
139 	x0 = x1;
140 	y0 = y1;
141     }
142     wmove(win, y, x);
143 }
144 
145 static void
show_status(WINDOW * win,STATUS * sp)146 show_status(WINDOW *win, STATUS * sp)
147 {
148     int y, x;
149 
150     getyx(win, y, x);
151     wmove(win, 0, 0);
152     wprintw(win, "Count %d", sp->count);
153     if (sp->v_msg != 0)
154 	wprintw(win, " Video %s", sp->v_msg);
155     if (sp->c_msg != 0)
156 	wprintw(win, " Color %s", sp->c_msg);
157     wclrtoeol(win);
158     wmove(win, y, x);
159 }
160 
161 static void
do_subwindow(WINDOW * win,STATUS * sp,void func (WINDOW *))162 do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *))
163 {
164     WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2,
165 			  sp->y_beg + 1, sp->x_beg + 1);
166 
167     if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) {
168 	WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1);
169 
170 	if (win2 != 0) {
171 	    box(win1, 0, 0);
172 	    wrefresh(win1);
173 	    func(win2);
174 
175 	    delwin(win2);
176 	} else {
177 	    beep();
178 	}
179 	delwin(win1);
180 	touchwin(win);
181     } else {
182 	if (win1 != 0)
183 	    delwin(win1);
184 	beep();
185     }
186 }
187 
188 static void
init_status(WINDOW * win,STATUS * sp)189 init_status(WINDOW *win, STATUS * sp)
190 {
191     memset(sp, 0, sizeof(*sp));
192     sp->c = 99;
193     sp->v = 99;
194     sp->ch = ' ';
195 
196     keypad(win, TRUE);
197     fill_window(win);
198 
199     getbegyx(win, sp->y_beg, sp->x_beg);
200     getmaxyx(win, sp->y_max, sp->x_max);
201 }
202 
203 static void
show_help(WINDOW * win)204 show_help(WINDOW *win)
205 {
206     static const char *msgs[] =
207     {
208 	"Basic commands:"
209 	,"Use h/j/k/l or arrow keys to move the cursor."
210 	,"Set the count parameter for chgat by entering digits 0-9."
211 	,""
212 	,"Other commands:"
213 	,"space toggles through the set of video attributes and colors."
214 	,"t     touches (forces repaint) of the current line."
215 	,".     calls *chgat at the current position with the given count."
216 	,",     calls *chgat at the window beginning with the given count."
217 	,"=     resets count to zero."
218 	,"-     negates count."
219 	,"?     shows this help-window"
220 	,0
221     };
222 
223     popup_msg(win, msgs);
224 }
225 
226 static void
update_status(WINDOW * win,STATUS * sp)227 update_status(WINDOW *win, STATUS * sp)
228 {
229     switch (sp->ch) {
230     case ' ':			/* next test-iteration */
231 	if (has_colors()) {
232 	    if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) {
233 		sp->c_msg = color_params(sp->c = 0, &(sp->pair));
234 		if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
235 		    sp->v_msg = video_params(sp->v = 0, &(sp->attr));
236 		}
237 	    }
238 	} else {
239 	    if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) {
240 		sp->v_msg = video_params(sp->v = 0, &(sp->attr));
241 	    }
242 	}
243 	sp->count = 0;
244 	show_status(win, sp);
245 	break;
246     case KEY_LEFT:
247     case 'h':
248 	if (sp->x_val > 0)
249 	    wmove(win, sp->y_val, --(sp->x_val));
250 	break;
251     case KEY_DOWN:
252     case 'j':
253 	if (sp->y_val < sp->y_max)
254 	    wmove(win, ++(sp->y_val), sp->x_val);
255 	break;
256     case KEY_UP:
257     case 'k':
258 	if (sp->y_val > 0)
259 	    wmove(win, --(sp->y_val), sp->x_val);
260 	break;
261     case KEY_RIGHT:
262     case 'l':
263 	if (sp->x_val < sp->x_max)
264 	    wmove(win, sp->y_val, ++(sp->x_val));
265 	break;
266     case 't':
267 	touchline(win, sp->y_val, 1);
268 	break;
269     case '=':
270 	sp->count = 0;
271 	show_status(win, sp);
272 	break;
273     case '-':
274 	sp->count = -(sp->count);
275 	show_status(win, sp);
276 	break;
277     case HELP_KEY_1:
278 	show_help(win);
279 	break;
280     default:
281 	if (isdigit(sp->ch)) {
282 	    sp->count = (sp->count * 10) + (sp->ch - '0');
283 	    show_status(win, sp);
284 	} else {
285 	    beep();
286 	}
287 	break;
288     }
289 }
290 
291 static void
test_wchgat(WINDOW * win)292 test_wchgat(WINDOW *win)
293 {
294     STATUS st;
295 
296     init_status(win, &st);
297 
298     do {
299 	switch (st.ch) {
300 	case '.':		/* change from current position */
301 	    wchgat(win, st.count, st.attr, st.pair, (void *) 0);
302 	    touch_if_needed(win, st.y_val);
303 	    break;
304 	case ',':		/* change from beginning of window */
305 	    mvwchgat(win, 0, 0, st.count, st.attr, st.pair, (void *) 0);
306 	    touch_if_needed(win, 0);
307 	    wmove(win, st.y_val, st.x_val);
308 	    break;
309 	case 'w':
310 	    do_subwindow(win, &st, test_wchgat);
311 	    break;
312 	case 'q':
313 	    return;
314 	default:
315 	    update_status(win, &st);
316 	    break;
317 	}
318     } while ((st.ch = wgetch(win)) != ERR);
319 }
320 
321 static void
test_chgat(void)322 test_chgat(void)
323 {
324     STATUS st;
325 
326     init_status(stdscr, &st);
327 
328     do {
329 	switch (st.ch) {
330 	case '.':		/* change from current position */
331 	    chgat(st.count, st.attr, st.pair, (void *) 0);
332 	    touch_if_needed(stdscr, st.y_val);
333 	    break;
334 	case ',':		/* change from beginning of window */
335 	    mvchgat(0, 0, st.count, st.attr, st.pair, (void *) 0);
336 	    touch_if_needed(stdscr, 0);
337 	    move(st.y_val, st.x_val);
338 	    break;
339 	case 'w':
340 	    do_subwindow(stdscr, &st, test_wchgat);
341 	    break;
342 	case 'q':
343 	    return;
344 	default:
345 	    update_status(stdscr, &st);
346 	    break;
347 	}
348     } while ((st.ch = getch()) != ERR);
349 }
350 
351 int
main(int argc GCC_UNUSED,char * argv[]GCC_UNUSED)352 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
353 {
354     initscr();
355     cbreak();
356     noecho();
357 
358     test_chgat();
359     endwin();
360 
361     ExitProgram(EXIT_SUCCESS);
362 }
363 
364 #else
365 int
main(void)366 main(void)
367 {
368     printf("This program requires the curses chgat function\n");
369     ExitProgram(EXIT_FAILURE);
370 }
371 #endif
372