xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/xcurses/wins_wch.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * wins_wch.c
31  *
32  * XCurses Library
33  *
34  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35  *
36  */
37 
38 #ifdef M_RCSID
39 #ifndef lint
40 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wins_wch.c 1.9 1995/09/28 13:18:36 ant Exp $";
41 #endif
42 #endif
43 
44 #include <private.h>
45 
46 /*
47  * Insert a character into a window line, shifting the line right
48  * the column width of the inserted character.  The right most columns
49  * will be truncated according to the width of the character inserted.
50  */
51 int
52 __m_cc_ins(w, y, x, cc)
53 WINDOW *w;
54 int y, x;
55 const cchar_t *cc;
56 {
57 	extern void *memmove();		/* quiet sparcv9 warning */
58 	int width;
59 
60 	/* Determine the character width to insert. */
61 	if ((width = __m_cc_width(cc)) <= 0 || w->_maxx < x + width)
62 		return -1;
63 
64 	x = __m_cc_first(w, y, x);
65 
66 	/* Insert a "hole" into the line. */
67 	(void) memmove(
68 		&w->_line[y][x + width], &w->_line[y][x],
69 		(w->_maxx - x - width) * sizeof **w->_line
70 	);
71 
72 	/* Write the character into the hole. */
73 	if (__m_cc_replace(w, y, x, cc, 0) != width)
74 		return -1;
75 
76 	/* Update dirty region markers. */
77 	if (x < w->_first[y])
78 		w->_first[y] = x;
79 	w->_last[y] = w->_maxx;
80 
81 	/* If the last character on the line is incomplete,
82 	 * blank it out.
83 	 */
84 	x = __m_cc_first(w, y, w->_maxx-1);
85 	if (w->_maxx < x + __m_cc_width(&w->_line[y][x]))
86 		(void) __m_cc_erase(w, y, x, y, w->_maxx-1);
87 
88 	return width;
89 }
90 
91 /*
92  * Special internal version of wins_wch() that can track the cursor
93  * position to facilitate inserting strings containing special characters
94  * like \b, \n, \r, and \t.
95  */
96 int
97 __m_wins_wch(w, y, x, cc, yp, xp)
98 WINDOW *w;
99 int y, x;
100 const cchar_t *cc;
101 int *yp, *xp;
102 {
103         cchar_t uc;
104         const wchar_t *p;
105         int code, nx, width;
106 
107 #ifdef M_CURSES_TRACE
108 	__m_trace("__m_wins_wch(%p, %d, %d, %p, %p, %p)", w, y, x, cc, yp, xp);
109 #endif
110 
111 	code = ERR;
112 
113 	switch (cc->_wc[0]) {
114 	case '\r':
115 		x = 0;
116 		break;
117 	case '\b':
118 		if (0 < x)
119 			--x;
120 		break;
121 	case '\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 '\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(cc)) == (wchar_t *) 0)
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 != (int *) 0)
158 		*yp = y;
159 	if (xp != (int *) 0)
160 		*xp = x;
161 
162 	WSYNC(w);
163 
164 	code = WFLUSH(w);
165 error:
166 	return __m_return_code("wins_wch", code);
167 }
168 
169 /*f
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(w, cc)
177 WINDOW *w;
178 const cchar_t *cc;
179 {
180 	int code;
181 
182 #ifdef M_CURSES_TRACE
183 	__m_trace("wins_wch(%p, %p) at (%d, %d)", w, cc, w->_cury, w->_curx);
184 #endif
185 
186 	code = __m_wins_wch(w, w->_cury, w->_curx, cc, (int *) 0, (int *) 0);
187 
188 	return __m_return_code("wins_wch", code);
189 }
190 
191