Home | History | Annotate | Line # | Download | only in libcurses
background.c revision 1.21
      1 /*	$NetBSD: background.c,v 1.21 2018/11/18 22:11:38 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.21 2018/11/18 22:11:38 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 	wbkgdset(win, ch);
     99 
    100 	for (y = 0; y < win->maxy; y++)
    101 		for (x = 0; x < win->maxx; x++) {
    102 			/* Copy character if space */
    103 			if (ch & __CHARTEXT && win->alines[y]->line[x].ch == ' ')
    104 				win->alines[y]->line[x].ch = ch & __CHARTEXT;
    105 			/* Merge attributes */
    106 			if (win->alines[y]->line[x].attr & __ALTCHARSET)
    107 				win->alines[y]->line[x].attr =
    108 				    (ch & __ATTRIBUTES) | __ALTCHARSET;
    109 			else
    110 				win->alines[y]->line[x].attr =
    111 				    ch & __ATTRIBUTES;
    112 #ifdef HAVE_WCHAR
    113 			SET_WCOL(win->alines[y]->line[x], 1);
    114 #endif
    115 		}
    116 	__touchwin(win);
    117 	return(OK);
    118 }
    119 
    120 /*
    121  * getbkgd --
    122  *	Get current background attributes.
    123  */
    124 chtype
    125 getbkgd(WINDOW *win)
    126 {
    127 	attr_t	battr;
    128 
    129 	/* Background attributes (check colour). */
    130 	battr = win->battr & A_ATTRIBUTES;
    131 	if (__using_color && ((battr & __COLOR) == __default_color))
    132 		battr &= ~__default_color;
    133 
    134 	return ((chtype) ((win->bch & A_CHARTEXT) | battr));
    135 }
    136 
    137 
    138 #ifdef HAVE_WCHAR
    139 
    140 int
    141 bkgrnd(const cchar_t *wch)
    142 {
    143 	return wbkgrnd( stdscr, wch );
    144 }
    145 
    146 
    147 void
    148 bkgrndset(const cchar_t *wch)
    149 {
    150 	wbkgrndset( stdscr, wch );
    151 }
    152 
    153 
    154 int
    155 getbkgrnd(cchar_t *wch)
    156 {
    157 	return wgetbkgrnd( stdscr, wch );
    158 }
    159 
    160 
    161 int
    162 wbkgrnd(WINDOW *win, const cchar_t *wch)
    163 {
    164 	attr_t battr;
    165 
    166 #ifdef DEBUG
    167 	__CTRACE(__CTRACE_ATTR, "wbkgrnd: (%p), '%s', %x\n",
    168 		win, (const char *) wunctrl(wch), wch->attributes);
    169 #endif
    170 
    171 	/* ignore multi-column characters */
    172 	if (!wch->elements || wcwidth( wch->vals[ 0 ]) > 1)
    173 		return ERR;
    174 
    175 	/* Background attributes (check colour). */
    176 	battr = wch->attributes & WA_ATTRIBUTES;
    177 	if (__using_color && !( battr & __COLOR))
    178 		battr |= __default_color;
    179 
    180 	win->battr = battr;
    181 	wbkgrndset(win, wch);
    182 	__touchwin(win);
    183 	return OK;
    184 }
    185 
    186 
    187 void
    188 wbkgrndset(WINDOW *win, const cchar_t *wch)
    189 {
    190 	attr_t battr;
    191 	nschar_t *np, *tnp;
    192 	int i;
    193 
    194 #ifdef DEBUG
    195 	__CTRACE(__CTRACE_ATTR, "wbkgrndset: (%p), '%s', %x\n",
    196 		win, (const char *) wunctrl(wch), wch->attributes);
    197 #endif
    198 
    199 	/* ignore multi-column characters */
    200 	if (!wch->elements || wcwidth(wch->vals[0]) > 1)
    201 		return;
    202 
    203 	/* Background character. */
    204 	tnp = np = win->bnsp;
    205 	if (wcwidth( wch->vals[0]))
    206 		win->bch = wch->vals[0];
    207 	else {
    208 		if (!np) {
    209 			np = malloc(sizeof(nschar_t));
    210 			if (!np)
    211 				return;
    212 			np->next = NULL;
    213 			win->bnsp = np;
    214 		}
    215 		np->ch = wch->vals[0];
    216 		tnp = np;
    217 		np = np->next;
    218 	}
    219 	/* add non-spacing characters */
    220 	if (wch->elements > 1) {
    221 		for (i = 1; i < wch->elements; i++) {
    222 			if ( !np ) {
    223 				np = malloc(sizeof(nschar_t));
    224 				if (!np)
    225 					return;
    226 				np->next = NULL;
    227 				if (tnp)
    228 					tnp->next = np;
    229 				else
    230 					win->bnsp = np;
    231 			}
    232 			np->ch = wch->vals[i];
    233 			tnp = np;
    234 			np = np->next;
    235 		}
    236 	}
    237 	/* clear the old non-spacing characters */
    238 	while (np) {
    239 		tnp = np->next;
    240 		free(np);
    241 		np = tnp;
    242 	}
    243 
    244 	/* Background attributes (check colour). */
    245 	battr = wch->attributes & WA_ATTRIBUTES;
    246 	if (__using_color && !( battr & __COLOR))
    247 		battr |= __default_color;
    248 	win->battr = battr;
    249 	SET_BGWCOL((*win), 1);
    250 }
    251 
    252 
    253 int
    254 wgetbkgrnd(WINDOW *win, cchar_t *wch)
    255 {
    256 	nschar_t *np;
    257 
    258 	/* Background attributes (check colour). */
    259 	wch->attributes = win->battr & WA_ATTRIBUTES;
    260 	if (__using_color && ((wch->attributes & __COLOR)
    261 			== __default_color))
    262 		wch->attributes &= ~__default_color;
    263 	wch->vals[0] = win->bch;
    264 	wch->elements = 1;
    265 	np = win->bnsp;
    266 	if (np) {
    267 		while (np && wch->elements < CURSES_CCHAR_MAX) {
    268 			wch->vals[wch->elements++] = np->ch;
    269 			np = np->next;
    270 		}
    271 	}
    272 
    273 	return OK;
    274 }
    275 
    276 #else  /* !HAVE_WCHAR */
    277 
    278 int
    279 bkgrnd(const cchar_t *wch)
    280 {
    281 	return ERR;
    282 }
    283 
    284 
    285 void
    286 bkgrndset(const cchar_t *wch)
    287 {
    288 	return;
    289 }
    290 
    291 int
    292 getbkgrnd(cchar_t *wch)
    293 {
    294 	return ERR;
    295 }
    296 
    297 
    298 int
    299 wbkgrnd(WINDOW *win, const cchar_t *wch)
    300 {
    301 	return ERR;
    302 }
    303 
    304 
    305 void
    306 wbkgrndset(WINDOW *win, const cchar_t *wch)
    307 {
    308 	return;
    309 }
    310 
    311 
    312 int
    313 wgetbkgrnd(WINDOW *win, cchar_t *wch)
    314 {
    315 	return ERR;
    316 }
    317 
    318 #endif /* !HAVE_WCHAR */
    319