]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_bkgd.c
ncurses 6.1 - patch 20190817
[ncurses.git] / ncurses / base / lib_bkgd.c
1 /****************************************************************************
2  * Copyright (c) 1998-2018,2019 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Juergen Pfeifer                         1997                    *
33  *     and: Sven Verdoolaege                        2000                    *
34  *     and: Thomas E. Dickey                        1996-on                 *
35  ****************************************************************************/
36
37 #include <curses.priv.h>
38
39 MODULE_ID("$Id: lib_bkgd.c,v 1.53 2019/08/17 20:59:41 tom Exp $")
40
41 /*
42  * Set the window's background information.
43  */
44 #if USE_WIDEC_SUPPORT
45 NCURSES_EXPORT(void)
46 #else
47 static NCURSES_INLINE void
48 #endif
49 wbkgrndset(WINDOW *win, const ARG_CH_T ch)
50 {
51     T((T_CALLED("wbkgdset(%p,%s)"), (void *) win, _tracech_t(ch)));
52
53     if (win) {
54         attr_t off = AttrOf(win->_nc_bkgd);
55         attr_t on = AttrOf(CHDEREF(ch));
56
57         toggle_attr_off(WINDOW_ATTRS(win), off);
58         toggle_attr_on(WINDOW_ATTRS(win), on);
59
60 #if NCURSES_EXT_COLORS
61         {
62             int pair;
63
64             if ((pair = GetPair(win->_nc_bkgd)) != 0)
65                 SET_WINDOW_PAIR(win, 0);
66             if ((pair = GetPair(CHDEREF(ch))) != 0)
67                 SET_WINDOW_PAIR(win, pair);
68         }
69 #endif
70
71         if (CharOf(CHDEREF(ch)) == L('\0')) {
72             SetChar(win->_nc_bkgd, BLANK_TEXT, AttrOf(CHDEREF(ch)));
73             if_EXT_COLORS(SetPair(win->_nc_bkgd, GetPair(CHDEREF(ch))));
74         } else {
75             win->_nc_bkgd = CHDEREF(ch);
76         }
77 #if USE_WIDEC_SUPPORT
78         /*
79          * If we're compiled for wide-character support, _bkgrnd is the
80          * preferred location for the background information since it stores
81          * more than _bkgd.  Update _bkgd each time we modify _bkgrnd, so the
82          * macro getbkgd() will work.
83          */
84         {
85             cchar_t wch;
86             int tmp;
87
88             memset(&wch, 0, sizeof(wch));
89             (void) wgetbkgrnd(win, &wch);
90             tmp = _nc_to_char((wint_t) CharOf(wch));
91
92             win->_bkgd = (((tmp == EOF) ? ' ' : (chtype) tmp)
93                           | (AttrOf(wch) & ALL_BUT_COLOR)
94                           | (chtype) ColorPair(GET_WINDOW_PAIR(win)));
95         }
96 #endif
97     }
98     returnVoid;
99 }
100
101 NCURSES_EXPORT(void)
102 wbkgdset(WINDOW *win, chtype ch)
103 {
104     NCURSES_CH_T wch;
105     SetChar2(wch, ch);
106     wbkgrndset(win, CHREF(wch));
107 }
108
109 /*
110  * Set the window's background information and apply it to each cell.
111  */
112 #if USE_WIDEC_SUPPORT
113 NCURSES_EXPORT(int)
114 #else
115 static NCURSES_INLINE int
116 #undef wbkgrnd
117 #endif
118 wbkgrnd(WINDOW *win, const ARG_CH_T ch)
119 {
120 #undef  SP_PARM
121 #define SP_PARM SP              /* to use Charable() */
122     int code = ERR;
123
124     T((T_CALLED("wbkgd(%p,%s)"), (void *) win, _tracech_t(ch)));
125
126     if (SP == 0) {
127         ;
128     } else if (win) {
129         NCURSES_CH_T new_bkgd = CHDEREF(ch);
130         NCURSES_CH_T old_bkgd;
131         int y;
132         NCURSES_CH_T old_char;
133         attr_t old_attr;
134         int old_pair;
135         NCURSES_CH_T new_char;
136         attr_t new_attr;
137         int new_pair;
138
139         /* SVr4 trims color info if non-color terminal */
140         if (!SP->_pair_limit) {
141             RemAttr(new_bkgd, A_COLOR);
142             SetPair(new_bkgd, 0);
143         }
144
145         memset(&old_bkgd, 0, sizeof(old_bkgd));
146         (void) wgetbkgrnd(win, &old_bkgd);
147
148         if (!memcmp(&old_bkgd, &new_bkgd, sizeof(new_bkgd))) {
149             T(("...unchanged"));
150             returnCode(OK);
151         }
152
153         old_char = old_bkgd;
154         RemAttr(old_char, ~A_CHARTEXT);
155         old_attr = AttrOf(old_bkgd);
156         old_pair = GetPair(old_bkgd);
157
158         if (!(old_attr & A_COLOR)) {
159             old_pair = 0;
160         }
161         T(("... old background char %s, attr %s, pair %d",
162            _tracechar(CharOf(old_char)), _traceattr(old_attr), old_pair));
163
164         new_char = new_bkgd;
165         RemAttr(new_char, ~A_CHARTEXT);
166         new_attr = AttrOf(new_bkgd);
167         new_pair = GetPair(new_bkgd);
168
169         /* SVr4 limits background character to printable 7-bits */
170         if (!Charable(new_bkgd)) {
171             new_char = old_char;
172         }
173         if (!(new_attr & A_COLOR)) {
174             new_pair = 0;
175         }
176         T(("... new background char %s, attr %s, pair %d",
177            _tracechar(CharOf(new_char)), _traceattr(new_attr), new_pair));
178
179         (void) wbkgrndset(win, CHREF(new_bkgd));
180
181         /* SVr4 updates color pair if old/new match, otherwise just attrs */
182         if ((new_pair != 0) && (new_pair == old_pair)) {
183             WINDOW_ATTRS(win) = new_attr;
184             SET_WINDOW_PAIR(win, new_pair);
185         } else {
186             WINDOW_ATTRS(win) = new_attr;
187         }
188
189         for (y = 0; y <= win->_maxy; y++) {
190             int x;
191
192             for (x = 0; x <= win->_maxx; x++) {
193                 NCURSES_CH_T *cp = &(win->_line[y].text[x]);
194                 int tmp_pair = GetPair(*cp);
195                 attr_t tmp_attr = AttrOf(*cp);
196
197                 if (CharEq(*cp, old_bkgd)) {
198                     SetChar2(*cp, CharOf(new_char));
199                 }
200                 if (tmp_pair != 0) {
201                     if (tmp_pair == old_pair) {
202                         SetAttr(*cp, (tmp_attr & ~old_attr) | new_attr);
203                         SetPair(*cp, new_pair);
204                     } else {
205                         SetAttr(*cp,
206                                 (tmp_attr & (~old_attr | A_COLOR))
207                                 | (new_attr & ALL_BUT_COLOR));
208                     }
209                 } else {
210                     SetAttr(*cp, (tmp_attr & ~old_attr) | new_attr);
211                     SetPair(*cp, new_pair);
212                 }
213             }
214         }
215         touchwin(win);
216         _nc_synchook(win);
217         code = OK;
218     }
219     returnCode(code);
220 }
221
222 NCURSES_EXPORT(int)
223 wbkgd(WINDOW *win, chtype ch)
224 {
225     NCURSES_CH_T wch;
226     SetChar2(wch, ch);
227     return wbkgrnd(win, CHREF(wch));
228 }