1 /****************************************************************************
2 * Copyright (c) 1998-2003,2004 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> *
33 * Rewritten 2001-2004 to support wide-characters by *
36 ****************************************************************************/
41 ** The routines waddnstr(), waddchnstr().
45 #include <curses.priv.h>
47 MODULE_ID("$Id: lib_addstr.c,v 1.43 2004/12/12 00:15:31 tom Exp $")
50 waddnstr(WINDOW *win, const char *astr, int n)
52 const char *str = astr;
55 T((T_CALLED("waddnstr(%p,%s,%d)"), win, _nc_visbufn(astr, n), n));
57 if (win && (str != 0)) {
58 TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
61 n = (int) strlen(astr);
63 TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
64 while ((n-- > 0) && (*str != '\0')) {
66 TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
67 SetChar(ch, UChar(*str++), A_NORMAL);
68 if (_nc_waddch_nosync(win, ch) == ERR) {
75 TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
80 waddchnstr(WINDOW *win, const chtype *astr, int n)
82 NCURSES_SIZE_T y = win->_cury;
83 NCURSES_SIZE_T x = win->_curx;
87 T((T_CALLED("waddchnstr(%p,%p,%d)"), win, astr, n));
95 for (str = (const chtype *) astr; *str != 0; str++)
98 if (n > win->_maxx - x + 1)
99 n = win->_maxx - x + 1;
103 line = &(win->_line[y]);
104 #if USE_WIDEC_SUPPORT
107 for (i = 0; i < n; ++i)
108 SetChar(line->text[i + x], ChCharOf(astr[i]), ChAttrOf(astr[i]));
111 memcpy(line->text + x, astr, n * sizeof(*astr));
113 CHANGED_RANGE(line, x, x + n - 1);
119 #if USE_WIDEC_SUPPORT
122 _nc_wchstrlen(const cchar_t *s)
125 while (CharOf(s[result]) != L'\0') {
132 wadd_wchnstr(WINDOW *win, const cchar_t *astr, int n)
134 NCURSES_CH_T blank = NewChar(BLANK_TEXT);
135 NCURSES_SIZE_T y = win->_cury;
136 NCURSES_SIZE_T x = win->_curx;
139 int i, j, start, len, end;
141 T((T_CALLED("wadd_wchnstr(%p,%s,%d)"), win, _nc_viscbuf(astr, n), n));
147 n = _nc_wchstrlen(astr);
149 if (n > win->_maxx - x + 1)
150 n = win->_maxx - x + 1;
154 line = &(win->_line[y]);
159 * Reset orphaned cells of multi-column characters that extend up to the
160 * new string's location to blanks.
162 if (x > 0 && isWidecExt(line->text[x])) {
163 for (i = 0; i <= x; ++i) {
164 if (!isWidecExt(line->text[x - i])) {
165 /* must be isWidecBase() */
168 line->text[x - i--] = _nc_render(win, blank);
176 * Copy the new string to the window.
178 for (i = 0; i < n && x <= win->_maxx; ++i) {
179 if (isWidecExt(astr[i]))
182 len = wcwidth(CharOf(astr[i]));
184 if (x + len - 1 <= win->_maxx) {
185 line->text[x] = _nc_render(win, astr[i]);
187 for (j = 0; j < len; ++j) {
189 line->text[x + j] = line->text[x];
191 SetWidecExt(line->text[x + j], j);
202 * Set orphaned cells of multi-column characters which lie after the new
205 while (x <= win->_maxx && isWidecExt(line->text[x])) {
206 line->text[x] = _nc_render(win, blank);
210 CHANGED_RANGE(line, start, end);
217 waddnwstr(WINDOW *win, const wchar_t *str, int n)
221 T((T_CALLED("waddnwstr(%p,%s,%d)"), win, _nc_viswbufn(str, n), n));
223 if (win && (str != 0)) {
224 TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
227 n = (int) wcslen(str);
229 TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
230 while ((n-- > 0) && (*str != L('\0'))) {
232 TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));
233 SetChar(ch, *str++, A_NORMAL);
234 if (wadd_wch(win, &ch) == ERR) {
241 TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));