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