+ if (arrow)
+ mvprintw(row, 5, "-->");
+ mvprintw(row, 8, "%s mode:", name);
+ mvprintw(row, 24, "|");
+ if (skip)
+ printw("%*s", skip, " ");
+ /*
+ * Just for testing, write text using the alternate character set one
+ * character at a time (to pass its rendition directly), and use the
+ * string operation for the other attributes.
+ */
+ if (attr & A_ALTCHARSET) {
+ const char *s;
+ chtype ch;
+
+ for (s = attr_test_string; *s != '\0'; ++s) {
+ ch = UChar(*s);
+ addch(ch | attr);
+ }
+ } else {
+ attrset(attr);
+ addstr(attr_test_string);
+ attroff(attr);
+ }
+ if (skip)
+ printw("%*s", skip, " ");
+ printw("|");
+ if (test != A_NORMAL) {
+ if (!(termattrs() & test)) {
+ printw(" (N/A)");
+ } else {
+ if (ncv > 0 && (getbkgd(stdscr) & A_COLOR)) {
+ static const chtype table[] =
+ {
+ A_STANDOUT,
+ A_UNDERLINE,
+ A_REVERSE,
+ A_BLINK,
+ A_DIM,
+ A_BOLD,
+#ifdef A_INVIS
+ A_INVIS,
+#endif
+ A_PROTECT,
+ A_ALTCHARSET
+ };
+ unsigned n;
+ bool found = FALSE;
+ for (n = 0; n < SIZEOF(table); n++) {
+ if ((table[n] & attr) != 0
+ && ((1 << n) & ncv) != 0) {
+ found = TRUE;
+ break;
+ }
+ }
+ if (found)
+ printw(" (NCV)");
+ }
+ if ((termattrs() & test) != test)
+ printw(" (Part)");
+ }
+ }
+ return row + 2;
+}
+/* *INDENT-OFF* */
+static const struct {
+ attr_t attr;
+ NCURSES_CONST char * name;
+} attrs_to_test[] = {
+ { A_STANDOUT, "STANDOUT" },
+ { A_REVERSE, "REVERSE" },
+ { A_BOLD, "BOLD" },
+ { A_UNDERLINE, "UNDERLINE" },
+ { A_DIM, "DIM" },
+ { A_BLINK, "BLINK" },
+ { A_PROTECT, "PROTECT" },
+#ifdef A_INVIS
+ { A_INVIS, "INVISIBLE" },
+#endif
+ { A_NORMAL, "NORMAL" },
+};
+/* *INDENT-ON* */
+
+static bool
+attr_getc(int *skip, short *fg, short *bg, short *tx, int *ac, unsigned *kc)
+{
+ bool result = TRUE;
+ bool error = FALSE;
+ WINDOW *helpwin;
+
+ do {
+ int ch = Getchar();
+
+ error = FALSE;
+ if (ch < 256 && isdigit(ch)) {
+ *skip = (ch - '0');
+ } else {
+ switch (ch) {
+ case CTRL('L'):
+ Repaint();
+ break;
+ case '?':
+ if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
+ box(helpwin, 0, 0);
+ attr_legend(helpwin);
+ wGetchar(helpwin);
+ delwin(helpwin);
+ }
+ break;
+ case 'a':
+ *ac = 0;
+ break;
+ case 'A':
+ *ac = A_ALTCHARSET;
+ break;
+ case 'v':
+ if (*kc == 0)
+ *kc = SIZEOF(attrs_to_test) - 1;
+ else
+ *kc -= 1;
+ break;
+ case 'V':
+ *kc += 1;
+ if (*kc >= SIZEOF(attrs_to_test))
+ *kc = 0;
+ break;
+ case '<':
+ adjust_attr_string(-1);
+ break;
+ case '>':
+ adjust_attr_string(1);
+ break;
+ case_QUIT:
+ result = FALSE;
+ break;
+ default:
+ error = cycle_color_attr(ch, fg, bg, tx);
+ break;
+ }
+ }
+ } while (error);
+ return result;
+}
+
+static void
+attr_test(void)
+/* test text attributes */
+{
+ int n;
+ int skip = get_xmc();
+ short fg = COLOR_BLACK; /* color pair 0 is special */
+ short bg = COLOR_BLACK;
+ short tx = -1;
+ int ac = 0;
+ unsigned j, k;
+
+ if (skip < 0)
+ skip = 0;
+
+ n = skip; /* make it easy */
+ k = SIZEOF(attrs_to_test) - 1;
+ init_attr_string();
+
+ do {
+ int row = 2;
+ chtype normal = A_NORMAL | BLANK;
+ chtype extras = ac;
+
+ if (use_colors) {
+ short pair = (fg != COLOR_BLACK || bg != COLOR_BLACK);
+ if (pair != 0) {
+ pair = 1;
+ if (init_pair(pair, fg, bg) == ERR) {
+ beep();
+ } else {
+ normal |= COLOR_PAIR(pair);
+ }
+ }
+ if (tx >= 0) {
+ pair = 2;
+ if (init_pair(pair, tx, bg) == ERR) {
+ beep();
+ } else {
+ extras |= COLOR_PAIR(pair);
+ }
+ }
+ }
+ bkgd(normal);
+ bkgdset(normal);
+ erase();
+
+ box(stdscr, 0, 0);
+ mvaddstr(0, 20, "Character attribute test display");
+
+ for (j = 0; j < SIZEOF(attrs_to_test); ++j) {
+ bool arrow = (j == k);
+ row = show_attr(row, n, arrow,
+ extras |
+ attrs_to_test[j].attr |
+ attrs_to_test[k].attr,
+ attrs_to_test[j].name);
+ }
+
+ mvprintw(row, 8,
+ "This terminal does %shave the magic-cookie glitch",
+ get_xmc() > -1 ? "" : "not ");
+ mvprintw(row + 1, 8, "Enter '?' for help.");
+ show_color_attr(fg, bg, tx);
+ printw(" ACS (%d)", ac != 0);
+
+ refresh();
+ } while (attr_getc(&n, &fg, &bg, &tx, &ac, &k));
+
+ bkgdset(A_NORMAL | BLANK);
+ erase();
+ endwin();
+}
+
+#if USE_WIDEC_SUPPORT
+static wchar_t wide_attr_test_string[MAX_ATTRSTRING + 1];
+
+static void
+wide_adjust_attr_string(int adjust)
+{
+ int first = ((int) UChar(wide_attr_test_string[0])) + adjust;
+ int last = first + LEN_ATTRSTRING;
+
+ if (first >= ' ' && last <= '~') { /* 32..126 */
+ int j, k;
+ for (j = 0, k = first; j < MAX_ATTRSTRING && k <= last; ++j, ++k) {
+ wide_attr_test_string[j] = k;
+ if (((k + 1 - first) % 5) == 0) {
+ ++j;
+ if (j < MAX_ATTRSTRING)
+ wide_attr_test_string[j] = ' ';
+ }
+ }
+ while (j < MAX_ATTRSTRING)
+ wide_attr_test_string[j++] = ' ';
+ wide_attr_test_string[j] = '\0';
+ } else {
+ beep();
+ }
+}
+
+static void
+wide_init_attr_string(void)
+{
+ wide_attr_test_string[0] = 'a';
+ wide_adjust_attr_string(0);
+}
+
+static void
+set_wide_background(short pair)
+{
+ cchar_t normal;
+ wchar_t blank[2];
+
+ blank[0] = ' ';
+ blank[1] = 0;
+ setcchar(&normal, blank, A_NORMAL, pair, 0);
+ bkgrnd(&normal);
+ bkgrndset(&normal);
+}
+
+static attr_t
+get_wide_background(void)
+{
+ attr_t result = A_NORMAL;
+ attr_t attr;
+ cchar_t ch;
+ short pair;
+ wchar_t wch[10];
+
+ if (getbkgrnd(&ch) != ERR) {
+ if (getcchar(&ch, wch, &attr, &pair, 0) != ERR) {
+ result = attr;
+ }
+ }
+ return result;
+}
+
+static int
+wide_show_attr(int row, int skip, bool arrow, chtype attr, short pair, const char *name)
+{
+ int ncv = get_ncv();
+ chtype test = attr & ~WA_ALTCHARSET;
+
+ if (arrow)
+ mvprintw(row, 5, "-->");
+ mvprintw(row, 8, "%s mode:", name);
+ mvprintw(row, 24, "|");
+ if (skip)
+ printw("%*s", skip, " ");
+
+ /*
+ * Just for testing, write text using the alternate character set one
+ * character at a time (to pass its rendition directly), and use the
+ * string operation for the other attributes.
+ */
+ if (attr & WA_ALTCHARSET) {
+ const wchar_t *s;
+ cchar_t ch;
+
+ for (s = wide_attr_test_string; *s != L'\0'; ++s) {
+ wchar_t fill[2];
+ fill[0] = *s;
+ fill[1] = L'\0';
+ setcchar(&ch, fill, attr, pair, 0);
+ add_wch(&ch);
+ }
+ } else {
+ attr_t old_attr;
+ short old_pair;
+
+ attr_get(&old_attr, &old_pair, 0);
+ attr_set(attr, pair, 0);
+ addwstr(wide_attr_test_string);
+ attr_set(old_attr, old_pair, 0);
+ }
+ if (skip)
+ printw("%*s", skip, " ");
+ printw("|");
+ if (test != A_NORMAL) {
+ if (!(term_attrs() & test)) {
+ printw(" (N/A)");
+ } else {
+ if (ncv > 0 && (get_wide_background() & A_COLOR)) {
+ static const attr_t table[] =
+ {
+ WA_STANDOUT,
+ WA_UNDERLINE,
+ WA_REVERSE,
+ WA_BLINK,
+ WA_DIM,
+ WA_BOLD,
+ WA_INVIS,
+ WA_PROTECT,
+ WA_ALTCHARSET
+ };
+ unsigned n;
+ bool found = FALSE;
+ for (n = 0; n < SIZEOF(table); n++) {
+ if ((table[n] & attr) != 0
+ && ((1 << n) & ncv) != 0) {
+ found = TRUE;
+ break;
+ }
+ }
+ if (found)
+ printw(" (NCV)");
+ }
+ if ((term_attrs() & test) != test)
+ printw(" (Part)");
+ }
+ }
+ return row + 2;
+}
+
+static bool
+wide_attr_getc(int *skip, short *fg, short *bg, short *tx, int *ac, unsigned *kc)
+{
+ bool result = TRUE;
+ bool error = FALSE;
+ WINDOW *helpwin;
+
+ do {
+ int ch = Getchar();
+
+ error = FALSE;
+ if (ch < 256 && isdigit(ch)) {
+ *skip = (ch - '0');
+ } else {
+ switch (ch) {
+ case CTRL('L'):
+ Repaint();
+ break;
+ case '?':
+ if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
+ box_set(helpwin, 0, 0);
+ attr_legend(helpwin);
+ wGetchar(helpwin);
+ delwin(helpwin);
+ }
+ break;
+ case 'a':
+ *ac = 0;
+ break;
+ case 'A':
+ *ac = A_ALTCHARSET;
+ break;
+ case 'v':
+ if (*kc == 0)
+ *kc = SIZEOF(attrs_to_test) - 1;
+ else
+ *kc -= 1;
+ break;
+ case 'V':
+ *kc += 1;
+ if (*kc >= SIZEOF(attrs_to_test))
+ *kc = 0;
+ break;
+ case '<':
+ wide_adjust_attr_string(-1);
+ break;
+ case '>':
+ wide_adjust_attr_string(1);
+ break;
+ case_QUIT:
+ result = FALSE;
+ break;
+ default:
+ error = cycle_color_attr(ch, fg, bg, tx);
+ break;
+ }
+ }
+ } while (error);
+ return result;
+}
+
+static void
+wide_attr_test(void)
+/* test text attributes using wide-character calls */
+{
+ int n;
+ int skip = get_xmc();
+ short fg = COLOR_BLACK; /* color pair 0 is special */
+ short bg = COLOR_BLACK;
+ short tx = -1;
+ int ac = 0;
+ unsigned j, k;
+
+ if (skip < 0)
+ skip = 0;
+
+ n = skip; /* make it easy */
+ k = SIZEOF(attrs_to_test) - 1;
+ wide_init_attr_string();
+
+ do {
+ int row = 2;
+ short pair = 0;
+ short extras = 0;
+
+ if (use_colors) {
+ pair = (fg != COLOR_BLACK || bg != COLOR_BLACK);
+ if (pair != 0) {
+ pair = 1;
+ if (init_pair(pair, fg, bg) == ERR) {
+ beep();
+ }
+ }
+ extras = pair;
+ if (tx >= 0) {
+ extras = 2;
+ if (init_pair(extras, tx, bg) == ERR) {
+ beep();
+ }
+ }
+ }
+ set_wide_background(pair);
+ erase();
+
+ box_set(stdscr, 0, 0);
+ mvaddstr(0, 20, "Character attribute test display");
+
+ for (j = 0; j < SIZEOF(attrs_to_test); ++j) {
+ row = wide_show_attr(row, n, j == k,
+ ac |
+ attrs_to_test[j].attr |
+ attrs_to_test[k].attr,
+ extras,
+ attrs_to_test[j].name);
+ }
+
+ mvprintw(row, 8,
+ "This terminal does %shave the magic-cookie glitch",
+ get_xmc() > -1 ? "" : "not ");
+ mvprintw(row + 1, 8, "Enter '?' for help.");
+ show_color_attr(fg, bg, tx);
+ printw(" ACS (%d)", ac != 0);
+
+ refresh();
+ } while (wide_attr_getc(&n, &fg, &bg, &tx, &ac, &k));
+
+ set_wide_background(0);
+ erase();
+ endwin();
+}
+#endif
+
+/****************************************************************************
+ *
+ * Color support tests
+ *
+ ****************************************************************************/
+
+static NCURSES_CONST char *the_color_names[] =
+{
+ "black",
+ "red",
+ "green",
+ "yellow",
+ "blue",
+ "magenta",
+ "cyan",
+ "white",
+ "BLACK",
+ "RED",
+ "GREEN",
+ "YELLOW",
+ "BLUE",
+ "MAGENTA",
+ "CYAN",
+ "WHITE"
+};
+
+static void
+show_color_name(int y, int x, int color, bool wide)
+{
+ if (move(y, x) != ERR) {
+ char temp[80];
+ int width = 8;
+
+ if (wide) {
+ sprintf(temp, "%02d", color);
+ width = 4;
+ } else if (color >= 8) {
+ sprintf(temp, "[%02d]", color);
+ } else {
+ strcpy(temp, the_color_names[color]);
+ }
+ printw("%-*.*s", width, width, temp);
+ }
+}
+
+static void
+color_legend(WINDOW *helpwin)
+{
+ int row = 1;
+ int col = 1;
+
+ mvwprintw(helpwin, row++, col,
+ "ESC to exit.");
+ ++row;
+ mvwprintw(helpwin, row++, col,
+ "Use up/down arrow to scroll through the display if it is");
+ mvwprintw(helpwin, row++, col,
+ "longer than one screen. Control/N and Control/P can be used");
+ mvwprintw(helpwin, row++, col,
+ "in place up up/down arrow. Use pageup/pagedown to scroll a");
+ mvwprintw(helpwin, row++, col,
+ "full screen; control/B and control/F can be used here.");
+ ++row;
+ mvwprintw(helpwin, row++, col,
+ "Toggles:");
+ mvwprintw(helpwin, row++, col,
+ " b/B toggle bold off/on");
+ mvwprintw(helpwin, row++, col,
+ " n/N toggle text/number on/off");
+ mvwprintw(helpwin, row++, col,
+ " w/W toggle width between 8/16 colors");
+}
+
+#define set_color_test(name, value) if (name != value) { name = value; base_row = 0; }
+
+/* generate a color test pattern */
+static void
+color_test(void)
+{
+ short i;
+ int top = 0, width;
+ int base_row = 0;
+ int grid_top = top + 3;
+ int page_size = (LINES - grid_top);
+ int pairs_max = PAIR_NUMBER(A_COLOR) + 1;
+ int row_limit;
+ int per_row;
+ char numbered[80];
+ const char *hello;
+ bool done = FALSE;
+ bool opt_bold = FALSE;
+ bool opt_wide = FALSE;
+ bool opt_nums = FALSE;
+ WINDOW *helpwin;
+
+ if (pairs_max > COLOR_PAIRS)
+ pairs_max = COLOR_PAIRS;
+
+ while (!done) {
+ int shown = 0;
+
+ /* this assumes an 80-column line */
+ if (opt_wide) {
+ width = 4;
+ hello = "Test";
+ per_row = (COLORS > 8) ? 16 : 8;
+ } else {
+ width = 8;
+ hello = "Hello";
+ per_row = 8;
+ }
+
+ row_limit = (pairs_max + per_row - 1) / per_row;