1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * wins_wch.c
29*7c478bd9Sstevel@tonic-gate  *
30*7c478bd9Sstevel@tonic-gate  * XCurses Library
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID
37*7c478bd9Sstevel@tonic-gate #ifndef lint
38*7c478bd9Sstevel@tonic-gate static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wins_wch.c 1.9 1995/09/28 13:18:36 ant Exp $";
39*7c478bd9Sstevel@tonic-gate #endif
40*7c478bd9Sstevel@tonic-gate #endif
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <private.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * Insert a character into a window line, shifting the line right
46*7c478bd9Sstevel@tonic-gate  * the column width of the inserted character.  The right most columns
47*7c478bd9Sstevel@tonic-gate  * will be truncated according to the width of the character inserted.
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate int
__m_cc_ins(w,y,x,cc)50*7c478bd9Sstevel@tonic-gate __m_cc_ins(w, y, x, cc)
51*7c478bd9Sstevel@tonic-gate WINDOW *w;
52*7c478bd9Sstevel@tonic-gate int y, x;
53*7c478bd9Sstevel@tonic-gate const cchar_t *cc;
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	extern void *memmove();		/* quiet sparcv9 warning */
56*7c478bd9Sstevel@tonic-gate 	int width;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	/* Determine the character width to insert. */
59*7c478bd9Sstevel@tonic-gate 	if ((width = __m_cc_width(cc)) <= 0 || w->_maxx < x + width)
60*7c478bd9Sstevel@tonic-gate 		return -1;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	x = __m_cc_first(w, y, x);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	/* Insert a "hole" into the line. */
65*7c478bd9Sstevel@tonic-gate 	(void) memmove(
66*7c478bd9Sstevel@tonic-gate 		&w->_line[y][x + width], &w->_line[y][x],
67*7c478bd9Sstevel@tonic-gate 		(w->_maxx - x - width) * sizeof **w->_line
68*7c478bd9Sstevel@tonic-gate 	);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	/* Write the character into the hole. */
71*7c478bd9Sstevel@tonic-gate 	if (__m_cc_replace(w, y, x, cc, 0) != width)
72*7c478bd9Sstevel@tonic-gate 		return -1;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	/* Update dirty region markers. */
75*7c478bd9Sstevel@tonic-gate 	if (x < w->_first[y])
76*7c478bd9Sstevel@tonic-gate 		w->_first[y] = x;
77*7c478bd9Sstevel@tonic-gate 	w->_last[y] = w->_maxx;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	/* If the last character on the line is incomplete,
80*7c478bd9Sstevel@tonic-gate 	 * blank it out.
81*7c478bd9Sstevel@tonic-gate 	 */
82*7c478bd9Sstevel@tonic-gate 	x = __m_cc_first(w, y, w->_maxx-1);
83*7c478bd9Sstevel@tonic-gate 	if (w->_maxx < x + __m_cc_width(&w->_line[y][x]))
84*7c478bd9Sstevel@tonic-gate 		(void) __m_cc_erase(w, y, x, y, w->_maxx-1);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	return width;
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  * Special internal version of wins_wch() that can track the cursor
91*7c478bd9Sstevel@tonic-gate  * position to facilitate inserting strings containing special characters
92*7c478bd9Sstevel@tonic-gate  * like \b, \n, \r, and \t.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate int
__m_wins_wch(w,y,x,cc,yp,xp)95*7c478bd9Sstevel@tonic-gate __m_wins_wch(w, y, x, cc, yp, xp)
96*7c478bd9Sstevel@tonic-gate WINDOW *w;
97*7c478bd9Sstevel@tonic-gate int y, x;
98*7c478bd9Sstevel@tonic-gate const cchar_t *cc;
99*7c478bd9Sstevel@tonic-gate int *yp, *xp;
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate         cchar_t uc;
102*7c478bd9Sstevel@tonic-gate         const wchar_t *p;
103*7c478bd9Sstevel@tonic-gate         int code, nx, width;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
106*7c478bd9Sstevel@tonic-gate 	__m_trace("__m_wins_wch(%p, %d, %d, %p, %p, %p)", w, y, x, cc, yp, xp);
107*7c478bd9Sstevel@tonic-gate #endif
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	code = ERR;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	switch (cc->_wc[0]) {
112*7c478bd9Sstevel@tonic-gate 	case '\r':
113*7c478bd9Sstevel@tonic-gate 		x = 0;
114*7c478bd9Sstevel@tonic-gate 		break;
115*7c478bd9Sstevel@tonic-gate 	case '\b':
116*7c478bd9Sstevel@tonic-gate 		if (0 < x)
117*7c478bd9Sstevel@tonic-gate 			--x;
118*7c478bd9Sstevel@tonic-gate 		break;
119*7c478bd9Sstevel@tonic-gate 	case '\t':
120*7c478bd9Sstevel@tonic-gate 		for (nx = x + (8 - (x & 07)); x < nx; x += width)
121*7c478bd9Sstevel@tonic-gate 			if ((width = __m_cc_ins(w, y, x, &w->_bg)) <= 0)
122*7c478bd9Sstevel@tonic-gate 				goto error;
123*7c478bd9Sstevel@tonic-gate 		break;
124*7c478bd9Sstevel@tonic-gate 	case '\n':
125*7c478bd9Sstevel@tonic-gate 		/* Truncate the tail of the current line. */
126*7c478bd9Sstevel@tonic-gate 		if (__m_cc_erase(w, y, x, y, w->_maxx-1) == -1)
127*7c478bd9Sstevel@tonic-gate 			goto error;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 		if (__m_do_scroll(w, y, x, &y, &x) == ERR)
130*7c478bd9Sstevel@tonic-gate 			goto error;
131*7c478bd9Sstevel@tonic-gate 		break;
132*7c478bd9Sstevel@tonic-gate 	default:
133*7c478bd9Sstevel@tonic-gate 		if (iswprint(cc->_wc[0])) {
134*7c478bd9Sstevel@tonic-gate 			if ((width = __m_cc_ins(w, y, x, cc)) <= 0)
135*7c478bd9Sstevel@tonic-gate 				goto error;
136*7c478bd9Sstevel@tonic-gate 			x += width;
137*7c478bd9Sstevel@tonic-gate 			break;
138*7c478bd9Sstevel@tonic-gate 		}
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		/* Convert non-printables into printable representation. */
141*7c478bd9Sstevel@tonic-gate 		uc._n = 1;
142*7c478bd9Sstevel@tonic-gate 		uc._at = cc->_at;
143*7c478bd9Sstevel@tonic-gate 		uc._co = cc->_co;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 		if ((p = wunctrl(cc)) == (wchar_t *) 0)
146*7c478bd9Sstevel@tonic-gate 			goto error;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 		for ( ; *p != '\0'; ++p, x += width) {
149*7c478bd9Sstevel@tonic-gate 			uc._wc[0] = *p;
150*7c478bd9Sstevel@tonic-gate 			if ((width = __m_cc_ins(w, y, x, &uc)) < 0)
151*7c478bd9Sstevel@tonic-gate 				goto error;
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	if (yp != (int *) 0)
156*7c478bd9Sstevel@tonic-gate 		*yp = y;
157*7c478bd9Sstevel@tonic-gate 	if (xp != (int *) 0)
158*7c478bd9Sstevel@tonic-gate 		*xp = x;
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	WSYNC(w);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	code = WFLUSH(w);
163*7c478bd9Sstevel@tonic-gate error:
164*7c478bd9Sstevel@tonic-gate 	return __m_return_code("wins_wch", code);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate /*f
168*7c478bd9Sstevel@tonic-gate  * Insert a character (with attributes) before the cursor. All
169*7c478bd9Sstevel@tonic-gate  * characters to the right of the cursor are moved one space to
170*7c478bd9Sstevel@tonic-gate  * the right, with a possibility of the rightmost character on
171*7c478bd9Sstevel@tonic-gate  * the line being lost.  The cursor position does not change.
172*7c478bd9Sstevel@tonic-gate  */
173*7c478bd9Sstevel@tonic-gate int
wins_wch(w,cc)174*7c478bd9Sstevel@tonic-gate wins_wch(w, cc)
175*7c478bd9Sstevel@tonic-gate WINDOW *w;
176*7c478bd9Sstevel@tonic-gate const cchar_t *cc;
177*7c478bd9Sstevel@tonic-gate {
178*7c478bd9Sstevel@tonic-gate 	int code;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
181*7c478bd9Sstevel@tonic-gate 	__m_trace("wins_wch(%p, %p) at (%d, %d)", w, cc, w->_cury, w->_curx);
182*7c478bd9Sstevel@tonic-gate #endif
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	code = __m_wins_wch(w, w->_cury, w->_curx, cc, (int *) 0, (int *) 0);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	return __m_return_code("wins_wch", code);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189