Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 2003-2012,2014 Free Software Foundation, Inc. * |
| 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: background.c,v 1.15 2014/08/09 22:31:23 tom Exp $ |
| 30 | */ |
| 31 | |
| 32 | #define NEED_COLOR_CODE 1 |
| 33 | #define NEED_COLOR_NAME 1 |
| 34 | #include <color_name.h> |
| 35 | |
| 36 | static int default_bg = COLOR_BLACK; |
| 37 | static int default_fg = COLOR_WHITE; |
| 38 | |
| 39 | static void |
| 40 | test_background(void) |
| 41 | { |
| 42 | NCURSES_COLOR_T f, b; |
| 43 | int row; |
| 44 | int chr; |
| 45 | |
| 46 | if (pair_content(0, &f, &b) == ERR) { |
| 47 | printw("pair 0 contains no data\n"); |
| 48 | } else { |
| 49 | printw("pair 0 contains (%d,%d)\n", (int) f, (int) b); |
| 50 | } |
| 51 | getch(); |
| 52 | |
| 53 | printw("Initializing pair 1 to red/%s\n", color_name(default_bg)); |
| 54 | init_pair(1, COLOR_RED, (NCURSES_COLOR_T) default_bg); |
| 55 | bkgdset((chtype) (' ' | COLOR_PAIR(1))); |
| 56 | printw("RED/BLACK\n"); |
| 57 | getch(); |
| 58 | |
| 59 | printw("Initializing pair 2 to %s/blue\n", color_name(default_fg)); |
| 60 | init_pair(2, (NCURSES_COLOR_T) default_fg, COLOR_BLUE); |
| 61 | bkgdset((chtype) (' ' | COLOR_PAIR(2))); |
| 62 | printw("This line should be %s/blue\n", color_name(default_fg)); |
| 63 | getch(); |
| 64 | |
| 65 | printw("Initializing pair 3 to %s/cyan (ACS_HLINE)\n", color_name(default_fg)); |
| 66 | init_pair(3, (NCURSES_COLOR_T) default_fg, COLOR_CYAN); |
| 67 | printw("...and drawing a box which should be followed by lines\n"); |
| 68 | bkgdset(ACS_HLINE | (attr_t) COLOR_PAIR(3)); |
| 69 | /* |
| 70 | * Characters from vt100 line-drawing should be mapped to line-drawing, |
| 71 | * since A_ALTCHARSET is set in the background, and the character part |
| 72 | * of the background is replaced by the nonblank characters written. |
| 73 | * |
| 74 | * Characters not in the line-drawing range are usually sent as-is. |
| 75 | * |
| 76 | * With SVr4 curses it is possible to rely on this to mix uppercase text |
| 77 | * with the (lowercase) line-drawing characters. ncurses uses some of |
| 78 | * the uppercase characters for encoding thick- and double-lines. |
| 79 | */ |
| 80 | row = 7; |
| 81 | mvprintw(row++, 10, "l"); |
| 82 | for (chr = 0; chr < 32; ++chr) |
| 83 | addch(' '); |
| 84 | printw("x\n"); |
| 85 | chr = 32; |
| 86 | while (chr < 128) { |
| 87 | if ((chr % 32) == 0) |
| 88 | mvprintw(row++, 10, "x"); |
| 89 | addch((chtype) ((chr == 127) ? ' ' : chr)); |
| 90 | if ((++chr % 32) == 0) |
| 91 | printw("x\n"); |
| 92 | } |
| 93 | mvprintw(row++, 10, "m"); |
| 94 | for (chr = 0; chr < 32; ++chr) |
| 95 | addch(' '); |
| 96 | printw("j\n"); |
| 97 | getch(); |
| 98 | |
| 99 | bkgdset((chtype) (' ' | COLOR_PAIR(0))); |
| 100 | printw("Default Colors\n"); |
| 101 | getch(); |
| 102 | |
| 103 | printw("Resetting colors to pair 1\n"); |
| 104 | bkgdset((chtype) (' ' | COLOR_PAIR(1))); |
| 105 | printw("This line should be red/%s\n", color_name(default_bg)); |
| 106 | getch(); |
| 107 | |
| 108 | printw("Setting screen to pair 0\n"); |
| 109 | bkgd((chtype) (' ' | COLOR_PAIR(0))); |
| 110 | getch(); |
| 111 | |
| 112 | printw("Setting screen to pair 1\n"); |
| 113 | bkgd((chtype) (' ' | COLOR_PAIR(1))); |
| 114 | getch(); |
| 115 | |
| 116 | printw("Setting screen to pair 2\n"); |
| 117 | bkgd((chtype) (' ' | COLOR_PAIR(2))); |
| 118 | getch(); |
| 119 | |
| 120 | printw("Setting screen to pair 3\n"); |
| 121 | bkgd((chtype) (' ' | COLOR_PAIR(3))); |
| 122 | getch(); |
| 123 | |
| 124 | printw("Setting screen to pair 0\n"); |
| 125 | bkgd((chtype) (' ' | COLOR_PAIR(0))); |
| 126 | getch(); |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | usage(void) |
| 131 | { |
| 132 | static const char *msg[] = |
| 133 | { |
| 134 | "Usage: background [options]" |
| 135 | ,"" |
| 136 | ,"Options:" |
| 137 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 138 | ," -a invoke assume_default_colors, repeat to use in init_pair" |
| 139 | #endif |
| 140 | ," -b XXX specify background color" |
| 141 | #if HAVE_USE_DEFAULT_COLORS |
| 142 | ," -d invoke use_default_colors, repeat to use in init_pair" |
| 143 | #endif |
| 144 | ," -f XXX specify foreground color" |
| 145 | }; |
| 146 | size_t n; |
| 147 | |
| 148 | for (n = 0; n < SIZEOF(msg); n++) |
| 149 | fprintf(stderr, "%s\n", msg[n]); |
| 150 | |
| 151 | ExitProgram(EXIT_FAILURE); |
| 152 | } |
| 153 | |
| 154 | int |
| 155 | main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) |
| 156 | { |
| 157 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 158 | int a_option = 0; |
| 159 | #endif |
| 160 | #if HAVE_USE_DEFAULT_COLORS |
| 161 | int d_option = 0; |
| 162 | #endif |
| 163 | int n; |
| 164 | |
| 165 | setlocale(LC_ALL, ""); |
| 166 | |
| 167 | while ((n = getopt(argc, argv, "ab:df:")) != -1) { |
| 168 | switch (n) { |
| 169 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 170 | case 'a': |
| 171 | ++a_option; |
| 172 | break; |
| 173 | #endif |
| 174 | case 'b': |
| 175 | default_bg = color_code(optarg); |
| 176 | break; |
| 177 | #if HAVE_USE_DEFAULT_COLORS |
| 178 | case 'd': |
| 179 | ++d_option; |
| 180 | break; |
| 181 | #endif |
| 182 | case 'f': |
| 183 | default_fg = color_code(optarg); |
| 184 | break; |
| 185 | default: |
| 186 | usage(); |
| 187 | } |
| 188 | } |
| 189 | #if HAVE_USE_DEFAULT_COLORS && HAVE_ASSUME_DEFAULT_COLORS |
| 190 | if (a_option && d_option) { |
| 191 | fprintf(stderr, "Use either -a or -d option, but not both\n"); |
| 192 | ExitProgram(EXIT_FAILURE); |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | initscr(); |
| 197 | cbreak(); |
| 198 | noecho(); |
| 199 | |
| 200 | if (has_colors()) { |
| 201 | start_color(); |
| 202 | |
| 203 | #if HAVE_USE_DEFAULT_COLORS |
| 204 | if (d_option) { |
| 205 | printw("Using default colors...\n"); |
| 206 | use_default_colors(); |
| 207 | if (d_option > 1) { |
| 208 | default_fg = -1; |
| 209 | default_bg = -1; |
| 210 | } |
| 211 | } |
| 212 | #endif |
| 213 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 214 | if (a_option) { |
| 215 | printw("Using assumed colors %s/%s...\n", |
| 216 | color_name(default_fg), |
| 217 | color_name(default_bg)); |
| 218 | assume_default_colors(default_fg, default_bg); |
| 219 | if (a_option > 1) { |
| 220 | default_fg = -1; |
| 221 | default_bg = -1; |
| 222 | } |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | test_background(); |
| 227 | |
| 228 | } else { |
| 229 | printw("This demo requires a color terminal"); |
| 230 | getch(); |
| 231 | } |
| 232 | endwin(); |
| 233 | |
| 234 | ExitProgram(EXIT_SUCCESS); |
| 235 | } |