]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/background.c
ncurses 6.2 - patch 20210213
[ncurses.git] / test / background.c
1 /****************************************************************************
2  * Copyright 2018-2020,2021 Thomas E. Dickey                                *
3  * Copyright 2003-2014,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * $Id: background.c,v 1.20 2021/02/13 20:54:08 tom Exp $
31  */
32
33 #define NEED_COLOR_CODE 1
34 #define NEED_COLOR_NAME 1
35 #include <color_name.h>
36 #include <dump_window.h>
37
38 static int default_bg = COLOR_BLACK;
39 static int default_fg = COLOR_WHITE;
40
41 static void
42 test_background(void)
43 {
44     NCURSES_COLOR_T f, b;
45     int row;
46     int chr;
47
48     if (pair_content(0, &f, &b) == ERR) {
49         printw("pair 0 contains no data\n");
50     } else {
51         printw("pair 0 contains (%d,%d)\n", (int) f, (int) b);
52     }
53     dump_window(stdscr);
54
55     printw("Initializing pair 1 to red/%s\n", color_name(default_bg));
56     init_pair(1, COLOR_RED, (NCURSES_COLOR_T) default_bg);
57     bkgdset((chtype) (' ' | COLOR_PAIR(1)));
58     printw("RED/BLACK\n");
59     dump_window(stdscr);
60
61     printw("Initializing pair 2 to %s/blue\n", color_name(default_fg));
62     init_pair(2, (NCURSES_COLOR_T) default_fg, COLOR_BLUE);
63     bkgdset((chtype) (' ' | COLOR_PAIR(2)));
64     printw("This line should be %s/blue\n", color_name(default_fg));
65     dump_window(stdscr);
66
67     printw("Initializing pair 3 to %s/cyan (ACS_HLINE)\n", color_name(default_fg));
68     init_pair(3, (NCURSES_COLOR_T) default_fg, COLOR_CYAN);
69     printw("...and drawing a box which should be followed by lines\n");
70     bkgdset(ACS_HLINE | (chtype) COLOR_PAIR(3));
71     /*
72      * Characters from vt100 line-drawing should be mapped to line-drawing,
73      * since A_ALTCHARSET is set in the background, and the character part
74      * of the background is replaced by the nonblank characters written.
75      *
76      * Characters not in the line-drawing range are usually sent as-is.
77      *
78      * With SVr4 curses it is possible to rely on this to mix uppercase text
79      * with the (lowercase) line-drawing characters.  ncurses uses some of
80      * the uppercase characters for encoding thick- and double-lines.
81      */
82     row = 7;
83     mvprintw(row++, 10, "l");
84     for (chr = 0; chr < 32; ++chr)
85         AddCh(' ');
86     printw("x\n");
87     chr = 32;
88     while (chr < 128) {
89         if ((chr % 32) == 0)
90             mvprintw(row++, 10, "x");
91         AddCh((chr == 127) ? ' ' : chr);
92         if ((++chr % 32) == 0)
93             printw("x\n");
94     }
95     mvprintw(row++, 10, "m");
96     for (chr = 0; chr < 32; ++chr)
97         AddCh(' ');
98     printw("j\n");
99     dump_window(stdscr);
100
101     bkgdset((chtype) (' ' | COLOR_PAIR(0)));
102     printw("Default Colors\n");
103     dump_window(stdscr);
104
105     printw("Resetting colors to pair 1\n");
106     bkgdset((chtype) (' ' | COLOR_PAIR(1)));
107     printw("This line should be red/%s\n", color_name(default_bg));
108     dump_window(stdscr);
109
110     printw("Setting screen to pair 0\n");
111     bkgd((chtype) (' ' | COLOR_PAIR(0)));
112     dump_window(stdscr);
113
114     printw("Setting screen to pair 1\n");
115     bkgd((chtype) (' ' | COLOR_PAIR(1)));
116     dump_window(stdscr);
117
118     printw("Setting screen to pair 2\n");
119     bkgd((chtype) (' ' | COLOR_PAIR(2)));
120     dump_window(stdscr);
121
122     printw("Setting screen to pair 3\n");
123     bkgd((chtype) (' ' | COLOR_PAIR(3)));
124     dump_window(stdscr);
125
126     printw("Setting screen to pair 0\n");
127     bkgd((chtype) (' ' | COLOR_PAIR(0)));
128     dump_window(stdscr);
129 }
130
131 static void
132 usage(void)
133 {
134     static const char *msg[] =
135     {
136         "Usage: background [options]"
137         ,""
138         ,"Options:"
139 #if HAVE_ASSUME_DEFAULT_COLORS
140         ," -a       invoke assume_default_colors, repeat to use in init_pair"
141 #endif
142         ," -b XXX   specify background color"
143 #if HAVE_USE_DEFAULT_COLORS
144         ," -d       invoke use_default_colors, repeat to use in init_pair"
145 #endif
146         ," -f XXX   specify foreground color"
147         ," -l FILE  log window-dumps to this file"
148     };
149     size_t n;
150
151     for (n = 0; n < SIZEOF(msg); n++)
152         fprintf(stderr, "%s\n", msg[n]);
153
154     ExitProgram(EXIT_FAILURE);
155 }
156
157 int
158 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
159 {
160 #if HAVE_ASSUME_DEFAULT_COLORS
161     int a_option = 0;
162 #endif
163 #if HAVE_USE_DEFAULT_COLORS
164     int d_option = 0;
165 #endif
166     int n;
167
168     setlocale(LC_ALL, "");
169
170     while ((n = getopt(argc, argv, "ab:df:l:")) != -1) {
171         switch (n) {
172 #if HAVE_ASSUME_DEFAULT_COLORS
173         case 'a':
174             ++a_option;
175             break;
176 #endif
177         case 'b':
178             default_bg = color_code(optarg);
179             break;
180 #if HAVE_USE_DEFAULT_COLORS
181         case 'd':
182             ++d_option;
183             break;
184 #endif
185         case 'f':
186             default_fg = color_code(optarg);
187             break;
188         case 'l':
189             if (!open_dump(optarg))
190                 usage();
191             break;
192         default:
193             usage();
194         }
195     }
196 #if HAVE_USE_DEFAULT_COLORS && HAVE_ASSUME_DEFAULT_COLORS
197     if (a_option && d_option) {
198         fprintf(stderr, "Use either -a or -d option, but not both\n");
199         ExitProgram(EXIT_FAILURE);
200     }
201 #endif
202
203     initscr();
204     cbreak();
205     noecho();
206
207     if (has_colors()) {
208         start_color();
209
210 #if HAVE_USE_DEFAULT_COLORS
211         if (d_option) {
212             printw("Using default colors...\n");
213             use_default_colors();
214             if (d_option > 1) {
215                 default_fg = -1;
216                 default_bg = -1;
217             }
218         }
219 #endif
220 #if HAVE_ASSUME_DEFAULT_COLORS
221         if (a_option) {
222             printw("Using assumed colors %s/%s...\n",
223                    color_name(default_fg),
224                    color_name(default_bg));
225             assume_default_colors(default_fg, default_bg);
226             if (a_option > 1) {
227                 default_fg = -1;
228                 default_bg = -1;
229             }
230         }
231 #endif
232
233         test_background();
234
235     } else {
236         printw("This demo requires a color terminal");
237         getch();
238     }
239     endwin();
240     close_dump();
241     ExitProgram(EXIT_SUCCESS);
242 }