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