Back to index

tetex-bin  3.0
lib_vidattr.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998,1999,2000,2001 Free Software Foundation, Inc.         *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 /*
00035  *     vidputs(newmode, outc)
00036  *
00037  *     newmode is taken to be the logical 'or' of the symbols in curses.h
00038  *     representing graphic renditions.  The terminal is set to be in all of
00039  *     the given modes, if possible.
00040  *
00041  *     if the new attribute is normal
00042  *            if exit-alt-char-set exists
00043  *                   emit it
00044  *            emit exit-attribute-mode
00045  *     else if set-attributes exists
00046  *            use it to set exactly what you want
00047  *     else
00048  *            if exit-attribute-mode exists
00049  *                   turn off everything
00050  *            else
00051  *                   turn off those which can be turned off and aren't in
00052  *                   newmode.
00053  *            turn on each mode which should be on and isn't, one by one
00054  *
00055  *     NOTE that this algorithm won't achieve the desired mix of attributes
00056  *     in some cases, but those are probably just those cases in which it is
00057  *     actually impossible, anyway, so...
00058  *
00059  *     NOTE that we cannot assume that there's no interaction between color
00060  *     and other attribute resets.  So each time we reset color (or other
00061  *     attributes) we'll have to be prepared to restore the other.
00062  */
00063 
00064 #include <curses.priv.h>
00065 #include <term.h>
00066 
00067 MODULE_ID("$Id: lib_vidattr.c,v 1.39 2001/08/26 00:40:46 Philippe.Blain Exp $")
00068 
00069 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
00070 
00071 #define TurnOn(mask,mode) \
00072        if ((turn_on & mask) && mode) { doPut(mode); }
00073 
00074 #define TurnOff(mask,mode) \
00075        if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
00076 
00077        /* if there is no current screen, assume we *can* do color */
00078 #define SetColorsIf(why,old_attr) \
00079        if (can_color && (why)) { \
00080               int old_pair = PAIR_NUMBER(old_attr); \
00081               TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
00082               if ((pair != old_pair) \
00083                || (fix_pair0 && (pair == 0)) \
00084                || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
00085                      _nc_do_color(old_pair, pair, reverse, outc); \
00086               } \
00087        }
00088 
00089 NCURSES_EXPORT(int)
00090 vidputs
00091 (chtype newmode, int (*outc) (int))
00092 {
00093     static attr_t previous_attr = A_NORMAL;
00094     attr_t turn_on, turn_off;
00095     int pair;
00096     bool reverse = FALSE;
00097     bool can_color = (SP == 0 || SP->_coloron);
00098 #if NCURSES_EXT_FUNCS
00099     bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
00100 #else
00101 #define fix_pair0 FALSE
00102 #endif
00103 
00104     T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
00105 
00106     /* this allows us to go on whether or not newterm() has been called */
00107     if (SP)
00108        previous_attr = SP->_current_attr;
00109 
00110     TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(previous_attr)));
00111 
00112 #if !USE_XMC_SUPPORT
00113     if ((SP != 0)
00114        && (magic_cookie_glitch > 0))
00115        newmode &= ~(SP->_xmc_suppress);
00116 #endif
00117 
00118     /*
00119      * If we have a terminal that cannot combine color with video
00120      * attributes, use the colors in preference.
00121      */
00122     if (((newmode & A_COLOR) != 0
00123         || fix_pair0)
00124        && (no_color_video > 0)) {
00125        /*
00126         * If we had chosen the A_xxx definitions to correspond to the
00127         * no_color_video mask, we could simply shift it up and mask off the
00128         * attributes.  But we did not (actually copied Solaris' definitions).
00129         * However, this is still simpler/faster than a lookup table.
00130         *
00131         * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
00132         * A_DIM, A_BOLD which are 1:1 with no_color_video.  The bits that
00133         * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
00134         * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
00135         * macro so this will work properly for the wide-character layout.
00136         */
00137        unsigned value = no_color_video;
00138        attr_t mask = NCURSES_BITS((value & 63)
00139                                | ((value & 192) << 1)
00140                                | ((value & 256) >> 2), 8);
00141 
00142        if ((mask & A_REVERSE) != 0
00143            && (newmode & A_REVERSE) != 0) {
00144            reverse = TRUE;
00145            mask &= ~A_REVERSE;
00146        }
00147        newmode &= ~mask;
00148     }
00149 
00150     if (newmode == previous_attr)
00151        returnCode(OK);
00152 
00153     pair = PAIR_NUMBER(newmode);
00154 
00155     if (reverse) {
00156        newmode &= ~A_REVERSE;
00157     }
00158 
00159     turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
00160     turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
00161 
00162     SetColorsIf(((pair == 0) && !fix_pair0), previous_attr);
00163 
00164     if (newmode == A_NORMAL) {
00165        if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
00166            doPut(exit_alt_charset_mode);
00167            previous_attr &= ~A_ALTCHARSET;
00168        }
00169        if (previous_attr) {
00170            if (exit_attribute_mode) {
00171               doPut(exit_attribute_mode);
00172            } else {
00173               if (!SP || SP->_use_rmul) {
00174                   TurnOff(A_UNDERLINE, exit_underline_mode);
00175               }
00176               if (!SP || SP->_use_rmso) {
00177                   TurnOff(A_STANDOUT, exit_standout_mode);
00178               }
00179            }
00180            previous_attr &= ~A_COLOR;
00181        }
00182 
00183        SetColorsIf((pair != 0) || fix_pair0, previous_attr);
00184     } else if (set_attributes) {
00185        if (turn_on || turn_off) {
00186            TPUTS_TRACE("set_attributes");
00187            tputs(tparm(set_attributes,
00188                      (newmode & A_STANDOUT) != 0,
00189                      (newmode & A_UNDERLINE) != 0,
00190                      (newmode & A_REVERSE) != 0,
00191                      (newmode & A_BLINK) != 0,
00192                      (newmode & A_DIM) != 0,
00193                      (newmode & A_BOLD) != 0,
00194                      (newmode & A_INVIS) != 0,
00195                      (newmode & A_PROTECT) != 0,
00196                      (newmode & A_ALTCHARSET) != 0), 1, outc);
00197            previous_attr &= ~A_COLOR;
00198        }
00199        SetColorsIf((pair != 0) || fix_pair0, previous_attr);
00200     } else {
00201 
00202        TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
00203 
00204        TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
00205 
00206        if (!SP || SP->_use_rmul) {
00207            TurnOff(A_UNDERLINE, exit_underline_mode);
00208        }
00209 
00210        if (!SP || SP->_use_rmso) {
00211            TurnOff(A_STANDOUT, exit_standout_mode);
00212        }
00213 
00214        if (turn_off && exit_attribute_mode) {
00215            doPut(exit_attribute_mode);
00216            turn_on |= (newmode & (chtype) (~A_COLOR));
00217            previous_attr &= ~A_COLOR;
00218        }
00219        SetColorsIf((pair != 0) || fix_pair0, previous_attr);
00220 
00221        TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
00222        /* *INDENT-OFF* */
00223        TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
00224        TurnOn(A_BLINK,             enter_blink_mode);
00225        TurnOn(A_BOLD,              enter_bold_mode);
00226        TurnOn(A_DIM,        enter_dim_mode);
00227        TurnOn(A_REVERSE,    enter_reverse_mode);
00228        TurnOn(A_STANDOUT,   enter_standout_mode);
00229        TurnOn(A_PROTECT,    enter_protected_mode);
00230        TurnOn(A_INVIS,             enter_secure_mode);
00231        TurnOn(A_UNDERLINE,  enter_underline_mode);
00232 #ifdef enter_horizontal_hl_mode
00233        TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
00234 #endif
00235 #ifdef enter_left_hl_mode
00236        TurnOn(A_LEFT,              enter_left_hl_mode);
00237 #endif
00238 #ifdef enter_low_hl_mode
00239        TurnOn(A_LOW,        enter_low_hl_mode);
00240 #endif
00241 #ifdef enter_right_hl_mode
00242        TurnOn(A_RIGHT,             enter_right_hl_mode);
00243 #endif
00244 #ifdef enter_top_hl_mode
00245        TurnOn(A_TOP,        enter_top_hl_mode);
00246 #endif
00247 #ifdef enter_vertical_hl_mode
00248        TurnOn(A_VERTICAL,   enter_vertical_hl_mode);
00249 #endif
00250        /* *INDENT-ON* */
00251 
00252     }
00253 
00254     if (reverse)
00255        newmode |= A_REVERSE;
00256 
00257     if (SP)
00258        SP->_current_attr = newmode;
00259     else
00260        previous_attr = newmode;
00261 
00262     returnCode(OK);
00263 }
00264 
00265 NCURSES_EXPORT(int)
00266 vidattr(chtype newmode)
00267 {
00268     T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
00269 
00270     returnCode(vidputs(newmode, _nc_outch));
00271 }
00272 
00273 NCURSES_EXPORT(chtype)
00274 termattrs(void)
00275 {
00276     chtype attrs = A_NORMAL;
00277 
00278     T((T_CALLED("termattrs()")));
00279     if (enter_alt_charset_mode)
00280        attrs |= A_ALTCHARSET;
00281 
00282     if (enter_blink_mode)
00283        attrs |= A_BLINK;
00284 
00285     if (enter_bold_mode)
00286        attrs |= A_BOLD;
00287 
00288     if (enter_dim_mode)
00289        attrs |= A_DIM;
00290 
00291     if (enter_reverse_mode)
00292        attrs |= A_REVERSE;
00293 
00294     if (enter_standout_mode)
00295        attrs |= A_STANDOUT;
00296 
00297     if (enter_protected_mode)
00298        attrs |= A_PROTECT;
00299 
00300     if (enter_secure_mode)
00301        attrs |= A_INVIS;
00302 
00303     if (enter_underline_mode)
00304        attrs |= A_UNDERLINE;
00305 
00306     if (SP->_coloron)
00307        attrs |= A_COLOR;
00308 
00309     returnChar(attrs);
00310 }