Home | History | Annotate | Line # | Download | only in libcurses
add_wchstr.c revision 1.1.2.1
      1 /*   $NetBSD: add_wchstr.c,v 1.1.2.1 2007/01/21 12:05:54 blymn 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.1.2.1 2007/01/21 12:05:54 blymn 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("wadd_wchnstr: win = %p, wchstr = %p, n = %d\n",
    178 	    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("[wadd_wchnstr]len=%d", cnt);
    194 #endif /* DEBUG */
    195 	chp = wchstr;
    196 	x = win->curx;
    197 	y = win->cury;
    198 	lp = &win->lines[y]->line[x];
    199 	lnp = win->lines[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 		wc = chp->vals[0];
    231 #ifdef DEBUG
    232 		__CTRACE("[wadd_wchnstr]adding %x", wc);
    233 #endif /* DEBUG */
    234         cw = wcwidth(wc);
    235 		if (cw) {
    236 			/* spacing character */
    237 #ifdef DEBUG
    238 			__CTRACE(" as a spacing char(width=%d)\n", cw);
    239 #endif /* DEBUG */
    240 			if (cw > win->maxx - ex) {
    241 				/* clear to EOL */
    242 				while (ex < win->maxx) {
    243 					lp->ch = (wchar_t)
    244 						btowc((int) win->bch);
    245 					if (_cursesi_copy_nsp(win->bnsp, lp)
    246 					    == ERR)
    247 						return ERR;
    248 					lp->attr = win->battr;
    249 					SET_WCOL(*lp, 1);
    250 					lp++, ex++;
    251 				}
    252 				ex = win->maxx - 1;
    253 				break;
    254 			}
    255 			/* this could combine with the insertion of
    256 			 * non-spacing char */
    257 			np = lp->nsp;
    258 			if (np) {
    259 				while (np) {
    260 					tnp = np->next;
    261 					free(np);
    262 					np = tnp;
    263 				}
    264 				lp->nsp = NULL;
    265 			}
    266 			lp->ch = chp->vals[0];
    267 			lp->attr = chp->attributes & WA_ATTRIBUTES;
    268 			SET_WCOL(*lp, cw);
    269 			if (chp->elements > 1) {
    270 				for (i = 1; i < chp->elements; i++) {
    271 					np = (nschar_t *)
    272 						malloc(sizeof(nschar_t));
    273 					if (!np)
    274 						return ERR;
    275 					np->ch = chp->vals[i];
    276 					np->next = lp->nsp;
    277 					lp->nsp = np;
    278 				}
    279 			}
    280 			lp++, ex++;
    281 			while (ex - x <= cw - 1) {
    282 				np = lp->nsp;
    283 				if (np) {
    284 					while (np) {
    285 						tnp = np->next;
    286 						free(np);
    287 						np = tnp;
    288 					}
    289 					lp->nsp = NULL;
    290 				}
    291 				lp->ch = chp->vals[0];
    292 				lp->attr = chp->attributes & WA_ATTRIBUTES;
    293 				SET_WCOL(*lp, x - ex);
    294 				lp++, ex++;
    295 			}
    296 		} else {
    297 			/* non-spacing character */
    298 #ifdef DEBUG
    299 			__CTRACE("[wadd_wchnstr]as non-spacing char");
    300 #endif /* DEBUG */
    301 			for (i = 0; i < chp->elements; i++) {
    302 				np = (nschar_t *)malloc(sizeof(nschar_t));
    303 				if (!np)
    304 					return ERR;
    305 				np->ch = chp->vals[i];
    306 				np->next = lp->nsp;
    307 				lp->nsp = np;
    308 			}
    309 		}
    310 		cnt--, chp++;
    311 	}
    312 #ifdef DEBUG
    313 	for (i = sx; i < ex; i++) {
    314 		__CTRACE("[wadd_wchnstr](%d,%d)=(%x,%x,%p)\n",
    315 		    win->cury, i, win->lines[win->cury]->line[i].ch,
    316 		    win->lines[win->cury]->line[i].attr,
    317 		    win->lines[win->cury]->line[i].nsp);
    318 	}
    319 #endif /* DEBUG */
    320 	lnp->flags |= __ISDIRTY;
    321 	newx = ex + win->ch_off;
    322 	if (newx > *lnp->lastchp)
    323 		*lnp->lastchp = newx;
    324 	__touchline(win, y, sx, ex);
    325 
    326 	return OK;
    327 #endif /* HAVE_WCHAR */
    328 }
    329