1 /****************************************************************************
2  * Copyright (c) 1998,2000,2001 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  ****************************************************************************/
33 
34 #include <curses.priv.h>
35 
36 MODULE_ID("$Id: lib_bkgd.c,v 1.26 2001/12/19 01:36:58 tom Exp $")
37 
38 /*
39  * Set the window's background information.
40  */
41 #if USE_WIDEC_SUPPORT
42 NCURSES_EXPORT(void)
43 #else
44 static inline void
45 #endif
46 wbkgrndset(WINDOW *win, const ARG_CH_T ch)
47 {
48     T((T_CALLED("wbkgdset(%p,%s)"), win, _tracech_t(ch)));
49 
50     if (win) {
51 	attr_t off = AttrOf(win->_nc_bkgd);
52 	attr_t on = AttrOf(CHDEREF(ch));
53 
54 	toggle_attr_off(win->_attrs, off);
55 	toggle_attr_on(win->_attrs, on);
56 
57 	if (CharOf(CHDEREF(ch)) == L('\0'))
58 	    SetChar(win->_nc_bkgd, BLANK_TEXT, AttrOf(CHDEREF(ch)));
59 	else
60 	    win->_nc_bkgd = CHDEREF(ch);
61 #if USE_WIDEC_SUPPORT
62 	/*
63 	 * If we're compiled for wide-character support, _bkgrnd is the
64 	 * preferred location for the background information since it stores
65 	 * more than _bkgd.  Update _bkgd each time we modify _bkgrnd, so the
66 	 * macro getbkgd() will work.
67 	 */
68 	{
69 	    cchar_t wch;
70 	    int tmp;
71 
72 	    wgetbkgrnd(win, &wch);
73 	    tmp = wctob(CharOf(wch));
74 
75 	    win->_bkgd = ((tmp == EOF) ? ' ' : (chtype) tmp) | AttrOf(wch);
76 	}
77 #endif
78     }
79     returnVoid;
80 }
81 
82 NCURSES_EXPORT(void)
83 wbkgdset(WINDOW *win, chtype ch)
84 {
85     NCURSES_CH_T wch;
86     SetChar2(wch, ch);
87     wbkgrndset(win, CHREF(wch));
88 }
89 
90 /*
91  * Set the window's background information and apply it to each cell.
92  */
93 #if USE_WIDEC_SUPPORT
94 NCURSES_EXPORT(int)
95 #else
96 static inline int
97 #undef wbkgrnd
98 #endif
99 wbkgrnd(WINDOW *win, const ARG_CH_T ch)
100 {
101     int code = ERR;
102     int x, y;
103     NCURSES_CH_T new_bkgd = CHDEREF(ch);
104 
105     T((T_CALLED("wbkgd(%p,%s)"), win, _tracech_t(ch)));
106 
107     if (win) {
108 	NCURSES_CH_T old_bkgrnd;
109 	wgetbkgrnd(win, &old_bkgrnd);
110 
111 	wbkgrndset(win, CHREF(new_bkgd));
112 	wattrset(win, AttrOf(win->_nc_bkgd));
113 
114 	for (y = 0; y <= win->_maxy; y++) {
115 	    for (x = 0; x <= win->_maxx; x++) {
116 		if (CharEq(win->_line[y].text[x], old_bkgrnd))
117 		    win->_line[y].text[x] = win->_nc_bkgd;
118 		else {
119 		    NCURSES_CH_T wch = win->_line[y].text[x];
120 		    RemAttr(wch, ~A_ALTCHARSET);
121 		    win->_line[y].text[x] = _nc_render(win, wch);
122 		}
123 	    }
124 	}
125 	touchwin(win);
126 	_nc_synchook(win);
127 	code = OK;
128     }
129     returnCode(code);
130 }
131 
132 NCURSES_EXPORT(int)
133 wbkgd(WINDOW *win, const chtype ch)
134 {
135     NCURSES_CH_T wch;
136     SetChar2(wch, ch);
137     return wbkgrnd(win, CHREF(wch));
138 }
139