Home | History | Annotate | Line # | Download | only in libcurses
background.c revision 1.11
      1 /*	$NetBSD: background.c,v 1.11 2007/05/28 15:01:54 blymn Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Julian Coleman.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __RCSID("$NetBSD: background.c,v 1.11 2007/05/28 15:01:54 blymn Exp $");
     42 #endif				/* not lint */
     43 
     44 #include <stdlib.h>
     45 #include "curses.h"
     46 #include "curses_private.h"
     47 
     48 /*
     49  * bkgdset
     50  *	Set new background attributes on stdscr.
     51  */
     52 void
     53 bkgdset(chtype ch)
     54 {
     55 	wbkgdset(stdscr, ch);
     56 }
     57 
     58 /*
     59  * bkgd --
     60  *	Set new background and new background attributes on stdscr.
     61  */
     62 int
     63 bkgd(chtype ch)
     64 {
     65 	return(wbkgd(stdscr, ch));
     66 }
     67 
     68 /*
     69  * wbkgdset
     70  *	Set new background attributes.
     71  */
     72 void
     73 wbkgdset(WINDOW *win, chtype ch)
     74 {
     75 #ifdef DEBUG
     76 	__CTRACE(__CTRACE_ATTR, "wbkgdset: (%p), '%s', %08x\n",
     77 	    win, unctrl(ch & +__CHARTEXT), ch & __ATTRIBUTES);
     78 #endif
     79 
     80 	/* Background character. */
     81 	if (ch & __CHARTEXT)
     82 		win->bch = (wchar_t) ch & __CHARTEXT;
     83 
     84 	/* Background attributes (check colour). */
     85 	if (__using_color && !(ch & __COLOR))
     86 		ch |= __default_color;
     87 	win->battr = (attr_t) ch & __ATTRIBUTES;
     88 }
     89 
     90 /*
     91  * wbkgd --
     92  *	Set new background and new background attributes.
     93  */
     94 int
     95 wbkgd(WINDOW *win, chtype ch)
     96 {
     97 	int	y, x;
     98 
     99 #ifdef DEBUG
    100 	__CTRACE(__CTRACE_ATTR, "wbkgd: (%p), '%s', %08x\n",
    101 	    win, unctrl(ch & +__CHARTEXT), ch & __ATTRIBUTES);
    102 #endif
    103 
    104 	/* Background attributes (check colour). */
    105 	if (__using_color && !(ch & __COLOR))
    106 		ch |= __default_color;
    107 
    108 	win->battr = (attr_t) ch & __ATTRIBUTES;
    109 	wbkgdset(win, ch);
    110 	for (y = 0; y < win->maxy; y++)
    111 		for (x = 0; x < win->maxx; x++) {
    112 			/* Copy character if space */
    113 			if (ch & A_CHARTEXT && win->lines[y]->line[x].ch == ' ')
    114 				win->lines[y]->line[x].ch = ch & __CHARTEXT;
    115 			/* Merge attributes */
    116 			if (win->lines[y]->line[x].attr & __ALTCHARSET)
    117 				win->lines[y]->line[x].attr =
    118 				    (ch & __ATTRIBUTES) | __ALTCHARSET;
    119 			else
    120 				win->lines[y]->line[x].attr =
    121 				    ch & __ATTRIBUTES;
    122 		}
    123 	__touchwin(win);
    124 	return(OK);
    125 }
    126 
    127 /*
    128  * getbkgd --
    129  *	Get current background attributes.
    130  */
    131 chtype
    132 getbkgd(WINDOW *win)
    133 {
    134 	attr_t	battr;
    135 
    136 	/* Background attributes (check colour). */
    137 	battr = win->battr & A_ATTRIBUTES;
    138 	if (__using_color && ((battr & __COLOR) == __default_color))
    139 		battr &= ~__default_color;
    140 
    141 	return ((chtype) ((win->bch & A_CHARTEXT) | battr));
    142 }
    143 
    144 int bkgrnd(const cchar_t *wch)
    145 {
    146 #ifndef HAVE_WCHAR
    147 	return ERR;
    148 #else
    149 	return wbkgrnd( stdscr, wch );
    150 #endif /* HAVE_WCHAR */
    151 }
    152 
    153 void bkgrndset(const cchar_t *wch)
    154 {
    155 #ifdef HAVE_WCHAR
    156 	wbkgrndset( stdscr, wch );
    157 #endif /* HAVE_WCHAR */
    158 }
    159 
    160 int getbkgrnd(cchar_t *wch)
    161 {
    162 #ifndef HAVE_WCHAR
    163 	return ERR;
    164 #else
    165 	return wgetbkgrnd( stdscr, wch );
    166 #endif /* HAVE_WCHAR */
    167 }
    168 
    169 int wbkgrnd(WINDOW *win, const cchar_t *wch)
    170 {
    171 #ifndef HAVE_WCHAR
    172 	return ERR;
    173 #else
    174 /* 	int	y, x, i; */
    175 	attr_t battr;
    176 /* 	nschar_t *np, *tnp, *pnp; */
    177 
    178 #ifdef DEBUG
    179 	__CTRACE(__CTRACE_ATTR, "wbkgrnd: (%p), '%s', %x\n",
    180 		win, (char *) wunctrl(wch), wch->attributes);
    181 #endif
    182 
    183 	/* ignore multi-column characters */
    184 	if ( !wch->elements || wcwidth( wch->vals[ 0 ]) > 1 )
    185 		return ERR;
    186 
    187 	/* Background attributes (check colour). */
    188 	battr = wch->attributes & WA_ATTRIBUTES;
    189 	if (__using_color && !( battr & __COLOR))
    190 		battr |= __default_color;
    191 
    192 	win->battr = battr;
    193 	wbkgrndset(win, wch);
    194 	__touchwin(win);
    195 	return OK;
    196 #endif /* HAVE_WCHAR */
    197 }
    198 
    199 void wbkgrndset(WINDOW *win, const cchar_t *wch)
    200 {
    201 #ifdef HAVE_WCHAR
    202 	attr_t battr;
    203 	nschar_t *np, *tnp;
    204 	int i;
    205 
    206 #ifdef DEBUG
    207 	__CTRACE(__CTRACE_ATTR, "wbkgrndset: (%p), '%s', %x\n",
    208 		win, (char *) wunctrl(wch), wch->attributes);
    209 #endif
    210 
    211 	/* ignore multi-column characters */
    212 	if ( !wch->elements || wcwidth( wch->vals[ 0 ]) > 1 )
    213 		return;
    214 
    215 	/* Background character. */
    216 	tnp = np = win->bnsp;
    217 	if ( wcwidth( wch->vals[ 0 ]))
    218 		win->bch = wch->vals[ 0 ];
    219 	else {
    220 		if ( !np ) {
    221 			np = (nschar_t *)malloc(sizeof(nschar_t));
    222 			if (!np)
    223 				return;
    224 			np->next = NULL;
    225 			win->bnsp = np;
    226 		}
    227 		np->ch = wch->vals[ 0 ];
    228 		tnp = np;
    229 		np = np->next;
    230 	}
    231 	/* add non-spacing characters */
    232 	if ( wch->elements > 1 ) {
    233 		for ( i = 1; i < wch->elements; i++ ) {
    234 			if ( !np ) {
    235 				np = (nschar_t *)malloc(sizeof(nschar_t));
    236 				if (!np)
    237 					return;
    238 				np->next = NULL;
    239 				if ( tnp )
    240 					tnp->next = np;
    241 				else
    242 					win->bnsp = np;
    243 			}
    244 			np->ch = wch->vals[ i ];
    245 			tnp = np;
    246 			np = np->next;
    247 		}
    248 	}
    249 	/* clear the old non-spacing characters */
    250 	while ( np ) {
    251 		tnp = np->next;
    252 		free( np );
    253 		np = tnp;
    254 	}
    255 
    256 	/* Background attributes (check colour). */
    257 	battr = wch->attributes & WA_ATTRIBUTES;
    258 	if (__using_color && !( battr & __COLOR))
    259 		battr |= __default_color;
    260 	win->battr = battr;
    261 	SET_BGWCOL((*win), 1);
    262 #endif /* HAVE_WCHAR */
    263 }
    264 
    265 int wgetbkgrnd(WINDOW *win, cchar_t *wch)
    266 {
    267 #ifndef HAVE_WCHAR
    268 	return ERR;
    269 #else
    270 	nschar_t *np;
    271 
    272 	/* Background attributes (check colour). */
    273 	wch->attributes = win->battr & WA_ATTRIBUTES;
    274 	if (__using_color && (( wch->attributes & __COLOR )
    275 			== __default_color))
    276 		wch->attributes &= ~__default_color;
    277 	wch->vals[ 0 ] = win->bch;
    278 	wch->elements = 1;
    279 	np = win->bnsp;
    280 	if (np) {
    281 		while ( np && wch->elements < CURSES_CCHAR_MAX ) {
    282 			wch->vals[ wch->elements++ ] = np->ch;
    283 			np = np->next;
    284 		}
    285 	}
    286 
    287 	return OK;
    288 #endif /* HAVE_WCHAR */
    289 }
    290