+#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;
+
+ 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 = tigetnum("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, int *fg, int *bg, int *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 'q':
+ case ESCAPE:
+ 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 = tigetnum("xmc");
+ int fg = COLOR_BLACK; /* color pair 0 is special */
+ int bg = COLOR_BLACK;
+ int 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 (has_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",
+ tigetnum("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
+