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