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