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 ****************************************************************************/
32 ** The routine waddch().
36 #include <curses.priv.h>
39 MODULE_ID("$Id: lib_addch.c,v 1.80 2004/02/07 18:20:46 tom Exp $")
42 * Ugly microtweaking alert. Everything from here to end of module is
43 * likely to be speed-critical -- profiling data sure says it is!
44 * Most of the important screen-painting functions are shells around
45 * waddch(). So we make every effort to reduce function-call overhead
46 * by inlining stuff, even at the cost of making wrapped copies for
47 * export. Also we supply some internal versions that don't call the
48 * window sync hook, for use by string-put functions.
51 /* Return bit mask for clearing color pair number if given ch has color */
52 #define COLOR_MASK(ch) (~(attr_t)((ch)&A_COLOR?A_COLOR:0))
54 static inline NCURSES_CH_T
55 render_char(WINDOW *win, NCURSES_CH_T ch)
56 /* compute a rendition of the given char correct for the current context */
58 attr_t a = win->_attrs;
60 if (ISBLANK(ch) && AttrOf(ch) == A_NORMAL) {
61 /* color in attrs has precedence over bkgrnd */
63 SetAttr(ch, a | (AttrOf(win->_nc_bkgd) & COLOR_MASK(a)));
65 /* color in attrs has precedence over bkgrnd */
66 a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a);
67 /* color in ch has precedence */
68 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
71 TR(TRACE_VIRTPUT, ("render_char bkg %s, attrs %s -> ch %s",
72 _tracech_t2(1, CHREF(win->_nc_bkgd)),
73 _traceattr(win->_attrs),
74 _tracech_t2(3, CHREF(ch))));
79 NCURSES_EXPORT(NCURSES_CH_T)
80 _nc_render(WINDOW *win, NCURSES_CH_T ch)
81 /* make render_char() visible while still allowing us to inline it below */
83 return render_char(win, ch);
86 /* check if position is legal; if not, return error */
87 #ifndef NDEBUG /* treat this like an assertion */
88 #define CHECK_POSITION(win, x, y) \
93 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
94 "(_maxx = %d, _maxy = %d)", win, x, y, \
95 win->_maxx, win->_maxy)); \
99 #define CHECK_POSITION(win, x, y) /* nothing */
103 #if !USE_WIDEC_SUPPORT /* cannot be inline if it is recursive */
107 waddch_literal(WINDOW *win, NCURSES_CH_T ch)
116 CHECK_POSITION(win, x, y);
119 * If we're trying to add a character at the lower-right corner more
120 * than once, fail. (Moving the cursor will clear the flag).
122 #if 0 /* Solaris 2.6 allows updating the corner more than once */
123 if (win->_flags & _WRAPPED) {
126 win->_flags &= ~_WRAPPED;
130 ch = render_char(win, ch);
132 line = win->_line + y;
134 CHANGED_CELL(line, x);
137 * Build up multibyte characters until we have a wide-character.
140 if (WINDOW_EXT(win, addch_used) == 0 && Charable(ch)) {
141 WINDOW_EXT(win, addch_used) = 0;
143 char *buffer = WINDOW_EXT(win, addch_work);
148 if ((WINDOW_EXT(win, addch_used) != 0) &&
149 (WINDOW_EXT(win, addch_x) != x ||
150 WINDOW_EXT(win, addch_y) != y)) {
151 /* discard the incomplete multibyte character */
152 WINDOW_EXT(win, addch_used) = 0;
154 WINDOW_EXT(win, addch_x) = x;
155 WINDOW_EXT(win, addch_y) = y;
157 memset(&state, 0, sizeof(state));
158 buffer[WINDOW_EXT(win, addch_used)] = CharOf(ch);
159 WINDOW_EXT(win, addch_used) += 1;
160 buffer[WINDOW_EXT(win, addch_used)] = '\0';
161 if ((len = mbrtowc(&result,
163 WINDOW_EXT(win, addch_used), &state)) > 0) {
164 attr_t attrs = AttrOf(ch);
165 SetChar(ch, result, attrs);
166 WINDOW_EXT(win, addch_used) = 0;
167 if (CharOf(ch) < 256) {
168 const char *s = unctrl(CharOf(ch));
170 return waddstr(win, s);
176 * An error occurred. We could either discard everything,
177 * or assume that the error was in the previous input.
180 TR(TRACE_VIRTPUT, ("Alert! mbrtowc returns error"));
181 buffer[0] = CharOf(ch);
182 WINDOW_EXT(win, addch_used) = 1;
190 * Handle non-spacing characters
193 if (wcwidth(CharOf(ch)) == 0) {
195 if ((x > 0 && y >= 0)
196 || ((y = win->_cury - 1) >= 0 &&
197 (x = win->_maxx) > 0)) {
198 wchar_t *chars = (win->_line[y].text[x - 1].chars);
199 for (i = 0; i < CCHARW_MAX; ++i) {
201 chars[i] = CharOf(ch);
209 line->text[x++] = ch;
211 * Provide for multi-column characters
214 int len = wcwidth(CharOf(ch));
216 if (x + (len - 1) > win->_maxx) {
217 NCURSES_CH_T blank = NewChar2(BLANK_TEXT, BLANK_ATTR);
218 AddAttr(blank, AttrOf(ch));
219 if (waddch_literal(win, blank) != ERR)
220 return waddch_literal(win, ch);
223 AddAttr(line->text[x++], WA_NAC);
224 TR(TRACE_VIRTPUT, ("added NAC %d", x - 1));
230 TR(TRACE_VIRTPUT, ("(%d, %d) = %s", win->_cury, x, _tracech_t(CHREF(ch))));
231 if (x > win->_maxx) {
233 * The _WRAPPED flag is useful only for telling an application that
234 * we've just wrapped the cursor. We don't do anything with this flag
235 * except set it when wrapping, and clear it whenever we move the
236 * cursor. If we try to wrap at the lower-right corner of a window, we
237 * cannot move the cursor (since that wouldn't be legal). So we return
238 * an error (which is what SVr4 does). Unlike SVr4, we can
239 * successfully add a character to the lower-right corner (Solaris 2.6
240 * does this also, however).
242 win->_flags |= _WRAPPED;
243 if (++win->_cury > win->_regbottom) {
244 win->_cury = win->_regbottom;
245 win->_curx = win->_maxx;
258 waddch_nosync(WINDOW *win, const NCURSES_CH_T ch)
259 /* the workhorse function -- add a character to the given window */
262 chtype t = CharOf(ch);
266 * If we are using the alternate character set, forget about locale.
267 * Otherwise, if unctrl() returns a single-character or the locale
268 * claims the code is printable, treat it that way.
270 if ((AttrOf(ch) & A_ALTCHARSET)
271 || ((s = unctrl(t))[1] == 0 ||
274 #if USE_WIDEC_SUPPORT
275 || WINDOW_EXT(win, addch_used)
278 return waddch_literal(win, ch);
281 * Handle carriage control and other codes that are not printable, or are
282 * known to expand to more than one character according to unctrl().
289 x += (TABSIZE - (x % TABSIZE));
292 * Space-fill the tab on the bottom line so that we'll get the
293 * "correct" cursor position.
295 if ((!win->_scroll && (y == win->_regbottom))
296 || (x <= win->_maxx)) {
297 NCURSES_CH_T blank = NewChar2(BLANK_TEXT, BLANK_ATTR);
298 AddAttr(blank, AttrOf(ch));
299 while (win->_curx < x) {
300 if (waddch_literal(win, blank) == ERR)
306 win->_flags |= _WRAPPED;
307 if (++y > win->_regbottom) {
321 if (++y > win->_regbottom) {
331 win->_flags &= ~_WRAPPED;
337 win->_flags &= ~_WRAPPED;
342 SetChar(sch, *s++, AttrOf(ch));
343 if (waddch_literal(win, sch) == ERR)
356 _nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c)
357 /* export copy of waddch_nosync() so the string-put functions can use it */
359 return (waddch_nosync(win, c));
363 * The versions below call _nc_synhook(). We wanted to avoid this in the
364 * version exported for string puts; they'll call _nc_synchook once at end
368 /* These are actual entry points */
371 waddch(WINDOW *win, const chtype ch)
377 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), win,
380 if (win && (waddch_nosync(win, wch) != ERR)) {
385 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
390 wechochar(WINDOW *win, const chtype ch)
396 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), win,
399 if (win && (waddch_nosync(win, wch) != ERR)) {
400 bool save_immed = win->_immed;
403 win->_immed = save_immed;
406 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));