xref: /netbsd/lib/libcurses/border.c (revision b496d403)
1 /*	$NetBSD: border.c,v 1.24 2022/05/03 07:25:34 blymn 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: border.c,v 1.24 2022/05/03 07:25:34 blymn Exp $");
35 #endif				/* not lint */
36 
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "curses.h"
41 #include "curses_private.h"
42 
43 #ifndef _CURSES_USE_MACROS
44 
45 /*
46  * border --
47  *	Draw a border around stdscr using the specified
48  *	delimiting characters.
49  */
50 int
border(chtype left,chtype right,chtype top,chtype bottom,chtype topleft,chtype topright,chtype botleft,chtype botright)51 border(chtype left, chtype right, chtype top, chtype bottom, chtype topleft,
52        chtype topright, chtype botleft, chtype botright)
53 {
54 	return wborder(stdscr, left, right, top, bottom, topleft, topright,
55 	    botleft, botright);
56 }
57 
58 #endif
59 
60 /*
61  * wborder --
62  *	Draw a border around the given window using the specified delimiting
63  *	characters.
64  */
65 int
wborder(WINDOW * win,chtype left,chtype right,chtype top,chtype bottom,chtype topleft,chtype topright,chtype botleft,chtype botright)66 wborder(WINDOW *win, chtype left, chtype right, chtype top, chtype bottom,
67 	chtype topleft, chtype topright, chtype botleft, chtype botright)
68 {
69 #ifndef HAVE_WCHAR
70 	int	 endy, endx, i;
71 	__LDATA	*fp, *lp;
72 
73 	if (!(left & __CHARTEXT))
74 		left |= ACS_VLINE;
75 	if (!(right & __CHARTEXT))
76 		right |= ACS_VLINE;
77 	if (!(top & __CHARTEXT))
78 		top |= ACS_HLINE;
79 	if (!(bottom & __CHARTEXT))
80 		bottom |= ACS_HLINE;
81 	if (!(topleft & __CHARTEXT))
82 		topleft |= ACS_ULCORNER;
83 	if (!(topright & __CHARTEXT))
84 		topright |= ACS_URCORNER;
85 	if (!(botleft & __CHARTEXT))
86 		botleft |= ACS_LLCORNER;
87 	if (!(botright & __CHARTEXT))
88 		botright |= ACS_LRCORNER;
89 
90 	__CTRACE(__CTRACE_INPUT, "wborder: window 0x%p\n", win);
91 	__CTRACE(__CTRACE_INPUT, "wborder: left = %c, 0x%x\n",
92 	    left & __CHARTEXT, left & __ATTRIBUTES);
93 	__CTRACE(__CTRACE_INPUT, "wborder: right = %c, 0x%x\n",
94 	    right & __CHARTEXT, right & __ATTRIBUTES);
95 	__CTRACE(__CTRACE_INPUT, "wborder: top = %c, 0x%x\n",
96 	    top & __CHARTEXT, top & __ATTRIBUTES);
97 	__CTRACE(__CTRACE_INPUT, "wborder: bottom = %c, 0x%x\n",
98 	    bottom & __CHARTEXT, bottom & __ATTRIBUTES);
99 	__CTRACE(__CTRACE_INPUT, "wborder: topleft = %c, 0x%x\n",
100 	    topleft & __CHARTEXT, topleft & __ATTRIBUTES);
101 	__CTRACE(__CTRACE_INPUT, "wborder: topright = %c, 0x%x\n",
102 	    topright & __CHARTEXT, topright & __ATTRIBUTES);
103 	__CTRACE(__CTRACE_INPUT, "wborder: botleft = %c, 0x%x\n",
104 	    botleft & __CHARTEXT, botleft & __ATTRIBUTES);
105 	__CTRACE(__CTRACE_INPUT, "wborder: botright = %c, 0x%x\n",
106 	    botright & __CHARTEXT, botright & __ATTRIBUTES);
107 
108 	/* Merge window and background attributes */
109 	left |= (left & __COLOR) ? (win->wattr & ~__COLOR) : win->wattr;
110 	left |= (left & __COLOR) ? (win->battr & ~__COLOR) : win->battr;
111 	right |= (right & __COLOR) ? (win->wattr & ~__COLOR) : win->wattr;
112 	right |= (right & __COLOR) ? (win->battr & ~__COLOR) : win->battr;
113 	top |= (top & __COLOR) ? (win->wattr & ~__COLOR) : win->wattr;
114 	top |= (top & __COLOR) ? (win->battr & ~__COLOR) : win->battr;
115 	bottom |= (bottom & __COLOR) ? (win->wattr & ~__COLOR) : win->wattr;
116 	bottom |= (bottom & __COLOR) ? (win->battr & ~__COLOR) : win->battr;
117 	topleft |= (topleft & __COLOR) ? (win->wattr & ~__COLOR) : win->wattr;
118 	topleft |= (topleft & __COLOR) ? (win->battr & ~__COLOR) : win->battr;
119 	topright |= (topright & __COLOR) ? (win->wattr & ~__COLOR) : win->wattr;
120 	topright |= (topright & __COLOR) ? (win->battr & ~__COLOR) : win->battr;
121 	botleft |= (botleft & __COLOR) ? (win->wattr & ~__COLOR) : win->wattr;
122 	botleft |= (botleft & __COLOR) ? (win->battr & ~__COLOR) : win->battr;
123 	botright |= (botright & __COLOR) ? (win->wattr & ~__COLOR) : win->wattr;
124 	botright |= (botright & __COLOR) ? (win->battr & ~__COLOR) : win->battr;
125 
126 	endx = win->maxx - 1;
127 	endy = win->maxy - 1;
128 	fp = win->alines[0]->line;
129 	lp = win->alines[endy]->line;
130 
131 	/* Sides */
132 	for (i = 1; i < endy; i++) {
133 		win->alines[i]->line[0].ch = (wchar_t) left & __CHARTEXT;
134 		win->alines[i]->line[0].attr = (attr_t) left & __ATTRIBUTES;
135 		win->alines[i]->line[endx].ch = (wchar_t) right & __CHARTEXT;
136 		win->alines[i]->line[endx].attr = (attr_t) right & __ATTRIBUTES;
137 	}
138 	for (i = 1; i < endx; i++) {
139 		fp[i].ch = (wchar_t) top & __CHARTEXT;
140 		fp[i].attr = (attr_t) top & __ATTRIBUTES;
141 		lp[i].ch = (wchar_t) bottom & __CHARTEXT;
142 		lp[i].attr = (attr_t) bottom & __ATTRIBUTES;
143 	}
144 
145 	/* Corners */
146 	if (!(win->maxy == LINES && win->maxx == COLS &&
147 	    (win->flags & __SCROLLOK) && (win->flags & __SCROLLWIN))) {
148 		fp[0].ch = (wchar_t) topleft & __CHARTEXT;
149 		fp[0].attr = (attr_t) topleft & __ATTRIBUTES;
150 		fp[endx].ch = (wchar_t) topright & __CHARTEXT;
151 		fp[endx].attr = (attr_t) topright & __ATTRIBUTES;
152 		lp[0].ch = (wchar_t) botleft & __CHARTEXT;
153 		lp[0].attr = (attr_t) botleft & __ATTRIBUTES;
154 		lp[endx].ch = (wchar_t) botright & __CHARTEXT;
155 		lp[endx].attr = (attr_t) botright & __ATTRIBUTES;
156 	}
157 	__touchwin(win, 0);
158 	return OK;
159 #else /* HAVE_WCHAR */
160 	cchar_t ls, rs, ts, bs, tl, tr, bl, br;
161 	cchar_t *lsp, *rsp, *tsp, *bsp, *tlp, *trp, *blp, *brp;
162 
163 #define S(in, out, def) \
164 	if (in & __CHARTEXT) { \
165 		__cursesi_chtype_to_cchar(in, &out); \
166 	} else { \
167 		memcpy(&out, def, sizeof(cchar_t)); \
168 		out.attributes |= in & __ATTRIBUTES; \
169 	} \
170 	out##p = &out;
171 
172 	S(left, ls, WACS_VLINE);
173 	S(right, rs, WACS_VLINE);
174 	S(top, ts, WACS_HLINE);
175 	S(bottom, bs, WACS_HLINE);
176 	S(topleft, tl, WACS_ULCORNER);
177 	S(topright, tr, WACS_URCORNER);
178 	S(botleft, bl, WACS_LLCORNER);
179 	S(botright, br, WACS_LRCORNER);
180 #undef S
181 	return wborder_set(win, lsp, rsp, tsp, bsp, tlp, trp, blp, brp);
182 #endif /* HAVE_WCHAR */
183 }
184 
border_set(const cchar_t * ls,const cchar_t * rs,const cchar_t * ts,const cchar_t * bs,const cchar_t * tl,const cchar_t * tr,const cchar_t * bl,const cchar_t * br)185 int border_set(const cchar_t *ls, const cchar_t *rs, const cchar_t *ts,
186 	   const cchar_t *bs, const cchar_t *tl, const cchar_t *tr,
187 	   const cchar_t *bl, const cchar_t *br)
188 {
189 #ifndef HAVE_WCHAR
190 	return ERR;
191 #else
192 	return wborder_set(stdscr, ls, rs, ts, bs, tl, tr, bl, br);
193 #endif /* HAVE_WCHAR */
194 }
195 
wborder_set(WINDOW * win,const cchar_t * ls,const cchar_t * rs,const cchar_t * ts,const cchar_t * bs,const cchar_t * tl,const cchar_t * tr,const cchar_t * bl,const cchar_t * br)196 int wborder_set(WINDOW *win, const cchar_t *ls, const cchar_t *rs,
197 		const cchar_t *ts, const cchar_t *bs,
198 		const cchar_t *tl, const cchar_t *tr,
199 		const cchar_t *bl, const cchar_t *br)
200 {
201 #ifndef HAVE_WCHAR
202 	return ERR;
203 #else
204 	int	 endy, endx, i, j, k, cw, pcw, tlcw, blcw, trcw, brcw;
205 	cchar_t left, right, bottom, top, topleft, topright, botleft, botright;
206 	nschar_t *np, *tnp;
207 
208 	if (ls && wcwidth(ls->vals[0]))
209 		memcpy(&left, ls, sizeof(cchar_t));
210 	else
211 		memcpy(&left, WACS_VLINE, sizeof(cchar_t));
212 	if (rs && wcwidth( rs->vals[0]))
213 		memcpy(&right, rs, sizeof(cchar_t));
214 	else
215 		memcpy(&right, WACS_VLINE, sizeof(cchar_t));
216 	if (ts && wcwidth( ts->vals[0]))
217 		memcpy(&top, ts, sizeof(cchar_t));
218 	else
219 		memcpy( &top, WACS_HLINE, sizeof(cchar_t));
220 	if (bs && wcwidth( bs->vals[0]))
221 		memcpy(&bottom, bs, sizeof(cchar_t));
222 	else
223 		memcpy(&bottom, WACS_HLINE, sizeof(cchar_t));
224 	if (tl && wcwidth(tl->vals[0]))
225 		memcpy( &topleft, tl, sizeof(cchar_t));
226 	else
227 		memcpy(&topleft, WACS_ULCORNER, sizeof(cchar_t));
228 	if (tr && wcwidth( tr->vals[0]))
229 		memcpy(&topright, tr, sizeof(cchar_t));
230 	else
231 		memcpy(&topright, WACS_URCORNER, sizeof( cchar_t ));
232 	if (bl && wcwidth( bl->vals[0]))
233 		memcpy(&botleft, bl, sizeof(cchar_t));
234 	else
235 		memcpy(&botleft, WACS_LLCORNER, sizeof(cchar_t));
236 	if (br && wcwidth( br->vals[0]))
237 		memcpy(&botright, br, sizeof(cchar_t));
238 	else
239 		memcpy(&botright, WACS_LRCORNER, sizeof(cchar_t));
240 
241 	__CTRACE(__CTRACE_INPUT, "wborder_set: window 0x%p\n", win);
242 	__CTRACE(__CTRACE_INPUT, "wborder_set: left = %c, 0x%x\n",
243 	    left.vals[0], left.attributes );
244 	__CTRACE(__CTRACE_INPUT, "wborder_set: right = %c, 0x%x\n",
245 	    right.vals[0], right.attributes );
246 	__CTRACE(__CTRACE_INPUT, "wborder_set: top = %c, 0x%x\n",
247 	    top.vals[0], top.attributes );
248 	__CTRACE(__CTRACE_INPUT, "wborder_set: bottom = %c, 0x%x\n",
249 	    bottom.vals[0], bottom.attributes );
250 	__CTRACE(__CTRACE_INPUT, "wborder_set: topleft = %c, 0x%x\n",
251 	    topleft.vals[0], topleft.attributes );
252 	__CTRACE(__CTRACE_INPUT, "wborder_set: topright = %c, 0x%x\n",
253 	    topright.vals[0], topright.attributes );
254 	__CTRACE(__CTRACE_INPUT, "wborder_set: botleft = %c, 0x%x\n",
255 	    botleft.vals[0], botleft.attributes );
256 	__CTRACE(__CTRACE_INPUT, "wborder_set: botright = %c, 0x%x\n",
257 	    botright.vals[0], botright.attributes );
258 
259 	/* Merge window attributes */
260 	left.attributes |= (left.attributes & __COLOR) ?
261 		(win->wattr & ~__COLOR) : win->wattr;
262 	left.attributes |= (left.attributes & __COLOR) ?
263 		(win->battr & ~__COLOR) : win->battr;
264 	right.attributes |= (right.attributes & __COLOR) ?
265 		(win->wattr & ~__COLOR) : win->wattr;
266 	right.attributes |= (right.attributes & __COLOR) ?
267 		(win->battr & ~__COLOR) : win->battr;
268 	top.attributes |= (top.attributes & __COLOR) ?
269 		(win->wattr & ~__COLOR) : win->wattr;
270 	top.attributes |= (top.attributes & __COLOR) ?
271 		(win->battr & ~__COLOR) : win->battr;
272 	bottom.attributes |= (bottom.attributes & __COLOR) ?
273 		(win->wattr & ~__COLOR) : win->wattr;
274 	bottom.attributes |= (bottom.attributes & __COLOR) ?
275 		(win->battr & ~__COLOR) : win->battr;
276 	topleft.attributes |= (topleft.attributes & __COLOR) ?
277 		(win->wattr & ~__COLOR) : win->wattr;
278 	topleft.attributes |= (topleft.attributes & __COLOR) ?
279 		(win->battr & ~__COLOR) : win->battr;
280 	topright.attributes |= (topright.attributes & __COLOR) ?
281 		(win->wattr & ~__COLOR) : win->wattr;
282 	topright.attributes |= (topright.attributes & __COLOR) ?
283 		(win->battr & ~__COLOR) : win->battr;
284 	botleft.attributes |= (botleft.attributes & __COLOR) ?
285 		(win->wattr & ~__COLOR) : win->wattr;
286 	botleft.attributes |= (botleft.attributes & __COLOR) ?
287 		(win->battr & ~__COLOR) : win->battr;
288 	botright.attributes |= (botright.attributes & __COLOR) ?
289 		(win->wattr & ~__COLOR) : win->wattr;
290 	botright.attributes |= (botright.attributes & __COLOR) ?
291 		(win->battr & ~__COLOR) : win->battr;
292 
293 	endx = win->maxx - 1;
294 	endy = win->maxy - 1;
295 
296 	/* Sides */
297 	for (i = 1; i < endy; i++) {
298 		/* left border */
299 		cw = wcwidth(left.vals[0]);
300 		if (cw < 0)
301 			cw = 1;
302 		for ( j = 0; j < cw; j++ ) {
303 			win->alines[i]->line[j].ch = left.vals[0];
304 			win->alines[i]->line[j].cflags &= ~CA_BACKGROUND;
305 			win->alines[i]->line[j].attr = left.attributes;
306 			np = win->alines[i]->line[j].nsp;
307 			if (np) {
308 				while (np) {
309 					tnp = np->next;
310 					free(np);
311 					np = tnp;
312 				}
313 				win->alines[i]->line[j].nsp = NULL;
314 			}
315 			if (j)
316 				win->alines[i]->line[j].wcols = -j;
317 			else {
318 				win->alines[i]->line[j].wcols = cw;
319 				if (left.elements > 1) {
320 					for (k = 1; k < left.elements; k++) {
321 						np = malloc(sizeof(nschar_t));
322 						if (!np)
323 							return ERR;
324 						np->ch = left.vals[ k ];
325 						np->next = win->alines[i]->line[j].nsp;
326 						win->alines[i]->line[j].nsp
327 							= np;
328 					}
329 				}
330 			}
331 		}
332 		for (j = cw; win->alines[i]->line[j].wcols < 0; j++) {
333 			__CTRACE(__CTRACE_INPUT,
334 			    "wborder_set: clean out partial char[%d]", j);
335 			win->alines[i]->line[j].ch = win->bch;
336 			win->alines[i]->line[j].cflags |= CA_BACKGROUND;
337 			if (_cursesi_copy_nsp(win->bnsp,
338 					      &win->alines[i]->line[j]) == ERR)
339 				return ERR;
340 			win->alines[i]->line[j].wcols = 1;
341 		}
342 		/* right border */
343 		cw = wcwidth(right.vals[0]);
344 		if (cw < 0)
345 			cw = 1;
346 		pcw = win->alines[i]->line[endx - cw].wcols;
347 		for ( j = endx - cw + 1; j <= endx; j++ ) {
348 			win->alines[i]->line[j].ch = right.vals[0];
349 			win->alines[i]->line[j].cflags &= ~CA_BACKGROUND;
350 			win->alines[i]->line[j].attr = right.attributes;
351 			np = win->alines[i]->line[j].nsp;
352 			if (np) {
353 				while (np) {
354 					tnp = np->next;
355 					free(np);
356 					np = tnp;
357 				}
358 				win->alines[i]->line[j].nsp = NULL;
359 			}
360 			if (j == endx - cw + 1) {
361 				win->alines[i]->line[j].wcols = cw;
362 				if (right.elements > 1) {
363 					for (k = 1; k < right.elements; k++) {
364 						np = malloc(sizeof(nschar_t));
365 						if (!np)
366 							return ERR;
367 						np->ch = right.vals[ k ];
368 						np->next = win->alines[i]->line[j].nsp;
369 						win->alines[i]->line[j].nsp
370 							= np;
371 					}
372 				}
373 			} else
374 				win->alines[i]->line[j].wcols =
375 				    endx - cw + 1 - j;
376 		}
377 		if (pcw != 1) {
378 			__CTRACE(__CTRACE_INPUT,
379 			    "wborder_set: clean out partial chars[%d:%d]",
380 			    endx - cw + pcw, endx - cw);
381 			k = pcw < 0 ? endx -cw + pcw : endx - cw;
382 			for (j = endx - cw; j >= k; j--) {
383 				win->alines[i]->line[j].ch = win->bch;
384 				win->alines[i]->line[j].cflags |= CA_BACKGROUND;
385 				if (_cursesi_copy_nsp(win->bnsp,
386 					       &win->alines[i]->line[j]) == ERR)
387 					return ERR;
388 				win->alines[i]->line[j].attr = win->battr;
389 				win->alines[i]->line[j].wcols = 1;
390 			}
391 		}
392 	}
393 	tlcw = wcwidth(topleft.vals[0]);
394 	if (tlcw < 0)
395 		tlcw = 1;
396 	blcw = wcwidth(botleft.vals[0]);
397 	if (blcw < 0)
398 		blcw = 1;
399 	trcw = wcwidth(topright.vals[0]);
400 	if (trcw < 0)
401 		trcw = 1;
402 	brcw = wcwidth(botright.vals[0]);
403 	if (brcw < 0)
404 		brcw = 1;
405 	/* upper border */
406 	cw = wcwidth(top.vals[0]);
407 	if (cw < 0)
408 		cw = 1;
409 	for (i = tlcw; i <= min( endx - cw, endx - trcw); i += cw) {
410 		for (j = 0; j < cw; j++) {
411 			win->alines[0]->line[i + j].ch = top.vals[0];
412 			win->alines[0]->line[i + j].cflags &= ~CA_BACKGROUND;
413 			win->alines[0]->line[i + j].attr = top.attributes;
414 			np = win->alines[0]->line[i + j].nsp;
415 			if (np) {
416 				while (np) {
417 					tnp = np->next;
418 					free(np);
419 					np = tnp;
420 				}
421 				win->alines[0]->line[i + j].nsp = NULL;
422 			}
423 			if (j)
424 				win->alines[ 0 ]->line[ i + j ].wcols = -j;
425 			else {
426 				win->alines[ 0 ]->line[ i + j ].wcols = cw;
427 				if ( top.elements > 1 ) {
428 					for (k = 1; k < top.elements; k++) {
429 						np = malloc(sizeof(nschar_t));
430 						if (!np)
431 							return ERR;
432 						np->ch = top.vals[k];
433 						np->next = win->alines[0]->line[i + j].nsp;
434 						win->alines[0]->line[i + j].nsp
435 							= np;
436 					}
437 				}
438 			}
439 		}
440 	}
441 	while (i <= endx - trcw) {
442 		win->alines[0]->line[i].ch = win->bch;
443 		win->alines[0]->line[i].cflags |= CA_BACKGROUND;
444 		if (_cursesi_copy_nsp(win->bnsp,
445 				      &win->alines[0]->line[i]) == ERR)
446 			return ERR;
447 		win->alines[0]->line[i].attr = win->battr;
448 		win->alines[0]->line[i].wcols = 1;
449 		i++;
450 	}
451 	/* lower border */
452 	for (i = blcw; i <= min( endx - cw, endx - brcw); i += cw) {
453 		for (j = 0; j < cw; j++) {
454 			win->alines[endy]->line[i + j].ch = bottom.vals[0];
455 			win->alines[endy]->line[i + j].cflags &= ~CA_BACKGROUND;
456 			win->alines[endy]->line[i + j].attr = bottom.attributes;
457 			np = win->alines[endy]->line[i + j].nsp;
458 			if (np) {
459 				while (np) {
460 					tnp = np->next;
461 					free(np);
462 					np = tnp;
463 				}
464 				win->alines[endy]->line[i + j].nsp = NULL;
465 			}
466 			if (j)
467 				win->alines[endy]->line[i + j].wcols = -j;
468 			else {
469 				win->alines[endy]->line[i + j].wcols = cw;
470 				if (bottom.elements > 1) {
471 					for (k = 1; k < bottom.elements; k++) {
472 						np = malloc(sizeof(nschar_t));
473 						if (!np)
474 							return ERR;
475 						np->ch = bottom.vals[ k ];
476 						np->next = win->alines[endy]->line[i + j].nsp;
477 						win->alines[endy]->line[i + j].nsp = np;
478 					}
479 				}
480 			}
481 		}
482 	}
483 	while (i <= endx - brcw) {
484 		win->alines[endy]->line[i].ch = win->bch;
485 		win->alines[endy]->line[i].cflags |= CA_BACKGROUND;
486 		if (_cursesi_copy_nsp(win->bnsp,
487 				      &win->alines[endy]->line[i]) == ERR)
488 			return ERR;
489 		win->alines[endy]->line[i].attr = win->battr;
490 		win->alines[endy]->line[i].wcols = 1;
491 		i++;
492 	}
493 
494 	/* Corners */
495 	if (!(win->maxy == LINES && win->maxx == COLS &&
496 		(win->flags & __SCROLLOK) && (win->flags & __SCROLLWIN))) {
497 		for (i = 0; i < tlcw; i++) {
498 			win->alines[0]->line[i].ch = topleft.vals[0];
499 			win->alines[0]->line[i].cflags &= ~CA_BACKGROUND;
500 			win->alines[0]->line[i].attr = topleft.attributes;
501 			np = win->alines[0]->line[i].nsp;
502 			if (np) {
503 				while (np) {
504 					tnp = np->next;
505 					free(np);
506 					np = tnp;
507 				}
508 				win->alines[0]->line[i].nsp = NULL;
509 			}
510 			if (i)
511 				win->alines[0]->line[i].wcols = -i;
512 			else {
513 				win->alines[0]->line[i].wcols = tlcw;
514 				if (topleft.elements > 1) {
515 					for (k = 1; k < topleft.elements; k++)
516 					{
517 						np = malloc(sizeof(nschar_t));
518 						if (!np)
519 							return ERR;
520 						np->ch = topleft.vals[k];
521 						np->next = win->alines[0]->line[i].nsp;
522 						win->alines[0]->line[i].nsp = np;
523 					}
524 				}
525 			}
526 		}
527 		for (i = endx - trcw + 1; i <= endx; i++) {
528 			win->alines[0]->line[i].ch = topright.vals[0];
529 			win->alines[0]->line[i].cflags &= ~CA_BACKGROUND;
530 			win->alines[0]->line[i].attr = topright.attributes;
531 			np = win->alines[0]->line[i].nsp;
532 			if (np) {
533 				while (np) {
534 					tnp = np->next;
535 					free(np);
536 					np = tnp;
537 				}
538 				win->alines[0]->line[i].nsp = NULL;
539 			}
540 			if (i == endx - trcw + 1) {
541 				win->alines[0]->line[i].wcols = trcw;
542 				if (topright.elements > 1) {
543 					for (k = 1; k < topright.elements;k ++)
544 					{
545 						np = malloc(sizeof(nschar_t));
546 						if (!np)
547 							return ERR;
548 						np->ch = topright.vals[k];
549 						np->next = win->alines[0]->line[i].nsp;
550 						win->alines[ 0 ]->line[i].nsp = np;
551 					}
552 				}
553 			} else
554 				win->alines[0]->line[i].wcols =
555 				    endx - trcw + 1 - i;
556 		}
557 		for (i = 0; i < blcw; i++) {
558 			win->alines[endy]->line[i].ch = botleft.vals[0];
559 			win->alines[endy]->line[i].cflags &= ~CA_BACKGROUND;
560 			win->alines[endy]->line[i].attr = botleft.attributes;
561 			np = win->alines[ endy ]->line[i].nsp;
562 			if (np) {
563 				while (np) {
564 					tnp = np->next;
565 					free(np);
566 					np = tnp;
567 				}
568 				win->alines[endy]->line[i].nsp = NULL;
569 			}
570 			if (i)
571 				win->alines[endy]->line[i].wcols = -i;
572 			else {
573 				win->alines[endy]->line[i].wcols = blcw;
574 				if (botleft.elements > 1) {
575 					for (k = 1; k < botleft.elements; k++) {
576 						np = malloc(sizeof(nschar_t));
577 						if (!np)
578 							return ERR;
579 						np->ch = botleft.vals[ k ];
580 						np->next = win->alines[endy]->line[i].nsp;
581 						win->alines[endy]->line[i].nsp = np;
582 					}
583 				}
584 			}
585 		}
586 		for (i = endx - brcw + 1; i <= endx; i++) {
587 			win->alines[endy]->line[i].ch = botright.vals[0];
588 			win->alines[endy]->line[i].cflags &= ~CA_BACKGROUND;
589 			win->alines[endy]->line[i].attr = botright.attributes;
590 			np = win->alines[endy]->line[i].nsp;
591 			if (np) {
592 				while (np) {
593 					tnp = np->next;
594 					free(np);
595 					np = tnp;
596 				}
597 				win->alines[endy]->line[i].nsp = NULL;
598 			}
599 			if (i == endx - brcw + 1) {
600 				win->alines[endy]->line[i].wcols = brcw;
601 				if (botright.elements > 1) {
602 					for (k = 1; k < botright.elements; k++){
603 						np = malloc(sizeof(nschar_t));
604 						if (!np)
605 							return ERR;
606 						np->ch = botright.vals[k];
607 						np->next = win->alines[endy]->line[i].nsp;
608 						win->alines[endy]->line[i].nsp = np;
609 					}
610 				}
611 			} else
612 				win->alines[endy]->line[i].wcols =
613 				    endx - brcw + 1 - i;
614 		}
615 	}
616 	__touchwin(win, 0);
617 	return OK;
618 #endif /* HAVE_WCHAR */
619 }
620