1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 2004-2011,2016 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
33 ** The routine wadd_wch().
37 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_add_wch.c,v 1.16 2020/02/02 23:34:34 tom Exp $")
45 /* clone/adapt lib_addch.c */
46 static const cchar_t blankchar = NewChar(BLANK_TEXT);
49 * Ugly microtweaking alert. Everything from here to end of module is
50 * likely to be speed-critical -- profiling data sure says it is!
51 * Most of the important screen-painting functions are shells around
52 * wadd_wch(). So we make every effort to reduce function-call overhead
53 * by inlining stuff, even at the cost of making wrapped copies for
54 * export. Also we supply some internal versions that don't call the
55 * window sync hook, for use by string-put functions.
58 /* Return bit mask for clearing color pair number if given ch has color */
59 #define COLOR_MASK(ch) (~(attr_t)(((ch) & A_COLOR) ? A_COLOR : 0))
61 static NCURSES_INLINE cchar_t
62 render_char(WINDOW *win, cchar_t ch)
63 /* compute a rendition of the given char correct for the current context */
65 attr_t a = WINDOW_ATTRS(win);
66 int pair = GetPair(ch);
69 && AttrOf(ch) == A_NORMAL
71 /* color/pair in attrs has precedence over bkgrnd */
73 SetAttr(ch, a | AttrOf(win->_nc_bkgd));
74 if ((pair = GET_WINDOW_PAIR(win)) == 0)
75 pair = GetPair(win->_nc_bkgd);
78 /* color in attrs has precedence over bkgrnd */
79 a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a);
80 /* color in ch has precedence */
82 if ((pair = GET_WINDOW_PAIR(win)) == 0)
83 pair = GetPair(win->_nc_bkgd);
85 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
90 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
91 _tracech_t2(1, CHREF(win->_nc_bkgd)),
92 GetPair(win->_nc_bkgd),
93 _traceattr(WINDOW_ATTRS(win)),
95 _tracech_t2(3, CHREF(ch)),
101 /* check if position is legal; if not, return error */
102 #ifndef NDEBUG /* treat this like an assertion */
103 #define CHECK_POSITION(win, x, y) \
108 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
109 "(_maxx = %d, _maxy = %d)", win, x, y, \
110 win->_maxx, win->_maxy)); \
114 #define CHECK_POSITION(win, x, y) /* nothing */
118 newline_forces_scroll(WINDOW *win, NCURSES_SIZE_T *ypos)
122 if (*ypos >= win->_regtop && *ypos == win->_regbottom) {
123 *ypos = win->_regbottom;
126 *ypos = (NCURSES_SIZE_T) (*ypos + 1);
132 * The _WRAPPED flag is useful only for telling an application that we've just
133 * wrapped the cursor. We don't do anything with this flag except set it when
134 * wrapping, and clear it whenever we move the cursor. If we try to wrap at
135 * the lower-right corner of a window, we cannot move the cursor (since that
136 * wouldn't be legal). So we return an error (which is what SVr4 does).
137 * Unlike SVr4, we can successfully add a character to the lower-right corner
138 * (Solaris 2.6 does this also, however).
141 wrap_to_next_line(WINDOW *win)
143 win->_flags |= _WRAPPED;
144 if (newline_forces_scroll(win, &(win->_cury))) {
145 win->_curx = win->_maxx;
154 static int wadd_wch_literal(WINDOW *, cchar_t);
156 * Fill the given number of cells with blanks using the current background
157 * rendition. This saves/restores the current x-position.
160 fill_cells(WINDOW *win, int count)
162 cchar_t blank = blankchar;
163 int save_x = win->_curx;
164 int save_y = win->_cury;
166 while (count-- > 0) {
167 if (wadd_wch_literal(win, blank) == ERR)
170 win->_curx = (NCURSES_SIZE_T) save_x;
171 win->_cury = (NCURSES_SIZE_T) save_y;
175 wadd_wch_literal(WINDOW *win, cchar_t ch)
184 CHECK_POSITION(win, x, y);
186 ch = render_char(win, ch);
188 line = win->_line + y;
190 CHANGED_CELL(line, x);
193 * Non-spacing characters are added to the current cell.
195 * Spacing characters that are wider than one column require some display
199 int len = _nc_wacs_width(CharOf(ch));
204 if (len == 0) { /* non-spacing */
205 if ((x > 0 && y >= 0)
206 || (win->_maxx >= 0 && win->_cury >= 1)) {
208 chars = (win->_line[y].text[x - 1].chars);
210 chars = (win->_line[y - 1].text[win->_maxx].chars);
211 for (i = 0; i < CCHARW_MAX; ++i) {
214 ("added non-spacing %d: %x",
215 x, (int) CharOf(ch)));
216 chars[i] = CharOf(ch);
222 } else if (len > 1) { /* multi-column characters */
224 * Check if the character will fit on the current line. If it does
225 * not fit, fill in the remainder of the line with blanks. and
226 * move to the next line.
228 if (len > win->_maxx + 1) {
229 TR(TRACE_VIRTPUT, ("character will not fit"));
231 } else if (x + len > win->_maxx + 1) {
232 int count = win->_maxx + 1 - x;
233 TR(TRACE_VIRTPUT, ("fill %d remaining cells", count));
234 fill_cells(win, count);
235 if (wrap_to_next_line(win) == ERR)
239 line = win->_line + y;
242 * Check for cells which are orphaned by adding this character, set
245 * FIXME: this actually could fill j-i cells, more complicated to
248 for (i = 0; i < len; ++i) {
249 if (isWidecBase(win->_line[y].text[x + i])) {
251 } else if (isWidecExt(win->_line[y].text[x + i])) {
252 for (j = i; x + j <= win->_maxx; ++j) {
253 if (!isWidecExt(win->_line[y].text[x + j])) {
254 TR(TRACE_VIRTPUT, ("fill %d orphan cells", j));
263 * Finally, add the cells for this character.
265 for (i = 0; i < len; ++i) {
267 SetWidecExt(value, i);
268 TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)",
270 win->_begy + y, win->_begx + x));
271 line->text[x] = value;
272 CHANGED_CELL(line, x);
280 * Single-column characters.
282 line->text[x++] = ch;
284 * This label is used only for wide-characters.
288 TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s",
289 (long) win->_cury, (long) win->_curx, x - 1,
290 _tracech_t(CHREF(ch))));
292 if (x > win->_maxx) {
293 return wrap_to_next_line(win);
295 win->_curx = (NCURSES_SIZE_T) x;
299 static NCURSES_INLINE int
300 wadd_wch_nosync(WINDOW *win, cchar_t ch)
301 /* the workhorse function -- add a character to the given window */
307 SCREEN *sp = _nc_screen_of(win);
311 * If we are using the alternate character set, forget about locale.
312 * Otherwise, if the locale claims the code is printable, treat it that
315 if ((AttrOf(ch) & A_ALTCHARSET)
316 || iswprint((wint_t) CharOf(ch)))
317 return wadd_wch_literal(win, ch);
320 * Handle carriage control and other codes that are not printable, or are
321 * known to expand to more than one character according to unctrl().
326 switch (CharOf(ch)) {
329 tabsize = *ptrTabsize(sp);
333 x = (NCURSES_SIZE_T) (x + (tabsize - (x % tabsize)));
335 * Space-fill the tab on the bottom line so that we'll get the
336 * "correct" cursor position.
338 if ((!win->_scroll && (y == win->_regbottom))
339 || (x <= win->_maxx)) {
340 cchar_t blank = blankchar;
341 AddAttr(blank, AttrOf(ch));
342 while (win->_curx < x) {
343 if (wadd_wch_literal(win, blank) == ERR)
349 win->_flags |= _WRAPPED;
350 if (newline_forces_scroll(win, &y)) {
363 if (newline_forces_scroll(win, &y)) {
372 win->_flags &= ~_WRAPPED;
378 win->_flags &= ~_WRAPPED;
381 if ((s = wunctrl(&ch)) != 0) {
384 SetChar(sch, *s++, AttrOf(ch));
385 if_EXT_COLORS(SetPair(sch, GetPair(ch)));
386 if (wadd_wch_literal(win, sch) == ERR)
401 * The versions below call _nc_synchook(). We wanted to avoid this in the
402 * version exported for string puts; they'll call _nc_synchook once at end
406 /* These are actual entry points */
409 wadd_wch(WINDOW *win, const cchar_t *wch)
413 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"),
415 _tracecchar_t(wch)));
417 if (win && (wadd_wch_nosync(win, *wch) != ERR)) {
422 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
427 wecho_wchar(WINDOW *win, const cchar_t *wch)
431 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"),
433 _tracecchar_t(wch)));
435 if (win && (wadd_wch_nosync(win, *wch) != ERR)) {
436 bool save_immed = win->_immed;
439 win->_immed = save_immed;
442 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));