]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_bkgd.c
ncurses 6.2 - patch 20200718
[ncurses.git] / ncurses / base / lib_bkgd.c
1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 1998-2014,2016 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Juergen Pfeifer                         1997                    *
34  *     and: Sven Verdoolaege                        2000                    *
35  *     and: Thomas E. Dickey                        1996-on                 *
36  ****************************************************************************/
37
38 #include <curses.priv.h>
39
40 MODULE_ID("$Id: lib_bkgd.c,v 1.55 2020/03/02 01:34:48 tom Exp $")
41
42 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
43
44 /*
45  * Set the window's background information.
46  */
47 #if USE_WIDEC_SUPPORT
48 NCURSES_EXPORT(void)
49 #else
50 static NCURSES_INLINE void
51 #endif
52 wbkgrndset(WINDOW *win, const ARG_CH_T ch)
53 {
54     T((T_CALLED("wbkgdset(%p,%s)"), (void *) win, _tracech_t(ch)));
55
56     if (win) {
57         attr_t off = AttrOf(win->_nc_bkgd);
58         attr_t on = AttrOf(CHDEREF(ch));
59
60         toggle_attr_off(WINDOW_ATTRS(win), off);
61         toggle_attr_on(WINDOW_ATTRS(win), on);
62
63 #if NCURSES_EXT_COLORS
64         {
65             int pair;
66
67             if ((pair = GetPair(win->_nc_bkgd)) != 0)
68                 SET_WINDOW_PAIR(win, 0);
69             if ((pair = GetPair(CHDEREF(ch))) != 0)
70                 SET_WINDOW_PAIR(win, pair);
71         }
72 #endif
73
74         if (CharOf(CHDEREF(ch)) == L('\0')) {
75             SetChar(win->_nc_bkgd, BLANK_TEXT, AttrOf(CHDEREF(ch)));
76             if_EXT_COLORS(SetPair(win->_nc_bkgd, GetPair(CHDEREF(ch))));
77         } else {
78             win->_nc_bkgd = CHDEREF(ch);
79         }
80 #if USE_WIDEC_SUPPORT
81         /*
82          * If we're compiled for wide-character support, _bkgrnd is the
83          * preferred location for the background information since it stores
84          * more than _bkgd.  Update _bkgd each time we modify _bkgrnd, so the
85          * macro getbkgd() will work.
86          */
87         {
88             cchar_t wch;
89             int tmp;
90
91             memset(&wch, 0, sizeof(wch));
92             (void) wgetbkgrnd(win, &wch);
93             tmp = _nc_to_char((wint_t) CharOf(wch));
94
95             win->_bkgd = (((tmp == EOF) ? ' ' : (chtype) tmp)
96                           | (AttrOf(wch) & ALL_BUT_COLOR)
97                           | (chtype) ColorPair(GET_WINDOW_PAIR(win)));
98         }
99 #endif
100     }
101     returnVoid;
102 }
103
104 NCURSES_EXPORT(void)
105 wbkgdset(WINDOW *win, chtype ch)
106 {
107     NCURSES_CH_T wch;
108     SetChar2(wch, ch);
109     wbkgrndset(win, CHREF(wch));
110 }
111
112 /*
113  * Set the window's background information and apply it to each cell.
114  */
115 #if USE_WIDEC_SUPPORT
116 NCURSES_EXPORT(int)
117 #else
118 static NCURSES_INLINE int
119 #undef wbkgrnd
120 #endif
121 wbkgrnd(WINDOW *win, const ARG_CH_T ch)
122 {
123 #undef  SP_PARM
124 #define SP_PARM SP              /* to use Charable() */
125     int code = ERR;
126
127     T((T_CALLED("wbkgd(%p,%s)"), (void *) win, _tracech_t(ch)));
128
129     if (SP == 0) {
130         ;
131     } else if (win) {
132         NCURSES_CH_T new_bkgd = CHDEREF(ch);
133         NCURSES_CH_T old_bkgd;
134         int y;
135         NCURSES_CH_T old_char;
136         attr_t old_attr;
137         int old_pair;
138         NCURSES_CH_T new_char;
139         attr_t new_attr;
140         int new_pair;
141
142         /* SVr4 trims color info if non-color terminal */
143         if (!SP->_pair_limit) {
144             RemAttr(new_bkgd, A_COLOR);
145             SetPair(new_bkgd, 0);
146         }
147
148         /* avoid setting background-character to a null */
149         if (CharOf(new_bkgd) == 0) {
150             NCURSES_CH_T tmp_bkgd = blank;
151             SetAttr(tmp_bkgd, AttrOf(new_bkgd));
152             SetPair(tmp_bkgd, GetPair(new_bkgd));
153             new_bkgd = tmp_bkgd;
154         }
155
156         memset(&old_bkgd, 0, sizeof(old_bkgd));
157         (void) wgetbkgrnd(win, &old_bkgd);
158
159         if (!memcmp(&old_bkgd, &new_bkgd, sizeof(new_bkgd))) {
160             T(("...unchanged"));
161             returnCode(OK);
162         }
163
164         old_char = old_bkgd;
165         RemAttr(old_char, ~A_CHARTEXT);
166         old_attr = AttrOf(old_bkgd);
167         old_pair = GetPair(old_bkgd);
168
169         if (!(old_attr & A_COLOR)) {
170             old_pair = 0;
171         }
172         T(("... old background char %s, attr %s, pair %d",
173            _tracechar(CharOf(old_char)), _traceattr(old_attr), old_pair));
174
175         new_char = new_bkgd;
176         RemAttr(new_char, ~A_CHARTEXT);
177         new_attr = AttrOf(new_bkgd);
178         new_pair = GetPair(new_bkgd);
179
180         /* SVr4 limits background character to printable 7-bits */
181         if (!Charable(new_bkgd)) {
182             new_char = old_char;
183         }
184         if (!(new_attr & A_COLOR)) {
185             new_pair = 0;
186         }
187         T(("... new background char %s, attr %s, pair %d",
188            _tracechar(CharOf(new_char)), _traceattr(new_attr), new_pair));
189
190         (void) wbkgrndset(win, CHREF(new_bkgd));
191
192         /* SVr4 updates color pair if old/new match, otherwise just attrs */
193         if ((new_pair != 0) && (new_pair == old_pair)) {
194             WINDOW_ATTRS(win) = new_attr;
195             SET_WINDOW_PAIR(win, new_pair);
196         } else {
197             WINDOW_ATTRS(win) = new_attr;
198         }
199
200         for (y = 0; y <= win->_maxy; y++) {
201             int x;
202
203             for (x = 0; x <= win->_maxx; x++) {
204                 NCURSES_CH_T *cp = &(win->_line[y].text[x]);
205                 int tmp_pair = GetPair(*cp);
206                 attr_t tmp_attr = AttrOf(*cp);
207
208                 if (CharEq(*cp, old_bkgd)) {
209                     SetChar2(*cp, CharOf(new_char));
210                 }
211                 if (tmp_pair != 0) {
212                     if (tmp_pair == old_pair) {
213                         SetAttr(*cp, (tmp_attr & ~old_attr) | new_attr);
214                         SetPair(*cp, new_pair);
215                     } else {
216                         SetAttr(*cp,
217                                 (tmp_attr & (~old_attr | A_COLOR))
218                                 | (new_attr & ALL_BUT_COLOR));
219                     }
220                 } else {
221                     SetAttr(*cp, (tmp_attr & ~old_attr) | new_attr);
222                     SetPair(*cp, new_pair);
223                 }
224             }
225         }
226         touchwin(win);
227         _nc_synchook(win);
228         code = OK;
229     }
230     returnCode(code);
231 }
232
233 NCURSES_EXPORT(int)
234 wbkgd(WINDOW *win, chtype ch)
235 {
236     NCURSES_CH_T wch;
237     SetChar2(wch, ch);
238     return wbkgrnd(win, CHREF(wch));
239 }