+ for (s = attr_test_string; *s != '\0'; ++s) {
+ ch = UChar(*s);
+ addch(ch | attr);
+ }
+ } else {
+ (void) 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 && stdscr && (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
+#ifdef A_ITALIC
+ A_ITALIC,
+#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;
+}
+
+typedef struct {
+ attr_t attr;
+ NCURSES_CONST char *name;
+} ATTR_TBL;
+/* *INDENT-OFF* */
+static const ATTR_TBL 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
+#ifdef A_ITALIC
+ { A_ITALIC, "ITALIC" },
+#endif
+ { A_NORMAL, "NORMAL" },
+};
+/* *INDENT-ON* */
+
+static unsigned
+init_attr_list(ATTR_TBL * target, attr_t attrs)
+{
+ unsigned result = 0;
+ size_t n;
+
+ for (n = 0; n < SIZEOF(attrs_to_test); ++n) {
+ attr_t test = attrs_to_test[n].attr;
+ if (test == A_NORMAL || (test & attrs) != 0) {
+ target[result++] = attrs_to_test[n];
+ }
+ }
+ return result;
+}
+
+static bool
+attr_getc(int *skip,
+ NCURSES_COLOR_T *fg,
+ NCURSES_COLOR_T *bg,
+ NCURSES_COLOR_T *tx,
+ int *ac,
+ unsigned *kc,
+ unsigned limit)
+{
+ 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 = limit - 1;
+ else
+ *kc -= 1;
+ break;
+ case 'V':
+ *kc += 1;
+ if (*kc >= limit)
+ *kc = 0;
+ break;
+ case '<':
+ adjust_attr_string(-1);
+ break;
+ case '>':
+ adjust_attr_string(1);
+ break;
+ case 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();
+ NCURSES_COLOR_T fg = COLOR_BLACK; /* color pair 0 is special */
+ NCURSES_COLOR_T bg = COLOR_BLACK;
+ NCURSES_COLOR_T tx = -1;
+ int ac = 0;
+ unsigned j, k;
+ ATTR_TBL my_list[SIZEOF(attrs_to_test)];
+ unsigned my_size = init_attr_list(my_list, termattrs());
+
+ if (my_size > 1) {
+ if (skip < 0)
+ skip = 0;
+
+ n = skip; /* make it easy */
+ k = my_size - 1;
+ init_attr_string();
+
+ do {
+ int row = 2;
+ chtype normal = A_NORMAL | BLANK;
+ chtype extras = (chtype) ac;
+
+ if (use_colors) {
+ NCURSES_PAIRS_T pair = 0;
+ if ((fg != COLOR_BLACK) || (bg != COLOR_BLACK)) {
+ pair = 1;
+ if (init_pair(pair, fg, bg) == ERR) {
+ beep();
+ } else {
+ normal |= (chtype) COLOR_PAIR(pair);
+ }
+ }
+ if (tx >= 0) {
+ pair = 2;
+ if (init_pair(pair, tx, bg) == ERR) {
+ beep();
+ } else {
+ extras |= (chtype) COLOR_PAIR(pair);
+ }
+ }
+ }
+ bkgd(normal);
+ bkgdset(normal);
+ erase();
+
+ box(stdscr, 0, 0);
+ MvAddStr(0, 20, "Character attribute test display");
+
+ for (j = 0; j < my_size; ++j) {
+ bool arrow = (j == k);
+ row = show_attr(row, n, arrow,
+ extras |
+ my_list[j].attr |
+ my_list[k].attr,
+ my_list[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, my_size));
+
+ bkgdset(A_NORMAL | BLANK);
+ erase();
+ endwin();
+ } else {
+ Cannot("does not support video attributes.");
+ }
+}
+
+#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) {
+ if (++j >= MAX_ATTRSTRING)
+ break;
+ 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(NCURSES_PAIRS_T 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;
+ NCURSES_PAIRS_T pair;
+ wchar_t wch[10];
+
+ memset(&ch, 0, sizeof(ch));
+ 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,
+ NCURSES_PAIRS_T 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 = 0;
+ NCURSES_PAIRS_T old_pair = 0;
+
+ (void) (attr_get) (&old_attr, &old_pair, 0);
+ (void) attr_set(attr, pair, 0);
+ addwstr(wide_attr_test_string);
+ (void) 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,
+ NCURSES_COLOR_T *fg, NCURSES_COLOR_T *bg,
+ NCURSES_COLOR_T *tx, int *ac,
+ unsigned *kc, unsigned limit)
+{
+ 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 = limit - 1;
+ else
+ *kc -= 1;
+ break;
+ case 'V':
+ *kc += 1;
+ if (*kc >= limit)
+ *kc = 0;
+ break;
+ case '<':
+ wide_adjust_attr_string(-1);
+ break;
+ case '>':
+ wide_adjust_attr_string(1);
+ break;
+ case 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();
+ NCURSES_COLOR_T fg = COLOR_BLACK; /* color pair 0 is special */
+ NCURSES_COLOR_T bg = COLOR_BLACK;
+ NCURSES_COLOR_T tx = -1;
+ int ac = 0;
+ unsigned j, k;
+ ATTR_TBL my_list[SIZEOF(attrs_to_test)];
+ unsigned my_size = init_attr_list(my_list, term_attrs());
+
+ if (my_size > 1) {
+ if (skip < 0)
+ skip = 0;
+
+ n = skip; /* make it easy */
+ k = my_size - 1;
+ wide_init_attr_string();
+
+ do {
+ int row = 2;
+ NCURSES_PAIRS_T pair = 0;
+ NCURSES_PAIRS_T extras = 0;
+
+ if (use_colors) {
+ pair = (NCURSES_PAIRS_T) (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 < my_size; ++j) {
+ row = wide_show_attr(row, n, j == k,
+ ((attr_t) ac |
+ my_list[j].attr |
+ my_list[k].attr),
+ extras,
+ my_list[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, my_size));
+
+ set_wide_background(0);
+ erase();
+ endwin();
+ } else {
+ Cannot("does not support extended video attributes.");
+ }
+}
+#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 if (color < 0) {
+ strcpy(temp, "default");
+ } else {
+ sprintf(temp, "%.*s", 16, the_color_names[color]);
+ }
+ printw("%-*.*s", width, width, temp);
+ }
+}
+
+static void
+color_legend(WINDOW *helpwin, bool wide)
+{
+ 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 of 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,
+ " a/A toggle altcharset off/on");
+ 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,
+ " r/R toggle reverse on/off");
+ MvWPrintw(helpwin, row++, col,
+ " w/W toggle width between 8/16 colors");
+#if USE_WIDEC_SUPPORT
+ if (wide) {
+ MvWPrintw(helpwin, row++, col,
+ "Wide characters:");
+ MvWPrintw(helpwin, row, col,
+ " x/X toggle text between ASCII and wide-character");
+ }
+#else
+ (void) wide;
+#endif
+}
+
+#define set_color_test(name, value) if (name != value) { name = value; base_row = 0; }
+
+/* generate a color test pattern */
+static void
+color_test(void)
+{
+ NCURSES_PAIRS_T 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_acsc = FALSE;
+ bool opt_bold = FALSE;
+ bool opt_revs = FALSE;
+ bool opt_nums = FALSE;
+ bool opt_wide = FALSE;
+ WINDOW *helpwin;
+
+ if (COLORS * COLORS == COLOR_PAIRS) {
+ int limit = (COLORS - min_colors) * (COLORS - min_colors);
+ if (pairs_max > limit)
+ pairs_max = limit;
+ } else {
+ 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;
+ }
+ per_row -= min_colors;
+
+ row_limit = (pairs_max + per_row - 1) / per_row;
+
+ move(0, 0);
+ (void) printw("There are %d color pairs and %d colors%s\n",
+ pairs_max, COLORS,
+ min_colors ? " besides 'default'" : "");
+
+ clrtobot();
+ MvPrintw(top + 1, 0,
+ "%dx%d matrix of foreground/background colors, bold *%s*\n",
+ row_limit,
+ per_row,
+ opt_bold ? "on" : "off");
+
+ /* show color names/numbers across the top */
+ for (i = 0; i < per_row; i++)
+ show_color_name(top + 2, (i + 1) * width, i + min_colors, opt_wide);
+
+ /* show a grid of colors, with color names/ numbers on the left */
+ for (i = (NCURSES_PAIRS_T) (base_row * per_row); i < pairs_max; i++) {
+ int row = grid_top + (i / per_row) - base_row;
+ int col = (i % per_row + 1) * width;
+ NCURSES_PAIRS_T pair = i;
+
+#define InxToFG(i) (NCURSES_COLOR_T) ((i % (COLORS - min_colors)) + min_colors)
+#define InxToBG(i) (NCURSES_COLOR_T) ((i / (COLORS - min_colors)) + min_colors)
+ if (row >= 0 && move(row, col) != ERR) {
+ NCURSES_COLOR_T fg = InxToFG(i);
+ NCURSES_COLOR_T bg = InxToBG(i);
+
+ init_pair(pair, fg, bg);
+ attron((attr_t) COLOR_PAIR(pair));
+ if (opt_acsc)
+ attron((attr_t) A_ALTCHARSET);
+ if (opt_bold)
+ attron((attr_t) A_BOLD);
+ if (opt_revs)
+ attron((attr_t) A_REVERSE);
+
+ if (opt_nums) {
+ sprintf(numbered, "{%02X}", (int) i);
+ hello = numbered;
+ }
+ printw("%-*.*s", width, width, hello);
+ (void) attrset(A_NORMAL);
+
+ if ((i % per_row) == 0 && InxToFG(i) == min_colors) {
+ show_color_name(row, 0, InxToBG(i), opt_wide);
+ }
+ ++shown;
+ } else if (shown) {
+ break;
+ }
+ }
+
+ switch (wGetchar(stdscr)) {
+ case 'a':
+ opt_acsc = FALSE;
+ break;
+ case 'A':
+ opt_acsc = TRUE;
+ break;
+ case 'b':
+ opt_bold = FALSE;
+ break;
+ case 'B':
+ opt_bold = TRUE;
+ break;
+ case 'n':
+ opt_nums = FALSE;
+ break;
+ case 'N':
+ opt_nums = TRUE;
+ break;
+ case 'r':
+ opt_revs = FALSE;
+ break;
+ case 'R':
+ opt_revs = TRUE;
+ break;
+ case case_QUIT:
+ done = TRUE;
+ continue;
+ case 'w':
+ set_color_test(opt_wide, FALSE);
+ break;
+ case 'W':
+ set_color_test(opt_wide, TRUE);
+ break;
+ case CTRL('p'):
+ case KEY_UP:
+ if (base_row <= 0) {
+ beep();
+ } else {
+ base_row -= 1;
+ }
+ break;
+ case CTRL('n'):
+ case KEY_DOWN:
+ if (base_row + page_size >= row_limit) {
+ beep();
+ } else {
+ base_row += 1;
+ }
+ break;
+ case CTRL('b'):
+ case KEY_PREVIOUS:
+ case KEY_PPAGE:
+ if (base_row <= 0) {
+ beep();
+ } else {
+ base_row -= (page_size - 1);
+ if (base_row < 0)
+ base_row = 0;
+ }
+ break;
+ case CTRL('f'):
+ case KEY_NEXT:
+ case KEY_NPAGE:
+ if (base_row + page_size >= row_limit) {
+ beep();
+ } else {
+ base_row += page_size - 1;
+ if (base_row + page_size >= row_limit) {
+ base_row = row_limit - page_size - 1;
+ }
+ }
+ break;
+ case '?':
+ if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
+ box(helpwin, 0, 0);
+ color_legend(helpwin, FALSE);
+ wGetchar(helpwin);
+ delwin(helpwin);
+ }
+ break;
+ default:
+ beep();
+ continue;
+ }
+ }
+
+ erase();
+ endwin();
+}
+
+#if USE_WIDEC_SUPPORT
+/* generate a color test pattern */
+static void
+wide_color_test(void)
+{
+ int i;
+ int top = 0, width;
+ int base_row = 0;
+ int grid_top = top + 3;
+ int page_size = (LINES - grid_top);
+ int pairs_max = (unsigned short) (-1);
+ int row_limit;
+ int per_row;
+ char numbered[80];
+ const char *hello;
+ bool done = FALSE;
+ bool opt_acsc = FALSE;
+ bool opt_bold = FALSE;
+ bool opt_revs = FALSE;
+ bool opt_wide = FALSE;
+ bool opt_nums = FALSE;
+ bool opt_xchr = FALSE;
+ wchar_t buffer[80];
+ WINDOW *helpwin;
+
+ if (COLORS * COLORS == COLOR_PAIRS) {
+ int limit = (COLORS - min_colors) * (COLORS - min_colors);
+ if (pairs_max > limit)
+ pairs_max = limit;
+ } else {
+ 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;
+ }
+ per_row -= min_colors;
+
+ if (opt_xchr) {
+ make_fullwidth_text(buffer, hello);
+ width *= 2;
+ per_row /= 2;
+ } else {
+ make_narrow_text(buffer, hello);
+ }
+
+ row_limit = (pairs_max + per_row - 1) / per_row;
+
+ move(0, 0);
+ (void) printw("There are %d color pairs and %d colors%s\n",
+ pairs_max, COLORS,
+ min_colors ? " besides 'default'" : "");
+
+ clrtobot();
+ MvPrintw(top + 1, 0,
+ "%dx%d matrix of foreground/background colors, bold *%s*\n",
+ row_limit,
+ per_row,
+ opt_bold ? "on" : "off");
+
+ /* show color names/numbers across the top */
+ for (i = 0; i < per_row; i++)
+ show_color_name(top + 2, (i + 1) * width, i + min_colors, opt_wide);
+
+ /* show a grid of colors, with color names/ numbers on the left */
+ for (i = (base_row * per_row); i < pairs_max; i++) {
+ int row = grid_top + (i / per_row) - base_row;
+ int col = (i % per_row + 1) * width;
+ NCURSES_PAIRS_T pair = (NCURSES_PAIRS_T) i;
+
+ if (row >= 0 && move(row, col) != ERR) {
+ init_pair(pair, InxToFG(i), InxToBG(i));
+ (void) color_set(pair, NULL);
+ if (opt_acsc)
+ attr_on((attr_t) A_ALTCHARSET, NULL);
+ if (opt_bold)
+ attr_on((attr_t) A_BOLD, NULL);
+ if (opt_revs)
+ attr_on((attr_t) A_REVERSE, NULL);
+
+ if (opt_nums) {
+ sprintf(numbered, "{%02X}", i);
+ if (opt_xchr) {
+ make_fullwidth_text(buffer, numbered);
+ } else {
+ make_narrow_text(buffer, numbered);
+ }
+ }
+ addnwstr(buffer, width);
+ (void) attr_set(A_NORMAL, 0, NULL);
+
+ if ((i % per_row) == 0 && InxToFG(i) == min_colors) {
+ show_color_name(row, 0, InxToBG(i), opt_wide);
+ }
+ ++shown;
+ } else if (shown) {
+ break;
+ }
+ }
+
+ switch (wGetchar(stdscr)) {
+ case 'a':
+ opt_acsc = FALSE;
+ break;
+ case 'A':
+ opt_acsc = TRUE;
+ break;
+ case 'b':
+ opt_bold = FALSE;
+ break;
+ case 'B':
+ opt_bold = TRUE;
+ break;
+ case 'n':
+ opt_nums = FALSE;
+ break;
+ case 'N':
+ opt_nums = TRUE;
+ break;
+ case 'r':
+ opt_revs = FALSE;
+ break;
+ case 'R':
+ opt_revs = TRUE;
+ break;
+ case case_QUIT:
+ done = TRUE;
+ continue;
+ case 'w':
+ set_color_test(opt_wide, FALSE);
+ break;
+ case 'W':
+ set_color_test(opt_wide, TRUE);
+ break;
+ case 'x':
+ opt_xchr = FALSE;
+ break;
+ case 'X':
+ opt_xchr = TRUE;
+ break;
+ case CTRL('p'):
+ case KEY_UP:
+ if (base_row <= 0) {
+ beep();
+ } else {
+ base_row -= 1;
+ }
+ break;
+ case CTRL('n'):
+ case KEY_DOWN:
+ if (base_row + page_size >= row_limit) {
+ beep();
+ } else {
+ base_row += 1;
+ }
+ break;
+ case CTRL('b'):
+ case KEY_PREVIOUS:
+ case KEY_PPAGE:
+ if (base_row <= 0) {
+ beep();
+ } else {
+ base_row -= (page_size - 1);
+ if (base_row < 0)
+ base_row = 0;
+ }
+ break;
+ case CTRL('f'):
+ case KEY_NEXT:
+ case KEY_NPAGE:
+ if (base_row + page_size >= row_limit) {
+ beep();
+ } else {
+ base_row += page_size - 1;
+ if (base_row + page_size >= row_limit) {
+ base_row = row_limit - page_size - 1;
+ }
+ }
+ break;
+ case '?':
+ if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
+ box(helpwin, 0, 0);
+ color_legend(helpwin, TRUE);
+ wGetchar(helpwin);
+ delwin(helpwin);
+ }
+ break;
+ default:
+ beep();
+ continue;
+ }
+ }
+
+ erase();
+ endwin();
+}
+#endif /* USE_WIDEC_SUPPORT */
+
+static void
+change_color(NCURSES_PAIRS_T current, int field, int value, int usebase)
+{
+ NCURSES_COLOR_T red, green, blue;
+
+ color_content(current, &red, &green, &blue);
+
+ switch (field) {
+ case 0:
+ red = (NCURSES_COLOR_T) (usebase ? (red + value) : value);
+ break;
+ case 1:
+ green = (NCURSES_COLOR_T) (usebase ? (green + value) : value);
+ break;
+ case 2:
+ blue = (NCURSES_COLOR_T) (usebase ? (blue + value) : value);
+ break;
+ }
+
+ if (init_color(current, red, green, blue) == ERR)
+ beep();
+}
+
+static void
+init_all_colors(void)
+{
+ NCURSES_PAIRS_T c;
+
+ for (c = 0; c < COLORS; ++c)
+ init_color(c,
+ all_colors[c].red,
+ all_colors[c].green,
+ all_colors[c].blue);
+}
+
+#define scaled_rgb(n) ((255 * (n)) / 1000)
+
+static void
+color_edit(void)
+/* display the color test pattern, without trying to edit colors */
+{
+ int i;
+ int current = 0;
+ int this_c = 0, value = 0, field = 0;
+ int last_c;
+ int top_color = 0;
+ int page_size = (LINES - 6);
+
+ init_all_colors();
+ refresh();
+
+ for (i = 0; i < max_colors; i++)
+ init_pair((NCURSES_PAIRS_T) i,
+ (NCURSES_COLOR_T) COLOR_WHITE,
+ (NCURSES_COLOR_T) i);
+
+ MvPrintw(LINES - 2, 0, "Number: %d", value);
+
+ do {
+ NCURSES_COLOR_T red, green, blue;
+
+ attron(A_BOLD);
+ MvAddStr(0, 20, "Color RGB Value Editing");
+ attroff(A_BOLD);
+
+ for (i = (NCURSES_COLOR_T) top_color;
+ (i - top_color < page_size)
+ && (i < max_colors); i++) {
+ char numeric[80];
+
+ sprintf(numeric, "[%d]", i);
+ MvPrintw(2 + i - top_color, 0, "%c %-8s:",
+ (i == current ? '>' : ' '),
+ (i < (int) SIZEOF(the_color_names)
+ ? the_color_names[i] : numeric));
+ (void) attrset((attr_t) COLOR_PAIR(i));
+ addstr(" ");
+ (void) attrset(A_NORMAL);
+
+ color_content((NCURSES_PAIRS_T) i, &red, &green, &blue);
+ addstr(" R = ");
+ if (current == i && field == 0)
+ attron(A_STANDOUT);
+ printw("%04d", (int) red);
+ if (current == i && field == 0)
+ (void) attrset(A_NORMAL);
+ addstr(", G = ");
+ if (current == i && field == 1)
+ attron(A_STANDOUT);
+ printw("%04d", (int) green);
+ if (current == i && field == 1)
+ (void) attrset(A_NORMAL);
+ addstr(", B = ");
+ if (current == i && field == 2)
+ attron(A_STANDOUT);
+ printw("%04d", (int) blue);
+ if (current == i && field == 2)
+ (void) attrset(A_NORMAL);
+ (void) attrset(A_NORMAL);
+ printw(" ( %3d %3d %3d )",
+ (int) scaled_rgb(red),
+ (int) scaled_rgb(green),
+ (int) scaled_rgb(blue));
+ }
+
+ MvAddStr(LINES - 3, 0,
+ "Use up/down to select a color, left/right to change fields.");
+ MvAddStr(LINES - 2, 0,
+ "Modify field by typing nnn=, nnn-, or nnn+. ? for help.");
+
+ move(2 + current - top_color, 0);
+
+ last_c = this_c;
+ this_c = Getchar();
+ if (this_c < 256 && isdigit(this_c) && !isdigit(last_c))
+ value = 0;
+
+ switch (this_c) {
+ case CTRL('b'):
+ case KEY_PPAGE:
+ if (current > 0)
+ current -= (page_size - 1);
+ else
+ beep();
+ break;
+
+ case CTRL('f'):
+ case KEY_NPAGE:
+ if (current < (max_colors - 1))
+ current += (page_size - 1);
+ else
+ beep();
+ break;
+
+ case CTRL('p'):
+ case KEY_UP:
+ current = (current == 0 ? (max_colors - 1) : current - 1);
+ break;
+
+ case CTRL('n'):
+ case KEY_DOWN:
+ current = (current == (max_colors - 1) ? 0 : current + 1);
+ break;
+
+ case KEY_RIGHT:
+ field = (field == 2 ? 0 : field + 1);
+ break;
+
+ case KEY_LEFT:
+ field = (field == 0 ? 2 : field - 1);
+ break;
+
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ value = value * 10 + (this_c - '0');
+ break;
+
+ case '+':
+ change_color((NCURSES_PAIRS_T) current, field, value, 1);
+ break;
+
+ case '-':
+ change_color((NCURSES_PAIRS_T) current, field, -value, 1);
+ break;
+
+ case '=':
+ change_color((NCURSES_PAIRS_T) current, field, value, 0);
+ break;
+
+ case '?':
+ erase();
+ P(" RGB Value Editing Help");
+ P("");
+ P("You are in the RGB value editor. Use the arrow keys to select one of");
+ P("the fields in one of the RGB triples of the current colors; the one");
+ P("currently selected will be reverse-video highlighted.");
+ P("");
+ P("To change a field, enter the digits of the new value; they are echoed");
+ P("as entered. Finish by typing `='. The change will take effect instantly.");
+ P("To increment or decrement a value, use the same procedure, but finish");
+ P("with a `+' or `-'.");
+ P("");
+ P("Press 'm' to invoke the top-level menu with the current color settings.");
+ P("To quit, do ESC");
+
+ Pause();
+ erase();
+ break;
+
+ case 'm':
+ endwin();
+ main_menu(FALSE);
+ for (i = 0; i < max_colors; i++)
+ init_pair((NCURSES_PAIRS_T) i,
+ (NCURSES_COLOR_T) COLOR_WHITE,
+ (NCURSES_COLOR_T) i);
+ refresh();
+ break;
+
+ case case_QUIT:
+ break;
+
+ default:
+ beep();
+ break;
+ }
+
+ if (current < 0)
+ current = 0;
+ if (current >= max_colors)
+ current = max_colors - 1;
+ if (current < top_color)
+ top_color = current;
+ if (current - top_color >= page_size)
+ top_color = current - (page_size - 1);
+
+ MvPrintw(LINES - 1, 0, "Number: %d", value);
+ clrtoeol();
+ } while
+ (!isQuit(this_c));
+
+ erase();
+
+ /*
+ * ncurses does not reset each color individually when calling endwin().
+ */
+ init_all_colors();
+
+ endwin();
+}
+
+/****************************************************************************
+ *
+ * Alternate character-set stuff
+ *
+ ****************************************************************************/
+static bool
+cycle_attr(int ch, unsigned *at_code, chtype *attr, ATTR_TBL * list, unsigned limit)
+{
+ bool result = TRUE;
+
+ switch (ch) {
+ case 'v':
+ if ((*at_code += 1) >= limit)
+ *at_code = 0;
+ break;
+ case 'V':
+ if (*at_code == 0)
+ *at_code = limit - 1;
+ else
+ *at_code -= 1;
+ break;
+ default:
+ result = FALSE;
+ break;
+ }
+ if (result)
+ *attr = list[*at_code].attr;
+ return result;
+}
+
+static bool
+cycle_colors(int ch, int *fg, int *bg, NCURSES_PAIRS_T *pair)
+{
+ bool result = FALSE;
+
+ if (use_colors) {
+ result = TRUE;
+ switch (ch) {
+ case 'F':
+ if ((*fg -= 1) < 0)
+ *fg = COLORS - 1;
+ break;
+ case 'f':
+ if ((*fg += 1) >= COLORS)
+ *fg = 0;
+ break;
+ case 'B':
+ if ((*bg -= 1) < 0)
+ *bg = COLORS - 1;
+ break;
+ case 'b':
+ if ((*bg += 1) >= COLORS)
+ *bg = 0;
+ break;
+ default:
+ result = FALSE;
+ break;
+ }
+ if (result) {
+ *pair = (NCURSES_PAIRS_T) (*fg != COLOR_BLACK || *bg != COLOR_BLACK);
+ if (*pair != 0) {
+ *pair = 1;
+ if (init_pair(*pair,
+ (NCURSES_COLOR_T) *fg,
+ (NCURSES_COLOR_T) *bg) == ERR) {
+ result = FALSE;
+ }
+ }
+ }
+ }
+ return result;
+}
+
+/****************************************************************************
+ *
+ * Soft-key label test
+ *
+ ****************************************************************************/
+
+#if USE_SOFTKEYS
+
+#define SLK_HELP 17
+#define SLK_WORK (SLK_HELP + 3)
+
+static void
+slk_help(void)
+{
+ static const char *table[] =
+ {
+ "Available commands are:"
+ ,""
+ ,"^L -- repaint this message and activate soft keys"
+ ,"a/d -- activate/disable soft keys"
+ ,"c -- set centered format for labels"
+ ,"l -- set left-justified format for labels"
+ ,"r -- set right-justified format for labels"
+ ,"[12345678] -- set label; labels are numbered 1 through 8"
+ ,"e -- erase stdscr (should not erase labels)"
+ ,"s -- test scrolling of shortened screen"
+ ,"v/V -- cycle through video attributes"
+#if HAVE_SLK_COLOR
+ ,"F/f/B/b -- cycle through foreground/background colors"
+#endif
+ ,"ESC -- return to main menu"
+ ,""
+ ,"Note: if activating the soft keys causes your terminal to scroll up"
+ ,"one line, your terminal auto-scrolls when anything is written to the"
+ ,"last screen position. The ncurses code does not yet handle this"
+ ,"gracefully."
+ };
+ unsigned j;
+
+ move(2, 0);
+ for (j = 0; j < SIZEOF(table); ++j) {
+ P(table[j]);
+ }
+ refresh();
+}
+
+#if HAVE_SLK_COLOR
+static void
+call_slk_color(int fg, int bg)
+{
+ init_pair(1, (NCURSES_COLOR_T) bg, (NCURSES_COLOR_T) fg);
+ slk_color(1);
+ MvPrintw(SLK_WORK, 0, "Colors %d/%d\n", fg, bg);
+ clrtoeol();
+ slk_touch();
+ slk_noutrefresh();
+ refresh();
+}
+#endif
+
+static void
+slk_test(void)
+/* exercise the soft keys */
+{
+ int c, fmt = 1;
+ char buf[9];
+ char *s;
+ chtype attr = A_NORMAL;
+ unsigned at_code = 0;
+#if HAVE_SLK_COLOR
+ int fg = COLOR_BLACK;
+ int bg = COLOR_WHITE;
+ NCURSES_PAIRS_T pair = 0;
+#endif
+ ATTR_TBL my_list[SIZEOF(attrs_to_test)];
+ unsigned my_size = init_attr_list(my_list, termattrs());
+
+ c = CTRL('l');
+#if HAVE_SLK_COLOR
+ if (use_colors) {
+ call_slk_color(fg, bg);
+ }
+#endif
+
+ do {
+ move(0, 0);
+ switch (c) {
+ case CTRL('l'):
+ erase();
+ attron(A_BOLD);
+ MvAddStr(0, 20, "Soft Key Exerciser");
+ attroff(A_BOLD);
+
+ slk_help();
+ /* fall through */
+
+ case 'a':
+ slk_restore();
+ break;
+
+ case 'e':
+ wclear(stdscr);
+ break;
+
+ case 's':
+ MvPrintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: ");
+ while ((c = Getchar()) != 'Q' && (c != ERR))
+ addch((chtype) c);
+ break;
+
+ case 'd':
+ slk_clear();
+ break;
+
+ case 'l':
+ fmt = 0;
+ break;
+
+ case 'c':
+ fmt = 1;
+ break;
+
+ case 'r':
+ fmt = 2;
+ break;
+
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ MvAddStr(SLK_WORK, 0, "Please enter the label value: ");
+ strcpy(buf, "");
+ if ((s = slk_label(c - '0')) != 0) {
+ strncpy(buf, s, (size_t) 8);
+ }
+ wGetstring(stdscr, buf, 8);
+ slk_set((c - '0'), buf, fmt);
+ slk_refresh();
+ move(SLK_WORK, 0);
+ clrtobot();
+ break;
+
+ case case_QUIT:
+ goto done;
+
+#if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
+ case KEY_RESIZE:
+ wnoutrefresh(stdscr);
+ break;
+#endif
+
+ default:
+ if (cycle_attr(c, &at_code, &attr, my_list, my_size)) {
+ slk_attrset(attr);
+ slk_touch();
+ slk_noutrefresh();
+ break;
+ }
+#if HAVE_SLK_COLOR
+ if (cycle_colors(c, &fg, &bg, &pair)) {
+ if (use_colors) {
+ call_slk_color(fg, bg);
+ } else {
+ beep();
+ }
+ break;
+ }
+#endif
+ beep();
+ break;
+ }
+ } while (!isQuit(c = Getchar()));
+
+ done:
+ slk_clear();
+ erase();
+ endwin();
+}
+
+#if USE_WIDEC_SUPPORT
+#define SLKLEN 8
+static void
+wide_slk_test(void)
+/* exercise the soft keys */
+{
+ int c, fmt = 1;
+ wchar_t buf[SLKLEN + 1];
+ char *s;
+ chtype attr = A_NORMAL;
+ unsigned at_code = 0;
+ int fg = COLOR_BLACK;
+ int bg = COLOR_WHITE;
+ NCURSES_PAIRS_T pair = 0;
+ ATTR_TBL my_list[SIZEOF(attrs_to_test)];
+ unsigned my_size = init_attr_list(my_list, term_attrs());
+
+ c = CTRL('l');
+ if (use_colors) {
+ call_slk_color(fg, bg);
+ }
+ do {
+ move(0, 0);
+ switch (c) {
+ case CTRL('l'):
+ erase();
+ attr_on(WA_BOLD, NULL);
+ MvAddStr(0, 20, "Soft Key Exerciser");
+ attr_off(WA_BOLD, NULL);
+
+ slk_help();
+ /* fall through */
+
+ case 'a':
+ slk_restore();
+ break;
+
+ case 'e':
+ wclear(stdscr);
+ break;
+
+ case 's':
+ MvPrintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: ");
+ while ((c = Getchar()) != 'Q' && (c != ERR))
+ addch((chtype) c);
+ break;
+
+ case 'd':
+ slk_clear();
+ break;
+
+ case 'l':
+ fmt = 0;
+ break;
+
+ case 'c':
+ fmt = 1;
+ break;
+
+ case 'r':
+ fmt = 2;
+ break;
+
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ MvAddStr(SLK_WORK, 0, "Please enter the label value: ");
+ *buf = 0;
+ if ((s = slk_label(c - '0')) != 0) {
+ char *temp = strdup(s);
+ size_t used = strlen(temp);
+ size_t want = SLKLEN;
+ size_t test;
+#ifndef state_unused
+ mbstate_t state;
+#endif
+
+ buf[0] = L'\0';
+ while (want > 0 && used != 0) {
+ const char *base = s;
+ reset_mbytes(state);
+ test = count_mbytes(base, 0, &state);
+ if (test == (size_t) -1) {
+ temp[--used] = 0;
+ } else if (test > want) {
+ temp[--used] = 0;
+ } else {
+ reset_mbytes(state);
+ trans_mbytes(buf, base, want, &state);
+ break;
+ }
+ }
+ free(temp);
+ }
+ wGet_wstring(stdscr, buf, SLKLEN);
+ slk_wset((c - '0'), buf, fmt);
+ slk_refresh();
+ move(SLK_WORK, 0);
+ clrtobot();
+ break;
+
+ case case_QUIT:
+ goto done;
+
+ case 'F':
+ if (use_colors) {
+ fg = (NCURSES_COLOR_T) ((fg + 1) % COLORS);
+ call_slk_color(fg, bg);
+ }
+ break;
+ case 'B':
+ if (use_colors) {
+ bg = (NCURSES_COLOR_T) ((bg + 1) % COLORS);
+ call_slk_color(fg, bg);