add_wchstr.c revision 1.6 1 /* $NetBSD: add_wchstr.c,v 1.6 2018/11/22 22:16:45 uwe Exp $ */
2
3 /*
4 * Copyright (c) 2005 The NetBSD Foundation Inc.
5 * All rights reserved.
6 *
7 * This code is derived from code donated to the NetBSD Foundation
8 * by Ruibiao Qiu <ruibiao (at) arl.wustl.edu,ruibiao (at) gmail.com>.
9 *
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: add_wchstr.c,v 1.6 2018/11/22 22:16:45 uwe Exp $");
40 #endif /* not lint */
41
42 #include <stdlib.h>
43
44 #include "curses.h"
45 #include "curses_private.h"
46
47 /*
48 * add_wchstr --
49 * Add a wide string to stdscr starting at (_cury, _curx).
50 */
51 int
52 add_wchstr(const cchar_t *wchstr)
53 {
54 return wadd_wchnstr(stdscr, wchstr, -1);
55 }
56
57
58 /*
59 * wadd_wchstr --
60 * Add a string to the given window starting at (_cury, _curx).
61 */
62 int
63 wadd_wchstr(WINDOW *win, const cchar_t *wchstr)
64 {
65 return wadd_wchnstr(win, wchstr, -1);
66 }
67
68
69 /*
70 * add_wchnstr --
71 * Add a string (at most n characters) to stdscr starting
72 * at (_cury, _curx). If n is negative, add the entire string.
73 */
74 int
75 add_wchnstr(const cchar_t *wchstr, int n)
76 {
77 return wadd_wchnstr(stdscr, wchstr, n);
78 }
79
80
81 /*
82 * mvadd_wchstr --
83 * Add a string to stdscr starting at (y, x)
84 */
85 int
86 mvadd_wchstr(int y, int x, const cchar_t *wchstr)
87 {
88 return mvwadd_wchnstr(stdscr, y, x, wchstr, -1);
89 }
90
91
92 /*
93 * mvwadd_wchstr --
94 * Add a string to the given window starting at (y, x)
95 */
96 int
97 mvwadd_wchstr(WINDOW *win, int y, int x, const cchar_t *wchstr)
98 {
99 return mvwadd_wchnstr(win, y, x, wchstr, -1);
100 }
101
102
103 /*
104 * mvadd_wchnstr --
105 * Add a string of at most n characters to stdscr
106 * starting at (y, x).
107 */
108 int
109 mvadd_wchnstr(int y, int x, const cchar_t *wchstr, int n)
110 {
111 return mvwadd_wchnstr(stdscr, y, x, wchstr, n);
112 }
113
114
115 /*
116 * mvwadd_wchnstr --
117 * Add a string of at most n characters to the given window
118 * starting at (y, x).
119 */
120 int
121 mvwadd_wchnstr(WINDOW *win, int y, int x, const cchar_t *wchstr, int n)
122 {
123 if (wmove(win, y, x) == ERR)
124 return ERR;
125
126 return wadd_wchnstr(win, wchstr, n);
127 }
128
129
130 /*
131 * wadd_wchnstr --
132 * Add a string (at most n wide characters) to the given window
133 * starting at (_cury, _curx). If n is -1, add the entire string.
134 */
135 int
136 wadd_wchnstr(WINDOW *win, const cchar_t *wchstr, int n)
137 {
138 const cchar_t *chp;
139 wchar_t wc;
140 int cw, x, y, sx, ex, newx, i, cnt;
141 __LDATA *lp, *tp;
142 nschar_t *np, *tnp;
143 __LINE *lnp;
144
145 #ifdef DEBUG
146 __CTRACE(__CTRACE_INPUT,
147 "wadd_wchnstr: win = %p, wchstr = %p, n = %d\n", win, wchstr, n);
148 #endif
149
150 if (!wchstr)
151 return OK;
152
153 /* compute length of the cchar string */
154 if (n < -1)
155 return ERR;
156 if (n >= 0)
157 for (chp = wchstr, cnt = 0; n && chp->vals[0];
158 n--, chp++, ++cnt);
159 else
160 for (chp = wchstr, cnt = 0; chp->vals[0]; chp++, ++cnt);
161 #ifdef DEBUG
162 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: len=%d\n", cnt);
163 #endif /* DEBUG */
164 chp = wchstr;
165 x = win->curx;
166 y = win->cury;
167 lp = &win->alines[y]->line[x];
168 lnp = win->alines[y];
169
170 cw = WCOL(*lp);
171 if (cw >= 0) {
172 sx = x;
173 } else {
174 if (wcwidth(chp->vals[0])) {
175 /* clear the partial character before cursor */
176 for (tp = lp + cw; tp < lp; tp++) {
177 tp->ch = (wchar_t) btowc((int) win->bch);
178 if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
179 return ERR;
180 tp->attr = win->battr;
181 SET_WCOL(*tp, 1);
182 np = tp->nsp;
183 }
184 } else {
185 /* move to the start of current char */
186 lp += cw;
187 x += cw;
188 }
189 sx = x + cw;
190 }
191 lnp->flags |= __ISDIRTY;
192 newx = sx + win->ch_off;
193 if (newx < *lnp->firstchp)
194 *lnp->firstchp = newx;
195
196 /* add characters in the string */
197 ex = x;
198 while (cnt) {
199 x = ex;
200 wc = chp->vals[0];
201 #ifdef DEBUG
202 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: adding %x", wc);
203 #endif /* DEBUG */
204 cw = wcwidth(wc);
205 if (cw < 0)
206 cw = 1;
207 if (cw) {
208 /* spacing character */
209 #ifdef DEBUG
210 __CTRACE(__CTRACE_INPUT,
211 " as a spacing char(width=%d)\n", cw);
212 #endif /* DEBUG */
213 if (cw > win->maxx - ex) {
214 /* clear to EOL */
215 while (ex < win->maxx) {
216 lp->ch = (wchar_t)
217 btowc((int) win->bch);
218 if (_cursesi_copy_nsp(win->bnsp, lp)
219 == ERR)
220 return ERR;
221 lp->attr = win->battr;
222 SET_WCOL(*lp, 1);
223 lp++, ex++;
224 }
225 ex = win->maxx - 1;
226 break;
227 }
228 /* this could combine with the insertion of
229 * non-spacing char */
230 np = lp->nsp;
231 if (np) {
232 while (np) {
233 tnp = np->next;
234 free(np);
235 np = tnp;
236 }
237 lp->nsp = NULL;
238 }
239 lp->ch = chp->vals[0];
240 lp->attr = chp->attributes & WA_ATTRIBUTES;
241 SET_WCOL(*lp, cw);
242 if (chp->elements > 1) {
243 for (i = 1; i < chp->elements; i++) {
244 np = (nschar_t *)
245 malloc(sizeof(nschar_t));
246 if (!np)
247 return ERR;
248 np->ch = chp->vals[i];
249 np->next = lp->nsp;
250 lp->nsp = np;
251 }
252 }
253 lp++, ex++;
254 #ifdef DEBUG
255 __CTRACE(__CTRACE_INPUT,
256 "wadd_wchnstr: ex = %d, x = %d, cw = %d\n",
257 ex, x, cw);
258 #endif /* DEBUG */
259 while (ex - x <= cw - 1) {
260 np = lp->nsp;
261 if (np) {
262 while (np) {
263 tnp = np->next;
264 free(np);
265 np = tnp;
266 }
267 lp->nsp = NULL;
268 }
269 lp->ch = chp->vals[0];
270 lp->attr = chp->attributes & WA_ATTRIBUTES;
271 SET_WCOL(*lp, x - ex);
272 lp++, ex++;
273 }
274 } else {
275 /* non-spacing character */
276 #ifdef DEBUG
277 __CTRACE(__CTRACE_INPUT,
278 "wadd_wchnstr: as non-spacing char");
279 #endif /* DEBUG */
280 for (i = 0; i < chp->elements; i++) {
281 np = malloc(sizeof(nschar_t));
282 if (!np)
283 return ERR;
284 np->ch = chp->vals[i];
285 np->next = lp->nsp;
286 lp->nsp = np;
287 }
288 }
289 cnt--, chp++;
290 }
291 #ifdef DEBUG
292 for (i = sx; i < ex; i++) {
293 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: (%d,%d)=(%x,%x,%p)\n",
294 win->cury, i, win->alines[win->cury]->line[i].ch,
295 win->alines[win->cury]->line[i].attr,
296 win->alines[win->cury]->line[i].nsp);
297 }
298 #endif /* DEBUG */
299 lnp->flags |= __ISDIRTY;
300 newx = ex + win->ch_off;
301 if (newx > *lnp->lastchp)
302 *lnp->lastchp = newx;
303 __touchline(win, y, sx, ex);
304
305 return OK;
306 }
307