lib_addch.c revision bacd15cb1fff2dee94d390e0f84c147cd82ea148
1/****************************************************************************
2 * Copyright (c) 1998 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 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32 ****************************************************************************/
33
34/*
35**	lib_addch.c
36**
37**	The routine waddch().
38**
39*/
40
41#include <curses.priv.h>
42#include <ctype.h>
43
44MODULE_ID("$Id: lib_addch.c,v 1.42 1999/07/24 20:01:05 tom Exp $")
45
46/*
47 * Ugly microtweaking alert.  Everything from here to end of module is
48 * likely to be speed-critical -- profiling data sure says it is!
49 * Most of the important screen-painting functions are shells around
50 * waddch().  So we make every effort to reduce function-call overhead
51 * by inlining stuff, even at the cost of making wrapped copies for
52 * export.  Also we supply some internal versions that don't call the
53 * window sync hook, for use by string-put functions.
54 */
55
56/* Return bit mask for clearing color pair number if given ch has color */
57#define COLOR_MASK(ch) (~(chtype)((ch)&A_COLOR?A_COLOR:0))
58
59static inline chtype render_char(WINDOW *win, chtype ch)
60/* compute a rendition of the given char correct for the current context */
61{
62	chtype a = win->_attrs;
63
64	if (ch == ' ')
65	{
66		/* color in attrs has precedence over bkgd */
67		ch = a | (win->_bkgd & COLOR_MASK(a));
68	}
69	else
70	{
71		/* color in attrs has precedence over bkgd */
72		a |= (win->_bkgd & A_ATTRIBUTES) & COLOR_MASK(a);
73		/* color in ch has precedence */
74		ch |= (a & COLOR_MASK(ch));
75	}
76
77	TR(TRACE_VIRTPUT, ("bkg = %lx, attrs = %lx -> ch = %lx", win->_bkgd,
78		win->_attrs, ch));
79
80	return(ch);
81}
82
83chtype _nc_background(WINDOW *win)
84/* make render_char() visible while still allowing us to inline it below */
85{
86	return (win->_bkgd);
87}
88
89chtype _nc_render(WINDOW *win, chtype ch)
90/* make render_char() visible while still allowing us to inline it below */
91{
92	return render_char(win, ch);
93}
94
95/* check if position is legal; if not, return error */
96#ifndef NDEBUG			/* treat this like an assertion */
97#define CHECK_POSITION(win, x, y) \
98	if (y > win->_maxy \
99	 || x > win->_maxx \
100	 || y < 0 \
101	 || x < 0) { \
102		TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
103				   "(_maxx = %d, _maxy = %d)", win, x, y, \
104				   win->_maxx, win->_maxy)); \
105		return(ERR); \
106	}
107#else
108#define CHECK_POSITION(win, x, y) /* nothing */
109#endif
110
111static inline
112int waddch_literal(WINDOW *win, chtype ch)
113{
114	int x;
115	struct ldat *line;
116
117	x = win->_curx;
118
119	CHECK_POSITION(win, x, win->_cury);
120
121	/*
122	 * If we're trying to add a character at the lower-right corner more
123	 * than once, fail.  (Moving the cursor will clear the flag).
124	 */
125	if (win->_flags & _WRAPPED) {
126		if (x >= win->_maxx)
127			return (ERR);
128		win->_flags &= ~_WRAPPED;
129	}
130
131	ch = render_char(win, ch);
132	TR(TRACE_VIRTPUT, ("win attr = %s", _traceattr(win->_attrs)));
133
134	line = win->_line+win->_cury;
135
136	CHANGED_CELL(line,x);
137
138	line->text[x++] = ch;
139
140	TR(TRACE_VIRTPUT, ("(%d, %d) = %s", win->_cury, x, _tracechtype(ch)));
141	if (x > win->_maxx) {
142		/*
143		 * The _WRAPPED flag is useful only for telling an application
144		 * that we've just wrapped the cursor.  We don't do anything
145		 * with this flag except set it when wrapping, and clear it
146		 * whenever we move the cursor.  If we try to wrap at the
147		 * lower-right corner of a window, we cannot move the cursor
148		 * (since that wouldn't be legal).  So we return an error
149		 * (which is what SVr4 does).  Unlike SVr4, we can successfully
150		 * add a character to the lower-right corner.
151		 */
152		win->_flags |= _WRAPPED;
153		if (++win->_cury > win->_regbottom) {
154			win->_cury = win->_regbottom;
155			win->_curx = win->_maxx;
156			if (!win->_scroll)
157				return (ERR);
158			scroll(win);
159		}
160		win->_curx = 0;
161		return (OK);
162	}
163	win->_curx = x;
164	return OK;
165}
166
167static inline
168int waddch_nosync(WINDOW *win, const chtype ch)
169/* the workhorse function -- add a character to the given window */
170{
171	int	x, y;
172	int	t = 0;
173	const char *s = 0;
174
175	if ((ch & A_ALTCHARSET)
176	    || ((t = TextOf(ch)) > 127)
177	    || ((s = unctrl(t))[1] == 0))
178		return waddch_literal(win, ch);
179
180	x = win->_curx;
181	y = win->_cury;
182
183	switch (t) {
184	case '\t':
185		x += (TABSIZE-(x%TABSIZE));
186
187		/*
188		 * Space-fill the tab on the bottom line so that we'll get the
189		 * "correct" cursor position.
190		 */
191		if ((! win->_scroll && (y == win->_regbottom))
192		 || (x <= win->_maxx)) {
193			chtype blank = (' ' | AttrOf(ch));
194			while (win->_curx < x) {
195				if (waddch_literal(win, blank) == ERR)
196					return(ERR);
197			}
198			break;
199		} else {
200			wclrtoeol(win);
201			win->_flags |= _WRAPPED;
202			if (++y > win->_regbottom) {
203				x = win->_maxx;
204				y--;
205				if (win->_scroll) {
206					scroll(win);
207					x = 0;
208				}
209			} else {
210				x = 0;
211			}
212		}
213		break;
214	case '\n':
215		wclrtoeol(win);
216		if (++y > win->_regbottom) {
217			y--;
218			if (win->_scroll)
219				scroll(win);
220			else
221				return (ERR);
222		}
223		/* FALLTHRU */
224	case '\r':
225		x = 0;
226		win->_flags &= ~_WRAPPED;
227		break;
228	case '\b':
229		if (x == 0)
230			return (OK);
231		x--;
232		win->_flags &= ~_WRAPPED;
233		break;
234	default:
235		while (*s)
236			if (waddch_literal(win, (*s++)|AttrOf(ch)) == ERR)
237				return ERR;
238		return(OK);
239	}
240
241	win->_curx = x;
242	win->_cury = y;
243
244	return(OK);
245}
246
247int _nc_waddch_nosync(WINDOW *win, const chtype c)
248/* export copy of waddch_nosync() so the string-put functions can use it */
249{
250    return(waddch_nosync(win, c));
251}
252
253/*
254 * The versions below call _nc_synhook().  We wanted to avoid this in the
255 * version exported for string puts; they'll call _nc_synchook once at end
256 * of run.
257 */
258
259/* These are actual entry points */
260
261int waddch(WINDOW *win, const chtype ch)
262{
263	int code = ERR;
264
265	TR(TRACE_VIRTPUT|TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), win, _tracechtype(ch)));
266
267	if (win && (waddch_nosync(win, ch) != ERR))
268	{
269		_nc_synchook(win);
270		code = OK;
271	}
272
273	TR(TRACE_VIRTPUT|TRACE_CCALLS, (T_RETURN("%d"), code));
274	return(code);
275}
276
277int wechochar(WINDOW *win, const chtype ch)
278{
279	int code = ERR;
280
281	TR(TRACE_VIRTPUT|TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), win, _tracechtype(ch)));
282
283	if (win && (waddch_nosync(win, ch) != ERR))
284	{
285		bool	save_immed = win->_immed;
286		win->_immed = TRUE;
287		_nc_synchook(win);
288		win->_immed = save_immed;
289		code = OK;
290	}
291	TR(TRACE_VIRTPUT|TRACE_CCALLS, (T_RETURN("%d"), code));
292	return(code);
293}
294