1 /* $NetBSD: add_wchstr.c,v 1.4 2010/02/23 19:48:26 drochner 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@arl.wustl.edu,ruibiao@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.4 2010/02/23 19:48:26 drochner 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 #ifndef HAVE_WCHAR 55 return ERR; 56 #else 57 return wadd_wchnstr(stdscr, wchstr, -1); 58 #endif 59 } 60 61 62 /* 63 * wadd_wchstr -- 64 * Add a string to the given window starting at (_cury, _curx). 65 */ 66 int 67 wadd_wchstr(WINDOW *win, const cchar_t *wchstr) 68 { 69 #ifndef HAVE_WCHAR 70 return ERR; 71 #else 72 return wadd_wchnstr(win, wchstr, -1); 73 #endif 74 } 75 76 77 /* 78 * add_wchnstr -- 79 * Add a string (at most n characters) to stdscr starting 80 * at (_cury, _curx). If n is negative, add the entire string. 81 */ 82 int 83 add_wchnstr(const cchar_t *wchstr, int n) 84 { 85 #ifndef HAVE_WCHAR 86 return ERR; 87 #else 88 return wadd_wchnstr(stdscr, wchstr, n); 89 #endif 90 } 91 92 93 /* 94 * mvadd_wchstr -- 95 * Add a string to stdscr starting at (y, x) 96 */ 97 int 98 mvadd_wchstr(int y, int x, const cchar_t *wchstr) 99 { 100 #ifndef HAVE_WCHAR 101 return ERR; 102 #else 103 return mvwadd_wchnstr(stdscr, y, x, wchstr, -1); 104 #endif 105 } 106 107 108 /* 109 * mvwadd_wchstr -- 110 * Add a string to the given window starting at (y, x) 111 */ 112 int 113 mvwadd_wchstr(WINDOW *win, int y, int x, const cchar_t *wchstr) 114 { 115 #ifndef HAVE_WCHAR 116 return ERR; 117 #else 118 return mvwadd_wchnstr(win, y, x, wchstr, -1); 119 #endif 120 } 121 122 123 /* 124 * mvadd_wchnstr -- 125 * Add a string of at most n characters to stdscr 126 * starting at (y, x). 127 */ 128 int 129 mvadd_wchnstr(int y, int x, const cchar_t *wchstr, int n) 130 { 131 #ifndef HAVE_WCHAR 132 return ERR; 133 #else 134 return mvwadd_wchnstr(stdscr, y, x, wchstr, n); 135 #endif 136 } 137 138 139 /* 140 * mvwadd_wchnstr -- 141 * Add a string of at most n characters to the given window 142 * starting at (y, x). 143 */ 144 int 145 mvwadd_wchnstr(WINDOW *win, int y, int x, const cchar_t *wchstr, int n) 146 { 147 #ifndef HAVE_WCHAR 148 return ERR; 149 #else 150 if (wmove(win, y, x) == ERR) 151 return ERR; 152 153 return wadd_wchnstr(win, wchstr, n); 154 #endif 155 } 156 157 158 /* 159 * wadd_wchnstr -- 160 * Add a string (at most n wide characters) to the given window 161 * starting at (_cury, _curx). If n is -1, add the entire string. 162 */ 163 int 164 wadd_wchnstr(WINDOW *win, const cchar_t *wchstr, int n) 165 { 166 #ifndef HAVE_WCHAR 167 return ERR; 168 #else 169 const cchar_t *chp; 170 wchar_t wc; 171 int cw, x, y, sx, ex, newx, i, cnt; 172 __LDATA *lp, *tp; 173 nschar_t *np, *tnp; 174 __LINE *lnp; 175 176 #ifdef DEBUG 177 __CTRACE(__CTRACE_INPUT, 178 "wadd_wchnstr: win = %p, wchstr = %p, n = %d\n", win, wchstr, n); 179 #endif 180 181 if (!wchstr) 182 return OK; 183 184 /* compute length of the cchar string */ 185 if (n < -1) 186 return ERR; 187 if (n >= 0) 188 for (chp = wchstr, cnt = 0; n && chp->vals[0]; 189 n--, chp++, ++cnt); 190 else 191 for (chp = wchstr, cnt = 0; chp->vals[0]; chp++, ++cnt); 192 #ifdef DEBUG 193 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: len=%d\n", cnt); 194 #endif /* DEBUG */ 195 chp = wchstr; 196 x = win->curx; 197 y = win->cury; 198 lp = &win->alines[y]->line[x]; 199 lnp = win->alines[y]; 200 201 cw = WCOL(*lp); 202 if (cw >= 0) { 203 sx = x; 204 } else { 205 if (wcwidth(chp->vals[0])) { 206 /* clear the partial character before cursor */ 207 for (tp = lp + cw; tp < lp; tp++) { 208 tp->ch = (wchar_t) btowc((int) win->bch); 209 if (_cursesi_copy_nsp(win->bnsp, tp) == ERR) 210 return ERR; 211 tp->attr = win->battr; 212 SET_WCOL(*tp, 1); 213 np = tp->nsp; 214 } 215 } else { 216 /* move to the start of current char */ 217 lp += cw; 218 x += cw; 219 } 220 sx = x + cw; 221 } 222 lnp->flags |= __ISDIRTY; 223 newx = sx + win->ch_off; 224 if (newx < *lnp->firstchp) 225 *lnp->firstchp = newx; 226 227 /* add characters in the string */ 228 ex = x; 229 while (cnt) { 230 x = ex; 231 wc = chp->vals[0]; 232 #ifdef DEBUG 233 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: adding %x", wc); 234 #endif /* DEBUG */ 235 cw = wcwidth(wc); 236 if (cw < 0) 237 cw = 1; 238 if (cw) { 239 /* spacing character */ 240 #ifdef DEBUG 241 __CTRACE(__CTRACE_INPUT, 242 " as a spacing char(width=%d)\n", cw); 243 #endif /* DEBUG */ 244 if (cw > win->maxx - ex) { 245 /* clear to EOL */ 246 while (ex < win->maxx) { 247 lp->ch = (wchar_t) 248 btowc((int) win->bch); 249 if (_cursesi_copy_nsp(win->bnsp, lp) 250 == ERR) 251 return ERR; 252 lp->attr = win->battr; 253 SET_WCOL(*lp, 1); 254 lp++, ex++; 255 } 256 ex = win->maxx - 1; 257 break; 258 } 259 /* this could combine with the insertion of 260 * non-spacing char */ 261 np = lp->nsp; 262 if (np) { 263 while (np) { 264 tnp = np->next; 265 free(np); 266 np = tnp; 267 } 268 lp->nsp = NULL; 269 } 270 lp->ch = chp->vals[0]; 271 lp->attr = chp->attributes & WA_ATTRIBUTES; 272 SET_WCOL(*lp, cw); 273 if (chp->elements > 1) { 274 for (i = 1; i < chp->elements; i++) { 275 np = (nschar_t *) 276 malloc(sizeof(nschar_t)); 277 if (!np) 278 return ERR; 279 np->ch = chp->vals[i]; 280 np->next = lp->nsp; 281 lp->nsp = np; 282 } 283 } 284 lp++, ex++; 285 #ifdef DEBUG 286 __CTRACE(__CTRACE_INPUT, 287 "wadd_wchnstr: ex = %d, x = %d, cw = %d\n", 288 ex, x, cw); 289 #endif /* DEBUG */ 290 while (ex - x <= cw - 1) { 291 np = lp->nsp; 292 if (np) { 293 while (np) { 294 tnp = np->next; 295 free(np); 296 np = tnp; 297 } 298 lp->nsp = NULL; 299 } 300 lp->ch = chp->vals[0]; 301 lp->attr = chp->attributes & WA_ATTRIBUTES; 302 SET_WCOL(*lp, x - ex); 303 lp++, ex++; 304 } 305 } else { 306 /* non-spacing character */ 307 #ifdef DEBUG 308 __CTRACE(__CTRACE_INPUT, 309 "wadd_wchnstr: as non-spacing char"); 310 #endif /* DEBUG */ 311 for (i = 0; i < chp->elements; i++) { 312 np = (nschar_t *)malloc(sizeof(nschar_t)); 313 if (!np) 314 return ERR; 315 np->ch = chp->vals[i]; 316 np->next = lp->nsp; 317 lp->nsp = np; 318 } 319 } 320 cnt--, chp++; 321 } 322 #ifdef DEBUG 323 for (i = sx; i < ex; i++) { 324 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: (%d,%d)=(%x,%x,%p)\n", 325 win->cury, i, win->alines[win->cury]->line[i].ch, 326 win->alines[win->cury]->line[i].attr, 327 win->alines[win->cury]->line[i].nsp); 328 } 329 #endif /* DEBUG */ 330 lnp->flags |= __ISDIRTY; 331 newx = ex + win->ch_off; 332 if (newx > *lnp->lastchp) 333 *lnp->lastchp = newx; 334 __touchline(win, y, sx, ex); 335 336 return OK; 337 #endif /* HAVE_WCHAR */ 338 } 339