lib_addch.c revision 9a7523d4b2c09cefef7372e8a011b8d54fb07e5e
1/****************************************************************************
2 * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
3 *                                                                          *
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:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
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.                               *
22 *                                                                          *
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       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/*
30**	lib_addch.c
31**
32**	The routine waddch().
33**
34*/
35
36#include <curses.priv.h>
37#include <ctype.h>
38
39MODULE_ID("$Id: lib_addch.c,v 1.64 2002/05/11 18:16:43 tom Exp $")
40
41/*
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.
49 */
50
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))
53
54static inline NCURSES_CH_T
55render_char(WINDOW *win, NCURSES_CH_T ch)
56/* compute a rendition of the given char correct for the current context */
57{
58    attr_t a = win->_attrs;
59
60    if (ISBLANK(ch) && AttrOf(ch) == A_NORMAL) {
61	/* color in attrs has precedence over bkgrnd */
62	ch = win->_nc_bkgd;
63	SetAttr(ch, a | (AttrOf(win->_nc_bkgd) & COLOR_MASK(a)));
64    } else {
65	/* color in attrs has precedence over bkgrnd */
66	a |= (AttrOf(win->_nc_bkgd) & A_ATTRIBUTES) & COLOR_MASK(a);
67	/* color in ch has precedence */
68	AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
69    }
70
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))));
75
76    return (ch);
77}
78
79NCURSES_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 */
82{
83    return render_char(win, ch);
84}
85
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) \
89	if (y > win->_maxy \
90	 || x > win->_maxx \
91	 || y < 0 \
92	 || x < 0) { \
93		TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
94				   "(_maxx = %d, _maxy = %d)", win, x, y, \
95				   win->_maxx, win->_maxy)); \
96		return(ERR); \
97	}
98#else
99#define CHECK_POSITION(win, x, y)	/* nothing */
100#endif
101
102static inline int
103waddch_literal(WINDOW *win, NCURSES_CH_T ch)
104{
105    int x;
106    struct ldat *line;
107
108    x = win->_curx;
109
110    CHECK_POSITION(win, x, win->_cury);
111
112    /*
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).
115     */
116#if 0				/* Solaris 2.6 allows updating the corner more than once */
117    if (win->_flags & _WRAPPED) {
118	if (x >= win->_maxx)
119	    return (ERR);
120	win->_flags &= ~_WRAPPED;
121    }
122#endif
123
124    ch = render_char(win, ch);
125    TR(TRACE_VIRTPUT, ("win attr = %s", _traceattr(win->_attrs)));
126
127    line = win->_line + win->_cury;
128
129    CHANGED_CELL(line, x);
130
131    line->text[x++] = ch;
132    if_WIDEC({
133	if (wcwidth(CharOf(ch)) > 1)
134	    AddAttr(line->text[x++], WA_NAC);
135    });
136
137    TR(TRACE_VIRTPUT, ("(%d, %d) = %s", win->_cury, x, _tracech_t(CHREF(ch))));
138    if (x > win->_maxx) {
139	/*
140	 * The _WRAPPED flag is useful only for telling an application that
141	 * we've just wrapped the cursor.  We don't do anything with this flag
142	 * except set it when wrapping, and clear it whenever we move the
143	 * cursor.  If we try to wrap at the lower-right corner of a window, we
144	 * cannot move the cursor (since that wouldn't be legal).  So we return
145	 * an error (which is what SVr4 does).  Unlike SVr4, we can
146	 * successfully add a character to the lower-right corner (Solaris 2.6
147	 * does this also, however).
148	 */
149	win->_flags |= _WRAPPED;
150	if (++win->_cury > win->_regbottom) {
151	    win->_cury = win->_regbottom;
152	    win->_curx = win->_maxx;
153	    if (!win->_scroll)
154		return (ERR);
155	    scroll(win);
156	}
157	win->_curx = 0;
158	return (OK);
159    }
160    win->_curx = x;
161    return OK;
162}
163
164static inline int
165waddch_nosync(WINDOW *win, const NCURSES_CH_T ch)
166/* the workhorse function -- add a character to the given window */
167{
168    int x, y;
169    chtype t = 0;
170    const char *s = 0;
171
172    if ((AttrOf(ch) & A_ALTCHARSET)
173	|| ((t = CharOf(ch)) > 127)
174	|| ((s = unctrl(t))[1] == 0))
175	return waddch_literal(win, ch);
176
177    x = win->_curx;
178    y = win->_cury;
179
180    switch (t) {
181    case '\t':
182	x += (TABSIZE - (x % TABSIZE));
183
184	/*
185	 * Space-fill the tab on the bottom line so that we'll get the
186	 * "correct" cursor position.
187	 */
188	if ((!win->_scroll && (y == win->_regbottom))
189	    || (x <= win->_maxx)) {
190	    NCURSES_CH_T blank = NewChar2(BLANK_TEXT, BLANK_ATTR);
191	    AddAttr(blank, AttrOf(ch));
192	    while (win->_curx < x) {
193		if (waddch_literal(win, blank) == ERR)
194		    return (ERR);
195	    }
196	    break;
197	} else {
198	    wclrtoeol(win);
199	    win->_flags |= _WRAPPED;
200	    if (++y > win->_regbottom) {
201		x = win->_maxx;
202		y--;
203		if (win->_scroll) {
204		    scroll(win);
205		    x = 0;
206		}
207	    } else {
208		x = 0;
209	    }
210	}
211	break;
212    case '\n':
213	wclrtoeol(win);
214	if (++y > win->_regbottom) {
215	    y--;
216	    if (win->_scroll)
217		scroll(win);
218	    else
219		return (ERR);
220	}
221	/* FALLTHRU */
222    case '\r':
223	x = 0;
224	win->_flags &= ~_WRAPPED;
225	break;
226    case '\b':
227	if (x == 0)
228	    return (OK);
229	x--;
230	win->_flags &= ~_WRAPPED;
231	break;
232    default:
233	while (*s) {
234	    NCURSES_CH_T sch;
235	    SetChar(sch, *s++, AttrOf(ch));
236	    if (waddch_literal(win, sch) == ERR)
237		return ERR;
238	}
239	return (OK);
240    }
241
242    win->_curx = x;
243    win->_cury = y;
244
245    return (OK);
246}
247
248NCURSES_EXPORT(int)
249_nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c)
250/* export copy of waddch_nosync() so the string-put functions can use it */
251{
252    return (waddch_nosync(win, c));
253}
254
255/*
256 * The versions below call _nc_synhook().  We wanted to avoid this in the
257 * version exported for string puts; they'll call _nc_synchook once at end
258 * of run.
259 */
260
261/* These are actual entry points */
262
263NCURSES_EXPORT(int)
264waddch(WINDOW *win, const chtype ch)
265{
266    int code = ERR;
267    NCURSES_CH_T wch;
268    SetChar2(wch, ch);
269
270    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), win,
271				      _tracechtype(ch)));
272
273    if (win && (waddch_nosync(win, wch) != ERR)) {
274	_nc_synchook(win);
275	code = OK;
276    }
277
278    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
279    return (code);
280}
281
282NCURSES_EXPORT(int)
283wechochar(WINDOW *win, const chtype ch)
284{
285    int code = ERR;
286    NCURSES_CH_T wch;
287    SetChar2(wch, ch);
288
289    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), win,
290				      _tracechtype(ch)));
291
292    if (win && (waddch_nosync(win, wch) != ERR)) {
293	bool save_immed = win->_immed;
294	win->_immed = TRUE;
295	_nc_synchook(win);
296	win->_immed = save_immed;
297	code = OK;
298    }
299    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
300    return (code);
301}
302
303#if USE_WIDEC_SUPPORT
304NCURSES_EXPORT(int)
305wadd_wch(WINDOW *win, const cchar_t * wch)
306{
307    int code = ERR;
308
309    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"), win,
310				      _tracech_t(wch)));
311
312    if (win && (waddch_nosync(win, *wch) != ERR)) {
313	_nc_synchook(win);
314	code = OK;
315    }
316
317    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
318    return (code);
319}
320
321NCURSES_EXPORT(int)
322wecho_wchar(WINDOW *win, const cchar_t * wch)
323{
324    int code = ERR;
325
326    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wecho_wchar(%p, %s)"), win,
327				      _tracech_t(wch)));
328
329    if (win && (waddch_nosync(win, *wch) != ERR)) {
330	bool save_immed = win->_immed;
331	win->_immed = TRUE;
332	_nc_synchook(win);
333	win->_immed = save_immed;
334	code = OK;
335    }
336    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
337    return (code);
338}
339#endif /* USE_WIDEC_SUPPORT */
340