1 /****************************************************************************
2 * Copyright (c) 1998-2006,2008 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.111 2008/03/29 18:48:02 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);
82 NCURSES_CH_T fixme = ch;
86 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
91 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
92 _tracech_t2(1, CHREF(win->_nc_bkgd)),
93 GetPair(win->_nc_bkgd),
94 _traceattr(WINDOW_ATTRS(win)),
96 _tracech_t2(3, CHREF(ch)),
102 NCURSES_EXPORT(NCURSES_CH_T)
103 _nc_render(WINDOW *win, NCURSES_CH_T ch)
104 /* make render_char() visible while still allowing us to inline it below */
106 return render_char(win, ch);
109 /* check if position is legal; if not, return error */
110 #ifndef NDEBUG /* treat this like an assertion */
111 #define CHECK_POSITION(win, x, y) \
116 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
117 "(_maxx = %d, _maxy = %d)", win, x, y, \
118 win->_maxx, win->_maxy)); \
122 #define CHECK_POSITION(win, x, y) /* nothing */
126 newline_forces_scroll(WINDOW *win, NCURSES_SIZE_T * ypos)
130 if (*ypos >= win->_regtop && *ypos == win->_regbottom) {
131 *ypos = win->_regbottom;
140 * The _WRAPPED flag is useful only for telling an application that we've just
141 * wrapped the cursor. We don't do anything with this flag except set it when
142 * wrapping, and clear it whenever we move the cursor. If we try to wrap at
143 * the lower-right corner of a window, we cannot move the cursor (since that
144 * wouldn't be legal). So we return an error (which is what SVr4 does).
145 * Unlike SVr4, we can successfully add a character to the lower-right corner
146 * (Solaris 2.6 does this also, however).
149 wrap_to_next_line(WINDOW *win)
151 win->_flags |= _WRAPPED;
152 if (newline_forces_scroll(win, &(win->_cury))) {
153 win->_curx = win->_maxx;
162 #if USE_WIDEC_SUPPORT
163 static int waddch_literal(WINDOW *, NCURSES_CH_T);
165 * Fill the given number of cells with blanks using the current background
166 * rendition. This saves/restores the current x-position.
169 fill_cells(WINDOW *win, int count)
171 NCURSES_CH_T blank = blankchar;
172 int save_x = win->_curx;
173 int save_y = win->_cury;
175 while (count-- > 0) {
176 if (waddch_literal(win, blank) == ERR)
185 * Build up the bytes for a multibyte character, returning the length when
186 * complete (a positive number), -1 for error and -2 for incomplete.
188 #if USE_WIDEC_SUPPORT
190 _nc_build_wch(WINDOW *win, ARG_CH_T ch)
192 char *buffer = WINDOW_EXT(win, addch_work);
199 if ((WINDOW_EXT(win, addch_used) != 0) &&
200 (WINDOW_EXT(win, addch_x) != x ||
201 WINDOW_EXT(win, addch_y) != y)) {
202 /* discard the incomplete multibyte character */
203 WINDOW_EXT(win, addch_used) = 0;
205 ("Alert discarded multibyte on move (%d,%d) -> (%d,%d)",
206 WINDOW_EXT(win, addch_y), WINDOW_EXT(win, addch_x),
209 WINDOW_EXT(win, addch_x) = x;
210 WINDOW_EXT(win, addch_y) = y;
213 buffer[WINDOW_EXT(win, addch_used)] = CharOf(CHDEREF(ch));
214 WINDOW_EXT(win, addch_used) += 1;
215 buffer[WINDOW_EXT(win, addch_used)] = '\0';
216 if ((len = mbrtowc(&result,
218 WINDOW_EXT(win, addch_used), &state)) > 0) {
219 attr_t attrs = AttrOf(CHDEREF(ch));
220 if_EXT_COLORS(int pair = GetPair(CHDEREF(ch)));
221 SetChar(CHDEREF(ch), result, attrs);
222 if_EXT_COLORS(SetPair(CHDEREF(ch), pair));
223 WINDOW_EXT(win, addch_used) = 0;
224 } else if (len == -1) {
226 * An error occurred. We could either discard everything,
227 * or assume that the error was in the previous input.
230 TR(TRACE_VIRTPUT, ("Alert! mbrtowc returns error"));
231 /* handle this with unctrl() */
232 WINDOW_EXT(win, addch_used) = 0;
236 #endif /* USE_WIDEC_SUPPORT */
239 #if !USE_WIDEC_SUPPORT /* cannot be inline if it is recursive */
243 waddch_literal(WINDOW *win, NCURSES_CH_T ch)
252 CHECK_POSITION(win, x, y);
254 ch = render_char(win, ch);
256 line = win->_line + y;
258 CHANGED_CELL(line, x);
261 * Build up multibyte characters until we have a wide-character.
264 if (WINDOW_EXT(win, addch_used) != 0 || !Charable(ch)) {
265 int len = _nc_build_wch(win, CHREF(ch));
269 if (is8bits(CharOf(ch))) {
270 const char *s = unctrl((chtype) CharOf(ch));
272 return waddstr(win, s);
276 return waddch(win, ' ');
284 * Non-spacing characters are added to the current cell.
286 * Spacing characters that are wider than one column require some display
290 int len = wcwidth(CharOf(ch));
294 if (len == 0) { /* non-spacing */
295 if ((x > 0 && y >= 0)
296 || ((y = win->_cury - 1) >= 0 &&
297 (x = win->_maxx) > 0)) {
298 wchar_t *chars = (win->_line[y].text[x - 1].chars);
299 for (i = 0; i < CCHARW_MAX; ++i) {
302 ("added non-spacing %d: %x",
303 x, (int) CharOf(ch)));
304 chars[i] = CharOf(ch);
310 } else if (len > 1) { /* multi-column characters */
312 * Check if the character will fit on the current line. If it does
313 * not fit, fill in the remainder of the line with blanks. and
314 * move to the next line.
316 if (len > win->_maxx + 1) {
317 TR(TRACE_VIRTPUT, ("character will not fit"));
319 } else if (x + len > win->_maxx + 1) {
320 int count = win->_maxx + 1 - x;
321 TR(TRACE_VIRTPUT, ("fill %d remaining cells", count));
322 fill_cells(win, count);
323 if (wrap_to_next_line(win) == ERR)
329 * Check for cells which are orphaned by adding this character, set
332 * FIXME: this actually could fill j-i cells, more complicated to
335 for (i = 0; i < len; ++i) {
336 if (isWidecBase(win->_line[y].text[x + i])) {
338 } else if (isWidecExt(win->_line[y].text[x + i])) {
339 for (j = i; x + j <= win->_maxx; ++j) {
340 if (!isWidecExt(win->_line[y].text[x + j])) {
341 TR(TRACE_VIRTPUT, ("fill %d orphan cells", j));
350 * Finally, add the cells for this character.
352 for (i = 0; i < len; ++i) {
353 NCURSES_CH_T value = ch;
354 SetWidecExt(value, i);
355 TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)",
357 win->_begy + y, win->_begx + x));
358 line->text[x] = value;
359 CHANGED_CELL(line, x);
367 * Single-column characters.
369 line->text[x++] = ch;
371 * This label is used only for wide-characters.
377 TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s",
378 (long) win->_cury, (long) win->_curx, x - 1,
379 _tracech_t(CHREF(ch))));
381 if (x > win->_maxx) {
382 return wrap_to_next_line(win);
388 static NCURSES_INLINE int
389 waddch_nosync(WINDOW *win, const NCURSES_CH_T ch)
390 /* the workhorse function -- add a character to the given window */
393 chtype t = CharOf(ch);
394 const char *s = unctrl(t);
397 * If we are using the alternate character set, forget about locale.
398 * Otherwise, if unctrl() returns a single-character or the locale
399 * claims the code is printable, treat it that way.
401 if ((AttrOf(ch) & A_ALTCHARSET)
403 #if USE_WIDEC_SUPPORT
404 (SP != 0 && SP->_legacy_coding) &&
410 #if USE_WIDEC_SUPPORT
411 || ((SP == 0 || !SP->_legacy_coding) &&
412 (WINDOW_EXT(win, addch_used)
413 || !_nc_is_charable(CharOf(ch))))
416 return waddch_literal(win, ch);
419 * Handle carriage control and other codes that are not printable, or are
420 * known to expand to more than one character according to unctrl().
427 x += (TABSIZE - (x % TABSIZE));
430 * Space-fill the tab on the bottom line so that we'll get the
431 * "correct" cursor position.
433 if ((!win->_scroll && (y == win->_regbottom))
434 || (x <= win->_maxx)) {
435 NCURSES_CH_T blank = blankchar;
436 AddAttr(blank, AttrOf(ch));
437 while (win->_curx < x) {
438 if (waddch_literal(win, blank) == ERR)
444 win->_flags |= _WRAPPED;
445 if (newline_forces_scroll(win, &y)) {
458 if (newline_forces_scroll(win, &y)) {
467 win->_flags &= ~_WRAPPED;
473 win->_flags &= ~_WRAPPED;
478 SetChar(sch, *s++, AttrOf(ch));
479 if_EXT_COLORS(SetPair(sch, GetPair(ch)));
480 if (waddch_literal(win, sch) == ERR)
493 _nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c)
494 /* export copy of waddch_nosync() so the string-put functions can use it */
496 return (waddch_nosync(win, c));
500 * The versions below call _nc_synchook(). We wanted to avoid this in the
501 * version exported for string puts; they'll call _nc_synchook once at end
505 /* These are actual entry points */
508 waddch(WINDOW *win, const chtype ch)
514 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), win,
517 if (win && (waddch_nosync(win, wch) != ERR)) {
522 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
527 wechochar(WINDOW *win, const chtype ch)
533 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), win,
536 if (win && (waddch_nosync(win, wch) != ERR)) {
537 bool save_immed = win->_immed;
540 win->_immed = save_immed;
543 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));