Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libcurses / add_wchstr.c
blob482168578ccd3011ccc9e7eeb57f312e815dc20c
1 /* $NetBSD: add_wchstr.c,v 1.2 2007/05/28 15:01:53 blymn Exp $ */
3 /*
4 * Copyright (c) 2005 The NetBSD Foundation Inc.
5 * All rights reserved.
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
13 * are met:
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
34 * SUCH DAMAGE.
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: add_wchstr.c,v 1.2 2007/05/28 15:01:53 blymn Exp $");
40 #endif /* not lint */
42 #include <stdlib.h>
44 #include "curses.h"
45 #include "curses_private.h"
48 * add_wchstr --
49 * Add a wide string to stdscr starting at (_cury, _curx).
51 int
52 add_wchstr(const cchar_t *wchstr)
54 #ifndef HAVE_WCHAR
55 return ERR;
56 #else
57 return wadd_wchnstr(stdscr, wchstr, -1);
58 #endif
63 * wadd_wchstr --
64 * Add a string to the given window starting at (_cury, _curx).
66 int
67 wadd_wchstr(WINDOW *win, const cchar_t *wchstr)
69 #ifndef HAVE_WCHAR
70 return ERR;
71 #else
72 return wadd_wchnstr(win, wchstr, -1);
73 #endif
78 * add_wchnstr --
79 * Add a string (at most n characters) to stdscr starting
80 * at (_cury, _curx). If n is negative, add the entire string.
82 int
83 add_wchnstr(const cchar_t *wchstr, int n)
85 #ifndef HAVE_WCHAR
86 return ERR;
87 #else
88 return wadd_wchnstr(stdscr, wchstr, n);
89 #endif
94 * mvadd_wchstr --
95 * Add a string to stdscr starting at (y, x)
97 int
98 mvadd_wchstr(int y, int x, const cchar_t *wchstr)
100 #ifndef HAVE_WCHAR
101 return ERR;
102 #else
103 return mvwadd_wchnstr(stdscr, y, x, wchstr, -1);
104 #endif
109 * mvwadd_wchstr --
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)
115 #ifndef HAVE_WCHAR
116 return ERR;
117 #else
118 return mvwadd_wchnstr(win, y, x, wchstr, -1);
119 #endif
124 * mvadd_wchnstr --
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)
131 #ifndef HAVE_WCHAR
132 return ERR;
133 #else
134 return mvwadd_wchnstr(stdscr, y, x, wchstr, n);
135 #endif
140 * mvwadd_wchnstr --
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)
147 #ifndef HAVE_WCHAR
148 return ERR;
149 #else
150 if (wmove(win, y, x) == ERR)
151 return ERR;
153 return wadd_wchnstr(win, wchstr, n);
154 #endif
159 * wadd_wchnstr --
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)
166 #ifndef HAVE_WCHAR
167 return ERR;
168 #else
169 const cchar_t *chp;
170 wchar_t wc;
171 int cw, x, y, sx, ex, newx, i, cnt;
172 __LDATA *lp, *tp;
173 nschar_t *np, *tnp;
174 __LINE *lnp;
176 #ifdef DEBUG
177 __CTRACE(__CTRACE_INPUT,
178 "wadd_wchnstr: win = %p, wchstr = %p, n = %d\n", win, wchstr, n);
179 #endif
181 if (!wchstr)
182 return OK;
184 /* compute length of the cchar string */
185 if (n < -1)
186 return ERR;
187 if (n >= 0)
188 for (chp = wchstr, cnt = 0; n && chp->vals[0];
189 n--, chp++, ++cnt);
190 else
191 for (chp = wchstr, cnt = 0; chp->vals[0]; chp++, ++cnt);
192 #ifdef DEBUG
193 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: len=%d\n", cnt);
194 #endif /* DEBUG */
195 chp = wchstr;
196 x = win->curx;
197 y = win->cury;
198 lp = &win->alines[y]->line[x];
199 lnp = win->alines[y];
201 cw = WCOL(*lp);
202 if (cw >= 0) {
203 sx = x;
204 } else {
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)
210 return ERR;
211 tp->attr = win->battr;
212 SET_WCOL(*tp, 1);
213 np = tp->nsp;
215 } else {
216 /* move to the start of current char */
217 lp += cw;
218 x += cw;
220 sx = x + cw;
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 */
228 ex = x;
229 while (cnt) {
230 x = ex;
231 wc = chp->vals[0];
232 #ifdef DEBUG
233 __CTRACE(__CTRACE_INPUT, "wadd_wchnstr: adding %x", wc);
234 #endif /* DEBUG */
235 cw = wcwidth(wc);
236 if (cw) {
237 /* spacing character */
238 #ifdef DEBUG
239 __CTRACE(__CTRACE_INPUT,
240 " as a spacing char(width=%d)\n", cw);
241 #endif /* DEBUG */
242 if (cw > win->maxx - ex) {
243 /* clear to EOL */
244 while (ex < win->maxx) {
245 lp->ch = (wchar_t)
246 btowc((int) win->bch);
247 if (_cursesi_copy_nsp(win->bnsp, lp)
248 == ERR)
249 return ERR;
250 lp->attr = win->battr;
251 SET_WCOL(*lp, 1);
252 lp++, ex++;
254 ex = win->maxx - 1;
255 break;
257 /* this could combine with the insertion of
258 * non-spacing char */
259 np = lp->nsp;
260 if (np) {
261 while (np) {
262 tnp = np->next;
263 free(np);
264 np = tnp;
266 lp->nsp = NULL;
268 lp->ch = chp->vals[0];
269 lp->attr = chp->attributes & WA_ATTRIBUTES;
270 SET_WCOL(*lp, cw);
271 if (chp->elements > 1) {
272 for (i = 1; i < chp->elements; i++) {
273 np = (nschar_t *)
274 malloc(sizeof(nschar_t));
275 if (!np)
276 return ERR;
277 np->ch = chp->vals[i];
278 np->next = lp->nsp;
279 lp->nsp = np;
282 lp++, ex++;
283 #ifdef DEBUG
284 __CTRACE(__CTRACE_INPUT,
285 "wadd_wchnstr: ex = %d, x = %d, cw = %d\n",
286 ex, x, cw);
287 #endif /* DEBUG */
288 while (ex - x <= cw - 1) {
289 np = lp->nsp;
290 if (np) {
291 while (np) {
292 tnp = np->next;
293 free(np);
294 np = tnp;
296 lp->nsp = NULL;
298 lp->ch = chp->vals[0];
299 lp->attr = chp->attributes & WA_ATTRIBUTES;
300 SET_WCOL(*lp, x - ex);
301 lp++, ex++;
303 } else {
304 /* non-spacing character */
305 #ifdef DEBUG
306 __CTRACE(__CTRACE_INPUT,
307 "wadd_wchnstr: as non-spacing char");
308 #endif /* DEBUG */
309 for (i = 0; i < chp->elements; i++) {
310 np = (nschar_t *)malloc(sizeof(nschar_t));
311 if (!np)
312 return ERR;
313 np->ch = chp->vals[i];
314 np->next = lp->nsp;
315 lp->nsp = np;
318 cnt--, chp++;
320 #ifdef DEBUG
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);
327 #endif /* DEBUG */
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);
334 return OK;
335 #endif /* HAVE_WCHAR */