xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/xcurses/wins_wch.c (revision 8226594fdd4479be135127f43632f1f995074654)
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 /*
28  * wins_wch.c
29  *
30  * XCurses Library
31  *
32  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
33  *
34  */
35 
36 #ifdef M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wins_wch.c 1.9 1995/09/28 13:18:36 ant Exp $";
39 #endif
40 #endif
41 
42 #include <private.h>
43 
44 /*
45  * Insert a character into a window line, shifting the line right
46  * the column width of the inserted character.  The right most columns
47  * will be truncated according to the width of the character inserted.
48  */
49 int
50 __m_cc_ins(w, y, x, cc)
51 WINDOW *w;
52 int y, x;
53 const cchar_t *cc;
54 {
55 	extern void *memmove();		/* quiet sparcv9 warning */
56 	int width;
57 
58 	/* Determine the character width to insert. */
59 	if ((width = __m_cc_width(cc)) <= 0 || w->_maxx < x + width)
60 		return -1;
61 
62 	x = __m_cc_first(w, y, x);
63 
64 	/* Insert a "hole" into the line. */
65 	(void) memmove(
66 		&w->_line[y][x + width], &w->_line[y][x],
67 		(w->_maxx - x - width) * sizeof **w->_line
68 	);
69 
70 	/* Write the character into the hole. */
71 	if (__m_cc_replace(w, y, x, cc, 0) != width)
72 		return -1;
73 
74 	/* Update dirty region markers. */
75 	if (x < w->_first[y])
76 		w->_first[y] = x;
77 	w->_last[y] = w->_maxx;
78 
79 	/* If the last character on the line is incomplete,
80 	 * blank it out.
81 	 */
82 	x = __m_cc_first(w, y, w->_maxx-1);
83 	if (w->_maxx < x + __m_cc_width(&w->_line[y][x]))
84 		(void) __m_cc_erase(w, y, x, y, w->_maxx-1);
85 
86 	return width;
87 }
88 
89 /*
90  * Special internal version of wins_wch() that can track the cursor
91  * position to facilitate inserting strings containing special characters
92  * like \b, \n, \r, and \t.
93  */
94 int
95 __m_wins_wch(w, y, x, cc, yp, xp)
96 WINDOW *w;
97 int y, x;
98 const cchar_t *cc;
99 int *yp, *xp;
100 {
101         cchar_t uc;
102         const wchar_t *p;
103         int code, nx, width;
104 
105 #ifdef M_CURSES_TRACE
106 	__m_trace("__m_wins_wch(%p, %d, %d, %p, %p, %p)", w, y, x, cc, yp, xp);
107 #endif
108 
109 	code = ERR;
110 
111 	switch (cc->_wc[0]) {
112 	case '\r':
113 		x = 0;
114 		break;
115 	case '\b':
116 		if (0 < x)
117 			--x;
118 		break;
119 	case '\t':
120 		for (nx = x + (8 - (x & 07)); x < nx; x += width)
121 			if ((width = __m_cc_ins(w, y, x, &w->_bg)) <= 0)
122 				goto error;
123 		break;
124 	case '\n':
125 		/* Truncate the tail of the current line. */
126 		if (__m_cc_erase(w, y, x, y, w->_maxx-1) == -1)
127 			goto error;
128 
129 		if (__m_do_scroll(w, y, x, &y, &x) == ERR)
130 			goto error;
131 		break;
132 	default:
133 		if (iswprint(cc->_wc[0])) {
134 			if ((width = __m_cc_ins(w, y, x, cc)) <= 0)
135 				goto error;
136 			x += width;
137 			break;
138 		}
139 
140 		/* Convert non-printables into printable representation. */
141 		uc._n = 1;
142 		uc._at = cc->_at;
143 		uc._co = cc->_co;
144 
145 		if ((p = wunctrl(cc)) == (wchar_t *) 0)
146 			goto error;
147 
148 		for ( ; *p != '\0'; ++p, x += width) {
149 			uc._wc[0] = *p;
150 			if ((width = __m_cc_ins(w, y, x, &uc)) < 0)
151 				goto error;
152 		}
153 	}
154 
155 	if (yp != (int *) 0)
156 		*yp = y;
157 	if (xp != (int *) 0)
158 		*xp = x;
159 
160 	WSYNC(w);
161 
162 	code = WFLUSH(w);
163 error:
164 	return __m_return_code("wins_wch", code);
165 }
166 
167 /*f
168  * Insert a character (with attributes) before the cursor. All
169  * characters to the right of the cursor are moved one space to
170  * the right, with a possibility of the rightmost character on
171  * the line being lost.  The cursor position does not change.
172  */
173 int
174 wins_wch(w, cc)
175 WINDOW *w;
176 const cchar_t *cc;
177 {
178 	int code;
179 
180 #ifdef M_CURSES_TRACE
181 	__m_trace("wins_wch(%p, %p) at (%d, %d)", w, cc, w->_cury, w->_curx);
182 #endif
183 
184 	code = __m_wins_wch(w, w->_cury, w->_curx, cc, (int *) 0, (int *) 0);
185 
186 	return __m_return_code("wins_wch", code);
187 }
188 
189