1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 2007-2008,2009 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 * $Id: test_opaque.c,v 1.14 2021/09/11 23:44:07 tom Exp $
32 * Author: Thomas E Dickey
34 * Demonstrate the opaque functions from the curses library.
36 WINDOW * wgetparent (const WINDOW *);
37 bool is_cleared(const WINDOW *win);
38 bool is_idcok(const WINDOW *win);
39 bool is_idlok(const WINDOW *win);
40 bool is_immedok(const WINDOW *win);
41 bool is_keypad(const WINDOW *win);
42 bool is_leaveok(const WINDOW *win);
43 bool is_nodelay(const WINDOW *win);
44 bool is_notimeout(const WINDOW *win);
45 bool is_scrollok(const WINDOW *win);
46 bool is_syncok(const WINDOW *win);
47 int wgetscrreg (const WINDOW *, int *, int *);
48 bool is_pad(const WINDOW *win);
49 bool is_subwin(const WINDOW *win);
50 int wgetdelay(const WINDOW *win);
53 #include <test.priv.h>
58 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20090906) && NCURSES_EXT_FUNCS && NCURSES_OPAQUE
63 return (ch == 'q' || ch == QUIT || ch == ESCAPE);
66 typedef bool(*BoolOpaque) (WINDOW *, int);
69 test_opaque_cleared(WINDOW *win, int mode)
75 return is_cleared(win);
79 test_opaque_idcok(WINDOW *win, int mode)
88 test_opaque_idlok(WINDOW *win, int mode)
97 test_opaque_immedok(WINDOW *win, int mode)
102 return is_immedok(win);
106 test_opaque_keypad(WINDOW *win, int mode)
111 return is_keypad(win);
115 test_opaque_leaveok(WINDOW *win, int mode)
120 return is_leaveok(win);
124 test_opaque_nodelay(WINDOW *win, int mode)
129 return is_nodelay(win);
133 test_opaque_notimeout(WINDOW *win, int mode)
136 notimeout(win, mode);
138 return is_notimeout(win);
142 test_opaque_scrollok(WINDOW *win, int mode)
147 return is_scrollok(win);
151 test_opaque_syncok(WINDOW *win, int mode)
156 return is_syncok(win);
160 status_y(WINDOW *stswin, int cell)
162 return (cell % getmaxy(stswin));
166 status_x(WINDOW *stswin, int cell)
168 return (15 * (cell / getmaxy(stswin)));
172 to_keyword(WINDOW *stswin, int cell)
174 wmove(stswin, status_y(stswin, cell), status_x(stswin, cell));
178 to_result(WINDOW *stswin, int cell, bool before)
180 int y = status_y(stswin, cell);
181 int x = status_x(stswin, cell) + 11;
188 show_keyword(WINDOW *stswin, int cell, int active, const char *name)
190 to_keyword(stswin, cell);
192 (void) wstandout(stswin);
193 wprintw(stswin, "%s:", name);
195 (void) wstandend(stswin);
202 { "cleared", test_opaque_cleared },
203 { "idcok", test_opaque_idcok },
204 { "idlok", test_opaque_idlok },
205 { "immedok", test_opaque_immedok },
206 { "keypad", test_opaque_keypad },
207 { "leaveok", test_opaque_leaveok },
208 { "nodelay", test_opaque_nodelay },
209 { "notimeout", test_opaque_notimeout },
210 { "scrollok", test_opaque_scrollok },
211 { "syncok", test_opaque_syncok }
215 #define bool2c(c) ((c) ? 'T' : 'F')
218 * Display and/or allow update for the properties accessed in the opaque
219 * window. Some may change state after refreshing the window, so we
220 * distinguish between them using the 'before' parameter.
223 show_opaque(WINDOW *stswin, WINDOW *txtwin, bool before, int active)
231 for (n = 0; n < (int) SIZEOF(bool_funcs); ++n) {
232 show_keyword(stswin, n, active, bool_funcs[n].name);
234 to_result(stswin, n, before);
235 wprintw(stswin, "%c", bool2c(bool_funcs[n].func(txtwin, -1)));
238 show_keyword(stswin, n, active, "is_pad");
239 to_result(stswin, n, TRUE);
240 wprintw(stswin, "%c", bool2c(is_pad(txtwin)));
243 show_keyword(stswin, n, active, "is_subwin");
244 to_result(stswin, n, TRUE);
245 wprintw(stswin, "%c", bool2c(is_subwin(txtwin)));
248 show_keyword(stswin, n, active, "wgetparent");
249 to_result(stswin, n, TRUE);
250 wprintw(stswin, "%p", (void *) wgetparent(txtwin));
253 show_keyword(stswin, n, active, "wgetdelay");
254 to_result(stswin, n, TRUE);
255 wprintw(stswin, "%d", wgetdelay(txtwin));
258 show_keyword(stswin, n, active, "wgetscrreg");
259 to_result(stswin, n, TRUE);
260 if (wgetscrreg(txtwin, &top, &bottom) == OK)
261 wprintw(stswin, "%d,%d", top, bottom);
263 wprintw(stswin, "none");
265 wnoutrefresh(stswin);
270 test_opaque(int level, char **argv, WINDOW *stswin)
276 int txt_x = 0, txt_y = 0;
278 bool in_status = FALSE;
281 if (argv[level] == 0) {
287 txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
289 wnoutrefresh(txtbox);
291 txtwin = derwin(txtbox,
301 keypad(txtwin, TRUE); /* enable keyboard mapping */
302 (void) cbreak(); /* take input chars one at a time, no wait for \n */
303 (void) noecho(); /* don't echo input */
307 wmove(txtwin, txt_y, txt_x);
309 if ((fp = fopen(argv[level], "r")) != 0) {
310 while ((ch = fgetc(fp)) != EOF) {
311 if (waddch(txtwin, UChar(ch)) != OK) {
317 wprintw(txtwin, "Cannot open:\n%s", argv[1]);
322 to_keyword(stswin, active);
325 show_opaque(stswin, txtwin, TRUE, active);
335 if (active < (int) SIZEOF(bool_funcs) - 1)
348 bool_funcs[active].func(txtwin,
349 !bool_funcs[active].func(txtwin, -1));
355 show_opaque(stswin, txtwin, FALSE, in_status ? active : -1);
357 ch = mvwgetch(txtwin, txt_y, txt_x);
358 show_opaque(stswin, txtwin, TRUE, -1);
368 if (txt_y < getmaxy(txtwin) - 1)
389 if (txt_x < getmaxx(txtwin) - 1)
395 test_opaque(level + 1, argv, stswin);
398 wnoutrefresh(txtbox);
401 wnoutrefresh(txtwin);
410 show_opaque(stswin, txtwin, FALSE, -1);
421 test_set_escdelay(void)
423 set_escdelay((100 + ESCDELAY) / 2);
427 test_set_tabsize(void)
431 int save_tabsize = TABSIZE;
433 (void) cbreak(); /* take input chars one at a time, no wait for \n */
434 (void) noecho(); /* don't echo input */
436 for (y = 0; y < LINES; ++y) {
438 if (move(y, 0) == ERR)
440 for (x = 0; x < COLS;) {
442 if (addch('*') == ERR) {
445 getyx(stdscr, y0, x0);
446 if (y0 != y || x0 == x) {
454 set_tabsize(save_tabsize);
458 main(int argc, char *argv[])
463 setlocale(LC_ALL, "");
466 fprintf(stderr, "usage: %s file\n", argv[0]);
475 stsbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
477 wnoutrefresh(stsbox);
479 stswin = derwin(stsbox, BASE_Y - 2, COLS - 2, 1, 1);
480 keypad(stswin, TRUE);
482 test_opaque(1, argv, stswin);
485 ExitProgram(EXIT_SUCCESS);
491 printf("This program requires the ncurses library\n");
492 ExitProgram(EXIT_FAILURE);