1 /* $NetBSD: add_wchstr.c,v 1.2 2007/05/28 15:01:53 blymn 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.2 2007/05/28 15:01:53 blymn 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
);
237 /* spacing character */
239 __CTRACE(__CTRACE_INPUT
,
240 " as a spacing char(width=%d)\n", cw
);
242 if (cw
> win
->maxx
- ex
) {
244 while (ex
< win
->maxx
) {
246 btowc((int) win
->bch
);
247 if (_cursesi_copy_nsp(win
->bnsp
, lp
)
250 lp
->attr
= win
->battr
;
257 /* this could combine with the insertion of
258 * non-spacing char */
268 lp
->ch
= chp
->vals
[0];
269 lp
->attr
= chp
->attributes
& WA_ATTRIBUTES
;
271 if (chp
->elements
> 1) {
272 for (i
= 1; i
< chp
->elements
; i
++) {
274 malloc(sizeof(nschar_t
));
277 np
->ch
= chp
->vals
[i
];
284 __CTRACE(__CTRACE_INPUT
,
285 "wadd_wchnstr: ex = %d, x = %d, cw = %d\n",
288 while (ex
- x
<= cw
- 1) {
298 lp
->ch
= chp
->vals
[0];
299 lp
->attr
= chp
->attributes
& WA_ATTRIBUTES
;
300 SET_WCOL(*lp
, x
- ex
);
304 /* non-spacing character */
306 __CTRACE(__CTRACE_INPUT
,
307 "wadd_wchnstr: as non-spacing char");
309 for (i
= 0; i
< chp
->elements
; i
++) {
310 np
= (nschar_t
*)malloc(sizeof(nschar_t
));
313 np
->ch
= chp
->vals
[i
];
321 for (i
= sx
; i
< ex
; i
++) {
322 __CTRACE(__CTRACE_INPUT
, "wadd_wchnstr: (%d,%d)=(%x,%x,%p)\n",
323 win
->cury
, i
, win
->alines
[win
->cury
]->line
[i
].ch
,
324 win
->alines
[win
->cury
]->line
[i
].attr
,
325 win
->alines
[win
->cury
]->line
[i
].nsp
);
328 lnp
->flags
|= __ISDIRTY
;
329 newx
= ex
+ win
->ch_off
;
330 if (newx
> *lnp
->lastchp
)
331 *lnp
->lastchp
= newx
;
332 __touchline(win
, y
, sx
, ex
);
335 #endif /* HAVE_WCHAR */