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