Home | History | Annotate | Line # | Download | only in libcurses
ins_wstr.c revision 1.2
      1 /*   $NetBSD: ins_wstr.c,v 1.2 2007/05/28 15:01:56 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: ins_wstr.c,v 1.2 2007/05/28 15:01:56 blymn Exp $");
     40 #endif						  /* not lint */
     41 
     42 #include <string.h>
     43 #include <stdlib.h>
     44 
     45 #include "curses.h"
     46 #include "curses_private.h"
     47 
     48 /*
     49  * ins_wstr --
     50  *	insert a multi-character wide character string into the current window
     51  */
     52 int
     53 ins_wstr(const wchar_t *wstr)
     54 {
     55 	return wins_wstr(stdscr, wstr);
     56 }
     57 
     58 /*
     59  * ins_nwstr --
     60  *	insert a multi-character wide character string into the current window
     61  *	with at most n characters
     62  */
     63 int
     64 ins_nwstr(const wchar_t *wstr, int n)
     65 {
     66 	return wins_nwstr(stdscr, wstr, n);
     67 }
     68 
     69 /*
     70  * mvins_wstr --
     71  *	  Do an insert-string on the line at (y, x).
     72  */
     73 int
     74 mvins_wstr(int y, int x, const wchar_t *wstr)
     75 {
     76 	return mvwins_wstr(stdscr, y, x, wstr);
     77 }
     78 
     79 /*
     80  * mvins_nwstr --
     81  *	  Do an insert-n-string on the line at (y, x).
     82  */
     83 int
     84 mvins_nwstr(int y, int x, const wchar_t *wstr, int n)
     85 {
     86 	return mvwins_nwstr(stdscr, y, x, wstr, n);
     87 }
     88 
     89 /*
     90  * mvwins_wstr --
     91  *	  Do an insert-string on the line at (y, x) in the given window.
     92  */
     93 int
     94 mvwins_wstr(WINDOW *win, int y, int x, const wchar_t *wstr)
     95 {
     96 	if (wmove(win, y, x) == ERR)
     97 		return ERR;
     98 
     99 	return wins_wstr(stdscr, wstr);
    100 }
    101 
    102 /*
    103  * mvwins_nwstr --
    104  *	  Do an insert-n-string on the line at (y, x) in the given window.
    105  */
    106 int
    107 mvwins_nwstr(WINDOW *win, int y, int x, const wchar_t *wstr, int n)
    108 {
    109 	if (wmove(win, y, x) == ERR)
    110 		return ERR;
    111 
    112 	return wins_nwstr(stdscr, wstr, n);
    113 }
    114 
    115 
    116 /*
    117  * wins_wstr --
    118  *	Do an insert-string on the line, leaving (cury, curx) unchanged.
    119  */
    120 int
    121 wins_wstr(WINDOW *win, const wchar_t *wstr)
    122 {
    123 	return wins_nwstr(win, wstr, -1);
    124 }
    125 
    126 /*
    127  * wins_nwstr --
    128  *	Do an insert-n-string on the line, leaving (cury, curx) unchanged.
    129  */
    130 int
    131 wins_nwstr(WINDOW *win, const wchar_t *wstr, int n)
    132 {
    133 #ifndef HAVE_WCHAR
    134 	return ERR;
    135 #else
    136 	__LDATA	 *start, *temp1, *temp2;
    137 	__LINE	  *lnp;
    138 	const wchar_t *scp;
    139 	int width, len, sx, x, y, cw, pcw, newx;
    140 	nschar_t *np;
    141 	wchar_t ws[] = L"		";
    142 
    143 	/* check for leading non-spacing character */
    144 	if (!wstr)
    145 		return OK;
    146 	cw = wcwidth(*wstr);
    147 	if (!cw)
    148 		return ERR;
    149 
    150 	scp = wstr + 1;
    151 	width = cw;
    152 	len = 1;
    153 	n--;
    154 	while (*scp) {
    155 		if (!n)
    156 			break;
    157 		n--, len++, width += wcwidth(*scp);
    158 		scp++;
    159 	}
    160 #ifdef DEBUG
    161 	__CTRACE(__CTRACE_INPUT, "wins_nwstr: width=%d,len=%d\n", width, len);
    162 #endif /* DEBUG */
    163 
    164 	if (cw > win->maxx - win->curx + 1)
    165 		return ERR;
    166 	start = &win->lines[win->cury]->line[win->curx];
    167 	sx = win->curx;
    168 	lnp = win->lines[win->cury];
    169 	pcw = WCOL(*start);
    170 	if (pcw < 0) {
    171 		sx += pcw;
    172 		start += pcw;
    173 	}
    174 #ifdef DEBUG
    175 	__CTRACE(__CTRACE_INPUT, "wins_nwstr: start@(%d)\n", sx);
    176 #endif /* DEBUG */
    177 	pcw = WCOL(*start);
    178 	lnp->flags |= __ISDIRTY;
    179 	newx = sx + win->ch_off;
    180 	if (newx < *lnp->firstchp)
    181 		*lnp->firstchp = newx;
    182 #ifdef DEBUG
    183 	{
    184 		int x;
    185 		__CTRACE(__CTRACE_INPUT, "========before=======\n");
    186 		for (x = 0; x < win->maxx; x++)
    187 			__CTRACE(__CTRACE_INPUT,
    188 			    "wins_nwstr: (%d,%d)=(%x,%x,%p)\n",
    189 			    (int) win->cury, x,
    190 			    win->lines[win->cury]->line[x].ch,
    191 			    win->lines[win->cury]->line[x].attr,
    192 			    win->lines[win->cury]->line[x].nsp);
    193 	}
    194 #endif /* DEBUG */
    195 
    196 	/* shift all complete characters */
    197 	if (sx + width + pcw <= win->maxx) {
    198 #ifdef DEBUG
    199 		__CTRACE(__CTRACE_INPUT, "wins_nwstr: shift all characters\n");
    200 #endif /* DEBUG */
    201 		temp1 = &win->lines[win->cury]->line[win->maxx - 1];
    202 		temp2 = temp1 - width;
    203 		pcw = WCOL(*(temp2 + 1));
    204 		if (pcw < 0) {
    205 #ifdef DEBUG
    206 			__CTRACE(__CTRACE_INPUT,
    207 			    "wins_nwstr: clear from %d to EOL(%d)\n",
    208 			    win->maxx + pcw, win->maxx - 1);
    209 #endif /* DEBUG */
    210 			temp2 += pcw;
    211 			while (temp1 > temp2 + width) {
    212 				temp1->ch = (wchar_t)btowc((int) win->bch);
    213 				if (_cursesi_copy_nsp(win->bnsp, temp1) == ERR)
    214 					return ERR;
    215 				temp1->attr = win->battr;
    216 				SET_WCOL(*temp1, 1);
    217 #ifdef DEBUG
    218 				__CTRACE(__CTRACE_INPUT,
    219 				    "wins_nwstr: empty cell(%p)\n", temp1);
    220 #endif /* DEBUG */
    221 				temp1--;
    222 			}
    223 		}
    224 		while (temp2 >= start) {
    225 			(void)memcpy(temp1, temp2, sizeof(__LDATA));
    226 			temp1--, temp2--;
    227 		}
    228 #ifdef DEBUG
    229 		{
    230 			int x;
    231 			__CTRACE(__CTRACE_INPUT, "=====after shift====\n");
    232 			for (x = 0; x < win->maxx; x++)
    233 				__CTRACE(__CTRACE_INPUT,
    234 				    "wins_nwstr: (%d,%d)=(%x,%x,%p)\n",
    235 				    (int) win->cury, x,
    236 				    win->lines[win->cury]->line[x].ch,
    237 				    win->lines[win->cury]->line[x].attr,
    238 				    win->lines[win->cury]->line[x].nsp);
    239 		}
    240 #endif /* DEBUG */
    241 	}
    242 
    243 	/* update string columns */
    244 	x = win->curx;
    245 	y = win->cury;
    246 	for (scp = wstr, temp1 = start; len; len--, scp++) {
    247 		switch (*scp) {
    248 			case L'\b':
    249 				if (--x < 0)
    250 					x = 0;
    251 				win->curx = x;
    252 				continue;;
    253 			case L'\r':
    254 				win->curx = 0;
    255 				continue;
    256 			case L'\n':
    257 				wclrtoeol(win);
    258 				if (y == win->scr_b) {
    259 					if (!(win->flags & __SCROLLOK))
    260 						return ERR;
    261 					scroll(win);
    262 				}
    263 				continue;
    264 			case L'\t':
    265 				if (wins_nwstr(win, ws,
    266 						min(win->maxx - x, 8-(x % 8)))
    267 							== ERR)
    268 					return ERR;
    269 				continue;
    270 		}
    271 		cw = wcwidth(*scp);
    272 		if (cw) {
    273 			/* 1st column */
    274 			temp1->ch = (wchar_t)*scp;
    275 			temp1->attr = win->wattr;
    276 			SET_WCOL(*temp1, cw);
    277 			temp1->nsp = NULL;
    278 #ifdef DEBUG
    279 			__CTRACE(__CTRACE_INPUT,
    280 			    "wins_nwstr: add spacing char(%x)\n", temp1->ch);
    281 #endif /* DEBUG */
    282 			temp2 = temp1++;
    283 			if (cw > 1) {
    284 				x = -1;
    285 				while (temp1 < temp2 + cw) {
    286 					/* the rest columns */
    287 					temp1->ch = (wchar_t)*scp;
    288 					temp1->attr = win->wattr;
    289 					temp1->nsp = NULL;
    290 					SET_WCOL(*temp1, x);
    291 					temp1++, x--;
    292 				}
    293 				temp1--;
    294 			}
    295 		} else {
    296 			/* non-spacing character */
    297 			np = (nschar_t *)malloc(sizeof(nschar_t));
    298 			if (!np)
    299 				return ERR;
    300 			np->ch = *scp;
    301 			np->next = temp1->nsp;
    302 			temp1->nsp = np;
    303 #ifdef DEBUG
    304 			__CTRACE(__CTRACE_INPUT,
    305 			    "wins_nstr: add non-spacing char(%x)\n", np->ch);
    306 #endif /* DEBUG */
    307 		}
    308 	}
    309 #ifdef DEBUG
    310 	{
    311 		int x;
    312 		__CTRACE(__CTRACE_INPUT, "========after=======\n");
    313 		for (x = 0; x < win->maxx; x++)
    314 			__CTRACE(__CTRACE_INPUT,
    315 			    "wins_nwstr: (%d,%d)=(%x,%x,%p)\n",
    316 			    (int) win->cury, x,
    317 			    win->lines[win->cury]->line[x].ch,
    318 			    win->lines[win->cury]->line[x].attr,
    319 			    win->lines[win->cury]->line[x].nsp);
    320 	}
    321 #endif /* DEBUG */
    322 	newx = win->maxx - 1 + win->ch_off;
    323 	if (newx > *lnp->lastchp)
    324 		*lnp->lastchp = newx;
    325 	__touchline(win, (int) win->cury, sx, (int) win->maxx - 1);
    326 	return OK;
    327 #endif /* HAVE_WCHAR */
    328 }
    329