1 /****************************************************************************
2 * Copyright (c) 2002-2008,2009 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey *
31 ****************************************************************************/
36 ** The routine wgetn_wstr().
40 #include <curses.priv.h>
42 MODULE_ID("$Id: lib_get_wstr.c,v 1.12 2009/10/24 22:38:11 tom Exp $")
45 wadd_wint(WINDOW
*win
, wint_t *src
)
50 wch
[0] = (wchar_t) (*src
);
52 setcchar(&tmp
, wch
, A_NORMAL
, 0, NULL
);
53 return wadd_wch(win
, &tmp
);
57 * This wipes out the last character, no matter whether it was a tab, control
58 * or other character, and handles reverse wraparound.
61 WipeOut(WINDOW
*win
, int y
, int x
, wint_t *first
, wint_t *last
, bool echoed
)
71 for (n
= 0; first
[n
] != 0; ++n
) {
72 wadd_wint(win
, first
+ n
);
75 while (win
->_cury
< y1
76 || (win
->_cury
== y1
&& win
->_curx
< x1
))
77 waddch(win
, (chtype
) ' ');
86 wgetn_wstr(WINDOW
*win
, wint_t *str
, int maxlen
)
88 SCREEN
*sp
= _nc_screen_of(win
);
90 bool oldnl
, oldecho
, oldraw
, oldcbreak
;
98 T((T_CALLED("wgetn_wstr(%p,%p, %d)"), (void *) win
, (void *) str
, maxlen
));
103 _nc_get_tty_mode(&buf
);
108 oldcbreak
= sp
->_cbreak
;
114 erasec
= (wint_t) erasechar();
115 killc
= (wint_t) killchar();
119 if (is_wintouched(win
) || (win
->_flags
& _HASMOVED
))
122 while ((code
= wget_wch(win
, &ch
)) != ERR
) {
124 * Map special characters into key-codes.
142 if (code
== KEY_CODE_YES
) {
144 * Some terminals (the Wyse-50 is the most common) generate a \n
145 * from the down-arrow key. With this logic, it's the user's
146 * choice whether to set kcud=\n for wget_wch(); terminating
147 * *getn_wstr() with \n should work either way.
149 if (ch
== KEY_DOWN
|| ch
== KEY_ENTER
) {
151 && win
->_cury
== win
->_maxy
153 wechochar(win
, (chtype
) '\n');
156 if (ch
== KEY_LEFT
|| ch
== KEY_BACKSPACE
) {
157 if (tmpstr
> oldstr
) {
158 tmpstr
= WipeOut(win
, y
, x
, oldstr
, tmpstr
, oldecho
);
160 } else if (ch
== KEY_EOL
) {
161 while (tmpstr
> oldstr
) {
162 tmpstr
= WipeOut(win
, y
, x
, oldstr
, tmpstr
, oldecho
);
167 } else if (maxlen
>= 0 && tmpstr
- oldstr
>= maxlen
) {
172 if (oldecho
== TRUE
) {
173 int oldy
= win
->_cury
;
175 if (wadd_wint(win
, tmpstr
- 1) == ERR
) {
177 * We can't really use the lower-right corner for input,
178 * since it'll mess up bookkeeping for erases.
180 win
->_flags
&= ~_WRAPPED
;
181 waddch(win
, (chtype
) ' ');
182 tmpstr
= WipeOut(win
, y
, x
, oldstr
, tmpstr
, oldecho
);
184 } else if (win
->_flags
& _WRAPPED
) {
186 * If the last waddch forced a wrap & scroll, adjust our
187 * reference point for erasures.
190 && oldy
== win
->_maxy
191 && win
->_cury
== win
->_maxy
) {
196 win
->_flags
&= ~_WRAPPED
;
204 win
->_flags
&= ~_WRAPPED
;
205 if (win
->_cury
< win
->_maxy
)
209 /* Restore with a single I/O call, to fix minor asymmetry between
215 sp
->_cbreak
= oldcbreak
;
217 (void) _nc_set_tty_mode(&buf
);
221 if (tmpstr
== oldstr
) {
228 T(("wgetn_wstr returns %s", _nc_viswibuf(oldstr
)));