xref: /openbsd/lib/libcurses/base/lib_bkgd.c (revision e5dd7070)
1 /* $OpenBSD: lib_bkgd.c,v 1.3 2010/01/12 23:22:05 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2006,2008 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  *     and: Juergen Pfeifer                         1997                    *
35  *     and: Sven Verdoolaege                        2000                    *
36  *     and: Thomas E. Dickey                        1996-on                 *
37  ****************************************************************************/
38 
39 #include <curses.priv.h>
40 
41 MODULE_ID("$Id: lib_bkgd.c,v 1.3 2010/01/12 23:22:05 nicm Exp $")
42 
43 /*
44  * Set the window's background information.
45  */
46 #if USE_WIDEC_SUPPORT
47 NCURSES_EXPORT(void)
48 #else
49 static NCURSES_INLINE void
50 #endif
51 wbkgrndset(WINDOW *win, const ARG_CH_T ch)
52 {
53     T((T_CALLED("wbkgdset(%p,%s)"), win, _tracech_t(ch)));
54 
55     if (win) {
56 	attr_t off = AttrOf(win->_nc_bkgd);
57 	attr_t on = AttrOf(CHDEREF(ch));
58 
59 	toggle_attr_off(WINDOW_ATTRS(win), off);
60 	toggle_attr_on(WINDOW_ATTRS(win), on);
61 
62 #if NCURSES_EXT_COLORS
63 	{
64 	    int pair;
65 
66 	    if ((pair = GetPair(win->_nc_bkgd)) != 0)
67 		SET_WINDOW_PAIR(win, 0);
68 	    if ((pair = GetPair(CHDEREF(ch))) != 0)
69 		SET_WINDOW_PAIR(win, pair);
70 	}
71 #endif
72 
73 	if (CharOf(CHDEREF(ch)) == L('\0')) {
74 	    SetChar(win->_nc_bkgd, BLANK_TEXT, AttrOf(CHDEREF(ch)));
75 	    if_EXT_COLORS(SetPair(win->_nc_bkgd, GetPair(CHDEREF(ch))));
76 	} else {
77 	    win->_nc_bkgd = CHDEREF(ch);
78 	}
79 #if USE_WIDEC_SUPPORT
80 	/*
81 	 * If we're compiled for wide-character support, _bkgrnd is the
82 	 * preferred location for the background information since it stores
83 	 * more than _bkgd.  Update _bkgd each time we modify _bkgrnd, so the
84 	 * macro getbkgd() will work.
85 	 */
86 	{
87 	    cchar_t wch;
88 	    int tmp;
89 
90 	    wgetbkgrnd(win, &wch);
91 	    tmp = _nc_to_char((wint_t) CharOf(wch));
92 
93 	    win->_bkgd = (((tmp == EOF) ? ' ' : (chtype) tmp)
94 			  | (AttrOf(wch) & ALL_BUT_COLOR)
95 			  | COLOR_PAIR(GET_WINDOW_PAIR(win)));
96 	}
97 #endif
98     }
99     returnVoid;
100 }
101 
102 NCURSES_EXPORT(void)
103 wbkgdset(WINDOW *win, chtype ch)
104 {
105     NCURSES_CH_T wch;
106     SetChar2(wch, ch);
107     wbkgrndset(win, CHREF(wch));
108 }
109 
110 /*
111  * Set the window's background information and apply it to each cell.
112  */
113 #if USE_WIDEC_SUPPORT
114 NCURSES_EXPORT(int)
115 #else
116 static NCURSES_INLINE int
117 #undef wbkgrnd
118 #endif
119 wbkgrnd(WINDOW *win, const ARG_CH_T ch)
120 {
121     int code = ERR;
122     int x, y;
123     NCURSES_CH_T new_bkgd = CHDEREF(ch);
124 
125     T((T_CALLED("wbkgd(%p,%s)"), win, _tracech_t(ch)));
126 
127     if (win) {
128 	NCURSES_CH_T old_bkgrnd;
129 	wgetbkgrnd(win, &old_bkgrnd);
130 
131 	wbkgrndset(win, CHREF(new_bkgd));
132 	wattrset(win, AttrOf(win->_nc_bkgd));
133 
134 	for (y = 0; y <= win->_maxy; y++) {
135 	    for (x = 0; x <= win->_maxx; x++) {
136 		if (CharEq(win->_line[y].text[x], old_bkgrnd)) {
137 		    win->_line[y].text[x] = win->_nc_bkgd;
138 		} else {
139 		    NCURSES_CH_T wch = win->_line[y].text[x];
140 		    RemAttr(wch, (~(A_ALTCHARSET | A_CHARTEXT)));
141 		    win->_line[y].text[x] = _nc_render(win, wch);
142 		}
143 	    }
144 	}
145 	touchwin(win);
146 	_nc_synchook(win);
147 	code = OK;
148     }
149     returnCode(code);
150 }
151 
152 NCURSES_EXPORT(int)
153 wbkgd(WINDOW *win, chtype ch)
154 {
155     NCURSES_CH_T wch;
156     SetChar2(wch, ch);
157     return wbkgrnd(win, CHREF(wch));
158 }
159