]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/background.c
ncurses 5.9 - patch 20110807
[ncurses.git] / test / background.c
1 /****************************************************************************
2  * Copyright (c) 2003-2006,2011 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.9 2011/04/23 21:16:38 tom Exp $
30  */
31
32 #define NEED_COLOR_CODE 1
33 #define NEED_COLOR_NAME 1
34 #include <color_name.h>
35
36 static int default_bg = COLOR_BLACK;
37 static int default_fg = COLOR_WHITE;
38
39 static void
40 test_background(void)
41 {
42     short f, b;
43
44     pair_content(0, &f, &b);
45     printw("pair 0 contains (%d,%d)\n", f, b);
46     getch();
47
48     printw("Initializing pair 1 to red/%s\n", color_name(default_bg));
49     init_pair(1, COLOR_RED, (short) default_bg);
50     bkgdset(' ' | COLOR_PAIR(1));
51     printw("RED/BLACK\n");
52     getch();
53
54     printw("Initializing pair 2 to %s/blue\n", color_name(default_fg));
55     init_pair(2, (short) default_fg, COLOR_BLUE);
56     bkgdset(' ' | COLOR_PAIR(2));
57     printw("This line should be %s/blue\n", color_name(default_fg));
58     getch();
59
60     printw("Resetting colors to pair 0\n");
61     bkgdset(' ' | COLOR_PAIR(0));
62     printw("Default Colors\n");
63     getch();
64
65     printw("Resetting colors to pair 1\n");
66     bkgdset(' ' | COLOR_PAIR(1));
67     printw("This line should be red/%s\n", color_name(default_bg));
68     getch();
69
70     printw("Setting screen to pair 0\n");
71     bkgd(' ' | COLOR_PAIR(0));
72     getch();
73
74     printw("Setting screen to pair 1\n");
75     bkgd(' ' | COLOR_PAIR(1));
76     getch();
77
78     printw("Setting screen to pair 2\n");
79     bkgd(' ' | COLOR_PAIR(2));
80     getch();
81
82     printw("Setting screen to pair 0\n");
83     bkgd(' ' | COLOR_PAIR(0));
84     getch();
85 }
86
87 static void
88 usage(void)
89 {
90     static const char *msg[] =
91     {
92         "Usage: background [options]"
93         ,""
94         ,"Options:"
95 #if HAVE_ASSUME_DEFAULT_COLORS
96         ," -a       invoke assume_default_colors, repeat to use in init_pair"
97 #endif
98         ," -b XXX   specify background color"
99 #if HAVE_USE_DEFAULT_COLORS
100         ," -d       invoke use_default_colors, repeat to use in init_pair"
101 #endif
102         ," -f XXX   specify foreground color"
103     };
104     size_t n;
105
106     for (n = 0; n < SIZEOF(msg); n++)
107         fprintf(stderr, "%s\n", msg[n]);
108
109     ExitProgram(EXIT_FAILURE);
110 }
111
112 int
113 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
114 {
115 #if HAVE_ASSUME_DEFAULT_COLORS
116     int a_option = 0;
117 #endif
118 #if HAVE_USE_DEFAULT_COLORS
119     int d_option = 0;
120 #endif
121     int n;
122
123     while ((n = getopt(argc, argv, "ab:df:")) != -1) {
124         switch (n) {
125 #if HAVE_ASSUME_DEFAULT_COLORS
126         case 'a':
127             ++a_option;
128             break;
129 #endif
130         case 'b':
131             default_bg = color_code(optarg);
132             break;
133 #if HAVE_USE_DEFAULT_COLORS
134         case 'd':
135             ++d_option;
136             break;
137 #endif
138         case 'f':
139             default_fg = color_code(optarg);
140             break;
141         default:
142             usage();
143         }
144     }
145 #if HAVE_USE_DEFAULT_COLORS && HAVE_ASSUME_DEFAULT_COLORS
146     if (a_option && d_option) {
147         fprintf(stderr, "Use either -a or -d option, but not both\n");
148         ExitProgram(EXIT_FAILURE);
149     }
150 #endif
151
152     initscr();
153     cbreak();
154     noecho();
155
156     if (has_colors()) {
157         start_color();
158
159 #if HAVE_USE_DEFAULT_COLORS
160         if (d_option) {
161             printw("Using default colors...\n");
162             use_default_colors();
163             if (d_option > 1) {
164                 default_fg = -1;
165                 default_bg = -1;
166             }
167         }
168 #endif
169 #if HAVE_ASSUME_DEFAULT_COLORS
170         if (a_option) {
171             printw("Using assumed colors %s/%s...\n",
172                    color_name(default_fg),
173                    color_name(default_bg));
174             assume_default_colors(default_fg, default_bg);
175             if (a_option > 1) {
176                 default_fg = -1;
177                 default_bg = -1;
178             }
179         }
180 #endif
181
182         test_background();
183
184     } else {
185         printw("This demo requires a color terminal");
186         getch();
187     }
188     endwin();
189
190     ExitProgram(EXIT_SUCCESS);
191 }