Remove NO_WERROR.
[dragonfly/port-amd64.git] / gnu / lib / libdialog / lineedit.c
blob7bfe0e0bba3d9e3c383a4bc11cd4ce01eb36cfdc
1 /*
2 * Changes Copyright (C) 1995 by Andrey A. Chernov, Moscow
4 * Original Copyright:
6 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <dialog.h>
25 #include "dialog.priv.h"
27 static void redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit, int attr_mask);
30 * Line editor
32 int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, chtype attr, int first, unsigned char *result, int attr_mask)
34 int i, key;
35 chtype old_attr;
36 static int input_x, scroll;
37 static unsigned char instr[MAX_LEN+1];
38 unsigned char erase_char = erasechar();
39 unsigned char kill_char = killchar();
40 #ifdef notyet
41 unsignec char werase_char = cur_term->Ottyb.c_cc[VWERASE];
42 #endif
44 old_attr = getattrs(dialog);
45 keypad(dialog, TRUE);
47 if (first) {
48 memset(instr, 0, sizeof(instr));
49 strcpy(instr, result);
50 i = strlen(instr);
51 /* input_x = i % box_width;*/
52 input_x = (i > box_width) ? box_width - 1 : i;
53 /* scroll = i - input_x;*/
54 scroll = (i > box_width) ? i - box_width + 1: 0;
56 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
58 for (;;) {
59 wattrset(dialog, attr);
60 wrefresh(dialog);
61 key = wgetch(dialog);
62 switch (key) {
63 case ctrl('q'):
64 goto ret;
65 break;
66 case KEY_F(1):
67 display_helpfile();
68 break;
69 case TAB:
70 case KEY_BTAB:
71 case KEY_UP:
72 case KEY_DOWN:
73 case ESC:
74 case '\r':
75 case '\n':
76 for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
77 instr[i] = '\0';
78 if (key == '\r')
79 key = '\n';
80 goto ret;
81 case '\025':
82 case '\030':
83 kill_it:
84 input_x = scroll = 0;
85 /* fall through */
86 case '\013':
87 case KEY_EOL:
88 memset(instr + scroll + input_x, '\0', sizeof(instr) - scroll - input_x);
89 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
90 continue;
91 case '\001':
92 case KEY_HOME:
93 input_x = scroll = 0;
94 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
95 continue;
96 case '\005':
97 case KEY_END:
98 for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
99 instr[i] = '\0';
100 i++;
101 input_x = i % box_width;
102 scroll = i - input_x;
103 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
104 continue;
105 case '\002':
106 case KEY_LEFT:
107 if (input_x || scroll) {
108 if (!input_x) {
109 int oldscroll = scroll;
110 scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
111 input_x = oldscroll - 1 - scroll;
112 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
113 } else {
114 input_x--;
115 wmove(dialog, box_y, input_x + box_x);
117 } else
118 beep();
119 continue;
120 case '\006':
121 case KEY_RIGHT:
122 if ( scroll+input_x < MAX_LEN
123 && (flen < 0 || scroll+input_x < flen)
125 if (!instr[scroll+input_x])
126 instr[scroll+input_x] = ' ';
127 if (input_x == box_width-1) {
128 scroll++;
129 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
131 else {
132 wmove(dialog, box_y, input_x + box_x);
133 waddch(dialog, instr[scroll+input_x]);
134 input_x++;
136 } else
137 beep(); /* Alarm user about overflow */
138 continue;
139 case '\b':
140 case '\177':
141 case KEY_BACKSPACE:
142 erase_it:
143 if (input_x || scroll) {
144 i = strlen(instr);
145 memmove(instr+scroll+input_x-1, instr+scroll+input_x, i-(scroll+input_x)+1);
146 if (!input_x) {
147 int oldscroll = scroll;
148 scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
149 input_x = oldscroll - 1 - scroll;
150 } else
151 input_x--;
152 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
153 } else
154 beep();
155 continue;
156 case '\004':
157 case KEY_DC:
158 for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
159 instr[i] = '\0';
160 i++;
161 if (i == 0) {
162 beep();
163 continue;
165 memmove(instr+scroll+input_x, instr+scroll+input_x+1, i-(scroll+input_x));
166 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
167 continue;
168 default:
169 if (CCEQ(key, erase_char))
170 goto erase_it;
171 if (CCEQ(key, kill_char))
172 goto kill_it;
173 if (key < 0x100 && isprint(key)) {
174 for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
175 instr[i] = '\0';
176 i++;
177 if (i < MAX_LEN && (flen < 0 || scroll+input_x < flen)) {
178 if (flen < 0 || i < flen)
179 memmove(instr+scroll+input_x+1, instr+scroll+input_x, i-(scroll+input_x));
180 instr[scroll+input_x] = key;
181 if (input_x == box_width-1 && (flen < 0 || i < flen))
182 scroll++;
183 else
184 input_x++;
185 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
186 } else
187 beep(); /* Alarm user about overflow */
188 continue;
192 ret:
193 redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, TRUE, attr_mask);
194 wrefresh(dialog);
195 strcpy(result, instr);
196 return key;
199 static void
200 redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit, int attr_mask)
202 int i, fix_len;
204 wattrset(dialog, fexit ? old_attr : attr);
205 wmove(dialog, box_y, box_x);
206 fix_len = flen >= 0 ? MIN(flen-scroll,box_width) : box_width;
207 for (i = 0; i < fix_len; i++)
208 waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
209 wattrset(dialog, old_attr);
210 for ( ; i < box_width; i++)
211 waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
212 wmove(dialog, box_y, input_x + box_x);