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