background.c revision 1.23 1 /* $NetBSD: background.c,v 1.23 2018/11/18 22:34:32 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.23 2018/11/18 22:34:32 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 &= ~__COLOR;
133
134 return ((chtype) ((win->bch & A_CHARTEXT) | battr));
135 }
136
137
138 #ifdef HAVE_WCHAR
139
140 void
141 bkgrndset(const cchar_t *wch)
142 {
143 wbkgrndset(stdscr, wch);
144 }
145
146
147 int
148 bkgrnd(const cchar_t *wch)
149 {
150 return wbkgrnd(stdscr, wch);
151 }
152
153
154 int
155 getbkgrnd(cchar_t *wch)
156 {
157 return wgetbkgrnd(stdscr, wch);
158 }
159
160
161 void
162 wbkgrndset(WINDOW *win, const cchar_t *wch)
163 {
164 attr_t battr;
165 nschar_t *np, *tnp;
166 int i;
167
168 #ifdef DEBUG
169 __CTRACE(__CTRACE_ATTR, "wbkgrndset: (%p), '%s', %x\n",
170 win, (const char *) wunctrl(wch), wch->attributes);
171 #endif
172
173 /* ignore multi-column characters */
174 if (!wch->elements || wcwidth(wch->vals[0]) > 1)
175 return;
176
177 /* Background character. */
178 tnp = np = win->bnsp;
179 if (wcwidth( wch->vals[0]))
180 win->bch = wch->vals[0];
181 else {
182 if (!np) {
183 np = malloc(sizeof(nschar_t));
184 if (!np)
185 return;
186 np->next = NULL;
187 win->bnsp = np;
188 }
189 np->ch = wch->vals[0];
190 tnp = np;
191 np = np->next;
192 }
193 /* add non-spacing characters */
194 if (wch->elements > 1) {
195 for (i = 1; i < wch->elements; i++) {
196 if ( !np ) {
197 np = malloc(sizeof(nschar_t));
198 if (!np)
199 return;
200 np->next = NULL;
201 if (tnp)
202 tnp->next = np;
203 else
204 win->bnsp = np;
205 }
206 np->ch = wch->vals[i];
207 tnp = np;
208 np = np->next;
209 }
210 }
211 /* clear the old non-spacing characters */
212 while (np) {
213 tnp = np->next;
214 free(np);
215 np = tnp;
216 }
217
218 /* Background attributes (check colour). */
219 battr = wch->attributes & WA_ATTRIBUTES;
220 if (__using_color && !( battr & __COLOR))
221 battr |= __default_color;
222 win->battr = battr;
223 SET_BGWCOL((*win), 1);
224 }
225
226
227 int
228 wbkgrnd(WINDOW *win, const cchar_t *wch)
229 {
230 attr_t battr;
231
232 #ifdef DEBUG
233 __CTRACE(__CTRACE_ATTR, "wbkgrnd: (%p), '%s', %x\n",
234 win, (const char *) wunctrl(wch), wch->attributes);
235 #endif
236
237 /* ignore multi-column characters */
238 if (!wch->elements || wcwidth( wch->vals[ 0 ]) > 1)
239 return ERR;
240
241 /* Background attributes (check colour). */
242 battr = wch->attributes & WA_ATTRIBUTES;
243 if (__using_color && !( battr & __COLOR))
244 battr |= __default_color;
245
246 win->battr = battr;
247 wbkgrndset(win, wch);
248 __touchwin(win);
249 return OK;
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) == __default_color))
261 wch->attributes &= ~__COLOR;
262 wch->vals[0] = win->bch;
263 wch->elements = 1;
264 np = win->bnsp;
265 if (np) {
266 while (np && wch->elements < CURSES_CCHAR_MAX) {
267 wch->vals[wch->elements++] = np->ch;
268 np = np->next;
269 }
270 }
271
272 return OK;
273 }
274
275 #else /* !HAVE_WCHAR */
276
277 void
278 bkgrndset(const cchar_t *wch)
279 {
280 return;
281 }
282
283 int
284 bkgrnd(const cchar_t *wch)
285 {
286 return ERR;
287 }
288
289
290 int
291 getbkgrnd(cchar_t *wch)
292 {
293 return ERR;
294 }
295
296
297 void
298 wbkgrndset(WINDOW *win, const cchar_t *wch)
299 {
300 return;
301 }
302
303
304 int
305 wbkgrnd(WINDOW *win, const cchar_t *wch)
306 {
307 return ERR;
308 }
309
310
311 int
312 wgetbkgrnd(WINDOW *win, cchar_t *wch)
313 {
314 return ERR;
315 }
316
317 #endif /* !HAVE_WCHAR */
318