1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 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.68 2002/09/28 17:48:13 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, ("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 waddch_literal(WINDOW *win, NCURSES_CH_T ch)
110 CHECK_POSITION(win, x, win->_cury);
113 * If we're trying to add a character at the lower-right corner more
114 * than once, fail. (Moving the cursor will clear the flag).
116 #if 0 /* Solaris 2.6 allows updating the corner more than once */
117 if (win->_flags & _WRAPPED) {
120 win->_flags &= ~_WRAPPED;
124 ch = render_char(win, ch);
125 TR(TRACE_VIRTPUT, ("win attr = %s", _traceattr(win->_attrs)));
127 line = win->_line + win->_cury;
129 CHANGED_CELL(line, x);
132 * Handle non-spacing characters
135 if (wcwidth(CharOf(ch)) == 0) {
138 if ((x > 0 && ((y = win->_cury) >= 0))
139 || ((y = win->_cury - 1) >= 0 &&
140 (x = win->_maxx) > 0)) {
141 wchar_t *chars = (win->_line[y].text[x - 1].chars);
142 for (i = 0; i < CCHARW_MAX; ++i) {
144 chars[i] = CharOf(ch);
152 line->text[x++] = ch;
154 * Provide for multi-column characters
157 if (wcwidth(CharOf(ch)) > 1)
158 AddAttr(line->text[x++], WA_NAC);
163 TR(TRACE_VIRTPUT, ("(%d, %d) = %s", win->_cury, x, _tracech_t(CHREF(ch))));
164 if (x > win->_maxx) {
166 * The _WRAPPED flag is useful only for telling an application that
167 * we've just wrapped the cursor. We don't do anything with this flag
168 * except set it when wrapping, and clear it whenever we move the
169 * cursor. If we try to wrap at the lower-right corner of a window, we
170 * cannot move the cursor (since that wouldn't be legal). So we return
171 * an error (which is what SVr4 does). Unlike SVr4, we can
172 * successfully add a character to the lower-right corner (Solaris 2.6
173 * does this also, however).
175 win->_flags |= _WRAPPED;
176 if (++win->_cury > win->_regbottom) {
177 win->_cury = win->_regbottom;
178 win->_curx = win->_maxx;
191 waddch_nosync(WINDOW *win, const NCURSES_CH_T ch)
192 /* the workhorse function -- add a character to the given window */
198 if ((AttrOf(ch) & A_ALTCHARSET)
199 || ((t = CharOf(ch)) > 127)
200 || ((s = unctrl(t))[1] == 0))
201 return waddch_literal(win, ch);
208 x += (TABSIZE - (x % TABSIZE));
211 * Space-fill the tab on the bottom line so that we'll get the
212 * "correct" cursor position.
214 if ((!win->_scroll && (y == win->_regbottom))
215 || (x <= win->_maxx)) {
216 NCURSES_CH_T blank = NewChar2(BLANK_TEXT, BLANK_ATTR);
217 AddAttr(blank, AttrOf(ch));
218 while (win->_curx < x) {
219 if (waddch_literal(win, blank) == ERR)
225 win->_flags |= _WRAPPED;
226 if (++y > win->_regbottom) {
240 if (++y > win->_regbottom) {
250 win->_flags &= ~_WRAPPED;
256 win->_flags &= ~_WRAPPED;
261 SetChar(sch, *s++, AttrOf(ch));
262 if (waddch_literal(win, sch) == ERR)
275 _nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c)
276 /* export copy of waddch_nosync() so the string-put functions can use it */
278 return (waddch_nosync(win, c));
282 * The versions below call _nc_synhook(). We wanted to avoid this in the
283 * version exported for string puts; they'll call _nc_synchook once at end
287 /* These are actual entry points */
290 waddch(WINDOW *win, const chtype ch)
296 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), win,
299 if (win && (waddch_nosync(win, wch) != ERR)) {
304 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
309 wechochar(WINDOW *win, const chtype ch)
315 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), win,
318 if (win && (waddch_nosync(win, wch) != ERR)) {
319 bool save_immed = win->_immed;
322 win->_immed = save_immed;
325 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
329 #if USE_WIDEC_SUPPORT
331 wadd_wch(WINDOW *win, const cchar_t * wch)
335 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"), win,
338 if (win && (waddch_nosync(win, *wch) != ERR)) {
343 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
348 wecho_wchar(WINDOW *win, const cchar_t * wch)
352 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wecho_wchar(%p, %s)"), win,
355 if (win && (waddch_nosync(win, *wch) != ERR)) {
356 bool save_immed = win->_immed;
359 win->_immed = save_immed;
362 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
365 #endif /* USE_WIDEC_SUPPORT */