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