micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright 2021,2022 Thomas E. Dickey * |
| 3 | * * |
| 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: * |
| 11 | * * |
| 12 | * The above copyright notice and this permission notice shall be included * |
| 13 | * in all copies or substantial portions of the Software. * |
| 14 | * * |
| 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. * |
| 22 | * * |
| 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 * |
| 26 | * authorization. * |
| 27 | ****************************************************************************/ |
| 28 | /* |
| 29 | * $Id: back_ground.c,v 1.9 2022/12/10 22:28:50 tom Exp $ |
| 30 | */ |
| 31 | |
| 32 | #include <test.priv.h> |
| 33 | |
| 34 | #if USE_WIDEC_SUPPORT |
| 35 | |
| 36 | #define NEED_COLOR_CODE 1 |
| 37 | #define NEED_COLOR_NAME 1 |
| 38 | #include <color_name.h> |
| 39 | #include <dump_window.h> |
| 40 | |
| 41 | static int default_bg = COLOR_BLACK; |
| 42 | static int default_fg = COLOR_WHITE; |
| 43 | static wchar_t wide_fill = L' '; |
| 44 | |
| 45 | static wchar_t |
| 46 | decode_wchar(const char *value) |
| 47 | { |
| 48 | long result; |
| 49 | char *next = NULL; |
| 50 | int radix = 0; |
| 51 | |
| 52 | if (!strncmp(value, "U+", 2)) { |
| 53 | value += 2; |
| 54 | radix = 16; |
| 55 | } |
| 56 | result = strtol(value, &next, radix); |
| 57 | if (next == value || (next == NULL || *next != '\0')) { |
| 58 | fprintf(stderr, "decoding wchar_t: %s\n", value); |
| 59 | exit(EXIT_FAILURE); |
| 60 | } |
| 61 | return (wchar_t) result; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | test_background(void) |
| 66 | { |
| 67 | NCURSES_COLOR_T f, b; |
| 68 | int row; |
| 69 | int chr; |
| 70 | wchar_t blank[2]; |
| 71 | wchar_t graphics[2]; |
| 72 | cchar_t data; |
| 73 | |
| 74 | if (pair_content(0, &f, &b) == ERR) { |
| 75 | printw("pair 0 contains no data\n"); |
| 76 | } else { |
| 77 | printw("pair 0 contains (%d,%d)\n", (int) f, (int) b); |
| 78 | } |
| 79 | dump_window(stdscr); |
| 80 | |
| 81 | blank[0] = wide_fill; |
| 82 | blank[1] = L'\0'; |
| 83 | |
| 84 | printw("Initializing pair 1 to red/%s\n", color_name(default_bg)); |
| 85 | init_pair(1, COLOR_RED, (NCURSES_COLOR_T) default_bg); |
| 86 | setcchar(&data, blank, A_NORMAL, 1, NULL); |
| 87 | bkgrndset(&data); |
| 88 | printw("RED/BLACK\n"); |
| 89 | dump_window(stdscr); |
| 90 | |
| 91 | printw("Initializing pair 2 to %s/blue\n", color_name(default_fg)); |
| 92 | init_pair(2, (NCURSES_COLOR_T) default_fg, COLOR_BLUE); |
| 93 | setcchar(&data, blank, A_NORMAL, 2, NULL); |
| 94 | bkgrndset(&data); |
| 95 | printw("This line should be %s/blue\n", color_name(default_fg)); |
| 96 | dump_window(stdscr); |
| 97 | |
| 98 | printw("Initializing pair 3 to %s/cyan (ACS_HLINE)\n", color_name(default_fg)); |
| 99 | init_pair(3, (NCURSES_COLOR_T) default_fg, COLOR_CYAN); |
| 100 | printw("...and drawing a box which should be followed by lines\n"); |
| 101 | graphics[0] = ACS_HLINE & A_CHARTEXT; |
| 102 | graphics[1] = L'\0'; |
| 103 | setcchar(&data, graphics, A_ALTCHARSET, 3, NULL); |
| 104 | bkgrndset(&data); |
| 105 | /* |
| 106 | * Characters from vt100 line-drawing should be mapped to line-drawing, |
| 107 | * since A_ALTCHARSET is set in the background, and the character part |
| 108 | * of the background is replaced by the nonblank characters written. |
| 109 | * |
| 110 | * Characters not in the line-drawing range are usually sent as-is. |
| 111 | * |
| 112 | * With SVr4 curses it is possible to rely on this to mix uppercase text |
| 113 | * with the (lowercase) line-drawing characters. ncurses uses some of |
| 114 | * the uppercase characters for encoding thick- and double-lines. |
| 115 | */ |
| 116 | row = 7; |
| 117 | mvprintw(row++, 10, "l"); |
| 118 | for (chr = 0; chr < 32; ++chr) |
| 119 | AddCh(' '); |
| 120 | printw("x\n"); |
| 121 | chr = 32; |
| 122 | while (chr < 128) { |
| 123 | if ((chr % 32) == 0) |
| 124 | mvprintw(row++, 10, "x"); |
| 125 | AddCh((chr == 127) ? ' ' : chr); |
| 126 | if ((++chr % 32) == 0) |
| 127 | printw("x\n"); |
| 128 | } |
| 129 | mvprintw(row++, 10, "m"); |
| 130 | for (chr = 0; chr < 32; ++chr) |
| 131 | AddCh(' '); |
| 132 | printw("j\n"); |
| 133 | dump_window(stdscr); |
| 134 | |
| 135 | setcchar(&data, blank, A_NORMAL, 0, NULL); |
| 136 | bkgrndset(&data); |
| 137 | printw("Default Colors\n"); |
| 138 | dump_window(stdscr); |
| 139 | |
| 140 | printw("Resetting colors to pair 1\n"); |
| 141 | setcchar(&data, blank, A_NORMAL, 1, NULL); |
| 142 | bkgrndset(&data); |
| 143 | printw("This line should be red/%s\n", color_name(default_bg)); |
| 144 | dump_window(stdscr); |
| 145 | |
| 146 | printw("Setting screen to pair 0\n"); |
| 147 | setcchar(&data, blank, A_NORMAL, 0, NULL); |
| 148 | bkgrndset(&data); |
| 149 | dump_window(stdscr); |
| 150 | |
| 151 | printw("Setting screen to pair 1\n"); |
| 152 | setcchar(&data, blank, A_NORMAL, 1, NULL); |
| 153 | bkgrndset(&data); |
| 154 | dump_window(stdscr); |
| 155 | |
| 156 | printw("Setting screen to pair 2\n"); |
| 157 | setcchar(&data, blank, A_NORMAL, 2, NULL); |
| 158 | bkgrndset(&data); |
| 159 | dump_window(stdscr); |
| 160 | |
| 161 | printw("Setting screen to pair 3\n"); |
| 162 | setcchar(&data, blank, A_NORMAL, 3, NULL); |
| 163 | bkgrndset(&data); |
| 164 | dump_window(stdscr); |
| 165 | |
| 166 | printw("Setting screen to pair 0\n"); |
| 167 | setcchar(&data, blank, A_NORMAL, 0, NULL); |
| 168 | bkgrndset(&data); |
| 169 | dump_window(stdscr); |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | usage(int ok) |
| 174 | { |
| 175 | static const char *msg[] = |
| 176 | { |
| 177 | "Usage: background [options]" |
| 178 | ,"" |
| 179 | ,USAGE_COMMON |
| 180 | ,"Options:" |
| 181 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 182 | ," -a invoke assume_default_colors, repeat to use in init_pair" |
| 183 | #endif |
| 184 | ," -b XXX specify background color" |
| 185 | #if HAVE_USE_DEFAULT_COLORS |
| 186 | ," -d invoke use_default_colors, repeat to use in init_pair" |
| 187 | #endif |
| 188 | ," -f XXX specify foreground color" |
| 189 | ," -l FILE log window-dumps to this file" |
| 190 | ," -w fill background with stipple pattern" |
| 191 | ," -W CODE fill background with this Unicode value" |
| 192 | }; |
| 193 | size_t n; |
| 194 | |
| 195 | for (n = 0; n < SIZEOF(msg); n++) |
| 196 | fprintf(stderr, "%s\n", msg[n]); |
| 197 | |
| 198 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 199 | } |
| 200 | /* *INDENT-OFF* */ |
| 201 | VERSION_COMMON() |
| 202 | /* *INDENT-ON* */ |
| 203 | |
| 204 | int |
| 205 | main(int argc, char *argv[]) |
| 206 | { |
| 207 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 208 | int a_option = 0; |
| 209 | #endif |
| 210 | #if HAVE_USE_DEFAULT_COLORS |
| 211 | int d_option = 0; |
| 212 | #endif |
| 213 | int ch; |
| 214 | |
| 215 | setlocale(LC_ALL, ""); |
| 216 | |
| 217 | while ((ch = getopt(argc, argv, OPTS_COMMON "ab:df:l:wW:")) != -1) { |
| 218 | switch (ch) { |
| 219 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 220 | case 'a': |
| 221 | ++a_option; |
| 222 | break; |
| 223 | #endif |
| 224 | case 'b': |
| 225 | default_bg = color_code(optarg); |
| 226 | break; |
| 227 | #if HAVE_USE_DEFAULT_COLORS |
| 228 | case 'd': |
| 229 | ++d_option; |
| 230 | break; |
| 231 | #endif |
| 232 | case 'f': |
| 233 | default_fg = color_code(optarg); |
| 234 | break; |
| 235 | case 'l': |
| 236 | if (!open_dump(optarg)) |
| 237 | usage(FALSE); |
| 238 | break; |
| 239 | case 'w': |
| 240 | wide_fill = L'\u2591'; |
| 241 | break; |
| 242 | case 'W': |
| 243 | wide_fill = decode_wchar(optarg); |
| 244 | break; |
| 245 | case OPTS_VERSION: |
| 246 | show_version(argv); |
| 247 | ExitProgram(EXIT_SUCCESS); |
| 248 | default: |
| 249 | usage(ch == OPTS_USAGE); |
| 250 | /* NOTREACHED */ |
| 251 | } |
| 252 | } |
| 253 | #if HAVE_USE_DEFAULT_COLORS && HAVE_ASSUME_DEFAULT_COLORS |
| 254 | if (a_option && d_option) { |
| 255 | fprintf(stderr, "Use either -a or -d option, but not both\n"); |
| 256 | ExitProgram(EXIT_FAILURE); |
| 257 | } |
| 258 | #endif |
| 259 | |
| 260 | initscr(); |
| 261 | cbreak(); |
| 262 | noecho(); |
| 263 | |
| 264 | if (has_colors()) { |
| 265 | start_color(); |
| 266 | |
| 267 | #if HAVE_USE_DEFAULT_COLORS |
| 268 | if (d_option) { |
| 269 | printw("Using default colors...\n"); |
| 270 | use_default_colors(); |
| 271 | if (d_option > 1) { |
| 272 | default_fg = -1; |
| 273 | default_bg = -1; |
| 274 | } |
| 275 | } |
| 276 | #endif |
| 277 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 278 | if (a_option) { |
| 279 | printw("Using assumed colors %s/%s...\n", |
| 280 | color_name(default_fg), |
| 281 | color_name(default_bg)); |
| 282 | assume_default_colors(default_fg, default_bg); |
| 283 | if (a_option > 1) { |
| 284 | default_fg = -1; |
| 285 | default_bg = -1; |
| 286 | } |
| 287 | } |
| 288 | #endif |
| 289 | |
| 290 | test_background(); |
| 291 | |
| 292 | } else { |
| 293 | printw("This demo requires a color terminal"); |
| 294 | getch(); |
| 295 | } |
| 296 | endwin(); |
| 297 | close_dump(); |
| 298 | ExitProgram(EXIT_SUCCESS); |
| 299 | } |
| 300 | |
| 301 | #else |
| 302 | int |
| 303 | main(void) |
| 304 | { |
| 305 | printf("This program requires the wide-curses library\n"); |
| 306 | ExitProgram(EXIT_FAILURE); |
| 307 | } |
| 308 | #endif /* USE_WIDEC_SUPPORT */ |