1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 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
42 $Id: ncurses.c,v 1.130 2000/07/07 11:05:16 tom Exp $
44 ***************************************************************************/
46 #include <test.priv.h>
59 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
63 #include <sys/select.h>
68 #define USE_LIBPANEL 1
71 #define USE_LIBPANEL 0
74 #if HAVE_MENU_H && HAVE_LIBMENU
81 #if HAVE_FORM_H && HAVE_LIBFORM
88 #ifdef NCURSES_VERSION
91 static int save_trace = TRACE_ORDINARY | TRACE_CALLS;
92 extern int _nc_tracing;
101 #define mmask_t chtype /* not specified in XSI */
102 #define attr_t chtype /* not specified in XSI */
103 #define ACS_S3 (acs_map['p']) /* scan line 3 */
104 #define ACS_S7 (acs_map['r']) /* scan line 7 */
105 #define ACS_LEQUAL (acs_map['y']) /* less/equal */
106 #define ACS_GEQUAL (acs_map['z']) /* greater/equal */
107 #define ACS_PI (acs_map['{']) /* Pi */
108 #define ACS_NEQUAL (acs_map['|']) /* not equal */
109 #define ACS_STERLING (acs_map['}']) /* UK pound sign */
113 #define P(string) printw("%s\n", string)
115 #define CTRL(x) ((x) & 0x1f)
118 #define QUIT CTRL('Q')
119 #define ESCAPE CTRL('[')
120 #define BLANK ' ' /* this is the background character */
122 /* The behavior of mvhline, mvvline for negative/zero length is unspecified,
123 * though we can rely on negative x/y values to stop the macro.
126 do_h_line(int y, int x, chtype c, int to)
129 mvhline(y, x, c, (to) - (x));
133 do_v_line(int y, int x, chtype c, int to)
136 mvvline(y, x, c, (to) - (y));
139 /* Common function to allow ^T to toggle trace-mode in the middle of a test
140 * so that trace-files can be made smaller.
143 wGetchar(WINDOW *win)
147 while ((c = wgetch(win)) == CTRL('T')) {
149 save_trace = _nc_tracing;
150 _tracef("TOGGLE-TRACING OFF");
153 _nc_tracing = save_trace;
157 _tracef("TOGGLE-TRACING ON");
164 #define Getchar() wGetchar(stdscr)
170 addstr("Press any key to continue... ");
175 Cannot(const char *what)
177 printw("\nThis %s terminal %s\n\n", getenv("TERM"), what);
182 ShellOut(bool message)
185 addstr("Shelling out...");
190 addstr("returned from shellout.\n");
194 #ifdef NCURSES_MOUSE_VERSION
196 mouse_decode(MEVENT const *ep)
200 (void) sprintf(buf, "id %2d at (%2d, %2d, %2d) state %4lx = {",
201 ep->id, ep->x, ep->y, ep->z, ep->bstate);
203 #define SHOW(m, s) if ((ep->bstate & m)==m) {strcat(buf,s); strcat(buf, ", ");}
204 SHOW(BUTTON1_RELEASED, "release-1");
205 SHOW(BUTTON1_PRESSED, "press-1");
206 SHOW(BUTTON1_CLICKED, "click-1");
207 SHOW(BUTTON1_DOUBLE_CLICKED, "doubleclick-1");
208 SHOW(BUTTON1_TRIPLE_CLICKED, "tripleclick-1");
209 SHOW(BUTTON1_RESERVED_EVENT, "reserved-1");
210 SHOW(BUTTON2_RELEASED, "release-2");
211 SHOW(BUTTON2_PRESSED, "press-2");
212 SHOW(BUTTON2_CLICKED, "click-2");
213 SHOW(BUTTON2_DOUBLE_CLICKED, "doubleclick-2");
214 SHOW(BUTTON2_TRIPLE_CLICKED, "tripleclick-2");
215 SHOW(BUTTON2_RESERVED_EVENT, "reserved-2");
216 SHOW(BUTTON3_RELEASED, "release-3");
217 SHOW(BUTTON3_PRESSED, "press-3");
218 SHOW(BUTTON3_CLICKED, "click-3");
219 SHOW(BUTTON3_DOUBLE_CLICKED, "doubleclick-3");
220 SHOW(BUTTON3_TRIPLE_CLICKED, "tripleclick-3");
221 SHOW(BUTTON3_RESERVED_EVENT, "reserved-3");
222 SHOW(BUTTON4_RELEASED, "release-4");
223 SHOW(BUTTON4_PRESSED, "press-4");
224 SHOW(BUTTON4_CLICKED, "click-4");
225 SHOW(BUTTON4_DOUBLE_CLICKED, "doubleclick-4");
226 SHOW(BUTTON4_TRIPLE_CLICKED, "tripleclick-4");
227 SHOW(BUTTON4_RESERVED_EVENT, "reserved-4");
228 SHOW(BUTTON_CTRL, "ctrl");
229 SHOW(BUTTON_SHIFT, "shift");
230 SHOW(BUTTON_ALT, "alt");
231 SHOW(ALL_MOUSE_EVENTS, "all-events");
232 SHOW(REPORT_MOUSE_POSITION, "position");
235 if (buf[strlen(buf) - 1] == ' ')
236 buf[strlen(buf) - 2] = '\0';
237 (void) strcat(buf, "}");
240 #endif /* NCURSES_MOUSE_VERSION */
242 /****************************************************************************
244 * Character input test
246 ****************************************************************************/
250 /* test the keypad feature */
254 int incount = 0, firsttime = 0;
255 bool blocking = TRUE;
260 #ifdef NCURSES_MOUSE_VERSION
261 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
264 (void) printw("Delay in 10ths of a second (<CR> for blocking input)? ");
270 if (isdigit(buf[0])) {
271 timeout(atoi(buf) * 100);
279 printw("Key pressed: %04o ", c);
280 #ifdef NCURSES_MOUSE_VERSION
281 if (c == KEY_MOUSE) {
285 printw("KEY_MOUSE, %s\n", mouse_decode(&event));
287 #endif /* NCURSES_MOUSE_VERSION */
289 (void) addstr(keyname(c));
291 } else if (c > 0x80) {
294 (void) printw("M-%c", c2);
296 (void) printw("M-%s", unctrl(c2));
297 addstr(" (high-half character)\n");
300 (void) printw("%c (ASCII printable character)\n", c);
302 (void) printw("%s (ASCII control character)\n", unctrl(c));
311 addstr("getstr test: ");
315 printw("I saw `%s'.\n", buf);
320 if (c == 'x' || c == 'q' || (c == ERR && blocking))
323 addstr("Type any key to see its keypad value. Also:\n");
324 addstr("g -- triggers a getstr test\n");
325 addstr("s -- shell out\n");
326 addstr("q -- quit\n");
327 addstr("? -- repeats this help message\n");
330 while ((c = Getchar()) == ERR)
332 (void) printw("%05d: input timed out\n", incount++);
334 (void) printw("%05d: input error\n", incount++);
339 #ifdef NCURSES_MOUSE_VERSION
340 mousemask(0, (mmask_t *) 0);
350 show_attr(int row, int skip, chtype attr, const char *name, bool once)
352 int ncv = tigetnum("ncv");
354 mvprintw(row, 8, "%s mode:", name);
355 mvprintw(row, 24, "|");
357 printw("%*s", skip, " ");
362 addstr("abcde fghij klmno pqrst uvwxy z");
366 printw("%*s", skip, " ");
368 if (attr != A_NORMAL) {
369 if (!(termattrs() & attr)) {
371 } else if (ncv > 0 && (getbkgd(stdscr) & A_COLOR)) {
372 static const attr_t table[] =
386 for (n = 0; n < sizeof(table) / sizeof(table[0]); n++) {
387 if ((table[n] & attr) != 0
388 && ((1 << n) & ncv) != 0) {
401 attr_getc(int *skip, int *fg, int *bg)
408 } else if (ch == CTRL('L')) {
412 } else if (has_colors()) {
444 /* test text attributes */
447 int skip = tigetnum("xmc");
448 int fg = COLOR_BLACK; /* color pair 0 is special */
449 int bg = COLOR_BLACK;
450 bool *pairs = (bool *) calloc(COLOR_PAIRS, sizeof(bool));
456 n = skip; /* make it easy */
460 int normal = A_NORMAL | BLANK;
463 int pair = (fg * COLORS) + bg;
465 init_pair(pair, fg, bg);
468 normal |= COLOR_PAIR(pair);
473 mvaddstr(0, 20, "Character attribute test display");
475 row = show_attr(row, n, A_STANDOUT, "STANDOUT", TRUE);
476 row = show_attr(row, n, A_REVERSE, "REVERSE", TRUE);
477 row = show_attr(row, n, A_BOLD, "BOLD", TRUE);
478 row = show_attr(row, n, A_UNDERLINE, "UNDERLINE", TRUE);
479 row = show_attr(row, n, A_DIM, "DIM", TRUE);
480 row = show_attr(row, n, A_BLINK, "BLINK", TRUE);
481 row = show_attr(row, n, A_PROTECT, "PROTECT", TRUE);
482 row = show_attr(row, n, A_INVIS, "INVISIBLE", TRUE);
483 row = show_attr(row, n, A_NORMAL, "NORMAL", FALSE);
486 "This terminal does %shave the magic-cookie glitch",
487 tigetnum("xmc") > -1 ? "" : "not ");
489 "Enter a digit to set gaps on each side of displayed attributes");
493 printw(". f/F/b/F toggle colors (now %d/%d)", fg, bg);
496 } while (attr_getc(&n, &fg, &bg));
498 free((char *) pairs);
499 bkgdset(A_NORMAL | BLANK);
504 /****************************************************************************
506 * Color support tests
508 ****************************************************************************/
510 static NCURSES_CONST char *color_names[] =
531 show_color_name(int y, int x, int color)
534 mvprintw(y, x, "%02d ", color);
536 mvaddstr(y, x, color_names[color]);
541 /* generate a color test pattern */
544 int base, top, width;
545 NCURSES_CONST char *hello;
548 (void) printw("There are %d color pairs\n", COLOR_PAIRS);
550 width = (COLORS > 8) ? 4 : 8;
551 hello = (COLORS > 8) ? "Test" : "Hello";
553 for (base = 0; base < 2; base++) {
554 top = (COLORS > 8) ? 0 : base * (COLORS + 3);
556 (void) mvprintw(top + 1, 0,
557 "%dx%d matrix of foreground/background colors, bright *%s*\n",
559 base ? "on" : "off");
560 for (i = 0; i < COLORS; i++)
561 show_color_name(top + 2, (i + 1) * width, i);
562 for (i = 0; i < COLORS; i++)
563 show_color_name(top + 3 + i, 0, i);
564 for (i = 1; i < COLOR_PAIRS; i++) {
565 init_pair(i, i % COLORS, i / COLORS);
566 attron((attr_t) COLOR_PAIR(i));
568 attron((attr_t) A_BOLD);
569 mvaddstr(top + 3 + (i / COLORS), (i % COLORS + 1) * width, hello);
572 if ((COLORS > 8) || base)
581 change_color(int current, int field, int value, int usebase)
583 short red, green, blue;
586 color_content(current, &red, &green, &blue);
588 red = green = blue = 0;
602 if (init_color(current, red, green, blue) == ERR)
608 /* display the color test pattern, without trying to edit colors */
610 int i, this_c = 0, value = 0, current = 0, field = 0;
612 int max_colors = COLORS > 16 ? 16 : COLORS;
616 for (i = 0; i < max_colors; i++)
617 init_pair(i, COLOR_WHITE, i);
619 mvprintw(LINES - 2, 0, "Number: %d", value);
622 short red, green, blue;
625 mvaddstr(0, 20, "Color RGB Value Editing");
628 for (i = 0; i < max_colors; i++) {
629 mvprintw(2 + i, 0, "%c %-8s:",
630 (i == current ? '>' : ' '),
631 (i < (int) SIZEOF(color_names)
632 ? color_names[i] : ""));
633 attrset(COLOR_PAIR(i));
638 * Note: this refresh should *not* be necessary! It works around
639 * a bug in attribute handling that apparently causes the A_NORMAL
640 * attribute sets to interfere with the actual emission of the
641 * color setting somehow. This needs to be fixed.
645 color_content(i, &red, &green, &blue);
647 if (current == i && field == 0)
650 if (current == i && field == 0)
653 if (current == i && field == 1)
655 printw("%04d", green);
656 if (current == i && field == 1)
659 if (current == i && field == 2)
661 printw("%04d", blue);
662 if (current == i && field == 2)
668 mvaddstr(max_colors + 3, 0,
669 "Use up/down to select a color, left/right to change fields.");
670 mvaddstr(max_colors + 4, 0,
671 "Modify field by typing nnn=, nnn-, or nnn+. ? for help.");
673 move(2 + current, 0);
677 if (isdigit(this_c) && !isdigit(last_c))
682 current = (current == 0 ? (max_colors - 1) : current - 1);
686 current = (current == (max_colors - 1) ? 0 : current + 1);
690 field = (field == 2 ? 0 : field + 1);
694 field = (field == 0 ? 2 : field - 1);
707 value = value * 10 + (this_c - '0');
711 change_color(current, field, value, 1);
715 change_color(current, field, -value, 1);
719 change_color(current, field, value, 0);
724 P(" RGB Value Editing Help");
726 P("You are in the RGB value editor. Use the arrow keys to select one of");
727 P("the fields in one of the RGB triples of the current colors; the one");
728 P("currently selected will be reverse-video highlighted.");
730 P("To change a field, enter the digits of the new value; they are echoed");
731 P("as entered. Finish by typing `='. The change will take effect instantly.");
732 P("To increment or decrement a value, use the same procedure, but finish");
733 P("with a `+' or `-'.");
735 P("To quit, do `x' or 'q'");
749 mvprintw(LINES - 2, 0, "Number: %d", value);
752 (this_c != 'x' && this_c != 'q');
758 /****************************************************************************
760 * Soft-key label test
762 ****************************************************************************/
766 /* exercise the soft keys */
778 mvaddstr(0, 20, "Soft Key Exerciser");
782 P("Available commands are:");
784 P("^L -- refresh screen");
785 P("a -- activate or restore soft keys");
786 P("d -- disable soft keys");
787 P("c -- set centered format for labels");
788 P("l -- set left-justified format for labels");
789 P("r -- set right-justified format for labels");
790 P("[12345678] -- set label; labels are numbered 1 through 8");
791 P("e -- erase stdscr (should not erase labels)");
792 P("s -- test scrolling of shortened screen");
793 P("x, q -- return to main menu");
795 P("Note: if activating the soft keys causes your terminal to");
796 P("scroll up one line, your terminal auto-scrolls when anything");
797 P("is written to the last screen position. The ncurses code");
798 P("does not yet handle this gracefully.");
811 mvprintw(20, 0, "Press Q to stop the scrolling-test: ");
812 while ((c = Getchar()) != 'Q' && (c != ERR))
840 (void) mvaddstr(20, 0, "Please enter the label value: ");
842 wgetnstr(stdscr, buf, 8);
844 slk_set((c - '0'), buf, fmt);
858 ((c = Getchar()) != EOF);
865 /****************************************************************************
867 * Alternate character-set stuff
869 ****************************************************************************/
871 /* ISO 6429: codes 0x80 to 0x9f may be control characters that cause the
872 * terminal to perform functions. The remaining codes can be graphic.
875 show_upper_chars(int first)
877 bool C1 = (first == 128);
879 int last = first + 31;
884 mvprintw(0, 20, "Display of %s Character Codes %d to %d",
885 C1 ? "C1" : "GR", first, last);
889 for (code = first; code <= last; code++) {
890 int row = 4 + ((code - first) % 16);
891 int col = ((code - first) / 16) * COLS / 2;
893 sprintf(tmp, "%3d (0x%x)", code, code);
894 mvprintw(row, col, "%*s: ", COLS / 4, tmp);
896 nodelay(stdscr, TRUE);
899 /* (yes, this _is_ crude) */
900 while ((reply = Getchar()) != ERR) {
904 nodelay(stdscr, FALSE);
910 show_1_acs(int n, const char *name, chtype code)
912 const int height = 16;
913 int row = 4 + (n % height);
914 int col = (n / height) * COLS / 2;
915 mvprintw(row, col, "%*s : ", COLS / 4, name);
922 /* display the ACS character set */
926 #define BOTH(name) #name, name
930 mvaddstr(0, 20, "Display of the ACS Character Set");
934 n = show_1_acs(0, BOTH(ACS_ULCORNER));
935 n = show_1_acs(n, BOTH(ACS_LLCORNER));
936 n = show_1_acs(n, BOTH(ACS_URCORNER));
937 n = show_1_acs(n, BOTH(ACS_LRCORNER));
938 n = show_1_acs(n, BOTH(ACS_RTEE));
939 n = show_1_acs(n, BOTH(ACS_LTEE));
940 n = show_1_acs(n, BOTH(ACS_BTEE));
941 n = show_1_acs(n, BOTH(ACS_TTEE));
942 n = show_1_acs(n, BOTH(ACS_HLINE));
943 n = show_1_acs(n, BOTH(ACS_VLINE));
944 n = show_1_acs(n, BOTH(ACS_PLUS));
945 n = show_1_acs(n, BOTH(ACS_S1));
946 n = show_1_acs(n, BOTH(ACS_S9));
947 n = show_1_acs(n, BOTH(ACS_DIAMOND));
948 n = show_1_acs(n, BOTH(ACS_CKBOARD));
949 n = show_1_acs(n, BOTH(ACS_DEGREE));
950 n = show_1_acs(n, BOTH(ACS_PLMINUS));
951 n = show_1_acs(n, BOTH(ACS_BULLET));
952 n = show_1_acs(n, BOTH(ACS_LARROW));
953 n = show_1_acs(n, BOTH(ACS_RARROW));
954 n = show_1_acs(n, BOTH(ACS_DARROW));
955 n = show_1_acs(n, BOTH(ACS_UARROW));
956 n = show_1_acs(n, BOTH(ACS_BOARD));
957 n = show_1_acs(n, BOTH(ACS_LANTERN));
958 n = show_1_acs(n, BOTH(ACS_BLOCK));
959 n = show_1_acs(n, BOTH(ACS_S3));
960 n = show_1_acs(n, BOTH(ACS_S7));
961 n = show_1_acs(n, BOTH(ACS_LEQUAL));
962 n = show_1_acs(n, BOTH(ACS_GEQUAL));
963 n = show_1_acs(n, BOTH(ACS_PI));
964 n = show_1_acs(n, BOTH(ACS_NEQUAL));
965 n = show_1_acs(n, BOTH(ACS_STERLING));
982 show_upper_chars((c - '0') * 32 + 128);
985 mvprintw(LINES - 3, 0,
986 "Note: ANSI terminals may not display C1 characters.");
987 mvprintw(LINES - 2, 0,
988 "Select: a=ACS, 0=C1, 1,2,3=GR characters, q=quit");
990 } while ((c = Getchar()) != 'x' && c != 'q');
998 * Graphic-rendition test (adapted from vttest)
1001 test_sgr_attributes(void)
1005 for (pass = 0; pass < 2; pass++) {
1006 int normal = ((pass == 0 ? A_NORMAL : A_REVERSE)) | BLANK;
1008 /* Use non-default colors if possible to exercise bce a little */
1010 init_pair(1, COLOR_WHITE, COLOR_BLUE);
1011 normal |= COLOR_PAIR(1);
1015 mvprintw(1, 20, "Graphic rendition test pattern:");
1017 mvprintw(4, 1, "vanilla");
1019 #define set_sgr(mask) bkgdset((normal^(mask)));
1021 mvprintw(4, 40, "bold");
1023 set_sgr(A_UNDERLINE);
1024 mvprintw(6, 6, "underline");
1026 set_sgr(A_BOLD | A_UNDERLINE);
1027 mvprintw(6, 45, "bold underline");
1030 mvprintw(8, 1, "blink");
1032 set_sgr(A_BLINK | A_BOLD);
1033 mvprintw(8, 40, "bold blink");
1035 set_sgr(A_UNDERLINE | A_BLINK);
1036 mvprintw(10, 6, "underline blink");
1038 set_sgr(A_BOLD | A_UNDERLINE | A_BLINK);
1039 mvprintw(10, 45, "bold underline blink");
1042 mvprintw(12, 1, "negative");
1044 set_sgr(A_BOLD | A_REVERSE);
1045 mvprintw(12, 40, "bold negative");
1047 set_sgr(A_UNDERLINE | A_REVERSE);
1048 mvprintw(14, 6, "underline negative");
1050 set_sgr(A_BOLD | A_UNDERLINE | A_REVERSE);
1051 mvprintw(14, 45, "bold underline negative");
1053 set_sgr(A_BLINK | A_REVERSE);
1054 mvprintw(16, 1, "blink negative");
1056 set_sgr(A_BOLD | A_BLINK | A_REVERSE);
1057 mvprintw(16, 40, "bold blink negative");
1059 set_sgr(A_UNDERLINE | A_BLINK | A_REVERSE);
1060 mvprintw(18, 6, "underline blink negative");
1062 set_sgr(A_BOLD | A_UNDERLINE | A_BLINK | A_REVERSE);
1063 mvprintw(18, 45, "bold underline blink negative");
1066 mvprintw(LINES - 2, 1, "%s background. ", pass == 0 ? "Dark" :
1072 bkgdset(A_NORMAL | BLANK);
1077 /****************************************************************************
1079 * Windows and scrolling tester.
1081 ****************************************************************************/
1083 #define BOTLINES 4 /* number of line stolen from screen bottom */
1089 #define FRAME struct frame
1098 /* We need to know if these flags are actually set, so don't look in FRAME.
1099 * These names are known to work with SVr4 curses as well as ncurses.
1102 HaveKeypad(FRAME * curp)
1104 WINDOW *win = (curp ? curp->wind : stdscr);
1105 return win->_use_keypad;
1109 HaveScroll(FRAME * curp)
1111 WINDOW *win = (curp ? curp->wind : stdscr);
1112 return win->_scroll;
1116 newwin_legend(FRAME * curp)
1118 static const struct {
1123 "^C = create window", 0
1126 "^N = next window", 0
1129 "^P = previous window", 0
1132 "^F = scroll forward", 0
1135 "^B = scroll backward", 0
1138 "^K = keypad(%s)", 1
1141 "^S = scrollok(%s)", 2
1144 "^W = save window to file", 0
1147 "^R = restore window", 0
1160 bool do_keypad = HaveKeypad(curp);
1161 bool do_scroll = HaveScroll(curp);
1165 for (n = 0; n < SIZEOF(legend); n++) {
1166 switch (legend[n].code) {
1168 strcpy(buf, legend[n].msg);
1171 sprintf(buf, legend[n].msg, do_keypad ? "yes" : "no");
1174 sprintf(buf, legend[n].msg, do_scroll ? "yes" : "no");
1177 sprintf(buf, legend[n].msg, do_keypad ? "/ESC" : "");
1180 getyx(stdscr, y, x);
1181 addstr((COLS < (x + 3 + (int) strlen(buf))) ? "\n" : (n ? ", " : ""));
1188 transient(FRAME * curp, NCURSES_CONST char *msg)
1190 newwin_legend(curp);
1192 mvaddstr(LINES - 1, 0, msg);
1198 printw("%s characters are echoed, window should %sscroll.",
1199 HaveKeypad(curp) ? "Non-arrow" : "All other",
1200 HaveScroll(curp) ? "" : "not ");
1205 newwin_report(FRAME * curp)
1206 /* report on the cursor's current position, then restore it */
1208 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
1212 transient(curp, (char *) 0);
1214 move(LINES - 1, COLS - 17);
1215 printw("Y = %2d X = %2d", y, x);
1223 selectcell(int uli, int ulj, int lri, int lrj)
1224 /* arrows keys move cursor, return location at current on non-arrow key */
1226 static pair res; /* result cell */
1227 int si = lri - uli + 1; /* depth of the select area */
1228 int sj = lrj - ulj + 1; /* width of the select area */
1229 int i = 0, j = 0; /* offsets into the select area */
1234 move(uli + i, ulj + j);
1235 newwin_report((FRAME *) 0);
1237 switch (Getchar()) {
1252 return ((pair *) 0);
1253 #ifdef NCURSES_MOUSE_VERSION
1259 if (event.y > uli && event.x > ulj) {
1280 outerbox(pair ul, pair lr, bool onoff)
1281 /* draw or erase a box *outside* the given pair of corners */
1283 mvaddch(ul.y - 1, lr.x - 1, onoff ? ACS_ULCORNER : ' ');
1284 mvaddch(ul.y - 1, lr.x + 1, onoff ? ACS_URCORNER : ' ');
1285 mvaddch(lr.y + 1, lr.x + 1, onoff ? ACS_LRCORNER : ' ');
1286 mvaddch(lr.y + 1, ul.x - 1, onoff ? ACS_LLCORNER : ' ');
1287 move(ul.y - 1, ul.x);
1288 hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
1289 move(ul.y, ul.x - 1);
1290 vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
1291 move(lr.y + 1, ul.x);
1292 hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
1293 move(ul.y, lr.x + 1);
1294 vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
1299 /* Ask user for a window definition */
1306 addstr("Use arrows to move cursor, anything else to mark corner 1");
1308 if ((tmp = selectcell(2, 1, LINES - BOTLINES - 2, COLS - 2)) == (pair *) 0)
1309 return ((WINDOW *) 0);
1310 memcpy(&ul, tmp, sizeof(pair));
1311 mvaddch(ul.y - 1, ul.x - 1, ACS_ULCORNER);
1314 addstr("Use arrows to move cursor, anything else to mark corner 2");
1316 if ((tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2)) ==
1318 return ((WINDOW *) 0);
1319 memcpy(&lr, tmp, sizeof(pair));
1321 rwindow = subwin(stdscr, lr.y - ul.y + 1, lr.x - ul.x + 1, ul.y, ul.x);
1323 outerbox(ul, lr, TRUE);
1334 newwin_move(FRAME * curp, int dy, int dx)
1336 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
1340 getyx(win, cur_y, cur_x);
1341 getmaxyx(win, max_y, max_x);
1342 if ((cur_x += dx) < 0)
1344 else if (cur_x >= max_x)
1346 if ((cur_y += dy) < 0)
1348 else if (cur_y >= max_y)
1350 wmove(win, cur_y, cur_x);
1354 delete_framed(FRAME * fp, bool showit)
1358 fp->last->next = fp->next;
1359 fp->next->last = fp->last;
1367 np = (fp == fp->next) ? 0 : fp->next;
1373 acs_and_scroll(void)
1374 /* Demonstrate windows */
1378 FRAME *current = (FRAME *) 0, *neww;
1379 WINDOW *usescr = stdscr;
1381 #define DUMPFILE "screendump"
1383 #ifdef NCURSES_MOUSE_VERSION
1384 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
1389 transient((FRAME *) 0, (char *) 0);
1392 neww = (FRAME *) calloc(1, sizeof(FRAME));
1393 if ((neww->wind = getwindow()) == (WINDOW *) 0)
1396 if (current == 0) { /* First element, */
1397 neww->next = neww; /* so point it at itself */
1400 neww->next = current->next;
1401 neww->last = current;
1402 neww->last->next = neww;
1403 neww->next->last = neww;
1406 /* SVr4 curses sets the keypad on all newly-created windows to
1407 * false. Someone reported that PDCurses makes new windows inherit
1408 * this flag. Remove the following 'keypad()' call to test this
1410 keypad(current->wind, TRUE);
1411 current->do_keypad = HaveKeypad(current);
1412 current->do_scroll = HaveScroll(current);
1415 case CTRL('N'): /* go to next window */
1417 current = current->next;
1420 case CTRL('P'): /* go to previous window */
1422 current = current->last;
1425 case CTRL('F'): /* scroll current window forward */
1427 wscrl(current->wind, 1);
1430 case CTRL('B'): /* scroll current window backwards */
1432 wscrl(current->wind, -1);
1435 case CTRL('K'): /* toggle keypad mode for current */
1437 current->do_keypad = !current->do_keypad;
1438 keypad(current->wind, current->do_keypad);
1444 current->do_scroll = !current->do_scroll;
1445 scrollok(current->wind, current->do_scroll);
1449 case CTRL('W'): /* save and delete window */
1450 if (current == current->next)
1452 if ((fp = fopen(DUMPFILE, "w")) == (FILE *) 0)
1453 transient(current, "Can't open screen dump file");
1455 (void) putwin(current->wind, fp);
1458 current = delete_framed(current, TRUE);
1462 case CTRL('R'): /* restore window */
1463 if ((fp = fopen(DUMPFILE, "r")) == (FILE *) 0)
1464 transient(current, "Can't open screen dump file");
1466 neww = (FRAME *) calloc(1, sizeof(FRAME));
1468 neww->next = current->next;
1469 neww->last = current;
1470 neww->last->next = neww;
1471 neww->next->last = neww;
1473 neww->wind = getwin(fp);
1476 wrefresh(neww->wind);
1481 case CTRL('X'): /* resize window */
1488 addstr("Use arrows to move cursor, anything else to mark new corner");
1491 getbegyx(current->wind, ul.y, ul.x);
1493 tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2);
1494 if (tmp == (pair *) 0) {
1499 getmaxyx(current->wind, lr.y, lr.x);
1502 outerbox(ul, lr, FALSE);
1503 wnoutrefresh(stdscr);
1505 /* strictly cosmetic hack for the test */
1506 getmaxyx(current->wind, my, mx);
1507 if (my > tmp->y - ul.y) {
1508 getyx(current->wind, lr.y, lr.x);
1509 wmove(current->wind, tmp->y - ul.y + 1, 0);
1510 wclrtobot(current->wind);
1511 wmove(current->wind, lr.y, lr.x);
1513 if (mx > tmp->x - ul.x)
1514 for (i = 0; i < my; i++) {
1515 wmove(current->wind, i, tmp->x - ul.x + 1);
1516 wclrtoeol(current->wind);
1518 wnoutrefresh(current->wind);
1520 memcpy(&lr, tmp, sizeof(pair));
1521 (void) wresize(current->wind, lr.y - ul.y + 0, lr.x - ul.x + 0);
1523 getbegyx(current->wind, ul.y, ul.x);
1524 getmaxyx(current->wind, lr.y, lr.x);
1527 outerbox(ul, lr, TRUE);
1528 wnoutrefresh(stdscr);
1530 wnoutrefresh(current->wind);
1536 #endif /* HAVE_WRESIZE */
1538 case KEY_F(10): /* undocumented --- use this to test area clears */
1539 selectcell(0, 0, LINES - 1, COLS - 1);
1545 newwin_move(current, -1, 0);
1548 newwin_move(current, 1, 0);
1551 newwin_move(current, 0, -1);
1554 newwin_move(current, 0, 1);
1562 getyx(current->wind, y, x);
1566 x = getmaxx(current->wind) - 1;
1568 mvwdelch(current->wind, y, x);
1578 waddch(current->wind, (chtype) c);
1583 newwin_report(current);
1584 usescr = (current ? current->wind : stdscr);
1587 ((c = wGetchar(usescr)) != QUIT
1588 && !((c == ESCAPE) && (usescr->_use_keypad))
1592 while (current != 0)
1593 current = delete_framed(current, FALSE);
1595 scrollok(stdscr, TRUE); /* reset to driver's default */
1596 #ifdef NCURSES_MOUSE_VERSION
1597 mousemask(0, (mmask_t *) 0);
1604 /****************************************************************************
1608 ****************************************************************************/
1611 static unsigned long nap_msec = 1;
1613 static NCURSES_CONST char *mod[] =
1623 /*+-------------------------------------------------------------------------
1625 --------------------------------------------------------------------------*/
1627 wait_a_while(unsigned long msec GCC_UNUSED)
1637 else if (msec > 1000L)
1638 sleep((int) msec / 1000L);
1642 } /* end of wait_a_while */
1644 /*+-------------------------------------------------------------------------
1646 --------------------------------------------------------------------------*/
1648 saywhat(NCURSES_CONST char *text)
1650 wmove(stdscr, LINES - 1, 0);
1652 waddstr(stdscr, text);
1653 } /* end of saywhat */
1655 /*+-------------------------------------------------------------------------
1656 mkpanel(rows,cols,tly,tlx) - alloc a win and panel and associate them
1657 --------------------------------------------------------------------------*/
1659 mkpanel(int color, int rows, int cols, int tly, int tlx)
1664 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
1665 if ((pan = new_panel(win)) == 0) {
1667 } else if (has_colors()) {
1668 int fg = (color == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
1670 init_pair(color, fg, bg);
1671 wbkgdset(win, COLOR_PAIR(color) | ' ');
1673 wbkgdset(win, A_BOLD | ' ');
1677 } /* end of mkpanel */
1679 /*+-------------------------------------------------------------------------
1681 --------------------------------------------------------------------------*/
1683 rmpanel(PANEL * pan)
1685 WINDOW *win = panel_window(pan);
1688 } /* end of rmpanel */
1690 /*+-------------------------------------------------------------------------
1692 --------------------------------------------------------------------------*/
1698 } /* end of pflush */
1700 /*+-------------------------------------------------------------------------
1702 --------------------------------------------------------------------------*/
1704 fill_panel(PANEL * pan)
1706 WINDOW *win = panel_window(pan);
1707 int num = ((const char *) panel_userptr(pan))[1];
1711 wprintw(win, "-pan%c-", num);
1714 for (y = 2; y < getmaxy(win) - 1; y++) {
1715 for (x = 1; x < getmaxx(win) - 1; x++) {
1720 } /* end of fill_panel */
1730 for (y = 0; y < LINES - 1; y++) {
1731 for (x = 0; x < COLS; x++)
1732 wprintw(stdscr, "%d", (y + x) % 10);
1734 for (y = 0; y < 5; y++) {
1741 p1 = mkpanel(COLOR_RED,
1746 set_panel_userptr(p1, "p1");
1748 p2 = mkpanel(COLOR_GREEN,
1753 set_panel_userptr(p2, "p2");
1755 p3 = mkpanel(COLOR_YELLOW,
1760 set_panel_userptr(p3, "p3");
1762 p4 = mkpanel(COLOR_BLUE,
1767 set_panel_userptr(p4, "p4");
1769 p5 = mkpanel(COLOR_MAGENTA,
1774 set_panel_userptr(p5, "p5");
1784 saywhat("press any key to continue");
1785 wait_a_while(nap_msec);
1787 saywhat("h3 s1 s2 s4 s5; press any key to continue");
1788 move_panel(p1, 0, 0);
1795 wait_a_while(nap_msec);
1797 saywhat("s1; press any key to continue");
1800 wait_a_while(nap_msec);
1802 saywhat("s2; press any key to continue");
1805 wait_a_while(nap_msec);
1807 saywhat("m2; press any key to continue");
1808 move_panel(p2, LINES / 3 + 1, COLS / 8);
1810 wait_a_while(nap_msec);
1815 wait_a_while(nap_msec);
1817 saywhat("m3; press any key to continue");
1818 move_panel(p3, LINES / 4 + 1, COLS / 15);
1820 wait_a_while(nap_msec);
1822 saywhat("b3; press any key to continue");
1825 wait_a_while(nap_msec);
1827 saywhat("s4; press any key to continue");
1830 wait_a_while(nap_msec);
1832 saywhat("s5; press any key to continue");
1835 wait_a_while(nap_msec);
1837 saywhat("t3; press any key to continue");
1840 wait_a_while(nap_msec);
1842 saywhat("t1; press any key to continue");
1845 wait_a_while(nap_msec);
1847 saywhat("t2; press any key to continue");
1850 wait_a_while(nap_msec);
1852 saywhat("t3; press any key to continue");
1855 wait_a_while(nap_msec);
1857 saywhat("t4; press any key to continue");
1860 wait_a_while(nap_msec);
1862 for (itmp = 0; itmp < 6; itmp++) {
1863 WINDOW *w4 = panel_window(p4);
1864 WINDOW *w5 = panel_window(p5);
1866 saywhat("m4; press any key to continue");
1867 wmove(w4, LINES / 8, 1);
1868 waddstr(w4, mod[itmp]);
1869 move_panel(p4, LINES / 6, itmp * (COLS / 8));
1870 wmove(w5, LINES / 6, 1);
1871 waddstr(w5, mod[itmp]);
1873 wait_a_while(nap_msec);
1875 saywhat("m5; press any key to continue");
1876 wmove(w4, LINES / 6, 1);
1877 waddstr(w4, mod[itmp]);
1878 move_panel(p5, LINES / 3 - 1, (itmp * 10) + 6);
1879 wmove(w5, LINES / 8, 1);
1880 waddstr(w5, mod[itmp]);
1882 wait_a_while(nap_msec);
1885 saywhat("m4; press any key to continue");
1886 move_panel(p4, LINES / 6, itmp * (COLS / 8));
1888 wait_a_while(nap_msec);
1890 saywhat("t5; press any key to continue");
1893 wait_a_while(nap_msec);
1895 saywhat("t2; press any key to continue");
1898 wait_a_while(nap_msec);
1900 saywhat("t1; press any key to continue");
1903 wait_a_while(nap_msec);
1905 saywhat("d2; press any key to continue");
1908 wait_a_while(nap_msec);
1910 saywhat("h3; press any key to continue");
1913 wait_a_while(nap_msec);
1915 saywhat("d1; press any key to continue");
1918 wait_a_while(nap_msec);
1920 saywhat("d4; press any key to continue");
1923 wait_a_while(nap_msec);
1925 saywhat("d5; press any key to continue");
1928 wait_a_while(nap_msec);
1938 /****************************************************************************
1942 ****************************************************************************/
1946 static bool show_panner_legend = TRUE;
1949 panner_legend(int line)
1951 static const char *const legend[] =
1953 "Use arrow keys (or U,D,L,R) to pan, q to quit (?,t,s flags)",
1954 "Use ! to shell-out. Toggle legend:?, timer:t, scroll mark:s.",
1955 "Use +,- (or j,k) to grow/shrink the panner vertically.",
1956 "Use <,> (or h,l) to grow/shrink the panner horizontally."
1958 int n = (SIZEOF(legend) - (LINES - line));
1959 if (line < LINES && (n >= 0)) {
1961 if (show_panner_legend)
1962 printw("%s", legend[n]);
1964 return show_panner_legend;
1970 panner_h_cleanup(int from_y, int from_x, int to_x)
1972 if (!panner_legend(from_y))
1973 do_h_line(from_y, from_x, ' ', to_x);
1977 panner_v_cleanup(int from_y, int from_x, int to_y)
1979 if (!panner_legend(from_y))
1980 do_v_line(from_y, from_x, ' ', to_y);
1985 int top_x, int top_y, int porty, int portx,
1986 int (*pgetc) (WINDOW *))
1988 #if HAVE_GETTIMEOFDAY
1989 struct timeval before, after;
1992 bool scrollers = TRUE;
1995 int pxmax, pymax, lowend, highend, c;
1997 getmaxyx(pad, pymax, pxmax);
1998 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
2002 #ifdef NCURSES_VERSION
2004 * During shell-out, the user may have resized the window. Adjust
2005 * the port size of the pad to accommodate this. Ncurses automatically
2006 * resizes all of the normal windows to fit on the new screen.
2024 show_panner_legend = !show_panner_legend;
2025 panner_legend(LINES - 4);
2026 panner_legend(LINES - 3);
2027 panner_legend(LINES - 2);
2028 panner_legend(LINES - 1);
2030 #if HAVE_GETTIMEOFDAY
2034 panner_legend(LINES - 1);
2038 scrollers = !scrollers;
2041 /* Move the top-left corner of the pad, keeping the bottom-right
2044 case 'h': /* increase-columns: move left edge to left */
2048 panner_v_cleanup(top_y, top_x, porty);
2053 case 'j': /* decrease-lines: move top-edge down */
2057 panner_h_cleanup(top_y - 1, top_x - (top_x > 0), portx);
2062 case 'k': /* increase-lines: move top-edge up */
2067 panner_h_cleanup(top_y, top_x, portx);
2071 case 'l': /* decrease-columns: move left-edge to right */
2075 panner_v_cleanup(top_y - (top_y > 0), top_x - 1, porty);
2080 /* Move the bottom-right corner of the pad, keeping the top-left
2083 case KEY_IC: /* increase-columns: move right-edge to right */
2084 if (portx >= pxmax || portx >= COLS)
2087 panner_v_cleanup(top_y - (top_y > 0), portx - 1, porty);
2092 case KEY_IL: /* increase-lines: move bottom-edge down */
2093 if (porty >= pymax || porty >= LINES)
2096 panner_h_cleanup(porty - 1, top_x - (top_x > 0), portx);
2101 case KEY_DC: /* decrease-columns: move bottom edge up */
2106 panner_v_cleanup(top_y - (top_y > 0), portx, porty);
2110 case KEY_DL: /* decrease-lines */
2115 panner_h_cleanup(porty, top_x - (top_x > 0), portx);
2119 case KEY_LEFT: /* pan leftwards */
2126 case KEY_RIGHT: /* pan rightwards */
2127 if (basex + portx - (pymax > porty) < pxmax)
2133 case KEY_UP: /* pan upwards */
2140 case KEY_DOWN: /* pan downwards */
2141 if (basey + porty - (pxmax > portx) < pymax)
2156 basey = pymax - porty;
2166 mvaddch(top_y - 1, top_x - 1, ACS_ULCORNER);
2167 do_v_line(top_y, top_x - 1, ACS_VLINE, porty);
2168 do_h_line(top_y - 1, top_x, ACS_HLINE, portx);
2170 if (scrollers && (pxmax > portx - 1)) {
2171 int length = (portx - top_x - 1);
2172 float ratio = ((float) length) / ((float) pxmax);
2174 lowend = (int) (top_x + (basex * ratio));
2175 highend = (int) (top_x + ((basex + length) * ratio));
2177 do_h_line(porty - 1, top_x, ACS_HLINE, lowend);
2178 if (highend < portx) {
2180 do_h_line(porty - 1, lowend, ' ', highend + 1);
2182 do_h_line(porty - 1, highend + 1, ACS_HLINE, portx);
2185 do_h_line(porty - 1, top_x, ACS_HLINE, portx);
2187 if (scrollers && (pymax > porty - 1)) {
2188 int length = (porty - top_y - 1);
2189 float ratio = ((float) length) / ((float) pymax);
2191 lowend = (int) (top_y + (basey * ratio));
2192 highend = (int) (top_y + ((basey + length) * ratio));
2194 do_v_line(top_y, portx - 1, ACS_VLINE, lowend);
2195 if (highend < porty) {
2197 do_v_line(lowend, portx - 1, ' ', highend + 1);
2199 do_v_line(highend + 1, portx - 1, ACS_VLINE, porty);
2202 do_v_line(top_y, portx - 1, ACS_VLINE, porty);
2204 mvaddch(top_y - 1, portx - 1, ACS_URCORNER);
2205 mvaddch(porty - 1, top_x - 1, ACS_LLCORNER);
2206 mvaddch(porty - 1, portx - 1, ACS_LRCORNER);
2208 #if HAVE_GETTIMEOFDAY
2209 gettimeofday(&before, 0);
2211 wnoutrefresh(stdscr);
2216 porty - (pxmax > portx) - 1,
2217 portx - (pymax > porty) - 1);
2220 #if HAVE_GETTIMEOFDAY
2223 gettimeofday(&after, 0);
2224 elapsed = (after.tv_sec + after.tv_usec / 1.0e6)
2225 - (before.tv_sec + before.tv_usec / 1.0e6);
2226 move(LINES - 1, COLS - 20);
2227 printw("Secs: %2.03f", elapsed);
2233 ((c = pgetc(pad)) != KEY_EXIT);
2235 scrollok(stdscr, TRUE); /* reset to driver's default */
2240 padgetch(WINDOW *win)
2244 switch (c = wGetchar(win)) {
2270 case ERR: /* FALLTHRU */
2278 #define PAD_HIGH 200
2279 #define PAD_WIDE 200
2283 /* Demonstrate pads. */
2286 unsigned gridcount = 0;
2287 WINDOW *panpad = newpad(PAD_HIGH, PAD_WIDE);
2290 Cannot("cannot create requested pad");
2294 for (i = 0; i < PAD_HIGH; i++) {
2295 for (j = 0; j < PAD_WIDE; j++)
2296 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
2297 if (i == 0 || j == 0)
2298 waddch(panpad, '+');
2300 waddch(panpad, (chtype) ('A' + (gridcount++ % 26)));
2301 } else if (i % GRIDSIZE == 0)
2302 waddch(panpad, '-');
2303 else if (j % GRIDSIZE == 0)
2304 waddch(panpad, '|');
2306 waddch(panpad, ' ');
2308 panner_legend(LINES - 4);
2309 panner_legend(LINES - 3);
2310 panner_legend(LINES - 2);
2311 panner_legend(LINES - 1);
2313 keypad(panpad, TRUE);
2315 /* Make the pad (initially) narrow enough that a trace file won't wrap.
2316 * We'll still be able to widen it during a test, since that's required
2317 * for testing boundaries.
2319 panner(panpad, 2, 2, LINES - 5, COLS - 15, padgetch);
2325 #endif /* USE_LIBPANEL */
2327 /****************************************************************************
2329 * Tests from John Burnell's PDCurses tester
2331 ****************************************************************************/
2334 Continue(WINDOW *win)
2338 mvwaddstr(win, 10, 1, " Press any key to continue");
2344 flushinp_test(WINDOW *win)
2345 /* Input test, adapted from John Burnell's PDCurses tester */
2347 int w, h, bx, by, sw, sh, i;
2352 getmaxyx(win, h, w);
2353 getbegyx(win, by, bx);
2356 if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == 0)
2361 init_pair(2, COLOR_CYAN, COLOR_BLUE);
2362 wbkgd(subWin, COLOR_PAIR(2) | ' ');
2365 wattrset(subWin, A_BOLD);
2366 box(subWin, ACS_VLINE, ACS_HLINE);
2367 mvwaddstr(subWin, 2, 1, "This is a subwindow");
2371 * This used to set 'nocbreak()'. However, Alexander Lukyanov says that
2372 * it only happened to "work" on SVr4 because that implementation does not
2373 * emulate nocbreak+noecho mode, whereas ncurses does. To get the desired
2374 * test behavior, we're using 'cbreak()', which will allow a single
2375 * character to return without needing a newline. - T.Dickey 1997/10/11.
2378 mvwaddstr(win, 0, 1, "This is a test of the flushinp() call.");
2380 mvwaddstr(win, 2, 1, "Type random keys for 5 seconds.");
2381 mvwaddstr(win, 3, 1,
2382 "These should be discarded (not echoed) after the subwindow goes away.");
2385 for (i = 0; i < 5; i++) {
2386 mvwprintw(subWin, 1, 1, "Time = %d", i);
2398 mvwaddstr(win, 2, 1,
2399 "If you were still typing when the window timer expired,");
2400 mvwaddstr(win, 3, 1,
2401 "or else you typed nothing at all while it was running,");
2402 mvwaddstr(win, 4, 1,
2403 "test was invalid. You'll see garbage or nothing at all. ");
2404 mvwaddstr(win, 6, 1, "Press a key");
2410 mvwaddstr(win, 12, 0,
2411 "If you see any key other than what you typed, flushinp() is broken.");
2420 "What you typed should now have been deleted; if not, wdelch() failed.");
2426 /****************************************************************************
2430 ****************************************************************************/
2438 menu_virtualize(int c)
2440 if (c == '\n' || c == KEY_EXIT)
2441 return (MAX_COMMAND + 1);
2443 return (REQ_SCR_ULINE);
2445 return (REQ_SCR_DLINE);
2446 else if (c == 'b' || c == KEY_NPAGE)
2447 return (REQ_SCR_UPAGE);
2448 else if (c == 'f' || c == KEY_PPAGE)
2449 return (REQ_SCR_DPAGE);
2450 else if (c == 'n' || c == KEY_DOWN)
2451 return (REQ_NEXT_ITEM);
2452 else if (c == 'p' || c == KEY_UP)
2453 return (REQ_PREV_ITEM);
2455 return (REQ_TOGGLE_ITEM);
2463 static const char *animals[] =
2465 "Lions", "Tigers", "Bears", "(Oh my!)", "Newts", "Platypi", "Lemurs",
2473 ITEM *items[SIZEOF(animals)];
2476 int mrows, mcols, c;
2479 #ifdef NCURSES_MOUSE_VERSION
2480 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
2482 mvaddstr(0, 0, "This is the menu test:");
2483 mvaddstr(2, 0, " Use up and down arrow to move the select bar.");
2484 mvaddstr(3, 0, " 'n' and 'p' act like arrows.");
2486 " 'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
2487 mvaddstr(5, 0, " Press return to exit.");
2490 for (ap = animals; *ap; ap++)
2491 *ip++ = new_item(*ap, "");
2494 m = new_menu(items);
2496 set_menu_format(m, (SIZEOF(animals) + 1) / 2, 1);
2497 scale_menu(m, &mrows, &mcols);
2499 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
2500 set_menu_win(m, menuwin);
2501 keypad(menuwin, TRUE);
2504 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
2508 while ((c = menu_driver(m, menu_virtualize(wGetchar(menuwin)))) != E_UNKNOWN_COMMAND) {
2509 if (c == E_REQUEST_DENIED)
2514 (void) mvprintw(LINES - 2, 0,
2515 "You chose: %s\n", item_name(current_item(m)));
2516 (void) addstr("Press any key to continue...");
2523 for (ip = items; *ip; ip++)
2525 #ifdef NCURSES_MOUSE_VERSION
2526 mousemask(0, (mmask_t *) 0);
2531 #define T_TBL(name) { #name, name }
2537 T_TBL(TRACE_DISABLE),
2540 T_TBL(TRACE_UPDATE),
2542 T_TBL(TRACE_CHARPUT),
2543 T_TBL(TRACE_ORDINARY),
2545 T_TBL(TRACE_VIRTPUT),
2546 T_TBL(TRACE_IEVENT),
2548 T_TBL(TRACE_ICALLS),
2549 T_TBL(TRACE_CCALLS),
2550 T_TBL(TRACE_DATABASE),
2551 T_TBL(TRACE_MAXIMUM),
2558 tracetrace(int tlevel)
2565 for (n = 0; t_tbl[n].name != 0; n++)
2566 need += strlen(t_tbl[n].name) + 2;
2567 buf = (char *) malloc(need);
2569 sprintf(buf, "0x%02x = {", tlevel);
2571 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
2573 for (n = 1; t_tbl[n].name != 0; n++)
2574 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
2575 strcat(buf, t_tbl[n].name);
2579 if (buf[strlen(buf) - 2] == ',')
2580 buf[strlen(buf) - 2] = '\0';
2581 return (strcat(buf, "}"));
2584 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
2588 run_trace_menu(MENU * m)
2594 bool changed = FALSE;
2595 switch (menu_driver(m, menu_virtualize(wGetchar(menu_win(m))))) {
2596 case E_UNKNOWN_COMMAND:
2599 items = menu_items(m);
2600 i = current_item(m);
2601 if (i == items[0]) {
2602 if (item_value(i)) {
2603 for (p = items + 1; *p != 0; p++)
2604 if (item_value(*p)) {
2605 set_item_value(*p, FALSE);
2610 for (p = items + 1; *p != 0; p++)
2611 if (item_value(*p)) {
2612 set_item_value(items[0], FALSE);
2625 /* interactively set the trace level */
2628 ITEM *items[SIZEOF(t_tbl)];
2630 int mrows, mcols, newtrace;
2634 mvaddstr(0, 0, "Interactively set trace level:");
2635 mvaddstr(2, 0, " Press space bar to toggle a selection.");
2636 mvaddstr(3, 0, " Use up and down arrow to move the select bar.");
2637 mvaddstr(4, 0, " Press return to set the trace level.");
2638 mvprintw(6, 0, "(Current trace level is %s)", tracetrace(_nc_tracing));
2642 for (n = 0; t_tbl[n].name != 0; n++)
2643 *ip++ = new_item(t_tbl[n].name, "");
2646 m = new_menu(items);
2648 set_menu_format(m, 0, 2);
2649 scale_menu(m, &mrows, &mcols);
2651 menu_opts_off(m, O_ONEVALUE);
2652 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
2653 set_menu_win(m, menuwin);
2654 keypad(menuwin, TRUE);
2657 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
2661 for (ip = menu_items(m); *ip; ip++) {
2662 int mask = t_tbl[item_index(*ip)].mask;
2664 set_item_value(*ip, _nc_tracing == 0);
2665 else if ((mask & _nc_tracing) == mask)
2666 set_item_value(*ip, TRUE);
2669 while (run_trace_menu(m))
2673 for (ip = menu_items(m); *ip; ip++)
2674 if (item_value(*ip))
2675 newtrace |= t_tbl[item_index(*ip)].mask;
2677 _tracef("trace level interactively set to %s", tracetrace(_nc_tracing));
2679 (void) mvprintw(LINES - 2, 0,
2680 "Trace level is %s\n", tracetrace(_nc_tracing));
2681 (void) addstr("Press any key to continue...");
2688 for (ip = items; *ip; ip++)
2692 #endif /* USE_LIBMENU */
2694 /****************************************************************************
2698 ****************************************************************************/
2701 make_label(int frow, int fcol, NCURSES_CONST char *label)
2703 FIELD *f = new_field(1, strlen(label), frow, fcol, 0, 0);
2706 set_field_buffer(f, 0, label);
2707 set_field_opts(f, field_opts(f) & ~O_ACTIVE);
2713 make_field(int frow, int fcol, int rows, int cols, bool secure)
2715 FIELD *f = new_field(rows, cols, frow, fcol, 0, secure ? 1 : 0);
2718 set_field_back(f, A_UNDERLINE);
2719 set_field_userptr(f, (void *) 0);
2725 display_form(FORM * f)
2730 scale_form(f, &rows, &cols);
2732 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
2734 set_form_sub(f, derwin(w, rows, cols, 1, 2));
2739 if (post_form(f) != E_OK)
2744 erase_form(FORM * f)
2746 WINDOW *w = form_win(f);
2747 WINDOW *s = form_sub(f);
2757 edit_secure(FIELD * me, int c)
2759 int rows, cols, frow, fcol, nrow, nbuf;
2761 if (field_info(me, &rows, &cols, &frow, &fcol, &nrow, &nbuf) == E_OK
2766 strcpy(temp, field_buffer(me, 1));
2767 len = (long) (char *) field_userptr(me);
2772 set_field_buffer(me, 1, temp);
2792 case REQ_RIGHT_CHAR:
2794 c = 0; /* we don't want to do inline editing */
2799 set_field_buffer(me, 1, temp);
2806 set_field_buffer(me, 1, temp);
2811 set_field_userptr(me, (void *) len);
2817 form_virtualize(FORM * f, WINDOW *w)
2819 static const struct {
2824 CTRL('A'), REQ_NEXT_CHOICE
2827 CTRL('B'), REQ_PREV_WORD
2830 CTRL('C'), REQ_CLR_EOL
2833 CTRL('D'), REQ_DOWN_FIELD
2836 CTRL('E'), REQ_END_FIELD
2839 CTRL('F'), REQ_NEXT_PAGE
2842 CTRL('G'), REQ_DEL_WORD
2845 CTRL('H'), REQ_DEL_PREV
2848 CTRL('I'), REQ_INS_CHAR
2851 CTRL('K'), REQ_CLR_EOF
2854 CTRL('L'), REQ_LEFT_FIELD
2857 CTRL('M'), REQ_NEW_LINE
2860 CTRL('N'), REQ_NEXT_FIELD
2863 CTRL('O'), REQ_INS_LINE
2866 CTRL('P'), REQ_PREV_FIELD
2869 CTRL('R'), REQ_RIGHT_FIELD
2872 CTRL('S'), REQ_BEG_FIELD
2875 CTRL('U'), REQ_UP_FIELD
2878 CTRL('V'), REQ_DEL_CHAR
2881 CTRL('W'), REQ_NEXT_WORD
2884 CTRL('X'), REQ_CLR_FIELD
2887 CTRL('Y'), REQ_DEL_LINE
2890 CTRL('Z'), REQ_PREV_CHOICE
2893 ESCAPE, MAX_FORM_COMMAND + 1
2896 KEY_BACKSPACE, REQ_DEL_PREV
2899 KEY_DOWN, REQ_DOWN_CHAR
2902 KEY_END, REQ_LAST_FIELD
2905 KEY_HOME, REQ_FIRST_FIELD
2908 KEY_LEFT, REQ_LEFT_CHAR
2911 KEY_LL, REQ_LAST_FIELD
2914 KEY_NEXT, REQ_NEXT_FIELD
2917 KEY_NPAGE, REQ_NEXT_PAGE
2920 KEY_PPAGE, REQ_PREV_PAGE
2923 KEY_PREVIOUS, REQ_PREV_FIELD
2926 KEY_RIGHT, REQ_RIGHT_CHAR
2932 QUIT, MAX_FORM_COMMAND + 1
2936 static int mode = REQ_INS_MODE;
2937 int c = wGetchar(w);
2939 FIELD *me = current_field(f);
2941 if (c == CTRL(']')) {
2942 if (mode == REQ_INS_MODE)
2943 mode = REQ_OVL_MODE;
2945 mode = REQ_INS_MODE;
2948 for (n = 0; n < sizeof(lookup) / sizeof(lookup[0]); n++) {
2949 if (lookup[n].code == c) {
2950 c = lookup[n].result;
2957 * Force the field that the user is typing into to be in reverse video,
2958 * while the other fields are shown underlined.
2961 c = edit_secure(me, c);
2962 set_field_back(me, A_REVERSE);
2963 } else if (c <= MAX_FORM_COMMAND) {
2964 c = edit_secure(me, c);
2965 set_field_back(me, A_UNDERLINE);
2971 my_form_driver(FORM * form, int c)
2973 if (c == (MAX_FORM_COMMAND + 1)
2974 && form_driver(form, REQ_VALIDATION) == E_OK)
2987 FIELD *f[12], *secure;
2988 int finished = 0, c;
2992 addstr("Defined form-traversal keys: ^Q/ESC- exit form\n");
2993 addstr("^N -- go to next field ^P -- go to previous field\n");
2994 addstr("Home -- go to first field End -- go to last field\n");
2995 addstr("^L -- go to field to left ^R -- go to field to right\n");
2996 addstr("^U -- move upward to field ^D -- move downward to field\n");
2997 addstr("^W -- go to next word ^B -- go to previous word\n");
2998 addstr("^S -- go to start of field ^E -- go to end of field\n");
2999 addstr("^H -- delete previous char ^Y -- delete line\n");
3000 addstr("^G -- delete current word ^C -- clear to end of line\n");
3001 addstr("^K -- clear to end of field ^X -- clear field\n");
3002 addstr("Arrow keys move within a field as you would expect.");
3004 mvaddstr(4, 57, "Forms Entry Test");
3008 /* describe the form */
3009 f[n++] = make_label(0, 15, "Sample Form");
3010 f[n++] = make_label(2, 0, "Last Name");
3011 f[n++] = make_field(3, 0, 1, 18, FALSE);
3012 f[n++] = make_label(2, 20, "First Name");
3013 f[n++] = make_field(3, 20, 1, 12, FALSE);
3014 f[n++] = make_label(2, 34, "Middle Name");
3015 f[n++] = make_field(3, 34, 1, 12, FALSE);
3016 f[n++] = make_label(5, 0, "Comments");
3017 f[n++] = make_field(6, 0, 4, 46, FALSE);
3018 f[n++] = make_label(5, 20, "Password:");
3020 f[n++] = make_field(5, 30, 1, 9, TRUE);
3021 f[n++] = (FIELD *) 0;
3029 nonl(); /* lets us read ^M's */
3031 switch (form_driver(form, c = form_virtualize(form, w))) {
3033 mvaddstr(5, 57, field_buffer(secure, 1));
3037 case E_UNKNOWN_COMMAND:
3038 finished = my_form_driver(form, c);
3049 for (c = 0; f[c] != 0; c++)
3054 #endif /* USE_LIBFORM */
3056 /****************************************************************************
3060 ****************************************************************************/
3063 fillwin(WINDOW *win, char ch)
3068 getmaxyx(win, y1, x1);
3069 for (y = 0; y < y1; y++) {
3071 for (x = 0; x < x1; x++)
3077 crosswin(WINDOW *win, char ch)
3082 getmaxyx(win, y1, x1);
3083 for (y = 0; y < y1; y++) {
3084 for (x = 0; x < x1; x++)
3085 if (((x > (x1 - 1) / 3) && (x <= (2 * (x1 - 1)) / 3))
3086 || (((y > (y1 - 1) / 3) && (y <= (2 * (y1 - 1)) / 3)))) {
3095 /* test effects of overlapping windows */
3099 WINDOW *win1 = newwin(9, 20, 3, 3);
3100 WINDOW *win2 = newwin(9, 20, 9, 16);
3105 printw("This test shows the behavior of wnoutrefresh() with respect to\n");
3106 printw("the shared region of two overlapping windows A and B. The cross\n");
3107 printw("pattern in each window does not overlap the other.\n");
3110 printw("a = refresh A, then B, then doupdate. b = refresh B, then A, then doupdaute\n");
3111 printw("c = fill window A with letter A. d = fill window B with letter B.\n");
3112 printw("e = cross pattern in window A. f = cross pattern in window B.\n");
3113 printw("g = clear window A. h = clear window B.\n");
3114 printw("i = overwrite A onto B. j = overwrite B onto A.\n");
3115 printw("^Q/ESC = terminate test.");
3117 while ((ch = Getchar()) != QUIT && ch != ESCAPE)
3119 case 'a': /* refresh window A first, then B */
3125 case 'b': /* refresh window B first, then A */
3131 case 'c': /* fill window A so it's visible */
3135 case 'd': /* fill window B so it's visible */
3139 case 'e': /* cross test pattern in window A */
3140 crosswin(win1, 'A');
3143 case 'f': /* cross test pattern in window A */
3144 crosswin(win2, 'B');
3147 case 'g': /* clear window A */
3152 case 'h': /* clear window B */
3157 case 'i': /* overwrite A onto B */
3158 overwrite(win1, win2);
3161 case 'j': /* overwrite B onto A */
3162 overwrite(win2, win1);
3172 /****************************************************************************
3176 ****************************************************************************/
3179 do_single_test(const char c)
3180 /* perform a single specified test */
3193 Cannot("does not support color.");
3200 Cannot("does not support color.");
3201 else if (!can_change_color())
3202 Cannot("has hardwired color values.");
3226 flushinp_test(stdscr);
3230 test_sgr_attributes();
3255 #if USE_LIBMENU && defined(TRACE)
3274 static const char *const tbl[] =
3276 "Usage: ncurses [options]"
3279 #ifdef NCURSES_VERSION
3280 ," -a f,b set default-colors (assumed white-on-black)"
3281 ," -d use default-colors if terminal supports them"
3283 ," -e fmt specify format for soft-keys test (e)"
3284 ," -f rip-off footer line (can repeat)"
3285 ," -h rip-off header line (can repeat)"
3286 ," -s msec specify nominal time for panel-demo (default: 1, to hold)"
3288 ," -t mask specify default trace-level (may toggle with ^T)"
3292 for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); n++)
3293 fprintf(stderr, "%s\n", tbl[n]);
3298 set_terminal_modes(void)
3303 scrollok(stdscr, TRUE);
3304 idlok(stdscr, TRUE);
3305 keypad(stdscr, TRUE);
3310 announce_sig(int sig)
3312 (void) fprintf(stderr, "Handled signal %d\r\n", sig);
3317 rip_footer(WINDOW *win, int columns)
3319 wbkgd(win, A_REVERSE);
3322 wprintw(win, "footer: %d columns", columns);
3328 rip_header(WINDOW *win, int columns)
3330 wbkgd(win, A_REVERSE);
3333 wprintw(win, "header: %d columns", columns);
3338 /*+-------------------------------------------------------------------------
3340 --------------------------------------------------------------------------*/
3343 main(int argc, char *argv[])
3347 #ifdef NCURSES_VERSION
3348 int default_fg = COLOR_WHITE;
3349 int default_bg = COLOR_BLACK;
3350 bool assumed_colors = FALSE;
3351 bool default_colors = FALSE;
3355 setlocale(LC_CTYPE, "");
3358 while ((c = getopt(argc, argv, "a:de:fhs:t:")) != EOF) {
3360 #ifdef NCURSES_VERSION
3362 assumed_colors = TRUE;
3363 sscanf(optarg, "%d,%d", &default_fg, &default_bg);
3366 default_colors = TRUE;
3370 my_e_param = atoi(optarg);
3371 #ifdef NCURSES_VERSION
3372 if (my_e_param > 3) /* allow extended layouts */
3380 ripoffline(-1, rip_footer);
3383 ripoffline(1, rip_header);
3387 nap_msec = atol(optarg);
3392 save_trace = atoi(optarg);
3401 * If there's no menus (unlikely for ncurses!), then we'll have to set
3402 * tracing on initially, just in case the user wants to test something that
3403 * doesn't involve wGetchar.
3406 /* enable debugging */
3410 if (!isatty(fileno(stdin)))
3412 #endif /* USE_LIBMENU */
3415 /* tell it we're going to play with soft keys */
3416 slk_init(my_e_param);
3419 /* set up null signal catcher so we can see what interrupts to getch do */
3420 signal(SIGUSR1, announce_sig);
3423 /* we must initialize the curses data structure only once */
3427 /* tests, in general, will want these modes */
3430 #ifdef NCURSES_VERSION
3432 use_default_colors();
3433 else if (assumed_colors)
3434 assume_default_colors(default_fg, default_bg);
3437 set_terminal_modes();
3441 * Return to terminal mode, so we're guaranteed of being able to
3442 * select terminal commands even if the capabilities are wrong.
3446 #if HAVE_CURSES_VERSION
3447 (void) printf("Welcome to %s. Press ? for help.\n", curses_version());
3448 #elif defined(NCURSES_VERSION_MAJOR) && defined(NCURSES_VERSION_MINOR) && defined(NCURSES_VERSION_PATCH)
3449 (void) printf("Welcome to ncurses %d.%d.%d. Press ? for help.\n",
3450 NCURSES_VERSION_MAJOR,
3451 NCURSES_VERSION_MINOR,
3452 NCURSES_VERSION_PATCH);
3454 (void) puts("Welcome to ncurses. Press ? for help.");
3458 (void) puts("This is the ncurses main menu");
3459 (void) puts("a = keyboard and mouse input test");
3460 (void) puts("b = character attribute test");
3461 (void) puts("c = color test pattern");
3462 (void) puts("d = edit RGB color values");
3463 (void) puts("e = exercise soft keys");
3464 (void) puts("f = display ACS characters");
3465 (void) puts("g = display windows and scrolling");
3466 (void) puts("i = test of flushinp()");
3467 (void) puts("k = display character attributes");
3469 (void) puts("m = menu code test");
3472 (void) puts("o = exercise panels library");
3473 (void) puts("p = exercise pad features");
3474 (void) puts("q = quit");
3477 (void) puts("r = exercise forms code");
3479 (void) puts("s = overlapping-refresh test");
3480 #if USE_LIBMENU && defined(TRACE)
3481 (void) puts("t = set trace level");
3483 (void) puts("? = repeat this command summary");
3485 (void) fputs("> ", stdout);
3486 (void) fflush(stdout); /* necessary under SVr4 curses */
3489 * This used to be an 'fgets()' call. However (on Linux, at least)
3490 * mixing stream I/O and 'read()' (used in the library) causes the
3491 * input stream to be flushed when switching between the two.
3496 if (read(fileno(stdin), &ch, 1) <= 0) {
3500 } else if (command == 0 && !isspace(ch)) {
3502 } else if (ch == '\n' || ch == '\r') {
3505 (void) fputs("> ", stdout);
3506 (void) fflush(stdout);
3510 if (do_single_test(command)) {
3512 * This may be overkill; it's intended to reset everything back
3513 * to the initial terminal modes so that tests don't get in
3517 set_terminal_modes();
3522 if (command == '?') {
3523 (void) puts("This is the ncurses capability tester.");
3525 puts("You may select a test from the main menu by typing the");
3527 puts("key letter of the choice (the letter to left of the =)");
3529 puts("at the > prompt. The commands `x' or `q' will exit.");
3536 ExitProgram(EXIT_SUCCESS);
3539 /* ncurses.c ends here */