blob: a1a55cb8bfd8b9bfa88ec7e11202beedfeb802b0 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28
29/****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * *
33 * Rewritten 2001-2004 to support wide-characters by *
34 * Sven Verdoolaege *
35 * Thomas Dickey *
36 ****************************************************************************/
37
38/*
39** lib_addstr.c
40*
41** The routines waddnstr(), waddchnstr().
42**
43*/
44
45#include <curses.priv.h>
46
Steve Kondikae271bc2015-11-15 02:50:53 +010047MODULE_ID("$Id: lib_addstr.c,v 1.52 2011/05/28 23:02:09 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053048
49NCURSES_EXPORT(int)
50waddnstr(WINDOW *win, const char *astr, int n)
51{
52 const char *str = astr;
53 int code = ERR;
54
Steve Kondikae271bc2015-11-15 02:50:53 +010055 T((T_CALLED("waddnstr(%p,%s,%d)"), (void *) win, _nc_visbufn(astr, n), n));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053056
57 if (win && (str != 0)) {
58 TR(TRACE_VIRTPUT | TRACE_ATTRS,
59 ("... current %s", _traceattr(WINDOW_ATTRS(win))));
60 code = OK;
61 if (n < 0)
62 n = (int) strlen(astr);
63
64 TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
65 while ((n-- > 0) && (*str != '\0')) {
66 NCURSES_CH_T ch;
67 TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
68 SetChar(ch, UChar(*str++), A_NORMAL);
69 if (_nc_waddch_nosync(win, ch) == ERR) {
70 code = ERR;
71 break;
72 }
73 }
74 _nc_synchook(win);
75 }
76 TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
77 returnCode(code);
78}
79
80NCURSES_EXPORT(int)
81waddchnstr(WINDOW *win, const chtype *astr, int n)
82{
83 NCURSES_SIZE_T y, x;
84 int code = OK;
85 int i;
86 struct ldat *line;
87
Steve Kondikae271bc2015-11-15 02:50:53 +010088 T((T_CALLED("waddchnstr(%p,%p,%d)"), (void *) win, (const void *) astr, n));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053089
90 if (!win)
91 returnCode(ERR);
92
93 y = win->_cury;
94 x = win->_curx;
95 if (n < 0) {
96 const chtype *str;
97 n = 0;
98 for (str = (const chtype *) astr; *str != 0; str++)
99 n++;
100 }
101 if (n > win->_maxx - x + 1)
102 n = win->_maxx - x + 1;
103 if (n == 0)
104 returnCode(code);
105
106 line = &(win->_line[y]);
107 for (i = 0; i < n && ChCharOf(astr[i]) != '\0'; ++i) {
108 SetChar2(line->text[i + x], astr[i]);
109 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100110 CHANGED_RANGE(line, x, (NCURSES_SIZE_T) (x + n - 1));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530111
112 _nc_synchook(win);
113 returnCode(code);
114}
115
116#if USE_WIDEC_SUPPORT
117
118NCURSES_EXPORT(int)
119_nc_wchstrlen(const cchar_t *s)
120{
121 int result = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100122 if (s != 0) {
123 while (CharOf(s[result]) != L'\0') {
124 result++;
125 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530126 }
127 return result;
128}
129
130NCURSES_EXPORT(int)
131wadd_wchnstr(WINDOW *win, const cchar_t *astr, int n)
132{
133 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
134 NCURSES_SIZE_T y;
135 NCURSES_SIZE_T x;
136 int code = OK;
137 struct ldat *line;
138 int i, j, start, len, end;
139
Steve Kondikae271bc2015-11-15 02:50:53 +0100140 T((T_CALLED("wadd_wchnstr(%p,%s,%d)"),
141 (void *) win,
142 _nc_viscbuf(astr, n),
143 n));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530144
145 if (!win)
146 returnCode(ERR);
147
148 y = win->_cury;
149 x = win->_curx;
150 if (n < 0) {
151 n = _nc_wchstrlen(astr);
152 }
153 if (n > win->_maxx - x + 1)
154 n = win->_maxx - x + 1;
155 if (n == 0)
156 returnCode(code);
157
158 line = &(win->_line[y]);
159 start = x;
160 end = x + n - 1;
161
162 /*
163 * Reset orphaned cells of multi-column characters that extend up to the
164 * new string's location to blanks.
165 */
166 if (x > 0 && isWidecExt(line->text[x])) {
167 for (i = 0; i <= x; ++i) {
168 if (!isWidecExt(line->text[x - i])) {
169 /* must be isWidecBase() */
170 start -= i;
171 while (i > 0) {
172 line->text[x - i--] = _nc_render(win, blank);
173 }
174 break;
175 }
176 }
177 }
178
179 /*
180 * Copy the new string to the window.
181 */
182 for (i = 0; i < n && CharOf(astr[i]) != L'\0' && x <= win->_maxx; ++i) {
183 if (isWidecExt(astr[i]))
184 continue;
185
186 len = wcwidth(CharOf(astr[i]));
187
188 if (x + len - 1 <= win->_maxx) {
189 line->text[x] = _nc_render(win, astr[i]);
190 if (len > 1) {
191 for (j = 0; j < len; ++j) {
192 if (j != 0) {
193 line->text[x + j] = line->text[x];
194 }
195 SetWidecExt(line->text[x + j], j);
196 }
197 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100198 x = (NCURSES_SIZE_T) (x + len);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530199 end += len - 1;
200 } else {
201 break;
202 }
203 }
204
205 /*
206 * Set orphaned cells of multi-column characters which lie after the new
207 * string to blanks.
208 */
209 while (x <= win->_maxx && isWidecExt(line->text[x])) {
210 line->text[x] = _nc_render(win, blank);
211 ++end;
212 ++x;
213 }
214 CHANGED_RANGE(line, start, end);
215
216 _nc_synchook(win);
217 returnCode(code);
218}
219
220NCURSES_EXPORT(int)
221waddnwstr(WINDOW *win, const wchar_t *str, int n)
222{
223 int code = ERR;
224
Steve Kondikae271bc2015-11-15 02:50:53 +0100225 T((T_CALLED("waddnwstr(%p,%s,%d)"), (void *) win, _nc_viswbufn(str, n), n));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530226
227 if (win && (str != 0)) {
228 TR(TRACE_VIRTPUT | TRACE_ATTRS,
229 ("... current %s", _traceattr(WINDOW_ATTRS(win))));
230 code = OK;
231 if (n < 0)
232 n = (int) wcslen(str);
233
234 TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
235 while ((n-- > 0) && (*str != L('\0'))) {
236 NCURSES_CH_T ch;
237 TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));
238 SetChar(ch, *str++, A_NORMAL);
239 if (wadd_wch(win, &ch) == ERR) {
240 code = ERR;
241 break;
242 }
243 }
244 _nc_synchook(win);
245 }
246 TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));
247 returnCode(code);
248}
249
250#endif