1 /****************************************************************************
2 * Copyright (c) 2003-2012,2014 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: background.c,v 1.15 2014/08/09 22:31:23 tom Exp $
32 #define NEED_COLOR_CODE 1
33 #define NEED_COLOR_NAME 1
34 #include <color_name.h>
36 static int default_bg = COLOR_BLACK;
37 static int default_fg = COLOR_WHITE;
46 if (pair_content(0, &f, &b) == ERR) {
47 printw("pair 0 contains no data\n");
49 printw("pair 0 contains (%d,%d)\n", (int) f, (int) b);
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");
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));
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));
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.
74 * Characters not in the line-drawing range are usually sent as-is.
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.
81 mvprintw(row++, 10, "l");
82 for (chr = 0; chr < 32; ++chr)
88 mvprintw(row++, 10, "x");
89 addch((chtype) ((chr == 127) ? ' ' : chr));
90 if ((++chr % 32) == 0)
93 mvprintw(row++, 10, "m");
94 for (chr = 0; chr < 32; ++chr)
99 bkgdset((chtype) (' ' | COLOR_PAIR(0)));
100 printw("Default Colors\n");
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));
108 printw("Setting screen to pair 0\n");
109 bkgd((chtype) (' ' | COLOR_PAIR(0)));
112 printw("Setting screen to pair 1\n");
113 bkgd((chtype) (' ' | COLOR_PAIR(1)));
116 printw("Setting screen to pair 2\n");
117 bkgd((chtype) (' ' | COLOR_PAIR(2)));
120 printw("Setting screen to pair 3\n");
121 bkgd((chtype) (' ' | COLOR_PAIR(3)));
124 printw("Setting screen to pair 0\n");
125 bkgd((chtype) (' ' | COLOR_PAIR(0)));
132 static const char *msg[] =
134 "Usage: background [options]"
137 #if HAVE_ASSUME_DEFAULT_COLORS
138 ," -a invoke assume_default_colors, repeat to use in init_pair"
140 ," -b XXX specify background color"
141 #if HAVE_USE_DEFAULT_COLORS
142 ," -d invoke use_default_colors, repeat to use in init_pair"
144 ," -f XXX specify foreground color"
148 for (n = 0; n < SIZEOF(msg); n++)
149 fprintf(stderr, "%s\n", msg[n]);
151 ExitProgram(EXIT_FAILURE);
155 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
157 #if HAVE_ASSUME_DEFAULT_COLORS
160 #if HAVE_USE_DEFAULT_COLORS
165 setlocale(LC_ALL, "");
167 while ((n = getopt(argc, argv, "ab:df:")) != -1) {
169 #if HAVE_ASSUME_DEFAULT_COLORS
175 default_bg = color_code(optarg);
177 #if HAVE_USE_DEFAULT_COLORS
183 default_fg = color_code(optarg);
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);
203 #if HAVE_USE_DEFAULT_COLORS
205 printw("Using default colors...\n");
206 use_default_colors();
213 #if HAVE_ASSUME_DEFAULT_COLORS
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);
229 printw("This demo requires a color terminal");
234 ExitProgram(EXIT_SUCCESS);