Standardize on the vi editing directives being on the first line.
[oweals/busybox.git] / scripts / config / lxdialog / inputbox.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  inputbox.c -- implements the input box
4  *
5  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "dialog.h"
24
25 char dialog_input_result[MAX_LEN + 1];
26
27 /*
28  *  Print the termination buttons
29  */
30 static void
31 print_buttons(WINDOW *dialog, int height, int width, int selected)
32 {
33     int x = width / 2 - 11;
34     int y = height - 2;
35
36     print_button (dialog, "  Ok  ", y, x, selected==0);
37     print_button (dialog, " Help ", y, x + 14, selected==1);
38
39     wmove(dialog, y, x+1+14*selected);
40     wrefresh(dialog);
41 }
42
43 /*
44  * Display a dialog box for inputing a string
45  */
46 int
47 dialog_inputbox (const char *title, const char *prompt, int height, int width,
48                  const char *init)
49 {
50     int i, x, y, box_y, box_x, box_width;
51     int input_x = 0, scroll = 0, key = 0, button = -1;
52     char *instr = dialog_input_result;
53     WINDOW *dialog;
54
55     /* center dialog box on screen */
56     x = (COLS - width) / 2;
57     y = (LINES - height) / 2;
58
59
60     draw_shadow (stdscr, y, x, height, width);
61
62     dialog = newwin (height, width, y, x);
63     keypad (dialog, TRUE);
64
65     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
66     wattrset (dialog, border_attr);
67     mvwaddch (dialog, height-3, 0, ACS_LTEE);
68     for (i = 0; i < width - 2; i++)
69         waddch (dialog, ACS_HLINE);
70     wattrset (dialog, dialog_attr);
71     waddch (dialog, ACS_RTEE);
72
73     if (title != NULL && strlen(title) >= width-2 ) {
74         /* truncate long title -- mec */
75         char * title2 = malloc(width-2+1);
76         memcpy( title2, title, width-2 );
77         title2[width-2] = '\0';
78         title = title2;
79     }
80
81     if (title != NULL) {
82         wattrset (dialog, title_attr);
83         mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
84         waddstr (dialog, (char *)title);
85         waddch (dialog, ' ');
86     }
87
88     wattrset (dialog, dialog_attr);
89     print_autowrap (dialog, prompt, width - 2, 1, 3);
90
91     /* Draw the input field box */
92     box_width = width - 6;
93     getyx (dialog, y, x);
94     box_y = y + 2;
95     box_x = (width - box_width) / 2;
96     draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2,
97               border_attr, dialog_attr);
98
99     print_buttons(dialog, height, width, 0);
100
101     /* Set up the initial value */
102     wmove (dialog, box_y, box_x);
103     wattrset (dialog, inputbox_attr);
104
105     if (!init)
106         instr[0] = '\0';
107     else
108         strcpy (instr, init);
109
110     input_x = strlen (instr);
111
112     if (input_x >= box_width) {
113         scroll = input_x - box_width + 1;
114         input_x = box_width - 1;
115         for (i = 0; i < box_width - 1; i++)
116             waddch (dialog, instr[scroll + i]);
117     } else
118         waddstr (dialog, instr);
119
120     wmove (dialog, box_y, box_x + input_x);
121
122     wrefresh (dialog);
123
124     while (key != ESC) {
125         key = wgetch (dialog);
126
127         if (button == -1) {     /* Input box selected */
128             switch (key) {
129             case TAB:
130             case KEY_UP:
131             case KEY_DOWN:
132                 break;
133             case KEY_LEFT:
134                 continue;
135             case KEY_RIGHT:
136                 continue;
137             case KEY_BACKSPACE:
138             case 127:
139                 if (input_x || scroll) {
140                     wattrset (dialog, inputbox_attr);
141                     if (!input_x) {
142                         scroll = scroll < box_width - 1 ?
143                             0 : scroll - (box_width - 1);
144                         wmove (dialog, box_y, box_x);
145                         for (i = 0; i < box_width; i++)
146                             waddch (dialog, instr[scroll + input_x + i] ?
147                                     instr[scroll + input_x + i] : ' ');
148                         input_x = strlen (instr) - scroll;
149                     } else
150                         input_x--;
151                     instr[scroll + input_x] = '\0';
152                     mvwaddch (dialog, box_y, input_x + box_x, ' ');
153                     wmove (dialog, box_y, input_x + box_x);
154                     wrefresh (dialog);
155                 }
156                 continue;
157             default:
158                 if (key < 0x100 && isprint (key)) {
159                     if (scroll + input_x < MAX_LEN) {
160                         wattrset (dialog, inputbox_attr);
161                         instr[scroll + input_x] = key;
162                         instr[scroll + input_x + 1] = '\0';
163                         if (input_x == box_width - 1) {
164                             scroll++;
165                             wmove (dialog, box_y, box_x);
166                             for (i = 0; i < box_width - 1; i++)
167                                 waddch (dialog, instr[scroll + i]);
168                         } else {
169                             wmove (dialog, box_y, input_x++ + box_x);
170                             waddch (dialog, key);
171                         }
172                         wrefresh (dialog);
173                     } else
174                         flash ();       /* Alarm user about overflow */
175                     continue;
176                 }
177             }
178         }
179         switch (key) {
180         case 'O':
181         case 'o':
182             delwin (dialog);
183             return 0;
184         case 'H':
185         case 'h':
186             delwin (dialog);
187             return 1;
188         case KEY_UP:
189         case KEY_LEFT:
190             switch (button) {
191             case -1:
192                 button = 1;     /* Indicates "Cancel" button is selected */
193                 print_buttons(dialog, height, width, 1);
194                 break;
195             case 0:
196                 button = -1;    /* Indicates input box is selected */
197                 print_buttons(dialog, height, width, 0);
198                 wmove (dialog, box_y, box_x + input_x);
199                 wrefresh (dialog);
200                 break;
201             case 1:
202                 button = 0;     /* Indicates "OK" button is selected */
203                 print_buttons(dialog, height, width, 0);
204                 break;
205             }
206             break;
207         case TAB:
208         case KEY_DOWN:
209         case KEY_RIGHT:
210             switch (button) {
211             case -1:
212                 button = 0;     /* Indicates "OK" button is selected */
213                 print_buttons(dialog, height, width, 0);
214                 break;
215             case 0:
216                 button = 1;     /* Indicates "Cancel" button is selected */
217                 print_buttons(dialog, height, width, 1);
218                 break;
219             case 1:
220                 button = -1;    /* Indicates input box is selected */
221                 print_buttons(dialog, height, width, 0);
222                 wmove (dialog, box_y, box_x + input_x);
223                 wrefresh (dialog);
224                 break;
225             }
226             break;
227         case ' ':
228         case '\n':
229             delwin (dialog);
230             return (button == -1 ? 0 : button);
231         case 'X':
232         case 'x':
233             key = ESC;
234         case ESC:
235             break;
236         }
237     }
238
239     delwin (dialog);
240     return -1;                  /* ESC pressed */
241 }