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