xref: /openbsd-src/lib/libcurses/base/lib_bkgd.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: lib_bkgd.c,v 1.2 2001/01/22 18:01:37 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,2000 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  ****************************************************************************/
35 
36 #include <curses.priv.h>
37 
38 MODULE_ID("$From: lib_bkgd.c,v 1.14 2000/12/10 02:43:26 tom Exp $")
39 
40 NCURSES_EXPORT(void)
41 wbkgdset(WINDOW *win, chtype ch)
42 {
43     T((T_CALLED("wbkgdset(%p,%s)"), win, _tracechtype(ch)));
44 
45     if (win) {
46 	chtype off = AttrOf(win->_bkgd);
47 	chtype on = AttrOf(ch);
48 
49 	toggle_attr_off(win->_attrs, off);
50 	toggle_attr_on(win->_attrs, on);
51 
52 	if (TextOf(ch) == 0)
53 	    ch |= BLANK;
54 	win->_bkgd = ch;
55     }
56     returnVoid;
57 }
58 
59 NCURSES_EXPORT(int)
60 wbkgd(WINDOW *win, const chtype ch)
61 {
62     int code = ERR;
63     int x, y;
64     chtype new_bkgd = ch;
65 
66     T((T_CALLED("wbkgd(%p,%s)"), win, _tracechtype(new_bkgd)));
67 
68     if (win) {
69 	chtype old_bkgd = getbkgd(win);
70 
71 	wbkgdset(win, new_bkgd);
72 	wattrset(win, AttrOf(win->_bkgd));
73 
74 	for (y = 0; y <= win->_maxy; y++) {
75 	    for (x = 0; x <= win->_maxx; x++) {
76 		if (win->_line[y].text[x] == old_bkgd)
77 		    win->_line[y].text[x] = win->_bkgd;
78 		else
79 		    win->_line[y].text[x] =
80 			_nc_render(win, (A_ALTCHARSET &
81 					 AttrOf(win->_line[y].text[x]))
82 				   | TextOf(win->_line[y].text[x]));
83 	    }
84 	}
85 	touchwin(win);
86 	_nc_synchook(win);
87 	code = OK;
88     }
89     returnCode(code);
90 }
91