]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_addstr.c
ncurses 5.5
[ncurses.git] / ncurses / base / lib_addstr.c
1 /****************************************************************************
2  * Copyright (c) 1998-2003,2004 Free Software Foundation, Inc.              *
3  *                                                                          *
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
47 MODULE_ID("$Id: lib_addstr.c,v 1.43 2004/12/12 00:15:31 tom Exp $")
48
49 NCURSES_EXPORT(int)
50 waddnstr(WINDOW *win, const char *astr, int n)
51 {
52     const char *str = astr;
53     int code = ERR;
54
55     T((T_CALLED("waddnstr(%p,%s,%d)"), win, _nc_visbufn(astr, n), n));
56
57     if (win && (str != 0)) {
58         TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
59         code = OK;
60         if (n < 0)
61             n = (int) strlen(astr);
62
63         TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
64         while ((n-- > 0) && (*str != '\0')) {
65             NCURSES_CH_T ch;
66             TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
67             SetChar(ch, UChar(*str++), A_NORMAL);
68             if (_nc_waddch_nosync(win, ch) == ERR) {
69                 code = ERR;
70                 break;
71             }
72         }
73         _nc_synchook(win);
74     }
75     TR(TRACE_VIRTPUT, ("waddnstr returns %d", code));
76     returnCode(code);
77 }
78
79 NCURSES_EXPORT(int)
80 waddchnstr(WINDOW *win, const chtype *astr, int n)
81 {
82     NCURSES_SIZE_T y = win->_cury;
83     NCURSES_SIZE_T x = win->_curx;
84     int code = OK;
85     struct ldat *line;
86
87     T((T_CALLED("waddchnstr(%p,%p,%d)"), win, astr, n));
88
89     if (!win)
90         returnCode(ERR);
91
92     if (n < 0) {
93         const chtype *str;
94         n = 0;
95         for (str = (const chtype *) astr; *str != 0; str++)
96             n++;
97     }
98     if (n > win->_maxx - x + 1)
99         n = win->_maxx - x + 1;
100     if (n == 0)
101         returnCode(code);
102
103     line = &(win->_line[y]);
104 #if USE_WIDEC_SUPPORT
105     {
106         int i;
107         for (i = 0; i < n; ++i)
108             SetChar(line->text[i + x], ChCharOf(astr[i]), ChAttrOf(astr[i]));
109     }
110 #else
111     memcpy(line->text + x, astr, n * sizeof(*astr));
112 #endif
113     CHANGED_RANGE(line, x, x + n - 1);
114
115     _nc_synchook(win);
116     returnCode(code);
117 }
118
119 #if USE_WIDEC_SUPPORT
120
121 NCURSES_EXPORT(int)
122 _nc_wchstrlen(const cchar_t *s)
123 {
124     int result = 0;
125     while (CharOf(s[result]) != L'\0') {
126         result++;
127     }
128     return result;
129 }
130
131 NCURSES_EXPORT(int)
132 wadd_wchnstr(WINDOW *win, const cchar_t *astr, int n)
133 {
134     NCURSES_CH_T blank = NewChar(BLANK_TEXT);
135     NCURSES_SIZE_T y = win->_cury;
136     NCURSES_SIZE_T x = win->_curx;
137     int code = OK;
138     struct ldat *line;
139     int i, j, start, len, end;
140
141     T((T_CALLED("wadd_wchnstr(%p,%s,%d)"), win, _nc_viscbuf(astr, n), n));
142
143     if (!win)
144         returnCode(ERR);
145
146     if (n < 0) {
147         n = _nc_wchstrlen(astr);
148     }
149     if (n > win->_maxx - x + 1)
150         n = win->_maxx - x + 1;
151     if (n == 0)
152         returnCode(code);
153
154     line = &(win->_line[y]);
155     start = x;
156     end = x + n - 1;
157
158     /*
159      * Reset orphaned cells of multi-column characters that extend up to the
160      * new string's location to blanks.
161      */
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() */
166                 start -= i;
167                 while (i > 0) {
168                     line->text[x - i--] = _nc_render(win, blank);
169                 }
170                 break;
171             }
172         }
173     }
174
175     /*
176      * Copy the new string to the window.
177      */
178     for (i = 0; i < n && x <= win->_maxx; ++i) {
179         if (isWidecExt(astr[i]))
180             continue;
181
182         len = wcwidth(CharOf(astr[i]));
183
184         if (x + len - 1 <= win->_maxx) {
185             line->text[x] = _nc_render(win, astr[i]);
186             if (len > 1) {
187                 for (j = 0; j < len; ++j) {
188                     if (j != 0) {
189                         line->text[x + j] = line->text[x];
190                     }
191                     SetWidecExt(line->text[x + j], j);
192                 }
193             }
194             x += len;
195             end += len - 1;
196         } else {
197             break;
198         }
199     }
200
201     /*
202      * Set orphaned cells of multi-column characters which lie after the new
203      * string to blanks.
204      */
205     while (x <= win->_maxx && isWidecExt(line->text[x])) {
206         line->text[x] = _nc_render(win, blank);
207         ++end;
208         ++x;
209     }
210     CHANGED_RANGE(line, start, end);
211
212     _nc_synchook(win);
213     returnCode(code);
214 }
215
216 NCURSES_EXPORT(int)
217 waddnwstr(WINDOW *win, const wchar_t *str, int n)
218 {
219     int code = ERR;
220
221     T((T_CALLED("waddnwstr(%p,%s,%d)"), win, _nc_viswbufn(str, n), n));
222
223     if (win && (str != 0)) {
224         TR(TRACE_VIRTPUT | TRACE_ATTRS, ("... current %s", _traceattr(win->_attrs)));
225         code = OK;
226         if (n < 0)
227             n = (int) wcslen(str);
228
229         TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
230         while ((n-- > 0) && (*str != L('\0'))) {
231             NCURSES_CH_T ch;
232             TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));
233             SetChar(ch, *str++, A_NORMAL);
234             if (wadd_wch(win, &ch) == ERR) {
235                 code = ERR;
236                 break;
237             }
238         }
239         _nc_synchook(win);
240     }
241     TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code));
242     returnCode(code);
243 }
244
245 #endif