1 /* $NetBSD: add_wchstr.c,v 1.4 2010/02/23 19:48:26 drochner Exp $ */
4 * Copyright (c) 2005 The NetBSD Foundation Inc.
7 * This code is derived from code donated to the NetBSD Foundation
8 * by Ruibiao Qiu <ruibiao@arl.wustl.edu,ruibiao@gmail.com>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: add_wchstr.c,v 1.4 2010/02/23 19:48:26 drochner Exp $");
45 #include "curses_private.h"
49 * Add a wide string to stdscr starting at (_cury, _curx).
52 add_wchstr(const cchar_t
*wchstr
)
57 return wadd_wchnstr(stdscr
, wchstr
, -1);
64 * Add a string to the given window starting at (_cury, _curx).
67 wadd_wchstr(WINDOW
*win
, const cchar_t
*wchstr
)
72 return wadd_wchnstr(win
, wchstr
, -1);
79 * Add a string (at most n characters) to stdscr starting
80 * at (_cury, _curx). If n is negative, add the entire string.
83 add_wchnstr(const cchar_t
*wchstr
, int n
)
88 return wadd_wchnstr(stdscr
, wchstr
, n
);
95 * Add a string to stdscr starting at (y, x)
98 mvadd_wchstr(int y
, int x
, const cchar_t
*wchstr
)
103 return mvwadd_wchnstr(stdscr
, y
, x
, wchstr
, -1);
110 * Add a string to the given window starting at (y, x)
113 mvwadd_wchstr(WINDOW
*win
, int y
, int x
, const cchar_t
*wchstr
)
118 return mvwadd_wchnstr(win
, y
, x
, wchstr
, -1);
125 * Add a string of at most n characters to stdscr
126 * starting at (y, x).
129 mvadd_wchnstr(int y
, int x
, const cchar_t
*wchstr
, int n
)
134 return mvwadd_wchnstr(stdscr
, y
, x
, wchstr
, n
);
141 * Add a string of at most n characters to the given window
142 * starting at (y, x).
145 mvwadd_wchnstr(WINDOW
*win
, int y
, int x
, const cchar_t
*wchstr
, int n
)
150 if (wmove(win
, y
, x
) == ERR
)
153 return wadd_wchnstr(win
, wchstr
, n
);
160 * Add a string (at most n wide characters) to the given window
161 * starting at (_cury, _curx). If n is -1, add the entire string.
164 wadd_wchnstr(WINDOW
*win
, const cchar_t
*wchstr
, int n
)
171 int cw
, x
, y
, sx
, ex
, newx
, i
, cnt
;
177 __CTRACE(__CTRACE_INPUT
,
178 "wadd_wchnstr: win = %p, wchstr = %p, n = %d\n", win
, wchstr
, n
);
184 /* compute length of the cchar string */
188 for (chp
= wchstr
, cnt
= 0; n
&& chp
->vals
[0];
191 for (chp
= wchstr
, cnt
= 0; chp
->vals
[0]; chp
++, ++cnt
);
193 __CTRACE(__CTRACE_INPUT
, "wadd_wchnstr: len=%d\n", cnt
);
198 lp
= &win
->alines
[y
]->line
[x
];
199 lnp
= win
->alines
[y
];
205 if (wcwidth(chp
->vals
[0])) {
206 /* clear the partial character before cursor */
207 for (tp
= lp
+ cw
; tp
< lp
; tp
++) {
208 tp
->ch
= (wchar_t) btowc((int) win
->bch
);
209 if (_cursesi_copy_nsp(win
->bnsp
, tp
) == ERR
)
211 tp
->attr
= win
->battr
;
216 /* move to the start of current char */
222 lnp
->flags
|= __ISDIRTY
;
223 newx
= sx
+ win
->ch_off
;
224 if (newx
< *lnp
->firstchp
)
225 *lnp
->firstchp
= newx
;
227 /* add characters in the string */
233 __CTRACE(__CTRACE_INPUT
, "wadd_wchnstr: adding %x", wc
);
239 /* spacing character */
241 __CTRACE(__CTRACE_INPUT
,
242 " as a spacing char(width=%d)\n", cw
);
244 if (cw
> win
->maxx
- ex
) {
246 while (ex
< win
->maxx
) {
248 btowc((int) win
->bch
);
249 if (_cursesi_copy_nsp(win
->bnsp
, lp
)
252 lp
->attr
= win
->battr
;
259 /* this could combine with the insertion of
260 * non-spacing char */
270 lp
->ch
= chp
->vals
[0];
271 lp
->attr
= chp
->attributes
& WA_ATTRIBUTES
;
273 if (chp
->elements
> 1) {
274 for (i
= 1; i
< chp
->elements
; i
++) {
276 malloc(sizeof(nschar_t
));
279 np
->ch
= chp
->vals
[i
];
286 __CTRACE(__CTRACE_INPUT
,
287 "wadd_wchnstr: ex = %d, x = %d, cw = %d\n",
290 while (ex
- x
<= cw
- 1) {
300 lp
->ch
= chp
->vals
[0];
301 lp
->attr
= chp
->attributes
& WA_ATTRIBUTES
;
302 SET_WCOL(*lp
, x
- ex
);
306 /* non-spacing character */
308 __CTRACE(__CTRACE_INPUT
,
309 "wadd_wchnstr: as non-spacing char");
311 for (i
= 0; i
< chp
->elements
; i
++) {
312 np
= (nschar_t
*)malloc(sizeof(nschar_t
));
315 np
->ch
= chp
->vals
[i
];
323 for (i
= sx
; i
< ex
; i
++) {
324 __CTRACE(__CTRACE_INPUT
, "wadd_wchnstr: (%d,%d)=(%x,%x,%p)\n",
325 win
->cury
, i
, win
->alines
[win
->cury
]->line
[i
].ch
,
326 win
->alines
[win
->cury
]->line
[i
].attr
,
327 win
->alines
[win
->cury
]->line
[i
].nsp
);
330 lnp
->flags
|= __ISDIRTY
;
331 newx
= ex
+ win
->ch_off
;
332 if (newx
> *lnp
->lastchp
)
333 *lnp
->lastchp
= newx
;
334 __touchline(win
, y
, sx
, ex
);
337 #endif /* HAVE_WCHAR */