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