150276Speter/****************************************************************************
2262685Sdelphij * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
3050276Speter *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3150276Speter *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32166124Srafan *     and: Juergen Pfeifer                         1997                    *
33166124Srafan *     and: Sven Verdoolaege                        2000                    *
34166124Srafan *     and: Thomas E. Dickey                        1996-on                 *
3550276Speter ****************************************************************************/
3650276Speter
3750276Speter#include <curses.priv.h>
3850276Speter
39262685SdelphijMODULE_ID("$Id: lib_bkgd.c,v 1.48 2012/12/09 01:01:19 tom Exp $")
4050276Speter
4197049Speter/*
4297049Speter * Set the window's background information.
4397049Speter */
4497049Speter#if USE_WIDEC_SUPPORT
4576726SpeterNCURSES_EXPORT(void)
4697049Speter#else
47166124Srafanstatic NCURSES_INLINE void
4897049Speter#endif
4997049Speterwbkgrndset(WINDOW *win, const ARG_CH_T ch)
5050276Speter{
51262629Sdelphij    T((T_CALLED("wbkgdset(%p,%s)"), (void *) win, _tracech_t(ch)));
5276726Speter
5376726Speter    if (win) {
5497049Speter	attr_t off = AttrOf(win->_nc_bkgd);
5597049Speter	attr_t on = AttrOf(CHDEREF(ch));
5676726Speter
57166124Srafan	toggle_attr_off(WINDOW_ATTRS(win), off);
58166124Srafan	toggle_attr_on(WINDOW_ATTRS(win), on);
5976726Speter
60166124Srafan#if NCURSES_EXT_COLORS
61166124Srafan	{
62166124Srafan	    int pair;
63166124Srafan
64166124Srafan	    if ((pair = GetPair(win->_nc_bkgd)) != 0)
65166124Srafan		SET_WINDOW_PAIR(win, 0);
66166124Srafan	    if ((pair = GetPair(CHDEREF(ch))) != 0)
67166124Srafan		SET_WINDOW_PAIR(win, pair);
68166124Srafan	}
69166124Srafan#endif
70166124Srafan
71166124Srafan	if (CharOf(CHDEREF(ch)) == L('\0')) {
7297049Speter	    SetChar(win->_nc_bkgd, BLANK_TEXT, AttrOf(CHDEREF(ch)));
73178866Srafan	    if_EXT_COLORS(SetPair(win->_nc_bkgd, GetPair(CHDEREF(ch))));
74166124Srafan	} else {
7597049Speter	    win->_nc_bkgd = CHDEREF(ch);
76166124Srafan	}
7797049Speter#if USE_WIDEC_SUPPORT
7897049Speter	/*
7997049Speter	 * If we're compiled for wide-character support, _bkgrnd is the
8097049Speter	 * preferred location for the background information since it stores
8197049Speter	 * more than _bkgd.  Update _bkgd each time we modify _bkgrnd, so the
8297049Speter	 * macro getbkgd() will work.
8397049Speter	 */
8497049Speter	{
8597049Speter	    cchar_t wch;
8697049Speter	    int tmp;
8797049Speter
88262685Sdelphij	    memset(&wch, 0, sizeof(wch));
89262629Sdelphij	    (void) wgetbkgrnd(win, &wch);
90166124Srafan	    tmp = _nc_to_char((wint_t) CharOf(wch));
9197049Speter
92166124Srafan	    win->_bkgd = (((tmp == EOF) ? ' ' : (chtype) tmp)
93166124Srafan			  | (AttrOf(wch) & ALL_BUT_COLOR)
94262629Sdelphij			  | (chtype) ColorPair(GET_WINDOW_PAIR(win)));
9597049Speter	}
9697049Speter#endif
9776726Speter    }
9876726Speter    returnVoid;
9950276Speter}
10050276Speter
10197049SpeterNCURSES_EXPORT(void)
10297049Speterwbkgdset(WINDOW *win, chtype ch)
10397049Speter{
10497049Speter    NCURSES_CH_T wch;
10597049Speter    SetChar2(wch, ch);
10697049Speter    wbkgrndset(win, CHREF(wch));
10797049Speter}
10897049Speter
10997049Speter/*
11097049Speter * Set the window's background information and apply it to each cell.
11197049Speter */
11297049Speter#if USE_WIDEC_SUPPORT
11376726SpeterNCURSES_EXPORT(int)
11497049Speter#else
115166124Srafanstatic NCURSES_INLINE int
11697049Speter#undef wbkgrnd
11797049Speter#endif
11897049Speterwbkgrnd(WINDOW *win, const ARG_CH_T ch)
11950276Speter{
12076726Speter    int code = ERR;
12176726Speter    int x, y;
12250276Speter
123262629Sdelphij    T((T_CALLED("wbkgd(%p,%s)"), (void *) win, _tracech_t(ch)));
12450276Speter
12576726Speter    if (win) {
126262685Sdelphij	NCURSES_CH_T new_bkgd = CHDEREF(ch);
12797049Speter	NCURSES_CH_T old_bkgrnd;
128262685Sdelphij
129262685Sdelphij	memset(&old_bkgrnd, 0, sizeof(old_bkgrnd));
13097049Speter	wgetbkgrnd(win, &old_bkgrnd);
13150276Speter
132262629Sdelphij	(void) wbkgrndset(win, CHREF(new_bkgd));
133262685Sdelphij	(void) wattrset(win, (int) AttrOf(win->_nc_bkgd));
13476726Speter
13576726Speter	for (y = 0; y <= win->_maxy; y++) {
13676726Speter	    for (x = 0; x <= win->_maxx; x++) {
137166124Srafan		if (CharEq(win->_line[y].text[x], old_bkgrnd)) {
13897049Speter		    win->_line[y].text[x] = win->_nc_bkgd;
139166124Srafan		} else {
14097049Speter		    NCURSES_CH_T wch = win->_line[y].text[x];
141166124Srafan		    RemAttr(wch, (~(A_ALTCHARSET | A_CHARTEXT)));
14297049Speter		    win->_line[y].text[x] = _nc_render(win, wch);
14397049Speter		}
14476726Speter	    }
14576726Speter	}
14676726Speter	touchwin(win);
14776726Speter	_nc_synchook(win);
14876726Speter	code = OK;
14950276Speter    }
15076726Speter    returnCode(code);
15150276Speter}
15297049Speter
15397049SpeterNCURSES_EXPORT(int)
154166124Srafanwbkgd(WINDOW *win, chtype ch)
15597049Speter{
15697049Speter    NCURSES_CH_T wch;
15797049Speter    SetChar2(wch, ch);
15897049Speter    return wbkgrnd(win, CHREF(wch));
15997049Speter}
160