xref: /openbsd-src/lib/libcurses/base/lib_box.c (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1 /* $OpenBSD: lib_box.c,v 1.5 2023/10/17 09:52:08 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2020 Thomas E. Dickey                                          *
5  * Copyright 1998-2010,2017 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
34  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
35  *     and: Thomas E. Dickey                        1996-on                 *
36  *     and: Sven Verdoolaege                        2001                    *
37  ****************************************************************************/
38 
39 /*
40 **	lib_box.c
41 **
42 **	The routine wborder().
43 **
44 */
45 
46 #include <curses.priv.h>
47 
48 MODULE_ID("$Id: lib_box.c,v 1.5 2023/10/17 09:52:08 nicm Exp $")
49 
50 #if USE_WIDEC_SUPPORT
51 static NCURSES_INLINE chtype
_my_render(WINDOW * win,chtype ch)52 _my_render(WINDOW *win, chtype ch)
53 {
54     NCURSES_CH_T wch;
55     SetChar2(wch, ch);
56     wch = _nc_render(win, wch);
57     return ((attr_t) CharOf(wch)) | AttrOf(wch);
58 }
59 
60 #define RENDER_WITH_DEFAULT(ch,def) w ## ch = _my_render(win, (ch == 0) ? def : ch)
61 #else
62 #define RENDER_WITH_DEFAULT(ch,def) w ## ch = _nc_render(win, (ch == 0) ? def : ch)
63 #endif
64 
65 NCURSES_EXPORT(int)
wborder(WINDOW * win,chtype ls,chtype rs,chtype ts,chtype bs,chtype tl,chtype tr,chtype bl,chtype br)66 wborder(WINDOW *win,
67 	chtype ls, chtype rs,
68 	chtype ts, chtype bs,
69 	chtype tl, chtype tr,
70 	chtype bl, chtype br)
71 {
72     NCURSES_SIZE_T i;
73     NCURSES_SIZE_T endx, endy;
74     chtype wls, wrs, wts, wbs, wtl, wtr, wbl, wbr;
75 
76     T((T_CALLED("wborder(%p,%s,%s,%s,%s,%s,%s,%s,%s)"),
77        (void *) win,
78        _tracechtype2(1, ls),
79        _tracechtype2(2, rs),
80        _tracechtype2(3, ts),
81        _tracechtype2(4, bs),
82        _tracechtype2(5, tl),
83        _tracechtype2(6, tr),
84        _tracechtype2(7, bl),
85        _tracechtype2(8, br)));
86 
87     if (!win)
88 	returnCode(ERR);
89 
90     RENDER_WITH_DEFAULT(ls, ACS_VLINE);
91     RENDER_WITH_DEFAULT(rs, ACS_VLINE);
92     RENDER_WITH_DEFAULT(ts, ACS_HLINE);
93     RENDER_WITH_DEFAULT(bs, ACS_HLINE);
94     RENDER_WITH_DEFAULT(tl, ACS_ULCORNER);
95     RENDER_WITH_DEFAULT(tr, ACS_URCORNER);
96     RENDER_WITH_DEFAULT(bl, ACS_LLCORNER);
97     RENDER_WITH_DEFAULT(br, ACS_LRCORNER);
98 
99     T(("using %s, %s, %s, %s, %s, %s, %s, %s",
100        _tracechtype2(1, wls),
101        _tracechtype2(2, wrs),
102        _tracechtype2(3, wts),
103        _tracechtype2(4, wbs),
104        _tracechtype2(5, wtl),
105        _tracechtype2(6, wtr),
106        _tracechtype2(7, wbl),
107        _tracechtype2(8, wbr)));
108 
109     endx = win->_maxx;
110     endy = win->_maxy;
111 
112     for (i = 0; i <= endx; i++) {
113 	SetChar2(win->_line[0].text[i], wts);
114 	SetChar2(win->_line[endy].text[i], wbs);
115     }
116     win->_line[endy].firstchar = win->_line[0].firstchar = 0;
117     win->_line[endy].lastchar = win->_line[0].lastchar = endx;
118 
119     for (i = 0; i <= endy; i++) {
120 #if USE_WIDEC_SUPPORT
121 	if (endx > 0 && isWidecExt(win->_line[i].text[endx])) {
122 	    SetChar2(win->_line[i].text[endx - 1], ' ');
123 	}
124 #endif
125 	SetChar2(win->_line[i].text[0], wls);
126 	SetChar2(win->_line[i].text[endx], wrs);
127 	win->_line[i].firstchar = 0;
128 	win->_line[i].lastchar = endx;
129 #if USE_WIDEC_SUPPORT
130 	if (isWidecExt(win->_line[i].text[1])) {
131 	    SetChar2(win->_line[i].text[1], ' ');
132 	}
133 #endif
134     }
135     SetChar2(win->_line[0].text[0], wtl);
136     SetChar2(win->_line[0].text[endx], wtr);
137     SetChar2(win->_line[endy].text[0], wbl);
138     SetChar2(win->_line[endy].text[endx], wbr);
139 
140     _nc_synchook(win);
141     returnCode(OK);
142 }
143