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 /* LINTLIBRARY */
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[] =
41 "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
42 "libxcurses/src/libc/xcurses/rcs/wins_wch.c 1.3 1998/06/04 17:54:38 "
43 "cbates Exp $";
44 #endif
45 #endif
46
47 #include <private.h>
48 #include <string.h>
49
50 /*
51 * Insert a character into a window line, shifting the line right
52 * the column width of the inserted character. The right most columns
53 * will be truncated according to the width of the character inserted.
54 */
55 int
__m_cc_ins(WINDOW * w,int y,int x,const cchar_t * cc)56 __m_cc_ins(WINDOW *w, int y, int x, const cchar_t *cc)
57 {
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 /*
67 * Use erase to remove possible multi-column chars
68 * before memmove clobbers them
69 */
70 (void) __m_cc_erase(w, y, w->_maxx - width, y, w->_maxx - 1);
71
72 /* Insert a "hole" into the line. */
73 (void) memmove(&w->_line[y][x + width], &w->_line[y][x],
74 (w->_maxx - x - width) * sizeof (**w->_line));
75
76 /* Write the character into the hole. */
77 if (__m_cc_replace(w, y, x, cc, 0) != width)
78 return (-1);
79
80 /* Update dirty region markers. */
81 if (x < w->_first[y])
82 w->_first[y] = (short) x;
83 w->_last[y] = w->_maxx;
84
85 /*
86 * If the last character on the line is incomplete,
87 * blank it out.
88 */
89 x = __m_cc_first(w, y, w->_maxx-1);
90 if (w->_maxx < x + __m_cc_width(&w->_line[y][x]))
91 (void) __m_cc_erase(w, y, x, y, w->_maxx-1);
92
93 return (width);
94 }
95
96 /*
97 * Special internal version of wins_wch() that can track the cursor
98 * position to facilitate inserting strings containing special characters
99 * like \b, \n, \r, and \t.
100 */
101 int
__m_wins_wch(WINDOW * w,int y,int x,const cchar_t * cc,int * yp,int * xp)102 __m_wins_wch(WINDOW *w, int y, int x, const cchar_t *cc,
103 int *yp, int *xp)
104 {
105 cchar_t uc;
106 const wchar_t *p;
107 int code, nx, width;
108
109 code = ERR;
110
111 switch (cc->_wc[0]) {
112 case L'\r':
113 x = 0;
114 break;
115 case L'\b':
116 if (0 < x)
117 --x;
118 break;
119 case L'\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 L'\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((cchar_t *)cc)) == NULL)
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 != NULL)
156 *yp = y;
157 if (xp != NULL)
158 *xp = x;
159
160 WSYNC(w);
161
162 code = WFLUSH(w);
163 error:
164 return (code);
165 }
166
167 /*
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
wins_wch(WINDOW * w,const cchar_t * cc)174 wins_wch(WINDOW *w, const cchar_t *cc)
175 {
176 int code;
177
178 code = __m_wins_wch(w, w->_cury, w->_curx, cc, NULL, NULL);
179
180 return (code);
181 }
182