Home | History | Annotate | Line # | Download | only in libcurses
ins_wch.c revision 1.20
      1 /*   $NetBSD: ins_wch.c,v 1.20 2022/10/19 06:09:27 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_wch.c,v 1.20 2022/10/19 06:09:27 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_wch --
     50  *	Do an insert-char on the line, leaving (cury, curx) unchanged.
     51  */
     52 int
     53 ins_wch(const cchar_t *wch)
     54 {
     55 	return wins_wch(stdscr, wch);
     56 }
     57 
     58 /*
     59  * mvins_wch --
     60  *	  Do an insert-char on the line at (y, x).
     61  */
     62 int
     63 mvins_wch(int y, int x, const cchar_t *wch)
     64 {
     65 	return mvwins_wch(stdscr, y, x, wch);
     66 }
     67 
     68 /*
     69  * mvwins_wch --
     70  *	  Do an insert-char on the line at (y, x) in the given window.
     71  */
     72 int
     73 mvwins_wch(WINDOW *win, int y, int x, const cchar_t *wch)
     74 {
     75 	if (wmove(win, y, x) == ERR)
     76 		return ERR;
     77 
     78 	return wins_wch(win, wch);
     79 }
     80 
     81 /*
     82  * wins_wch --
     83  *	Do an insert-char on the line, leaving (cury, curx) unchanged.
     84  */
     85 int
     86 wins_wch(WINDOW *win, const cchar_t *wch)
     87 {
     88 	__LDATA	*start, *temp1, *temp2;
     89 	__LINE *lnp;
     90 	int cw, pcw, x, y, sx, ex, newx, i;
     91 	nschar_t *np, *tnp;
     92 	wchar_t ws[] = L"		";
     93 
     94 	/* check for non-spacing characters */
     95 	if (!wch)
     96 		return OK;
     97 	cw = wcwidth(wch->vals[0]);
     98 	__CTRACE(__CTRACE_INPUT, "wins_wch: wcwidth %d\n", cw);
     99 	if (cw < 0)
    100 		cw = 1;
    101 	if (!cw)
    102 		return wadd_wch( win, wch );
    103 
    104 	x = win->curx;
    105 	y = win->cury;
    106 	__CTRACE(__CTRACE_INPUT, "wins_wch: (%d,%d)\n", y, x);
    107 	switch (wch->vals[0]) {
    108 		case L'\b':
    109 			if (--x < 0)
    110 				x = 0;
    111 			win->curx = x;
    112 			return OK;
    113 		case L'\r':
    114 			win->curx = 0;
    115 			return OK;
    116 		case L'\n':
    117 			wclrtoeol(win);
    118 			if (y == win->scr_b) {
    119 				if (!(win->flags & __SCROLLOK))
    120 					return ERR;
    121 				scroll(win);
    122 			}
    123 			return OK;
    124 		case L'\t':
    125 			if (wins_nwstr(win, ws, min(win->maxx - x,
    126 			    TABSIZE - (x % TABSIZE))) == ERR)
    127 				return ERR;
    128 			return OK;
    129 	}
    130 
    131 	/* locate current cell */
    132 	x = win->curx;
    133 	y = win->cury;
    134 	lnp = win->alines[y];
    135 	start = &win->alines[y]->line[x];
    136 	sx = x;
    137 	pcw = start->wcols;
    138 	if (pcw < 0) {
    139 		start += pcw;
    140 		sx += pcw;
    141 	}
    142 	if (cw > win->maxx - sx)
    143 		return ERR;
    144 	lnp->flags |= __ISDIRTY;
    145 	newx = sx + win->ch_off;
    146 	if (newx < *lnp->firstchp)
    147 		*lnp->firstchp = newx;
    148 
    149 	/* shift all complete characters */
    150 	__CTRACE(__CTRACE_INPUT, "wins_wch: shift all characters\n");
    151 	temp1 = &win->alines[y]->line[win->maxx - 1];
    152 	temp2 = temp1 - cw;
    153 	pcw = (temp2 + 1)->wcols;
    154 	if (pcw < 0) {
    155 		__CTRACE(__CTRACE_INPUT, "wins_wch: clear EOL\n");
    156 		temp2 += pcw;
    157 		while (temp1 > temp2 + cw) {
    158 			np = temp1->nsp;
    159 			if (np) {
    160 				while (np) {
    161 					tnp = np->next;
    162 					free(np);
    163 					np = tnp;
    164 				}
    165 				temp1->nsp = NULL;
    166 			}
    167 			temp1->ch = win->bch;
    168 			if (_cursesi_copy_nsp(win->bnsp, temp1) == ERR)
    169 				return ERR;
    170 			temp1->attr = win->battr;
    171 			temp1->wcols = 1;
    172 			temp1--;
    173 		}
    174 	}
    175 	while (temp2 >= start) {
    176 		(void)memcpy(temp1, temp2, sizeof(__LDATA));
    177 		temp1--, temp2--;
    178 	}
    179 
    180 	/* update character under cursor */
    181 	start->nsp = NULL;
    182 	start->ch = wch->vals[0];
    183 	start->attr = wch->attributes & WA_ATTRIBUTES;
    184 	start->wcols = cw;
    185 	if (wch->elements > 1) {
    186 		for (i = 1; i < wch->elements; i++) {
    187 			np = malloc(sizeof(nschar_t));
    188 			if (!np)
    189 				return ERR;
    190 			np->ch = wch->vals[i];
    191 			np->next = start->nsp;
    192 			start->nsp = np;
    193 		}
    194 	}
    195 	__CTRACE(__CTRACE_INPUT, "wins_wch: insert (%x,%x,%p)\n",
    196 	    start->ch, start->attr, start->nsp);
    197 	temp1 = start + 1;
    198 	ex = x + 1;
    199 	while (ex - x < cw) {
    200 		temp1->ch = wch->vals[0];
    201 		temp1->wcols = x - ex;
    202 		temp1->nsp = NULL;
    203 		temp1->cflags |= CA_CONTINUATION;
    204 		ex++, temp1++;
    205 	}
    206 
    207 	newx = win->maxx - 1 + win->ch_off;
    208 	if (newx > *lnp->lastchp)
    209 		*lnp->lastchp = newx;
    210 	__touchline(win, y, sx, (int)win->maxx - 1);
    211 	__sync(win);
    212 	return OK;
    213 }
    214