Home | History | Annotate | Line # | Download | only in libcurses
add_wchstr.c revision 1.13
      1 /*   $NetBSD: add_wchstr.c,v 1.13 2022/05/03 07:25:34 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.13 2022/05/03 07:25:34 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 	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 	__CTRACE(__CTRACE_INPUT,
    146 	    "wadd_wchnstr: win = %p, wchstr = %p, n = %d\n", win, wchstr, n);
    147 
    148 	if (!wchstr)
    149 		return OK;
    150 
    151 	/* compute length of the cchar string */
    152 	if (n < -1)
    153 		return ERR;
    154 	if (n >= 0)
    155 		for (chp = wchstr, cnt = 0; n && chp->vals[0];
    156 			n--, chp++, ++cnt);
    157 	else
    158 		for (chp = wchstr, cnt = 0; chp->vals[0]; chp++, ++cnt);
    159 	__CTRACE(__CTRACE_INPUT, "wadd_wchnstr: len=%d\n", cnt);
    160 	chp = wchstr;
    161 	x = win->curx;
    162 	y = win->cury;
    163 	lp = &win->alines[y]->line[x];
    164 	lnp = win->alines[y];
    165 
    166 	cw = (*lp).wcols;
    167 	if (cw >= 0) {
    168 		sx = x;
    169 	} else {
    170 		if (wcwidth(chp->vals[0])) {
    171 			/* clear the partial character before cursor */
    172 			for (tp = lp + cw; tp < lp; tp++) {
    173 				tp->ch = win->bch;
    174 				if (_cursesi_copy_nsp(win->bnsp, tp) == ERR)
    175 					return ERR;
    176 				tp->attr = win->battr;
    177 				(*tp).wcols = 1;
    178 				np = tp->nsp;
    179 			}
    180 		} else {
    181 			/* move to the start of current char */
    182 			lp += cw;
    183 			x += cw;
    184 		}
    185 		sx = x + cw;
    186 	}
    187 	lnp->flags |= __ISDIRTY;
    188 	newx = sx + win->ch_off;
    189 	if (newx < *lnp->firstchp)
    190 		*lnp->firstchp = newx;
    191 
    192 	/* add characters in the string */
    193 	ex = x;
    194 	while (cnt) {
    195 		x = ex;
    196 		wc = chp->vals[0];
    197 		__CTRACE(__CTRACE_INPUT, "wadd_wchnstr: adding %x", wc);
    198 		cw = wcwidth(wc);
    199 		if (cw < 0)
    200 			cw = 1;
    201 		if (cw) {
    202 			/* spacing character */
    203 			__CTRACE(__CTRACE_INPUT,
    204 			    " as a spacing char(width=%d)\n", cw);
    205 			if (cw > win->maxx - ex) {
    206 				/* clear to EOL */
    207 				while (ex < win->maxx) {
    208 					lp->ch = win->bch;
    209 					if (_cursesi_copy_nsp(win->bnsp, lp)
    210 					    == ERR)
    211 						return ERR;
    212 					lp->attr = win->battr;
    213 					lp->cflags |= CA_BACKGROUND;
    214 					(*lp).wcols = 1;
    215 					lp++, ex++;
    216 				}
    217 				ex = win->maxx - 1;
    218 				break;
    219 			}
    220 			/* this could combine with the insertion of
    221 			 * non-spacing char */
    222 			np = lp->nsp;
    223 			if (np) {
    224 				while (np) {
    225 					tnp = np->next;
    226 					free(np);
    227 					np = tnp;
    228 				}
    229 				lp->nsp = NULL;
    230 			}
    231 			lp->ch = chp->vals[0];
    232 			lp->attr = chp->attributes & WA_ATTRIBUTES;
    233 			lp->cflags &= ~CA_BACKGROUND;
    234 			(*lp).wcols = cw;
    235 			if (chp->elements > 1) {
    236 				for (i = 1; i < chp->elements; i++) {
    237 					np = (nschar_t *)
    238 						malloc(sizeof(nschar_t));
    239 					if (!np)
    240 						return ERR;
    241 					np->ch = chp->vals[i];
    242 					np->next = lp->nsp;
    243 					lp->nsp = np;
    244 				}
    245 			}
    246 			lp++, ex++;
    247 			__CTRACE(__CTRACE_INPUT,
    248 			    "wadd_wchnstr: ex = %d, x = %d, cw = %d\n",
    249 			    ex, x, cw);
    250 			while (ex - x <= cw - 1) {
    251 				np = lp->nsp;
    252 				if (np) {
    253 					while (np) {
    254 						tnp = np->next;
    255 						free(np);
    256 						np = tnp;
    257 					}
    258 					lp->nsp = NULL;
    259 				}
    260 				lp->ch = chp->vals[0];
    261 				lp->attr = chp->attributes & WA_ATTRIBUTES;
    262 				(*lp).wcols = x - ex;
    263 				lp++, ex++;
    264 			}
    265 		} else {
    266 			/* non-spacing character */
    267 			__CTRACE(__CTRACE_INPUT,
    268 			    "wadd_wchnstr: as non-spacing char");
    269 			for (i = 0; i < chp->elements; i++) {
    270 				np = malloc(sizeof(nschar_t));
    271 				if (!np)
    272 					return ERR;
    273 				np->ch = chp->vals[i];
    274 				np->next = lp->nsp;
    275 				lp->nsp = np;
    276 			}
    277 		}
    278 		cnt--, chp++;
    279 	}
    280 #ifdef DEBUG
    281 	for (i = sx; i < ex; i++) {
    282 		__CTRACE(__CTRACE_INPUT, "wadd_wchnstr: (%d,%d)=(%x,%x,%p)\n",
    283 		    win->cury, i, win->alines[win->cury]->line[i].ch,
    284 		    win->alines[win->cury]->line[i].attr,
    285 		    win->alines[win->cury]->line[i].nsp);
    286 	}
    287 #endif /* DEBUG */
    288 	lnp->flags |= __ISDIRTY;
    289 	newx = ex + win->ch_off;
    290 	if (newx > *lnp->lastchp)
    291 		*lnp->lastchp = newx;
    292 	__touchline(win, y, sx, ex);
    293 
    294 	return OK;
    295 }
    296