1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
28 /****************************************************************************
31 ncurses.c --- ncurses library exerciser
37 An interactive test module for the ncurses library.
40 Author: Eric S. Raymond <esr@snark.thyrsus.com> 1993
41 Thomas E. Dickey (beginning revision 1.27 in 1996).
43 $Id: ncurses.c,v 1.180 2002/09/15 00:39:33 tom Exp $
45 ***************************************************************************/
51 #include <test.priv.h>
54 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
58 #include <sys/select.h>
62 #if HAVE_PANEL_H && HAVE_LIBPANEL
63 #define USE_LIBPANEL 1
66 #define USE_LIBPANEL 0
69 #if HAVE_MENU_H && HAVE_LIBMENU
76 #if HAVE_FORM_H && HAVE_LIBFORM
83 #ifdef NCURSES_VERSION
86 static int save_trace = TRACE_ORDINARY | TRACE_CALLS;
87 extern int _nc_tracing;
92 #define mmask_t chtype /* not specified in XSI */
94 #ifdef CURSES_ACS_ARRAY
95 #define ACS_S3 (CURSES_ACS_ARRAY['p']) /* scan line 3 */
96 #define ACS_S7 (CURSES_ACS_ARRAY['r']) /* scan line 7 */
97 #define ACS_LEQUAL (CURSES_ACS_ARRAY['y']) /* less/equal */
98 #define ACS_GEQUAL (CURSES_ACS_ARRAY['z']) /* greater/equal */
99 #define ACS_PI (CURSES_ACS_ARRAY['{']) /* Pi */
100 #define ACS_NEQUAL (CURSES_ACS_ARRAY['|']) /* not equal */
101 #define ACS_STERLING (CURSES_ACS_ARRAY['}']) /* UK pound sign */
103 #define ACS_S3 (A_ALTCHARSET + 'p') /* scan line 3 */
104 #define ACS_S7 (A_ALTCHARSET + 'r') /* scan line 7 */
105 #define ACS_LEQUAL (A_ALTCHARSET + 'y') /* less/equal */
106 #define ACS_GEQUAL (A_ALTCHARSET + 'z') /* greater/equal */
107 #define ACS_PI (A_ALTCHARSET + '{') /* Pi */
108 #define ACS_NEQUAL (A_ALTCHARSET + '|') /* not equal */
109 #define ACS_STERLING (A_ALTCHARSET + '}') /* UK pound sign */
112 #ifdef CURSES_WACS_ARRAY
113 #define WACS_S3 (&(CURSES_WACS_ARRAY['p'])) /* scan line 3 */
114 #define WACS_S7 (&(CURSES_WACS_ARRAY['r'])) /* scan line 7 */
115 #define WACS_LEQUAL (&(CURSES_WACS_ARRAY['y'])) /* less/equal */
116 #define WACS_GEQUAL (&(CURSES_WACS_ARRAY['z'])) /* greater/equal */
117 #define WACS_PI (&(CURSES_WACS_ARRAY['{'])) /* Pi */
118 #define WACS_NEQUAL (&(CURSES_WACS_ARRAY['|'])) /* not equal */
119 #define WACS_STERLING (&(CURSES_WACS_ARRAY['}'])) /* UK pound sign */
124 #define P(string) printw("%s\n", string)
128 #define CTRL(x) ((x) & 0x1f)
130 #define QUIT CTRL('Q')
131 #define ESCAPE CTRL('[')
132 #define BLANK ' ' /* this is the background character */
135 static int max_colors; /* the actual number of colors we'll use */
138 static int max_pairs; /* ...and the number of color pairs */
140 /* The behavior of mvhline, mvvline for negative/zero length is unspecified,
141 * though we can rely on negative x/y values to stop the macro.
144 do_h_line(int y, int x, chtype c, int to)
147 mvhline(y, x, c, (to) - (x));
151 do_v_line(int y, int x, chtype c, int to)
154 mvvline(y, x, c, (to) - (y));
157 /* Common function to allow ^T to toggle trace-mode in the middle of a test
158 * so that trace-files can be made smaller.
161 wGetchar(WINDOW *win)
165 while ((c = wgetch(win)) == CTRL('T')) {
167 save_trace = _nc_tracing;
168 _tracef("TOGGLE-TRACING OFF");
171 _nc_tracing = save_trace;
175 _tracef("TOGGLE-TRACING ON");
182 #define Getchar() wGetchar(stdscr)
184 #if USE_WIDEC_SUPPORT
186 wGet_wchar(WINDOW *win, wint_t * result)
190 while ((c = wget_wch(win, result)) == CTRL('T')) {
192 save_trace = _nc_tracing;
193 _tracef("TOGGLE-TRACING OFF");
196 _nc_tracing = save_trace;
200 _tracef("TOGGLE-TRACING ON");
203 c = wget_wch(win, result);
207 #define Get_wchar(result) wGet_wchar(stdscr, result)
215 addstr("Press any key to continue... ");
220 Cannot(const char *what)
222 printw("\nThis %s terminal %s\n\n", getenv("TERM"), what);
227 ShellOut(bool message)
230 addstr("Shelling out...");
235 addstr("returned from shellout.\n");
239 #ifdef NCURSES_MOUSE_VERSION
241 mouse_decode(MEVENT const *ep)
245 (void) sprintf(buf, "id %2d at (%2d, %2d, %2d) state %4lx = {",
246 ep->id, ep->x, ep->y, ep->z, ep->bstate);
248 #define SHOW(m, s) if ((ep->bstate & m)==m) {strcat(buf,s); strcat(buf, ", ");}
249 SHOW(BUTTON1_RELEASED, "release-1");
250 SHOW(BUTTON1_PRESSED, "press-1");
251 SHOW(BUTTON1_CLICKED, "click-1");
252 SHOW(BUTTON1_DOUBLE_CLICKED, "doubleclick-1");
253 SHOW(BUTTON1_TRIPLE_CLICKED, "tripleclick-1");
254 SHOW(BUTTON1_RESERVED_EVENT, "reserved-1");
255 SHOW(BUTTON2_RELEASED, "release-2");
256 SHOW(BUTTON2_PRESSED, "press-2");
257 SHOW(BUTTON2_CLICKED, "click-2");
258 SHOW(BUTTON2_DOUBLE_CLICKED, "doubleclick-2");
259 SHOW(BUTTON2_TRIPLE_CLICKED, "tripleclick-2");
260 SHOW(BUTTON2_RESERVED_EVENT, "reserved-2");
261 SHOW(BUTTON3_RELEASED, "release-3");
262 SHOW(BUTTON3_PRESSED, "press-3");
263 SHOW(BUTTON3_CLICKED, "click-3");
264 SHOW(BUTTON3_DOUBLE_CLICKED, "doubleclick-3");
265 SHOW(BUTTON3_TRIPLE_CLICKED, "tripleclick-3");
266 SHOW(BUTTON3_RESERVED_EVENT, "reserved-3");
267 SHOW(BUTTON4_RELEASED, "release-4");
268 SHOW(BUTTON4_PRESSED, "press-4");
269 SHOW(BUTTON4_CLICKED, "click-4");
270 SHOW(BUTTON4_DOUBLE_CLICKED, "doubleclick-4");
271 SHOW(BUTTON4_TRIPLE_CLICKED, "tripleclick-4");
272 SHOW(BUTTON4_RESERVED_EVENT, "reserved-4");
273 SHOW(BUTTON_CTRL, "ctrl");
274 SHOW(BUTTON_SHIFT, "shift");
275 SHOW(BUTTON_ALT, "alt");
276 SHOW(ALL_MOUSE_EVENTS, "all-events");
277 SHOW(REPORT_MOUSE_POSITION, "position");
280 if (buf[strlen(buf) - 1] == ' ')
281 buf[strlen(buf) - 2] = '\0';
282 (void) strcat(buf, "}");
285 #endif /* NCURSES_MOUSE_VERSION */
287 /****************************************************************************
289 * Character input test
291 ****************************************************************************/
294 setup_getch(WINDOW *win, bool flags[])
296 keypad(win, flags['k']); /* should be redundant, but for testing */
297 meta(win, flags['m']); /* force this to a known state */
305 wgetch_help(WINDOW *win, bool flags[])
307 static const char *help[] =
309 "e -- toggle echo mode"
310 ,"g -- triggers a getstr test"
311 ,"k -- toggle keypad/literal mode"
312 ,"m -- toggle meta (7-bit/8-bit) mode"
313 ,"q -- quit (x also exits)"
315 ,"w -- create a new window"
317 ,"z -- suspend this process"
321 unsigned chk = ((SIZEOF(help) + 1) / 2);
326 printw("Type any key to see its %s value. Also:\n",
327 flags['k'] ? "keypad" : "literal");
328 for (n = 0; n < SIZEOF(help); ++n) {
329 int row = 1 + (n % chk);
330 int col = (n >= chk) ? COLS / 2 : 0;
331 int flg = ((strstr(help[n], "toggle") != 0)
332 && (flags[UChar(*help[n])] != FALSE));
335 mvprintw(row, col, "%s", help[n]);
346 wgetch_wrap(WINDOW *win, int first_y)
348 int last_y = getmaxy(win) - 1;
349 int y = getcury(win) + 1;
357 #if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
363 static WINSTACK *winstack = 0;
364 static unsigned len_winstack = 0;
367 remember_boxes(unsigned level, WINDOW *txt_win, WINDOW *box_win)
369 unsigned need = (level + 1) * 2;
373 winstack = malloc(len_winstack * sizeof(WINSTACK));
374 } else if (need >= len_winstack) {
376 winstack = realloc(winstack, len_winstack * sizeof(WINSTACK));
378 winstack[level].text = txt_win;
379 winstack[level].frame = box_win;
383 * For wgetch_test(), we create pairs of windows - one for a box, one for text.
384 * Resize both and paint the box in the parent.
387 resize_boxes(int level, WINDOW *win)
391 int high = LINES - base;
395 wnoutrefresh(stdscr);
397 /* FIXME: this chunk should be done in resizeterm() */
402 for (n = 0; (int) n < level; ++n) {
403 wresize(winstack[n].frame, high, wide);
404 wresize(winstack[n].text, high - 2, wide - 2);
407 werase(winstack[n].text);
408 box(winstack[n].frame, 0, 0);
409 wnoutrefresh(winstack[n].frame);
410 wprintw(winstack[n].text,
412 getmaxy(winstack[n].text),
413 getmaxx(winstack[n].text));
414 wnoutrefresh(winstack[n].text);
415 if (winstack[n].text == win)
421 #define remember_boxes(level,text,frame) /* nothing */
425 wgetch_test(int level, WINDOW *win, int delay)
428 int first_y, first_x;
432 bool blocking = (delay < 0);
435 memset(flags, FALSE, sizeof(flags));
436 flags['k'] = (win == stdscr);
438 setup_getch(win, flags);
439 wtimeout(win, delay);
440 getyx(win, first_y, first_x);
442 wgetch_help(win, flags);
443 wsetscrreg(win, first_y, getmaxy(win) - 1);
447 while ((c = wGetchar(win)) == ERR) {
450 (void) wprintw(win, "%05d: input error", incount);
453 (void) wprintw(win, "%05d: input timed out", incount);
455 wgetch_wrap(win, first_y);
457 if (c == ERR && blocking) {
459 wgetch_wrap(win, first_y);
460 } else if (c == 'x' || c == 'q') {
462 } else if (c == 'e') {
463 flags['e'] = !flags['e'];
464 setup_getch(win, flags);
465 wgetch_help(win, flags);
466 } else if (c == 'g') {
467 waddstr(win, "getstr test: ");
469 wgetnstr(win, buf, sizeof(buf) - 1);
471 wprintw(win, "I saw %d characters:\n\t`%s'.", (int) strlen(buf), buf);
473 wgetch_wrap(win, first_y);
474 } else if (c == 'k') {
475 flags['k'] = !flags['k'];
476 setup_getch(win, flags);
477 wgetch_help(win, flags);
478 } else if (c == 'm') {
479 flags['m'] = !flags['m'];
480 setup_getch(win, flags);
481 wgetch_help(win, flags);
482 } else if (c == 's') {
484 } else if (c == 'w') {
485 int high = getmaxy(win) - 1 - first_y + 1;
486 int wide = getmaxx(win) - first_x;
488 int new_y = first_y + getbegy(win);
489 int new_x = first_x + getbegx(win);
491 getyx(win, old_y, old_x);
492 if (high > 2 && wide > 2) {
493 WINDOW *wb = newwin(high, wide, new_y, new_x);
494 WINDOW *wi = newwin(high - 2, wide - 2, new_y + 1, new_x + 1);
499 remember_boxes(level, wi, wb);
500 wgetch_test(level + 1, wi, delay);
504 wgetch_help(win, flags);
505 wmove(win, old_y, old_x);
511 } else if (c == 'z') {
512 kill(getpid(), SIGTSTP);
515 wprintw(win, "Key pressed: %04o ", c);
516 #ifdef NCURSES_MOUSE_VERSION
517 if (c == KEY_MOUSE) {
521 wprintw(win, "KEY_MOUSE, %s", mouse_decode(&event));
523 move(event.y, event.x);
527 #endif /* NCURSES_MOUSE_VERSION */
529 #if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
530 if (c == KEY_RESIZE) {
531 resize_boxes(level, win);
534 (void) waddstr(win, keyname(c));
535 } else if (c > 0x80) {
538 (void) wprintw(win, "M-%c", c2);
540 (void) wprintw(win, "M-%s", unctrl(c2));
541 waddstr(win, " (high-half character)");
544 (void) wprintw(win, "%c (ASCII printable character)", c);
546 (void) wprintw(win, "%s (ASCII control character)",
549 wgetch_wrap(win, first_y);
557 begin_getch_test(void)
564 #ifdef NCURSES_MOUSE_VERSION
565 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
568 (void) printw("Delay in 10ths of a second (<CR> for blocking input)? ");
570 getnstr(buf, sizeof(buf) - 1);
574 if (isdigit(UChar(buf[0]))) {
575 delay = atoi(buf) * 100;
585 finish_getch_test(void)
587 #ifdef NCURSES_MOUSE_VERSION
588 mousemask(0, (mmask_t *) 0);
599 int delay = begin_getch_test();
600 wgetch_test(0, stdscr, delay);
604 #if USE_WIDEC_SUPPORT
606 * For wgetch_test(), we create pairs of windows - one for a box, one for text.
607 * Resize both and paint the box in the parent.
610 resize_wide_boxes(int level, WINDOW *win)
614 int high = LINES - base;
618 wnoutrefresh(stdscr);
620 /* FIXME: this chunk should be done in resizeterm() */
625 for (n = 0; (int) n < level; ++n) {
626 wresize(winstack[n].frame, high, wide);
627 wresize(winstack[n].text, high - 2, wide - 2);
630 werase(winstack[n].text);
631 box_set(winstack[n].frame, 0, 0);
632 wnoutrefresh(winstack[n].frame);
633 wprintw(winstack[n].text,
635 getmaxy(winstack[n].text),
636 getmaxx(winstack[n].text));
637 wnoutrefresh(winstack[n].text);
638 if (winstack[n].text == win)
645 wget_wch_test(int level, WINDOW *win, int delay)
648 int first_y, first_x;
652 bool blocking = (delay < 0);
655 memset(flags, FALSE, sizeof(flags));
656 flags['k'] = (win == stdscr);
658 setup_getch(win, flags);
659 wtimeout(win, delay);
660 getyx(win, first_y, first_x);
662 wgetch_help(win, flags);
663 wsetscrreg(win, first_y, getmaxy(win) - 1);
667 while ((code = wGet_wchar(win, &c)) == ERR) {
670 (void) wprintw(win, "%05d: input error", incount);
673 (void) wprintw(win, "%05d: input timed out", incount);
675 wgetch_wrap(win, first_y);
677 if (code == ERR && blocking) {
679 wgetch_wrap(win, first_y);
680 } else if (c == 'x' || c == 'q') {
682 } else if (c == 'e') {
683 flags['e'] = !flags['e'];
684 setup_getch(win, flags);
685 wgetch_help(win, flags);
686 } else if (c == 'g') {
687 waddstr(win, "getstr test: ");
689 wgetnstr(win, buf, sizeof(buf) - 1);
691 wprintw(win, "I saw %d characters:\n\t`%s'.", strlen(buf), buf);
693 wgetch_wrap(win, first_y);
694 } else if (c == 'k') {
695 flags['k'] = !flags['k'];
696 setup_getch(win, flags);
697 wgetch_help(win, flags);
698 } else if (c == 'm') {
699 flags['m'] = !flags['m'];
700 setup_getch(win, flags);
701 wgetch_help(win, flags);
702 } else if (c == 's') {
704 } else if (c == 'w') {
705 int high = getmaxy(win) - 1 - first_y + 1;
706 int wide = getmaxx(win) - first_x;
708 int new_y = first_y + getbegy(win);
709 int new_x = first_x + getbegx(win);
711 getyx(win, old_y, old_x);
712 if (high > 2 && wide > 2) {
713 WINDOW *wb = newwin(high, wide, new_y, new_x);
714 WINDOW *wi = newwin(high - 2, wide - 2, new_y + 1, new_x + 1);
719 remember_boxes(level, wi, wb);
720 wget_wch_test(level + 1, wi, delay);
724 wgetch_help(win, flags);
725 wmove(win, old_y, old_x);
730 } else if (c == 'z') {
731 kill(getpid(), SIGTSTP);
734 wprintw(win, "Key pressed: %04o ", c);
735 #ifdef NCURSES_MOUSE_VERSION
736 if (c == KEY_MOUSE) {
740 wprintw(win, "KEY_MOUSE, %s", mouse_decode(&event));
742 move(event.y, event.x);
746 #endif /* NCURSES_MOUSE_VERSION */
747 if (code == KEY_CODE_YES) {
749 if (c == KEY_RESIZE) {
750 resize_wide_boxes(level, win);
753 (void) waddstr(win, key_name(c));
755 if (c < 256 && iscntrl(c)) {
756 (void) wprintw(win, "%s (control character)", unctrl(c));
759 waddnwstr(win, &c2, 1);
760 (void) wprintw(win, " = %#x (printable character)", c);
763 wgetch_wrap(win, first_y);
773 int delay = begin_getch_test();
774 wget_wch_test(0, stdscr, delay);
779 /****************************************************************************
781 * Character attributes test
783 ****************************************************************************/
786 show_attr(int row, int skip, chtype attr, const char *name)
788 static const char *string = "abcde fghij klmno pqrst uvwxy z";
789 int ncv = tigetnum("ncv");
791 mvprintw(row, 8, "%s mode:", name);
792 mvprintw(row, 24, "|");
794 printw("%*s", skip, " ");
797 * If we're to write a string in the alternate character set, it is not
798 * sufficient to just set A_ALTCHARSET. We have to perform the mapping
799 * that corresponds. This is not needed for vt100-compatible devices
800 * because the acs_map[] is 1:1, but for PC-style devices such as Linux
801 * console, the acs_map[] is scattered about the range.
803 * The addch/addstr functions do not themselves do this mapping, since it
804 * is possible to turn off the A_ALTCHARSET flag for the characters which
805 * are added, and it would be an unexpected result to have the mapped
806 * characters visible on the screen.
808 * This example works because the indices into acs_map[] are mostly from
809 * the lowercase characters.
811 if (attr & A_ALTCHARSET) {
812 const char *s = string;
815 #ifdef CURSES_ACS_ARRAY
816 if ((ch = CURSES_ACS_ARRAY[ch]) == 0)
826 printw("%*s", skip, " ");
828 if (attr != A_NORMAL) {
829 if (!(termattrs() & attr)) {
831 } else if (ncv > 0 && (getbkgd(stdscr) & A_COLOR)) {
832 static const chtype table[] =
846 for (n = 0; n < SIZEOF(table); n++) {
847 if ((table[n] & attr) != 0
848 && ((1 << n) & ncv) != 0) {
861 attr_getc(int *skip, int *fg, int *bg, int *ac)
867 } else if (ch == CTRL('L')) {
870 } else if (has_colors()) {
893 if (*fg >= max_colors)
896 *fg = max_colors - 1;
897 if (*bg >= max_colors)
900 *bg = max_colors - 1;
918 /* test text attributes */
921 int skip = tigetnum("xmc");
922 int fg = COLOR_BLACK; /* color pair 0 is special */
923 int bg = COLOR_BLACK;
925 bool *pairs = (bool *) calloc(max_pairs, sizeof(bool));
931 n = skip; /* make it easy */
935 int normal = A_NORMAL | BLANK;
938 int pair = (fg * max_colors) + bg;
940 init_pair(pair, fg, bg);
943 normal |= COLOR_PAIR(pair);
950 mvaddstr(0, 20, "Character attribute test display");
952 row = show_attr(row, n, ac | A_STANDOUT, "STANDOUT");
953 row = show_attr(row, n, ac | A_REVERSE, "REVERSE");
954 row = show_attr(row, n, ac | A_BOLD, "BOLD");
955 row = show_attr(row, n, ac | A_UNDERLINE, "UNDERLINE");
956 row = show_attr(row, n, ac | A_DIM, "DIM");
957 row = show_attr(row, n, ac | A_BLINK, "BLINK");
958 row = show_attr(row, n, ac | A_PROTECT, "PROTECT");
959 row = show_attr(row, n, ac | A_INVIS, "INVISIBLE");
960 row = show_attr(row, n, ac | A_NORMAL, "NORMAL");
963 "This terminal does %shave the magic-cookie glitch",
964 tigetnum("xmc") > -1 ? "" : "not ");
966 "Enter a digit to set gaps on each side of displayed attributes");
970 printw(". f/F/b/F toggle colors (now %d/%d), a/A altcharset (%d)",
973 printw(". a/A altcharset (%d)", ac != 0);
976 } while (attr_getc(&n, &fg, &bg, &ac));
978 free((char *) pairs);
979 bkgdset(A_NORMAL | BLANK);
984 /****************************************************************************
986 * Color support tests
988 ****************************************************************************/
990 static NCURSES_CONST char *the_color_names[] =
1011 show_color_name(int y, int x, int color)
1014 mvprintw(y, x, "%02d ", color);
1016 mvaddstr(y, x, the_color_names[color]);
1021 /* generate a color test pattern */
1024 int base, top, width;
1025 NCURSES_CONST char *hello;
1028 (void) printw("There are %d color pairs\n", COLOR_PAIRS);
1030 width = (max_colors > 8) ? 4 : 8;
1031 hello = (max_colors > 8) ? "Test" : "Hello";
1033 for (base = 0; base < 2; base++) {
1034 top = (max_colors > 8) ? 0 : base * (max_colors + 3);
1036 (void) mvprintw(top + 1, 0,
1037 "%dx%d matrix of foreground/background colors, bright *%s*\n",
1038 max_colors, max_colors,
1039 base ? "on" : "off");
1040 for (i = 0; i < max_colors; i++)
1041 show_color_name(top + 2, (i + 1) * width, i);
1042 for (i = 0; i < max_colors; i++)
1043 show_color_name(top + 3 + i, 0, i);
1044 for (i = 1; i < max_pairs; i++) {
1045 init_pair(i, i % max_colors, i / max_colors);
1046 attron((attr_t) COLOR_PAIR(i));
1048 attron((attr_t) A_BOLD);
1049 mvaddstr(top + 3 + (i / max_colors), (i % max_colors + 1) *
1053 if ((max_colors > 8) || base)
1062 change_color(int current, int field, int value, int usebase)
1064 short red, green, blue;
1067 color_content(current, &red, &green, &blue);
1069 red = green = blue = 0;
1083 if (init_color(current, red, green, blue) == ERR)
1089 /* display the color test pattern, without trying to edit colors */
1091 int i, this_c = 0, value = 0, current = 0, field = 0;
1096 for (i = 0; i < max_colors; i++)
1097 init_pair(i, COLOR_WHITE, i);
1099 mvprintw(LINES - 2, 0, "Number: %d", value);
1102 short red, green, blue;
1105 mvaddstr(0, 20, "Color RGB Value Editing");
1108 for (i = 0; i < max_colors; i++) {
1109 mvprintw(2 + i, 0, "%c %-8s:",
1110 (i == current ? '>' : ' '),
1111 (i < (int) SIZEOF(the_color_names)
1112 ? the_color_names[i] : ""));
1113 attrset(COLOR_PAIR(i));
1118 * Note: this refresh should *not* be necessary! It works around
1119 * a bug in attribute handling that apparently causes the A_NORMAL
1120 * attribute sets to interfere with the actual emission of the
1121 * color setting somehow. This needs to be fixed.
1125 color_content(i, &red, &green, &blue);
1127 if (current == i && field == 0)
1129 printw("%04d", red);
1130 if (current == i && field == 0)
1133 if (current == i && field == 1)
1135 printw("%04d", green);
1136 if (current == i && field == 1)
1139 if (current == i && field == 2)
1141 printw("%04d", blue);
1142 if (current == i && field == 2)
1148 mvaddstr(max_colors + 3, 0,
1149 "Use up/down to select a color, left/right to change fields.");
1150 mvaddstr(max_colors + 4, 0,
1151 "Modify field by typing nnn=, nnn-, or nnn+. ? for help.");
1153 move(2 + current, 0);
1157 if (isdigit(this_c) && !isdigit(last_c))
1162 current = (current == 0 ? (max_colors - 1) : current - 1);
1166 current = (current == (max_colors - 1) ? 0 : current + 1);
1170 field = (field == 2 ? 0 : field + 1);
1174 field = (field == 0 ? 2 : field - 1);
1187 value = value * 10 + (this_c - '0');
1191 change_color(current, field, value, 1);
1195 change_color(current, field, -value, 1);
1199 change_color(current, field, value, 0);
1204 P(" RGB Value Editing Help");
1206 P("You are in the RGB value editor. Use the arrow keys to select one of");
1207 P("the fields in one of the RGB triples of the current colors; the one");
1208 P("currently selected will be reverse-video highlighted.");
1210 P("To change a field, enter the digits of the new value; they are echoed");
1211 P("as entered. Finish by typing `='. The change will take effect instantly.");
1212 P("To increment or decrement a value, use the same procedure, but finish");
1213 P("with a `+' or `-'.");
1215 P("To quit, do `x' or 'q'");
1229 mvprintw(LINES - 2, 0, "Number: %d", value);
1232 (this_c != 'x' && this_c != 'q');
1238 /****************************************************************************
1240 * Soft-key label test
1242 ****************************************************************************/
1246 /* exercise the soft keys */
1258 mvaddstr(0, 20, "Soft Key Exerciser");
1262 P("Available commands are:");
1264 P("^L -- refresh screen");
1265 P("a -- activate or restore soft keys");
1266 P("d -- disable soft keys");
1267 P("c -- set centered format for labels");
1268 P("l -- set left-justified format for labels");
1269 P("r -- set right-justified format for labels");
1270 P("[12345678] -- set label; labels are numbered 1 through 8");
1271 P("e -- erase stdscr (should not erase labels)");
1272 P("s -- test scrolling of shortened screen");
1273 P("x, q -- return to main menu");
1275 P("Note: if activating the soft keys causes your terminal to");
1276 P("scroll up one line, your terminal auto-scrolls when anything");
1277 P("is written to the last screen position. The ncurses code");
1278 P("does not yet handle this gracefully.");
1291 mvprintw(20, 0, "Press Q to stop the scrolling-test: ");
1292 while ((c = Getchar()) != 'Q' && (c != ERR))
1320 (void) mvaddstr(20, 0, "Please enter the label value: ");
1322 wgetnstr(stdscr, buf, 8);
1324 slk_set((c - '0'), buf, fmt);
1338 ((c = Getchar()) != EOF);
1345 /****************************************************************************
1347 * Alternate character-set stuff
1349 ****************************************************************************/
1351 /* ISO 6429: codes 0x80 to 0x9f may be control characters that cause the
1352 * terminal to perform functions. The remaining codes can be graphic.
1355 show_upper_chars(int first)
1357 bool C1 = (first == 128);
1359 int last = first + 31;
1364 mvprintw(0, 20, "Display of %s Character Codes %d to %d",
1365 C1 ? "C1" : "GR", first, last);
1369 for (code = first; code <= last; code++) {
1370 int row = 4 + ((code - first) % 16);
1371 int col = ((code - first) / 16) * COLS / 2;
1373 sprintf(tmp, "%3d (0x%x)", code, code);
1374 mvprintw(row, col, "%*s: ", COLS / 4, tmp);
1376 nodelay(stdscr, TRUE);
1379 /* (yes, this _is_ crude) */
1380 while ((reply = Getchar()) != ERR) {
1384 nodelay(stdscr, FALSE);
1390 show_box_chars(void)
1394 mvaddstr(0, 20, "Display of the ACS Line-Drawing Set");
1399 mvhline(LINES / 2, 0, ACS_HLINE, COLS);
1400 mvvline(0, COLS / 2, ACS_VLINE, LINES);
1401 mvaddch(0, COLS / 2, ACS_TTEE);
1402 mvaddch(LINES / 2, COLS / 2, ACS_PLUS);
1403 mvaddch(LINES - 1, COLS / 2, ACS_BTEE);
1404 mvaddch(LINES / 2, 0, ACS_LTEE);
1405 mvaddch(LINES / 2, COLS - 1, ACS_RTEE);
1411 show_1_acs(int n, const char *name, chtype code)
1413 const int height = 16;
1414 int row = 4 + (n % height);
1415 int col = (n / height) * COLS / 2;
1416 mvprintw(row, col, "%*s : ", COLS / 4, name);
1422 show_acs_chars(void)
1423 /* display the ACS character set */
1427 #define BOTH(name) #name, name
1431 mvaddstr(0, 20, "Display of the ACS Character Set");
1435 n = show_1_acs(0, BOTH(ACS_ULCORNER));
1436 n = show_1_acs(n, BOTH(ACS_URCORNER));
1437 n = show_1_acs(n, BOTH(ACS_LLCORNER));
1438 n = show_1_acs(n, BOTH(ACS_LRCORNER));
1440 n = show_1_acs(n, BOTH(ACS_LTEE));
1441 n = show_1_acs(n, BOTH(ACS_RTEE));
1442 n = show_1_acs(n, BOTH(ACS_TTEE));
1443 n = show_1_acs(n, BOTH(ACS_BTEE));
1445 n = show_1_acs(n, BOTH(ACS_HLINE));
1446 n = show_1_acs(n, BOTH(ACS_VLINE));
1448 n = show_1_acs(n, BOTH(ACS_LARROW));
1449 n = show_1_acs(n, BOTH(ACS_RARROW));
1450 n = show_1_acs(n, BOTH(ACS_UARROW));
1451 n = show_1_acs(n, BOTH(ACS_DARROW));
1453 n = show_1_acs(n, BOTH(ACS_BLOCK));
1454 n = show_1_acs(n, BOTH(ACS_BOARD));
1455 n = show_1_acs(n, BOTH(ACS_LANTERN));
1456 n = show_1_acs(n, BOTH(ACS_BULLET));
1457 n = show_1_acs(n, BOTH(ACS_CKBOARD));
1458 n = show_1_acs(n, BOTH(ACS_DEGREE));
1459 n = show_1_acs(n, BOTH(ACS_DIAMOND));
1460 n = show_1_acs(n, BOTH(ACS_PLMINUS));
1461 n = show_1_acs(n, BOTH(ACS_PLUS));
1463 n = show_1_acs(n, BOTH(ACS_GEQUAL));
1464 n = show_1_acs(n, BOTH(ACS_NEQUAL));
1465 n = show_1_acs(n, BOTH(ACS_LEQUAL));
1467 n = show_1_acs(n, BOTH(ACS_STERLING));
1468 n = show_1_acs(n, BOTH(ACS_PI));
1469 n = show_1_acs(n, BOTH(ACS_S1));
1470 n = show_1_acs(n, BOTH(ACS_S3));
1471 n = show_1_acs(n, BOTH(ACS_S7));
1472 n = show_1_acs(n, BOTH(ACS_S9));
1492 show_upper_chars((c - '0') * 32 + 128);
1495 mvprintw(LINES - 3, 0,
1496 "Note: ANSI terminals may not display C1 characters.");
1497 mvprintw(LINES - 2, 0,
1498 "Select: a=ACS, b=box, 0=C1, 1,2,3=GR characters, q=quit");
1500 } while ((c = Getchar()) != 'x' && c != 'q');
1507 #if USE_WIDEC_SUPPORT
1509 show_upper_widechars(int first)
1513 int last = first + 31;
1517 mvprintw(0, 20, "Display of Character Codes %d to %d", first, last);
1521 for (code = first; code <= last; code++) {
1522 int row = 4 + ((code - first) % 16);
1523 int col = ((code - first) / 16) * COLS / 2;
1525 attr_t attrs = A_NORMAL;
1528 memset(&codes, 0, sizeof(codes));
1530 sprintf(tmp, "%3ld (0x%lx)", code, code);
1531 mvprintw(row, col, "%*s: ", COLS / 4, tmp);
1532 setcchar(&temp, codes, attrs, 0, 0);
1538 show_1_wacs(int n, const char *name, const cchar_t * code)
1540 const int height = 16;
1541 int row = 4 + (n % height);
1542 int col = (n / height) * COLS / 2;
1543 mvprintw(row, col, "%*s : ", COLS / 4, name);
1544 add_wchnstr(code, 1);
1549 show_wacs_chars(void)
1550 /* display the wide-ACS character set */
1554 /*#define BOTH2(name) #name, &(name) */
1555 #define BOTH2(name) #name, name
1559 mvaddstr(0, 20, "Display of the Wide-ACS Character Set");
1563 n = show_1_wacs(0, BOTH2(WACS_ULCORNER));
1564 n = show_1_wacs(n, BOTH2(WACS_URCORNER));
1565 n = show_1_wacs(n, BOTH2(WACS_LLCORNER));
1566 n = show_1_wacs(n, BOTH2(WACS_LRCORNER));
1568 n = show_1_wacs(n, BOTH2(WACS_LTEE));
1569 n = show_1_wacs(n, BOTH2(WACS_RTEE));
1570 n = show_1_wacs(n, BOTH2(WACS_TTEE));
1571 n = show_1_wacs(n, BOTH2(WACS_BTEE));
1573 n = show_1_wacs(n, BOTH2(WACS_HLINE));
1574 n = show_1_wacs(n, BOTH2(WACS_VLINE));
1576 n = show_1_wacs(n, BOTH2(WACS_LARROW));
1577 n = show_1_wacs(n, BOTH2(WACS_RARROW));
1578 n = show_1_wacs(n, BOTH2(WACS_UARROW));
1579 n = show_1_wacs(n, BOTH2(WACS_DARROW));
1581 n = show_1_wacs(n, BOTH2(WACS_BLOCK));
1582 n = show_1_wacs(n, BOTH2(WACS_BOARD));
1583 n = show_1_wacs(n, BOTH2(WACS_LANTERN));
1584 n = show_1_wacs(n, BOTH2(WACS_BULLET));
1585 n = show_1_wacs(n, BOTH2(WACS_CKBOARD));
1586 n = show_1_wacs(n, BOTH2(WACS_DEGREE));
1587 n = show_1_wacs(n, BOTH2(WACS_DIAMOND));
1588 n = show_1_wacs(n, BOTH2(WACS_PLMINUS));
1589 n = show_1_wacs(n, BOTH2(WACS_PLUS));
1591 #ifdef CURSES_WACS_ARRAY
1592 n = show_1_wacs(n, BOTH2(WACS_GEQUAL));
1593 n = show_1_wacs(n, BOTH2(WACS_NEQUAL));
1594 n = show_1_wacs(n, BOTH2(WACS_LEQUAL));
1596 n = show_1_wacs(n, BOTH2(WACS_STERLING));
1597 n = show_1_wacs(n, BOTH2(WACS_PI));
1598 n = show_1_wacs(n, BOTH2(WACS_S1));
1599 n = show_1_wacs(n, BOTH2(WACS_S3));
1600 n = show_1_wacs(n, BOTH2(WACS_S7));
1601 n = show_1_wacs(n, BOTH2(WACS_S9));
1606 show_wbox_chars(void)
1610 mvaddstr(0, 20, "Display of the Wide-ACS Line-Drawing Set");
1613 box_set(stdscr, 0, 0);
1615 mvhline_set(LINES / 2, 0, WACS_HLINE, COLS);
1616 mvvline_set(0, COLS / 2, WACS_VLINE, LINES);
1617 mvadd_wch(0, COLS / 2, WACS_TTEE);
1618 mvadd_wch(LINES / 2, COLS / 2, WACS_PLUS);
1619 mvadd_wch(LINES - 1, COLS / 2, WACS_BTEE);
1620 mvadd_wch(LINES / 2, 0, WACS_LTEE);
1621 mvadd_wch(LINES / 2, COLS - 1, WACS_RTEE);
1627 show_2_wacs(int n, const char *name, char *code)
1629 const int height = 16;
1630 int row = 4 + (n % height);
1631 int col = (n / height) * COLS / 2;
1632 mvprintw(row, col, "%*s : ", COLS / 4, name);
1638 show_utf8_chars(void)
1639 /* display the wide-ACS character set */
1645 mvaddstr(0, 20, "Display of the Wide-ACS Character Set");
1649 n = show_2_wacs(0, "WACS_ULCORNER", "\342\224\214");
1650 n = show_2_wacs(n, "WACS_URCORNER", "\342\224\220");
1651 n = show_2_wacs(n, "WACS_LLCORNER", "\342\224\224");
1652 n = show_2_wacs(n, "WACS_LRCORNER", "\342\224\230");
1654 n = show_2_wacs(n, "WACS_LTEE", "\342\224\234");
1655 n = show_2_wacs(n, "WACS_RTEE", "\342\224\244");
1656 n = show_2_wacs(n, "WACS_TTEE", "\342\224\254");
1657 n = show_2_wacs(n, "WACS_BTEE", "\342\224\264");
1659 n = show_2_wacs(n, "WACS_HLINE", "\342\224\200");
1660 n = show_2_wacs(n, "WACS_VLINE", "\342\224\202");
1662 n = show_2_wacs(n, "WACS_LARROW", "\342\206\220");
1663 n = show_2_wacs(n, "WACS_RARROW", "\342\206\222");
1664 n = show_2_wacs(n, "WACS_UARROW", "\342\206\221");
1665 n = show_2_wacs(n, "WACS_DARROW", "\342\206\223");
1667 n = show_2_wacs(n, "WACS_BLOCK", "\342\226\256");
1668 n = show_2_wacs(n, "WACS_BOARD", "\342\226\222");
1669 n = show_2_wacs(n, "WACS_LANTERN", "\342\230\203");
1670 n = show_2_wacs(n, "WACS_BULLET", "\302\267");
1671 n = show_2_wacs(n, "WACS_CKBOARD", "\342\226\222");
1672 n = show_2_wacs(n, "WACS_DEGREE", "\302\260");
1673 n = show_2_wacs(n, "WACS_DIAMOND", "\342\227\206");
1674 n = show_2_wacs(n, "WACS_PLMINUS", "\302\261");
1675 n = show_2_wacs(n, "WACS_PLUS", "\342\224\274");
1676 n = show_2_wacs(n, "WACS_GEQUAL", "\342\211\245");
1677 n = show_2_wacs(n, "WACS_NEQUAL", "\342\211\240");
1678 n = show_2_wacs(n, "WACS_LEQUAL", "\342\211\244");
1680 n = show_2_wacs(n, "WACS_STERLING", "\302\243");
1681 n = show_2_wacs(n, "WACS_PI", "\317\200");
1682 n = show_2_wacs(n, "WACS_S1", "\342\216\272");
1683 n = show_2_wacs(n, "WACS_S3", "\342\216\273");
1684 n = show_2_wacs(n, "WACS_S7", "\342\216\274");
1685 n = show_2_wacs(n, "WACS_S9", "\342\216\275");
1690 wide_acs_display(void)
1707 show_upper_widechars((c - '0') * 32 + 128);
1710 mvprintw(LINES - 2, 0,
1711 "Select: a WACS, b box, u UTF-8, 0-9 non-ASCII characters, q=quit");
1713 } while ((c = Getchar()) != 'x' && c != 'q');
1723 * Graphic-rendition test (adapted from vttest)
1726 test_sgr_attributes(void)
1730 for (pass = 0; pass < 2; pass++) {
1731 int normal = ((pass == 0 ? A_NORMAL : A_REVERSE)) | BLANK;
1733 /* Use non-default colors if possible to exercise bce a little */
1735 init_pair(1, COLOR_WHITE, COLOR_BLUE);
1736 normal |= COLOR_PAIR(1);
1740 mvprintw(1, 20, "Graphic rendition test pattern:");
1742 mvprintw(4, 1, "vanilla");
1744 #define set_sgr(mask) bkgdset((normal^(mask)));
1746 mvprintw(4, 40, "bold");
1748 set_sgr(A_UNDERLINE);
1749 mvprintw(6, 6, "underline");
1751 set_sgr(A_BOLD | A_UNDERLINE);
1752 mvprintw(6, 45, "bold underline");
1755 mvprintw(8, 1, "blink");
1757 set_sgr(A_BLINK | A_BOLD);
1758 mvprintw(8, 40, "bold blink");
1760 set_sgr(A_UNDERLINE | A_BLINK);
1761 mvprintw(10, 6, "underline blink");
1763 set_sgr(A_BOLD | A_UNDERLINE | A_BLINK);
1764 mvprintw(10, 45, "bold underline blink");
1767 mvprintw(12, 1, "negative");
1769 set_sgr(A_BOLD | A_REVERSE);
1770 mvprintw(12, 40, "bold negative");
1772 set_sgr(A_UNDERLINE | A_REVERSE);
1773 mvprintw(14, 6, "underline negative");
1775 set_sgr(A_BOLD | A_UNDERLINE | A_REVERSE);
1776 mvprintw(14, 45, "bold underline negative");
1778 set_sgr(A_BLINK | A_REVERSE);
1779 mvprintw(16, 1, "blink negative");
1781 set_sgr(A_BOLD | A_BLINK | A_REVERSE);
1782 mvprintw(16, 40, "bold blink negative");
1784 set_sgr(A_UNDERLINE | A_BLINK | A_REVERSE);
1785 mvprintw(18, 6, "underline blink negative");
1787 set_sgr(A_BOLD | A_UNDERLINE | A_BLINK | A_REVERSE);
1788 mvprintw(18, 45, "bold underline blink negative");
1791 mvprintw(LINES - 2, 1, "%s background. ", pass == 0 ? "Dark" :
1797 bkgdset(A_NORMAL | BLANK);
1802 /****************************************************************************
1804 * Windows and scrolling tester.
1806 ****************************************************************************/
1808 #define BOTLINES 4 /* number of line stolen from screen bottom */
1814 #define FRAME struct frame
1823 #ifdef NCURSES_VERSION
1824 #define keypad_active(win) (win)->_use_keypad
1825 #define scroll_active(win) (win)->_scroll
1827 #define keypad_active(win) FALSE
1828 #define scroll_active(win) FALSE
1831 /* We need to know if these flags are actually set, so don't look in FRAME.
1832 * These names are known to work with SVr4 curses as well as ncurses. The
1833 * _use_keypad name does not work with Solaris 8.
1836 HaveKeypad(FRAME * curp)
1838 WINDOW *win = (curp ? curp->wind : stdscr);
1839 return keypad_active(win);
1843 HaveScroll(FRAME * curp)
1845 WINDOW *win = (curp ? curp->wind : stdscr);
1846 return scroll_active(win);
1850 newwin_legend(FRAME * curp)
1852 static const struct {
1857 "^C = create window", 0
1860 "^N = next window", 0
1863 "^P = previous window", 0
1866 "^F = scroll forward", 0
1869 "^B = scroll backward", 0
1872 "^K = keypad(%s)", 1
1875 "^S = scrollok(%s)", 2
1878 "^W = save window to file", 0
1881 "^R = restore window", 0
1894 bool do_keypad = HaveKeypad(curp);
1895 bool do_scroll = HaveScroll(curp);
1899 for (n = 0; n < SIZEOF(legend); n++) {
1900 switch (legend[n].code) {
1902 strcpy(buf, legend[n].msg);
1905 sprintf(buf, legend[n].msg, do_keypad ? "yes" : "no");
1908 sprintf(buf, legend[n].msg, do_scroll ? "yes" : "no");
1911 sprintf(buf, legend[n].msg, do_keypad ? "/ESC" : "");
1914 x = getcurx(stdscr);
1915 addstr((COLS < (x + 3 + (int) strlen(buf))) ? "\n" : (n ? ", " : ""));
1922 transient(FRAME * curp, NCURSES_CONST char *msg)
1924 newwin_legend(curp);
1926 mvaddstr(LINES - 1, 0, msg);
1932 printw("%s characters are echoed, window should %sscroll.",
1933 HaveKeypad(curp) ? "Non-arrow" : "All other",
1934 HaveScroll(curp) ? "" : "not ");
1939 newwin_report(FRAME * curp)
1940 /* report on the cursor's current position, then restore it */
1942 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
1946 transient(curp, (char *) 0);
1948 move(LINES - 1, COLS - 17);
1949 printw("Y = %2d X = %2d", y, x);
1957 selectcell(int uli, int ulj, int lri, int lrj)
1958 /* arrows keys move cursor, return location at current on non-arrow key */
1960 static pair res; /* result cell */
1961 int si = lri - uli + 1; /* depth of the select area */
1962 int sj = lrj - ulj + 1; /* width of the select area */
1963 int i = 0, j = 0; /* offsets into the select area */
1968 move(uli + i, ulj + j);
1969 newwin_report((FRAME *) 0);
1971 switch (Getchar()) {
1986 return ((pair *) 0);
1987 #ifdef NCURSES_MOUSE_VERSION
1993 if (event.y > uli && event.x > ulj) {
2014 outerbox(pair ul, pair lr, bool onoff)
2015 /* draw or erase a box *outside* the given pair of corners */
2017 mvaddch(ul.y - 1, lr.x - 1, onoff ? ACS_ULCORNER : ' ');
2018 mvaddch(ul.y - 1, lr.x + 1, onoff ? ACS_URCORNER : ' ');
2019 mvaddch(lr.y + 1, lr.x + 1, onoff ? ACS_LRCORNER : ' ');
2020 mvaddch(lr.y + 1, ul.x - 1, onoff ? ACS_LLCORNER : ' ');
2021 move(ul.y - 1, ul.x);
2022 hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
2023 move(ul.y, ul.x - 1);
2024 vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
2025 move(lr.y + 1, ul.x);
2026 hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
2027 move(ul.y, lr.x + 1);
2028 vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
2033 /* Ask user for a window definition */
2040 addstr("Use arrows to move cursor, anything else to mark corner 1");
2042 if ((tmp = selectcell(2, 1, LINES - BOTLINES - 2, COLS - 2)) == (pair *) 0)
2043 return ((WINDOW *) 0);
2044 memcpy(&ul, tmp, sizeof(pair));
2045 mvaddch(ul.y - 1, ul.x - 1, ACS_ULCORNER);
2048 addstr("Use arrows to move cursor, anything else to mark corner 2");
2050 if ((tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2)) ==
2052 return ((WINDOW *) 0);
2053 memcpy(&lr, tmp, sizeof(pair));
2055 rwindow = subwin(stdscr, lr.y - ul.y + 1, lr.x - ul.x + 1, ul.y, ul.x);
2057 outerbox(ul, lr, TRUE);
2068 newwin_move(FRAME * curp, int dy, int dx)
2070 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
2074 getyx(win, cur_y, cur_x);
2075 getmaxyx(win, max_y, max_x);
2076 if ((cur_x += dx) < 0)
2078 else if (cur_x >= max_x)
2080 if ((cur_y += dy) < 0)
2082 else if (cur_y >= max_y)
2084 wmove(win, cur_y, cur_x);
2088 delete_framed(FRAME * fp, bool showit)
2092 fp->last->next = fp->next;
2093 fp->next->last = fp->last;
2101 np = (fp == fp->next) ? 0 : fp->next;
2107 acs_and_scroll(void)
2108 /* Demonstrate windows */
2112 FRAME *current = (FRAME *) 0, *neww;
2113 WINDOW *usescr = stdscr;
2115 #define DUMPFILE "screendump"
2117 #ifdef NCURSES_MOUSE_VERSION
2118 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
2123 transient((FRAME *) 0, (char *) 0);
2126 neww = (FRAME *) calloc(1, sizeof(FRAME));
2127 if ((neww->wind = getwindow()) == (WINDOW *) 0)
2130 if (current == 0) { /* First element, */
2131 neww->next = neww; /* so point it at itself */
2134 neww->next = current->next;
2135 neww->last = current;
2136 neww->last->next = neww;
2137 neww->next->last = neww;
2140 /* SVr4 curses sets the keypad on all newly-created windows to
2141 * false. Someone reported that PDCurses makes new windows inherit
2142 * this flag. Remove the following 'keypad()' call to test this
2144 keypad(current->wind, TRUE);
2145 current->do_keypad = HaveKeypad(current);
2146 current->do_scroll = HaveScroll(current);
2149 case CTRL('N'): /* go to next window */
2151 current = current->next;
2154 case CTRL('P'): /* go to previous window */
2156 current = current->last;
2159 case CTRL('F'): /* scroll current window forward */
2161 wscrl(current->wind, 1);
2164 case CTRL('B'): /* scroll current window backwards */
2166 wscrl(current->wind, -1);
2169 case CTRL('K'): /* toggle keypad mode for current */
2171 current->do_keypad = !current->do_keypad;
2172 keypad(current->wind, current->do_keypad);
2178 current->do_scroll = !current->do_scroll;
2179 scrollok(current->wind, current->do_scroll);
2183 case CTRL('W'): /* save and delete window */
2184 if (current == current->next) {
2185 transient(current, "Will not save/delete ONLY window");
2187 } else if ((fp = fopen(DUMPFILE, "w")) == (FILE *) 0) {
2188 transient(current, "Can't open screen dump file");
2190 (void) putwin(current->wind, fp);
2193 current = delete_framed(current, TRUE);
2197 case CTRL('R'): /* restore window */
2198 if ((fp = fopen(DUMPFILE, "r")) == (FILE *) 0) {
2199 transient(current, "Can't open screen dump file");
2201 neww = (FRAME *) calloc(1, sizeof(FRAME));
2203 neww->next = current->next;
2204 neww->last = current;
2205 neww->last->next = neww;
2206 neww->next->last = neww;
2208 neww->wind = getwin(fp);
2211 wrefresh(neww->wind);
2216 case CTRL('X'): /* resize window */
2223 addstr("Use arrows to move cursor, anything else to mark new corner");
2226 getbegyx(current->wind, ul.y, ul.x);
2228 tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2);
2229 if (tmp == (pair *) 0) {
2234 getmaxyx(current->wind, lr.y, lr.x);
2237 outerbox(ul, lr, FALSE);
2238 wnoutrefresh(stdscr);
2240 /* strictly cosmetic hack for the test */
2241 getmaxyx(current->wind, my, mx);
2242 if (my > tmp->y - ul.y) {
2243 getyx(current->wind, lr.y, lr.x);
2244 wmove(current->wind, tmp->y - ul.y + 1, 0);
2245 wclrtobot(current->wind);
2246 wmove(current->wind, lr.y, lr.x);
2248 if (mx > tmp->x - ul.x)
2249 for (i = 0; i < my; i++) {
2250 wmove(current->wind, i, tmp->x - ul.x + 1);
2251 wclrtoeol(current->wind);
2253 wnoutrefresh(current->wind);
2255 memcpy(&lr, tmp, sizeof(pair));
2256 (void) wresize(current->wind, lr.y - ul.y + 0, lr.x - ul.x + 0);
2258 getbegyx(current->wind, ul.y, ul.x);
2259 getmaxyx(current->wind, lr.y, lr.x);
2262 outerbox(ul, lr, TRUE);
2263 wnoutrefresh(stdscr);
2265 wnoutrefresh(current->wind);
2271 #endif /* HAVE_WRESIZE */
2273 case KEY_F(10): /* undocumented --- use this to test area clears */
2274 selectcell(0, 0, LINES - 1, COLS - 1);
2280 newwin_move(current, -1, 0);
2283 newwin_move(current, 1, 0);
2286 newwin_move(current, 0, -1);
2289 newwin_move(current, 0, 1);
2297 getyx(current->wind, y, x);
2301 x = getmaxx(current->wind) - 1;
2303 mvwdelch(current->wind, y, x);
2313 waddch(current->wind, (chtype) c);
2318 newwin_report(current);
2319 usescr = (current ? current->wind : stdscr);
2322 ((c = wGetchar(usescr)) != QUIT
2323 && !((c == ESCAPE) && (keypad_active(usescr)))
2327 while (current != 0)
2328 current = delete_framed(current, FALSE);
2330 scrollok(stdscr, TRUE); /* reset to driver's default */
2331 #ifdef NCURSES_MOUSE_VERSION
2332 mousemask(0, (mmask_t *) 0);
2339 /****************************************************************************
2343 ****************************************************************************/
2346 static unsigned long nap_msec = 1;
2348 static NCURSES_CONST char *mod[] =
2358 /*+-------------------------------------------------------------------------
2360 --------------------------------------------------------------------------*/
2362 wait_a_while(unsigned long msec GCC_UNUSED)
2372 else if (msec > 1000L)
2373 sleep((int) msec / 1000L);
2377 } /* end of wait_a_while */
2379 /*+-------------------------------------------------------------------------
2381 --------------------------------------------------------------------------*/
2383 saywhat(NCURSES_CONST char *text)
2385 wmove(stdscr, LINES - 1, 0);
2387 waddstr(stdscr, text);
2388 } /* end of saywhat */
2390 /*+-------------------------------------------------------------------------
2391 mkpanel(rows,cols,tly,tlx) - alloc a win and panel and associate them
2392 --------------------------------------------------------------------------*/
2394 mkpanel(int color, int rows, int cols, int tly, int tlx)
2399 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
2400 if ((pan = new_panel(win)) == 0) {
2402 } else if (has_colors()) {
2403 int fg = (color == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
2405 init_pair(color, fg, bg);
2406 wbkgdset(win, COLOR_PAIR(color) | ' ');
2408 wbkgdset(win, A_BOLD | ' ');
2412 } /* end of mkpanel */
2414 /*+-------------------------------------------------------------------------
2416 --------------------------------------------------------------------------*/
2418 rmpanel(PANEL * pan)
2420 WINDOW *win = panel_window(pan);
2423 } /* end of rmpanel */
2425 /*+-------------------------------------------------------------------------
2427 --------------------------------------------------------------------------*/
2433 } /* end of pflush */
2435 /*+-------------------------------------------------------------------------
2437 --------------------------------------------------------------------------*/
2439 fill_panel(PANEL * pan)
2441 WINDOW *win = panel_window(pan);
2442 int num = ((const char *) panel_userptr(pan))[1];
2446 wprintw(win, "-pan%c-", num);
2449 for (y = 2; y < getmaxy(win) - 1; y++) {
2450 for (x = 1; x < getmaxx(win) - 1; x++) {
2455 } /* end of fill_panel */
2465 for (y = 0; y < LINES - 1; y++) {
2466 for (x = 0; x < COLS; x++)
2467 wprintw(stdscr, "%d", (y + x) % 10);
2469 for (y = 0; y < 5; y++) {
2476 p1 = mkpanel(COLOR_RED,
2481 set_panel_userptr(p1, "p1");
2483 p2 = mkpanel(COLOR_GREEN,
2488 set_panel_userptr(p2, "p2");
2490 p3 = mkpanel(COLOR_YELLOW,
2495 set_panel_userptr(p3, "p3");
2497 p4 = mkpanel(COLOR_BLUE,
2502 set_panel_userptr(p4, "p4");
2504 p5 = mkpanel(COLOR_MAGENTA,
2509 set_panel_userptr(p5, "p5");
2519 saywhat("press any key to continue");
2520 wait_a_while(nap_msec);
2522 saywhat("h3 s1 s2 s4 s5; press any key to continue");
2523 move_panel(p1, 0, 0);
2530 wait_a_while(nap_msec);
2532 saywhat("s1; press any key to continue");
2535 wait_a_while(nap_msec);
2537 saywhat("s2; press any key to continue");
2540 wait_a_while(nap_msec);
2542 saywhat("m2; press any key to continue");
2543 move_panel(p2, LINES / 3 + 1, COLS / 8);
2545 wait_a_while(nap_msec);
2550 wait_a_while(nap_msec);
2552 saywhat("m3; press any key to continue");
2553 move_panel(p3, LINES / 4 + 1, COLS / 15);
2555 wait_a_while(nap_msec);
2557 saywhat("b3; press any key to continue");
2560 wait_a_while(nap_msec);
2562 saywhat("s4; press any key to continue");
2565 wait_a_while(nap_msec);
2567 saywhat("s5; press any key to continue");
2570 wait_a_while(nap_msec);
2572 saywhat("t3; press any key to continue");
2575 wait_a_while(nap_msec);
2577 saywhat("t1; press any key to continue");
2580 wait_a_while(nap_msec);
2582 saywhat("t2; press any key to continue");
2585 wait_a_while(nap_msec);
2587 saywhat("t3; press any key to continue");
2590 wait_a_while(nap_msec);
2592 saywhat("t4; press any key to continue");
2595 wait_a_while(nap_msec);
2597 for (itmp = 0; itmp < 6; itmp++) {
2598 WINDOW *w4 = panel_window(p4);
2599 WINDOW *w5 = panel_window(p5);
2601 saywhat("m4; press any key to continue");
2602 wmove(w4, LINES / 8, 1);
2603 waddstr(w4, mod[itmp]);
2604 move_panel(p4, LINES / 6, itmp * (COLS / 8));
2605 wmove(w5, LINES / 6, 1);
2606 waddstr(w5, mod[itmp]);
2608 wait_a_while(nap_msec);
2610 saywhat("m5; press any key to continue");
2611 wmove(w4, LINES / 6, 1);
2612 waddstr(w4, mod[itmp]);
2613 move_panel(p5, LINES / 3 - 1, (itmp * 10) + 6);
2614 wmove(w5, LINES / 8, 1);
2615 waddstr(w5, mod[itmp]);
2617 wait_a_while(nap_msec);
2620 saywhat("m4; press any key to continue");
2621 move_panel(p4, LINES / 6, itmp * (COLS / 8));
2623 wait_a_while(nap_msec);
2625 saywhat("t5; press any key to continue");
2628 wait_a_while(nap_msec);
2630 saywhat("t2; press any key to continue");
2633 wait_a_while(nap_msec);
2635 saywhat("t1; press any key to continue");
2638 wait_a_while(nap_msec);
2640 saywhat("d2; press any key to continue");
2643 wait_a_while(nap_msec);
2645 saywhat("h3; press any key to continue");
2648 wait_a_while(nap_msec);
2650 saywhat("d1; press any key to continue");
2653 wait_a_while(nap_msec);
2655 saywhat("d4; press any key to continue");
2658 wait_a_while(nap_msec);
2660 saywhat("d5; press any key to continue");
2663 wait_a_while(nap_msec);
2673 /****************************************************************************
2677 ****************************************************************************/
2681 static bool pending_pan = FALSE;
2682 static bool show_panner_legend = TRUE;
2685 panner_legend(int line)
2687 static const char *const legend[] =
2689 "Use arrow keys (or U,D,L,R) to pan, q to quit, ! to shell-out.",
2690 "Use +,- (or j,k) to grow/shrink the panner vertically.",
2691 "Use <,> (or h,l) to grow/shrink the panner horizontally.",
2692 "Number repeats. Toggle legend:?, timer:t, scroll mark:s."
2694 int n = (SIZEOF(legend) - (LINES - line));
2695 if (line < LINES && (n >= 0)) {
2697 if (show_panner_legend)
2698 printw("%s", legend[n]);
2700 return show_panner_legend;
2706 panner_h_cleanup(int from_y, int from_x, int to_x)
2708 if (!panner_legend(from_y))
2709 do_h_line(from_y, from_x, ' ', to_x);
2713 panner_v_cleanup(int from_y, int from_x, int to_y)
2715 if (!panner_legend(from_y))
2716 do_v_line(from_y, from_x, ' ', to_y);
2721 int top_x, int top_y, int porty, int portx,
2722 int (*pgetc) (WINDOW *))
2724 #if HAVE_GETTIMEOFDAY
2725 struct timeval before, after;
2728 bool scrollers = TRUE;
2731 int pxmax, pymax, lowend, highend, c;
2733 getmaxyx(pad, pymax, pxmax);
2734 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
2738 #ifdef NCURSES_VERSION
2740 * During shell-out, the user may have resized the window. Adjust
2741 * the port size of the pad to accommodate this. Ncurses automatically
2742 * resizes all of the normal windows to fit on the new screen.
2760 show_panner_legend = !show_panner_legend;
2761 panner_legend(LINES - 4);
2762 panner_legend(LINES - 3);
2763 panner_legend(LINES - 2);
2764 panner_legend(LINES - 1);
2766 #if HAVE_GETTIMEOFDAY
2770 panner_legend(LINES - 1);
2774 scrollers = !scrollers;
2777 /* Move the top-left corner of the pad, keeping the bottom-right
2780 case 'h': /* increase-columns: move left edge to left */
2784 panner_v_cleanup(top_y, top_x, porty);
2789 case 'j': /* decrease-lines: move top-edge down */
2793 panner_h_cleanup(top_y - 1, top_x - (top_x > 0), portx);
2798 case 'k': /* increase-lines: move top-edge up */
2803 panner_h_cleanup(top_y, top_x, portx);
2807 case 'l': /* decrease-columns: move left-edge to right */
2811 panner_v_cleanup(top_y - (top_y > 0), top_x - 1, porty);
2816 /* Move the bottom-right corner of the pad, keeping the top-left
2819 case KEY_IC: /* increase-columns: move right-edge to right */
2820 if (portx >= pxmax || portx >= COLS)
2823 panner_v_cleanup(top_y - (top_y > 0), portx - 1, porty);
2828 case KEY_IL: /* increase-lines: move bottom-edge down */
2829 if (porty >= pymax || porty >= LINES)
2832 panner_h_cleanup(porty - 1, top_x - (top_x > 0), portx);
2837 case KEY_DC: /* decrease-columns: move bottom edge up */
2842 panner_v_cleanup(top_y - (top_y > 0), portx, porty);
2846 case KEY_DL: /* decrease-lines */
2851 panner_h_cleanup(porty, top_x - (top_x > 0), portx);
2855 case KEY_LEFT: /* pan leftwards */
2862 case KEY_RIGHT: /* pan rightwards */
2863 if (basex + portx - (pymax > porty) < pxmax)
2869 case KEY_UP: /* pan upwards */
2876 case KEY_DOWN: /* pan downwards */
2877 if (basey + porty - (pxmax > portx) < pymax)
2892 basey = pymax - porty;
2902 mvaddch(top_y - 1, top_x - 1, ACS_ULCORNER);
2903 do_v_line(top_y, top_x - 1, ACS_VLINE, porty);
2904 do_h_line(top_y - 1, top_x, ACS_HLINE, portx);
2906 if (scrollers && (pxmax > portx - 1)) {
2907 int length = (portx - top_x - 1);
2908 float ratio = ((float) length) / ((float) pxmax);
2910 lowend = (int) (top_x + (basex * ratio));
2911 highend = (int) (top_x + ((basex + length) * ratio));
2913 do_h_line(porty - 1, top_x, ACS_HLINE, lowend);
2914 if (highend < portx) {
2916 do_h_line(porty - 1, lowend, ' ', highend + 1);
2918 do_h_line(porty - 1, highend + 1, ACS_HLINE, portx);
2921 do_h_line(porty - 1, top_x, ACS_HLINE, portx);
2923 if (scrollers && (pymax > porty - 1)) {
2924 int length = (porty - top_y - 1);
2925 float ratio = ((float) length) / ((float) pymax);
2927 lowend = (int) (top_y + (basey * ratio));
2928 highend = (int) (top_y + ((basey + length) * ratio));
2930 do_v_line(top_y, portx - 1, ACS_VLINE, lowend);
2931 if (highend < porty) {
2933 do_v_line(lowend, portx - 1, ' ', highend + 1);
2935 do_v_line(highend + 1, portx - 1, ACS_VLINE, porty);
2938 do_v_line(top_y, portx - 1, ACS_VLINE, porty);
2940 mvaddch(top_y - 1, portx - 1, ACS_URCORNER);
2941 mvaddch(porty - 1, top_x - 1, ACS_LLCORNER);
2942 mvaddch(porty - 1, portx - 1, ACS_LRCORNER);
2945 #if HAVE_GETTIMEOFDAY
2946 gettimeofday(&before, 0);
2948 wnoutrefresh(stdscr);
2953 porty - (pxmax > portx) - 1,
2954 portx - (pymax > porty) - 1);
2957 #if HAVE_GETTIMEOFDAY
2960 gettimeofday(&after, 0);
2961 elapsed = (after.tv_sec + after.tv_usec / 1.0e6)
2962 - (before.tv_sec + before.tv_usec / 1.0e6);
2963 move(LINES - 1, COLS - 20);
2964 printw("Secs: %2.03f", elapsed);
2971 ((c = pgetc(pad)) != KEY_EXIT);
2973 scrollok(stdscr, TRUE); /* reset to driver's default */
2977 padgetch(WINDOW *win)
2983 if ((pending_pan = (count > 0)) != FALSE) {
2985 pending_pan = (count != 0);
2988 switch (c = wGetchar(win)) {
3024 case ERR: /* FALLTHRU */
3030 if (c >= '0' && c <= '9') {
3031 count = count * 10 + (c - '0');
3045 #define PAD_HIGH 200
3046 #define PAD_WIDE 200
3050 /* Demonstrate pads. */
3053 unsigned gridcount = 0;
3054 WINDOW *panpad = newpad(PAD_HIGH, PAD_WIDE);
3057 Cannot("cannot create requested pad");
3061 for (i = 0; i < PAD_HIGH; i++) {
3062 for (j = 0; j < PAD_WIDE; j++)
3063 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
3064 if (i == 0 || j == 0)
3065 waddch(panpad, '+');
3067 waddch(panpad, (chtype) ('A' + (gridcount++ % 26)));
3068 } else if (i % GRIDSIZE == 0)
3069 waddch(panpad, '-');
3070 else if (j % GRIDSIZE == 0)
3071 waddch(panpad, '|');
3073 waddch(panpad, ' ');
3075 panner_legend(LINES - 4);
3076 panner_legend(LINES - 3);
3077 panner_legend(LINES - 2);
3078 panner_legend(LINES - 1);
3080 keypad(panpad, TRUE);
3082 /* Make the pad (initially) narrow enough that a trace file won't wrap.
3083 * We'll still be able to widen it during a test, since that's required
3084 * for testing boundaries.
3086 panner(panpad, 2, 2, LINES - 5, COLS - 15, padgetch);
3092 #endif /* USE_LIBPANEL */
3094 /****************************************************************************
3096 * Tests from John Burnell's PDCurses tester
3098 ****************************************************************************/
3101 Continue(WINDOW *win)
3105 mvwaddstr(win, 10, 1, " Press any key to continue");
3111 flushinp_test(WINDOW *win)
3112 /* Input test, adapted from John Burnell's PDCurses tester */
3114 int w, h, bx, by, sw, sh, i;
3119 getmaxyx(win, h, w);
3120 getbegyx(win, by, bx);
3123 if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == 0)
3128 init_pair(2, COLOR_CYAN, COLOR_BLUE);
3129 wbkgd(subWin, COLOR_PAIR(2) | ' ');
3132 wattrset(subWin, A_BOLD);
3133 box(subWin, ACS_VLINE, ACS_HLINE);
3134 mvwaddstr(subWin, 2, 1, "This is a subwindow");
3138 * This used to set 'nocbreak()'. However, Alexander Lukyanov says that
3139 * it only happened to "work" on SVr4 because that implementation does not
3140 * emulate nocbreak+noecho mode, whereas ncurses does. To get the desired
3141 * test behavior, we're using 'cbreak()', which will allow a single
3142 * character to return without needing a newline. - T.Dickey 1997/10/11.
3145 mvwaddstr(win, 0, 1, "This is a test of the flushinp() call.");
3147 mvwaddstr(win, 2, 1, "Type random keys for 5 seconds.");
3148 mvwaddstr(win, 3, 1,
3149 "These should be discarded (not echoed) after the subwindow goes away.");
3152 for (i = 0; i < 5; i++) {
3153 mvwprintw(subWin, 1, 1, "Time = %d", i);
3165 mvwaddstr(win, 2, 1,
3166 "If you were still typing when the window timer expired,");
3167 mvwaddstr(win, 3, 1,
3168 "or else you typed nothing at all while it was running,");
3169 mvwaddstr(win, 4, 1,
3170 "test was invalid. You'll see garbage or nothing at all. ");
3171 mvwaddstr(win, 6, 1, "Press a key");
3177 mvwaddstr(win, 12, 0,
3178 "If you see any key other than what you typed, flushinp() is broken.");
3187 "What you typed should now have been deleted; if not, wdelch() failed.");
3193 /****************************************************************************
3197 ****************************************************************************/
3205 menu_virtualize(int c)
3207 if (c == '\n' || c == KEY_EXIT)
3208 return (MAX_COMMAND + 1);
3210 return (REQ_SCR_ULINE);
3212 return (REQ_SCR_DLINE);
3213 else if (c == 'b' || c == KEY_NPAGE)
3214 return (REQ_SCR_UPAGE);
3215 else if (c == 'f' || c == KEY_PPAGE)
3216 return (REQ_SCR_DPAGE);
3217 else if (c == 'n' || c == KEY_DOWN)
3218 return (REQ_NEXT_ITEM);
3219 else if (c == 'p' || c == KEY_UP)
3220 return (REQ_PREV_ITEM);
3222 return (REQ_TOGGLE_ITEM);
3230 static const char *animals[] =
3232 "Lions", "Tigers", "Bears", "(Oh my!)", "Newts", "Platypi", "Lemurs",
3240 ITEM *items[SIZEOF(animals)];
3243 int mrows, mcols, c;
3246 #ifdef NCURSES_MOUSE_VERSION
3247 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
3249 mvaddstr(0, 0, "This is the menu test:");
3250 mvaddstr(2, 0, " Use up and down arrow to move the select bar.");
3251 mvaddstr(3, 0, " 'n' and 'p' act like arrows.");
3253 " 'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
3254 mvaddstr(5, 0, " Press return to exit.");
3257 for (ap = animals; *ap; ap++)
3258 *ip++ = new_item(*ap, "");
3261 m = new_menu(items);
3263 set_menu_format(m, (SIZEOF(animals) + 1) / 2, 1);
3264 scale_menu(m, &mrows, &mcols);
3266 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
3267 set_menu_win(m, menuwin);
3268 keypad(menuwin, TRUE);
3271 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
3275 while ((c = menu_driver(m, menu_virtualize(wGetchar(menuwin)))) != E_UNKNOWN_COMMAND) {
3276 if (c == E_REQUEST_DENIED)
3281 (void) mvprintw(LINES - 2, 0,
3282 "You chose: %s\n", item_name(current_item(m)));
3283 (void) addstr("Press any key to continue...");
3290 for (ip = items; *ip; ip++)
3292 #ifdef NCURSES_MOUSE_VERSION
3293 mousemask(0, (mmask_t *) 0);
3298 #define T_TBL(name) { #name, name }
3304 T_TBL(TRACE_DISABLE),
3307 T_TBL(TRACE_UPDATE),
3309 T_TBL(TRACE_CHARPUT),
3310 T_TBL(TRACE_ORDINARY),
3312 T_TBL(TRACE_VIRTPUT),
3313 T_TBL(TRACE_IEVENT),
3315 T_TBL(TRACE_ICALLS),
3316 T_TBL(TRACE_CCALLS),
3317 T_TBL(TRACE_DATABASE),
3319 T_TBL(TRACE_MAXIMUM),
3326 tracetrace(int tlevel)
3333 for (n = 0; t_tbl[n].name != 0; n++)
3334 need += strlen(t_tbl[n].name) + 2;
3335 buf = (char *) malloc(need);
3337 sprintf(buf, "0x%02x = {", tlevel);
3339 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
3341 for (n = 1; t_tbl[n].name != 0; n++)
3342 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
3343 strcat(buf, t_tbl[n].name);
3347 if (buf[strlen(buf) - 2] == ',')
3348 buf[strlen(buf) - 2] = '\0';
3349 return (strcat(buf, "}"));
3352 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
3356 run_trace_menu(MENU * m)
3362 bool changed = FALSE;
3363 switch (menu_driver(m, menu_virtualize(wGetchar(menu_win(m))))) {
3364 case E_UNKNOWN_COMMAND:
3367 items = menu_items(m);
3368 i = current_item(m);
3369 if (i == items[0]) {
3370 if (item_value(i)) {
3371 for (p = items + 1; *p != 0; p++)
3372 if (item_value(*p)) {
3373 set_item_value(*p, FALSE);
3378 for (p = items + 1; *p != 0; p++)
3379 if (item_value(*p)) {
3380 set_item_value(items[0], FALSE);
3393 /* interactively set the trace level */
3396 ITEM *items[SIZEOF(t_tbl)];
3398 int mrows, mcols, newtrace;
3402 mvaddstr(0, 0, "Interactively set trace level:");
3403 mvaddstr(2, 0, " Press space bar to toggle a selection.");
3404 mvaddstr(3, 0, " Use up and down arrow to move the select bar.");
3405 mvaddstr(4, 0, " Press return to set the trace level.");
3406 mvprintw(6, 0, "(Current trace level is %s)", tracetrace(_nc_tracing));
3410 for (n = 0; t_tbl[n].name != 0; n++)
3411 *ip++ = new_item(t_tbl[n].name, "");
3414 m = new_menu(items);
3416 set_menu_format(m, 0, 2);
3417 scale_menu(m, &mrows, &mcols);
3419 menu_opts_off(m, O_ONEVALUE);
3420 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
3421 set_menu_win(m, menuwin);
3422 keypad(menuwin, TRUE);
3425 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
3429 for (ip = menu_items(m); *ip; ip++) {
3430 int mask = t_tbl[item_index(*ip)].mask;
3432 set_item_value(*ip, _nc_tracing == 0);
3433 else if ((mask & _nc_tracing) == mask)
3434 set_item_value(*ip, TRUE);
3437 while (run_trace_menu(m))
3441 for (ip = menu_items(m); *ip; ip++)
3442 if (item_value(*ip))
3443 newtrace |= t_tbl[item_index(*ip)].mask;
3445 _tracef("trace level interactively set to %s", tracetrace(_nc_tracing));
3447 (void) mvprintw(LINES - 2, 0,
3448 "Trace level is %s\n", tracetrace(_nc_tracing));
3449 (void) addstr("Press any key to continue...");
3456 for (ip = items; *ip; ip++)
3460 #endif /* USE_LIBMENU */
3462 /****************************************************************************
3466 ****************************************************************************/
3469 make_label(int frow, int fcol, NCURSES_CONST char *label)
3471 FIELD *f = new_field(1, strlen(label), frow, fcol, 0, 0);
3474 set_field_buffer(f, 0, label);
3475 set_field_opts(f, field_opts(f) & ~O_ACTIVE);
3481 make_field(int frow, int fcol, int rows, int cols, bool secure)
3483 FIELD *f = new_field(rows, cols, frow, fcol, 0, secure ? 1 : 0);
3486 set_field_back(f, A_UNDERLINE);
3487 set_field_userptr(f, (void *) 0);
3493 display_form(FORM * f)
3498 scale_form(f, &rows, &cols);
3500 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
3502 set_form_sub(f, derwin(w, rows, cols, 1, 2));
3507 if (post_form(f) != E_OK)
3512 erase_form(FORM * f)
3514 WINDOW *w = form_win(f);
3515 WINDOW *s = form_sub(f);
3525 edit_secure(FIELD * me, int c)
3527 int rows, cols, frow, fcol, nrow, nbuf;
3529 if (field_info(me, &rows, &cols, &frow, &fcol, &nrow, &nbuf) == E_OK
3534 strcpy(temp, field_buffer(me, 1));
3535 len = (long) (char *) field_userptr(me);
3540 set_field_buffer(me, 1, temp);
3560 case REQ_RIGHT_CHAR:
3562 c = 0; /* we don't want to do inline editing */
3567 set_field_buffer(me, 1, temp);
3574 set_field_buffer(me, 1, temp);
3579 set_field_userptr(me, (void *) len);
3585 form_virtualize(FORM * f, WINDOW *w)
3587 static const struct {
3592 CTRL('A'), REQ_NEXT_CHOICE
3595 CTRL('B'), REQ_PREV_WORD
3598 CTRL('C'), REQ_CLR_EOL
3601 CTRL('D'), REQ_DOWN_FIELD
3604 CTRL('E'), REQ_END_FIELD
3607 CTRL('F'), REQ_NEXT_PAGE
3610 CTRL('G'), REQ_DEL_WORD
3613 CTRL('H'), REQ_DEL_PREV
3616 CTRL('I'), REQ_INS_CHAR
3619 CTRL('K'), REQ_CLR_EOF
3622 CTRL('L'), REQ_LEFT_FIELD
3625 CTRL('M'), REQ_NEW_LINE
3628 CTRL('N'), REQ_NEXT_FIELD
3631 CTRL('O'), REQ_INS_LINE
3634 CTRL('P'), REQ_PREV_FIELD
3637 CTRL('R'), REQ_RIGHT_FIELD
3640 CTRL('S'), REQ_BEG_FIELD
3643 CTRL('U'), REQ_UP_FIELD
3646 CTRL('V'), REQ_DEL_CHAR
3649 CTRL('W'), REQ_NEXT_WORD
3652 CTRL('X'), REQ_CLR_FIELD
3655 CTRL('Y'), REQ_DEL_LINE
3658 CTRL('Z'), REQ_PREV_CHOICE
3661 ESCAPE, MAX_FORM_COMMAND + 1
3664 KEY_BACKSPACE, REQ_DEL_PREV
3667 KEY_DOWN, REQ_DOWN_CHAR
3670 KEY_END, REQ_LAST_FIELD
3673 KEY_HOME, REQ_FIRST_FIELD
3676 KEY_LEFT, REQ_LEFT_CHAR
3679 KEY_LL, REQ_LAST_FIELD
3682 KEY_NEXT, REQ_NEXT_FIELD
3685 KEY_NPAGE, REQ_NEXT_PAGE
3688 KEY_PPAGE, REQ_PREV_PAGE
3691 KEY_PREVIOUS, REQ_PREV_FIELD
3694 KEY_RIGHT, REQ_RIGHT_CHAR
3700 QUIT, MAX_FORM_COMMAND + 1
3704 static int mode = REQ_INS_MODE;
3705 int c = wGetchar(w);
3707 FIELD *me = current_field(f);
3709 if (c == CTRL(']')) {
3710 if (mode == REQ_INS_MODE)
3711 mode = REQ_OVL_MODE;
3713 mode = REQ_INS_MODE;
3716 for (n = 0; n < SIZEOF(lookup); n++) {
3717 if (lookup[n].code == c) {
3718 c = lookup[n].result;
3725 * Force the field that the user is typing into to be in reverse video,
3726 * while the other fields are shown underlined.
3729 c = edit_secure(me, c);
3730 set_field_back(me, A_REVERSE);
3731 } else if (c <= MAX_FORM_COMMAND) {
3732 c = edit_secure(me, c);
3733 set_field_back(me, A_UNDERLINE);
3739 my_form_driver(FORM * form, int c)
3741 if (c == (MAX_FORM_COMMAND + 1)
3742 && form_driver(form, REQ_VALIDATION) == E_OK)
3755 FIELD *f[12], *secure;
3756 int finished = 0, c;
3760 addstr("Defined form-traversal keys: ^Q/ESC- exit form\n");
3761 addstr("^N -- go to next field ^P -- go to previous field\n");
3762 addstr("Home -- go to first field End -- go to last field\n");
3763 addstr("^L -- go to field to left ^R -- go to field to right\n");
3764 addstr("^U -- move upward to field ^D -- move downward to field\n");
3765 addstr("^W -- go to next word ^B -- go to previous word\n");
3766 addstr("^S -- go to start of field ^E -- go to end of field\n");
3767 addstr("^H -- delete previous char ^Y -- delete line\n");
3768 addstr("^G -- delete current word ^C -- clear to end of line\n");
3769 addstr("^K -- clear to end of field ^X -- clear field\n");
3770 addstr("Arrow keys move within a field as you would expect.");
3772 mvaddstr(4, 57, "Forms Entry Test");
3776 /* describe the form */
3777 f[n++] = make_label(0, 15, "Sample Form");
3778 f[n++] = make_label(2, 0, "Last Name");
3779 f[n++] = make_field(3, 0, 1, 18, FALSE);
3780 f[n++] = make_label(2, 20, "First Name");
3781 f[n++] = make_field(3, 20, 1, 12, FALSE);
3782 f[n++] = make_label(2, 34, "Middle Name");
3783 f[n++] = make_field(3, 34, 1, 12, FALSE);
3784 f[n++] = make_label(5, 0, "Comments");
3785 f[n++] = make_field(6, 0, 4, 46, FALSE);
3786 f[n++] = make_label(5, 20, "Password:");
3788 f[n++] = make_field(5, 30, 1, 9, TRUE);
3789 f[n++] = (FIELD *) 0;
3797 nonl(); /* lets us read ^M's */
3799 switch (form_driver(form, c = form_virtualize(form, w))) {
3801 mvaddstr(5, 57, field_buffer(secure, 1));
3805 case E_UNKNOWN_COMMAND:
3806 finished = my_form_driver(form, c);
3817 for (c = 0; f[c] != 0; c++)
3822 #endif /* USE_LIBFORM */
3824 /****************************************************************************
3828 ****************************************************************************/
3831 fillwin(WINDOW *win, char ch)
3836 getmaxyx(win, y1, x1);
3837 for (y = 0; y < y1; y++) {
3839 for (x = 0; x < x1; x++)
3845 crosswin(WINDOW *win, char ch)
3850 getmaxyx(win, y1, x1);
3851 for (y = 0; y < y1; y++) {
3852 for (x = 0; x < x1; x++)
3853 if (((x > (x1 - 1) / 3) && (x <= (2 * (x1 - 1)) / 3))
3854 || (((y > (y1 - 1) / 3) && (y <= (2 * (y1 - 1)) / 3)))) {
3863 /* test effects of overlapping windows */
3867 WINDOW *win1 = newwin(9, 20, 3, 3);
3868 WINDOW *win2 = newwin(9, 20, 9, 16);
3873 printw("This test shows the behavior of wnoutrefresh() with respect to\n");
3874 printw("the shared region of two overlapping windows A and B. The cross\n");
3875 printw("pattern in each window does not overlap the other.\n");
3878 printw("a = refresh A, then B, then doupdate. b = refresh B, then A, then doupdaute\n");
3879 printw("c = fill window A with letter A. d = fill window B with letter B.\n");
3880 printw("e = cross pattern in window A. f = cross pattern in window B.\n");
3881 printw("g = clear window A. h = clear window B.\n");
3882 printw("i = overwrite A onto B. j = overwrite B onto A.\n");
3883 printw("^Q/ESC = terminate test.");
3885 while ((ch = Getchar()) != QUIT && ch != ESCAPE)
3887 case 'a': /* refresh window A first, then B */
3893 case 'b': /* refresh window B first, then A */
3899 case 'c': /* fill window A so it's visible */
3903 case 'd': /* fill window B so it's visible */
3907 case 'e': /* cross test pattern in window A */
3908 crosswin(win1, 'A');
3911 case 'f': /* cross test pattern in window A */
3912 crosswin(win2, 'B');
3915 case 'g': /* clear window A */
3920 case 'h': /* clear window B */
3925 case 'i': /* overwrite A onto B */
3926 overwrite(win1, win2);
3929 case 'j': /* overwrite B onto A */
3930 overwrite(win2, win1);
3940 /****************************************************************************
3944 ****************************************************************************/
3947 do_single_test(const char c)
3948 /* perform a single specified test */
3955 #if USE_WIDEC_SUPPORT
3967 Cannot("does not support color.");
3974 Cannot("does not support color.");
3975 else if (!can_change_color())
3976 Cannot("has hardwired color values.");
3989 #if USE_WIDEC_SUPPORT
4006 flushinp_test(stdscr);
4010 test_sgr_attributes();
4035 #if USE_LIBMENU && defined(TRACE)
4054 static const char *const tbl[] =
4056 "Usage: ncurses [options]"
4059 #ifdef NCURSES_VERSION
4060 ," -a f,b set default-colors (assumed white-on-black)"
4061 ," -d use default-colors if terminal supports them"
4063 ," -e fmt specify format for soft-keys test (e)"
4064 ," -f rip-off footer line (can repeat)"
4065 ," -h rip-off header line (can repeat)"
4066 ," -s msec specify nominal time for panel-demo (default: 1, to hold)"
4068 ," -t mask specify default trace-level (may toggle with ^T)"
4072 for (n = 0; n < SIZEOF(tbl); n++)
4073 fprintf(stderr, "%s\n", tbl[n]);
4074 ExitProgram(EXIT_FAILURE);
4078 set_terminal_modes(void)
4083 scrollok(stdscr, TRUE);
4084 idlok(stdscr, TRUE);
4085 keypad(stdscr, TRUE);
4090 announce_sig(int sig)
4092 (void) fprintf(stderr, "Handled signal %d\r\n", sig);
4097 rip_footer(WINDOW *win, int cols)
4099 wbkgd(win, A_REVERSE);
4102 wprintw(win, "footer: %d columns", cols);
4108 rip_header(WINDOW *win, int cols)
4110 wbkgd(win, A_REVERSE);
4113 wprintw(win, "header: %d columns", cols);
4118 /*+-------------------------------------------------------------------------
4120 --------------------------------------------------------------------------*/
4123 main(int argc, char *argv[])
4127 #ifdef NCURSES_VERSION
4128 int default_fg = COLOR_WHITE;
4129 int default_bg = COLOR_BLACK;
4130 bool assumed_colors = FALSE;
4131 bool default_colors = FALSE;
4134 setlocale(LC_ALL, "");
4136 while ((c = getopt(argc, argv, "a:de:fhs:t:")) != EOF) {
4138 #ifdef NCURSES_VERSION
4140 assumed_colors = TRUE;
4141 sscanf(optarg, "%d,%d", &default_fg, &default_bg);
4144 default_colors = TRUE;
4148 my_e_param = atoi(optarg);
4149 #ifdef NCURSES_VERSION
4150 if (my_e_param > 3) /* allow extended layouts */
4158 ripoffline(-1, rip_footer);
4161 ripoffline(1, rip_header);
4165 nap_msec = atol(optarg);
4170 save_trace = atoi(optarg);
4179 * If there's no menus (unlikely for ncurses!), then we'll have to set
4180 * tracing on initially, just in case the user wants to test something that
4181 * doesn't involve wGetchar.
4184 /* enable debugging */
4188 if (!isatty(fileno(stdin)))
4190 #endif /* USE_LIBMENU */
4193 /* tell it we're going to play with soft keys */
4194 slk_init(my_e_param);
4197 /* set up null signal catcher so we can see what interrupts to getch do */
4198 signal(SIGUSR1, announce_sig);
4201 /* we must initialize the curses data structure only once */
4205 /* tests, in general, will want these modes */
4208 #ifdef NCURSES_VERSION_PATCH
4209 max_colors = COLORS > 16 ? 16 : COLORS;
4210 #if HAVE_USE_DEFAULT_COLORS
4212 use_default_colors();
4213 #if NCURSES_VERSION_PATCH >= 20000708
4214 else if (assumed_colors)
4215 assume_default_colors(default_fg, default_bg);
4218 #else /* normal SVr4 curses */
4219 max_colors = COLORS > 8 ? 8 : COLORS;
4221 max_pairs = (max_colors * max_colors);
4222 if (max_pairs < COLOR_PAIRS)
4223 max_pairs = COLOR_PAIRS;
4225 set_terminal_modes();
4229 * Return to terminal mode, so we're guaranteed of being able to
4230 * select terminal commands even if the capabilities are wrong.
4234 #if HAVE_CURSES_VERSION
4235 (void) printf("Welcome to %s. Press ? for help.\n", curses_version());
4236 #elif defined(NCURSES_VERSION_MAJOR) && defined(NCURSES_VERSION_MINOR) && defined(NCURSES_VERSION_PATCH)
4237 (void) printf("Welcome to ncurses %d.%d.%d. Press ? for help.\n",
4238 NCURSES_VERSION_MAJOR,
4239 NCURSES_VERSION_MINOR,
4240 NCURSES_VERSION_PATCH);
4242 (void) puts("Welcome to ncurses. Press ? for help.");
4246 (void) puts("This is the ncurses main menu");
4247 (void) puts("a = keyboard and mouse input test");
4248 #if USE_WIDEC_SUPPORT
4249 (void) puts("A = wide-character keyboard and mouse input test");
4251 (void) puts("b = character attribute test");
4252 (void) puts("c = color test pattern");
4253 (void) puts("d = edit RGB color values");
4254 (void) puts("e = exercise soft keys");
4255 (void) puts("f = display ACS characters");
4256 #if USE_WIDEC_SUPPORT
4257 (void) puts("F = display Wide-ACS characters");
4259 (void) puts("g = display windows and scrolling");
4260 (void) puts("i = test of flushinp()");
4261 (void) puts("k = display character attributes");
4263 (void) puts("m = menu code test");
4266 (void) puts("o = exercise panels library");
4267 (void) puts("p = exercise pad features");
4268 (void) puts("q = quit");
4271 (void) puts("r = exercise forms code");
4273 (void) puts("s = overlapping-refresh test");
4274 #if USE_LIBMENU && defined(TRACE)
4275 (void) puts("t = set trace level");
4277 (void) puts("? = repeat this command summary");
4279 (void) fputs("> ", stdout);
4280 (void) fflush(stdout); /* necessary under SVr4 curses */
4283 * This used to be an 'fgets()' call. However (on Linux, at least)
4284 * mixing stream I/O and 'read()' (used in the library) causes the
4285 * input stream to be flushed when switching between the two.
4290 if (read(fileno(stdin), &ch, 1) <= 0) {
4294 } else if (command == 0 && !isspace(UChar(ch))) {
4296 } else if (ch == '\n' || ch == '\r') {
4299 (void) fputs("> ", stdout);
4300 (void) fflush(stdout);
4304 if (do_single_test(command)) {
4306 * This may be overkill; it's intended to reset everything back
4307 * to the initial terminal modes so that tests don't get in
4311 set_terminal_modes();
4316 if (command == '?') {
4317 (void) puts("This is the ncurses capability tester.");
4319 puts("You may select a test from the main menu by typing the");
4321 puts("key letter of the choice (the letter to left of the =)");
4323 puts("at the > prompt. The commands `x' or `q' will exit.");
4330 ExitProgram(EXIT_SUCCESS);
4333 /* ncurses.c ends here */