]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/back_ground.c
ncurses 6.2 - patch 20210418
[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.5 2021/02/20 12:23:21 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(void)
174 {
175     static const char *msg[] =
176     {
177         "Usage: background [options]"
178         ,""
179         ,"Options:"
180 #if HAVE_ASSUME_DEFAULT_COLORS
181         ," -a       invoke assume_default_colors, repeat to use in init_pair"
182 #endif
183         ," -b XXX   specify background color"
184 #if HAVE_USE_DEFAULT_COLORS
185         ," -d       invoke use_default_colors, repeat to use in init_pair"
186 #endif
187         ," -f XXX   specify foreground color"
188         ," -l FILE  log window-dumps to this file"
189         ," -w       fill background with stipple pattern"
190         ," -W CODE  fill background with this Unicode value"
191     };
192     size_t n;
193
194     for (n = 0; n < SIZEOF(msg); n++)
195         fprintf(stderr, "%s\n", msg[n]);
196
197     ExitProgram(EXIT_FAILURE);
198 }
199
200 int
201 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
202 {
203 #if HAVE_ASSUME_DEFAULT_COLORS
204     int a_option = 0;
205 #endif
206 #if HAVE_USE_DEFAULT_COLORS
207     int d_option = 0;
208 #endif
209     int n;
210
211     setlocale(LC_ALL, "");
212
213     while ((n = getopt(argc, argv, "ab:df:l:wW:")) != -1) {
214         switch (n) {
215 #if HAVE_ASSUME_DEFAULT_COLORS
216         case 'a':
217             ++a_option;
218             break;
219 #endif
220         case 'b':
221             default_bg = color_code(optarg);
222             break;
223 #if HAVE_USE_DEFAULT_COLORS
224         case 'd':
225             ++d_option;
226             break;
227 #endif
228         case 'f':
229             default_fg = color_code(optarg);
230             break;
231         case 'l':
232             if (!open_dump(optarg))
233                 usage();
234             break;
235         case 'w':
236             wide_fill = L'\u2591';
237             break;
238         case 'W':
239             wide_fill = decode_wchar(optarg);
240             break;
241         default:
242             usage();
243         }
244     }
245 #if HAVE_USE_DEFAULT_COLORS && HAVE_ASSUME_DEFAULT_COLORS
246     if (a_option && d_option) {
247         fprintf(stderr, "Use either -a or -d option, but not both\n");
248         ExitProgram(EXIT_FAILURE);
249     }
250 #endif
251
252     initscr();
253     cbreak();
254     noecho();
255
256     if (has_colors()) {
257         start_color();
258
259 #if HAVE_USE_DEFAULT_COLORS
260         if (d_option) {
261             printw("Using default colors...\n");
262             use_default_colors();
263             if (d_option > 1) {
264                 default_fg = -1;
265                 default_bg = -1;
266             }
267         }
268 #endif
269 #if HAVE_ASSUME_DEFAULT_COLORS
270         if (a_option) {
271             printw("Using assumed colors %s/%s...\n",
272                    color_name(default_fg),
273                    color_name(default_bg));
274             assume_default_colors(default_fg, default_bg);
275             if (a_option > 1) {
276                 default_fg = -1;
277                 default_bg = -1;
278             }
279         }
280 #endif
281
282         test_background();
283
284     } else {
285         printw("This demo requires a color terminal");
286         getch();
287     }
288     endwin();
289     close_dump();
290     ExitProgram(EXIT_SUCCESS);
291 }
292
293 #else
294 int
295 main(void)
296 {
297     printf("This program requires the wide-curses library\n");
298     ExitProgram(EXIT_FAILURE);
299 }
300 #endif /* USE_WIDEC_SUPPORT */