1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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 waddch().
36 #include <curses.priv.h>
39 MODULE_ID("$Id: lib_addch.c,v 1.124 2010/04/24 22:41:05 tom Exp $")
41 static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
44 * Ugly microtweaking alert. Everything from here to end of module is
45 * likely to be speed-critical -- profiling data sure says it is!
46 * Most of the important screen-painting functions are shells around
47 * waddch(). So we make every effort to reduce function-call overhead
48 * by inlining stuff, even at the cost of making wrapped copies for
49 * export. Also we supply some internal versions that don't call the
50 * window sync hook, for use by string-put functions.
53 /* Return bit mask for clearing color pair number if given ch has color */
54 #define COLOR_MASK(ch) (~(attr_t)((ch) & A_COLOR ? A_COLOR : 0))
56 static NCURSES_INLINE NCURSES_CH_T
57 render_char(WINDOW *win, NCURSES_CH_T ch)
58 /* compute a rendition of the given char correct for the current context */
60 attr_t a = WINDOW_ATTRS(win);
61 int pair = GetPair(ch);
64 && AttrOf(ch) == A_NORMAL
66 /* color/pair in attrs has precedence over bkgrnd */
68 SetAttr(ch, a | AttrOf(win->_nc_bkgd));
69 if ((pair = GET_WINDOW_PAIR(win)) == 0)
70 pair = GetPair(win->_nc_bkgd);
73 /* color in attrs has precedence over bkgrnd */
74 a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a);
75 /* color in ch has precedence */
77 if ((pair = GET_WINDOW_PAIR(win)) == 0)
78 pair = GetPair(win->_nc_bkgd);
80 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
85 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
86 _tracech_t2(1, CHREF(win->_nc_bkgd)),
87 GetPair(win->_nc_bkgd),
88 _traceattr(WINDOW_ATTRS(win)),
90 _tracech_t2(3, CHREF(ch)),
96 NCURSES_EXPORT(NCURSES_CH_T)
97 _nc_render(WINDOW *win, NCURSES_CH_T ch)
98 /* make render_char() visible while still allowing us to inline it below */
100 return render_char(win, ch);
103 /* check if position is legal; if not, return error */
104 #ifndef NDEBUG /* treat this like an assertion */
105 #define CHECK_POSITION(win, x, y) \
110 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
111 "(_maxx = %d, _maxy = %d)", win, x, y, \
112 win->_maxx, win->_maxy)); \
116 #define CHECK_POSITION(win, x, y) /* nothing */
120 newline_forces_scroll(WINDOW *win, NCURSES_SIZE_T * ypos)
124 if (*ypos >= win->_regtop && *ypos == win->_regbottom) {
125 *ypos = win->_regbottom;
128 *ypos = (NCURSES_SIZE_T) (*ypos + 1);
134 * The _WRAPPED flag is useful only for telling an application that we've just
135 * wrapped the cursor. We don't do anything with this flag except set it when
136 * wrapping, and clear it whenever we move the cursor. If we try to wrap at
137 * the lower-right corner of a window, we cannot move the cursor (since that
138 * wouldn't be legal). So we return an error (which is what SVr4 does).
139 * Unlike SVr4, we can successfully add a character to the lower-right corner
140 * (Solaris 2.6 does this also, however).
143 wrap_to_next_line(WINDOW *win)
145 win->_flags |= _WRAPPED;
146 if (newline_forces_scroll(win, &(win->_cury))) {
147 win->_curx = win->_maxx;
156 #if USE_WIDEC_SUPPORT
157 static int waddch_literal(WINDOW *, NCURSES_CH_T);
159 * Fill the given number of cells with blanks using the current background
160 * rendition. This saves/restores the current x-position.
163 fill_cells(WINDOW *win, int count)
165 NCURSES_CH_T blank = blankchar;
166 int save_x = win->_curx;
167 int save_y = win->_cury;
169 while (count-- > 0) {
170 if (waddch_literal(win, blank) == ERR)
173 win->_curx = (NCURSES_SIZE_T) save_x;
174 win->_cury = (NCURSES_SIZE_T) save_y;
179 * Build up the bytes for a multibyte character, returning the length when
180 * complete (a positive number), -1 for error and -2 for incomplete.
182 #if USE_WIDEC_SUPPORT
184 _nc_build_wch(WINDOW *win, ARG_CH_T ch)
186 char *buffer = WINDOW_EXT(win, addch_work);
193 if ((WINDOW_EXT(win, addch_used) != 0) &&
194 (WINDOW_EXT(win, addch_x) != x ||
195 WINDOW_EXT(win, addch_y) != y)) {
196 /* discard the incomplete multibyte character */
197 WINDOW_EXT(win, addch_used) = 0;
199 ("Alert discarded multibyte on move (%d,%d) -> (%d,%d)",
200 WINDOW_EXT(win, addch_y), WINDOW_EXT(win, addch_x),
203 WINDOW_EXT(win, addch_x) = x;
204 WINDOW_EXT(win, addch_y) = y;
207 buffer[WINDOW_EXT(win, addch_used)] = (char) CharOf(CHDEREF(ch));
208 WINDOW_EXT(win, addch_used) += 1;
209 buffer[WINDOW_EXT(win, addch_used)] = '\0';
210 if ((len = (int) mbrtowc(&result,
212 WINDOW_EXT(win, addch_used), &state)) > 0) {
213 attr_t attrs = AttrOf(CHDEREF(ch));
214 if_EXT_COLORS(int pair = GetPair(CHDEREF(ch)));
215 SetChar(CHDEREF(ch), result, attrs);
216 if_EXT_COLORS(SetPair(CHDEREF(ch), pair));
217 WINDOW_EXT(win, addch_used) = 0;
218 } else if (len == -1) {
220 * An error occurred. We could either discard everything,
221 * or assume that the error was in the previous input.
224 TR(TRACE_VIRTPUT, ("Alert! mbrtowc returns error"));
225 /* handle this with unctrl() */
226 WINDOW_EXT(win, addch_used) = 0;
230 #endif /* USE_WIDEC_SUPPORT */
233 #if !USE_WIDEC_SUPPORT /* cannot be inline if it is recursive */
237 waddch_literal(WINDOW *win, NCURSES_CH_T ch)
246 CHECK_POSITION(win, x, y);
248 ch = render_char(win, ch);
250 line = win->_line + y;
252 CHANGED_CELL(line, x);
255 * Build up multibyte characters until we have a wide-character.
258 #define DeriveSP() SCREEN *sp = _nc_screen_of(win);
260 #define DeriveSP() /*nothing */
264 if (WINDOW_EXT(win, addch_used) != 0 || !Charable(ch)) {
265 int len = _nc_build_wch(win, CHREF(ch));
268 attr_t attr = AttrOf(ch);
270 /* handle EILSEQ (i.e., when len >= -1) */
271 if (len == -1 && is8bits(CharOf(ch))) {
273 const char *s = NCURSES_SP_NAME(unctrl)
274 (NCURSES_SP_ARGx (chtype) CharOf(ch));
278 rc = waddch(win, UChar(*s) | attr);
287 return waddch(win, ' ' | attr);
295 * Non-spacing characters are added to the current cell.
297 * Spacing characters that are wider than one column require some display
301 int len = wcwidth(CharOf(ch));
306 if (len == 0) { /* non-spacing */
307 if ((x > 0 && y >= 0)
308 || (win->_maxx >= 0 && win->_cury >= 1)) {
310 chars = (win->_line[y].text[x - 1].chars);
312 chars = (win->_line[y - 1].text[win->_maxx].chars);
313 for (i = 0; i < CCHARW_MAX; ++i) {
316 ("added non-spacing %d: %x",
317 x, (int) CharOf(ch)));
318 chars[i] = CharOf(ch);
324 } else if (len > 1) { /* multi-column characters */
326 * Check if the character will fit on the current line. If it does
327 * not fit, fill in the remainder of the line with blanks. and
328 * move to the next line.
330 if (len > win->_maxx + 1) {
331 TR(TRACE_VIRTPUT, ("character will not fit"));
333 } else if (x + len > win->_maxx + 1) {
334 int count = win->_maxx + 1 - x;
335 TR(TRACE_VIRTPUT, ("fill %d remaining cells", count));
336 fill_cells(win, count);
337 if (wrap_to_next_line(win) == ERR)
341 line = win->_line + y;
344 * Check for cells which are orphaned by adding this character, set
347 * FIXME: this actually could fill j-i cells, more complicated to
350 for (i = 0; i < len; ++i) {
351 if (isWidecBase(win->_line[y].text[x + i])) {
353 } else if (isWidecExt(win->_line[y].text[x + i])) {
354 for (j = i; x + j <= win->_maxx; ++j) {
355 if (!isWidecExt(win->_line[y].text[x + j])) {
356 TR(TRACE_VIRTPUT, ("fill %d orphan cells", j));
365 * Finally, add the cells for this character.
367 for (i = 0; i < len; ++i) {
368 NCURSES_CH_T value = ch;
369 SetWidecExt(value, i);
370 TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)",
372 win->_begy + y, win->_begx + x));
373 line->text[x] = value;
374 CHANGED_CELL(line, x);
382 * Single-column characters.
384 line->text[x++] = ch;
386 * This label is used only for wide-characters.
392 TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s",
393 (long) win->_cury, (long) win->_curx, x - 1,
394 _tracech_t(CHREF(ch))));
396 if (x > win->_maxx) {
397 return wrap_to_next_line(win);
399 win->_curx = (NCURSES_SIZE_T) x;
403 static NCURSES_INLINE int
404 waddch_nosync(WINDOW *win, const NCURSES_CH_T ch)
405 /* the workhorse function -- add a character to the given window */
408 chtype t = (chtype) CharOf(ch);
409 #if USE_WIDEC_SUPPORT || NCURSES_SP_FUNCS || USE_REENTRANT
410 SCREEN *sp = _nc_screen_of(win);
412 const char *s = NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx t);
415 * If we are using the alternate character set, forget about locale.
416 * Otherwise, if unctrl() returns a single-character or the locale
417 * claims the code is printable, treat it that way.
419 if ((AttrOf(ch) & A_ALTCHARSET)
421 #if USE_WIDEC_SUPPORT
422 (sp != 0 && sp->_legacy_coding) &&
428 #if USE_WIDEC_SUPPORT
429 || ((sp == 0 || !sp->_legacy_coding) &&
430 (WINDOW_EXT(win, addch_used)
431 || !_nc_is_charable(CharOf(ch))))
434 return waddch_literal(win, ch);
437 * Handle carriage control and other codes that are not printable, or are
438 * known to expand to more than one character according to unctrl().
446 tabsize = *ptrTabsize(sp);
450 x = (NCURSES_SIZE_T) (x + (tabsize - (x % tabsize)));
452 * Space-fill the tab on the bottom line so that we'll get the
453 * "correct" cursor position.
455 if ((!win->_scroll && (y == win->_regbottom))
456 || (x <= win->_maxx)) {
457 NCURSES_CH_T blank = blankchar;
458 AddAttr(blank, AttrOf(ch));
459 while (win->_curx < x) {
460 if (waddch_literal(win, blank) == ERR)
466 win->_flags |= _WRAPPED;
467 if (newline_forces_scroll(win, &y)) {
480 if (newline_forces_scroll(win, &y)) {
489 win->_flags &= ~_WRAPPED;
495 win->_flags &= ~_WRAPPED;
500 SetChar(sch, *s++, AttrOf(ch));
501 if_EXT_COLORS(SetPair(sch, GetPair(ch)));
502 if (waddch_literal(win, sch) == ERR)
515 _nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c)
516 /* export copy of waddch_nosync() so the string-put functions can use it */
518 return (waddch_nosync(win, c));
522 * The versions below call _nc_synchook(). We wanted to avoid this in the
523 * version exported for string puts; they'll call _nc_synchook once at end
527 /* These are actual entry points */
530 waddch(WINDOW *win, const chtype ch)
536 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), (void *) win,
539 if (win && (waddch_nosync(win, wch) != ERR)) {
544 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
549 wechochar(WINDOW *win, const chtype ch)
555 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"),
559 if (win && (waddch_nosync(win, wch) != ERR)) {
560 bool save_immed = win->_immed;
563 win->_immed = save_immed;
566 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));