]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/widechar/lib_get_wstr.c
ncurses 6.1 - patch 20190323
[ncurses.git] / ncurses / widechar / lib_get_wstr.c
1 /****************************************************************************
2  * Copyright (c) 2002-2011,2018 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Thomas E. Dickey                                                *
31  ****************************************************************************/
32
33 /*
34 **      lib_get_wstr.c
35 **
36 **      The routine wgetn_wstr().
37 **
38 */
39
40 #include <curses.priv.h>
41
42 MODULE_ID("$Id: lib_get_wstr.c,v 1.15 2018/09/01 20:13:00 tom Exp $")
43
44 static int
45 wadd_wint(WINDOW *win, wint_t *src)
46 {
47     cchar_t tmp;
48     wchar_t wch[2];
49
50     wch[0] = (wchar_t) (*src);
51     wch[1] = 0;
52     setcchar(&tmp, wch, A_NORMAL, (short) 0, NULL);
53     return wadd_wch(win, &tmp);
54 }
55
56 /*
57  * This wipes out the last character, no matter whether it was a tab, control
58  * or other character, and handles reverse wraparound.
59  */
60 static wint_t *
61 WipeOut(WINDOW *win, int y, int x, wint_t *first, wint_t *last, int echoed)
62 {
63     if (last > first) {
64         *--last = '\0';
65         if (echoed) {
66             int y1 = win->_cury;
67             int x1 = win->_curx;
68             int n;
69
70             wmove(win, y, x);
71             for (n = 0; first[n] != 0; ++n) {
72                 wadd_wint(win, first + n);
73             }
74             getyx(win, y, x);
75             while (win->_cury < y1
76                    || (win->_cury == y1 && win->_curx < x1))
77                 waddch(win, (chtype) ' ');
78
79             wmove(win, y, x);
80         }
81     }
82     return last;
83 }
84
85 NCURSES_EXPORT(int)
86 wgetn_wstr(WINDOW *win, wint_t *str, int maxlen)
87 {
88     SCREEN *sp = _nc_screen_of(win);
89     TTY buf;
90     bool oldnl, oldecho, oldraw, oldcbreak;
91     wint_t erasec;
92     wint_t killc;
93     wint_t *oldstr = str;
94     wint_t *tmpstr = str;
95     wint_t ch;
96     int y, x, code;
97
98     T((T_CALLED("wgetn_wstr(%p,%p, %d)"), (void *) win, (void *) str, maxlen));
99
100     if (!win)
101         returnCode(ERR);
102
103     maxlen = _nc_getstr_limit(maxlen);
104
105     _nc_get_tty_mode(&buf);
106
107     oldnl = sp->_nl;
108     oldecho = sp->_echo;
109     oldraw = sp->_raw;
110     oldcbreak = sp->_cbreak;
111     nl();
112     noecho();
113     noraw();
114     cbreak();
115
116     erasec = (wint_t) erasechar();
117     killc = (wint_t) killchar();
118
119     getyx(win, y, x);
120
121     if (is_wintouched(win) || (win->_flags & _HASMOVED))
122         wrefresh(win);
123
124     while ((code = wget_wch(win, &ch)) != ERR) {
125         /*
126          * Map special characters into key-codes.
127          */
128         if (ch == '\r')
129             ch = '\n';
130         if (ch == '\n') {
131             code = KEY_CODE_YES;
132             ch = KEY_ENTER;
133         }
134         if (ch < KEY_MIN) {
135             if (ch == erasec) {
136                 ch = KEY_BACKSPACE;
137                 code = KEY_CODE_YES;
138             }
139             if (ch == killc) {
140                 ch = KEY_EOL;
141                 code = KEY_CODE_YES;
142             }
143         }
144         if (code == KEY_CODE_YES) {
145             /*
146              * Some terminals (the Wyse-50 is the most common) generate a \n
147              * from the down-arrow key.  With this logic, it's the user's
148              * choice whether to set kcud=\n for wget_wch(); terminating
149              * *getn_wstr() with \n should work either way.
150              */
151             if (ch == KEY_DOWN || ch == KEY_ENTER) {
152                 if (oldecho == TRUE
153                     && win->_cury == win->_maxy
154                     && win->_scroll)
155                     wechochar(win, (chtype) '\n');
156                 break;
157             }
158             if (ch == KEY_LEFT || ch == KEY_BACKSPACE) {
159                 if (tmpstr > oldstr) {
160                     tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
161                 }
162             } else if (ch == KEY_EOL) {
163                 while (tmpstr > oldstr) {
164                     tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
165                 }
166             } else {
167                 beep();
168             }
169         } else if (tmpstr - oldstr >= maxlen) {
170             beep();
171         } else {
172             *tmpstr++ = ch;
173             *tmpstr = 0;
174             if (oldecho == TRUE) {
175                 int oldy = win->_cury;
176
177                 if (wadd_wint(win, tmpstr - 1) == ERR) {
178                     /*
179                      * We can't really use the lower-right corner for input,
180                      * since it'll mess up bookkeeping for erases.
181                      */
182                     win->_flags &= ~_WRAPPED;
183                     waddch(win, (chtype) ' ');
184                     tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
185                     continue;
186                 } else if (win->_flags & _WRAPPED) {
187                     /*
188                      * If the last waddch forced a wrap & scroll, adjust our
189                      * reference point for erasures.
190                      */
191                     if (win->_scroll
192                         && oldy == win->_maxy
193                         && win->_cury == win->_maxy) {
194                         if (--y <= 0) {
195                             y = 0;
196                         }
197                     }
198                     win->_flags &= ~_WRAPPED;
199                 }
200                 wrefresh(win);
201             }
202         }
203     }
204
205     win->_curx = 0;
206     win->_flags &= ~_WRAPPED;
207     if (win->_cury < win->_maxy)
208         win->_cury++;
209     wrefresh(win);
210
211     /* Restore with a single I/O call, to fix minor asymmetry between
212      * raw/noraw, etc.
213      */
214     sp->_nl = oldnl;
215     sp->_echo = oldecho;
216     sp->_raw = oldraw;
217     sp->_cbreak = oldcbreak;
218
219     (void) _nc_set_tty_mode(&buf);
220
221     *tmpstr = 0;
222     if (code == ERR) {
223         if (tmpstr == oldstr) {
224             *tmpstr++ = WEOF;
225             *tmpstr = 0;
226         }
227         returnCode(ERR);
228     }
229
230     T(("wgetn_wstr returns %s", _nc_viswibuf(oldstr)));
231
232     returnCode(OK);
233 }