OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / extra / config / lxdialog / menubox.c
1 /*
2  *  menubox.c -- implements the menu box
3  *
4  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcapw@cfw.com)
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version 2
10  *  of the License, or (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /*
22  *  Changes by Clifford Wolf (god@clifford.at)
23  *
24  *  [ 1998-06-13 ]
25  *
26  *    *)  A bugfix for the Page-Down problem
27  *
28  *    *)  Formerly when I used Page Down and Page Up, the cursor would be set
29  *        to the first position in the menu box.  Now lxdialog is a bit
30  *        smarter and works more like other menu systems (just have a look at
31  *        it).
32  *
33  *    *)  Formerly if I selected something my scrolling would be broken because
34  *        lxdialog is re-invoked by the Menuconfig shell script, can't
35  *        remember the last scrolling position, and just sets it so that the
36  *        cursor is at the bottom of the box.  Now it writes the temporary file
37  *        lxdialog.scrltmp which contains this information. The file is
38  *        deleted by lxdialog if the user leaves a submenu or enters a new
39  *        one, but it would be nice if Menuconfig could make another "rm -f"
40  *        just to be sure.  Just try it out - you will recognise a difference!
41  *
42  *  [ 1998-06-14 ]
43  *
44  *    *)  Now lxdialog is crash-safe against broken "lxdialog.scrltmp" files
45  *        and menus change their size on the fly.
46  *
47  *    *)  If for some reason the last scrolling position is not saved by
48  *        lxdialog, it sets the scrolling so that the selected item is in the
49  *        middle of the menu box, not at the bottom.
50  *
51  * 02 January 1999, Michael Elizabeth Chastain (mec@shout.net)
52  * Reset 'scroll' to 0 if the value from lxdialog.scrltmp is bogus.
53  * This fixes a bug in Menuconfig where using ' ' to descend into menus
54  * would leave mis-synchronized lxdialog.scrltmp files lying around,
55  * fscanf would read in 'scroll', and eventually that value would get used.
56  */
57
58 #include "dialog.h"
59
60 static int menu_width, item_x;
61
62 /*
63  * Print menu item
64  */
65 static void do_print_item(WINDOW * win, const char *item, int line_y,
66                           int selected, int hotkey)
67 {
68         int j;
69         char *menu_item = malloc(menu_width + 1);
70
71         strncpy(menu_item, item, menu_width - item_x);
72         menu_item[menu_width - item_x] = '\0';
73         j = first_alpha(menu_item, "YyNnMmHh");
74
75         /* Clear 'residue' of last item */
76         wattrset(win, dlg.menubox.atr);
77         wmove(win, line_y, 0);
78 #if OLD_NCURSES
79         {
80                 int i;
81                 for (i = 0; i < menu_width; i++)
82                         waddch(win, ' ');
83         }
84 #else
85         wclrtoeol(win);
86 #endif
87         wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
88         mvwaddstr(win, line_y, item_x, menu_item);
89         if (hotkey) {
90                 wattrset(win, selected ? dlg.tag_key_selected.atr
91                          : dlg.tag_key.atr);
92                 mvwaddch(win, line_y, item_x + j, menu_item[j]);
93         }
94         if (selected) {
95                 wmove(win, line_y, item_x + 1);
96         }
97         free(menu_item);
98         wrefresh(win);
99 }
100
101 #define print_item(index, choice, selected)                             \
102 do {                                                                    \
103         item_set(index);                                                \
104         do_print_item(menu, item_str(), choice, selected, !item_is_tag(':')); \
105 } while (0)
106
107 /*
108  * Print the scroll indicators.
109  */
110 static void print_arrows(WINDOW * win, int item_no, int scroll, int y, int x,
111                          int height)
112 {
113         int cur_y, cur_x;
114
115         getyx(win, cur_y, cur_x);
116
117         wmove(win, y, x);
118
119         if (scroll > 0) {
120                 wattrset(win, dlg.uarrow.atr);
121                 waddch(win, ACS_UARROW);
122                 waddstr(win, "(-)");
123         } else {
124                 wattrset(win, dlg.menubox.atr);
125                 waddch(win, ACS_HLINE);
126                 waddch(win, ACS_HLINE);
127                 waddch(win, ACS_HLINE);
128                 waddch(win, ACS_HLINE);
129         }
130
131         y = y + height + 1;
132         wmove(win, y, x);
133         wrefresh(win);
134
135         if ((height < item_no) && (scroll + height < item_no)) {
136                 wattrset(win, dlg.darrow.atr);
137                 waddch(win, ACS_DARROW);
138                 waddstr(win, "(+)");
139         } else {
140                 wattrset(win, dlg.menubox_border.atr);
141                 waddch(win, ACS_HLINE);
142                 waddch(win, ACS_HLINE);
143                 waddch(win, ACS_HLINE);
144                 waddch(win, ACS_HLINE);
145         }
146
147         wmove(win, cur_y, cur_x);
148         wrefresh(win);
149 }
150
151 /*
152  * Display the termination buttons.
153  */
154 static void print_buttons(WINDOW * win, int height, int width, int selected)
155 {
156         int x = width / 2 - 16;
157         int y = height - 2;
158
159         print_button(win, gettext("Select"), y, x, selected == 0);
160         print_button(win, gettext(" Exit "), y, x + 12, selected == 1);
161         print_button(win, gettext(" Help "), y, x + 24, selected == 2);
162
163         wmove(win, y, x + 1 + 12 * selected);
164         wrefresh(win);
165 }
166
167 /* scroll up n lines (n may be negative) */
168 static void do_scroll(WINDOW *win, int *scroll, int n)
169 {
170         /* Scroll menu up */
171         scrollok(win, TRUE);
172         wscrl(win, n);
173         scrollok(win, FALSE);
174         *scroll = *scroll + n;
175         wrefresh(win);
176 }
177
178 /*
179  * Display a menu for choosing among a number of options
180  */
181 int dialog_menu(const char *title, const char *prompt,
182                 const void *selected, int *s_scroll)
183 {
184         int i, j, x, y, box_x, box_y;
185         int height, width, menu_height;
186         int key = 0, button = 0, scroll = 0, choice = 0;
187         int first_item =  0, max_choice;
188         WINDOW *dialog, *menu;
189
190 do_resize:
191         height = getmaxy(stdscr);
192         width = getmaxx(stdscr);
193         if (height < 15 || width < 65)
194                 return -ERRDISPLAYTOOSMALL;
195
196         height -= 4;
197         width  -= 5;
198         menu_height = height - 10;
199
200         max_choice = MIN(menu_height, item_count());
201
202         /* center dialog box on screen */
203         x = (COLS - width) / 2;
204         y = (LINES - height) / 2;
205
206         draw_shadow(stdscr, y, x, height, width);
207
208         dialog = newwin(height, width, y, x);
209         keypad(dialog, TRUE);
210
211         draw_box(dialog, 0, 0, height, width,
212                  dlg.dialog.atr, dlg.border.atr);
213         wattrset(dialog, dlg.border.atr);
214         mvwaddch(dialog, height - 3, 0, ACS_LTEE);
215         for (i = 0; i < width - 2; i++)
216                 waddch(dialog, ACS_HLINE);
217         wattrset(dialog, dlg.dialog.atr);
218         wbkgdset(dialog, dlg.dialog.atr & A_COLOR);
219         waddch(dialog, ACS_RTEE);
220
221         print_title(dialog, title, width);
222
223         wattrset(dialog, dlg.dialog.atr);
224         print_autowrap(dialog, prompt, width - 2, 1, 3);
225
226         menu_width = width - 6;
227         box_y = height - menu_height - 5;
228         box_x = (width - menu_width) / 2 - 1;
229
230         /* create new window for the menu */
231         menu = subwin(dialog, menu_height, menu_width,
232                       y + box_y + 1, x + box_x + 1);
233         keypad(menu, TRUE);
234
235         /* draw a box around the menu items */
236         draw_box(dialog, box_y, box_x, menu_height + 2, menu_width + 2,
237                  dlg.menubox_border.atr, dlg.menubox.atr);
238
239         if (menu_width >= 80)
240                 item_x = (menu_width - 70) / 2;
241         else
242                 item_x = 4;
243
244         /* Set choice to default item */
245         item_foreach()
246                 if (selected && (selected == item_data()))
247                         choice = item_n();
248         /* get the saved scroll info */
249         scroll = *s_scroll;
250         if ((scroll <= choice) && (scroll + max_choice > choice) &&
251            (scroll >= 0) && (scroll + max_choice <= item_count())) {
252                 first_item = scroll;
253                 choice = choice - scroll;
254         } else {
255                 scroll = 0;
256         }
257         if ((choice >= max_choice)) {
258                 if (choice >= item_count() - max_choice / 2)
259                         scroll = first_item = item_count() - max_choice;
260                 else
261                         scroll = first_item = choice - max_choice / 2;
262                 choice = choice - scroll;
263         }
264
265         /* Print the menu */
266         for (i = 0; i < max_choice; i++) {
267                 print_item(first_item + i, i, i == choice);
268         }
269
270         wnoutrefresh(menu);
271
272         print_arrows(dialog, item_count(), scroll,
273                      box_y, box_x + item_x + 1, menu_height);
274
275         print_buttons(dialog, height, width, 0);
276         wmove(menu, choice, item_x + 1);
277         wrefresh(menu);
278
279         while (key != KEY_ESC) {
280                 key = wgetch(menu);
281
282                 if (key < 256 && isalpha(key))
283                         key = tolower(key);
284
285                 if (strchr("ynmh", key))
286                         i = max_choice;
287                 else {
288                         for (i = choice + 1; i < max_choice; i++) {
289                                 item_set(scroll + i);
290                                 j = first_alpha(item_str(), "YyNnMmHh");
291                                 if (key == tolower(item_str()[j]))
292                                         break;
293                         }
294                         if (i == max_choice)
295                                 for (i = 0; i < max_choice; i++) {
296                                         item_set(scroll + i);
297                                         j = first_alpha(item_str(), "YyNnMmHh");
298                                         if (key == tolower(item_str()[j]))
299                                                 break;
300                                 }
301                 }
302
303                 if (i < max_choice ||
304                     key == KEY_UP || key == KEY_DOWN ||
305                     key == '-' || key == '+' ||
306                     key == KEY_PPAGE || key == KEY_NPAGE) {
307                         /* Remove highligt of current item */
308                         print_item(scroll + choice, choice, FALSE);
309
310                         if (key == KEY_UP || key == '-') {
311                                 if (choice < 2 && scroll) {
312                                         /* Scroll menu down */
313                                         do_scroll(menu, &scroll, -1);
314
315                                         print_item(scroll, 0, FALSE);
316                                 } else
317                                         choice = MAX(choice - 1, 0);
318
319                         } else if (key == KEY_DOWN || key == '+') {
320                                 print_item(scroll+choice, choice, FALSE);
321
322                                 if ((choice > max_choice - 3) &&
323                                     (scroll + max_choice < item_count())) {
324                                         /* Scroll menu up */
325                                         do_scroll(menu, &scroll, 1);
326
327                                         print_item(scroll+max_choice - 1,
328                                                    max_choice - 1, FALSE);
329                                 } else
330                                         choice = MIN(choice + 1, max_choice - 1);
331
332                         } else if (key == KEY_PPAGE) {
333                                 scrollok(menu, TRUE);
334                                 for (i = 0; (i < max_choice); i++) {
335                                         if (scroll > 0) {
336                                                 do_scroll(menu, &scroll, -1);
337                                                 print_item(scroll, 0, FALSE);
338                                         } else {
339                                                 if (choice > 0)
340                                                         choice--;
341                                         }
342                                 }
343
344                         } else if (key == KEY_NPAGE) {
345                                 for (i = 0; (i < max_choice); i++) {
346                                         if (scroll + max_choice < item_count()) {
347                                                 do_scroll(menu, &scroll, 1);
348                                                 print_item(scroll+max_choice-1,
349                                                            max_choice - 1, FALSE);
350                                         } else {
351                                                 if (choice + 1 < max_choice)
352                                                         choice++;
353                                         }
354                                 }
355                         } else
356                                 choice = i;
357
358                         print_item(scroll + choice, choice, TRUE);
359
360                         print_arrows(dialog, item_count(), scroll,
361                                      box_y, box_x + item_x + 1, menu_height);
362
363                         wnoutrefresh(dialog);
364                         wrefresh(menu);
365
366                         continue;       /* wait for another key press */
367                 }
368
369                 switch (key) {
370                 case KEY_LEFT:
371                 case TAB:
372                 case KEY_RIGHT:
373                         button = ((key == KEY_LEFT ? --button : ++button) < 0)
374                             ? 2 : (button > 2 ? 0 : button);
375
376                         print_buttons(dialog, height, width, button);
377                         wrefresh(menu);
378                         break;
379                 case ' ':
380                 case 's':
381                 case 'y':
382                 case 'n':
383                 case 'm':
384                 case '/':
385                         /* save scroll info */
386                         *s_scroll = scroll;
387                         delwin(menu);
388                         delwin(dialog);
389                         item_set(scroll + choice);
390                         item_set_selected(1);
391                         switch (key) {
392                         case 's':
393                                 return 3;
394                         case 'y':
395                                 return 3;
396                         case 'n':
397                                 return 4;
398                         case 'm':
399                                 return 5;
400                         case ' ':
401                                 return 6;
402                         case '/':
403                                 return 7;
404                         }
405                         return 0;
406                 case 'h':
407                 case '?':
408                         button = 2;
409                 case '\n':
410                         *s_scroll = scroll;
411                         delwin(menu);
412                         delwin(dialog);
413                         item_set(scroll + choice);
414                         item_set_selected(1);
415                         return button;
416                 case 'e':
417                 case 'x':
418                         key = KEY_ESC;
419                         break;
420                 case KEY_ESC:
421                         key = on_key_esc(menu);
422                         break;
423                 case KEY_RESIZE:
424                         on_key_resize();
425                         delwin(menu);
426                         delwin(dialog);
427                         goto do_resize;
428                 }
429         }
430         delwin(menu);
431         delwin(dialog);
432         return key;             /* ESC pressed */
433 }