1 /****************************************************************************
2 * Copyright (c) 1998-2004,2005 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.253 2005/10/01 16:00:56 tom Exp $
45 ***************************************************************************/
47 #include <test.priv.h>
50 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
54 #include <sys/select.h>
70 #ifdef NCURSES_VERSION
73 static unsigned save_trace = TRACE_ORDINARY | TRACE_CALLS;
74 extern unsigned _nc_tracing;
79 #define mmask_t chtype /* not specified in XSI */
81 #ifdef CURSES_ACS_ARRAY
82 #define ACS_S3 (CURSES_ACS_ARRAY['p']) /* scan line 3 */
83 #define ACS_S7 (CURSES_ACS_ARRAY['r']) /* scan line 7 */
84 #define ACS_LEQUAL (CURSES_ACS_ARRAY['y']) /* less/equal */
85 #define ACS_GEQUAL (CURSES_ACS_ARRAY['z']) /* greater/equal */
86 #define ACS_PI (CURSES_ACS_ARRAY['{']) /* Pi */
87 #define ACS_NEQUAL (CURSES_ACS_ARRAY['|']) /* not equal */
88 #define ACS_STERLING (CURSES_ACS_ARRAY['}']) /* UK pound sign */
90 #define ACS_S3 (A_ALTCHARSET + 'p') /* scan line 3 */
91 #define ACS_S7 (A_ALTCHARSET + 'r') /* scan line 7 */
92 #define ACS_LEQUAL (A_ALTCHARSET + 'y') /* less/equal */
93 #define ACS_GEQUAL (A_ALTCHARSET + 'z') /* greater/equal */
94 #define ACS_PI (A_ALTCHARSET + '{') /* Pi */
95 #define ACS_NEQUAL (A_ALTCHARSET + '|') /* not equal */
96 #define ACS_STERLING (A_ALTCHARSET + '}') /* UK pound sign */
99 #ifdef CURSES_WACS_ARRAY
100 #define WACS_S3 (&(CURSES_WACS_ARRAY['p'])) /* scan line 3 */
101 #define WACS_S7 (&(CURSES_WACS_ARRAY['r'])) /* scan line 7 */
102 #define WACS_LEQUAL (&(CURSES_WACS_ARRAY['y'])) /* less/equal */
103 #define WACS_GEQUAL (&(CURSES_WACS_ARRAY['z'])) /* greater/equal */
104 #define WACS_PI (&(CURSES_WACS_ARRAY['{'])) /* Pi */
105 #define WACS_NEQUAL (&(CURSES_WACS_ARRAY['|'])) /* not equal */
106 #define WACS_STERLING (&(CURSES_WACS_ARRAY['}'])) /* UK pound sign */
111 #define P(string) printw("%s\n", string)
113 #define BLANK ' ' /* this is the background character */
116 static int max_colors; /* the actual number of colors we'll use */
117 static int min_colors; /* the minimum color code */
120 static int max_pairs; /* ...and the number of color pairs */
128 static RGB_DATA *all_colors;
130 static void main_menu(bool);
132 /* The behavior of mvhline, mvvline for negative/zero length is unspecified,
133 * though we can rely on negative x/y values to stop the macro.
136 do_h_line(int y, int x, chtype c, int to)
139 mvhline(y, x, c, (to) - (x));
143 do_v_line(int y, int x, chtype c, int to)
146 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 /* replaces wgetnstr(), since we want to be able to edit values */
186 wGetstring(WINDOW *win, char *buffer, int limit)
193 wattrset(win, A_REVERSE);
197 if (x > (int) strlen(buffer))
198 x = (int) strlen(buffer);
200 wprintw(win, "%-*s", limit, buffer);
201 wmove(win, y0, x0 + x);
202 switch (ch = wGetchar(win)) {
215 for (j = --x; (buffer[j] = buffer[j + 1]) != '\0'; ++j) {
233 if (!isprint(ch) || ch >= KEY_MIN) {
235 } else if ((int) strlen(buffer) < limit) {
237 for (j = strlen(buffer) + 1; j > x; --j) {
238 buffer[j] = buffer[j - 1];
247 wattroff(win, A_REVERSE);
252 #if USE_WIDEC_SUPPORT
254 wGet_wchar(WINDOW *win, wint_t *result)
258 while ((c = wget_wch(win, result)) == CTRL('T')) {
260 save_trace = _nc_tracing;
261 _tracef("TOGGLE-TRACING OFF");
264 _nc_tracing = save_trace;
268 _tracef("TOGGLE-TRACING ON");
271 c = wget_wch(win, result);
275 #define Get_wchar(result) wGet_wchar(stdscr, result)
277 /* replaces wgetn_wstr(), since we want to be able to edit values */
279 wGet_wstring(WINDOW *win, wchar_t *buffer, int limit)
288 wattrset(win, A_REVERSE);
292 if (x > (int) wcslen(buffer))
293 x = (int) wcslen(buffer);
295 /* clear the "window' */
297 wprintw(win, "%*s", limit, " ");
299 /* write the existing buffer contents */
301 waddnwstr(win, buffer, limit);
303 /* positions the cursor past character 'x' */
305 waddnwstr(win, buffer, x);
307 switch (wGet_wchar(win, &ch)) {
347 for (j = --x; (buffer[j] = buffer[j + 1]) != '\0'; ++j) {
367 } else if ((int) wcslen(buffer) < limit) {
369 for (j = wcslen(buffer) + 1; j > x; --j) {
370 buffer[j] = buffer[j - 1];
379 wattroff(win, A_REVERSE);
390 addstr("Press any key to continue... ");
395 Cannot(const char *what)
397 printw("\nThis %s terminal %s\n\n", getenv("TERM"), what);
402 ShellOut(bool message)
405 addstr("Shelling out...");
410 addstr("returned from shellout.\n");
414 #ifdef NCURSES_MOUSE_VERSION
416 * This function is the same as _tracemouse(), but we cannot count on that
417 * being available in the non-debug library.
420 mouse_decode(MEVENT const *ep)
422 static char buf[80 + (5 * 10) + (32 * 15)];
424 (void) sprintf(buf, "id %2d at (%2d, %2d, %2d) state %4lx = {",
425 ep->id, ep->x, ep->y, ep->z, (unsigned long) ep->bstate);
427 #define SHOW(m, s) if ((ep->bstate & m)==m) {strcat(buf,s); strcat(buf, ", ");}
429 SHOW(BUTTON1_RELEASED, "release-1");
430 SHOW(BUTTON1_PRESSED, "press-1");
431 SHOW(BUTTON1_CLICKED, "click-1");
432 SHOW(BUTTON1_DOUBLE_CLICKED, "doubleclick-1");
433 SHOW(BUTTON1_TRIPLE_CLICKED, "tripleclick-1");
434 #if NCURSES_MOUSE_VERSION == 1
435 SHOW(BUTTON1_RESERVED_EVENT, "reserved-1");
438 SHOW(BUTTON2_RELEASED, "release-2");
439 SHOW(BUTTON2_PRESSED, "press-2");
440 SHOW(BUTTON2_CLICKED, "click-2");
441 SHOW(BUTTON2_DOUBLE_CLICKED, "doubleclick-2");
442 SHOW(BUTTON2_TRIPLE_CLICKED, "tripleclick-2");
443 #if NCURSES_MOUSE_VERSION == 1
444 SHOW(BUTTON2_RESERVED_EVENT, "reserved-2");
447 SHOW(BUTTON3_RELEASED, "release-3");
448 SHOW(BUTTON3_PRESSED, "press-3");
449 SHOW(BUTTON3_CLICKED, "click-3");
450 SHOW(BUTTON3_DOUBLE_CLICKED, "doubleclick-3");
451 SHOW(BUTTON3_TRIPLE_CLICKED, "tripleclick-3");
452 #if NCURSES_MOUSE_VERSION == 1
453 SHOW(BUTTON3_RESERVED_EVENT, "reserved-3");
456 SHOW(BUTTON4_RELEASED, "release-4");
457 SHOW(BUTTON4_PRESSED, "press-4");
458 SHOW(BUTTON4_CLICKED, "click-4");
459 SHOW(BUTTON4_DOUBLE_CLICKED, "doubleclick-4");
460 SHOW(BUTTON4_TRIPLE_CLICKED, "tripleclick-4");
461 #if NCURSES_MOUSE_VERSION == 1
462 SHOW(BUTTON4_RESERVED_EVENT, "reserved-4");
465 #if NCURSES_MOUSE_VERSION == 2
466 SHOW(BUTTON5_RELEASED, "release-5");
467 SHOW(BUTTON5_PRESSED, "press-5");
468 SHOW(BUTTON5_CLICKED, "click-5");
469 SHOW(BUTTON5_DOUBLE_CLICKED, "doubleclick-5");
470 SHOW(BUTTON5_TRIPLE_CLICKED, "tripleclick-5");
473 SHOW(BUTTON_CTRL, "ctrl");
474 SHOW(BUTTON_SHIFT, "shift");
475 SHOW(BUTTON_ALT, "alt");
476 SHOW(ALL_MOUSE_EVENTS, "all-events");
477 SHOW(REPORT_MOUSE_POSITION, "position");
481 if (buf[strlen(buf) - 1] == ' ')
482 buf[strlen(buf) - 2] = '\0';
483 (void) strcat(buf, "}");
486 #endif /* NCURSES_MOUSE_VERSION */
488 /****************************************************************************
490 * Character input test
492 ****************************************************************************/
495 setup_getch(WINDOW *win, bool flags[])
497 keypad(win, flags['k']); /* should be redundant, but for testing */
498 meta(win, flags['m']); /* force this to a known state */
506 wgetch_help(WINDOW *win, bool flags[])
508 static const char *help[] =
510 "e -- toggle echo mode"
511 ,"g -- triggers a getstr test"
512 ,"k -- toggle keypad/literal mode"
513 ,"m -- toggle meta (7-bit/8-bit) mode"
514 ,"q -- quit (x also exits)"
516 ,"w -- create a new window"
518 ,"z -- suspend this process"
522 unsigned chk = ((SIZEOF(help) + 1) / 2);
527 printw("Type any key to see its %s value. Also:\n",
528 flags['k'] ? "keypad" : "literal");
529 for (n = 0; n < SIZEOF(help); ++n) {
530 int row = 1 + (n % chk);
531 int col = (n >= chk) ? COLS / 2 : 0;
532 int flg = ((strstr(help[n], "toggle") != 0)
533 && (flags[UChar(*help[n])] != FALSE));
536 mvprintw(row, col, "%s", help[n]);
547 wgetch_wrap(WINDOW *win, int first_y)
549 int last_y = getmaxy(win) - 1;
550 int y = getcury(win) + 1;
558 #if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
564 static WINSTACK *winstack = 0;
565 static unsigned len_winstack = 0;
568 remember_boxes(unsigned level, WINDOW *txt_win, WINDOW *box_win)
570 unsigned need = (level + 1) * 2;
574 winstack = (WINSTACK *) malloc(len_winstack * sizeof(WINSTACK));
575 } else if (need >= len_winstack) {
577 winstack = (WINSTACK *) realloc(winstack, len_winstack * sizeof(WINSTACK));
579 winstack[level].text = txt_win;
580 winstack[level].frame = box_win;
584 * For wgetch_test(), we create pairs of windows - one for a box, one for text.
585 * Resize both and paint the box in the parent.
588 resize_boxes(unsigned level, WINDOW *win)
592 int high = LINES - base;
596 wnoutrefresh(stdscr);
598 /* FIXME: this chunk should be done in resizeterm() */
603 for (n = 0; n < level; ++n) {
604 wresize(winstack[n].frame, high, wide);
605 wresize(winstack[n].text, high - 2, wide - 2);
608 werase(winstack[n].text);
609 box(winstack[n].frame, 0, 0);
610 wnoutrefresh(winstack[n].frame);
611 wprintw(winstack[n].text,
613 getmaxy(winstack[n].text),
614 getmaxx(winstack[n].text));
615 wnoutrefresh(winstack[n].text);
616 if (winstack[n].text == win)
622 #define remember_boxes(level,text,frame) /* nothing */
626 wgetch_test(unsigned level, WINDOW *win, int delay)
629 int first_y, first_x;
633 bool blocking = (delay < 0);
635 memset(flags, FALSE, sizeof(flags));
636 flags[UChar('k')] = (win == stdscr);
638 setup_getch(win, flags);
639 wtimeout(win, delay);
640 getyx(win, first_y, first_x);
642 wgetch_help(win, flags);
643 wsetscrreg(win, first_y, getmaxy(win) - 1);
647 while ((c = wGetchar(win)) == ERR) {
650 (void) wprintw(win, "%05d: input error", incount);
653 (void) wprintw(win, "%05d: input timed out", incount);
655 wgetch_wrap(win, first_y);
657 if (c == ERR && blocking) {
659 wgetch_wrap(win, first_y);
660 } else if (c == 'x' || c == 'q') {
662 } else if (c == 'e') {
663 flags[UChar('e')] = !flags[UChar('e')];
664 setup_getch(win, flags);
665 wgetch_help(win, flags);
666 } else if (c == 'g') {
667 waddstr(win, "getstr test: ");
669 wgetnstr(win, buf, sizeof(buf) - 1);
671 wprintw(win, "I saw %d characters:\n\t`%s'.", (int) strlen(buf), buf);
673 wgetch_wrap(win, first_y);
674 } else if (c == 'k') {
675 flags[UChar('k')] = !flags[UChar('k')];
676 setup_getch(win, flags);
677 wgetch_help(win, flags);
678 } else if (c == 'm') {
679 flags[UChar('m')] = !flags[UChar('m')];
680 setup_getch(win, flags);
681 wgetch_help(win, flags);
682 } else if (c == 's') {
684 } else if (c == 'w') {
685 int high = getmaxy(win) - 1 - first_y + 1;
686 int wide = getmaxx(win) - first_x;
688 int new_y = first_y + getbegy(win);
689 int new_x = first_x + getbegx(win);
691 getyx(win, old_y, old_x);
692 if (high > 2 && wide > 2) {
693 WINDOW *wb = newwin(high, wide, new_y, new_x);
694 WINDOW *wi = newwin(high - 2, wide - 2, new_y + 1, new_x + 1);
699 remember_boxes(level, wi, wb);
700 wgetch_test(level + 1, wi, delay);
704 wgetch_help(win, flags);
705 wmove(win, old_y, old_x);
711 } else if (c == 'z') {
712 kill(getpid(), SIGTSTP);
715 wprintw(win, "Key pressed: %04o ", c);
716 #ifdef NCURSES_MOUSE_VERSION
717 if (c == KEY_MOUSE) {
722 wprintw(win, "KEY_MOUSE, %s", mouse_decode(&event));
724 move(event.y, event.x);
728 #endif /* NCURSES_MOUSE_VERSION */
730 #if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
731 if (c == KEY_RESIZE) {
732 resize_boxes(level, win);
735 (void) waddstr(win, keyname(c));
736 } else if (c > 0x80) {
737 unsigned c2 = (c & 0x7f);
739 (void) wprintw(win, "M-%c", UChar(c2));
741 (void) wprintw(win, "M-%s", unctrl(c2));
742 waddstr(win, " (high-half character)");
745 (void) wprintw(win, "%c (ASCII printable character)", c);
747 (void) wprintw(win, "%s (ASCII control character)",
750 wgetch_wrap(win, first_y);
758 begin_getch_test(void)
765 #ifdef NCURSES_MOUSE_VERSION
766 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
769 (void) printw("Delay in 10ths of a second (<CR> for blocking input)? ");
771 getnstr(buf, sizeof(buf) - 1);
775 if (isdigit(UChar(buf[0]))) {
776 delay = atoi(buf) * 100;
786 finish_getch_test(void)
788 #ifdef NCURSES_MOUSE_VERSION
789 mousemask(0, (mmask_t *) 0);
800 int delay = begin_getch_test();
801 wgetch_test(0, stdscr, delay);
805 #if USE_WIDEC_SUPPORT
807 * For wgetch_test(), we create pairs of windows - one for a box, one for text.
808 * Resize both and paint the box in the parent.
812 resize_wide_boxes(unsigned level, WINDOW *win)
816 int high = LINES - base;
820 wnoutrefresh(stdscr);
822 /* FIXME: this chunk should be done in resizeterm() */
827 for (n = 0; n < level; ++n) {
828 wresize(winstack[n].frame, high, wide);
829 wresize(winstack[n].text, high - 2, wide - 2);
832 werase(winstack[n].text);
833 box_set(winstack[n].frame, 0, 0);
834 wnoutrefresh(winstack[n].frame);
835 wprintw(winstack[n].text,
837 getmaxy(winstack[n].text),
838 getmaxx(winstack[n].text));
839 wnoutrefresh(winstack[n].text);
840 if (winstack[n].text == win)
845 #endif /* KEY_RESIZE */
848 wcstos(const wchar_t *src)
853 const wchar_t *tmp = src;
855 memset(&state, 0, sizeof(state));
856 if ((need = wcsrtombs(0, &tmp, 0, &state)) > 0) {
857 unsigned have = need;
858 result = (char *) calloc(have + 1, 1);
860 if (wcsrtombs(result, &tmp, have, &state) != have) {
869 wget_wch_test(unsigned level, WINDOW *win, int delay)
871 wchar_t wchar_buf[BUFSIZ];
872 wint_t wint_buf[BUFSIZ];
873 int first_y, first_x;
877 bool blocking = (delay < 0);
881 memset(flags, FALSE, sizeof(flags));
882 flags[UChar('k')] = (win == stdscr);
884 setup_getch(win, flags);
885 wtimeout(win, delay);
886 getyx(win, first_y, first_x);
888 wgetch_help(win, flags);
889 wsetscrreg(win, first_y, getmaxy(win) - 1);
893 while ((code = wGet_wchar(win, &c)) == ERR) {
896 (void) wprintw(win, "%05d: input error", incount);
899 (void) wprintw(win, "%05d: input timed out", incount);
901 wgetch_wrap(win, first_y);
903 if (code == ERR && blocking) {
905 wgetch_wrap(win, first_y);
906 } else if (c == 'x' || c == 'q') {
908 } else if (c == 'e') {
909 flags[UChar('e')] = !flags[UChar('e')];
910 setup_getch(win, flags);
911 wgetch_help(win, flags);
912 } else if (c == 'g') {
913 waddstr(win, "getstr test: ");
915 code = wgetn_wstr(win, wint_buf, sizeof(wint_buf) - 1);
918 wprintw(win, "wgetn_wstr returns an error.");
921 for (n = 0; (wchar_buf[n] = wint_buf[n]) != 0; ++n) ;
922 if ((temp = wcstos(wchar_buf)) != 0) {
923 wprintw(win, "I saw %d characters:\n\t`%s'.",
924 wcslen(wchar_buf), temp);
927 wprintw(win, "I saw %d characters (cannot convert).",
932 wgetch_wrap(win, first_y);
933 } else if (c == 'k') {
934 flags[UChar('k')] = !flags[UChar('k')];
935 setup_getch(win, flags);
936 wgetch_help(win, flags);
937 } else if (c == 'm') {
938 flags[UChar('m')] = !flags[UChar('m')];
939 setup_getch(win, flags);
940 wgetch_help(win, flags);
941 } else if (c == 's') {
943 } else if (c == 'w') {
944 int high = getmaxy(win) - 1 - first_y + 1;
945 int wide = getmaxx(win) - first_x;
947 int new_y = first_y + getbegy(win);
948 int new_x = first_x + getbegx(win);
950 getyx(win, old_y, old_x);
951 if (high > 2 && wide > 2) {
952 WINDOW *wb = newwin(high, wide, new_y, new_x);
953 WINDOW *wi = newwin(high - 2, wide - 2, new_y + 1, new_x + 1);
958 remember_boxes(level, wi, wb);
959 wget_wch_test(level + 1, wi, delay);
963 wgetch_help(win, flags);
964 wmove(win, old_y, old_x);
969 } else if (c == 'z') {
970 kill(getpid(), SIGTSTP);
973 wprintw(win, "Key pressed: %04o ", c);
974 #ifdef NCURSES_MOUSE_VERSION
975 if (c == KEY_MOUSE) {
979 wprintw(win, "KEY_MOUSE, %s", mouse_decode(&event));
981 move(event.y, event.x);
985 #endif /* NCURSES_MOUSE_VERSION */
986 if (code == KEY_CODE_YES) {
988 if (c == KEY_RESIZE) {
989 resize_wide_boxes(level, win);
992 (void) waddstr(win, key_name((wchar_t) c));
994 if (c < 256 && iscntrl(c)) {
995 (void) wprintw(win, "%s (control character)", unctrl(c));
998 waddnwstr(win, &c2, 1);
999 (void) wprintw(win, " = %#x (printable character)", c);
1002 wgetch_wrap(win, first_y);
1012 int delay = begin_getch_test();
1013 wget_wch_test(0, stdscr, delay);
1014 finish_getch_test();
1018 /****************************************************************************
1020 * Character attributes test
1022 ****************************************************************************/
1024 #define MAX_ATTRSTRING 31
1025 #define LEN_ATTRSTRING 26
1027 static char attr_test_string[MAX_ATTRSTRING + 1];
1030 attr_legend(WINDOW *helpwin)
1035 mvwprintw(helpwin, row++, col,
1036 "q or ESC to exit.");
1037 mvwprintw(helpwin, row++, col,
1040 mvwprintw(helpwin, row++, col,
1041 "Modify the test strings:");
1042 mvwprintw(helpwin, row++, col,
1043 " A digit sets gaps on each side of displayed attributes");
1044 mvwprintw(helpwin, row++, col,
1045 " </> shifts the text left/right. ");
1047 mvwprintw(helpwin, row++, col,
1050 mvwprintw(helpwin, row++, col,
1051 " f/F/b/F toggle foreground/background background color");
1052 mvwprintw(helpwin, row++, col,
1053 " t/T toggle text/background color attribute");
1055 mvwprintw(helpwin, row++, col,
1056 " a/A toggle ACS (alternate character set) mapping");
1057 mvwprintw(helpwin, row++, col,
1058 " v/V toggle video attribute to combine with each line");
1062 show_color_attr(int fg, int bg, int tx)
1065 printw(" Colors (fg %d, bg %d", fg, bg);
1067 printw(", text %d", tx);
1073 cycle_color_attr(int ch, int *fg, int *bg, int *tx)
1104 if (*fg < min_colors)
1108 if (*bg < min_colors)
1122 adjust_attr_string(int adjust)
1124 int first = ((int) UChar(attr_test_string[0])) + adjust;
1125 int last = first + LEN_ATTRSTRING;
1127 if (first >= ' ' && last <= '~') { /* 32..126 */
1129 for (j = 0, k = first; j < MAX_ATTRSTRING && k <= last; ++j, ++k) {
1130 attr_test_string[j] = k;
1131 if (((k + 1 - first) % 5) == 0) {
1133 if (j < MAX_ATTRSTRING)
1134 attr_test_string[j] = ' ';
1137 while (j < MAX_ATTRSTRING)
1138 attr_test_string[j++] = ' ';
1139 attr_test_string[j] = '\0';
1146 init_attr_string(void)
1148 attr_test_string[0] = 'a';
1149 adjust_attr_string(0);
1153 show_attr(int row, int skip, bool arrow, chtype attr, const char *name)
1155 int ncv = tigetnum("ncv");
1156 chtype test = attr & (chtype) (~A_ALTCHARSET);
1159 mvprintw(row, 5, "-->");
1160 mvprintw(row, 8, "%s mode:", name);
1161 mvprintw(row, 24, "|");
1163 printw("%*s", skip, " ");
1165 * Just for testing, write text using the alternate character set one
1166 * character at a time (to pass its rendition directly), and use the
1167 * string operation for the other attributes.
1169 if (attr & A_ALTCHARSET) {
1173 for (s = attr_test_string; *s != '\0'; ++s) {
1179 addstr(attr_test_string);
1183 printw("%*s", skip, " ");
1185 if (test != A_NORMAL) {
1186 if (!(termattrs() & test)) {
1189 if (ncv > 0 && (getbkgd(stdscr) & A_COLOR)) {
1190 static const chtype table[] =
1204 for (n = 0; n < SIZEOF(table); n++) {
1205 if ((table[n] & attr) != 0
1206 && ((1 << n) & ncv) != 0) {
1214 if ((termattrs() & test) != test)
1221 static const struct {
1223 NCURSES_CONST char * name;
1224 } attrs_to_test[] = {
1225 { A_STANDOUT, "STANDOUT" },
1226 { A_REVERSE, "REVERSE" },
1228 { A_UNDERLINE, "UNDERLINE" },
1230 { A_BLINK, "BLINK" },
1231 { A_PROTECT, "PROTECT" },
1232 { A_INVIS, "INVISIBLE" },
1233 { A_NORMAL, "NORMAL" },
1238 attr_getc(int *skip, int *fg, int *bg, int *tx, int *ac, unsigned *kc)
1248 if (ch < 256 && isdigit(ch)) {
1256 if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
1258 attr_legend(helpwin);
1271 *kc = SIZEOF(attrs_to_test) - 1;
1277 if (*kc >= SIZEOF(attrs_to_test))
1281 adjust_attr_string(-1);
1284 adjust_attr_string(1);
1291 error = cycle_color_attr(ch, fg, bg, tx);
1301 /* test text attributes */
1304 int skip = tigetnum("xmc");
1305 int fg = COLOR_BLACK; /* color pair 0 is special */
1306 int bg = COLOR_BLACK;
1314 n = skip; /* make it easy */
1315 k = SIZEOF(attrs_to_test) - 1;
1320 chtype normal = A_NORMAL | BLANK;
1324 int pair = (fg != COLOR_BLACK || bg != COLOR_BLACK);
1327 if (init_pair(pair, fg, bg) == ERR) {
1330 normal |= COLOR_PAIR(pair);
1335 if (init_pair(pair, tx, bg) == ERR) {
1338 extras |= COLOR_PAIR(pair);
1347 mvaddstr(0, 20, "Character attribute test display");
1349 for (j = 0; j < SIZEOF(attrs_to_test); ++j) {
1350 row = show_attr(row, n, j == k,
1352 attrs_to_test[j].attr |
1353 attrs_to_test[k].attr,
1354 attrs_to_test[j].name);
1358 "This terminal does %shave the magic-cookie glitch",
1359 tigetnum("xmc") > -1 ? "" : "not ");
1360 mvprintw(row + 1, 8, "Enter '?' for help.");
1361 show_color_attr(fg, bg, tx);
1362 printw(" ACS (%d)", ac != 0);
1365 } while (attr_getc(&n, &fg, &bg, &tx, &ac, &k));
1367 bkgdset(A_NORMAL | BLANK);
1372 #if USE_WIDEC_SUPPORT
1373 static wchar_t wide_attr_test_string[MAX_ATTRSTRING + 1];
1376 wide_adjust_attr_string(int adjust)
1378 int first = ((int) UChar(wide_attr_test_string[0])) + adjust;
1379 int last = first + LEN_ATTRSTRING;
1381 if (first >= ' ' && last <= '~') { /* 32..126 */
1383 for (j = 0, k = first; j < MAX_ATTRSTRING && k <= last; ++j, ++k) {
1384 wide_attr_test_string[j] = k;
1385 if (((k + 1 - first) % 5) == 0) {
1387 if (j < MAX_ATTRSTRING)
1388 wide_attr_test_string[j] = ' ';
1391 while (j < MAX_ATTRSTRING)
1392 wide_attr_test_string[j++] = ' ';
1393 wide_attr_test_string[j] = '\0';
1400 wide_init_attr_string(void)
1402 wide_attr_test_string[0] = 'a';
1403 wide_adjust_attr_string(0);
1407 set_wide_background(short pair)
1414 setcchar(&normal, blank, A_NORMAL, pair, 0);
1420 get_wide_background(void)
1422 attr_t result = A_NORMAL;
1428 if (getbkgrnd(&ch) != ERR) {
1429 if (getcchar(&ch, &wch, &attr, &pair, 0) != ERR) {
1437 wide_show_attr(int row, int skip, bool arrow, chtype attr, short pair, const char *name)
1439 int ncv = tigetnum("ncv");
1440 chtype test = attr & ~WA_ALTCHARSET;
1443 mvprintw(row, 5, "-->");
1444 mvprintw(row, 8, "%s mode:", name);
1445 mvprintw(row, 24, "|");
1447 printw("%*s", skip, " ");
1450 * Just for testing, write text using the alternate character set one
1451 * character at a time (to pass its rendition directly), and use the
1452 * string operation for the other attributes.
1454 if (attr & WA_ALTCHARSET) {
1458 for (s = wide_attr_test_string; *s != L'\0'; ++s) {
1462 setcchar(&ch, fill, attr, pair, 0);
1469 attr_get(&old_attr, &old_pair, 0);
1470 attr_set(attr, pair, 0);
1471 addwstr(wide_attr_test_string);
1472 attr_set(old_attr, old_pair, 0);
1475 printw("%*s", skip, " ");
1477 if (test != A_NORMAL) {
1478 if (!(term_attrs() & test)) {
1481 if (ncv > 0 && (get_wide_background() & A_COLOR)) {
1482 static const attr_t table[] =
1496 for (n = 0; n < SIZEOF(table); n++) {
1497 if ((table[n] & attr) != 0
1498 && ((1 << n) & ncv) != 0) {
1506 if ((term_attrs() & test) != test)
1514 wide_attr_getc(int *skip, int *fg, int *bg, int *tx, int *ac, unsigned *kc)
1524 if (ch < 256 && isdigit(ch)) {
1532 if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
1533 box_set(helpwin, 0, 0);
1534 attr_legend(helpwin);
1547 *kc = SIZEOF(attrs_to_test) - 1;
1553 if (*kc >= SIZEOF(attrs_to_test))
1557 wide_adjust_attr_string(-1);
1560 wide_adjust_attr_string(1);
1567 error = cycle_color_attr(ch, fg, bg, tx);
1576 wide_attr_test(void)
1577 /* test text attributes using wide-character calls */
1580 int skip = tigetnum("xmc");
1581 int fg = COLOR_BLACK; /* color pair 0 is special */
1582 int bg = COLOR_BLACK;
1590 n = skip; /* make it easy */
1591 k = SIZEOF(attrs_to_test) - 1;
1592 wide_init_attr_string();
1600 pair = (fg != COLOR_BLACK || bg != COLOR_BLACK);
1603 if (init_pair(pair, fg, bg) == ERR) {
1610 if (init_pair(extras, tx, bg) == ERR) {
1615 set_wide_background(pair);
1618 box_set(stdscr, 0, 0);
1619 mvaddstr(0, 20, "Character attribute test display");
1621 for (j = 0; j < SIZEOF(attrs_to_test); ++j) {
1622 row = wide_show_attr(row, n, j == k,
1624 attrs_to_test[j].attr |
1625 attrs_to_test[k].attr,
1627 attrs_to_test[j].name);
1631 "This terminal does %shave the magic-cookie glitch",
1632 tigetnum("xmc") > -1 ? "" : "not ");
1633 mvprintw(row + 1, 8, "Enter '?' for help.");
1634 show_color_attr(fg, bg, tx);
1635 printw(" ACS (%d)", ac != 0);
1638 } while (wide_attr_getc(&n, &fg, &bg, &tx, &ac, &k));
1640 set_wide_background(0);
1646 /****************************************************************************
1648 * Color support tests
1650 ****************************************************************************/
1652 static NCURSES_CONST char *the_color_names[] =
1673 show_color_name(int y, int x, int color, bool wide)
1675 if (move(y, x) != ERR) {
1680 sprintf(temp, "%02d", color);
1682 } else if (color >= 8) {
1683 sprintf(temp, "[%02d]", color);
1685 strcpy(temp, the_color_names[color]);
1687 printw("%-*.*s", width, width, temp);
1692 color_legend(WINDOW *helpwin)
1697 mvwprintw(helpwin, row++, col,
1698 "q or ESC to exit.");
1700 mvwprintw(helpwin, row++, col,
1701 "Use up/down arrow to scroll through the display if it is");
1702 mvwprintw(helpwin, row++, col,
1703 "longer than one screen. Control/N and Control/P can be used");
1704 mvwprintw(helpwin, row++, col,
1705 "in place up up/down arrow. Use pageup/pagedown to scroll a");
1706 mvwprintw(helpwin, row++, col,
1707 "full screen; control/B and control/F can be used here.");
1709 mvwprintw(helpwin, row++, col,
1711 mvwprintw(helpwin, row++, col,
1712 " b/B toggle bold off/on");
1713 mvwprintw(helpwin, row++, col,
1714 " n/N toggle text/number on/off");
1715 mvwprintw(helpwin, row++, col,
1716 " w/W toggle width between 8/16 colors");
1719 #define set_color_test(name, value) if (name != value) { name = value; base_row = 0; }
1721 /* generate a color test pattern */
1729 int grid_top = top + 3;
1730 int page_size = (LINES - grid_top);
1731 int pairs_max = PAIR_NUMBER(A_COLOR) + 1;
1737 bool opt_bold = FALSE;
1738 bool opt_wide = FALSE;
1739 bool opt_nums = FALSE;
1742 if (pairs_max > COLOR_PAIRS)
1743 pairs_max = COLOR_PAIRS;
1748 /* this assumes an 80-column line */
1752 per_row = (COLORS > 8) ? 16 : 8;
1759 row_limit = (pairs_max + per_row - 1) / per_row;
1762 (void) printw("There are %d color pairs and %d colors\n",
1766 (void) mvprintw(top + 1, 0,
1767 "%dx%d matrix of foreground/background colors, bold *%s*\n",
1770 opt_bold ? "on" : "off");
1772 /* show color names/numbers across the top */
1773 for (i = 0; i < per_row; i++)
1774 show_color_name(top + 2, (i + 1) * width, i, opt_wide);
1776 /* show a grid of colors, with color names/ numbers on the left */
1777 for (i = (base_row * per_row); i < pairs_max; i++) {
1778 int row = grid_top + (i / per_row) - base_row;
1779 int col = (i % per_row + 1) * width;
1782 if (row >= 0 && move(row, col) != ERR) {
1783 init_pair(pair, i % COLORS, i / COLORS);
1784 attron((attr_t) COLOR_PAIR(pair));
1786 attron((attr_t) A_BOLD);
1789 sprintf(numbered, "{%02X}", i);
1792 printw("%-*.*s", width, width, hello);
1795 if ((i % per_row) == 0 && (i % COLORS) == 0) {
1796 show_color_name(row, 0, i / COLORS, opt_wide);
1804 switch (c = wGetchar(stdscr)) {
1822 set_color_test(opt_wide, FALSE);
1825 set_color_test(opt_wide, TRUE);
1829 if (base_row <= 0) {
1837 if (base_row + page_size >= row_limit) {
1846 if (base_row <= 0) {
1849 base_row -= (page_size - 1);
1857 if (base_row + page_size >= row_limit) {
1860 base_row += page_size - 1;
1861 if (base_row + page_size >= row_limit) {
1862 base_row = row_limit - page_size - 1;
1867 if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
1869 color_legend(helpwin);
1884 #if USE_WIDEC_SUPPORT
1885 /* generate a color test pattern */
1887 wide_color_test(void)
1893 int grid_top = top + 3;
1894 int page_size = (LINES - grid_top);
1895 int pairs_max = COLOR_PAIRS;
1901 bool opt_bold = FALSE;
1902 bool opt_wide = FALSE;
1903 bool opt_nums = FALSE;
1909 /* this assumes an 80-column line */
1913 per_row = (COLORS > 8) ? 16 : 8;
1920 row_limit = (pairs_max + per_row - 1) / per_row;
1923 (void) printw("There are %d color pairs and %d colors\n",
1927 (void) mvprintw(top + 1, 0,
1928 "%dx%d matrix of foreground/background colors, bold *%s*\n",
1931 opt_bold ? "on" : "off");
1933 /* show color names/numbers across the top */
1934 for (i = 0; i < per_row; i++)
1935 show_color_name(top + 2, (i + 1) * width, i, opt_wide);
1937 /* show a grid of colors, with color names/ numbers on the left */
1938 for (i = (base_row * per_row); i < pairs_max; i++) {
1939 int row = grid_top + (i / per_row) - base_row;
1940 int col = (i % per_row + 1) * width;
1943 if (row >= 0 && move(row, col) != ERR) {
1944 init_pair(pair, i % COLORS, i / COLORS);
1945 color_set(pair, NULL);
1947 attr_on((attr_t) A_BOLD, NULL);
1950 sprintf(numbered, "{%02X}", i);
1953 printw("%-*.*s", width, width, hello);
1954 attr_set(A_NORMAL, 0, NULL);
1956 if ((i % per_row) == 0 && (i % COLORS) == 0) {
1957 show_color_name(row, 0, i / COLORS, opt_wide);
1965 switch (c = wGetchar(stdscr)) {
1983 set_color_test(opt_wide, FALSE);
1986 set_color_test(opt_wide, TRUE);
1990 if (base_row <= 0) {
1998 if (base_row + page_size >= row_limit) {
2007 if (base_row <= 0) {
2010 base_row -= (page_size - 1);
2018 if (base_row + page_size >= row_limit) {
2021 base_row += page_size - 1;
2022 if (base_row + page_size >= row_limit) {
2023 base_row = row_limit - page_size - 1;
2028 if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
2030 color_legend(helpwin);
2044 #endif /* USE_WIDEC_SUPPORT */
2047 change_color(int current, int field, int value, int usebase)
2049 short red, green, blue;
2052 color_content(current, &red, &green, &blue);
2054 red = green = blue = 0;
2068 if (init_color(current, red, green, blue) == ERR)
2073 init_all_colors(void)
2076 for (c = 0; c < COLORS; ++c)
2079 all_colors[c].green,
2080 all_colors[c].blue);
2083 #define scaled_rgb(n) ((255 * (n)) / 1000)
2087 /* display the color test pattern, without trying to edit colors */
2089 int i, this_c = 0, value = 0, current = 0, field = 0;
2092 int page_size = (LINES - 6);
2097 for (i = 0; i < max_colors; i++)
2098 init_pair(i, COLOR_WHITE, i);
2100 mvprintw(LINES - 2, 0, "Number: %d", value);
2103 short red, green, blue;
2106 mvaddstr(0, 20, "Color RGB Value Editing");
2110 (i - top_color < page_size)
2111 && (i < max_colors); i++) {
2113 sprintf(numeric, "[%d]", i);
2114 mvprintw(2 + i - top_color, 0, "%c %-8s:",
2115 (i == current ? '>' : ' '),
2116 (i < (int) SIZEOF(the_color_names)
2117 ? the_color_names[i] : numeric));
2118 attrset(COLOR_PAIR(i));
2122 color_content(i, &red, &green, &blue);
2124 if (current == i && field == 0)
2126 printw("%04d", red);
2127 if (current == i && field == 0)
2130 if (current == i && field == 1)
2132 printw("%04d", green);
2133 if (current == i && field == 1)
2136 if (current == i && field == 2)
2138 printw("%04d", blue);
2139 if (current == i && field == 2)
2142 printw(" ( %3d %3d %3d )",
2148 mvaddstr(LINES - 3, 0,
2149 "Use up/down to select a color, left/right to change fields.");
2150 mvaddstr(LINES - 2, 0,
2151 "Modify field by typing nnn=, nnn-, or nnn+. ? for help.");
2153 move(2 + current - top_color, 0);
2157 if (this_c < 256 && isdigit(this_c) && !isdigit(last_c))
2164 current -= (page_size - 1);
2171 if (current < (max_colors - 1))
2172 current += (page_size - 1);
2179 current = (current == 0 ? (max_colors - 1) : current - 1);
2184 current = (current == (max_colors - 1) ? 0 : current + 1);
2188 field = (field == 2 ? 0 : field + 1);
2192 field = (field == 0 ? 2 : field - 1);
2205 value = value * 10 + (this_c - '0');
2209 change_color(current, field, value, 1);
2213 change_color(current, field, -value, 1);
2217 change_color(current, field, value, 0);
2222 P(" RGB Value Editing Help");
2224 P("You are in the RGB value editor. Use the arrow keys to select one of");
2225 P("the fields in one of the RGB triples of the current colors; the one");
2226 P("currently selected will be reverse-video highlighted.");
2228 P("To change a field, enter the digits of the new value; they are echoed");
2229 P("as entered. Finish by typing `='. The change will take effect instantly.");
2230 P("To increment or decrement a value, use the same procedure, but finish");
2231 P("with a `+' or `-'.");
2233 P("Press 'm' to invoke the top-level menu with the current color settings.");
2234 P("To quit, do `x' or 'q'");
2257 if (current >= max_colors)
2258 current = max_colors - 1;
2259 if (current < top_color)
2260 top_color = current;
2261 if (current - top_color >= page_size)
2262 top_color = current - (page_size - 1);
2264 mvprintw(LINES - 1, 0, "Number: %d", value);
2267 (this_c != 'x' && this_c != 'q');
2272 * ncurses does not reset each color individually when calling endwin().
2279 /****************************************************************************
2281 * Soft-key label test
2283 ****************************************************************************/
2286 #define SLK_WORK (SLK_HELP + 3)
2291 static const char *table[] =
2293 "Available commands are:"
2295 ,"^L -- repaint this message and activate soft keys"
2296 ,"a/d -- activate/disable soft keys"
2297 ,"c -- set centered format for labels"
2298 ,"l -- set left-justified format for labels"
2299 ,"r -- set right-justified format for labels"
2300 ,"[12345678] -- set label; labels are numbered 1 through 8"
2301 ,"e -- erase stdscr (should not erase labels)"
2302 ,"s -- test scrolling of shortened screen"
2304 ,"F/B -- cycle through foreground/background colors"
2306 ,"x, q -- return to main menu"
2308 ,"Note: if activating the soft keys causes your terminal to scroll up"
2309 ,"one line, your terminal auto-scrolls when anything is written to the"
2310 ,"last screen position. The ncurses code does not yet handle this"
2316 for (j = 0; j < SIZEOF(table); ++j) {
2324 /* exercise the soft keys */
2330 short fg = COLOR_BLACK;
2331 short bg = COLOR_WHITE;
2332 bool new_color = FALSE;
2345 init_pair(1, bg, fg);
2348 mvprintw(SLK_WORK, 0, "Colors %d/%d\n", fg, bg);
2357 mvaddstr(0, 20, "Soft Key Exerciser");
2372 mvprintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: ");
2373 while ((c = Getchar()) != 'Q' && (c != ERR))
2401 (void) mvaddstr(SLK_WORK, 0, "Please enter the label value: ");
2403 if ((s = slk_label(c - '0')) != 0) {
2406 wGetstring(stdscr, buf, 8);
2407 slk_set((c - '0'), buf, fmt);
2420 fg = (fg + 1) % COLORS;
2426 bg = (bg + 1) % COLORS;
2436 ((c = Getchar()) != EOF);
2443 #if USE_WIDEC_SUPPORT
2447 /* exercise the soft keys */
2450 wchar_t buf[SLKLEN + 1];
2452 short fg = COLOR_BLACK;
2453 short bg = COLOR_WHITE;
2454 bool new_color = FALSE;
2462 init_pair(1, bg, fg);
2465 mvprintw(SLK_WORK, 0, "Colors %d/%d\n", fg, bg);
2472 attr_on(WA_BOLD, NULL);
2473 mvaddstr(0, 20, "Soft Key Exerciser");
2474 attr_off(WA_BOLD, NULL);
2488 mvprintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: ");
2489 while ((c = Getchar()) != 'Q' && (c != ERR))
2517 (void) mvaddstr(SLK_WORK, 0, "Please enter the label value: ");
2519 if ((s = slk_label(c - '0')) != 0) {
2520 char *temp = strdup(s);
2521 size_t used = strlen(temp);
2522 size_t want = SLKLEN;
2527 while (want > 0 && used != 0) {
2528 const char *base = s;
2529 memset(&state, 0, sizeof(state));
2530 test = mbsrtowcs(0, &base, 0, &state);
2531 if (test == (size_t) -1) {
2533 } else if (test > want) {
2536 memset(&state, 0, sizeof(state));
2537 mbsrtowcs(buf, &base, want, &state);
2543 wGet_wstring(stdscr, buf, SLKLEN);
2544 slk_wset((c - '0'), buf, fmt);
2556 fg = (fg + 1) % COLORS;
2562 bg = (bg + 1) % COLORS;
2571 ((c = Getchar()) != EOF);
2579 /****************************************************************************
2581 * Alternate character-set stuff
2583 ****************************************************************************/
2585 /* ISO 6429: codes 0x80 to 0x9f may be control characters that cause the
2586 * terminal to perform functions. The remaining codes can be graphic.
2589 show_upper_chars(unsigned first)
2591 bool C1 = (first == 128);
2593 unsigned last = first + 31;
2598 mvprintw(0, 20, "Display of %s Character Codes %d to %d",
2599 C1 ? "C1" : "GR", first, last);
2603 for (code = first; code <= last; code++) {
2604 int row = 4 + ((code - first) % 16);
2605 int col = ((code - first) / 16) * COLS / 2;
2607 sprintf(tmp, "%3u (0x%x)", code, code);
2608 mvprintw(row, col, "%*s: ", COLS / 4, tmp);
2610 nodelay(stdscr, TRUE);
2613 /* (yes, this _is_ crude) */
2614 while ((reply = Getchar()) != ERR) {
2615 addch(UChar(reply));
2618 nodelay(stdscr, FALSE);
2624 show_box_chars(void)
2628 mvaddstr(0, 20, "Display of the ACS Line-Drawing Set");
2633 mvhline(LINES / 2, 0, ACS_HLINE, COLS);
2634 mvvline(0, COLS / 2, ACS_VLINE, LINES);
2635 mvaddch(0, COLS / 2, ACS_TTEE);
2636 mvaddch(LINES / 2, COLS / 2, ACS_PLUS);
2637 mvaddch(LINES - 1, COLS / 2, ACS_BTEE);
2638 mvaddch(LINES / 2, 0, ACS_LTEE);
2639 mvaddch(LINES / 2, COLS - 1, ACS_RTEE);
2645 show_1_acs(int n, const char *name, chtype code)
2647 const int height = 16;
2648 int row = 4 + (n % height);
2649 int col = (n / height) * COLS / 2;
2650 mvprintw(row, col, "%*s : ", COLS / 4, name);
2656 show_acs_chars(void)
2657 /* display the ACS character set */
2661 #define BOTH(name) #name, name
2665 mvaddstr(0, 20, "Display of the ACS Character Set");
2669 n = show_1_acs(0, BOTH(ACS_ULCORNER));
2670 n = show_1_acs(n, BOTH(ACS_URCORNER));
2671 n = show_1_acs(n, BOTH(ACS_LLCORNER));
2672 n = show_1_acs(n, BOTH(ACS_LRCORNER));
2674 n = show_1_acs(n, BOTH(ACS_LTEE));
2675 n = show_1_acs(n, BOTH(ACS_RTEE));
2676 n = show_1_acs(n, BOTH(ACS_TTEE));
2677 n = show_1_acs(n, BOTH(ACS_BTEE));
2679 n = show_1_acs(n, BOTH(ACS_HLINE));
2680 n = show_1_acs(n, BOTH(ACS_VLINE));
2682 n = show_1_acs(n, BOTH(ACS_LARROW));
2683 n = show_1_acs(n, BOTH(ACS_RARROW));
2684 n = show_1_acs(n, BOTH(ACS_UARROW));
2685 n = show_1_acs(n, BOTH(ACS_DARROW));
2687 n = show_1_acs(n, BOTH(ACS_BLOCK));
2688 n = show_1_acs(n, BOTH(ACS_BOARD));
2689 n = show_1_acs(n, BOTH(ACS_LANTERN));
2690 n = show_1_acs(n, BOTH(ACS_BULLET));
2691 n = show_1_acs(n, BOTH(ACS_CKBOARD));
2692 n = show_1_acs(n, BOTH(ACS_DEGREE));
2693 n = show_1_acs(n, BOTH(ACS_DIAMOND));
2694 n = show_1_acs(n, BOTH(ACS_PLMINUS));
2695 n = show_1_acs(n, BOTH(ACS_PLUS));
2697 n = show_1_acs(n, BOTH(ACS_GEQUAL));
2698 n = show_1_acs(n, BOTH(ACS_NEQUAL));
2699 n = show_1_acs(n, BOTH(ACS_LEQUAL));
2701 n = show_1_acs(n, BOTH(ACS_STERLING));
2702 n = show_1_acs(n, BOTH(ACS_PI));
2703 n = show_1_acs(n, BOTH(ACS_S1));
2704 n = show_1_acs(n, BOTH(ACS_S3));
2705 n = show_1_acs(n, BOTH(ACS_S7));
2706 n = show_1_acs(n, BOTH(ACS_S9));
2729 show_upper_chars((unsigned) ((c - '0') * 32 + 128));
2732 mvprintw(LINES - 3, 0,
2733 "Note: ANSI terminals may not display C1 characters.");
2734 mvprintw(LINES - 2, 0,
2735 "Select: a=ACS, b=box, 0=C1, 1,2,3=GR characters, q=quit");
2737 } while ((c = Getchar()) != 'x' && c != 'q');
2744 #if USE_WIDEC_SUPPORT
2746 show_upper_widechars(int first, int repeat, int space)
2750 int last = first + 31;
2754 mvprintw(0, 20, "Display of Character Codes %d to %d", first, last);
2757 for (code = first; code <= last; code++) {
2758 int row = 4 + ((code - first) % 16);
2759 int col = ((code - first) / 16) * COLS / 2;
2761 attr_t attrs = A_NORMAL;
2765 memset(&codes, 0, sizeof(codes));
2767 sprintf(tmp, "%3ld (0x%lx)", (long) code, (long) code);
2768 mvprintw(row, col, "%*s: ", COLS / 4, tmp);
2769 setcchar(&temp, codes, attrs, 0, 0);
2772 * Give non-spacing characters something to combine with. If we
2773 * don't, they'll bunch up in a heap on the space after the ":".
2774 * Mark them with reverse-video to make them simpler to find on
2777 if (wcwidth(code) == 0)
2778 addch(space | A_REVERSE);
2780 * This could use add_wch(), but is done for comparison with the
2781 * normal 'f' test (and to make a test-case for echo_wchar()).
2782 * The screen will flicker because the erase() at the top of the
2783 * function is met by the builtin refresh() in echo_wchar().
2786 } while (--count > 0);
2791 show_1_wacs(int n, const char *name, const cchar_t *code)
2793 const int height = 16;
2794 int row = 4 + (n % height);
2795 int col = (n / height) * COLS / 2;
2796 mvprintw(row, col, "%*s : ", COLS / 4, name);
2797 add_wchnstr(code, 1);
2802 show_wacs_chars(void)
2803 /* display the wide-ACS character set */
2807 /*#define BOTH2(name) #name, &(name) */
2808 #define BOTH2(name) #name, name
2812 mvaddstr(0, 20, "Display of the Wide-ACS Character Set");
2816 n = show_1_wacs(0, BOTH2(WACS_ULCORNER));
2817 n = show_1_wacs(n, BOTH2(WACS_URCORNER));
2818 n = show_1_wacs(n, BOTH2(WACS_LLCORNER));
2819 n = show_1_wacs(n, BOTH2(WACS_LRCORNER));
2821 n = show_1_wacs(n, BOTH2(WACS_LTEE));
2822 n = show_1_wacs(n, BOTH2(WACS_RTEE));
2823 n = show_1_wacs(n, BOTH2(WACS_TTEE));
2824 n = show_1_wacs(n, BOTH2(WACS_BTEE));
2826 n = show_1_wacs(n, BOTH2(WACS_HLINE));
2827 n = show_1_wacs(n, BOTH2(WACS_VLINE));
2829 n = show_1_wacs(n, BOTH2(WACS_LARROW));
2830 n = show_1_wacs(n, BOTH2(WACS_RARROW));
2831 n = show_1_wacs(n, BOTH2(WACS_UARROW));
2832 n = show_1_wacs(n, BOTH2(WACS_DARROW));
2834 n = show_1_wacs(n, BOTH2(WACS_BLOCK));
2835 n = show_1_wacs(n, BOTH2(WACS_BOARD));
2836 n = show_1_wacs(n, BOTH2(WACS_LANTERN));
2837 n = show_1_wacs(n, BOTH2(WACS_BULLET));
2838 n = show_1_wacs(n, BOTH2(WACS_CKBOARD));
2839 n = show_1_wacs(n, BOTH2(WACS_DEGREE));
2840 n = show_1_wacs(n, BOTH2(WACS_DIAMOND));
2841 n = show_1_wacs(n, BOTH2(WACS_PLMINUS));
2842 n = show_1_wacs(n, BOTH2(WACS_PLUS));
2844 #ifdef CURSES_WACS_ARRAY
2845 n = show_1_wacs(n, BOTH2(WACS_GEQUAL));
2846 n = show_1_wacs(n, BOTH2(WACS_NEQUAL));
2847 n = show_1_wacs(n, BOTH2(WACS_LEQUAL));
2849 n = show_1_wacs(n, BOTH2(WACS_STERLING));
2850 n = show_1_wacs(n, BOTH2(WACS_PI));
2851 n = show_1_wacs(n, BOTH2(WACS_S1));
2852 n = show_1_wacs(n, BOTH2(WACS_S3));
2853 n = show_1_wacs(n, BOTH2(WACS_S7));
2854 n = show_1_wacs(n, BOTH2(WACS_S9));
2859 show_wbox_chars(void)
2863 mvaddstr(0, 20, "Display of the Wide-ACS Line-Drawing Set");
2866 box_set(stdscr, 0, 0);
2868 mvhline_set(LINES / 2, 0, WACS_HLINE, COLS);
2869 mvvline_set(0, COLS / 2, WACS_VLINE, LINES);
2870 mvadd_wch(0, COLS / 2, WACS_TTEE);
2871 mvadd_wch(LINES / 2, COLS / 2, WACS_PLUS);
2872 mvadd_wch(LINES - 1, COLS / 2, WACS_BTEE);
2873 mvadd_wch(LINES / 2, 0, WACS_LTEE);
2874 mvadd_wch(LINES / 2, COLS - 1, WACS_RTEE);
2880 show_2_wacs(int n, const char *name, const char *code)
2882 const int height = 16;
2883 int row = 4 + (n % height);
2884 int col = (n / height) * COLS / 2;
2887 mvprintw(row, col, "%*s : ", COLS / 4, name);
2888 addstr(strcpy(temp, code));
2893 show_utf8_chars(void)
2894 /* display the wide-ACS character set */
2900 mvaddstr(0, 20, "Display of the Wide-ACS Character Set");
2904 n = show_2_wacs(0, "WACS_ULCORNER", "\342\224\214");
2905 n = show_2_wacs(n, "WACS_URCORNER", "\342\224\220");
2906 n = show_2_wacs(n, "WACS_LLCORNER", "\342\224\224");
2907 n = show_2_wacs(n, "WACS_LRCORNER", "\342\224\230");
2909 n = show_2_wacs(n, "WACS_LTEE", "\342\224\234");
2910 n = show_2_wacs(n, "WACS_RTEE", "\342\224\244");
2911 n = show_2_wacs(n, "WACS_TTEE", "\342\224\254");
2912 n = show_2_wacs(n, "WACS_BTEE", "\342\224\264");
2914 n = show_2_wacs(n, "WACS_HLINE", "\342\224\200");
2915 n = show_2_wacs(n, "WACS_VLINE", "\342\224\202");
2917 n = show_2_wacs(n, "WACS_LARROW", "\342\206\220");
2918 n = show_2_wacs(n, "WACS_RARROW", "\342\206\222");
2919 n = show_2_wacs(n, "WACS_UARROW", "\342\206\221");
2920 n = show_2_wacs(n, "WACS_DARROW", "\342\206\223");
2922 n = show_2_wacs(n, "WACS_BLOCK", "\342\226\256");
2923 n = show_2_wacs(n, "WACS_BOARD", "\342\226\222");
2924 n = show_2_wacs(n, "WACS_LANTERN", "\342\230\203");
2925 n = show_2_wacs(n, "WACS_BULLET", "\302\267");
2926 n = show_2_wacs(n, "WACS_CKBOARD", "\342\226\222");
2927 n = show_2_wacs(n, "WACS_DEGREE", "\302\260");
2928 n = show_2_wacs(n, "WACS_DIAMOND", "\342\227\206");
2929 n = show_2_wacs(n, "WACS_PLMINUS", "\302\261");
2930 n = show_2_wacs(n, "WACS_PLUS", "\342\224\274");
2931 n = show_2_wacs(n, "WACS_GEQUAL", "\342\211\245");
2932 n = show_2_wacs(n, "WACS_NEQUAL", "\342\211\240");
2933 n = show_2_wacs(n, "WACS_LEQUAL", "\342\211\244");
2935 n = show_2_wacs(n, "WACS_STERLING", "\302\243");
2936 n = show_2_wacs(n, "WACS_PI", "\317\200");
2937 n = show_2_wacs(n, "WACS_S1", "\342\216\272");
2938 n = show_2_wacs(n, "WACS_S3", "\342\216\273");
2939 n = show_2_wacs(n, "WACS_S7", "\342\216\274");
2940 n = show_2_wacs(n, "WACS_S9", "\342\216\275");
2946 wide_acs_display(void)
2968 if (c < 256 && isdigit(c))
2972 else if (c == '-' && digit > 0)
2976 else if (c == '<' && repeat > 0)
2979 space = (space == ' ') ? '_' : ' ';
2984 show_upper_widechars(digit * 32 + 128, repeat, space);
2987 mvprintw(LINES - 2, 0,
2988 "Select: a WACS, b box, u UTF-8, 0-9,+/- non-ASCII, </> repeat, q=quit");
2990 } while ((c = Getchar()) != 'x' && c != 'q');
3000 * Graphic-rendition test (adapted from vttest)
3003 test_sgr_attributes(void)
3007 for (pass = 0; pass < 2; pass++) {
3008 chtype normal = ((pass == 0 ? A_NORMAL : A_REVERSE)) | BLANK;
3010 /* Use non-default colors if possible to exercise bce a little */
3012 init_pair(1, COLOR_WHITE, COLOR_BLUE);
3013 normal |= COLOR_PAIR(1);
3017 mvprintw(1, 20, "Graphic rendition test pattern:");
3019 mvprintw(4, 1, "vanilla");
3021 #define set_sgr(mask) bkgdset((normal^(mask)));
3023 mvprintw(4, 40, "bold");
3025 set_sgr(A_UNDERLINE);
3026 mvprintw(6, 6, "underline");
3028 set_sgr(A_BOLD | A_UNDERLINE);
3029 mvprintw(6, 45, "bold underline");
3032 mvprintw(8, 1, "blink");
3034 set_sgr(A_BLINK | A_BOLD);
3035 mvprintw(8, 40, "bold blink");
3037 set_sgr(A_UNDERLINE | A_BLINK);
3038 mvprintw(10, 6, "underline blink");
3040 set_sgr(A_BOLD | A_UNDERLINE | A_BLINK);
3041 mvprintw(10, 45, "bold underline blink");
3044 mvprintw(12, 1, "negative");
3046 set_sgr(A_BOLD | A_REVERSE);
3047 mvprintw(12, 40, "bold negative");
3049 set_sgr(A_UNDERLINE | A_REVERSE);
3050 mvprintw(14, 6, "underline negative");
3052 set_sgr(A_BOLD | A_UNDERLINE | A_REVERSE);
3053 mvprintw(14, 45, "bold underline negative");
3055 set_sgr(A_BLINK | A_REVERSE);
3056 mvprintw(16, 1, "blink negative");
3058 set_sgr(A_BOLD | A_BLINK | A_REVERSE);
3059 mvprintw(16, 40, "bold blink negative");
3061 set_sgr(A_UNDERLINE | A_BLINK | A_REVERSE);
3062 mvprintw(18, 6, "underline blink negative");
3064 set_sgr(A_BOLD | A_UNDERLINE | A_BLINK | A_REVERSE);
3065 mvprintw(18, 45, "bold underline blink negative");
3068 mvprintw(LINES - 2, 1, "%s background. ", pass == 0 ? "Dark" :
3074 bkgdset(A_NORMAL | BLANK);
3079 /****************************************************************************
3081 * Windows and scrolling tester.
3083 ****************************************************************************/
3085 #define BOTLINES 4 /* number of line stolen from screen bottom */
3091 #define FRAME struct frame
3100 #ifdef NCURSES_VERSION
3101 #define keypad_active(win) (win)->_use_keypad
3102 #define scroll_active(win) (win)->_scroll
3104 #define keypad_active(win) FALSE
3105 #define scroll_active(win) FALSE
3108 /* We need to know if these flags are actually set, so don't look in FRAME.
3109 * These names are known to work with SVr4 curses as well as ncurses. The
3110 * _use_keypad name does not work with Solaris 8.
3113 HaveKeypad(FRAME * curp)
3115 WINDOW *win = (curp ? curp->wind : stdscr);
3116 return keypad_active(win);
3120 HaveScroll(FRAME * curp)
3122 WINDOW *win = (curp ? curp->wind : stdscr);
3123 return scroll_active(win);
3127 newwin_legend(FRAME * curp)
3129 static const struct {
3134 "^C = create window", 0
3137 "^N = next window", 0
3140 "^P = previous window", 0
3143 "^F = scroll forward", 0
3146 "^B = scroll backward", 0
3149 "^K = keypad(%s)", 1
3152 "^S = scrollok(%s)", 2
3155 "^W = save window to file", 0
3158 "^R = restore window", 0
3171 bool do_keypad = HaveKeypad(curp);
3172 bool do_scroll = HaveScroll(curp);
3176 for (n = 0; n < SIZEOF(legend); n++) {
3177 switch (legend[n].code) {
3179 strcpy(buf, legend[n].msg);
3182 sprintf(buf, legend[n].msg, do_keypad ? "yes" : "no");
3185 sprintf(buf, legend[n].msg, do_scroll ? "yes" : "no");
3188 sprintf(buf, legend[n].msg, do_keypad ? "/ESC" : "");
3191 x = getcurx(stdscr);
3192 addstr((COLS < (x + 3 + (int) strlen(buf))) ? "\n" : (n ? ", " : ""));
3199 transient(FRAME * curp, NCURSES_CONST char *msg)
3201 newwin_legend(curp);
3203 mvaddstr(LINES - 1, 0, msg);
3209 printw("%s characters are echoed, window should %sscroll.",
3210 HaveKeypad(curp) ? "Non-arrow" : "All other",
3211 HaveScroll(curp) ? "" : "not ");
3216 newwin_report(FRAME * curp)
3217 /* report on the cursor's current position, then restore it */
3219 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
3223 transient(curp, (char *) 0);
3225 move(LINES - 1, COLS - 17);
3226 printw("Y = %2d X = %2d", y, x);
3234 selectcell(int uli, int ulj, int lri, int lrj)
3235 /* arrows keys move cursor, return location at current on non-arrow key */
3237 static pair res; /* result cell */
3238 int si = lri - uli + 1; /* depth of the select area */
3239 int sj = lrj - ulj + 1; /* width of the select area */
3240 int i = 0, j = 0; /* offsets into the select area */
3245 move(uli + i, ulj + j);
3246 newwin_report((FRAME *) 0);
3248 switch (Getchar()) {
3263 return ((pair *) 0);
3264 #ifdef NCURSES_MOUSE_VERSION
3270 if (event.y > uli && event.x > ulj) {
3291 outerbox(pair ul, pair lr, bool onoff)
3292 /* draw or erase a box *outside* the given pair of corners */
3294 mvaddch(ul.y - 1, lr.x - 1, onoff ? ACS_ULCORNER : ' ');
3295 mvaddch(ul.y - 1, lr.x + 1, onoff ? ACS_URCORNER : ' ');
3296 mvaddch(lr.y + 1, lr.x + 1, onoff ? ACS_LRCORNER : ' ');
3297 mvaddch(lr.y + 1, ul.x - 1, onoff ? ACS_LLCORNER : ' ');
3298 move(ul.y - 1, ul.x);
3299 hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
3300 move(ul.y, ul.x - 1);
3301 vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
3302 move(lr.y + 1, ul.x);
3303 hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
3304 move(ul.y, lr.x + 1);
3305 vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
3310 /* Ask user for a window definition */
3317 addstr("Use arrows to move cursor, anything else to mark corner 1");
3319 if ((tmp = selectcell(2, 1, LINES - BOTLINES - 2, COLS - 2)) == (pair *) 0)
3320 return ((WINDOW *) 0);
3321 memcpy(&ul, tmp, sizeof(pair));
3322 mvaddch(ul.y - 1, ul.x - 1, ACS_ULCORNER);
3325 addstr("Use arrows to move cursor, anything else to mark corner 2");
3327 if ((tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2)) ==
3329 return ((WINDOW *) 0);
3330 memcpy(&lr, tmp, sizeof(pair));
3332 rwindow = subwin(stdscr, lr.y - ul.y + 1, lr.x - ul.x + 1, ul.y, ul.x);
3334 outerbox(ul, lr, TRUE);
3345 newwin_move(FRAME * curp, int dy, int dx)
3347 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
3351 getyx(win, cur_y, cur_x);
3352 getmaxyx(win, max_y, max_x);
3353 if ((cur_x += dx) < 0)
3355 else if (cur_x >= max_x)
3357 if ((cur_y += dy) < 0)
3359 else if (cur_y >= max_y)
3361 wmove(win, cur_y, cur_x);
3365 delete_framed(FRAME * fp, bool showit)
3369 fp->last->next = fp->next;
3370 fp->next->last = fp->last;
3378 np = (fp == fp->next) ? 0 : fp->next;
3384 acs_and_scroll(void)
3385 /* Demonstrate windows */
3389 FRAME *current = (FRAME *) 0, *neww;
3390 WINDOW *usescr = stdscr;
3392 #define DUMPFILE "screendump"
3394 #ifdef NCURSES_MOUSE_VERSION
3395 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
3400 transient((FRAME *) 0, (char *) 0);
3403 neww = (FRAME *) calloc(1, sizeof(FRAME));
3404 if ((neww->wind = getwindow()) == (WINDOW *) 0)
3407 if (current == 0) { /* First element, */
3408 neww->next = neww; /* so point it at itself */
3411 neww->next = current->next;
3412 neww->last = current;
3413 neww->last->next = neww;
3414 neww->next->last = neww;
3417 /* SVr4 curses sets the keypad on all newly-created windows to
3418 * false. Someone reported that PDCurses makes new windows inherit
3419 * this flag. Remove the following 'keypad()' call to test this
3421 keypad(current->wind, TRUE);
3422 current->do_keypad = HaveKeypad(current);
3423 current->do_scroll = HaveScroll(current);
3426 case CTRL('N'): /* go to next window */
3428 current = current->next;
3431 case CTRL('P'): /* go to previous window */
3433 current = current->last;
3436 case CTRL('F'): /* scroll current window forward */
3438 wscrl(current->wind, 1);
3441 case CTRL('B'): /* scroll current window backwards */
3443 wscrl(current->wind, -1);
3446 case CTRL('K'): /* toggle keypad mode for current */
3448 current->do_keypad = !current->do_keypad;
3449 keypad(current->wind, current->do_keypad);
3455 current->do_scroll = !current->do_scroll;
3456 scrollok(current->wind, current->do_scroll);
3460 case CTRL('W'): /* save and delete window */
3461 if (current == current->next) {
3462 transient(current, "Will not save/delete ONLY window");
3464 } else if ((fp = fopen(DUMPFILE, "w")) == (FILE *) 0) {
3465 transient(current, "Can't open screen dump file");
3467 (void) putwin(current->wind, fp);
3470 current = delete_framed(current, TRUE);
3474 case CTRL('R'): /* restore window */
3475 if ((fp = fopen(DUMPFILE, "r")) == (FILE *) 0) {
3476 transient(current, "Can't open screen dump file");
3478 neww = (FRAME *) calloc(1, sizeof(FRAME));
3480 neww->next = current->next;
3481 neww->last = current;
3482 neww->last->next = neww;
3483 neww->next->last = neww;
3485 neww->wind = getwin(fp);
3488 wrefresh(neww->wind);
3493 case CTRL('X'): /* resize window */
3500 addstr("Use arrows to move cursor, anything else to mark new corner");
3503 getbegyx(current->wind, ul.y, ul.x);
3505 tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2);
3506 if (tmp == (pair *) 0) {
3511 getmaxyx(current->wind, lr.y, lr.x);
3514 outerbox(ul, lr, FALSE);
3515 wnoutrefresh(stdscr);
3517 /* strictly cosmetic hack for the test */
3518 getmaxyx(current->wind, my, mx);
3519 if (my > tmp->y - ul.y) {
3520 getyx(current->wind, lr.y, lr.x);
3521 wmove(current->wind, tmp->y - ul.y + 1, 0);
3522 wclrtobot(current->wind);
3523 wmove(current->wind, lr.y, lr.x);
3525 if (mx > tmp->x - ul.x)
3526 for (i = 0; i < my; i++) {
3527 wmove(current->wind, i, tmp->x - ul.x + 1);
3528 wclrtoeol(current->wind);
3530 wnoutrefresh(current->wind);
3532 memcpy(&lr, tmp, sizeof(pair));
3533 (void) wresize(current->wind, lr.y - ul.y + 0, lr.x - ul.x + 0);
3535 getbegyx(current->wind, ul.y, ul.x);
3536 getmaxyx(current->wind, lr.y, lr.x);
3539 outerbox(ul, lr, TRUE);
3540 wnoutrefresh(stdscr);
3542 wnoutrefresh(current->wind);
3548 #endif /* HAVE_WRESIZE */
3550 case KEY_F(10): /* undocumented --- use this to test area clears */
3551 selectcell(0, 0, LINES - 1, COLS - 1);
3557 newwin_move(current, -1, 0);
3560 newwin_move(current, 1, 0);
3563 newwin_move(current, 0, -1);
3566 newwin_move(current, 0, 1);
3574 getyx(current->wind, y, x);
3578 x = getmaxx(current->wind) - 1;
3580 mvwdelch(current->wind, y, x);
3590 waddch(current->wind, (chtype) c);
3595 newwin_report(current);
3596 usescr = (current ? current->wind : stdscr);
3599 ((c = wGetchar(usescr)) != QUIT
3600 && !((c == ESCAPE) && (keypad_active(usescr)))
3604 while (current != 0)
3605 current = delete_framed(current, FALSE);
3607 scrollok(stdscr, TRUE); /* reset to driver's default */
3608 #ifdef NCURSES_MOUSE_VERSION
3609 mousemask(0, (mmask_t *) 0);
3616 /****************************************************************************
3620 ****************************************************************************/
3623 static int nap_msec = 1;
3625 static NCURSES_CONST char *mod[] =
3635 /*+-------------------------------------------------------------------------
3637 --------------------------------------------------------------------------*/
3639 wait_a_while(int msec GCC_UNUSED)
3649 else if (msec > 1000)
3650 sleep((unsigned) msec / 1000);
3654 } /* end of wait_a_while */
3656 /*+-------------------------------------------------------------------------
3658 --------------------------------------------------------------------------*/
3660 saywhat(NCURSES_CONST char *text)
3662 wmove(stdscr, LINES - 1, 0);
3664 waddstr(stdscr, text);
3665 } /* end of saywhat */
3667 /*+-------------------------------------------------------------------------
3668 mkpanel(rows,cols,tly,tlx) - alloc a win and panel and associate them
3669 --------------------------------------------------------------------------*/
3671 mkpanel(unsigned color, int rows, int cols, int tly, int tlx)
3676 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
3677 if ((pan = new_panel(win)) == 0) {
3679 } else if (has_colors()) {
3680 int fg = (color == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
3682 init_pair(color, fg, bg);
3683 wbkgdset(win, COLOR_PAIR(color) | ' ');
3685 wbkgdset(win, A_BOLD | ' ');
3689 } /* end of mkpanel */
3691 /*+-------------------------------------------------------------------------
3693 --------------------------------------------------------------------------*/
3695 rmpanel(PANEL * pan)
3697 WINDOW *win = panel_window(pan);
3700 } /* end of rmpanel */
3702 /*+-------------------------------------------------------------------------
3704 --------------------------------------------------------------------------*/
3710 } /* end of pflush */
3712 /*+-------------------------------------------------------------------------
3714 --------------------------------------------------------------------------*/
3716 fill_panel(PANEL * pan)
3718 WINDOW *win = panel_window(pan);
3719 int num = ((const char *) panel_userptr(pan))[1];
3723 wprintw(win, "-pan%c-", num);
3726 for (y = 2; y < getmaxy(win) - 1; y++) {
3727 for (x = 1; x < getmaxx(win) - 1; x++) {
3729 waddch(win, UChar(num));
3732 } /* end of fill_panel */
3742 for (y = 0; y < LINES - 1; y++) {
3743 for (x = 0; x < COLS; x++)
3744 wprintw(stdscr, "%d", (y + x) % 10);
3746 for (y = 0; y < 5; y++) {
3753 p1 = mkpanel(COLOR_RED,
3758 set_panel_userptr(p1, (NCURSES_CONST void *) "p1");
3760 p2 = mkpanel(COLOR_GREEN,
3765 set_panel_userptr(p2, (NCURSES_CONST void *) "p2");
3767 p3 = mkpanel(COLOR_YELLOW,
3772 set_panel_userptr(p3, (NCURSES_CONST void *) "p3");
3774 p4 = mkpanel(COLOR_BLUE,
3779 set_panel_userptr(p4, (NCURSES_CONST void *) "p4");
3781 p5 = mkpanel(COLOR_MAGENTA,
3786 set_panel_userptr(p5, (NCURSES_CONST void *) "p5");
3796 saywhat("press any key to continue");
3797 wait_a_while(nap_msec);
3799 saywhat("h3 s1 s2 s4 s5; press any key to continue");
3800 move_panel(p1, 0, 0);
3807 wait_a_while(nap_msec);
3809 saywhat("s1; press any key to continue");
3812 wait_a_while(nap_msec);
3814 saywhat("s2; press any key to continue");
3817 wait_a_while(nap_msec);
3819 saywhat("m2; press any key to continue");
3820 move_panel(p2, LINES / 3 + 1, COLS / 8);
3822 wait_a_while(nap_msec);
3827 wait_a_while(nap_msec);
3829 saywhat("m3; press any key to continue");
3830 move_panel(p3, LINES / 4 + 1, COLS / 15);
3832 wait_a_while(nap_msec);
3834 saywhat("b3; press any key to continue");
3837 wait_a_while(nap_msec);
3839 saywhat("s4; press any key to continue");
3842 wait_a_while(nap_msec);
3844 saywhat("s5; press any key to continue");
3847 wait_a_while(nap_msec);
3849 saywhat("t3; press any key to continue");
3852 wait_a_while(nap_msec);
3854 saywhat("t1; press any key to continue");
3857 wait_a_while(nap_msec);
3859 saywhat("t2; press any key to continue");
3862 wait_a_while(nap_msec);
3864 saywhat("t3; press any key to continue");
3867 wait_a_while(nap_msec);
3869 saywhat("t4; press any key to continue");
3872 wait_a_while(nap_msec);
3874 for (itmp = 0; itmp < 6; itmp++) {
3875 WINDOW *w4 = panel_window(p4);
3876 WINDOW *w5 = panel_window(p5);
3878 saywhat("m4; press any key to continue");
3879 wmove(w4, LINES / 8, 1);
3880 waddstr(w4, mod[itmp]);
3881 move_panel(p4, LINES / 6, itmp * (COLS / 8));
3882 wmove(w5, LINES / 6, 1);
3883 waddstr(w5, mod[itmp]);
3885 wait_a_while(nap_msec);
3887 saywhat("m5; press any key to continue");
3888 wmove(w4, LINES / 6, 1);
3889 waddstr(w4, mod[itmp]);
3890 move_panel(p5, LINES / 3 - 1, (itmp * 10) + 6);
3891 wmove(w5, LINES / 8, 1);
3892 waddstr(w5, mod[itmp]);
3894 wait_a_while(nap_msec);
3897 saywhat("m4; press any key to continue");
3898 move_panel(p4, LINES / 6, itmp * (COLS / 8));
3900 wait_a_while(nap_msec);
3902 saywhat("t5; press any key to continue");
3905 wait_a_while(nap_msec);
3907 saywhat("t2; press any key to continue");
3910 wait_a_while(nap_msec);
3912 saywhat("t1; press any key to continue");
3915 wait_a_while(nap_msec);
3917 saywhat("d2; press any key to continue");
3920 wait_a_while(nap_msec);
3922 saywhat("h3; press any key to continue");
3925 wait_a_while(nap_msec);
3927 saywhat("d1; press any key to continue");
3930 wait_a_while(nap_msec);
3932 saywhat("d4; press any key to continue");
3935 wait_a_while(nap_msec);
3937 saywhat("d5; press any key to continue");
3944 wait_a_while(nap_msec);
3954 /****************************************************************************
3958 ****************************************************************************/
3962 static bool pending_pan = FALSE;
3963 static bool show_panner_legend = TRUE;
3966 panner_legend(int line)
3968 static const char *const legend[] =
3970 "Use arrow keys (or U,D,L,R) to pan, q to quit, ! to shell-out.",
3971 "Use +,- (or j,k) to grow/shrink the panner vertically.",
3972 "Use <,> (or h,l) to grow/shrink the panner horizontally.",
3973 "Number repeats. Toggle legend:? filler:a timer:t scrollmark:s."
3975 int n = (SIZEOF(legend) - (LINES - line));
3976 if (line < LINES && (n >= 0)) {
3978 if (show_panner_legend)
3979 printw("%s", legend[n]);
3981 return show_panner_legend;
3987 panner_h_cleanup(int from_y, int from_x, int to_x)
3989 if (!panner_legend(from_y))
3990 do_h_line(from_y, from_x, ' ', to_x);
3994 panner_v_cleanup(int from_y, int from_x, int to_y)
3996 if (!panner_legend(from_y))
3997 do_v_line(from_y, from_x, ' ', to_y);
4001 fill_pad(WINDOW *panpad, bool pan_lines)
4004 unsigned gridcount = 0;
4006 wmove(panpad, 0, 0);
4007 for (y = 0; y < getmaxy(panpad); y++) {
4008 for (x = 0; x < getmaxx(panpad); x++) {
4009 if (y % GRIDSIZE == 0 && x % GRIDSIZE == 0) {
4010 if (y == 0 && x == 0)
4011 waddch(panpad, pan_lines ? ACS_ULCORNER : '+');
4013 waddch(panpad, pan_lines ? ACS_TTEE : '+');
4014 else if (y == 0 || x == 0)
4015 waddch(panpad, pan_lines ? ACS_LTEE : '+');
4017 waddch(panpad, (chtype) ((pan_lines ? 'a' : 'A') +
4018 (gridcount++ % 26)));
4019 } else if (y % GRIDSIZE == 0)
4020 waddch(panpad, pan_lines ? ACS_HLINE : '-');
4021 else if (x % GRIDSIZE == 0)
4022 waddch(panpad, pan_lines ? ACS_VLINE : '|');
4024 waddch(panpad, ' ');
4031 int top_x, int top_y, int porty, int portx,
4032 int (*pgetc) (WINDOW *))
4034 #if HAVE_GETTIMEOFDAY
4035 struct timeval before, after;
4038 bool pan_lines = FALSE;
4039 bool scrollers = TRUE;
4042 int pxmax, pymax, lowend, highend, c;
4044 getmaxyx(pad, pymax, pxmax);
4045 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
4049 #ifdef NCURSES_VERSION
4051 * During shell-out, the user may have resized the window. Adjust
4052 * the port size of the pad to accommodate this. Ncurses automatically
4053 * resizes all of the normal windows to fit on the new screen.
4071 show_panner_legend = !show_panner_legend;
4072 panner_legend(LINES - 4);
4073 panner_legend(LINES - 3);
4074 panner_legend(LINES - 2);
4075 panner_legend(LINES - 1);
4078 pan_lines = !pan_lines;
4079 fill_pad(pad, pan_lines);
4080 pending_pan = FALSE;
4083 #if HAVE_GETTIMEOFDAY
4087 panner_legend(LINES - 1);
4091 scrollers = !scrollers;
4094 /* Move the top-left corner of the pad, keeping the bottom-right
4097 case 'h': /* increase-columns: move left edge to left */
4101 panner_v_cleanup(top_y, top_x, porty);
4106 case 'j': /* decrease-lines: move top-edge down */
4110 panner_h_cleanup(top_y - 1, top_x - (top_x > 0), portx);
4115 case 'k': /* increase-lines: move top-edge up */
4120 panner_h_cleanup(top_y, top_x, portx);
4124 case 'l': /* decrease-columns: move left-edge to right */
4128 panner_v_cleanup(top_y - (top_y > 0), top_x - 1, porty);
4133 /* Move the bottom-right corner of the pad, keeping the top-left
4136 case KEY_IC: /* increase-columns: move right-edge to right */
4137 if (portx >= pxmax || portx >= COLS)
4140 panner_v_cleanup(top_y - (top_y > 0), portx - 1, porty);
4145 case KEY_IL: /* increase-lines: move bottom-edge down */
4146 if (porty >= pymax || porty >= LINES)
4149 panner_h_cleanup(porty - 1, top_x - (top_x > 0), portx);
4154 case KEY_DC: /* decrease-columns: move bottom edge up */
4159 panner_v_cleanup(top_y - (top_y > 0), portx, porty);
4163 case KEY_DL: /* decrease-lines */
4168 panner_h_cleanup(porty, top_x - (top_x > 0), portx);
4172 case KEY_LEFT: /* pan leftwards */
4179 case KEY_RIGHT: /* pan rightwards */
4180 if (basex + portx - (pymax > porty) < pxmax)
4186 case KEY_UP: /* pan upwards */
4193 case KEY_DOWN: /* pan downwards */
4194 if (basey + porty - (pxmax > portx) < pymax)
4209 basey = pymax - porty;
4219 mvaddch(top_y - 1, top_x - 1, ACS_ULCORNER);
4220 do_v_line(top_y, top_x - 1, ACS_VLINE, porty);
4221 do_h_line(top_y - 1, top_x, ACS_HLINE, portx);
4223 if (scrollers && (pxmax > portx - 1)) {
4224 int length = (portx - top_x - 1);
4225 float ratio = ((float) length) / ((float) pxmax);
4227 lowend = (int) (top_x + (basex * ratio));
4228 highend = (int) (top_x + ((basex + length) * ratio));
4230 do_h_line(porty - 1, top_x, ACS_HLINE, lowend);
4231 if (highend < portx) {
4233 do_h_line(porty - 1, lowend, ' ', highend + 1);
4235 do_h_line(porty - 1, highend + 1, ACS_HLINE, portx);
4238 do_h_line(porty - 1, top_x, ACS_HLINE, portx);
4240 if (scrollers && (pymax > porty - 1)) {
4241 int length = (porty - top_y - 1);
4242 float ratio = ((float) length) / ((float) pymax);
4244 lowend = (int) (top_y + (basey * ratio));
4245 highend = (int) (top_y + ((basey + length) * ratio));
4247 do_v_line(top_y, portx - 1, ACS_VLINE, lowend);
4248 if (highend < porty) {
4250 do_v_line(lowend, portx - 1, ' ', highend + 1);
4252 do_v_line(highend + 1, portx - 1, ACS_VLINE, porty);
4255 do_v_line(top_y, portx - 1, ACS_VLINE, porty);
4257 mvaddch(top_y - 1, portx - 1, ACS_URCORNER);
4258 mvaddch(porty - 1, top_x - 1, ACS_LLCORNER);
4259 mvaddch(porty - 1, portx - 1, ACS_LRCORNER);
4262 #if HAVE_GETTIMEOFDAY
4263 gettimeofday(&before, 0);
4265 wnoutrefresh(stdscr);
4270 porty - (pxmax > portx) - 1,
4271 portx - (pymax > porty) - 1);
4274 #if HAVE_GETTIMEOFDAY
4277 gettimeofday(&after, 0);
4278 elapsed = (after.tv_sec + after.tv_usec / 1.0e6)
4279 - (before.tv_sec + before.tv_usec / 1.0e6);
4280 move(LINES - 1, COLS - 12);
4281 printw("Secs: %2.03f", elapsed);
4288 ((c = pgetc(pad)) != KEY_EXIT);
4290 scrollok(stdscr, TRUE); /* reset to driver's default */
4294 padgetch(WINDOW *win)
4300 if ((pending_pan = (count > 0)) != FALSE) {
4302 pending_pan = (count != 0);
4305 switch (c = wGetchar(win)) {
4341 case ERR: /* FALLTHRU */
4347 if (c >= '0' && c <= '9') {
4348 count = count * 10 + (c - '0');
4362 #define PAD_HIGH 200
4363 #define PAD_WIDE 200
4367 /* Demonstrate pads. */
4369 WINDOW *panpad = newpad(PAD_HIGH, PAD_WIDE);
4372 Cannot("cannot create requested pad");
4376 fill_pad(panpad, FALSE);
4378 panner_legend(LINES - 4);
4379 panner_legend(LINES - 3);
4380 panner_legend(LINES - 2);
4381 panner_legend(LINES - 1);
4383 keypad(panpad, TRUE);
4385 /* Make the pad (initially) narrow enough that a trace file won't wrap.
4386 * We'll still be able to widen it during a test, since that's required
4387 * for testing boundaries.
4389 panner(panpad, 2, 2, LINES - 5, COLS - 15, padgetch);
4395 #endif /* USE_LIBPANEL */
4397 /****************************************************************************
4399 * Tests from John Burnell's PDCurses tester
4401 ****************************************************************************/
4404 Continue(WINDOW *win)
4408 mvwaddstr(win, 10, 1, " Press any key to continue");
4414 flushinp_test(WINDOW *win)
4415 /* Input test, adapted from John Burnell's PDCurses tester */
4417 int w, h, bx, by, sw, sh, i;
4422 getmaxyx(win, h, w);
4423 getbegyx(win, by, bx);
4426 if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == 0)
4431 init_pair(2, COLOR_CYAN, COLOR_BLUE);
4432 wbkgd(subWin, COLOR_PAIR(2) | ' ');
4435 wattrset(subWin, A_BOLD);
4436 box(subWin, ACS_VLINE, ACS_HLINE);
4437 mvwaddstr(subWin, 2, 1, "This is a subwindow");
4441 * This used to set 'nocbreak()'. However, Alexander Lukyanov says that
4442 * it only happened to "work" on SVr4 because that implementation does not
4443 * emulate nocbreak+noecho mode, whereas ncurses does. To get the desired
4444 * test behavior, we're using 'cbreak()', which will allow a single
4445 * character to return without needing a newline. - T.Dickey 1997/10/11.
4448 mvwaddstr(win, 0, 1, "This is a test of the flushinp() call.");
4450 mvwaddstr(win, 2, 1, "Type random keys for 5 seconds.");
4451 mvwaddstr(win, 3, 1,
4452 "These should be discarded (not echoed) after the subwindow goes away.");
4455 for (i = 0; i < 5; i++) {
4456 mvwprintw(subWin, 1, 1, "Time = %d", i);
4468 mvwaddstr(win, 2, 1,
4469 "If you were still typing when the window timer expired,");
4470 mvwaddstr(win, 3, 1,
4471 "or else you typed nothing at all while it was running,");
4472 mvwaddstr(win, 4, 1,
4473 "test was invalid. You'll see garbage or nothing at all. ");
4474 mvwaddstr(win, 6, 1, "Press a key");
4480 mvwaddstr(win, 12, 0,
4481 "If you see any key other than what you typed, flushinp() is broken.");
4490 "What you typed should now have been deleted; if not, wdelch() failed.");
4496 /****************************************************************************
4500 ****************************************************************************/
4508 menu_virtualize(int c)
4510 if (c == '\n' || c == KEY_EXIT)
4511 return (MAX_COMMAND + 1);
4513 return (REQ_SCR_ULINE);
4515 return (REQ_SCR_DLINE);
4516 else if (c == 'b' || c == KEY_NPAGE)
4517 return (REQ_SCR_UPAGE);
4518 else if (c == 'f' || c == KEY_PPAGE)
4519 return (REQ_SCR_DPAGE);
4520 else if (c == 'n' || c == KEY_DOWN)
4521 return (REQ_NEXT_ITEM);
4522 else if (c == 'p' || c == KEY_UP)
4523 return (REQ_PREV_ITEM);
4525 return (REQ_TOGGLE_ITEM);
4533 static const char *animals[] =
4546 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
4547 "Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs, Lions, Tigers, Bears, (Oh my!), Newts, Platypi, Lemurs",
4555 ITEM *items[SIZEOF(animals)];
4558 int mrows, mcols, c;
4561 #ifdef NCURSES_MOUSE_VERSION
4562 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
4564 mvaddstr(0, 0, "This is the menu test:");
4565 mvaddstr(2, 0, " Use up and down arrow to move the select bar.");
4566 mvaddstr(3, 0, " 'n' and 'p' act like arrows.");
4568 " 'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
4569 mvaddstr(5, 0, " Press return to exit.");
4572 for (ap = animals; *ap; ap++)
4573 *ip++ = new_item(*ap, "");
4576 m = new_menu(items);
4578 set_menu_format(m, (SIZEOF(animals) + 1) / 2, 1);
4579 scale_menu(m, &mrows, &mcols);
4581 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
4582 set_menu_win(m, menuwin);
4583 keypad(menuwin, TRUE);
4586 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
4590 while ((c = menu_driver(m, menu_virtualize(wGetchar(menuwin)))) != E_UNKNOWN_COMMAND) {
4591 if (c == E_NOT_POSTED)
4593 if (c == E_REQUEST_DENIED)
4598 (void) mvprintw(LINES - 2, 0,
4599 "You chose: %s\n", item_name(current_item(m)));
4600 (void) addstr("Press any key to continue...");
4607 for (ip = items; *ip; ip++)
4609 #ifdef NCURSES_MOUSE_VERSION
4610 mousemask(0, (mmask_t *) 0);
4615 #define T_TBL(name) { #name, name }
4621 T_TBL(TRACE_DISABLE),
4624 T_TBL(TRACE_UPDATE),
4626 T_TBL(TRACE_CHARPUT),
4627 T_TBL(TRACE_ORDINARY),
4629 T_TBL(TRACE_VIRTPUT),
4630 T_TBL(TRACE_IEVENT),
4632 T_TBL(TRACE_ICALLS),
4633 T_TBL(TRACE_CCALLS),
4634 T_TBL(TRACE_DATABASE),
4636 T_TBL(TRACE_MAXIMUM),
4643 tracetrace(unsigned tlevel)
4650 for (n = 0; t_tbl[n].name != 0; n++)
4651 need += strlen(t_tbl[n].name) + 2;
4652 buf = (char *) malloc(need);
4654 sprintf(buf, "0x%02x = {", tlevel);
4656 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
4658 for (n = 1; t_tbl[n].name != 0; n++)
4659 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
4660 strcat(buf, t_tbl[n].name);
4664 if (buf[strlen(buf) - 2] == ',')
4665 buf[strlen(buf) - 2] = '\0';
4666 return (strcat(buf, "}"));
4669 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
4673 run_trace_menu(MENU * m)
4679 bool changed = FALSE;
4680 switch (menu_driver(m, menu_virtualize(wGetchar(menu_win(m))))) {
4681 case E_UNKNOWN_COMMAND:
4684 items = menu_items(m);
4685 i = current_item(m);
4686 if (i == items[0]) {
4687 if (item_value(i)) {
4688 for (p = items + 1; *p != 0; p++)
4689 if (item_value(*p)) {
4690 set_item_value(*p, FALSE);
4695 for (p = items + 1; *p != 0; p++)
4696 if (item_value(*p)) {
4697 set_item_value(items[0], FALSE);
4710 /* interactively set the trace level */
4713 ITEM *items[SIZEOF(t_tbl)];
4720 mvaddstr(0, 0, "Interactively set trace level:");
4721 mvaddstr(2, 0, " Press space bar to toggle a selection.");
4722 mvaddstr(3, 0, " Use up and down arrow to move the select bar.");
4723 mvaddstr(4, 0, " Press return to set the trace level.");
4724 mvprintw(6, 0, "(Current trace level is %s)", tracetrace(_nc_tracing));
4728 for (n = 0; t_tbl[n].name != 0; n++)
4729 *ip++ = new_item(t_tbl[n].name, "");
4732 m = new_menu(items);
4734 set_menu_format(m, 0, 2);
4735 scale_menu(m, &mrows, &mcols);
4737 menu_opts_off(m, O_ONEVALUE);
4738 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
4739 set_menu_win(m, menuwin);
4740 keypad(menuwin, TRUE);
4743 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
4747 for (ip = menu_items(m); *ip; ip++) {
4748 unsigned mask = t_tbl[item_index(*ip)].mask;
4750 set_item_value(*ip, _nc_tracing == 0);
4751 else if ((mask & _nc_tracing) == mask)
4752 set_item_value(*ip, TRUE);
4755 while (run_trace_menu(m))
4759 for (ip = menu_items(m); *ip; ip++)
4760 if (item_value(*ip))
4761 newtrace |= t_tbl[item_index(*ip)].mask;
4763 _tracef("trace level interactively set to %s", tracetrace(_nc_tracing));
4765 (void) mvprintw(LINES - 2, 0,
4766 "Trace level is %s\n", tracetrace(_nc_tracing));
4767 (void) addstr("Press any key to continue...");
4774 for (ip = items; *ip; ip++)
4778 #endif /* USE_LIBMENU */
4780 /****************************************************************************
4784 ****************************************************************************/
4787 make_label(int frow, int fcol, NCURSES_CONST char *label)
4789 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
4792 set_field_buffer(f, 0, label);
4793 set_field_opts(f, (int) (field_opts(f) & ~O_ACTIVE));
4799 make_field(int frow, int fcol, int rows, int cols, bool secure)
4801 FIELD *f = new_field(rows, cols, frow, fcol, 0, secure ? 1 : 0);
4804 set_field_back(f, A_UNDERLINE);
4805 set_field_userptr(f, (void *) 0);
4811 display_form(FORM * f)
4816 scale_form(f, &rows, &cols);
4818 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
4820 set_form_sub(f, derwin(w, rows, cols, 1, 2));
4825 if (post_form(f) != E_OK)
4830 erase_form(FORM * f)
4832 WINDOW *w = form_win(f);
4833 WINDOW *s = form_sub(f);
4843 edit_secure(FIELD * me, int c)
4845 int rows, cols, frow, fcol, nrow, nbuf;
4847 if (field_info(me, &rows, &cols, &frow, &fcol, &nrow, &nbuf) == E_OK
4852 strcpy(temp, field_buffer(me, 1));
4853 len = (long) (char *) field_userptr(me);
4855 if (isgraph(c) && (len + 1) < (int) sizeof(temp)) {
4858 set_field_buffer(me, 1, temp);
4878 case REQ_RIGHT_CHAR:
4880 c = 0; /* we don't want to do inline editing */
4885 set_field_buffer(me, 1, temp);
4892 set_field_buffer(me, 1, temp);
4897 set_field_userptr(me, (void *) len);
4903 form_virtualize(FORM * f, WINDOW *w)
4905 static const struct {
4910 CTRL('A'), REQ_NEXT_CHOICE
4913 CTRL('B'), REQ_PREV_WORD
4916 CTRL('C'), REQ_CLR_EOL
4919 CTRL('D'), REQ_DOWN_FIELD
4922 CTRL('E'), REQ_END_FIELD
4925 CTRL('F'), REQ_NEXT_PAGE
4928 CTRL('G'), REQ_DEL_WORD
4931 CTRL('H'), REQ_DEL_PREV
4934 CTRL('I'), REQ_INS_CHAR
4937 CTRL('K'), REQ_CLR_EOF
4940 CTRL('L'), REQ_LEFT_FIELD
4943 CTRL('M'), REQ_NEW_LINE
4946 CTRL('N'), REQ_NEXT_FIELD
4949 CTRL('O'), REQ_INS_LINE
4952 CTRL('P'), REQ_PREV_FIELD
4955 CTRL('R'), REQ_RIGHT_FIELD
4958 CTRL('S'), REQ_BEG_FIELD
4961 CTRL('U'), REQ_UP_FIELD
4964 CTRL('V'), REQ_DEL_CHAR
4967 CTRL('W'), REQ_NEXT_WORD
4970 CTRL('X'), REQ_CLR_FIELD
4973 CTRL('Y'), REQ_DEL_LINE
4976 CTRL('Z'), REQ_PREV_CHOICE
4979 ESCAPE, MAX_FORM_COMMAND + 1
4982 KEY_BACKSPACE, REQ_DEL_PREV
4985 KEY_DOWN, REQ_DOWN_CHAR
4988 KEY_END, REQ_LAST_FIELD
4991 KEY_HOME, REQ_FIRST_FIELD
4994 KEY_LEFT, REQ_LEFT_CHAR
4997 KEY_LL, REQ_LAST_FIELD
5000 KEY_NEXT, REQ_NEXT_FIELD
5003 KEY_NPAGE, REQ_NEXT_PAGE
5006 KEY_PPAGE, REQ_PREV_PAGE
5009 KEY_PREVIOUS, REQ_PREV_FIELD
5012 KEY_RIGHT, REQ_RIGHT_CHAR
5018 QUIT, MAX_FORM_COMMAND + 1
5022 static int mode = REQ_INS_MODE;
5023 int c = wGetchar(w);
5025 FIELD *me = current_field(f);
5026 bool current = TRUE;
5028 if (c == CTRL(']')) {
5029 if (mode == REQ_INS_MODE) {
5030 mode = REQ_OVL_MODE;
5032 mode = REQ_INS_MODE;
5036 for (n = 0; n < SIZEOF(lookup); n++) {
5037 if (lookup[n].code == c) {
5038 c = lookup[n].result;
5043 mvprintw(0, COLS - 6, "(%s)", mode == REQ_INS_MODE ? "INS" : "OVL");
5046 * Force the field that the user is typing into to be in reverse video,
5047 * while the other fields are shown underlined.
5062 case REQ_LEFT_FIELD:
5064 case REQ_RIGHT_CHAR:
5068 current = (c < KEY_MAX);
5072 c = edit_secure(me, c);
5073 set_field_back(me, A_REVERSE);
5075 c = edit_secure(me, c);
5076 set_field_back(me, A_UNDERLINE);
5082 my_form_driver(FORM * form, int c)
5084 if (c == (MAX_FORM_COMMAND + 1)
5085 && form_driver(form, REQ_VALIDATION) == E_OK)
5094 * Allow a middle initial, optionally with a '.' to end it.
5097 mi_field_check(FIELD * fld, const void *data GCC_UNUSED)
5099 char *s = field_buffer(fld, 0);
5103 for (n = 0; s[n] != '\0'; ++n) {
5110 } else if (isspace(UChar(s[n]))) {
5115 if (!isspace(UChar(s[n])))
5121 /* force the form to display a leading capital */
5122 if (islower(UChar(s[0]))) {
5123 s[0] = toupper(UChar(s[0]));
5124 set_field_buffer(fld, 0, s);
5130 mi_char_check(int ch, const void *data GCC_UNUSED)
5132 return ((isalpha(ch) || ch == '.') ? TRUE : FALSE);
5136 * Passwords should be at least 6 characters.
5139 pw_field_check(FIELD * fld, const void *data GCC_UNUSED)
5141 char *s = field_buffer(fld, 0);
5144 for (n = 0; s[n] != '\0'; ++n) {
5145 if (isspace(UChar(s[n]))) {
5154 pw_char_check(int ch, const void *data GCC_UNUSED)
5156 return (isgraph(ch) ? TRUE : FALSE);
5164 FIELD *f[12], *secure;
5165 FIELDTYPE *fty_middle = new_fieldtype(mi_field_check, mi_char_check);
5166 FIELDTYPE *fty_passwd = new_fieldtype(pw_field_check, pw_char_check);
5167 int finished = 0, c;
5171 addstr("Defined edit/traversal keys: ^Q/ESC- exit form\n");
5172 addstr("^N -- go to next field ^P -- go to previous field\n");
5173 addstr("Home -- go to first field End -- go to last field\n");
5174 addstr("^L -- go to field to left ^R -- go to field to right\n");
5175 addstr("^U -- move upward to field ^D -- move downward to field\n");
5176 addstr("^W -- go to next word ^B -- go to previous word\n");
5177 addstr("^S -- go to start of field ^E -- go to end of field\n");
5178 addstr("^H -- delete previous char ^Y -- delete line\n");
5179 addstr("^G -- delete current word ^C -- clear to end of line\n");
5180 addstr("^K -- clear to end of field ^X -- clear field\n");
5181 addstr("Arrow keys move within a field as you would expect. ^] toggles overlay mode.");
5183 mvaddstr(4, 57, "Forms Entry Test");
5187 /* describe the form */
5188 f[n++] = make_label(0, 15, "Sample Form");
5190 f[n++] = make_label(2, 0, "Last Name");
5191 f[n++] = make_field(3, 0, 1, 18, FALSE);
5192 set_field_type(f[n - 1], TYPE_ALPHA, 1);
5194 f[n++] = make_label(2, 20, "First Name");
5195 f[n++] = make_field(3, 20, 1, 12, FALSE);
5196 set_field_type(f[n - 1], TYPE_ALPHA, 1);
5198 f[n++] = make_label(2, 34, "Middle Name");
5199 f[n++] = make_field(3, 34, 1, 12, FALSE);
5200 set_field_type(f[n - 1], fty_middle);
5202 f[n++] = make_label(5, 0, "Comments");
5203 f[n++] = make_field(6, 0, 4, 46, FALSE);
5205 f[n++] = make_label(5, 20, "Password:");
5207 f[n++] = make_field(5, 30, 1, 9, TRUE);
5208 set_field_type(f[n - 1], fty_passwd);
5209 f[n++] = (FIELD *) 0;
5217 nonl(); /* lets us read ^M's */
5219 switch (form_driver(form, c = form_virtualize(form, w))) {
5221 mvaddstr(5, 57, field_buffer(secure, 1));
5225 case E_UNKNOWN_COMMAND:
5226 finished = my_form_driver(form, c);
5237 for (c = 0; f[c] != 0; c++)
5239 free_fieldtype(fty_middle);
5240 free_fieldtype(fty_passwd);
5244 #endif /* USE_LIBFORM */
5246 /****************************************************************************
5250 ****************************************************************************/
5253 fillwin(WINDOW *win, char ch)
5258 getmaxyx(win, y1, x1);
5259 for (y = 0; y < y1; y++) {
5261 for (x = 0; x < x1; x++)
5262 waddch(win, UChar(ch));
5267 crosswin(WINDOW *win, char ch)
5272 getmaxyx(win, y1, x1);
5273 for (y = 0; y < y1; y++) {
5274 for (x = 0; x < x1; x++)
5275 if (((x > (x1 - 1) / 3) && (x <= (2 * (x1 - 1)) / 3))
5276 || (((y > (y1 - 1) / 3) && (y <= (2 * (y1 - 1)) / 3)))) {
5278 waddch(win, UChar(ch));
5285 /* test effects of overlapping windows */
5289 WINDOW *win1 = newwin(9, 20, 3, 3);
5290 WINDOW *win2 = newwin(9, 20, 9, 16);
5295 printw("This test shows the behavior of wnoutrefresh() with respect to\n");
5296 printw("the shared region of two overlapping windows A and B. The cross\n");
5297 printw("pattern in each window does not overlap the other.\n");
5300 printw("a = refresh A, then B, then doupdate. b = refresh B, then A, then doupdaute\n");
5301 printw("c = fill window A with letter A. d = fill window B with letter B.\n");
5302 printw("e = cross pattern in window A. f = cross pattern in window B.\n");
5303 printw("g = clear window A. h = clear window B.\n");
5304 printw("i = overwrite A onto B. j = overwrite B onto A.\n");
5305 printw("^Q/ESC = terminate test.");
5307 while ((ch = Getchar()) != QUIT && ch != ESCAPE)
5309 case 'a': /* refresh window A first, then B */
5315 case 'b': /* refresh window B first, then A */
5321 case 'c': /* fill window A so it's visible */
5325 case 'd': /* fill window B so it's visible */
5329 case 'e': /* cross test pattern in window A */
5330 crosswin(win1, 'A');
5333 case 'f': /* cross test pattern in window A */
5334 crosswin(win2, 'B');
5337 case 'g': /* clear window A */
5342 case 'h': /* clear window B */
5347 case 'i': /* overwrite A onto B */
5348 overwrite(win1, win2);
5351 case 'j': /* overwrite B onto A */
5352 overwrite(win2, win1);
5362 /****************************************************************************
5366 ****************************************************************************/
5369 do_single_test(const char c)
5370 /* perform a single specified test */
5377 #if USE_WIDEC_SUPPORT
5387 #if USE_WIDEC_SUPPORT
5395 Cannot("does not support color.");
5400 #if USE_WIDEC_SUPPORT
5403 Cannot("does not support color.");
5411 Cannot("does not support color.");
5412 else if (!can_change_color())
5413 Cannot("has hardwired color values.");
5422 #if USE_WIDEC_SUPPORT
5431 #if USE_WIDEC_SUPPORT
5448 flushinp_test(stdscr);
5452 test_sgr_attributes();
5477 #if USE_LIBMENU && defined(TRACE)
5496 static const char *const tbl[] =
5498 "Usage: ncurses [options]"
5501 #ifdef NCURSES_VERSION
5502 ," -a f,b set default-colors (assumed white-on-black)"
5503 ," -d use default-colors if terminal supports them"
5505 ," -e fmt specify format for soft-keys test (e)"
5506 ," -f rip-off footer line (can repeat)"
5507 ," -h rip-off header line (can repeat)"
5508 ," -p file rgb values to use in 'd' rather than ncurses's builtin"
5510 ," -s msec specify nominal time for panel-demo (default: 1, to hold)"
5513 ," -t mask specify default trace-level (may toggle with ^T)"
5517 for (n = 0; n < SIZEOF(tbl); n++)
5518 fprintf(stderr, "%s\n", tbl[n]);
5519 ExitProgram(EXIT_FAILURE);
5523 set_terminal_modes(void)
5528 scrollok(stdscr, TRUE);
5529 idlok(stdscr, TRUE);
5530 keypad(stdscr, TRUE);
5535 announce_sig(int sig)
5537 (void) fprintf(stderr, "Handled signal %d\r\n", sig);
5542 rip_footer(WINDOW *win, int cols)
5544 wbkgd(win, A_REVERSE);
5547 wprintw(win, "footer: %d columns", cols);
5553 rip_header(WINDOW *win, int cols)
5555 wbkgd(win, A_REVERSE);
5558 wprintw(win, "header: %d columns", cols);
5569 (void) puts("This is the ncurses main menu");
5570 (void) puts("a = keyboard and mouse input test");
5571 #if USE_WIDEC_SUPPORT
5572 (void) puts("A = wide-character keyboard and mouse input test");
5574 (void) puts("b = character attribute test");
5575 #if USE_WIDEC_SUPPORT
5576 (void) puts("B = wide-character attribute test");
5578 (void) puts("c = color test pattern");
5579 #if USE_WIDEC_SUPPORT
5580 (void) puts("C = color test pattern using wide-character calls");
5583 (void) puts("d = edit RGB color values");
5584 (void) puts("e = exercise soft keys");
5585 #if USE_WIDEC_SUPPORT
5586 (void) puts("E = exercise soft keys using wide-characters");
5588 (void) puts("f = display ACS characters");
5589 #if USE_WIDEC_SUPPORT
5590 (void) puts("F = display Wide-ACS characters");
5592 (void) puts("g = display windows and scrolling");
5593 (void) puts("i = test of flushinp()");
5594 (void) puts("k = display character attributes");
5596 (void) puts("m = menu code test");
5599 (void) puts("o = exercise panels library");
5600 (void) puts("p = exercise pad features");
5601 (void) puts("q = quit");
5604 (void) puts("r = exercise forms code");
5606 (void) puts("s = overlapping-refresh test");
5607 #if USE_LIBMENU && defined(TRACE)
5608 (void) puts("t = set trace level");
5610 (void) puts("? = repeat this command summary");
5612 (void) fputs("> ", stdout);
5613 (void) fflush(stdout); /* necessary under SVr4 curses */
5616 * This used to be an 'fgets()' call. However (on Linux, at least)
5617 * mixing stream I/O and 'read()' (used in the library) causes the
5618 * input stream to be flushed when switching between the two.
5623 if (read(fileno(stdin), &ch, 1) <= 0) {
5627 } else if (command == 0 && !isspace(UChar(ch))) {
5629 } else if (ch == '\n' || ch == '\r') {
5630 if ((command == 'd') && !top) {
5631 (void) fputs("Do not nest test-d\n", stdout);
5636 (void) fputs("> ", stdout);
5637 (void) fflush(stdout);
5641 if (do_single_test(command)) {
5643 * This may be overkill; it's intended to reset everything back
5644 * to the initial terminal modes so that tests don't get in
5648 set_terminal_modes();
5653 if (command == '?') {
5654 (void) puts("This is the ncurses capability tester.");
5656 puts("You may select a test from the main menu by typing the");
5658 puts("key letter of the choice (the letter to left of the =)");
5660 puts("at the > prompt. The commands `x' or `q' will exit.");
5668 /*+-------------------------------------------------------------------------
5670 --------------------------------------------------------------------------*/
5672 #define okCOLOR(n) ((n) >= 0 && (n) < max_colors)
5673 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
5676 main(int argc, char *argv[])
5680 #ifdef NCURSES_VERSION
5681 int default_fg = COLOR_WHITE;
5682 int default_bg = COLOR_BLACK;
5683 bool assumed_colors = FALSE;
5684 bool default_colors = FALSE;
5686 char *palette_file = 0;
5688 setlocale(LC_ALL, "");
5690 while ((c = getopt(argc, argv, "a:de:fhp:s:t:")) != EOF) {
5692 #ifdef NCURSES_VERSION
5694 assumed_colors = TRUE;
5695 sscanf(optarg, "%d,%d", &default_fg, &default_bg);
5698 default_colors = TRUE;
5702 my_e_param = atoi(optarg);
5703 #ifdef NCURSES_VERSION
5704 if (my_e_param > 3) /* allow extended layouts */
5712 ripoffline(-1, rip_footer);
5715 ripoffline(1, rip_header);
5718 palette_file = optarg;
5722 nap_msec = atol(optarg);
5727 save_trace = strtol(optarg, 0, 0);
5736 * If there's no menus (unlikely for ncurses!), then we'll have to set
5737 * tracing on initially, just in case the user wants to test something that
5738 * doesn't involve wGetchar.
5741 /* enable debugging */
5745 if (!isatty(fileno(stdin)))
5747 #endif /* USE_LIBMENU */
5750 /* tell it we're going to play with soft keys */
5751 slk_init(my_e_param);
5754 /* set up null signal catcher so we can see what interrupts to getch do */
5755 signal(SIGUSR1, announce_sig);
5758 /* we must initialize the curses data structure only once */
5762 /* tests, in general, will want these modes */
5765 #ifdef NCURSES_VERSION_PATCH
5766 max_colors = COLORS; /* was > 16 ? 16 : COLORS */
5767 #if HAVE_USE_DEFAULT_COLORS
5768 if (default_colors) {
5769 use_default_colors();
5772 #if NCURSES_VERSION_PATCH >= 20000708
5773 else if (assumed_colors)
5774 assume_default_colors(default_fg, default_bg);
5777 #else /* normal SVr4 curses */
5778 max_colors = COLORS; /* was > 8 ? 8 : COLORS */
5780 max_pairs = COLOR_PAIRS; /* was > 256 ? 256 : COLOR_PAIRS */
5782 if (can_change_color()) {
5783 all_colors = (RGB_DATA *) malloc(max_colors * sizeof(RGB_DATA));
5784 for (c = 0; c < max_colors; ++c) {
5787 &all_colors[c].green,
5788 &all_colors[c].blue);
5790 if (palette_file != 0) {
5791 FILE *fp = fopen(palette_file, "r");
5793 char buffer[BUFSIZ];
5794 int red, green, blue;
5796 while (fgets(buffer, sizeof(buffer), fp) != 0) {
5797 if (sscanf(buffer, "scale:%d", &c) == 1) {
5799 } else if (sscanf(buffer, "%d:%d %d %d",
5808 all_colors[c].red = (red * 1000) / scale;
5809 all_colors[c].green = (green * 1000) / scale;
5810 all_colors[c].blue = (blue * 1000) / scale;
5818 set_terminal_modes();
5822 * Return to terminal mode, so we're guaranteed of being able to
5823 * select terminal commands even if the capabilities are wrong.
5827 #if HAVE_CURSES_VERSION
5828 (void) printf("Welcome to %s. Press ? for help.\n", curses_version());
5829 #elif defined(NCURSES_VERSION_MAJOR) && defined(NCURSES_VERSION_MINOR) && defined(NCURSES_VERSION_PATCH)
5830 (void) printf("Welcome to ncurses %d.%d.%d. Press ? for help.\n",
5831 NCURSES_VERSION_MAJOR,
5832 NCURSES_VERSION_MINOR,
5833 NCURSES_VERSION_PATCH);
5835 (void) puts("Welcome to ncurses. Press ? for help.");
5840 ExitProgram(EXIT_SUCCESS);
5843 /* ncurses.c ends here */