1 /****************************************************************************
2 * Copyright (c) 1998,1999 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.120 1999/10/23 20:01:30 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.
125 static void do_h_line(int y, int x, chtype c, int to)
128 mvhline(y, x, c, (to) - (x));
131 static void do_v_line(int y, int x, chtype c, int to)
134 mvvline(y, x, c, (to) - (y));
137 /* Common function to allow ^T to toggle trace-mode in the middle of a test
138 * so that trace-files can be made smaller.
140 static int wGetchar(WINDOW *win)
144 while ((c = wgetch(win)) == CTRL('T')) {
146 save_trace = _nc_tracing;
147 _tracef("TOGGLE-TRACING OFF");
150 _nc_tracing = save_trace;
154 _tracef("TOGGLE-TRACING ON");
161 #define Getchar() wGetchar(stdscr)
163 static void Pause(void)
166 addstr("Press any key to continue... ");
170 static void Cannot(const char *what)
172 printw("\nThis %s terminal %s\n\n", getenv("TERM"), what);
176 static void ShellOut(bool message)
179 addstr("Shelling out...");
184 addstr("returned from shellout.\n");
188 #ifdef NCURSES_MOUSE_VERSION
189 static const char *mouse_decode(MEVENT const *ep)
193 (void) sprintf(buf, "id %2d at (%2d, %2d, %2d) state %4lx = {",
194 ep->id, ep->x, ep->y, ep->z, ep->bstate);
196 #define SHOW(m, s) if ((ep->bstate & m)==m) {strcat(buf,s); strcat(buf, ", ");}
197 SHOW(BUTTON1_RELEASED, "release-1")
198 SHOW(BUTTON1_PRESSED, "press-1")
199 SHOW(BUTTON1_CLICKED, "click-1")
200 SHOW(BUTTON1_DOUBLE_CLICKED, "doubleclick-1")
201 SHOW(BUTTON1_TRIPLE_CLICKED, "tripleclick-1")
202 SHOW(BUTTON1_RESERVED_EVENT, "reserved-1")
203 SHOW(BUTTON2_RELEASED, "release-2")
204 SHOW(BUTTON2_PRESSED, "press-2")
205 SHOW(BUTTON2_CLICKED, "click-2")
206 SHOW(BUTTON2_DOUBLE_CLICKED, "doubleclick-2")
207 SHOW(BUTTON2_TRIPLE_CLICKED, "tripleclick-2")
208 SHOW(BUTTON2_RESERVED_EVENT, "reserved-2")
209 SHOW(BUTTON3_RELEASED, "release-3")
210 SHOW(BUTTON3_PRESSED, "press-3")
211 SHOW(BUTTON3_CLICKED, "click-3")
212 SHOW(BUTTON3_DOUBLE_CLICKED, "doubleclick-3")
213 SHOW(BUTTON3_TRIPLE_CLICKED, "tripleclick-3")
214 SHOW(BUTTON3_RESERVED_EVENT, "reserved-3")
215 SHOW(BUTTON4_RELEASED, "release-4")
216 SHOW(BUTTON4_PRESSED, "press-4")
217 SHOW(BUTTON4_CLICKED, "click-4")
218 SHOW(BUTTON4_DOUBLE_CLICKED, "doubleclick-4")
219 SHOW(BUTTON4_TRIPLE_CLICKED, "tripleclick-4")
220 SHOW(BUTTON4_RESERVED_EVENT, "reserved-4")
221 SHOW(BUTTON_CTRL, "ctrl")
222 SHOW(BUTTON_SHIFT, "shift")
223 SHOW(BUTTON_ALT, "alt")
224 SHOW(ALL_MOUSE_EVENTS, "all-events")
225 SHOW(REPORT_MOUSE_POSITION, "position")
228 if (buf[strlen(buf)-1] == ' ')
229 buf[strlen(buf)-2] = '\0';
230 (void) strcat(buf, "}");
233 #endif /* NCURSES_MOUSE_VERSION */
235 /****************************************************************************
237 * Character input test
239 ****************************************************************************/
241 static void getch_test(void)
242 /* test the keypad feature */
246 int incount = 0, firsttime = 0;
247 bool blocking = TRUE;
252 #ifdef NCURSES_MOUSE_VERSION
253 mousemask(ALL_MOUSE_EVENTS, (mmask_t *)0);
256 (void) printw("Delay in 10ths of a second (<CR> for blocking input)? ");
264 timeout(atoi(buf) * 100);
274 printw("Key pressed: %04o ", c);
275 #ifdef NCURSES_MOUSE_VERSION
281 printw("KEY_MOUSE, %s\n", mouse_decode(&event));
284 #endif /* NCURSES_MOUSE_VERSION */
287 (void) addstr(keyname(c));
294 (void) printw("M-%c", c2);
296 (void) printw("M-%s", unctrl(c2));
297 addstr(" (high-half character)\n");
302 (void) printw("%c (ASCII printable character)\n", c);
304 (void) printw("%s (ASCII control character)\n", unctrl(c));
314 addstr("getstr test: ");
315 echo(); getstr(buf); noecho();
316 printw("I saw `%s'.\n", buf);
322 if (c == 'x' || c == 'q' || (c == ERR && blocking))
326 addstr("Type any key to see its keypad value. Also:\n");
327 addstr("g -- triggers a getstr test\n");
328 addstr("s -- shell out\n");
329 addstr("q -- quit\n");
330 addstr("? -- repeats this help message\n");
333 while ((c = Getchar()) == ERR)
335 (void) printw("%05d: input timed out\n", incount++);
337 (void) printw("%05d: input error\n", incount++);
342 #ifdef NCURSES_MOUSE_VERSION
343 mousemask(0, (mmask_t *)0);
352 static int show_attr(int row, int skip, chtype attr, const char *name, bool once)
354 int ncv = tigetnum("ncv");
356 mvprintw(row, 8, "%s mode:", name);
357 mvprintw(row, 24, "|");
358 if (skip) printw("%*s", skip, " ");
363 addstr("abcde fghij klmno pqrst uvwxy z");
366 if (skip) 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[] = {
385 for (n = 0; n < sizeof(table)/sizeof(table[0]); n++) {
386 if ((table[n] & attr) != 0
387 && ((1 << n) & ncv) != 0) {
399 static bool attr_getc(int *skip, int *fg, int *bg)
406 } else if (ch == CTRL('L')) {
410 } else if (has_colors()) {
412 case 'f': *fg = (*fg + 1); break;
413 case 'F': *fg = (*fg - 1); break;
414 case 'b': *bg = (*bg + 1); break;
415 case 'B': *bg = (*bg - 1); break;
419 if (*fg >= COLORS) *fg = 0;
420 if (*fg < 0) *fg = COLORS - 1;
421 if (*bg >= COLORS) *bg = 0;
422 if (*bg < 0) *bg = COLORS - 1;
428 static void attr_test(void)
429 /* test text attributes */
432 int skip = tigetnum("xmc");
433 int fg = COLOR_BLACK; /* color pair 0 is special */
434 int bg = COLOR_BLACK;
435 bool *pairs = (bool *)calloc(COLOR_PAIRS, sizeof(bool));
441 n = skip; /* make it easy */
445 int normal = A_NORMAL | BLANK;
448 int pair = (fg * COLORS) + bg;
450 init_pair(pair, fg, bg);
453 normal |= COLOR_PAIR(pair);
458 mvaddstr(0, 20, "Character attribute test display");
460 row = show_attr(row, n, A_STANDOUT, "STANDOUT", TRUE);
461 row = show_attr(row, n, A_REVERSE, "REVERSE", TRUE);
462 row = show_attr(row, n, A_BOLD, "BOLD", TRUE);
463 row = show_attr(row, n, A_UNDERLINE, "UNDERLINE", TRUE);
464 row = show_attr(row, n, A_DIM, "DIM", TRUE);
465 row = show_attr(row, n, A_BLINK, "BLINK", TRUE);
466 row = show_attr(row, n, A_PROTECT, "PROTECT", TRUE);
467 row = show_attr(row, n, A_INVIS, "INVISIBLE", TRUE);
468 row = show_attr(row, n, A_NORMAL, "NORMAL", FALSE);
471 "This terminal does %shave the magic-cookie glitch",
472 tigetnum("xmc") > -1 ? "" : "not ");
474 "Enter a digit to set gaps on each side of displayed attributes");
478 printw(". f/F/b/F toggle colors (now %d/%d)", fg, bg);
481 } while (attr_getc(&n, &fg, &bg));
484 bkgdset(A_NORMAL | BLANK);
489 /****************************************************************************
491 * Color support tests
493 ****************************************************************************/
495 static NCURSES_CONST char *color_names[] =
507 static void show_color_name(int y, int x, int color)
510 mvprintw(y, x, "%02d ", color);
512 mvaddstr(y, x, color_names[color]);
515 static void color_test(void)
516 /* generate a color test pattern */
519 int base, top, width;
520 NCURSES_CONST char *hello;
523 (void) printw("There are %d color pairs\n", COLOR_PAIRS);
525 width = (COLORS > 8) ? 4 : 8;
526 hello = (COLORS > 8) ? "Test" : "Hello";
528 for (base = 0; base < 2; base++)
530 top = (COLORS > 8) ? 0 : base * (COLORS+3);
532 (void) mvprintw(top + 1, 0,
533 "%dx%d matrix of foreground/background colors, bright *%s*\n",
535 base ? "on" : "off");
536 for (i = 0; i < COLORS; i++)
537 show_color_name(top + 2, (i+1) * width, i);
538 for (i = 0; i < COLORS; i++)
539 show_color_name(top + 3 + i, 0, i);
540 for (i = 1; i < COLOR_PAIRS; i++)
542 init_pair(i, i % COLORS, i / COLORS);
543 attron((attr_t)COLOR_PAIR(i));
545 attron((attr_t)A_BOLD);
546 mvaddstr(top + 3 + (i / COLORS), (i % COLORS + 1) * width, hello);
549 if ((COLORS > 8) || base)
557 static void change_color(int current, int field, int value, int usebase)
559 short red, green, blue;
562 color_content(current, &red, &green, &blue);
564 red = green = blue = 0;
578 if (init_color(current, red, green, blue) == ERR)
582 static void color_edit(void)
583 /* display the color test pattern, without trying to edit colors */
585 int i, this_c = 0, value = 0, current = 0, field = 0;
590 for (i = 0; i < COLORS; i++)
591 init_pair(i, COLOR_WHITE, i);
593 mvprintw(LINES-2, 0, "Number: %d", value);
596 short red, green, blue;
599 mvaddstr(0, 20, "Color RGB Value Editing");
602 for (i = 0; i < COLORS; i++)
604 mvprintw(2 + i, 0, "%c %-8s:",
605 (i == current ? '>' : ' '),
606 (i < (int) SIZEOF(color_names)
607 ? color_names[i] : ""));
608 attrset(COLOR_PAIR(i));
613 * Note: this refresh should *not* be necessary! It works around
614 * a bug in attribute handling that apparently causes the A_NORMAL
615 * attribute sets to interfere with the actual emission of the
616 * color setting somehow. This needs to be fixed.
620 color_content(i, &red, &green, &blue);
622 if (current == i && field == 0) attron(A_STANDOUT);
624 if (current == i && field == 0) attrset(A_NORMAL);
626 if (current == i && field == 1) attron(A_STANDOUT);
627 printw("%04d", green);
628 if (current == i && field == 1) attrset(A_NORMAL);
630 if (current == i && field == 2) attron(A_STANDOUT);
631 printw("%04d", blue);
632 if (current == i && field == 2) attrset(A_NORMAL);
637 mvaddstr(COLORS + 3, 0,
638 "Use up/down to select a color, left/right to change fields.");
639 mvaddstr(COLORS + 4, 0,
640 "Modify field by typing nnn=, nnn-, or nnn+. ? for help.");
642 move(2 + current, 0);
646 if (isdigit(this_c) && !isdigit(last_c))
652 current = (current == 0 ? (COLORS - 1) : current - 1);
656 current = (current == (COLORS - 1) ? 0 : current + 1);
660 field = (field == 2 ? 0 : field + 1);
664 field = (field == 0 ? 2 : field - 1);
667 case '0': case '1': case '2': case '3': case '4':
668 case '5': case '6': case '7': case '8': case '9':
669 value = value * 10 + (this_c - '0');
673 change_color(current, field, value, 1);
677 change_color(current, field, -value, 1);
681 change_color(current, field, value, 0);
686 P(" RGB Value Editing Help");
688 P("You are in the RGB value editor. Use the arrow keys to select one of");
689 P("the fields in one of the RGB triples of the current colors; the one");
690 P("currently selected will be reverse-video highlighted.");
692 P("To change a field, enter the digits of the new value; they are echoed");
693 P("as entered. Finish by typing `='. The change will take effect instantly.");
694 P("To increment or decrement a value, use the same procedure, but finish");
695 P("with a `+' or `-'.");
697 P("To quit, do `x' or 'q'");
711 mvprintw(LINES-2, 0, "Number: %d", value);
714 (this_c != 'x' && this_c != 'q');
720 /****************************************************************************
722 * Soft-key label test
724 ****************************************************************************/
726 static void slk_test(void)
727 /* exercise the soft keys */
740 mvaddstr(0, 20, "Soft Key Exerciser");
744 P("Available commands are:");
746 P("^L -- refresh screen");
747 P("a -- activate or restore soft keys");
748 P("d -- disable soft keys");
749 P("c -- set centered format for labels");
750 P("l -- set left-justified format for labels");
751 P("r -- set right-justified format for labels");
752 P("[12345678] -- set label; labels are numbered 1 through 8");
753 P("e -- erase stdscr (should not erase labels)");
754 P("s -- test scrolling of shortened screen");
755 P("x, q -- return to main menu");
757 P("Note: if activating the soft keys causes your terminal to");
758 P("scroll up one line, your terminal auto-scrolls when anything");
759 P("is written to the last screen position. The ncurses code");
760 P("does not yet handle this gracefully.");
773 mvprintw(20, 0, "Press Q to stop the scrolling-test: ");
774 while ((c = Getchar()) != 'Q' && (c != ERR))
794 case '1': case '2': case '3': case '4':
795 case '5': case '6': case '7': case '8':
796 (void) mvaddstr(20, 0, "Please enter the label value: ");
798 wgetnstr(stdscr, buf, 8);
800 slk_set((c - '0'), buf, fmt);
802 move(20, 0); clrtoeol();
813 ((c = Getchar()) != EOF);
820 /****************************************************************************
822 * Alternate character-set stuff
824 ****************************************************************************/
826 /* ISO 6429: codes 0x80 to 0x9f may be control characters that cause the
827 * terminal to perform functions. The remaining codes can be graphic.
829 static void show_upper_chars(int first)
831 bool C1 = (first == 128);
833 int last = first + 31;
838 mvprintw(0, 20, "Display of %s Character Codes %d to %d",
839 C1 ? "C1" : "GR", first, last);
843 for (code = first; code <= last; code++) {
844 int row = 4 + ((code - first) % 16);
845 int col = ((code - first) / 16) * COLS / 2;
847 sprintf(tmp, "%3d (0x%x)", code, code);
848 mvprintw(row, col, "%*s: ", COLS/4, tmp);
850 nodelay(stdscr, TRUE);
853 /* (yes, this _is_ crude) */
854 while ((reply = Getchar()) != ERR) {
858 nodelay(stdscr, FALSE);
863 static int show_1_acs(int n, const char *name, chtype code)
865 const int height = 16;
866 int row = 4 + (n % height);
867 int col = (n / height) * COLS / 2;
868 mvprintw(row, col, "%*s : ", COLS/4, name);
873 static void show_acs_chars(void)
874 /* display the ACS character set */
878 #define BOTH(name) #name, name
882 mvaddstr(0, 20, "Display of the ACS Character Set");
886 n = show_1_acs(0, BOTH(ACS_ULCORNER));
887 n = show_1_acs(n, BOTH(ACS_LLCORNER));
888 n = show_1_acs(n, BOTH(ACS_URCORNER));
889 n = show_1_acs(n, BOTH(ACS_LRCORNER));
890 n = show_1_acs(n, BOTH(ACS_RTEE));
891 n = show_1_acs(n, BOTH(ACS_LTEE));
892 n = show_1_acs(n, BOTH(ACS_BTEE));
893 n = show_1_acs(n, BOTH(ACS_TTEE));
894 n = show_1_acs(n, BOTH(ACS_HLINE));
895 n = show_1_acs(n, BOTH(ACS_VLINE));
896 n = show_1_acs(n, BOTH(ACS_PLUS));
897 n = show_1_acs(n, BOTH(ACS_S1));
898 n = show_1_acs(n, BOTH(ACS_S9));
899 n = show_1_acs(n, BOTH(ACS_DIAMOND));
900 n = show_1_acs(n, BOTH(ACS_CKBOARD));
901 n = show_1_acs(n, BOTH(ACS_DEGREE));
902 n = show_1_acs(n, BOTH(ACS_PLMINUS));
903 n = show_1_acs(n, BOTH(ACS_BULLET));
904 n = show_1_acs(n, BOTH(ACS_LARROW));
905 n = show_1_acs(n, BOTH(ACS_RARROW));
906 n = show_1_acs(n, BOTH(ACS_DARROW));
907 n = show_1_acs(n, BOTH(ACS_UARROW));
908 n = show_1_acs(n, BOTH(ACS_BOARD));
909 n = show_1_acs(n, BOTH(ACS_LANTERN));
910 n = show_1_acs(n, BOTH(ACS_BLOCK));
911 n = show_1_acs(n, BOTH(ACS_S3));
912 n = show_1_acs(n, BOTH(ACS_S7));
913 n = show_1_acs(n, BOTH(ACS_LEQUAL));
914 n = show_1_acs(n, BOTH(ACS_GEQUAL));
915 n = show_1_acs(n, BOTH(ACS_PI));
916 n = show_1_acs(n, BOTH(ACS_NEQUAL));
917 n = show_1_acs(n, BOTH(ACS_STERLING));
920 static void acs_display(void)
933 show_upper_chars((c - '0') * 32 + 128);
936 mvprintw(LINES-3,0, "Note: ANSI terminals may not display C1 characters.");
937 mvprintw(LINES-2,0, "Select: a=ACS, 0=C1, 1,2,3=GR characters, q=quit");
939 } while ((c = Getchar()) != 'x' && c != 'q');
947 * Graphic-rendition test (adapted from vttest)
950 test_sgr_attributes(void)
954 for (pass = 0; pass < 2; pass++) {
955 int normal = ((pass == 0 ? A_NORMAL : A_REVERSE)) | BLANK;
957 /* Use non-default colors if possible to exercise bce a little */
959 init_pair(1, COLOR_WHITE, COLOR_BLUE);
960 normal |= COLOR_PAIR(1);
964 mvprintw( 1,20, "Graphic rendition test pattern:");
966 mvprintw( 4, 1, "vanilla");
968 #define set_sgr(mask) bkgdset((normal^(mask)));
970 mvprintw( 4,40, "bold");
972 set_sgr(A_UNDERLINE);
973 mvprintw( 6, 6, "underline");
975 set_sgr(A_BOLD|A_UNDERLINE);
976 mvprintw( 6,45, "bold underline");
979 mvprintw( 8, 1, "blink");
981 set_sgr(A_BLINK|A_BOLD);
982 mvprintw( 8,40, "bold blink");
984 set_sgr(A_UNDERLINE|A_BLINK);
985 mvprintw(10, 6, "underline blink");
987 set_sgr(A_BOLD|A_UNDERLINE|A_BLINK);
988 mvprintw(10,45, "bold underline blink");
991 mvprintw(12, 1, "negative");
993 set_sgr(A_BOLD|A_REVERSE);
994 mvprintw(12,40, "bold negative");
996 set_sgr(A_UNDERLINE|A_REVERSE);
997 mvprintw(14, 6, "underline negative");
999 set_sgr(A_BOLD|A_UNDERLINE|A_REVERSE);
1000 mvprintw(14,45, "bold underline negative");
1002 set_sgr(A_BLINK|A_REVERSE);
1003 mvprintw(16, 1, "blink negative");
1005 set_sgr(A_BOLD|A_BLINK|A_REVERSE);
1006 mvprintw(16,40, "bold blink negative");
1008 set_sgr(A_UNDERLINE|A_BLINK|A_REVERSE);
1009 mvprintw(18, 6, "underline blink negative");
1011 set_sgr(A_BOLD|A_UNDERLINE|A_BLINK|A_REVERSE);
1012 mvprintw(18,45, "bold underline blink negative");
1015 mvprintw(LINES-2,1, "%s background. ", pass == 0 ? "Dark" : "Light");
1020 bkgdset(A_NORMAL | BLANK);
1025 /****************************************************************************
1027 * Windows and scrolling tester.
1029 ****************************************************************************/
1031 #define BOTLINES 4 /* number of line stolen from screen bottom */
1039 #define FRAME struct frame
1048 /* We need to know if these flags are actually set, so don't look in FRAME.
1049 * These names are known to work with SVr4 curses as well as ncurses.
1051 static bool HaveKeypad(FRAME *curp)
1053 WINDOW *win = (curp ? curp->wind : stdscr);
1054 return win->_use_keypad;
1057 static bool HaveScroll(FRAME *curp)
1059 WINDOW *win = (curp ? curp->wind : stdscr);
1060 return win->_scroll;
1063 static void newwin_legend(FRAME *curp)
1065 static const struct {
1069 { "^C = create window", 0 },
1070 { "^N = next window", 0 },
1071 { "^P = previous window", 0 },
1072 { "^F = scroll forward", 0 },
1073 { "^B = scroll backward", 0 },
1074 { "^K = keypad(%s)", 1 },
1075 { "^S = scrollok(%s)", 2 },
1076 { "^W = save window to file", 0 },
1077 { "^R = restore window", 0 },
1078 #ifdef NCURSES_VERSION
1079 { "^X = resize", 0 },
1081 { "^Q%s = exit", 3 }
1085 bool do_keypad = HaveKeypad(curp);
1086 bool do_scroll = HaveScroll(curp);
1090 for (n = 0; n < SIZEOF(legend); n++) {
1091 switch (legend[n].code) {
1093 strcpy(buf, legend[n].msg);
1096 sprintf(buf, legend[n].msg, do_keypad ? "yes" : "no");
1099 sprintf(buf, legend[n].msg, do_scroll ? "yes" : "no");
1102 sprintf(buf, legend[n].msg, do_keypad ? "/ESC" : "");
1105 getyx(stdscr, y, x);
1106 addstr((COLS < (x + 3 + (int)strlen(buf))) ? "\n" : (n ? ", " : ""));
1112 static void transient(FRAME *curp, NCURSES_CONST char *msg)
1114 newwin_legend(curp);
1117 mvaddstr(LINES - 1, 0, msg);
1123 printw("%s characters are echoed, window should %sscroll.",
1124 HaveKeypad(curp) ? "Non-arrow" : "All other",
1125 HaveScroll(curp) ? "" : "not " );
1129 static void newwin_report(FRAME *curp)
1130 /* report on the cursor's current position, then restore it */
1132 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
1136 transient(curp, (char *)0);
1138 move(LINES - 1, COLS - 17);
1139 printw("Y = %2d X = %2d", y, x);
1146 static pair *selectcell(int uli, int ulj, int lri, int lrj)
1147 /* arrows keys move cursor, return location at current on non-arrow key */
1149 static pair res; /* result cell */
1150 int si = lri - uli + 1; /* depth of the select area */
1151 int sj = lrj - ulj + 1; /* width of the select area */
1152 int i = 0, j = 0; /* offsets into the select area */
1158 move(uli + i, ulj + j);
1159 newwin_report((FRAME *)0);
1163 case KEY_UP: i += si - 1; break;
1164 case KEY_DOWN: i++; break;
1165 case KEY_LEFT: j += sj - 1; break;
1166 case KEY_RIGHT: j++; break;
1168 case ESCAPE: return((pair *)0);
1169 #ifdef NCURSES_MOUSE_VERSION
1175 if (event.y > uli && event.x > ulj) {
1185 default: res.y = uli + i; res.x = ulj + j; return(&res);
1192 static void outerbox(pair ul, pair lr, bool onoff)
1193 /* draw or erase a box *outside* the given pair of corners */
1195 mvaddch(ul.y-1, lr.x-1, onoff ? ACS_ULCORNER : ' ');
1196 mvaddch(ul.y-1, lr.x+1, onoff ? ACS_URCORNER : ' ');
1197 mvaddch(lr.y+1, lr.x+1, onoff ? ACS_LRCORNER : ' ');
1198 mvaddch(lr.y+1, ul.x-1, onoff ? ACS_LLCORNER : ' ');
1199 move(ul.y-1, ul.x); hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
1200 move(ul.y, ul.x-1); vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
1201 move(lr.y+1, ul.x); hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
1202 move(ul.y, lr.x+1); vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
1205 static WINDOW *getwindow(void)
1206 /* Ask user for a window definition */
1211 move(0, 0); clrtoeol();
1212 addstr("Use arrows to move cursor, anything else to mark corner 1");
1214 if ((tmp = selectcell(2, 1, LINES-BOTLINES-2, COLS-2)) == (pair *)0)
1215 return((WINDOW *)0);
1216 memcpy(&ul, tmp, sizeof(pair));
1217 mvaddch(ul.y-1, ul.x-1, ACS_ULCORNER);
1218 move(0, 0); clrtoeol();
1219 addstr("Use arrows to move cursor, anything else to mark corner 2");
1221 if ((tmp = selectcell(ul.y, ul.x, LINES-BOTLINES-2, COLS-2)) == (pair *)0)
1222 return((WINDOW *)0);
1223 memcpy(&lr, tmp, sizeof(pair));
1225 rwindow = subwin(stdscr, lr.y - ul.y + 1, lr.x - ul.x + 1, ul.y, ul.x);
1227 outerbox(ul, lr, TRUE);
1232 move(0, 0); clrtoeol();
1236 static void newwin_move(FRAME *curp, int dy, int dx)
1238 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
1242 getyx(win, cur_y, cur_x);
1243 getmaxyx(win, max_y, max_x);
1244 if ((cur_x += dx) < 0)
1246 else if (cur_x >= max_x)
1248 if ((cur_y += dy) < 0)
1250 else if (cur_y >= max_y)
1252 wmove(win, cur_y, cur_x);
1255 static FRAME *delete_framed(FRAME *fp, bool showit)
1259 fp->last->next = fp->next;
1260 fp->next->last = fp->last;
1268 np = (fp == fp->next) ? 0 : fp->next;
1273 static void acs_and_scroll(void)
1274 /* Demonstrate windows */
1278 FRAME *current = (FRAME *)0, *neww;
1279 WINDOW *usescr = stdscr;
1281 #define DUMPFILE "screendump"
1283 #ifdef NCURSES_MOUSE_VERSION
1284 mousemask(BUTTON1_CLICKED, (mmask_t *)0);
1289 transient((FRAME *)0, (char *)0);
1293 neww = (FRAME *) calloc(1, sizeof(FRAME));
1294 if ((neww->wind = getwindow()) == (WINDOW *)0)
1297 if (current == 0) /* First element, */
1299 neww->next = neww; /* so point it at itself */
1304 neww->next = current->next;
1305 neww->last = current;
1306 neww->last->next = neww;
1307 neww->next->last = neww;
1310 /* SVr4 curses sets the keypad on all newly-created windows to
1311 * false. Someone reported that PDCurses makes new windows inherit
1312 * this flag. Remove the following 'keypad()' call to test this
1314 keypad(current->wind, TRUE);
1315 current->do_keypad = HaveKeypad(current);
1316 current->do_scroll = HaveScroll(current);
1319 case CTRL('N'): /* go to next window */
1321 current = current->next;
1324 case CTRL('P'): /* go to previous window */
1326 current = current->last;
1329 case CTRL('F'): /* scroll current window forward */
1331 wscrl(current->wind, 1);
1334 case CTRL('B'): /* scroll current window backwards */
1336 wscrl(current->wind, -1);
1339 case CTRL('K'): /* toggle keypad mode for current */
1341 current->do_keypad = !current->do_keypad;
1342 keypad(current->wind, current->do_keypad);
1348 current->do_scroll = !current->do_scroll;
1349 scrollok(current->wind, current->do_scroll);
1353 case CTRL('W'): /* save and delete window */
1354 if (current == current->next)
1356 if ((fp = fopen(DUMPFILE, "w")) == (FILE *)0)
1357 transient(current, "Can't open screen dump file");
1360 (void) putwin(current->wind, fp);
1363 current = delete_framed(current, TRUE);
1367 case CTRL('R'): /* restore window */
1368 if ((fp = fopen(DUMPFILE, "r")) == (FILE *)0)
1369 transient(current, "Can't open screen dump file");
1372 neww = (FRAME *) calloc(1, sizeof(FRAME));
1374 neww->next = current->next;
1375 neww->last = current;
1376 neww->last->next = neww;
1377 neww->next->last = neww;
1379 neww->wind = getwin(fp);
1382 wrefresh(neww->wind);
1386 #ifdef NCURSES_VERSION
1387 case CTRL('X'): /* resize window */
1393 move(0, 0); clrtoeol();
1394 addstr("Use arrows to move cursor, anything else to mark new corner");
1397 getbegyx(current->wind, ul.y, ul.x);
1399 tmp = selectcell(ul.y, ul.x, LINES-BOTLINES-2, COLS-2);
1400 if (tmp == (pair *)0)
1406 getmaxyx(current->wind, lr.y, lr.x);
1409 outerbox(ul, lr, FALSE);
1410 wnoutrefresh(stdscr);
1412 /* strictly cosmetic hack for the test */
1413 getmaxyx(current->wind, my, mx);
1414 if (my > tmp->y - ul.y)
1416 getyx(current->wind, lr.y, lr.x);
1417 wmove(current->wind, tmp->y - ul.y + 1, 0);
1418 wclrtobot(current->wind);
1419 wmove(current->wind, lr.y, lr.x);
1421 if (mx > tmp->x - ul.x)
1422 for (i = 0; i < my; i++)
1424 wmove(current->wind, i, tmp->x - ul.x + 1);
1425 wclrtoeol(current->wind);
1427 wnoutrefresh(current->wind);
1429 memcpy(&lr, tmp, sizeof(pair));
1430 (void) wresize(current->wind, lr.y-ul.y+0, lr.x-ul.x+0);
1432 getbegyx(current->wind, ul.y, ul.x);
1433 getmaxyx(current->wind, lr.y, lr.x);
1436 outerbox(ul, lr, TRUE);
1437 wnoutrefresh(stdscr);
1439 wnoutrefresh(current->wind);
1440 move(0, 0); clrtoeol();
1444 #endif /* NCURSES_VERSION */
1446 case KEY_F(10): /* undocumented --- use this to test area clears */
1447 selectcell(0, 0, LINES - 1, COLS - 1);
1453 newwin_move(current, -1, 0);
1456 newwin_move(current, 1, 0);
1459 newwin_move(current, 0, -1);
1462 newwin_move(current, 0, 1);
1470 getyx(current->wind, y, x);
1474 x = getmaxx(current->wind) - 1;
1476 mvwdelch(current->wind, y, x);
1486 waddch(current->wind, (chtype)c);
1491 newwin_report(current);
1492 usescr = (current ? current->wind : stdscr);
1495 ((c = wGetchar(usescr)) != QUIT
1496 && !((c == ESCAPE) && (usescr->_use_keypad))
1500 while (current != 0)
1501 current = delete_framed(current, FALSE);
1503 scrollok(stdscr, TRUE); /* reset to driver's default */
1504 #ifdef NCURSES_MOUSE_VERSION
1505 mousemask(0, (mmask_t *)0);
1512 /****************************************************************************
1516 ****************************************************************************/
1519 static unsigned long nap_msec = 1;
1521 static NCURSES_CONST char *mod[] =
1531 /*+-------------------------------------------------------------------------
1533 --------------------------------------------------------------------------*/
1535 wait_a_while(unsigned long msec GCC_UNUSED)
1545 else if(msec > 1000L)
1546 sleep((int)msec/1000L);
1550 } /* end of wait_a_while */
1552 /*+-------------------------------------------------------------------------
1554 --------------------------------------------------------------------------*/
1556 saywhat(NCURSES_CONST char *text)
1558 wmove(stdscr,LINES - 1,0);
1560 waddstr(stdscr, text);
1561 } /* end of saywhat */
1563 /*+-------------------------------------------------------------------------
1564 mkpanel(rows,cols,tly,tlx) - alloc a win and panel and associate them
1565 --------------------------------------------------------------------------*/
1567 mkpanel(int color, int rows, int cols, int tly, int tlx)
1572 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
1573 if ((pan = new_panel(win)) == 0) {
1575 } else if (has_colors()) {
1576 int fg = (color == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
1578 init_pair(color, fg, bg);
1579 wbkgdset(win, COLOR_PAIR(color) | ' ');
1581 wbkgdset(win, A_BOLD | ' ');
1585 } /* end of mkpanel */
1587 /*+-------------------------------------------------------------------------
1589 --------------------------------------------------------------------------*/
1593 WINDOW *win = panel_window(pan);
1596 } /* end of rmpanel */
1598 /*+-------------------------------------------------------------------------
1600 --------------------------------------------------------------------------*/
1606 } /* end of pflush */
1608 /*+-------------------------------------------------------------------------
1610 --------------------------------------------------------------------------*/
1612 fill_panel(PANEL *pan)
1614 WINDOW *win = panel_window(pan);
1615 int num = ((const char *)panel_userptr(pan))[1];
1619 wprintw(win,"-pan%c-", num);
1622 for(y = 2; y < getmaxy(win) - 1; y++)
1624 for(x = 1; x < getmaxx(win) - 1; x++)
1630 } /* end of fill_panel */
1632 static void demo_panels(void)
1639 for(y = 0; y < LINES - 1; y++)
1641 for(x = 0; x < COLS; x++)
1642 wprintw(stdscr,"%d",(y + x) % 10);
1644 for(y = 0; y < 5; y++)
1652 p1 = mkpanel(COLOR_RED, LINES/2 - 2, COLS/8 + 1, 0, 0);
1653 set_panel_userptr(p1,"p1");
1655 p2 = mkpanel(COLOR_GREEN, LINES/2 + 1, COLS/7, LINES/4, COLS/10);
1656 set_panel_userptr(p2,"p2");
1658 p3 = mkpanel(COLOR_YELLOW, LINES/4, COLS/10, LINES/2, COLS/9);
1659 set_panel_userptr(p3,"p3");
1661 p4 = mkpanel(COLOR_BLUE, LINES/2 - 2, COLS/8, LINES/2 - 2, COLS/3);
1662 set_panel_userptr(p4,"p4");
1664 p5 = mkpanel(COLOR_MAGENTA, LINES/2 - 2, COLS/8, LINES/2, COLS/2 - 2);
1665 set_panel_userptr(p5,"p5");
1675 saywhat("press any key to continue");
1676 wait_a_while(nap_msec);
1678 saywhat("h3 s1 s2 s4 s5; press any key to continue");
1686 wait_a_while(nap_msec);
1688 saywhat("s1; press any key to continue");
1691 wait_a_while(nap_msec);
1693 saywhat("s2; press any key to continue");
1696 wait_a_while(nap_msec);
1698 saywhat("m2; press any key to continue");
1699 move_panel(p2, LINES/3 + 1, COLS / 8);
1701 wait_a_while(nap_msec);
1706 wait_a_while(nap_msec);
1708 saywhat("m3; press any key to continue");
1709 move_panel(p3, LINES/4 + 1, COLS / 15);
1711 wait_a_while(nap_msec);
1713 saywhat("b3; press any key to continue");
1716 wait_a_while(nap_msec);
1718 saywhat("s4; press any key to continue");
1721 wait_a_while(nap_msec);
1723 saywhat("s5; press any key to continue");
1726 wait_a_while(nap_msec);
1728 saywhat("t3; press any key to continue");
1731 wait_a_while(nap_msec);
1733 saywhat("t1; press any key to continue");
1736 wait_a_while(nap_msec);
1738 saywhat("t2; press any key to continue");
1741 wait_a_while(nap_msec);
1743 saywhat("t3; press any key to continue");
1746 wait_a_while(nap_msec);
1748 saywhat("t4; press any key to continue");
1751 wait_a_while(nap_msec);
1753 for(itmp = 0; itmp < 6; itmp++)
1755 WINDOW *w4 = panel_window(p4);
1756 WINDOW *w5 = panel_window(p5);
1758 saywhat("m4; press any key to continue");
1759 wmove(w4, LINES/8, 1);
1760 waddstr(w4,mod[itmp]);
1761 move_panel(p4, LINES/6, itmp*(COLS/8));
1762 wmove(w5, LINES/6, 1);
1763 waddstr(w5,mod[itmp]);
1765 wait_a_while(nap_msec);
1767 saywhat("m5; press any key to continue");
1768 wmove(w4, LINES/6, 1);
1769 waddstr(w4,mod[itmp]);
1770 move_panel(p5, LINES/3 - 1,(itmp*10) + 6);
1771 wmove(w5, LINES/8, 1);
1772 waddstr(w5,mod[itmp]);
1774 wait_a_while(nap_msec);
1777 saywhat("m4; press any key to continue");
1778 move_panel(p4, LINES/6, itmp*(COLS/8));
1780 wait_a_while(nap_msec);
1782 saywhat("t5; press any key to continue");
1785 wait_a_while(nap_msec);
1787 saywhat("t2; press any key to continue");
1790 wait_a_while(nap_msec);
1792 saywhat("t1; press any key to continue");
1795 wait_a_while(nap_msec);
1797 saywhat("d2; press any key to continue");
1800 wait_a_while(nap_msec);
1802 saywhat("h3; press any key to continue");
1805 wait_a_while(nap_msec);
1807 saywhat("d1; press any key to continue");
1810 wait_a_while(nap_msec);
1812 saywhat("d4; press any key to continue");
1815 wait_a_while(nap_msec);
1817 saywhat("d5; press any key to continue");
1820 wait_a_while(nap_msec);
1830 /****************************************************************************
1834 ****************************************************************************/
1838 static bool show_panner_legend = TRUE;
1840 static int panner_legend(int line)
1842 static const char *const legend[] = {
1843 "Use arrow keys (or U,D,L,R) to pan, q to quit (?,t,s flags)",
1844 "Use ! to shell-out. Toggle legend:?, timer:t, scroll mark:s.",
1845 "Use +,- (or j,k) to grow/shrink the panner vertically.",
1846 "Use <,> (or h,l) to grow/shrink the panner horizontally."
1848 int n = (SIZEOF(legend) - (LINES - line));
1849 if (line < LINES && (n >= 0)) {
1851 if (show_panner_legend)
1852 printw("%s", legend[n]);
1854 return show_panner_legend;
1859 static void panner_h_cleanup(int from_y, int from_x, int to_x)
1861 if (!panner_legend(from_y))
1862 do_h_line(from_y, from_x, ' ', to_x);
1865 static void panner_v_cleanup(int from_y, int from_x, int to_y)
1867 if (!panner_legend(from_y))
1868 do_v_line(from_y, from_x, ' ', to_y);
1871 static void panner(WINDOW *pad,
1872 int top_x, int top_y, int porty, int portx,
1873 int (*pgetc)(WINDOW *))
1875 #if HAVE_GETTIMEOFDAY
1876 struct timeval before, after;
1879 bool scrollers = TRUE;
1882 int pxmax, pymax, lowend, highend, c;
1884 getmaxyx(pad, pymax, pxmax);
1885 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
1889 #ifdef NCURSES_VERSION
1891 * During shell-out, the user may have resized the window. Adjust
1892 * the port size of the pad to accommodate this. Ncurses automatically
1893 * resizes all of the normal windows to fit on the new screen.
1895 if (top_x > COLS) top_x = COLS;
1896 if (portx > COLS) portx = COLS;
1897 if (top_y > LINES) top_y = LINES;
1898 if (porty > LINES) porty = LINES;
1908 show_panner_legend = !show_panner_legend;
1909 panner_legend(LINES - 4);
1910 panner_legend(LINES - 3);
1911 panner_legend(LINES - 2);
1912 panner_legend(LINES - 1);
1914 #if HAVE_GETTIMEOFDAY
1918 panner_legend(LINES-1);
1922 scrollers = !scrollers;
1925 /* Move the top-left corner of the pad, keeping the bottom-right
1928 case 'h': /* increase-columns: move left edge to left */
1933 panner_v_cleanup(top_y, top_x, porty);
1938 case 'j': /* decrease-lines: move top-edge down */
1943 panner_h_cleanup(top_y - 1, top_x - (top_x > 0), portx);
1948 case 'k': /* increase-lines: move top-edge up */
1954 panner_h_cleanup(top_y, top_x, portx);
1958 case 'l': /* decrease-columns: move left-edge to right */
1963 panner_v_cleanup(top_y - (top_y > 0), top_x - 1, porty);
1968 /* Move the bottom-right corner of the pad, keeping the top-left
1971 case KEY_IC: /* increase-columns: move right-edge to right */
1972 if (portx >= pxmax || portx >= COLS)
1976 panner_v_cleanup(top_y - (top_y > 0), portx - 1, porty);
1981 case KEY_IL: /* increase-lines: move bottom-edge down */
1982 if (porty >= pymax || porty >= LINES)
1986 panner_h_cleanup(porty - 1, top_x - (top_x > 0), portx);
1991 case KEY_DC: /* decrease-columns: move bottom edge up */
1997 panner_v_cleanup(top_y - (top_y > 0), portx, porty);
2001 case KEY_DL: /* decrease-lines */
2007 panner_h_cleanup(porty, top_x - (top_x > 0), portx);
2011 case KEY_LEFT: /* pan leftwards */
2018 case KEY_RIGHT: /* pan rightwards */
2019 if (basex + portx - (pymax > porty) < pxmax)
2025 case KEY_UP: /* pan upwards */
2032 case KEY_DOWN: /* pan downwards */
2033 if (basey + porty - (pxmax > portx) < pymax)
2044 mvaddch(top_y - 1, top_x - 1, ACS_ULCORNER);
2045 do_v_line(top_y, top_x - 1, ACS_VLINE, porty);
2046 do_h_line(top_y - 1, top_x, ACS_HLINE, portx);
2048 if (scrollers && (pxmax > portx - 1)) {
2049 int length = (portx - top_x - 1);
2050 float ratio = ((float) length) / ((float) pxmax);
2052 lowend = (int)(top_x + (basex * ratio));
2053 highend = (int)(top_x + ((basex + length) * ratio));
2055 do_h_line(porty - 1, top_x, ACS_HLINE, lowend);
2056 if (highend < portx) {
2058 do_h_line(porty - 1, lowend, ' ', highend + 1);
2060 do_h_line(porty - 1, highend + 1, ACS_HLINE, portx);
2063 do_h_line(porty - 1, top_x, ACS_HLINE, portx);
2065 if (scrollers && (pymax > porty - 1)) {
2066 int length = (porty - top_y - 1);
2067 float ratio = ((float) length) / ((float) pymax);
2069 lowend = (int)(top_y + (basey * ratio));
2070 highend = (int)(top_y + ((basey + length) * ratio));
2072 do_v_line(top_y, portx - 1, ACS_VLINE, lowend);
2073 if (highend < porty) {
2075 do_v_line(lowend, portx - 1, ' ', highend + 1);
2077 do_v_line(highend + 1, portx - 1, ACS_VLINE, porty);
2080 do_v_line(top_y, portx - 1, ACS_VLINE, porty);
2082 mvaddch(top_y - 1, portx - 1, ACS_URCORNER);
2083 mvaddch(porty - 1, top_x - 1, ACS_LLCORNER);
2084 mvaddch(porty - 1, portx - 1, ACS_LRCORNER);
2086 #if HAVE_GETTIMEOFDAY
2087 gettimeofday(&before, 0);
2089 wnoutrefresh(stdscr);
2094 porty - (pxmax > portx) - 1,
2095 portx - (pymax > porty) - 1);
2098 #if HAVE_GETTIMEOFDAY
2101 gettimeofday(&after, 0);
2102 elapsed = (after.tv_sec + after.tv_usec / 1.0e6)
2103 - (before.tv_sec + before.tv_usec / 1.0e6);
2104 move(LINES-1, COLS-20);
2105 printw("Secs: %2.03f", elapsed);
2111 ((c = pgetc(pad)) != KEY_EXIT);
2113 scrollok(stdscr, TRUE); /* reset to driver's default */
2117 int padgetch(WINDOW *win)
2121 switch(c = wGetchar(win))
2123 case '!': ShellOut(FALSE); return KEY_REFRESH;
2124 case CTRL('r'): endwin(); refresh(); return KEY_REFRESH;
2125 case CTRL('l'): return KEY_REFRESH;
2126 case 'U': return(KEY_UP);
2127 case 'D': return(KEY_DOWN);
2128 case 'R': return(KEY_RIGHT);
2129 case 'L': return(KEY_LEFT);
2130 case '+': return(KEY_IL);
2131 case '-': return(KEY_DL);
2132 case '>': return(KEY_IC);
2133 case '<': return(KEY_DC);
2134 case ERR: /* FALLTHRU */
2135 case 'q': return(KEY_EXIT);
2140 static void demo_pad(void)
2141 /* Demonstrate pads. */
2144 unsigned gridcount = 0;
2145 WINDOW *panpad = newpad(200, 200);
2147 for (i = 0; i < 200; i++)
2149 for (j = 0; j < 200; j++)
2150 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0)
2152 if (i == 0 || j == 0)
2153 waddch(panpad, '+');
2155 waddch(panpad, (chtype)('A' + (gridcount++ % 26)));
2157 else if (i % GRIDSIZE == 0)
2158 waddch(panpad, '-');
2159 else if (j % GRIDSIZE == 0)
2160 waddch(panpad, '|');
2162 waddch(panpad, ' ');
2164 panner_legend(LINES - 4);
2165 panner_legend(LINES - 3);
2166 panner_legend(LINES - 2);
2167 panner_legend(LINES - 1);
2169 keypad(panpad, TRUE);
2171 /* Make the pad (initially) narrow enough that a trace file won't wrap.
2172 * We'll still be able to widen it during a test, since that's required
2173 * for testing boundaries.
2175 panner(panpad, 2, 2, LINES - 5, COLS-15, padgetch);
2181 #endif /* USE_LIBPANEL */
2183 /****************************************************************************
2185 * Tests from John Burnell's PDCurses tester
2187 ****************************************************************************/
2189 static void Continue (WINDOW *win)
2193 mvwaddstr(win, 10, 1, " Press any key to continue");
2198 static void flushinp_test(WINDOW *win)
2199 /* Input test, adapted from John Burnell's PDCurses tester */
2201 int w, h, bx, by, sw, sh, i;
2206 getmaxyx(win, h, w);
2207 getbegyx(win, by, bx);
2210 if((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == 0)
2216 init_pair(2,COLOR_CYAN,COLOR_BLUE);
2217 wattrset(subWin, COLOR_PAIR(2) | A_BOLD);
2220 wattrset(subWin, A_BOLD);
2222 wattrset(subWin, A_BOLD);
2224 box(subWin, ACS_VLINE, ACS_HLINE);
2225 mvwaddstr(subWin, 2, 1, "This is a subwindow");
2229 * This used to set 'nocbreak()'. However, Alexander Lukyanov says that
2230 * it only happened to "work" on SVr4 because that implementation does not
2231 * emulate nocbreak+noecho mode, whereas ncurses does. To get the desired
2232 * test behavior, we're using 'cbreak()', which will allow a single
2233 * character to return without needing a newline. - T.Dickey 1997/10/11.
2236 mvwaddstr(win, 0, 1, "This is a test of the flushinp() call.");
2238 mvwaddstr(win, 2, 1, "Type random keys for 5 seconds.");
2239 mvwaddstr(win, 3, 1,
2240 "These should be discarded (not echoed) after the subwindow goes away.");
2243 for (i = 0; i < 5; i++)
2245 mvwprintw (subWin, 1, 1, "Time = %d", i);
2257 mvwaddstr(win, 2, 1,
2258 "If you were still typing when the window timer expired,");
2259 mvwaddstr(win, 3, 1,
2260 "or else you typed nothing at all while it was running,");
2261 mvwaddstr(win, 4, 1,
2262 "test was invalid. You'll see garbage or nothing at all. ");
2263 mvwaddstr(win, 6, 1, "Press a key");
2269 mvwaddstr(win, 12, 0,
2270 "If you see any key other than what you typed, flushinp() is broken.");
2279 "What you typed should now have been deleted; if not, wdelch() failed.");
2285 /****************************************************************************
2289 ****************************************************************************/
2296 static int menu_virtualize(int c)
2298 if (c == '\n' || c == KEY_EXIT)
2299 return(MAX_COMMAND + 1);
2301 return(REQ_SCR_ULINE);
2303 return(REQ_SCR_DLINE);
2304 else if (c == 'b' || c == KEY_NPAGE)
2305 return(REQ_SCR_UPAGE);
2306 else if (c == 'f' || c == KEY_PPAGE)
2307 return(REQ_SCR_DPAGE);
2308 else if (c == 'n' || c == KEY_DOWN)
2309 return(REQ_NEXT_ITEM);
2310 else if (c == 'p' || c == KEY_UP)
2311 return(REQ_PREV_ITEM);
2313 return(REQ_TOGGLE_ITEM);
2321 static const char *animals[] =
2323 "Lions", "Tigers", "Bears", "(Oh my!)", "Newts", "Platypi", "Lemurs",
2327 static void menu_test(void)
2330 ITEM *items[SIZEOF(animals)];
2333 int mrows, mcols, c;
2336 #ifdef NCURSES_MOUSE_VERSION
2337 mousemask(ALL_MOUSE_EVENTS, (mmask_t *)0);
2339 mvaddstr(0, 0, "This is the menu test:");
2340 mvaddstr(2, 0, " Use up and down arrow to move the select bar.");
2341 mvaddstr(3, 0, " 'n' and 'p' act like arrows.");
2342 mvaddstr(4, 0, " 'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
2343 mvaddstr(5, 0, " Press return to exit.");
2346 for (ap = animals; *ap; ap++)
2347 *ip++ = new_item(*ap, "");
2350 m = new_menu(items);
2352 set_menu_format(m, (SIZEOF(animals)+1)/2, 1);
2353 scale_menu(m, &mrows, &mcols);
2355 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
2356 set_menu_win(m, menuwin);
2357 keypad(menuwin, TRUE);
2360 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
2364 while ((c = menu_driver(m, menu_virtualize(wGetchar(menuwin)))) != E_UNKNOWN_COMMAND) {
2365 if (c == E_REQUEST_DENIED)
2370 (void) mvprintw(LINES - 2, 0,
2371 "You chose: %s\n", item_name(current_item(m)));
2372 (void) addstr("Press any key to continue...");
2379 for (ip = items; *ip; ip++)
2381 #ifdef NCURSES_MOUSE_VERSION
2382 mousemask(0, (mmask_t *)0);
2387 #define T_TBL(name) { #name, name }
2392 T_TBL(TRACE_DISABLE),
2395 T_TBL(TRACE_UPDATE),
2397 T_TBL(TRACE_CHARPUT),
2398 T_TBL(TRACE_ORDINARY),
2400 T_TBL(TRACE_VIRTPUT),
2401 T_TBL(TRACE_IEVENT),
2403 T_TBL(TRACE_ICALLS),
2404 T_TBL(TRACE_CCALLS),
2405 T_TBL(TRACE_MAXIMUM),
2409 static char *tracetrace(int tlevel)
2416 for (n = 0; t_tbl[n].name != 0; n++)
2417 need += strlen(t_tbl[n].name) + 2;
2418 buf = (char *)malloc(need);
2420 sprintf(buf, "0x%02x = {", tlevel);
2422 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
2424 for (n = 1; t_tbl[n].name != 0; n++)
2425 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask)
2427 strcat(buf, t_tbl[n].name);
2431 if (buf[strlen(buf) - 2] == ',')
2432 buf[strlen(buf) - 2] = '\0';
2433 return(strcat(buf,"}"));
2436 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
2439 static int run_trace_menu(MENU *m)
2445 bool changed = FALSE;
2446 switch (menu_driver(m, menu_virtualize(wGetchar(menu_win(m))))) {
2447 case E_UNKNOWN_COMMAND:
2450 items = menu_items(m);
2451 i = current_item(m);
2452 if (i == items[0]) {
2453 if (item_value(i)) {
2454 for (p = items+1; *p != 0; p++)
2455 if (item_value(*p)) {
2456 set_item_value(*p, FALSE);
2461 for (p = items+1; *p != 0; p++)
2462 if (item_value(*p)) {
2463 set_item_value(items[0], FALSE);
2474 static void trace_set(void)
2475 /* interactively set the trace level */
2478 ITEM *items[SIZEOF(t_tbl)];
2480 int mrows, mcols, newtrace;
2484 mvaddstr(0, 0, "Interactively set trace level:");
2485 mvaddstr(2, 0, " Press space bar to toggle a selection.");
2486 mvaddstr(3, 0, " Use up and down arrow to move the select bar.");
2487 mvaddstr(4, 0, " Press return to set the trace level.");
2488 mvprintw(6, 0, "(Current trace level is %s)", tracetrace(_nc_tracing));
2492 for (n = 0; t_tbl[n].name != 0; n++)
2493 *ip++ = new_item(t_tbl[n].name, "");
2496 m = new_menu(items);
2498 set_menu_format(m, 0, 2);
2499 scale_menu(m, &mrows, &mcols);
2501 menu_opts_off(m, O_ONEVALUE);
2502 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
2503 set_menu_win(m, menuwin);
2504 keypad(menuwin, TRUE);
2507 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
2511 for (ip = menu_items(m); *ip; ip++) {
2512 int mask = t_tbl[item_index(*ip)].mask;
2514 set_item_value(*ip, _nc_tracing == 0);
2515 else if ((mask & _nc_tracing) == mask)
2516 set_item_value(*ip, TRUE);
2519 while (run_trace_menu(m))
2523 for (ip = menu_items(m); *ip; ip++)
2524 if (item_value(*ip))
2525 newtrace |= t_tbl[item_index(*ip)].mask;
2527 _tracef("trace level interactively set to %s", tracetrace(_nc_tracing));
2529 (void) mvprintw(LINES - 2, 0,
2530 "Trace level is %s\n", tracetrace(_nc_tracing));
2531 (void) addstr("Press any key to continue...");
2538 for (ip = items; *ip; ip++)
2542 #endif /* USE_LIBMENU */
2544 /****************************************************************************
2548 ****************************************************************************/
2550 static FIELD *make_label(int frow, int fcol, NCURSES_CONST char *label)
2552 FIELD *f = new_field(1, strlen(label), frow, fcol, 0, 0);
2556 set_field_buffer(f, 0, label);
2557 set_field_opts(f, field_opts(f) & ~O_ACTIVE);
2562 static FIELD *make_field(int frow, int fcol, int rows, int cols, bool secure)
2564 FIELD *f = new_field(rows, cols, frow, fcol, 0, secure ? 1 : 0);
2567 set_field_back(f, A_UNDERLINE);
2568 set_field_userptr(f, (void *)0);
2573 static void display_form(FORM *f)
2578 scale_form(f, &rows, &cols);
2580 if ((w =newwin(rows+2, cols+4, 0, 0)) != (WINDOW *)0)
2583 set_form_sub(f, derwin(w, rows, cols, 1, 2));
2588 if (post_form(f) != E_OK)
2592 static void erase_form(FORM *f)
2594 WINDOW *w = form_win(f);
2595 WINDOW *s = form_sub(f);
2604 static int edit_secure(FIELD *me, int c)
2606 int rows, cols, frow, fcol, nbuf;
2608 if (field_info(me, &rows, &cols, &frow, &fcol, (int *)0, &nbuf) == E_OK
2613 strcpy(temp, field_buffer(me, 1));
2614 len = (long)(char *) field_userptr(me);
2619 set_field_buffer(me, 1, temp);
2639 case REQ_RIGHT_CHAR:
2641 c = 0; /* we don't want to do inline editing */
2646 set_field_buffer(me, 1, temp);
2653 set_field_buffer(me, 1, temp);
2658 set_field_userptr(me, (void *)len);
2663 static int form_virtualize(FORM *f, WINDOW *w)
2665 static const struct {
2669 { CTRL('A'), REQ_NEXT_CHOICE },
2670 { CTRL('B'), REQ_PREV_WORD },
2671 { CTRL('C'), REQ_CLR_EOL },
2672 { CTRL('D'), REQ_DOWN_FIELD },
2673 { CTRL('E'), REQ_END_FIELD },
2674 { CTRL('F'), REQ_NEXT_PAGE },
2675 { CTRL('G'), REQ_DEL_WORD },
2676 { CTRL('H'), REQ_DEL_PREV },
2677 { CTRL('I'), REQ_INS_CHAR },
2678 { CTRL('K'), REQ_CLR_EOF },
2679 { CTRL('L'), REQ_LEFT_FIELD },
2680 { CTRL('M'), REQ_NEW_LINE },
2681 { CTRL('N'), REQ_NEXT_FIELD },
2682 { CTRL('O'), REQ_INS_LINE },
2683 { CTRL('P'), REQ_PREV_FIELD },
2684 { CTRL('R'), REQ_RIGHT_FIELD },
2685 { CTRL('S'), REQ_BEG_FIELD },
2686 { CTRL('U'), REQ_UP_FIELD },
2687 { CTRL('V'), REQ_DEL_CHAR },
2688 { CTRL('W'), REQ_NEXT_WORD },
2689 { CTRL('X'), REQ_CLR_FIELD },
2690 { CTRL('Y'), REQ_DEL_LINE },
2691 { CTRL('Z'), REQ_PREV_CHOICE },
2692 { ESCAPE, MAX_FORM_COMMAND + 1 },
2693 { KEY_BACKSPACE, REQ_DEL_PREV },
2694 { KEY_DOWN, REQ_DOWN_CHAR },
2695 { KEY_END, REQ_LAST_FIELD },
2696 { KEY_HOME, REQ_FIRST_FIELD },
2697 { KEY_LEFT, REQ_LEFT_CHAR },
2698 { KEY_LL, REQ_LAST_FIELD },
2699 { KEY_NEXT, REQ_NEXT_FIELD },
2700 { KEY_NPAGE, REQ_NEXT_PAGE },
2701 { KEY_PPAGE, REQ_PREV_PAGE },
2702 { KEY_PREVIOUS, REQ_PREV_FIELD },
2703 { KEY_RIGHT, REQ_RIGHT_CHAR },
2704 { KEY_UP, REQ_UP_CHAR },
2705 { QUIT, MAX_FORM_COMMAND + 1 }
2708 static int mode = REQ_INS_MODE;
2709 int c = wGetchar(w);
2711 FIELD *me = current_field(f);
2713 if (c == CTRL(']')) {
2714 if (mode == REQ_INS_MODE)
2715 mode = REQ_OVL_MODE;
2717 mode = REQ_INS_MODE;
2720 for (n = 0; n < sizeof(lookup)/sizeof(lookup[0]); n++) {
2721 if (lookup[n].code == c) {
2722 c = lookup[n].result;
2729 * Force the field that the user is typing into to be in reverse video,
2730 * while the other fields are shown underlined.
2733 c = edit_secure(me, c);
2734 set_field_back(me, A_REVERSE);
2735 } else if (c <= MAX_FORM_COMMAND) {
2736 c = edit_secure(me, c);
2737 set_field_back(me, A_UNDERLINE);
2742 static int my_form_driver(FORM *form, int c)
2744 if (c == (MAX_FORM_COMMAND + 1)
2745 && form_driver(form, REQ_VALIDATION) == E_OK)
2754 static void demo_forms(void)
2758 FIELD *f[12], *secure;
2759 int finished = 0, c;
2763 addstr("Defined form-traversal keys: ^Q/ESC- exit form\n");
2764 addstr("^N -- go to next field ^P -- go to previous field\n");
2765 addstr("Home -- go to first field End -- go to last field\n");
2766 addstr("^L -- go to field to left ^R -- go to field to right\n");
2767 addstr("^U -- move upward to field ^D -- move downward to field\n");
2768 addstr("^W -- go to next word ^B -- go to previous word\n");
2769 addstr("^S -- go to start of field ^E -- go to end of field\n");
2770 addstr("^H -- delete previous char ^Y -- delete line\n");
2771 addstr("^G -- delete current word ^C -- clear to end of line\n");
2772 addstr("^K -- clear to end of field ^X -- clear field\n");
2773 addstr("Arrow keys move within a field as you would expect.");
2775 mvaddstr(4, 57, "Forms Entry Test");
2779 /* describe the form */
2780 f[n++] = make_label(0, 15, "Sample Form");
2781 f[n++] = make_label(2, 0, "Last Name");
2782 f[n++] = make_field(3, 0, 1, 18, FALSE);
2783 f[n++] = make_label(2, 20, "First Name");
2784 f[n++] = make_field(3, 20, 1, 12, FALSE);
2785 f[n++] = make_label(2, 34, "Middle Name");
2786 f[n++] = make_field(3, 34, 1, 12, FALSE);
2787 f[n++] = make_label(5, 0, "Comments");
2788 f[n++] = make_field(6, 0, 4, 46, FALSE);
2789 f[n++] = make_label(5, 20, "Password:");
2791 f[n++] = make_field(5, 30, 1, 9, TRUE);
2792 f[n++] = (FIELD *)0;
2802 switch(form_driver(form, c = form_virtualize(form, w)))
2805 mvaddstr(5, 57, field_buffer(secure, 1));
2809 case E_UNKNOWN_COMMAND:
2810 finished = my_form_driver(form, c);
2821 for (c = 0; f[c] != 0; c++)
2825 #endif /* USE_LIBFORM */
2827 /****************************************************************************
2831 ****************************************************************************/
2833 static void fillwin(WINDOW *win, char ch)
2838 getmaxyx(win, y1, x1);
2839 for (y = 0; y < y1; y++)
2842 for (x = 0; x < x1; x++)
2847 static void crosswin(WINDOW *win, char ch)
2852 getmaxyx(win, y1, x1);
2853 for (y = 0; y < y1; y++)
2855 for (x = 0; x < x1; x++)
2856 if (((x > (x1 - 1) / 3) && (x <= (2 * (x1 - 1)) / 3))
2857 || (((y > (y1 - 1) / 3) && (y <= (2 * (y1 - 1)) / 3))))
2865 static void overlap_test(void)
2866 /* test effects of overlapping windows */
2870 WINDOW *win1 = newwin(9, 20, 3, 3);
2871 WINDOW *win2 = newwin(9, 20, 9, 16);
2876 printw("This test shows the behavior of wnoutrefresh() with respect to\n");
2877 printw("the shared region of two overlapping windows A and B. The cross\n");
2878 printw("pattern in each window does not overlap the other.\n");
2882 printw("a = refresh A, then B, then doupdate. b = refresh B, then A, then doupdaute\n");
2883 printw("c = fill window A with letter A. d = fill window B with letter B.\n");
2884 printw("e = cross pattern in window A. f = cross pattern in window B.\n");
2885 printw("g = clear window A. h = clear window B.\n");
2886 printw("i = overwrite A onto B. j = overwrite B onto A.\n");
2887 printw("^Q/ESC = terminate test.");
2889 while ((ch = Getchar()) != QUIT && ch != ESCAPE)
2892 case 'a': /* refresh window A first, then B */
2898 case 'b': /* refresh window B first, then A */
2904 case 'c': /* fill window A so it's visible */
2908 case 'd': /* fill window B so it's visible */
2912 case 'e': /* cross test pattern in window A */
2913 crosswin(win1, 'A');
2916 case 'f': /* cross test pattern in window A */
2917 crosswin(win2, 'B');
2920 case 'g': /* clear window A */
2925 case 'h': /* clear window B */
2930 case 'i': /* overwrite A onto B */
2931 overwrite(win1, win2);
2934 case 'j': /* overwrite B onto A */
2935 overwrite(win2, win1);
2945 /****************************************************************************
2949 ****************************************************************************/
2952 do_single_test(const char c)
2953 /* perform a single specified test */
2967 Cannot("does not support color.");
2974 Cannot("does not support color.");
2975 else if (!can_change_color())
2976 Cannot("has hardwired color values.");
3000 flushinp_test(stdscr);
3004 test_sgr_attributes();
3029 #if USE_LIBMENU && defined(TRACE)
3048 static const char *const tbl[] = {
3049 "Usage: ncurses [options]"
3052 ," -e fmt specify format for soft-keys test (e)"
3053 ," -f rip-off footer line (can repeat)"
3054 ," -h rip-off header line (can repeat)"
3055 ," -s msec specify nominal time for panel-demo (default: 1, to hold)"
3057 ," -t mask specify default trace-level (may toggle with ^T)"
3061 for (n = 0; n < sizeof(tbl)/sizeof(tbl[0]); n++)
3062 fprintf(stderr, "%s\n", tbl[n]);
3067 set_terminal_modes(void)
3072 scrollok(stdscr, TRUE);
3073 idlok(stdscr, TRUE);
3074 keypad(stdscr, TRUE);
3078 static RETSIGTYPE announce_sig(int sig)
3080 (void) fprintf(stderr, "Handled signal %d\r\n", sig);
3084 static int rip_footer(WINDOW *win, int columns)
3086 wbkgd(win, A_REVERSE);
3089 wprintw(win, "footer: %d columns", columns);
3094 static int rip_header(WINDOW *win, int columns)
3096 wbkgd(win, A_REVERSE);
3099 wprintw(win, "header: %d columns", columns);
3104 /*+-------------------------------------------------------------------------
3106 --------------------------------------------------------------------------*/
3109 main(int argc, char *argv[])
3115 setlocale(LC_CTYPE, "");
3118 while ((c = getopt(argc, argv, "e:fhs:t:")) != EOF) {
3121 my_e_param = atoi(optarg);
3122 #ifdef NCURSES_VERSION
3123 if (my_e_param > 3) /* allow extended layouts */
3131 ripoffline(-1, rip_footer);
3134 ripoffline(1, rip_header);
3138 nap_msec = atol(optarg);
3143 save_trace = atoi(optarg);
3152 * If there's no menus (unlikely for ncurses!), then we'll have to set
3153 * tracing on initially, just in case the user wants to test something that
3154 * doesn't involve wGetchar.
3157 /* enable debugging */
3161 if (!isatty(fileno(stdin)))
3163 #endif /* USE_LIBMENU */
3166 /* tell it we're going to play with soft keys */
3167 slk_init(my_e_param);
3170 /* set up null signal catcher so we can see what interrupts to getch do */
3171 signal(SIGUSR1, announce_sig);
3174 /* we must initialize the curses data structure only once */
3178 /* tests, in general, will want these modes */
3180 set_terminal_modes();
3184 * Return to terminal mode, so we're guaranteed of being able to
3185 * select terminal commands even if the capabilities are wrong.
3189 #if HAVE_CURSES_VERSION
3190 (void) printf("Welcome to %s. Press ? for help.\n", curses_version());
3191 #elif defined(NCURSES_VERSION_MAJOR) && defined(NCURSES_VERSION_MINOR) && defined(NCURSES_VERSION_PATCH)
3192 (void) printf("Welcome to ncurses %d.%d.%d. Press ? for help.\n",
3193 NCURSES_VERSION_MAJOR,
3194 NCURSES_VERSION_MINOR,
3195 NCURSES_VERSION_PATCH);
3197 (void) puts("Welcome to ncurses. Press ? for help.");
3201 (void) puts("This is the ncurses main menu");
3202 (void) puts("a = keyboard and mouse input test");
3203 (void) puts("b = character attribute test");
3204 (void) puts("c = color test pattern");
3205 (void) puts("d = edit RGB color values");
3206 (void) puts("e = exercise soft keys");
3207 (void) puts("f = display ACS characters");
3208 (void) puts("g = display windows and scrolling");
3209 (void) puts("i = test of flushinp()");
3210 (void) puts("k = display character attributes");
3212 (void) puts("m = menu code test");
3215 (void) puts("o = exercise panels library");
3216 (void) puts("p = exercise pad features");
3217 (void) puts("q = quit");
3220 (void) puts("r = exercise forms code");
3222 (void) puts("s = overlapping-refresh test");
3223 #if USE_LIBMENU && defined(TRACE)
3224 (void) puts("t = set trace level");
3226 (void) puts("? = repeat this command summary");
3228 (void) fputs("> ", stdout);
3229 (void) fflush(stdout); /* necessary under SVr4 curses */
3232 * This used to be an 'fgets()' call. However (on Linux, at least)
3233 * mixing stream I/O and 'read()' (used in the library) causes the
3234 * input stream to be flushed when switching between the two.
3239 if (read(fileno(stdin), &ch, 1) <= 0) {
3243 } else if (command == 0 && !isspace(ch)) {
3245 } else if (ch == '\n' || ch == '\r') {
3248 (void) fputs("> ", stdout);
3249 (void) fflush(stdout);
3253 if (do_single_test(command)) {
3255 * This may be overkill; it's intended to reset everything back
3256 * to the initial terminal modes so that tests don't get in
3260 set_terminal_modes();
3265 if (command == '?') {
3266 (void) puts("This is the ncurses capability tester.");
3267 (void) puts("You may select a test from the main menu by typing the");
3268 (void) puts("key letter of the choice (the letter to left of the =)");
3269 (void) puts("at the > prompt. The commands `x' or `q' will exit.");
3276 ExitProgram(EXIT_SUCCESS);
3279 /* ncurses.c ends here */