attributes.c revision 1.15.4.1 1 1.15.4.1 matt /* attributes.c,v 1.15 2007/05/28 15:01:54 blymn Exp */
2 1.1 mrg
3 1.3 jdc /*-
4 1.3 jdc * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.3 jdc * This code is derived from software contributed to The NetBSD Foundation
8 1.3 jdc * by Julian Coleman.
9 1.3 jdc *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.3 jdc * 2. Redistributions in binary form must reproduce the above copyright
16 1.3 jdc * notice, this list of conditions and the following disclaimer in the
17 1.3 jdc * documentation and/or other materials provided with the distribution.
18 1.3 jdc * 3. All advertising materials mentioning features or use of this software
19 1.3 jdc * must display the following acknowledgement:
20 1.3 jdc * This product includes software developed by the NetBSD
21 1.3 jdc * Foundation, Inc. and its contributors.
22 1.3 jdc * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3 jdc * contributors may be used to endorse or promote products derived
24 1.3 jdc * from this software without specific prior written permission.
25 1.1 mrg *
26 1.3 jdc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3 jdc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3 jdc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3 jdc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3 jdc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3 jdc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3 jdc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3 jdc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3 jdc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3 jdc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3 jdc * POSSIBILITY OF SUCH DAMAGE.
37 1.1 mrg */
38 1.6 blymn
39 1.6 blymn #include <sys/cdefs.h>
40 1.6 blymn #ifndef lint
41 1.15.4.1 matt __RCSID("attributes.c,v 1.15 2007/05/28 15:01:54 blymn Exp");
42 1.6 blymn #endif /* not lint */
43 1.1 mrg
44 1.1 mrg #include "curses.h"
45 1.2 blymn #include "curses_private.h"
46 1.1 mrg
47 1.12 jdc void __wcolor_set(WINDOW *, attr_t);
48 1.12 jdc
49 1.4 blymn #ifndef _CURSES_USE_MACROS
50 1.4 blymn /*
51 1.12 jdc * attr_get --
52 1.15.4.1 matt * Get wide attributes and color pair from stdscr
53 1.12 jdc */
54 1.12 jdc /* ARGSUSED */
55 1.12 jdc int
56 1.12 jdc attr_get(attr_t *attr, short *pair, void *opt)
57 1.12 jdc {
58 1.12 jdc return wattr_get(stdscr, attr, pair, opt);
59 1.12 jdc }
60 1.12 jdc
61 1.12 jdc /*
62 1.12 jdc * attr_on --
63 1.15.4.1 matt * Test and set wide attributes on stdscr
64 1.12 jdc */
65 1.12 jdc /* ARGSUSED */
66 1.12 jdc int
67 1.12 jdc attr_on(attr_t attr, void *opt)
68 1.12 jdc {
69 1.12 jdc return wattr_on(stdscr, attr, opt);
70 1.12 jdc }
71 1.12 jdc
72 1.12 jdc /*
73 1.12 jdc * attr_off --
74 1.15.4.1 matt * Test and unset wide attributes on stdscr
75 1.12 jdc */
76 1.12 jdc /* ARGSUSED */
77 1.12 jdc int
78 1.12 jdc attr_off(attr_t attr, void *opt)
79 1.12 jdc {
80 1.12 jdc return wattr_off(stdscr, attr, opt);
81 1.12 jdc }
82 1.12 jdc
83 1.12 jdc /*
84 1.12 jdc * attr_set --
85 1.15.4.1 matt * Set wide attributes and color pair on stdscr
86 1.12 jdc */
87 1.12 jdc /* ARGSUSED */
88 1.12 jdc int
89 1.12 jdc attr_set(attr_t attr, short pair, void *opt)
90 1.12 jdc {
91 1.12 jdc return wattr_set(stdscr, attr, pair, opt);
92 1.12 jdc }
93 1.12 jdc
94 1.12 jdc /*
95 1.12 jdc * color_set --
96 1.12 jdc * Set color pair on stdscr
97 1.12 jdc */
98 1.12 jdc /* ARGSUSED */
99 1.12 jdc int
100 1.12 jdc color_set(short pair, void *opt)
101 1.12 jdc {
102 1.12 jdc return wcolor_set(stdscr, pair, opt);
103 1.12 jdc }
104 1.12 jdc
105 1.12 jdc /*
106 1.8 jdc * attron --
107 1.4 blymn * Test and set attributes on stdscr
108 1.4 blymn */
109 1.4 blymn int
110 1.4 blymn attron(int attr)
111 1.4 blymn {
112 1.12 jdc return wattr_on(stdscr, (attr_t) attr, NULL);
113 1.4 blymn }
114 1.4 blymn
115 1.4 blymn /*
116 1.8 jdc * attroff --
117 1.4 blymn * Test and unset attributes on stdscr.
118 1.4 blymn */
119 1.4 blymn int
120 1.4 blymn attroff(int attr)
121 1.4 blymn {
122 1.12 jdc return wattr_off(stdscr, (attr_t) attr, NULL);
123 1.4 blymn }
124 1.4 blymn
125 1.4 blymn /*
126 1.8 jdc * attrset --
127 1.4 blymn * Set specific attribute modes.
128 1.4 blymn * Unset others. On stdscr.
129 1.4 blymn */
130 1.4 blymn int
131 1.4 blymn attrset(int attr)
132 1.4 blymn {
133 1.4 blymn return wattrset(stdscr, attr);
134 1.4 blymn }
135 1.12 jdc #endif /* _CURSES_USE_MACROS */
136 1.4 blymn
137 1.12 jdc /*
138 1.12 jdc * wattr_get --
139 1.15.4.1 matt * Get wide attributes and colour pair from window
140 1.12 jdc * Note that attributes also includes colour.
141 1.12 jdc */
142 1.12 jdc /* ARGSUSED */
143 1.12 jdc int
144 1.12 jdc wattr_get(WINDOW *win, attr_t *attr, short *pair, void *opt)
145 1.12 jdc {
146 1.12 jdc #ifdef DEBUG
147 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattr_get: win %p\n", win);
148 1.4 blymn #endif
149 1.15 blymn if (attr != NULL) {
150 1.12 jdc *attr = win->wattr;
151 1.15 blymn #ifdef HAVE_WCHAR
152 1.15 blymn *attr &= WA_ATTRIBUTES;
153 1.15 blymn #endif
154 1.15 blymn }
155 1.15 blymn
156 1.12 jdc if (pair != NULL)
157 1.12 jdc *pair = PAIR_NUMBER(win->wattr);
158 1.12 jdc return OK;
159 1.12 jdc }
160 1.4 blymn
161 1.1 mrg /*
162 1.12 jdc * wattr_on --
163 1.15.4.1 matt * Test and set wide attributes on window
164 1.1 mrg */
165 1.12 jdc /* ARGSUSED */
166 1.1 mrg int
167 1.12 jdc wattr_on(WINDOW *win, attr_t attr, void *opt)
168 1.1 mrg {
169 1.3 jdc #ifdef DEBUG
170 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattr_on: win %p, attr %08x\n", win, attr);
171 1.3 jdc #endif
172 1.7 mycroft /* If can enter modes, set the relevent attribute bits. */
173 1.9 jdc if (__tc_me != NULL) {
174 1.12 jdc if (attr & __BLINK && __tc_mb != NULL)
175 1.2 blymn win->wattr |= __BLINK;
176 1.12 jdc if (attr & __BOLD && __tc_md != NULL)
177 1.2 blymn win->wattr |= __BOLD;
178 1.12 jdc if (attr & __DIM && __tc_mh != NULL)
179 1.2 blymn win->wattr |= __DIM;
180 1.12 jdc if (attr & __BLANK && __tc_mk != NULL)
181 1.2 blymn win->wattr |= __BLANK;
182 1.12 jdc if (attr & __PROTECT && __tc_mp != NULL)
183 1.2 blymn win->wattr |= __PROTECT;
184 1.12 jdc if (attr & __REVERSE && __tc_mr != NULL)
185 1.2 blymn win->wattr |= __REVERSE;
186 1.15 blymn #ifdef HAVE_WCHAR
187 1.15 blymn if (attr & WA_LOW && __tc_Xo != NULL)
188 1.15 blymn win->wattr |= WA_LOW;
189 1.15 blymn if (attr & WA_TOP && __tc_Xt != NULL)
190 1.15 blymn win->wattr |= WA_TOP;
191 1.15 blymn if (attr & WA_LEFT && __tc_Xl != NULL)
192 1.15 blymn win->wattr |= WA_LEFT;
193 1.15 blymn if (attr & WA_RIGHT && __tc_Xr != NULL)
194 1.15 blymn win->wattr |= WA_RIGHT;
195 1.15 blymn if (attr & WA_HORIZONTAL && __tc_Xh != NULL)
196 1.15 blymn win->wattr |= WA_HORIZONTAL;
197 1.15 blymn if (attr & WA_VERTICAL && __tc_Xv != NULL)
198 1.15 blymn win->wattr |= WA_VERTICAL;
199 1.15 blymn #endif /* HAVE_WCHAR */
200 1.1 mrg }
201 1.15.4.1 matt if (attr & __STANDOUT && __tc_so != NULL && __tc_se != NULL)
202 1.2 blymn wstandout(win);
203 1.15.4.1 matt if (attr & __UNDERSCORE && __tc_us != NULL && __tc_ue != NULL)
204 1.1 mrg wunderscore(win);
205 1.12 jdc if ((attr_t) attr & __COLOR)
206 1.12 jdc __wcolor_set(win, (attr_t) attr);
207 1.12 jdc return OK;
208 1.1 mrg }
209 1.1 mrg
210 1.1 mrg /*
211 1.12 jdc * wattr_off --
212 1.15.4.1 matt * Test and unset wide attributes on window
213 1.1 mrg *
214 1.1 mrg * Note that the 'me' sequence unsets all attributes. We handle
215 1.1 mrg * which attributes should really be set in refresh.c:makech().
216 1.1 mrg */
217 1.12 jdc /* ARGSUSED */
218 1.1 mrg int
219 1.12 jdc wattr_off(WINDOW *win, attr_t attr, void *opt)
220 1.1 mrg {
221 1.3 jdc #ifdef DEBUG
222 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattr_off: win %p, attr %08x\n", win, attr);
223 1.3 jdc #endif
224 1.3 jdc /* If can do exit modes, unset the relevent attribute bits. */
225 1.9 jdc if (__tc_me != NULL) {
226 1.12 jdc if (attr & __BLINK)
227 1.2 blymn win->wattr &= ~__BLINK;
228 1.12 jdc if (attr & __BOLD)
229 1.2 blymn win->wattr &= ~__BOLD;
230 1.12 jdc if (attr & __DIM)
231 1.2 blymn win->wattr &= ~__DIM;
232 1.12 jdc if (attr & __BLANK)
233 1.2 blymn win->wattr &= ~__BLANK;
234 1.12 jdc if (attr & __PROTECT)
235 1.2 blymn win->wattr &= ~__PROTECT;
236 1.12 jdc if (attr & __REVERSE)
237 1.2 blymn win->wattr &= ~__REVERSE;
238 1.15 blymn #ifdef HAVE_WCHAR
239 1.15 blymn if (attr & WA_LOW)
240 1.15 blymn win->wattr &= ~WA_LOW;
241 1.15 blymn if (attr & WA_TOP)
242 1.15 blymn win->wattr &= ~WA_TOP;
243 1.15 blymn if (attr & WA_LEFT)
244 1.15 blymn win->wattr &= ~WA_LEFT;
245 1.15 blymn if (attr & WA_RIGHT)
246 1.15 blymn win->wattr &= ~WA_RIGHT;
247 1.15 blymn if (attr & WA_HORIZONTAL)
248 1.15 blymn win->wattr &= ~WA_HORIZONTAL;
249 1.15 blymn if (attr & WA_VERTICAL)
250 1.15 blymn win->wattr &= ~WA_VERTICAL;
251 1.15 blymn #endif /* HAVE_WCHAR */
252 1.1 mrg }
253 1.12 jdc if (attr & __STANDOUT)
254 1.2 blymn wstandend(win);
255 1.12 jdc if (attr & __UNDERSCORE)
256 1.1 mrg wunderend(win);
257 1.3 jdc if ((attr_t) attr & __COLOR) {
258 1.13 fvdl if (__tc_Co != 0)
259 1.3 jdc win->wattr &= ~__COLOR;
260 1.3 jdc }
261 1.12 jdc return OK;
262 1.12 jdc }
263 1.12 jdc
264 1.12 jdc /*
265 1.12 jdc * wattr_set --
266 1.15.4.1 matt * Set wide attributes and color pair on window
267 1.12 jdc */
268 1.12 jdc int
269 1.12 jdc wattr_set(WINDOW *win, attr_t attr, short pair, void *opt)
270 1.12 jdc {
271 1.12 jdc #ifdef DEBUG
272 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattr_set: win %p, attr %08x, pair %d\n",
273 1.14 jdc win, attr, pair);
274 1.12 jdc #endif
275 1.15 blymn wattr_off(win, __ATTRIBUTES, opt);
276 1.12 jdc /*
277 1.15 blymn * This overwrites any colour setting from the attributes
278 1.12 jdc * and is compatible with ncurses.
279 1.12 jdc */
280 1.15 blymn attr = (attr & ~__COLOR) | COLOR_PAIR(pair);
281 1.15 blymn wattr_on(win, attr, opt);
282 1.12 jdc return OK;
283 1.12 jdc }
284 1.12 jdc
285 1.12 jdc /*
286 1.12 jdc * wattron --
287 1.12 jdc * Test and set attributes.
288 1.12 jdc */
289 1.12 jdc int
290 1.12 jdc wattron(WINDOW *win, int attr)
291 1.12 jdc {
292 1.12 jdc #ifdef DEBUG
293 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattron: win %p, attr %08x\n", win, attr);
294 1.12 jdc #endif
295 1.12 jdc return wattr_on(win, (attr_t) attr, NULL);
296 1.12 jdc }
297 1.12 jdc
298 1.12 jdc /*
299 1.12 jdc * wattroff --
300 1.12 jdc * Test and unset attributes.
301 1.12 jdc */
302 1.12 jdc int
303 1.12 jdc wattroff(WINDOW *win, int attr)
304 1.12 jdc {
305 1.12 jdc #ifdef DEBUG
306 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattroff: win %p, attr %08x\n", win, attr);
307 1.12 jdc #endif
308 1.12 jdc return wattr_off(win, (attr_t) attr, NULL);
309 1.1 mrg }
310 1.1 mrg
311 1.1 mrg /*
312 1.8 jdc * wattrset --
313 1.1 mrg * Set specific attribute modes.
314 1.1 mrg * Unset others.
315 1.1 mrg */
316 1.1 mrg int
317 1.4 blymn wattrset(WINDOW *win, int attr)
318 1.1 mrg {
319 1.5 jdc #ifdef DEBUG
320 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattrset: win %p, attr %08x\n", win, attr);
321 1.5 jdc #endif
322 1.15 blymn wattr_off(win, __ATTRIBUTES, NULL);
323 1.12 jdc wattr_on(win, (attr_t) attr, NULL);
324 1.12 jdc return OK;
325 1.12 jdc }
326 1.12 jdc
327 1.12 jdc /*
328 1.12 jdc * wcolor_set --
329 1.12 jdc * Set color pair on window
330 1.12 jdc */
331 1.12 jdc /* ARGSUSED */
332 1.12 jdc int
333 1.12 jdc wcolor_set(WINDOW *win, short pair, void *opt)
334 1.12 jdc {
335 1.12 jdc #ifdef DEBUG
336 1.14 jdc __CTRACE(__CTRACE_COLOR, "wolor_set: win %p, pair %d\n", win, pair);
337 1.12 jdc #endif
338 1.12 jdc __wcolor_set(win, (attr_t) COLOR_PAIR(pair));
339 1.12 jdc return OK;
340 1.8 jdc }
341 1.8 jdc
342 1.8 jdc /*
343 1.8 jdc * getattrs --
344 1.15.4.1 matt * Get window attributes.
345 1.8 jdc */
346 1.8 jdc chtype
347 1.8 jdc getattrs(WINDOW *win)
348 1.8 jdc {
349 1.8 jdc #ifdef DEBUG
350 1.14 jdc __CTRACE(__CTRACE_ATTR, "getattrs: win %p\n", win);
351 1.8 jdc #endif
352 1.8 jdc return((chtype) win->wattr);
353 1.12 jdc }
354 1.12 jdc
355 1.12 jdc /*
356 1.15.4.1 matt * termattrs --
357 1.15.4.1 matt * Get terminal attributes
358 1.15.4.1 matt */
359 1.15.4.1 matt chtype
360 1.15.4.1 matt termattrs(void)
361 1.15.4.1 matt {
362 1.15.4.1 matt #ifdef DEBUG
363 1.15.4.1 matt __CTRACE(__CTRACE_ATTR, "termattrs\n");
364 1.15.4.1 matt #endif
365 1.15.4.1 matt chtype ch = 0;
366 1.15.4.1 matt
367 1.15.4.1 matt if (__tc_me != NULL) {
368 1.15.4.1 matt if (__tc_mb != NULL)
369 1.15.4.1 matt ch |= __BLINK;
370 1.15.4.1 matt if (__tc_md != NULL)
371 1.15.4.1 matt ch |= __BOLD;
372 1.15.4.1 matt if (__tc_mh != NULL)
373 1.15.4.1 matt ch |= __DIM;
374 1.15.4.1 matt if (__tc_mk != NULL)
375 1.15.4.1 matt ch |= __BLANK;
376 1.15.4.1 matt if (__tc_mp != NULL)
377 1.15.4.1 matt ch |= __PROTECT;
378 1.15.4.1 matt if (__tc_mr != NULL)
379 1.15.4.1 matt ch |= __REVERSE;
380 1.15.4.1 matt }
381 1.15.4.1 matt if (__tc_so != NULL && __tc_se != NULL)
382 1.15.4.1 matt ch |= __STANDOUT;
383 1.15.4.1 matt if (__tc_us != NULL && __tc_ue != NULL)
384 1.15.4.1 matt ch |= __UNDERSCORE;
385 1.15.4.1 matt if (__tc_as != NULL && __tc_ae != NULL)
386 1.15.4.1 matt ch |= __ALTCHARSET;
387 1.15.4.1 matt
388 1.15.4.1 matt return ch;
389 1.15.4.1 matt }
390 1.15.4.1 matt
391 1.15.4.1 matt /*
392 1.15.4.1 matt * term_attrs --
393 1.15.4.1 matt * Get terminal wide attributes
394 1.15.4.1 matt */
395 1.15.4.1 matt attr_t
396 1.15.4.1 matt term_attrs(void)
397 1.15.4.1 matt {
398 1.15.4.1 matt #ifdef DEBUG
399 1.15.4.1 matt __CTRACE(__CTRACE_ATTR, "term_attrs\n");
400 1.15.4.1 matt #endif
401 1.15.4.1 matt attr_t attr = 0;
402 1.15.4.1 matt
403 1.15.4.1 matt if (__tc_me != NULL) {
404 1.15.4.1 matt if (__tc_mb != NULL)
405 1.15.4.1 matt attr |= WA_BLINK;
406 1.15.4.1 matt if (__tc_md != NULL)
407 1.15.4.1 matt attr |= WA_BOLD;
408 1.15.4.1 matt if (__tc_mh != NULL)
409 1.15.4.1 matt attr |= WA_DIM;
410 1.15.4.1 matt if (__tc_mk != NULL)
411 1.15.4.1 matt attr |= WA_INVIS;
412 1.15.4.1 matt if (__tc_mp != NULL)
413 1.15.4.1 matt attr |= WA_PROTECT;
414 1.15.4.1 matt if (__tc_mr != NULL)
415 1.15.4.1 matt attr |= WA_REVERSE;
416 1.15.4.1 matt #ifdef HAVE_WCHAR
417 1.15.4.1 matt if (__tc_Xo != NULL)
418 1.15.4.1 matt attr |= WA_LOW;
419 1.15.4.1 matt if (__tc_Xt != NULL)
420 1.15.4.1 matt attr |= WA_TOP;
421 1.15.4.1 matt if (__tc_Xl != NULL)
422 1.15.4.1 matt attr |= WA_LEFT;
423 1.15.4.1 matt if (__tc_Xr != NULL)
424 1.15.4.1 matt attr |= WA_RIGHT;
425 1.15.4.1 matt if (__tc_Xh != NULL)
426 1.15.4.1 matt attr |= WA_HORIZONTAL;
427 1.15.4.1 matt if (__tc_Xv != NULL)
428 1.15.4.1 matt attr |= WA_VERTICAL;
429 1.15.4.1 matt #endif /* HAVE_WCHAR */
430 1.15.4.1 matt }
431 1.15.4.1 matt if (__tc_so != NULL && __tc_se != NULL)
432 1.15.4.1 matt attr |= WA_STANDOUT;
433 1.15.4.1 matt if (__tc_us != NULL && __tc_ue != NULL)
434 1.15.4.1 matt attr |= WA_UNDERLINE;
435 1.15.4.1 matt if (__tc_as != NULL && __tc_ae != NULL)
436 1.15.4.1 matt attr |= WA_ALTCHARSET;
437 1.15.4.1 matt
438 1.15.4.1 matt return attr;
439 1.15.4.1 matt }
440 1.15.4.1 matt
441 1.15.4.1 matt /*
442 1.12 jdc * __wcolor_set --
443 1.12 jdc * Set color attribute on window
444 1.12 jdc */
445 1.12 jdc void
446 1.12 jdc __wcolor_set(WINDOW *win, attr_t attr)
447 1.12 jdc {
448 1.12 jdc /* If another color pair is set, turn that off first. */
449 1.12 jdc win->wattr &= ~__COLOR;
450 1.12 jdc /* If can do color video, set the color pair bits. */
451 1.13 fvdl if (__tc_Co != 0 && attr & __COLOR)
452 1.12 jdc win->wattr |= attr & __COLOR;
453 1.1 mrg }
454