1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
37 ** The routines waddnstr(), waddchnstr().
41 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_addstr.c,v 1.34 2002/10/06 00:25:25 tom Exp $")
46 #define CONV_DATA mbstate_t state; wchar_t cached; int clen = 0
47 #define CONV_INIT memset (&state, '\0', sizeof (state)); cached = (wchar_t)WEOF
48 #define NEXT_CHAR(s,ch, n) \
51 memset(&ch, 0, sizeof(cchar_t)); \
52 if (cached != (wchar_t) WEOF) { \
53 ch.chars[i++] = cached; \
54 cached = (wchar_t) WEOF; \
58 for (; i < CCHARW_MAX && n > 0; ++i) { \
59 if ((len = mbrtowc(&ch.chars[i], s, n, &state)) < 0) { \
63 if (i == 0 || wcwidth(ch.chars[i]) == 0) { \
67 cached = ch.chars[i]; \
69 ch.chars[i] = L'\0'; \
79 #define NEXT_CHAR(s,ch, n) \
85 waddnstr(WINDOW *win, const char *astr, int n)
87 unsigned const char *str = (unsigned const char *) astr;
91 T((T_CALLED("waddnstr(%p,%s,%d)"), win, _nc_visbufn(astr, n), n));
93 if (win && (str != 0)) {
94 TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
97 n = (int) strlen(astr);
99 TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
101 while ((n > 0) && (*str != '\0')) {
103 TR(TRACE_VIRTPUT, ("*str = %#x", *str));
104 NEXT_CHAR(str, ch, n);
105 if (_nc_waddch_nosync(win, ch) == ERR) {
112 TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
117 waddchnstr(WINDOW *win, const chtype * astr, int n)
119 NCURSES_SIZE_T y = win->_cury;
120 NCURSES_SIZE_T x = win->_curx;
124 T((T_CALLED("waddchnstr(%p,%p,%d)"), win, astr, n));
132 for (str = (const chtype *) astr; *str != 0; str++)
135 if (n > win->_maxx - x + 1)
136 n = win->_maxx - x + 1;
140 line = &(win->_line[y]);
141 #if USE_WIDEC_SUPPORT
144 for (i = 0; i < n; ++i)
145 SetChar(line->text[i + x], ChCharOf(astr[i]), ChAttrOf(astr[i]));
148 memcpy(line->text + x, astr, n * sizeof(*astr));
150 CHANGED_RANGE(line, x, x + n - 1);
156 #if USE_WIDEC_SUPPORT
159 _nc_wchstrlen(const cchar_t * s)
162 while (CharOf(s[result]) != L'\0') {
169 wadd_wchnstr(WINDOW *win, const cchar_t * const astr, int n)
171 NCURSES_SIZE_T y = win->_cury;
172 NCURSES_SIZE_T x = win->_curx;
177 T((T_CALLED("wadd_wchnstr(%p,%s,%d)"), win, _nc_viscbuf(astr, n), n));
183 n = _nc_wchstrlen(astr);
185 if (n > win->_maxx - x + 1)
186 n = win->_maxx - x + 1;
190 line = &(win->_line[y]);
193 if (isnac(line->text[x])) {
194 line->text[x - 1] = win->_nc_bkgd;
197 for (i = 0; i < n && x <= win->_maxx; ++i) {
198 line->text[x++] = astr[i];
199 if (wcwidth(CharOf(astr[i])) > 1) {
201 AddAttr(line->text[x++], WA_NAC);
203 line->text[x - 1] = win->_nc_bkgd;
206 if (x <= win->_maxx && isnac(line->text[x])) {
207 line->text[x] = win->_nc_bkgd;
210 CHANGED_RANGE(line, start, end);
217 waddnwstr(WINDOW *win, const wchar_t * str, int n)
222 T((T_CALLED("waddnwstr(%p,%s,%d)"), win, _nc_viswbufn(str,n), n));
224 if (win && (str != 0)) {
225 TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
228 n = (int) wcslen(str);
230 TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
231 while ((n-- > 0) && (*str != L('\0'))) {
233 TR(TRACE_VIRTPUT, ("*str[0] = %#lx", *str));
234 SetChar(ch, *str++, A_NORMAL);
236 while (i < CCHARW_MAX && n > 0 && (*str != L('\0'))
237 && wcwidth(*str) == 0) {
238 TR(TRACE_VIRTPUT, ("*str[%d] = %#lx", i, *str));
239 ch.chars[i++] = *str++;
242 if (_nc_waddch_nosync(win, ch) == ERR) {
249 TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));