attributes.c revision 1.14 1 /* $NetBSD: attributes.c,v 1.14 2007/01/21 13:25:36 jdc Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julian Coleman.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: attributes.c,v 1.14 2007/01/21 13:25:36 jdc Exp $");
42 #endif /* not lint */
43
44 #include "curses.h"
45 #include "curses_private.h"
46
47 void __wcolor_set(WINDOW *, attr_t);
48
49 #ifndef _CURSES_USE_MACROS
50 /*
51 * attr_get --
52 * Get attributes and color pair from stdscr
53 */
54 /* ARGSUSED */
55 int
56 attr_get(attr_t *attr, short *pair, void *opt)
57 {
58 return wattr_get(stdscr, attr, pair, opt);
59 }
60
61 /*
62 * attr_on --
63 * Test and set attributes on stdscr
64 */
65 /* ARGSUSED */
66 int
67 attr_on(attr_t attr, void *opt)
68 {
69 return wattr_on(stdscr, attr, opt);
70 }
71
72 /*
73 * attr_off --
74 * Test and unset attributes on stdscr
75 */
76 /* ARGSUSED */
77 int
78 attr_off(attr_t attr, void *opt)
79 {
80 return wattr_off(stdscr, attr, opt);
81 }
82
83 /*
84 * attr_set --
85 * Set attributes and color pair on stdscr
86 */
87 /* ARGSUSED */
88 int
89 attr_set(attr_t attr, short pair, void *opt)
90 {
91 return wattr_set(stdscr, attr, pair, opt);
92 }
93
94 /*
95 * color_set --
96 * Set color pair on stdscr
97 */
98 /* ARGSUSED */
99 int
100 color_set(short pair, void *opt)
101 {
102 return wcolor_set(stdscr, pair, opt);
103 }
104
105 /*
106 * attron --
107 * Test and set attributes on stdscr
108 */
109 int
110 attron(int attr)
111 {
112 return wattr_on(stdscr, (attr_t) attr, NULL);
113 }
114
115 /*
116 * attroff --
117 * Test and unset attributes on stdscr.
118 */
119 int
120 attroff(int attr)
121 {
122 return wattr_off(stdscr, (attr_t) attr, NULL);
123 }
124
125 /*
126 * attrset --
127 * Set specific attribute modes.
128 * Unset others. On stdscr.
129 */
130 int
131 attrset(int attr)
132 {
133 return wattrset(stdscr, attr);
134 }
135 #endif /* _CURSES_USE_MACROS */
136
137 /*
138 * wattr_get --
139 * Get attributes and colour pair from window
140 * Note that attributes also includes colour.
141 */
142 /* ARGSUSED */
143 int
144 wattr_get(WINDOW *win, attr_t *attr, short *pair, void *opt)
145 {
146 #ifdef DEBUG
147 __CTRACE(__CTRACE_ATTR, "wattr_get: win %p\n", win);
148 #endif
149 if (attr != NULL)
150 *attr = win->wattr;
151 if (pair != NULL)
152 *pair = PAIR_NUMBER(win->wattr);
153 return OK;
154 }
155
156 /*
157 * wattr_on --
158 * Test and set attributes on stdscr
159 *
160 * Modes are blinking, bold (extra bright), dim (half-bright),
161 * blanking (invisible), protected and reverse video
162 */
163 /* ARGSUSED */
164 int
165 wattr_on(WINDOW *win, attr_t attr, void *opt)
166 {
167 #ifdef DEBUG
168 __CTRACE(__CTRACE_ATTR, "wattr_on: win %p, attr %08x\n", win, attr);
169 #endif
170 /* If can enter modes, set the relevent attribute bits. */
171 if (__tc_me != NULL) {
172 if (attr & __BLINK && __tc_mb != NULL)
173 win->wattr |= __BLINK;
174 if (attr & __BOLD && __tc_md != NULL)
175 win->wattr |= __BOLD;
176 if (attr & __DIM && __tc_mh != NULL)
177 win->wattr |= __DIM;
178 if (attr & __BLANK && __tc_mk != NULL)
179 win->wattr |= __BLANK;
180 if (attr & __PROTECT && __tc_mp != NULL)
181 win->wattr |= __PROTECT;
182 if (attr & __REVERSE && __tc_mr != NULL)
183 win->wattr |= __REVERSE;
184 }
185 if (attr & __STANDOUT)
186 wstandout(win);
187 if (attr & __UNDERSCORE)
188 wunderscore(win);
189 if ((attr_t) attr & __COLOR)
190 __wcolor_set(win, (attr_t) attr);
191 return OK;
192 }
193
194 /*
195 * wattr_off --
196 * Test and unset attributes on stdscr
197 *
198 * Note that the 'me' sequence unsets all attributes. We handle
199 * which attributes should really be set in refresh.c:makech().
200 */
201 /* ARGSUSED */
202 int
203 wattr_off(WINDOW *win, attr_t attr, void *opt)
204 {
205 #ifdef DEBUG
206 __CTRACE(__CTRACE_ATTR, "wattr_off: win %p, attr %08x\n", win, attr);
207 #endif
208 /* If can do exit modes, unset the relevent attribute bits. */
209 if (__tc_me != NULL) {
210 if (attr & __BLINK)
211 win->wattr &= ~__BLINK;
212 if (attr & __BOLD)
213 win->wattr &= ~__BOLD;
214 if (attr & __DIM)
215 win->wattr &= ~__DIM;
216 if (attr & __BLANK)
217 win->wattr &= ~__BLANK;
218 if (attr & __PROTECT)
219 win->wattr &= ~__PROTECT;
220 if (attr & __REVERSE)
221 win->wattr &= ~__REVERSE;
222 }
223 if (attr & __STANDOUT)
224 wstandend(win);
225 if (attr & __UNDERSCORE)
226 wunderend(win);
227 if ((attr_t) attr & __COLOR) {
228 if (__tc_Co != 0)
229 win->wattr &= ~__COLOR;
230 }
231 return OK;
232 }
233
234 /*
235 * wattr_set --
236 * Set attributes and color pair on stdscr
237 */
238 /* ARGSUSED */
239 int
240 wattr_set(WINDOW *win, attr_t attr, short pair, void *opt)
241 {
242 #ifdef DEBUG
243 __CTRACE(__CTRACE_ATTR, "wattr_set: win %p, attr %08x, pair %d\n",
244 win, attr, pair);
245 #endif
246 wattr_on(win, attr, NULL);
247 wattr_off(win, (~attr & ~__COLOR) | ((attr & __COLOR) ? 0 : __COLOR),
248 NULL);
249 /*
250 * This overwrites any colour setting from the attributes
251 * and is compatible with ncurses.
252 */
253 __wcolor_set(win, (attr_t) COLOR_PAIR(pair));
254 return OK;
255 }
256
257 /*
258 * wattron --
259 * Test and set attributes.
260 */
261
262 int
263 wattron(WINDOW *win, int attr)
264 {
265 #ifdef DEBUG
266 __CTRACE(__CTRACE_ATTR, "wattron: win %p, attr %08x\n", win, attr);
267 #endif
268 return wattr_on(win, (attr_t) attr, NULL);
269 }
270
271 /*
272 * wattroff --
273 * Test and unset attributes.
274 */
275 int
276 wattroff(WINDOW *win, int attr)
277 {
278 #ifdef DEBUG
279 __CTRACE(__CTRACE_ATTR, "wattroff: win %p, attr %08x\n", win, attr);
280 #endif
281 return wattr_off(win, (attr_t) attr, NULL);
282 }
283
284 /*
285 * wattrset --
286 * Set specific attribute modes.
287 * Unset others.
288 */
289 int
290 wattrset(WINDOW *win, int attr)
291 {
292 #ifdef DEBUG
293 __CTRACE(__CTRACE_ATTR, "wattrset: win %p, attr %08x\n", win, attr);
294 #endif
295 wattr_on(win, (attr_t) attr, NULL);
296 wattr_off(win,
297 (attr_t) (~attr & ~__COLOR) | ((attr & __COLOR) ? 0 : __COLOR),
298 NULL);
299 return OK;
300 }
301
302 /*
303 * wcolor_set --
304 * Set color pair on window
305 */
306 /* ARGSUSED */
307 int
308 wcolor_set(WINDOW *win, short pair, void *opt)
309 {
310 #ifdef DEBUG
311 __CTRACE(__CTRACE_COLOR, "wolor_set: win %p, pair %d\n", win, pair);
312 #endif
313 __wcolor_set(win, (attr_t) COLOR_PAIR(pair));
314 return OK;
315 }
316
317 /*
318 * getattrs --
319 * Get window attributes.
320 */
321 chtype
322 getattrs(WINDOW *win)
323 {
324 #ifdef DEBUG
325 __CTRACE(__CTRACE_ATTR, "getattrs: win %p\n", win);
326 #endif
327 return((chtype) win->wattr);
328 }
329
330 /*
331 * __wcolor_set --
332 * Set color attribute on window
333 */
334 void
335 __wcolor_set(WINDOW *win, attr_t attr)
336 {
337 /* If another color pair is set, turn that off first. */
338 win->wattr &= ~__COLOR;
339 /* If can do color video, set the color pair bits. */
340 if (__tc_Co != 0 && attr & __COLOR)
341 win->wattr |= attr & __COLOR;
342 }
343