1 /****************************************************************************
2 * Copyright (c) 2004-2010,2011 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 wadd_wch().
36 #include <curses.priv.h>
42 MODULE_ID("$Id: lib_add_wch.c,v 1.12 2011/03/22 09:31:15 Petr.Pavlu Exp $")
44 /* clone/adapt lib_addch.c */
45 static const cchar_t blankchar
= NewChar(BLANK_TEXT
);
48 * Ugly microtweaking alert. Everything from here to end of module is
49 * likely to be speed-critical -- profiling data sure says it is!
50 * Most of the important screen-painting functions are shells around
51 * wadd_wch(). So we make every effort to reduce function-call overhead
52 * by inlining stuff, even at the cost of making wrapped copies for
53 * export. Also we supply some internal versions that don't call the
54 * window sync hook, for use by string-put functions.
57 /* Return bit mask for clearing color pair number if given ch has color */
58 #define COLOR_MASK(ch) (~(attr_t)((ch) & A_COLOR ? A_COLOR : 0))
60 static NCURSES_INLINE cchar_t
61 render_char(WINDOW
*win
, cchar_t ch
)
62 /* compute a rendition of the given char correct for the current context */
64 attr_t a
= WINDOW_ATTRS(win
);
65 int pair
= GetPair(ch
);
68 && AttrOf(ch
) == A_NORMAL
70 /* color/pair in attrs has precedence over bkgrnd */
72 SetAttr(ch
, a
| AttrOf(win
->_nc_bkgd
));
73 if ((pair
= GET_WINDOW_PAIR(win
)) == 0)
74 pair
= GetPair(win
->_nc_bkgd
);
77 /* color in attrs has precedence over bkgrnd */
78 a
|= AttrOf(win
->_nc_bkgd
) & COLOR_MASK(a
);
79 /* color in ch has precedence */
81 if ((pair
= GET_WINDOW_PAIR(win
)) == 0)
82 pair
= GetPair(win
->_nc_bkgd
);
84 AddAttr(ch
, (a
& COLOR_MASK(AttrOf(ch
))));
89 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
90 _tracech_t2(1, CHREF(win
->_nc_bkgd
)),
91 GetPair(win
->_nc_bkgd
),
92 _traceattr(WINDOW_ATTRS(win
)),
94 _tracech_t2(3, CHREF(ch
)),
100 /* check if position is legal; if not, return error */
101 #ifndef NDEBUG /* treat this like an assertion */
102 #define CHECK_POSITION(win, x, y) \
107 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
108 "(_maxx = %d, _maxy = %d)", win, x, y, \
109 win->_maxx, win->_maxy)); \
113 #define CHECK_POSITION(win, x, y) /* nothing */
117 newline_forces_scroll(WINDOW
*win
, NCURSES_SIZE_T
* ypos
)
121 if (*ypos
>= win
->_regtop
&& *ypos
== win
->_regbottom
) {
122 *ypos
= win
->_regbottom
;
125 *ypos
= (NCURSES_SIZE_T
) (*ypos
+ 1);
131 * The _WRAPPED flag is useful only for telling an application that we've just
132 * wrapped the cursor. We don't do anything with this flag except set it when
133 * wrapping, and clear it whenever we move the cursor. If we try to wrap at
134 * the lower-right corner of a window, we cannot move the cursor (since that
135 * wouldn't be legal). So we return an error (which is what SVr4 does).
136 * Unlike SVr4, we can successfully add a character to the lower-right corner
137 * (Solaris 2.6 does this also, however).
140 wrap_to_next_line(WINDOW
*win
)
142 win
->_flags
|= _WRAPPED
;
143 if (newline_forces_scroll(win
, &(win
->_cury
))) {
144 win
->_curx
= win
->_maxx
;
153 static int wadd_wch_literal(WINDOW
*, cchar_t
);
155 * Fill the given number of cells with blanks using the current background
156 * rendition. This saves/restores the current x-position.
159 fill_cells(WINDOW
*win
, int count
)
161 cchar_t blank
= blankchar
;
162 int save_x
= win
->_curx
;
163 int save_y
= win
->_cury
;
165 while (count
-- > 0) {
166 if (wadd_wch_literal(win
, blank
) == ERR
)
169 win
->_curx
= (NCURSES_SIZE_T
) save_x
;
170 win
->_cury
= (NCURSES_SIZE_T
) save_y
;
174 wadd_wch_literal(WINDOW
*win
, cchar_t ch
)
183 CHECK_POSITION(win
, x
, y
);
185 ch
= render_char(win
, ch
);
187 line
= win
->_line
+ y
;
189 CHANGED_CELL(line
, x
);
192 * Non-spacing characters are added to the current cell.
194 * Spacing characters that are wider than one column require some display
198 int len
= wcwidth(CharOf(ch
));
203 if (len
== 0) { /* non-spacing */
204 if ((x
> 0 && y
>= 0)
205 || (win
->_maxx
>= 0 && win
->_cury
>= 1)) {
207 chars
= (win
->_line
[y
].text
[x
- 1].chars
);
209 chars
= (win
->_line
[y
- 1].text
[win
->_maxx
].chars
);
210 for (i
= 0; i
< CCHARW_MAX
; ++i
) {
213 ("added non-spacing %d: %x",
214 x
, (int) CharOf(ch
)));
215 chars
[i
] = CharOf(ch
);
221 } else if (len
> 1) { /* multi-column characters */
223 * Check if the character will fit on the current line. If it does
224 * not fit, fill in the remainder of the line with blanks. and
225 * move to the next line.
227 if (len
> win
->_maxx
+ 1) {
228 TR(TRACE_VIRTPUT
, ("character will not fit"));
230 } else if (x
+ len
> win
->_maxx
+ 1) {
231 int count
= win
->_maxx
+ 1 - x
;
232 TR(TRACE_VIRTPUT
, ("fill %d remaining cells", count
));
233 fill_cells(win
, count
);
234 if (wrap_to_next_line(win
) == ERR
)
238 line
= win
->_line
+ y
;
241 * Check for cells which are orphaned by adding this character, set
244 * FIXME: this actually could fill j-i cells, more complicated to
247 for (i
= 0; i
< len
; ++i
) {
248 if (isWidecBase(win
->_line
[y
].text
[x
+ i
])) {
250 } else if (isWidecExt(win
->_line
[y
].text
[x
+ i
])) {
251 for (j
= i
; x
+ j
<= win
->_maxx
; ++j
) {
252 if (!isWidecExt(win
->_line
[y
].text
[x
+ j
])) {
253 TR(TRACE_VIRTPUT
, ("fill %d orphan cells", j
));
262 * Finally, add the cells for this character.
264 for (i
= 0; i
< len
; ++i
) {
266 SetWidecExt(value
, i
);
267 TR(TRACE_VIRTPUT
, ("multicolumn %d:%d (%d,%d)",
269 win
->_begy
+ y
, win
->_begx
+ x
));
270 line
->text
[x
] = value
;
271 CHANGED_CELL(line
, x
);
279 * Single-column characters.
281 line
->text
[x
++] = ch
;
283 * This label is used only for wide-characters.
287 TR(TRACE_VIRTPUT
, ("cell (%ld, %ld..%d) = %s",
288 (long) win
->_cury
, (long) win
->_curx
, x
- 1,
289 _tracech_t(CHREF(ch
))));
291 if (x
> win
->_maxx
) {
292 return wrap_to_next_line(win
);
294 win
->_curx
= (NCURSES_SIZE_T
) x
;
298 static NCURSES_INLINE
int
299 wadd_wch_nosync(WINDOW
*win
, cchar_t ch
)
300 /* the workhorse function -- add a character to the given window */
306 SCREEN
*sp
= _nc_screen_of(win
);
310 * If we are using the alternate character set, forget about locale.
311 * Otherwise, if the locale claims the code is printable, treat it that
314 if ((AttrOf(ch
) & A_ALTCHARSET
)
315 || iswprint((wint_t) CharOf(ch
)))
316 return wadd_wch_literal(win
, ch
);
319 * Handle carriage control and other codes that are not printable, or are
320 * known to expand to more than one character according to unctrl().
325 switch (CharOf(ch
)) {
328 tabsize
= *ptrTabsize(sp
);
332 x
= (NCURSES_SIZE_T
) (x
+ (tabsize
- (x
% tabsize
)));
334 * Space-fill the tab on the bottom line so that we'll get the
335 * "correct" cursor position.
337 if ((!win
->_scroll
&& (y
== win
->_regbottom
))
338 || (x
<= win
->_maxx
)) {
339 cchar_t blank
= blankchar
;
340 AddAttr(blank
, AttrOf(ch
));
341 while (win
->_curx
< x
) {
342 if (wadd_wch_literal(win
, blank
) == ERR
)
348 win
->_flags
|= _WRAPPED
;
349 if (newline_forces_scroll(win
, &y
)) {
362 if (newline_forces_scroll(win
, &y
)) {
371 win
->_flags
&= ~_WRAPPED
;
377 win
->_flags
&= ~_WRAPPED
;
380 if ((s
= wunctrl(&ch
)) != 0) {
383 SetChar(sch
, *s
++, AttrOf(ch
));
384 if_EXT_COLORS(SetPair(sch
, GetPair(ch
)));
385 if (wadd_wch_literal(win
, sch
) == ERR
)
400 * The versions below call _nc_synchook(). We wanted to avoid this in the
401 * version exported for string puts; they'll call _nc_synchook once at end
405 /* These are actual entry points */
408 wadd_wch(WINDOW
*win
, const cchar_t
*wch
)
412 TR(TRACE_VIRTPUT
| TRACE_CCALLS
, (T_CALLED("wadd_wch(%p, %s)"),
414 _tracecchar_t(wch
)));
416 if (win
&& (wadd_wch_nosync(win
, *wch
) != ERR
)) {
421 TR(TRACE_VIRTPUT
| TRACE_CCALLS
, (T_RETURN("%d"), code
));
426 wecho_wchar(WINDOW
*win
, const cchar_t
*wch
)
430 TR(TRACE_VIRTPUT
| TRACE_CCALLS
, (T_CALLED("wechochar(%p, %s)"),
432 _tracecchar_t(wch
)));
434 if (win
&& (wadd_wch_nosync(win
, *wch
) != ERR
)) {
435 bool save_immed
= win
->_immed
;
438 win
->_immed
= save_immed
;
441 TR(TRACE_VIRTPUT
| TRACE_CCALLS
, (T_RETURN("%d"), code
));