xref: /netbsd-src/lib/libcurses/attributes.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: attributes.c,v 1.15 2007/05/28 15:01:54 blymn 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.15 2007/05/28 15:01:54 blymn 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 #ifdef HAVE_WCHAR
152 		*attr &= WA_ATTRIBUTES;
153 #endif
154 	}
155 
156 	if (pair != NULL)
157 		*pair = PAIR_NUMBER(win->wattr);
158 	return OK;
159 }
160 
161 /*
162  * wattr_on --
163  *	Test and set attributes on stdscr
164  *
165  *	Modes are blinking, bold (extra bright), dim (half-bright),
166  *	blanking (invisible), protected and reverse video
167  */
168 /* ARGSUSED */
169 int
170 wattr_on(WINDOW *win, attr_t attr, void *opt)
171 {
172 #ifdef DEBUG
173 	__CTRACE(__CTRACE_ATTR, "wattr_on: win %p, attr %08x\n", win, attr);
174 #endif
175 	/* If can enter modes, set the relevent attribute bits. */
176 	if (__tc_me != NULL) {
177 		if (attr & __BLINK && __tc_mb != NULL)
178 			win->wattr |= __BLINK;
179 		if (attr & __BOLD && __tc_md != NULL)
180 			win->wattr |= __BOLD;
181 		if (attr & __DIM && __tc_mh != NULL)
182 			win->wattr |= __DIM;
183 		if (attr & __BLANK && __tc_mk != NULL)
184 			win->wattr |= __BLANK;
185 		if (attr & __PROTECT && __tc_mp != NULL)
186 			win->wattr |= __PROTECT;
187 		if (attr & __REVERSE && __tc_mr != NULL)
188 			win->wattr |= __REVERSE;
189 #ifdef HAVE_WCHAR
190 		if (attr & WA_LOW && __tc_Xo != NULL)
191 			win->wattr |= WA_LOW;
192 		if (attr & WA_TOP && __tc_Xt != NULL)
193 			win->wattr |= WA_TOP;
194 		if (attr & WA_LEFT && __tc_Xl != NULL)
195 			win->wattr |= WA_LEFT;
196 		if (attr & WA_RIGHT && __tc_Xr != NULL)
197 			win->wattr |= WA_RIGHT;
198 		if (attr & WA_HORIZONTAL && __tc_Xh != NULL)
199 			win->wattr |= WA_HORIZONTAL;
200 		if (attr & WA_VERTICAL && __tc_Xv != NULL)
201 			win->wattr |= WA_VERTICAL;
202 #endif /* HAVE_WCHAR */
203 	}
204 	if (attr & __STANDOUT)
205 		wstandout(win);
206 	if (attr & __UNDERSCORE)
207 		wunderscore(win);
208 	if ((attr_t) attr & __COLOR)
209 		__wcolor_set(win, (attr_t) attr);
210 	return OK;
211 }
212 
213 /*
214  * wattr_off --
215  *	Test and unset attributes on stdscr
216  *
217  *	Note that the 'me' sequence unsets all attributes.  We handle
218  *	which attributes should really be set in refresh.c:makech().
219  */
220 /* ARGSUSED */
221 int
222 wattr_off(WINDOW *win, attr_t attr, void *opt)
223 {
224 #ifdef DEBUG
225 	__CTRACE(__CTRACE_ATTR, "wattr_off: win %p, attr %08x\n", win, attr);
226 #endif
227 	/* If can do exit modes, unset the relevent attribute bits. */
228 	if (__tc_me != NULL) {
229 		if (attr & __BLINK)
230 			win->wattr &= ~__BLINK;
231 		if (attr & __BOLD)
232 			win->wattr &= ~__BOLD;
233 		if (attr & __DIM)
234 			win->wattr &= ~__DIM;
235 		if (attr & __BLANK)
236 			win->wattr &= ~__BLANK;
237 		if (attr & __PROTECT)
238 			win->wattr &= ~__PROTECT;
239 		if (attr & __REVERSE)
240 			win->wattr &= ~__REVERSE;
241 #ifdef HAVE_WCHAR
242 		if (attr & WA_LOW)
243 			win->wattr &= ~WA_LOW;
244 		if (attr & WA_TOP)
245 			win->wattr &= ~WA_TOP;
246 		if (attr & WA_LEFT)
247 			win->wattr &= ~WA_LEFT;
248 		if (attr & WA_RIGHT)
249 			win->wattr &= ~WA_RIGHT;
250 		if (attr & WA_HORIZONTAL)
251 			win->wattr &= ~WA_HORIZONTAL;
252 		if (attr & WA_VERTICAL)
253 			win->wattr &= ~WA_VERTICAL;
254 #endif /* HAVE_WCHAR */
255 	}
256 	if (attr & __STANDOUT)
257 		wstandend(win);
258 	if (attr & __UNDERSCORE)
259 		wunderend(win);
260 	if ((attr_t) attr & __COLOR) {
261 		if (__tc_Co != 0)
262 			win->wattr &= ~__COLOR;
263 	}
264 	return OK;
265 }
266 
267 /*
268  * wattr_set --
269  *	Set attributes and color pair on stdscr
270  */
271 int
272 wattr_set(WINDOW *win, attr_t attr, short pair, void *opt)
273 {
274 #ifdef DEBUG
275 	__CTRACE(__CTRACE_ATTR, "wattr_set: win %p, attr %08x, pair %d\n",
276 	    win, attr, pair);
277 #endif
278  	wattr_off(win, __ATTRIBUTES, opt);
279 	/*
280 	 * This overwrites any colour setting from the attributes
281 	 * and is compatible with ncurses.
282 	 */
283  	attr = (attr & ~__COLOR) | COLOR_PAIR(pair);
284  	wattr_on(win, attr, opt);
285 	return OK;
286 }
287 
288 /*
289  * wattron --
290  *	Test and set attributes.
291  */
292 int
293 wattron(WINDOW *win, int attr)
294 {
295 #ifdef DEBUG
296 	__CTRACE(__CTRACE_ATTR, "wattron: win %p, attr %08x\n", win, attr);
297 #endif
298 	return wattr_on(win, (attr_t) attr, NULL);
299 }
300 
301 /*
302  * wattroff --
303  *	Test and unset attributes.
304  */
305 int
306 wattroff(WINDOW *win, int attr)
307 {
308 #ifdef DEBUG
309 	__CTRACE(__CTRACE_ATTR, "wattroff: win %p, attr %08x\n", win, attr);
310 #endif
311 	return wattr_off(win, (attr_t) attr, NULL);
312 }
313 
314 /*
315  * wattrset --
316  *	Set specific attribute modes.
317  *	Unset others.
318  */
319 int
320 wattrset(WINDOW *win, int attr)
321 {
322 #ifdef DEBUG
323 	__CTRACE(__CTRACE_ATTR, "wattrset: win %p, attr %08x\n", win, attr);
324 #endif
325 	wattr_off(win, __ATTRIBUTES, NULL);
326 	wattr_on(win, (attr_t) attr, NULL);
327 	return OK;
328 }
329 
330 /*
331  * wcolor_set --
332  *	Set color pair on window
333  */
334 /* ARGSUSED */
335 int
336 wcolor_set(WINDOW *win, short pair, void *opt)
337 {
338 #ifdef DEBUG
339 	__CTRACE(__CTRACE_COLOR, "wolor_set: win %p, pair %d\n", win, pair);
340 #endif
341 	__wcolor_set(win, (attr_t) COLOR_PAIR(pair));
342 	return OK;
343 }
344 
345 /*
346  * getattrs --
347  * Get window attributes.
348  */
349 chtype
350 getattrs(WINDOW *win)
351 {
352 #ifdef DEBUG
353 	__CTRACE(__CTRACE_ATTR, "getattrs: win %p\n", win);
354 #endif
355 	return((chtype) win->wattr);
356 }
357 
358 /*
359  * __wcolor_set --
360  * Set color attribute on window
361  */
362 void
363 __wcolor_set(WINDOW *win, attr_t attr)
364 {
365 	/* If another color pair is set, turn that off first. */
366 	win->wattr &= ~__COLOR;
367 	/* If can do color video, set the color pair bits. */
368 	if (__tc_Co != 0 && attr & __COLOR)
369 		win->wattr |= attr & __COLOR;
370 }
371