]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/widechar/lib_vid_attr.c
ncurses 5.7 - patch 20090425
[ncurses.git] / ncurses / widechar / lib_vid_attr.c
1 /****************************************************************************
2  * Copyright (c) 2002-2007,2009 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 /****************************************************************************
30  *  Author: Thomas E. Dickey                                                *
31  ****************************************************************************/
32
33 #include <curses.priv.h>
34 #include <term.h>
35
36 MODULE_ID("$Id: lib_vid_attr.c,v 1.7 2009/05/02 23:30:44 tom Exp $")
37
38 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
39
40 #define TurnOn(mask,mode) \
41         if ((turn_on & mask) && mode) { doPut(mode); }
42
43 #define TurnOff(mask,mode) \
44         if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
45
46         /* if there is no current screen, assume we *can* do color */
47 #define SetColorsIf(why, old_attr, old_pair) \
48         if (can_color && (why)) { \
49                 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
50                 if ((pair != old_pair) \
51                  || (fix_pair0 && (pair == 0)) \
52                  || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
53                         _nc_do_color(old_pair, pair, reverse, outc); \
54                 } \
55         }
56
57 #define set_color(mode, pair) mode &= ALL_BUT_COLOR; mode |= COLOR_PAIR(pair)
58
59 NCURSES_EXPORT(int)
60 NCURSES_SP_NAME(vid_puts)(NCURSES_SP_DCLx
61                           attr_t newmode,
62                           short pair,
63                           void *opts GCC_UNUSED,
64                           int (*outc) (int))
65 {
66 #if NCURSES_EXT_COLORS
67     static attr_t previous_attr = A_NORMAL;
68     static int previous_pair = 0;
69
70     attr_t turn_on, turn_off;
71     bool reverse = FALSE;
72     bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
73 #if NCURSES_EXT_FUNCS
74     bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
75 #else
76 #define fix_pair0 FALSE
77 #endif
78
79     newmode &= A_ATTRIBUTES;
80     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
81
82     /* this allows us to go on whether or not newterm() has been called */
83     if (SP_PARM) {
84         previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM));
85         previous_pair = GetPair(SCREEN_ATTRS(SP_PARM));
86     }
87
88     TR(TRACE_ATTRS, ("previous attribute was %s, %d",
89                      _traceattr(previous_attr), previous_pair));
90
91 #if !USE_XMC_SUPPORT
92     if ((SP_PARM != 0)
93         && (magic_cookie_glitch > 0))
94         newmode &= ~(SP_PARM->_xmc_suppress);
95 #endif
96
97     /*
98      * If we have a terminal that cannot combine color with video
99      * attributes, use the colors in preference.
100      */
101     if ((pair != 0
102          || fix_pair0)
103         && (no_color_video > 0)) {
104         /*
105          * If we had chosen the A_xxx definitions to correspond to the
106          * no_color_video mask, we could simply shift it up and mask off the
107          * attributes.  But we did not (actually copied Solaris' definitions).
108          * However, this is still simpler/faster than a lookup table.
109          *
110          * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
111          * A_DIM, A_BOLD which are 1:1 with no_color_video.  The bits that
112          * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
113          * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
114          * macro so this will work properly for the wide-character layout.
115          */
116         unsigned value = no_color_video;
117         attr_t mask = NCURSES_BITS((value & 63)
118                                    | ((value & 192) << 1)
119                                    | ((value & 256) >> 2), 8);
120
121         if ((mask & A_REVERSE) != 0
122             && (newmode & A_REVERSE) != 0) {
123             reverse = TRUE;
124             mask &= ~A_REVERSE;
125         }
126         newmode &= ~mask;
127     }
128
129     if (newmode == previous_attr
130         && pair == previous_pair)
131         returnCode(OK);
132
133     if (reverse) {
134         newmode &= ~A_REVERSE;
135     }
136
137     turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
138     turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
139
140     SetColorsIf(((pair == 0) && !fix_pair0), previous_attr, previous_pair);
141
142     if (newmode == A_NORMAL) {
143         if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
144             doPut(exit_alt_charset_mode);
145             previous_attr &= ~A_ALTCHARSET;
146         }
147         if (previous_attr) {
148             if (exit_attribute_mode) {
149                 doPut(exit_attribute_mode);
150             } else {
151                 if (!SP_PARM || SP_PARM->_use_rmul) {
152                     TurnOff(A_UNDERLINE, exit_underline_mode);
153                 }
154                 if (!SP_PARM || SP_PARM->_use_rmso) {
155                     TurnOff(A_STANDOUT, exit_standout_mode);
156                 }
157             }
158             previous_attr &= ALL_BUT_COLOR;
159             previous_pair = 0;
160         }
161
162         SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
163     } else if (set_attributes) {
164         if (turn_on || turn_off) {
165             TPUTS_TRACE("set_attributes");
166             tputs(TPARM_9(set_attributes,
167                           (newmode & A_STANDOUT) != 0,
168                           (newmode & A_UNDERLINE) != 0,
169                           (newmode & A_REVERSE) != 0,
170                           (newmode & A_BLINK) != 0,
171                           (newmode & A_DIM) != 0,
172                           (newmode & A_BOLD) != 0,
173                           (newmode & A_INVIS) != 0,
174                           (newmode & A_PROTECT) != 0,
175                           (newmode & A_ALTCHARSET) != 0), 1, outc);
176             previous_attr &= ALL_BUT_COLOR;
177             previous_pair = 0;
178         }
179         SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
180     } else {
181
182         TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
183
184         TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
185
186         if (!SP_PARM || SP_PARM->_use_rmul) {
187             TurnOff(A_UNDERLINE, exit_underline_mode);
188         }
189
190         if (!SP_PARM || SP_PARM->_use_rmso) {
191             TurnOff(A_STANDOUT, exit_standout_mode);
192         }
193
194         if (turn_off && exit_attribute_mode) {
195             doPut(exit_attribute_mode);
196             turn_on |= (newmode & ALL_BUT_COLOR);
197             previous_attr &= ALL_BUT_COLOR;
198             previous_pair = 0;
199         }
200         SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
201
202         TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
203         /* *INDENT-OFF* */
204         TurnOn(A_ALTCHARSET,    enter_alt_charset_mode);
205         TurnOn(A_BLINK,         enter_blink_mode);
206         TurnOn(A_BOLD,          enter_bold_mode);
207         TurnOn(A_DIM,           enter_dim_mode);
208         TurnOn(A_REVERSE,       enter_reverse_mode);
209         TurnOn(A_STANDOUT,      enter_standout_mode);
210         TurnOn(A_PROTECT,       enter_protected_mode);
211         TurnOn(A_INVIS,         enter_secure_mode);
212         TurnOn(A_UNDERLINE,     enter_underline_mode);
213 #if USE_WIDEC_SUPPORT
214         TurnOn(A_HORIZONTAL,    enter_horizontal_hl_mode);
215         TurnOn(A_LEFT,          enter_left_hl_mode);
216         TurnOn(A_LOW,           enter_low_hl_mode);
217         TurnOn(A_RIGHT,         enter_right_hl_mode);
218         TurnOn(A_TOP,           enter_top_hl_mode);
219         TurnOn(A_VERTICAL,      enter_vertical_hl_mode);
220 #endif
221         /* *INDENT-ON* */
222
223     }
224
225     if (reverse)
226         newmode |= A_REVERSE;
227
228     if (SP_PARM) {
229         SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
230         SetPair(SCREEN_ATTRS(SP_PARM), pair);
231     } else {
232         previous_attr = newmode;
233         previous_pair = pair;
234     }
235
236     returnCode(OK);
237 #else
238     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
239     set_color(newmode, pair);
240     returnCode(vidputs(newmode, outc));
241 #endif
242 }
243
244 #undef vid_attr
245 NCURSES_EXPORT(int)
246 NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx
247                            attr_t newmode,
248                            short pair,
249                            void *opts)
250 {
251     T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), pair));
252     returnCode(vid_puts(newmode, pair, opts, _nc_outch));
253 }
254
255 #if NCURSES_SP_FUNCS
256 NCURSES_EXPORT(int)
257 vid_puts(attr_t newmode, short pair, void *opts GCC_UNUSED, int (*outc) (int))
258 {
259     return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN, newmode, pair, opts, outc);
260 }
261
262 NCURSES_EXPORT(int)
263 vid_attr(attr_t newmode, short pair, void *opts)
264 {
265     return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair, opts);
266 }
267 #endif
268
269 /*
270  * This implementation uses the same mask values for A_xxx and WA_xxx, so
271  * we can use termattrs() for part of the logic.
272  */
273 NCURSES_EXPORT(attr_t)
274 term_attrs(void)
275 {
276     attr_t attrs;
277
278     T((T_CALLED("term_attrs()")));
279     attrs = termattrs();
280
281     /* these are only supported for wide-character mode */
282     if (enter_horizontal_hl_mode)
283         attrs |= WA_HORIZONTAL;
284     if (enter_left_hl_mode)
285         attrs |= WA_LEFT;
286     if (enter_low_hl_mode)
287         attrs |= WA_LOW;
288     if (enter_right_hl_mode)
289         attrs |= WA_RIGHT;
290     if (enter_top_hl_mode)
291         attrs |= WA_TOP;
292     if (enter_vertical_hl_mode)
293         attrs |= WA_VERTICAL;
294
295     returnAttr(attrs);
296 }