]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_addch.c
ncurses 5.4
[ncurses.git] / ncurses / base / lib_addch.c
1 /****************************************************************************
2  * Copyright (c) 1998-2003,2004 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
39 MODULE_ID("$Id: lib_addch.c,v 1.80 2004/02/07 18:20:46 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
54 static inline NCURSES_CH_T
55 render_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) & COLOR_MASK(a);
67         /* color in ch has precedence */
68         AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
69     }
70
71     TR(TRACE_VIRTPUT, ("render_char 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
79 NCURSES_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
102 static
103 #if !USE_WIDEC_SUPPORT          /* cannot be inline if it is recursive */
104   inline
105 #endif
106 int
107 waddch_literal(WINDOW *win, NCURSES_CH_T ch)
108 {
109     int x;
110     int y;
111     struct ldat *line;
112
113     x = win->_curx;
114     y = win->_cury;
115
116     CHECK_POSITION(win, x, y);
117
118     /*
119      * If we're trying to add a character at the lower-right corner more
120      * than once, fail.  (Moving the cursor will clear the flag).
121      */
122 #if 0                           /* Solaris 2.6 allows updating the corner more than once */
123     if (win->_flags & _WRAPPED) {
124         if (x >= win->_maxx)
125             return (ERR);
126         win->_flags &= ~_WRAPPED;
127     }
128 #endif
129
130     ch = render_char(win, ch);
131
132     line = win->_line + y;
133
134     CHANGED_CELL(line, x);
135
136     /*
137      * Build up multibyte characters until we have a wide-character.
138      */
139     if_WIDEC({
140         if (WINDOW_EXT(win, addch_used) == 0 && Charable(ch)) {
141             WINDOW_EXT(win, addch_used) = 0;
142         } else {
143             char *buffer = WINDOW_EXT(win, addch_work);
144             int len;
145             mbstate_t state;
146             wchar_t result;
147
148             if ((WINDOW_EXT(win, addch_used) != 0) &&
149                 (WINDOW_EXT(win, addch_x) != x ||
150                  WINDOW_EXT(win, addch_y) != y)) {
151                 /* discard the incomplete multibyte character */
152                 WINDOW_EXT(win, addch_used) = 0;
153             }
154             WINDOW_EXT(win, addch_x) = x;
155             WINDOW_EXT(win, addch_y) = y;
156
157             memset(&state, 0, sizeof(state));
158             buffer[WINDOW_EXT(win, addch_used)] = CharOf(ch);
159             WINDOW_EXT(win, addch_used) += 1;
160             buffer[WINDOW_EXT(win, addch_used)] = '\0';
161             if ((len = mbrtowc(&result,
162                                buffer,
163                                WINDOW_EXT(win, addch_used), &state)) > 0) {
164                 attr_t attrs = AttrOf(ch);
165                 SetChar(ch, result, attrs);
166                 WINDOW_EXT(win, addch_used) = 0;
167                 if (CharOf(ch) < 256) {
168                     const char *s = unctrl(CharOf(ch));
169                     if (s[1] != 0) {
170                         return waddstr(win, s);
171                     }
172                 }
173             } else {
174                 if (len == -1) {
175                     /*
176                      * An error occurred.  We could either discard everything,
177                      * or assume that the error was in the previous input.
178                      * Try the latter.
179                      */
180                     TR(TRACE_VIRTPUT, ("Alert! mbrtowc returns error"));
181                     buffer[0] = CharOf(ch);
182                     WINDOW_EXT(win, addch_used) = 1;
183                 }
184                 return OK;
185             }
186         }
187     });
188
189     /*
190      * Handle non-spacing characters
191      */
192     if_WIDEC({
193         if (wcwidth(CharOf(ch)) == 0) {
194             int i;
195             if ((x > 0 && y >= 0)
196                 || ((y = win->_cury - 1) >= 0 &&
197                     (x = win->_maxx) > 0)) {
198                 wchar_t *chars = (win->_line[y].text[x - 1].chars);
199                 for (i = 0; i < CCHARW_MAX; ++i) {
200                     if (chars[i] == 0) {
201                         chars[i] = CharOf(ch);
202                         break;
203                     }
204                 }
205             }
206             goto testwrapping;
207         }
208     });
209     line->text[x++] = ch;
210     /*
211      * Provide for multi-column characters
212      */
213     if_WIDEC({
214         int len = wcwidth(CharOf(ch));
215         while (len-- > 1) {
216             if (x + (len - 1) > win->_maxx) {
217                 NCURSES_CH_T blank = NewChar2(BLANK_TEXT, BLANK_ATTR);
218                 AddAttr(blank, AttrOf(ch));
219                 if (waddch_literal(win, blank) != ERR)
220                     return waddch_literal(win, ch);
221                 return ERR;
222             }
223             AddAttr(line->text[x++], WA_NAC);
224             TR(TRACE_VIRTPUT, ("added NAC %d", x - 1));
225         }
226     }
227   testwrapping:
228     );
229
230     TR(TRACE_VIRTPUT, ("(%d, %d) = %s", win->_cury, x, _tracech_t(CHREF(ch))));
231     if (x > win->_maxx) {
232         /*
233          * The _WRAPPED flag is useful only for telling an application that
234          * we've just wrapped the cursor.  We don't do anything with this flag
235          * except set it when wrapping, and clear it whenever we move the
236          * cursor.  If we try to wrap at the lower-right corner of a window, we
237          * cannot move the cursor (since that wouldn't be legal).  So we return
238          * an error (which is what SVr4 does).  Unlike SVr4, we can
239          * successfully add a character to the lower-right corner (Solaris 2.6
240          * does this also, however).
241          */
242         win->_flags |= _WRAPPED;
243         if (++win->_cury > win->_regbottom) {
244             win->_cury = win->_regbottom;
245             win->_curx = win->_maxx;
246             if (!win->_scroll)
247                 return (ERR);
248             scroll(win);
249         }
250         win->_curx = 0;
251         return (OK);
252     }
253     win->_curx = x;
254     return OK;
255 }
256
257 static inline int
258 waddch_nosync(WINDOW *win, const NCURSES_CH_T ch)
259 /* the workhorse function -- add a character to the given window */
260 {
261     int x, y;
262     chtype t = CharOf(ch);
263     const char *s = 0;
264
265     /*
266      * If we are using the alternate character set, forget about locale.
267      * Otherwise, if unctrl() returns a single-character or the locale
268      * claims the code is printable, treat it that way.
269      */
270     if ((AttrOf(ch) & A_ALTCHARSET)
271         || ((s = unctrl(t))[1] == 0 ||
272                 (
273                 isprint(t)
274 #if USE_WIDEC_SUPPORT
275                 || WINDOW_EXT(win, addch_used)
276 #endif
277                 )))
278         return waddch_literal(win, ch);
279
280     /*
281      * Handle carriage control and other codes that are not printable, or are
282      * known to expand to more than one character according to unctrl().
283      */
284     x = win->_curx;
285     y = win->_cury;
286
287     switch (t) {
288     case '\t':
289         x += (TABSIZE - (x % TABSIZE));
290
291         /*
292          * Space-fill the tab on the bottom line so that we'll get the
293          * "correct" cursor position.
294          */
295         if ((!win->_scroll && (y == win->_regbottom))
296             || (x <= win->_maxx)) {
297             NCURSES_CH_T blank = NewChar2(BLANK_TEXT, BLANK_ATTR);
298             AddAttr(blank, AttrOf(ch));
299             while (win->_curx < x) {
300                 if (waddch_literal(win, blank) == ERR)
301                     return (ERR);
302             }
303             break;
304         } else {
305             wclrtoeol(win);
306             win->_flags |= _WRAPPED;
307             if (++y > win->_regbottom) {
308                 x = win->_maxx;
309                 y--;
310                 if (win->_scroll) {
311                     scroll(win);
312                     x = 0;
313                 }
314             } else {
315                 x = 0;
316             }
317         }
318         break;
319     case '\n':
320         wclrtoeol(win);
321         if (++y > win->_regbottom) {
322             y--;
323             if (win->_scroll)
324                 scroll(win);
325             else
326                 return (ERR);
327         }
328         /* FALLTHRU */
329     case '\r':
330         x = 0;
331         win->_flags &= ~_WRAPPED;
332         break;
333     case '\b':
334         if (x == 0)
335             return (OK);
336         x--;
337         win->_flags &= ~_WRAPPED;
338         break;
339     default:
340         while (*s) {
341             NCURSES_CH_T sch;
342             SetChar(sch, *s++, AttrOf(ch));
343             if (waddch_literal(win, sch) == ERR)
344                 return ERR;
345         }
346         return (OK);
347     }
348
349     win->_curx = x;
350     win->_cury = y;
351
352     return (OK);
353 }
354
355 NCURSES_EXPORT(int)
356 _nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c)
357 /* export copy of waddch_nosync() so the string-put functions can use it */
358 {
359     return (waddch_nosync(win, c));
360 }
361
362 /*
363  * The versions below call _nc_synhook().  We wanted to avoid this in the
364  * version exported for string puts; they'll call _nc_synchook once at end
365  * of run.
366  */
367
368 /* These are actual entry points */
369
370 NCURSES_EXPORT(int)
371 waddch(WINDOW *win, const chtype ch)
372 {
373     int code = ERR;
374     NCURSES_CH_T wch;
375     SetChar2(wch, ch);
376
377     TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), win,
378                                       _tracechtype(ch)));
379
380     if (win && (waddch_nosync(win, wch) != ERR)) {
381         _nc_synchook(win);
382         code = OK;
383     }
384
385     TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
386     return (code);
387 }
388
389 NCURSES_EXPORT(int)
390 wechochar(WINDOW *win, const chtype ch)
391 {
392     int code = ERR;
393     NCURSES_CH_T wch;
394     SetChar2(wch, ch);
395
396     TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), win,
397                                       _tracechtype(ch)));
398
399     if (win && (waddch_nosync(win, wch) != ERR)) {
400         bool save_immed = win->_immed;
401         win->_immed = TRUE;
402         _nc_synchook(win);
403         win->_immed = save_immed;
404         code = OK;
405     }
406     TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
407     return (code);
408 }