1 /* $NetBSD: insstr.c,v 1.2 2007/05/28 15:01:56 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: insstr.c,v 1.2 2007/05/28 15:01:56 blymn Exp $");
46 #include "curses_private.h"
48 #ifndef _CURSES_USE_MACROS
52 * insert a multi-byte character string into the current window
55 insstr(const char *str
)
57 return winsstr(stdscr
, str
);
62 * insert a multi-byte character string into the current window
63 * with at most n characters
66 insnstr(const char *str
, int n
)
68 return winsnstr(stdscr
, str
, n
);
73 * Do an insert-string on the line at (y, x).
76 mvinsstr(int y
, int x
, const char *str
)
78 return mvwinsstr(stdscr
, y
, x
, str
);
83 * Do an insert-n-string on the line at (y, x).
86 mvinsnstr(int y
, int x
, const char *str
, int n
)
88 return mvwinsnstr(stdscr
, y
, x
, str
, n
);
93 * Do an insert-string on the line at (y, x) in the given window.
96 mvwinsstr(WINDOW
*win
, int y
, int x
, const char *str
)
98 if (wmove(win
, y
, x
) == ERR
)
101 return winsstr(stdscr
, str
);
106 * Do an insert-n-string on the line at (y, x) in the given window.
109 mvwinsnstr(WINDOW
*win
, int y
, int x
, const char *str
, int n
)
111 if (wmove(win
, y
, x
) == ERR
)
114 return winsnstr(stdscr
, str
, n
);
121 * Do an insert-string on the line, leaving (cury, curx) unchanged.
125 winsstr(WINDOW
*win
, const char *str
)
127 return winsnstr( win
, str
, -1 );
132 * Do an insert-n-string on the line, leaving (cury, curx) unchanged.
136 winsnstr(WINDOW
*win
, const char *str
, int n
)
138 __LDATA
*end
, *temp1
, *temp2
;
144 #endif /* HAVE_WCHAR */
146 /* find string length */
148 for ( scp
= str
, len
= 0; n
-- && *scp
++; ++len
);
150 for ( scp
= str
, len
= 0; *scp
++; ++len
);
152 __CTRACE(__CTRACE_INPUT
, "winsnstr: len = %d\n", len
);
156 end
= &win
->alines
[win
->cury
]->line
[win
->curx
];
157 if ( len
< win
->maxx
- win
->curx
) {
159 __CTRACE(__CTRACE_INPUT
, "winsnstr: shift %d cells\n", len
);
161 temp1
= &win
->alines
[win
->cury
]->line
[win
->maxx
- 1];
163 while (temp2
>= end
) {
174 #endif /* HAVE_WCHAR */
175 (void) memcpy(temp1
, temp2
, sizeof(__LDATA
));
180 for ( scp
= str
, temp1
= end
, x
= win
->curx
;
181 *scp
&& x
< len
+ win
->curx
&& x
< win
->maxx
;
182 scp
++, temp1
++, x
++ ) {
183 temp1
->ch
= (wchar_t)*scp
& __CHARTEXT
;
184 temp1
->attr
= win
->wattr
;
186 SET_WCOL( *temp1
, 1 );
187 #endif /* HAVE_WCHAR */
193 for ( i
= win
->curx
; i
< win
->curx
+ len
; i
++ ) {
194 __CTRACE(__CTRACE_INPUT
,
195 "winsnstr: (%d,%d)=('%c',%x)\n", win
->cury
, i
,
196 win
->alines
[win
->cury
]->line
[i
].ch
,
197 win
->alines
[win
->cury
]->line
[i
].attr
);
201 lnp
= win
->alines
[ win
->cury
];
202 lnp
->flags
|= __ISDIRTY
;
203 if ( win
->ch_off
< *lnp
->firstchp
)
204 *lnp
->firstchp
= win
->ch_off
;
205 if ( win
->ch_off
+ win
->maxx
- 1 > *lnp
->lastchp
)
206 *lnp
->lastchp
= win
->ch_off
+ win
->maxx
- 1;
207 __touchline(win
, (int)win
->cury
, (int)win
->curx
, (int)win
->maxx
- 1);