xref: /illumos-gate/usr/src/lib/libcurses/screen/wbkgd.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*LINTLIBRARY*/
41 
42 #include	<sys/types.h>
43 #include	"curses_inc.h"
44 
45 /* Change the background of a window.  nbkgd :	new background. */
46 
47 int
48 wbkgd(WINDOW *win, chtype nbkgd)
49 {
50 	short	maxx;
51 	int	x, y;
52 	chtype	*wcp, obkgda, obkgdc, nbkgda,
53 		nbkgdc, acolor, c;
54 	short	*begch, *endch;
55 
56 	/* if 'nbkgd' contains color information, but this is not a color   */
57 	/* terminal, erase that information.				*/
58 
59 	if ((nbkgd & A_COLOR) && (cur_term->_pairs_tbl == NULL))
60 		nbkgd &= ~A_COLOR;
61 
62 	if (nbkgd == win->_bkgd)
63 		return (OK);
64 
65 	obkgdc = _CHAR(win->_bkgd);
66 	obkgda = _ATTR(win->_bkgd);
67 
68 	nbkgdc = _CHAR(nbkgd);
69 	nbkgda = _ATTR(nbkgd);
70 
71 	/* switch byte order if necessary */
72 	if (ISCBIT(nbkgdc))
73 		nbkgdc = _CHAR((RBYTE(nbkgdc) << 8) | (LBYTE(nbkgdc)|MBIT)) |
74 		    CBIT;
75 	c = RBYTE(nbkgdc);
76 	if ((nbkgdc < ' ' || nbkgdc == _CTRL('?')) ||
77 	    _curs_scrwidth[TYPE(c)] > 1)
78 		nbkgdc = obkgdc;
79 	nbkgd = (nbkgdc & ~CBIT) | nbkgda;
80 
81 	win->_bkgd = nbkgd;
82 
83 	/* delete the old background from the attribute field and replace    */
84 	/* it with the new background.  Note: if the same attribute was	*/
85 	/* first set by wbkgd() and then by wattron(), or vice versa, it */
86 	/* will be deleted, so the effect of wattron() will be lost.	 */
87 	/* This applies to both video and color attributes.		 */
88 
89 	if ((acolor = (win->_attrs & A_COLOR)) != 0) {
90 		if (acolor == (obkgda & A_COLOR)) {
91 			win->_attrs = _ATTR((win->_attrs & ~obkgda) | nbkgda);
92 		} else {
93 			win->_attrs = _ATTR((win->_attrs &
94 			    (~obkgda | A_COLOR)) | (nbkgda & ~A_COLOR));
95 		}
96 	} else
97 		win->_attrs = _ATTR((win->_attrs & ~obkgda) | nbkgda);
98 
99 	maxx = win->_maxx - 1;
100 	begch = win->_firstch;
101 	endch = win->_lastch;
102 	for (y = win->_maxy-1; y >= 0; --y, ++begch, ++endch) {
103 		for (x = maxx, wcp = win->_y[y]; x-- >= 0; ++wcp) {
104 			if ((c = _CHAR(*wcp)) == obkgdc)
105 				c = nbkgdc;
106 			if ((acolor = (*wcp & A_COLOR)) != 0) {
107 				if (acolor == (obkgda & A_COLOR))
108 					*wcp = c | _ATTR((*wcp & ~obkgda) |
109 					    nbkgda);
110 				else
111 					*wcp = c | _ATTR((*wcp & (~obkgda |
112 					    A_COLOR)) | (nbkgda & ~A_COLOR));
113 			} else
114 				*wcp = c | _ATTR((*wcp & ~obkgda) | nbkgda);
115 		}
116 		*begch = 0;
117 		*endch = maxx;
118 	}
119 
120 	win->_flags |= _WINCHANGED;
121 	if (win->_sync)
122 		wsyncup(win);
123 
124 	return (win->_immed ? wrefresh(win) : OK);
125 }
126