xref: /illumos-gate/usr/src/lib/libcurses/screen/wbkgd.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	<sys/types.h>
45 #include	"curses_inc.h"
46 
47 /* Change the background of a window.  nbkgd :	new background. */
48 
49 int
50 wbkgd(WINDOW *win, chtype nbkgd)
51 {
52 	short	maxx;
53 	int	x, y;
54 	chtype	*wcp, obkgda, obkgdc, nbkgda,
55 		nbkgdc, acolor, c;
56 	short	*begch, *endch;
57 
58 	/* if 'nbkgd' contains color information, but this is not a color   */
59 	/* terminal, erase that information.				*/
60 
61 	if ((nbkgd & A_COLOR) && (cur_term->_pairs_tbl == NULL))
62 		nbkgd &= ~A_COLOR;
63 
64 	if (nbkgd == win->_bkgd)
65 		return (OK);
66 
67 	obkgdc = _CHAR(win->_bkgd);
68 	obkgda = _ATTR(win->_bkgd);
69 
70 	nbkgdc = _CHAR(nbkgd);
71 	nbkgda = _ATTR(nbkgd);
72 
73 	/* switch byte order if necessary */
74 	if (ISCBIT(nbkgdc))
75 		nbkgdc = _CHAR((RBYTE(nbkgdc) << 8) | (LBYTE(nbkgdc)|MBIT)) |
76 		    CBIT;
77 	c = RBYTE(nbkgdc);
78 	if ((nbkgdc < ' ' || nbkgdc == _CTRL('?')) ||
79 	    _curs_scrwidth[TYPE(c)] > 1)
80 		nbkgdc = obkgdc;
81 	nbkgd = (nbkgdc & ~CBIT) | nbkgda;
82 
83 	win->_bkgd = nbkgd;
84 
85 	/* delete the old background from the attribute field and replace    */
86 	/* it with the new background.  Note: if the same attribute was	*/
87 	/* first set by wbkgd() and then by wattron(), or vice versa, it */
88 	/* will be deleted, so the effect of wattron() will be lost.	 */
89 	/* This applies to both video and color attributes.		 */
90 
91 	if ((acolor = (win->_attrs & A_COLOR)) != 0) {
92 		if (acolor == (obkgda & A_COLOR)) {
93 			win->_attrs = _ATTR((win->_attrs & ~obkgda) | nbkgda);
94 		} else {
95 			win->_attrs = _ATTR((win->_attrs &
96 			    (~obkgda | A_COLOR)) | (nbkgda & ~A_COLOR));
97 		}
98 	} else
99 		win->_attrs = _ATTR((win->_attrs & ~obkgda) | nbkgda);
100 
101 	maxx = win->_maxx - 1;
102 	begch = win->_firstch;
103 	endch = win->_lastch;
104 	for (y = win->_maxy-1; y >= 0; --y, ++begch, ++endch) {
105 		for (x = maxx, wcp = win->_y[y]; x-- >= 0; ++wcp) {
106 			if ((c = _CHAR(*wcp)) == obkgdc)
107 				c = nbkgdc;
108 			if ((acolor = (*wcp & A_COLOR)) != 0) {
109 				if (acolor == (obkgda & A_COLOR))
110 					*wcp = c | _ATTR((*wcp & ~obkgda) |
111 					    nbkgda);
112 				else
113 					*wcp = c | _ATTR((*wcp & (~obkgda |
114 					    A_COLOR)) | (nbkgda & ~A_COLOR));
115 			} else
116 				*wcp = c | _ATTR((*wcp & ~obkgda) | nbkgda);
117 		}
118 		*begch = 0;
119 		*endch = maxx;
120 	}
121 
122 	win->_flags |= _WINCHANGED;
123 	if (win->_sync)
124 		wsyncup(win);
125 
126 	return (win->_immed ? wrefresh(win) : OK);
127 }
128