summaryrefslogtreecommitdiff
path: root/lib/libcurses/lib_addch.c
blob: 005c5bfbd2230bba5a3d1a02bbd9e90a049f52bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

/***************************************************************************
*                            COPYRIGHT NOTICE                              *
****************************************************************************
*                ncurses is copyright (C) 1992-1995                        *
*                          Zeyd M. Ben-Halim                               *
*                          zmbenhal@netcom.com                             *
*                          Eric S. Raymond                                 *
*                          esr@snark.thyrsus.com                           *
*                                                                          *
*        Permission is hereby granted to reproduce and distribute ncurses  *
*        by any means and for any fee, whether alone or as part of a       *
*        larger distribution, in source or in binary form, PROVIDED        *
*        this notice is included with any such distribution, and is not    *
*        removed from any of its header files. Mention of ncurses in any   *
*        applications linked with it is highly appreciated.                *
*                                                                          *
*        ncurses comes AS IS with no warranty, implied or expressed.       *
*                                                                          *
***************************************************************************/

/*
**	lib_addch.c
**
**	The routines waddch(), wattr_on(), wattr_off(), wchgat().
**
*/

#include "curses.priv.h"
#include <ctype.h>
#include "unctrl.h"

#define ALL_BUT_COLOR ((chtype)~(A_COLOR))

int wattr_on(WINDOW *win, const attr_t at)
{
	T(("wattr_on(%p,%s) current = %s", win, _traceattr(at), _traceattr(win->_attrs)));
	if (PAIR_NUMBER(at) > 0x00) {
		win->_attrs = (win->_attrs & ALL_BUT_COLOR) | at ;
		T(("new attribute is %s", _traceattr(win->_attrs)));
	} else {
		win->_attrs |= at;
		T(("new attribute is %s", _traceattr(win->_attrs)));
	}
	return OK;
}

int wattr_off(WINDOW *win, const attr_t at)
{
#define IGNORE_COLOR_OFF FALSE

	T(("wattr_off(%p,%s) current = %s", win, _traceattr(at), _traceattr(win->_attrs)));
	if (IGNORE_COLOR_OFF == TRUE) {
		if (PAIR_NUMBER(at) == 0xff) /* turn off color */
			win->_attrs &= ~at;
		else /* leave color alone */
			win->_attrs &= ~(at|ALL_BUT_COLOR);
	} else {
		if (PAIR_NUMBER(at) > 0x00) /* turn off color */
			win->_attrs &= ~at;
		else /* leave color alone */
			win->_attrs &= ~(at|ALL_BUT_COLOR);
	}
	T(("new attribute is %s", _traceattr(win->_attrs)));
	return OK;
}

int wchgat(WINDOW *win, int n, attr_t attr, short color, const void *opts)
{
    int	i;

    for (i = win->_curx; i <= win->_maxx && (n == -1 || (n-- > 0)); i++)
	win->_line[win->_cury].text[i]
	    = (win->_line[win->_cury].text[i] & A_CHARTEXT)
		| attr
		| COLOR_PAIR(color);

    return OK;
}

/*
 * Ugly microtweaking alert.  Everything from here to end of module is
 * likely to be speed-critical -- profiling data sure says it is!
 * Most of the important screen-painting functions are shells around
 * waddch().  So we make every effort to reduce function-call overhead
 * by inlining stuff, even at the cost of making wrapped copies for
 * export.  Also we supply some internal versions that don't call the
 * window sync hook, for use by string-put functions.
 */

static __inline chtype render_char(WINDOW *win, chtype oldch, chtype newch)
/* compute a rendition of the given char correct for the current context */
{
	if ((oldch & A_CHARTEXT) == ' ')
		newch |= win->_bkgd;
	else if (!(newch & A_ATTRIBUTES))
		newch |= (win->_bkgd & A_ATTRIBUTES);
	TR(TRACE_VIRTPUT, ("bkg = %lx -> ch = %lx", win->_bkgd, newch));

	return(newch);
}

chtype _nc_render(WINDOW *win, chtype oldch, chtype newch)
/* make render_char() visible while still allowing us to inline it below */
{
    return(render_char(win, oldch, newch));
}

/* actions needed to process a newline within addch_nosync() */
#define DO_NEWLINE	x = 0; \
			win->_flags &= ~_NEED_WRAP; \
			y++; \
			if (y > win->_regbottom) { \
				y--; \
				if (win->_scroll) \
					scroll(win); \
			}

/* check if position is legal; if not, return error */
#define CHECK_POSITION(win, x, y) \
	if (y > win->_maxy || x > win->_maxx || y < 0 || x < 0) { \
		TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
				   "(_maxx = %d, _maxy = %d)", win, x, y, \
				   win->_maxx, win->_maxy)); \
	  	win->_curx = win->_cury = 0; \
		win->_flags &= ~_NEED_WRAP; \
	    	return(ERR); \
	}

static __inline
int waddch_literal(WINDOW *win, chtype ch)
{
register int x, y;

	x = win->_curx;
	y = win->_cury;

	CHECK_POSITION(win, x, y);

	if (win->_flags & _NEED_WRAP) {
		TR(TRACE_MOVE, ("new char when NEED_WRAP set at %d,%d",y,x));
		DO_NEWLINE
	}

	/*
	 * We used to pass in
	 *	win->_line[y].text[x]
	 * as a second argument, but the value of the old character
	 * is not relevant here.
	 */
	ch = render_char(win, 0, ch);

	TR(TRACE_VIRTPUT, ("win attr = %s", _traceattr(win->_attrs)));
	ch |= win->_attrs;

	if (win->_line[y].text[x] != ch) {
		if (win->_line[y].firstchar == _NOCHANGE)
			win->_line[y].firstchar = win->_line[y].lastchar = x;
		else if (x < win->_line[y].firstchar)
			win->_line[y].firstchar = x;
		else if (x > win->_line[y].lastchar)
			win->_line[y].lastchar = x;

	}

	win->_line[y].text[x++] = ch;
	TR(TRACE_VIRTPUT, ("(%d, %d) = %s | %s", 
			   y, x,
			   _tracechar((unsigned char)(ch & A_CHARTEXT)),
			   _traceattr((ch & (chtype)A_ATTRIBUTES))));
	if (x > win->_maxx) {
		TR(TRACE_MOVE, ("NEED_WRAP set at %d,%d",y,x));
		win->_flags |= _NEED_WRAP;
		x--;
	}

	win->_curx = x;
	win->_cury = y;

	return OK;
}

static __inline
int waddch_nosync(WINDOW *win, const chtype c)
/* the workhorse function -- add a character to the given window */
{
register chtype	ch = c;
register int	x, y;
int		newx;

	x = win->_curx;
	y = win->_cury;

	CHECK_POSITION(win, x, y);

	if (ch & A_ALTCHARSET)
		goto noctrl;

	switch ((int)(ch&A_CHARTEXT)) {
    	case '\t':
		if (win->_flags & _NEED_WRAP) {
		  	x = 0;
			newx = min(TABSIZE, win->_maxx+1);
		} else
			newx = min(x + (TABSIZE-(x%TABSIZE)), win->_maxx+1);
		while (win->_curx < newx) {
	    		if (waddch_literal(win, ' ' | (ch&A_ATTRIBUTES)) == ERR)
				return(ERR);
		}
		return(OK);
    	case '\n':
		wclrtoeol(win);
		DO_NEWLINE
		break;
    	case '\r':
		x = 0;
		win->_flags &= ~_NEED_WRAP;
		break;
    	case '\b':
		if (win->_flags & _NEED_WRAP)
			win->_flags &= ~_NEED_WRAP;
		else if (--x < 0)
			x = 0;
		break;
    	default:
		if (is7bits(ch & A_CHARTEXT) && iscntrl(ch & A_CHARTEXT))
		    	return(waddstr(win, unctrl((unsigned char)ch)));

		/* FALLTHRU */
        noctrl:
		waddch_literal(win, ch);
		return(OK);
	}

	win->_curx = x;
	win->_cury = y;

	return(OK);
}

#undef DO_NEWLINE

int _nc_waddch_nosync(WINDOW *win, const chtype c)
/* export copy of waddch_nosync() so the string-put functions can use it */
{
    return(waddch_nosync(win, c));
}

/*
 * The versions below call _nc_synhook().  We wanted to avoid this in the
 * version exported for string puts; they'll call _nc_synchook once at end
 * of run.
 */

/* These are actual entry points */

int waddch(WINDOW *win, const chtype ch)
{
	TR(TRACE_VIRTPUT, ("waddch(%p, %s | %s) called", win,
			  _tracechar((unsigned char)(ch & A_CHARTEXT)),
			  _traceattr((ch & (chtype)A_ATTRIBUTES))));

	if (waddch_nosync(win, ch) == ERR)
		return(ERR);
	else
	{
		_nc_synchook(win);
		TR(TRACE_VIRTPUT, ("waddch() is done"));
		return(OK);
	}
}

int wechochar(WINDOW *win, const chtype ch)
{
	TR(TRACE_VIRTPUT, ("wechochar(%p,%s (%s)) called", win,
			  _tracechar((unsigned char)(ch & A_CHARTEXT)),
			  _traceattr((ch & (chtype)A_ATTRIBUTES))));

	if (waddch_literal(win, ch) == ERR)
		return(ERR);
	else
	{
		_nc_synchook(win);
		TR(TRACE_VIRTPUT, ("wechochar() is done"));
		return(OK);
	}
}