1 /****************************************************************************
2 * Copyright (c) 2004-2006,2009 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 ****************************************************************************/
32 ** The routine wadd_wch().
36 #include <curses.priv.h>
42 MODULE_ID("$Id: lib_add_wch.c,v 1.9 2009/10/24 22:45:42 tom Exp $")
44 /* clone/adapt lib_addch.c */
45 static const cchar_t blankchar = NewChar(BLANK_TEXT);
48 * Ugly microtweaking alert. Everything from here to end of module is
49 * likely to be speed-critical -- profiling data sure says it is!
50 * Most of the important screen-painting functions are shells around
51 * wadd_wch(). So we make every effort to reduce function-call overhead
52 * by inlining stuff, even at the cost of making wrapped copies for
53 * export. Also we supply some internal versions that don't call the
54 * window sync hook, for use by string-put functions.
57 /* Return bit mask for clearing color pair number if given ch has color */
58 #define COLOR_MASK(ch) (~(attr_t)((ch) & A_COLOR ? A_COLOR : 0))
60 static NCURSES_INLINE cchar_t
61 render_char(WINDOW *win, cchar_t ch)
62 /* compute a rendition of the given char correct for the current context */
64 attr_t a = WINDOW_ATTRS(win);
65 int pair = GetPair(ch);
68 && AttrOf(ch) == A_NORMAL
70 /* color/pair in attrs has precedence over bkgrnd */
72 SetAttr(ch, a | AttrOf(win->_nc_bkgd));
73 if ((pair = GET_WINDOW_PAIR(win)) == 0)
74 pair = GetPair(win->_nc_bkgd);
77 /* color in attrs has precedence over bkgrnd */
78 a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a);
79 /* color in ch has precedence */
81 if ((pair = GET_WINDOW_PAIR(win)) == 0)
82 pair = GetPair(win->_nc_bkgd);
90 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
95 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
96 _tracech_t2(1, CHREF(win->_nc_bkgd)),
97 GetPair(win->_nc_bkgd),
98 _traceattr(WINDOW_ATTRS(win)),
100 _tracech_t2(3, CHREF(ch)),
106 /* check if position is legal; if not, return error */
107 #ifndef NDEBUG /* treat this like an assertion */
108 #define CHECK_POSITION(win, x, y) \
113 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
114 "(_maxx = %d, _maxy = %d)", win, x, y, \
115 win->_maxx, win->_maxy)); \
119 #define CHECK_POSITION(win, x, y) /* nothing */
123 newline_forces_scroll(WINDOW *win, NCURSES_SIZE_T * ypos)
127 if (*ypos >= win->_regtop && *ypos == win->_regbottom) {
128 *ypos = win->_regbottom;
137 * The _WRAPPED flag is useful only for telling an application that we've just
138 * wrapped the cursor. We don't do anything with this flag except set it when
139 * wrapping, and clear it whenever we move the cursor. If we try to wrap at
140 * the lower-right corner of a window, we cannot move the cursor (since that
141 * wouldn't be legal). So we return an error (which is what SVr4 does).
142 * Unlike SVr4, we can successfully add a character to the lower-right corner
143 * (Solaris 2.6 does this also, however).
146 wrap_to_next_line(WINDOW *win)
148 win->_flags |= _WRAPPED;
149 if (newline_forces_scroll(win, &(win->_cury))) {
150 win->_curx = win->_maxx;
159 static int wadd_wch_literal(WINDOW *, cchar_t);
161 * Fill the given number of cells with blanks using the current background
162 * rendition. This saves/restores the current x-position.
165 fill_cells(WINDOW *win, int count)
167 cchar_t blank = blankchar;
168 int save_x = win->_curx;
169 int save_y = win->_cury;
171 while (count-- > 0) {
172 if (wadd_wch_literal(win, blank) == ERR)
180 wadd_wch_literal(WINDOW *win, cchar_t ch)
189 CHECK_POSITION(win, x, y);
191 ch = render_char(win, ch);
193 line = win->_line + y;
195 CHANGED_CELL(line, x);
198 * Non-spacing characters are added to the current cell.
200 * Spacing characters that are wider than one column require some display
204 int len = wcwidth(CharOf(ch));
209 if (len == 0) { /* non-spacing */
210 if ((x > 0 && y >= 0)
211 || (win->_maxx >= 0 && win->_cury >= 1)) {
213 chars = (win->_line[y].text[x - 1].chars);
215 chars = (win->_line[y - 1].text[win->_maxx].chars);
216 for (i = 0; i < CCHARW_MAX; ++i) {
219 ("added non-spacing %d: %x",
220 x, (int) CharOf(ch)));
221 chars[i] = CharOf(ch);
227 } else if (len > 1) { /* multi-column characters */
229 * Check if the character will fit on the current line. If it does
230 * not fit, fill in the remainder of the line with blanks. and
231 * move to the next line.
233 if (len > win->_maxx + 1) {
234 TR(TRACE_VIRTPUT, ("character will not fit"));
236 } else if (x + len > win->_maxx + 1) {
237 int count = win->_maxx + 1 - x;
238 TR(TRACE_VIRTPUT, ("fill %d remaining cells", count));
239 fill_cells(win, count);
240 if (wrap_to_next_line(win) == ERR)
244 line = win->_line + y;
247 * Check for cells which are orphaned by adding this character, set
250 * FIXME: this actually could fill j-i cells, more complicated to
253 for (i = 0; i < len; ++i) {
254 if (isWidecBase(win->_line[y].text[x + i])) {
256 } else if (isWidecExt(win->_line[y].text[x + i])) {
257 for (j = i; x + j <= win->_maxx; ++j) {
258 if (!isWidecExt(win->_line[y].text[x + j])) {
259 TR(TRACE_VIRTPUT, ("fill %d orphan cells", j));
268 * Finally, add the cells for this character.
270 for (i = 0; i < len; ++i) {
272 SetWidecExt(value, i);
273 TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)",
275 win->_begy + y, win->_begx + x));
276 line->text[x] = value;
277 CHANGED_CELL(line, x);
285 * Single-column characters.
287 line->text[x++] = ch;
289 * This label is used only for wide-characters.
293 TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s",
294 (long) win->_cury, (long) win->_curx, x - 1,
295 _tracech_t(CHREF(ch))));
297 if (x > win->_maxx) {
298 return wrap_to_next_line(win);
304 static NCURSES_INLINE int
305 wadd_wch_nosync(WINDOW *win, cchar_t ch)
306 /* the workhorse function -- add a character to the given window */
312 SCREEN *sp = _nc_screen_of(win);
316 * If we are using the alternate character set, forget about locale.
317 * Otherwise, if the locale * claims the code is printable, treat it that
320 if ((AttrOf(ch) & A_ALTCHARSET)
321 || iswprint(CharOf(ch)))
322 return wadd_wch_literal(win, ch);
325 * Handle carriage control and other codes that are not printable, or are
326 * known to expand to more than one character according to unctrl().
331 switch (CharOf(ch)) {
334 tabsize = *ptrTabsize(sp);
338 x += (tabsize - (x % tabsize));
340 * Space-fill the tab on the bottom line so that we'll get the
341 * "correct" cursor position.
343 if ((!win->_scroll && (y == win->_regbottom))
344 || (x <= win->_maxx)) {
345 cchar_t blank = blankchar;
346 AddAttr(blank, AttrOf(ch));
347 while (win->_curx < x) {
348 if (wadd_wch_literal(win, blank) == ERR)
354 win->_flags |= _WRAPPED;
355 if (newline_forces_scroll(win, &y)) {
368 if (newline_forces_scroll(win, &y)) {
377 win->_flags &= ~_WRAPPED;
383 win->_flags &= ~_WRAPPED;
386 if ((s = wunctrl(&ch)) != 0) {
389 SetChar(sch, *s++, AttrOf(ch));
390 if_EXT_COLORS(SetPair(sch, GetPair(ch)));
391 if (wadd_wch_literal(win, sch) == ERR)
406 * The versions below call _nc_synchook(). We wanted to avoid this in the
407 * version exported for string puts; they'll call _nc_synchook once at end
411 /* These are actual entry points */
414 wadd_wch(WINDOW *win, const cchar_t *wch)
418 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"),
420 _tracecchar_t(wch)));
422 if (win && (wadd_wch_nosync(win, *wch) != ERR)) {
427 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
432 wecho_wchar(WINDOW *win, const cchar_t *wch)
436 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"),
438 _tracecchar_t(wch)));
440 if (win && (wadd_wch_nosync(win, *wch) != ERR)) {
441 bool save_immed = win->_immed;
444 win->_immed = save_immed;
447 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));