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