OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [scripts/] [lxdialog/] [menubox.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1629 jcastillo
/*
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
#include "dialog.h"
23
 
24
static int menu_width, item_x;
25
 
26
/*
27
 * Print menu item
28
 */
29
static void
30
print_item (WINDOW * win, const char *item, int choice, int selected, int hotkey)
31
{
32
    int i, j;
33
    char menu_item[menu_width+1];
34
 
35
    strncpy(menu_item, item, menu_width);
36
    menu_item[menu_width] = 0;
37
    j = first_alpha(menu_item, "YyNnMm");
38
 
39
    /* Clear 'residue' of last item */
40
    wattrset (win, menubox_attr);
41
    wmove (win, choice, 0);
42
#if OLD_NCURSES
43
    for (i = 0; i < menu_width; i++)
44
        waddch (win, ' ');
45
#else
46
    wclrtoeol(win);
47
#endif
48
    wattrset (win, selected ? item_selected_attr : item_attr);
49
    mvwaddstr (win, choice, item_x, menu_item);
50
    if (hotkey) {
51
        wattrset (win, selected ? tag_key_selected_attr : tag_key_attr);
52
        mvwaddch(win, choice, item_x+j, menu_item[j]);
53
    }
54
}
55
 
56
/*
57
 * Print the scroll indicators.
58
 */
59
static void
60
print_arrows (WINDOW * win, int item_no, int scroll,
61
                int y, int x, int height)
62
{
63
    int cur_y, cur_x;
64
 
65
    getyx(win, cur_y, cur_x);
66
 
67
    wmove(win, y, x);
68
 
69
    if (scroll > 0) {
70
        wattrset (win, uarrow_attr);
71
        waddch (win, ACS_UARROW);
72
        waddstr (win, "(-)");
73
    }
74
    else {
75
        wattrset (win, menubox_attr);
76
        waddch (win, ACS_HLINE);
77
        waddch (win, ACS_HLINE);
78
        waddch (win, ACS_HLINE);
79
        waddch (win, ACS_HLINE);
80
    }
81
 
82
   y = y + height + 1;
83
   wmove(win, y, x);
84
 
85
   if ((height < item_no) && (scroll + height < item_no)) {
86
        wattrset (win, darrow_attr);
87
        waddch (win, ACS_DARROW);
88
        waddstr (win, "(+)");
89
    }
90
    else {
91
        wattrset (win, menubox_border_attr);
92
        waddch (win, ACS_HLINE);
93
        waddch (win, ACS_HLINE);
94
        waddch (win, ACS_HLINE);
95
        waddch (win, ACS_HLINE);
96
   }
97
 
98
   wmove(win, cur_y, cur_x);
99
}
100
 
101
/*
102
 * Display the termination buttons.
103
 */
104
static void
105
print_buttons (WINDOW *win, int height, int width, int selected)
106
{
107
    int x = width / 2 - 16;
108
    int y = height - 2;
109
 
110
    print_button (win, "Select", y, x, selected == 0);
111
    print_button (win, " Exit ", y, x + 12, selected == 1);
112
    print_button (win, " Help ", y, x + 24, selected == 2);
113
 
114
    wmove(win, y, x+1+12*selected);
115
    wrefresh (win);
116
}
117
 
118
/*
119
 * Display a menu for choosing among a number of options
120
 */
121
int
122
dialog_menu (const char *title, const char *prompt, int height, int width,
123
                int menu_height, const char *current, int item_no,
124
                const char * const * items)
125
 
126
{
127
    int i, j, x, y, box_x, box_y;
128
    int key = 0, button = 0, scroll = 0, choice = 0, first_item = 0, max_choice;
129
    WINDOW *dialog, *menu;
130
 
131
    max_choice = MIN (menu_height, item_no);
132
 
133
    /* center dialog box on screen */
134
    x = (COLS - width) / 2;
135
    y = (LINES - height) / 2;
136
 
137
    draw_shadow (stdscr, y, x, height, width);
138
 
139
    dialog = newwin (height, width, y, x);
140
    keypad (dialog, TRUE);
141
 
142
    draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
143
    wattrset (dialog, border_attr);
144
    mvwaddch (dialog, height - 3, 0, ACS_LTEE);
145
    for (i = 0; i < width - 2; i++)
146
        waddch (dialog, ACS_HLINE);
147
    wattrset (dialog, dialog_attr);
148
    wbkgdset (dialog, dialog_attr & A_COLOR);
149
    waddch (dialog, ACS_RTEE);
150
 
151
    if (title != NULL) {
152
        wattrset (dialog, title_attr);
153
        mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
154
        waddstr (dialog, (char *)title);
155
        waddch (dialog, ' ');
156
    }
157
 
158
    wattrset (dialog, dialog_attr);
159
    print_autowrap (dialog, prompt, width - 2, 1, 3);
160
 
161
    menu_width = width - 6;
162
    box_y = height - menu_height - 5;
163
    box_x = (width - menu_width) / 2 - 1;
164
 
165
    /* create new window for the menu */
166
    menu = subwin (dialog, menu_height, menu_width,
167
                y + box_y + 1, x + box_x + 1);
168
    keypad (menu, TRUE);
169
 
170
    /* draw a box around the menu items */
171
    draw_box (dialog, box_y, box_x, menu_height + 2, menu_width + 2,
172
              menubox_border_attr, menubox_attr);
173
 
174
    /*
175
     * Find length of longest item in order to center menu.
176
     * Set 'choice' to default item.
177
     */
178
    item_x = 0;
179
    for (i = 0; i < item_no; i++) {
180
        item_x = MAX (item_x, MIN(menu_width, strlen (items[i * 2 + 1]) + 2));
181
        if (strcmp(current, items[i*2]) == 0) choice = i;
182
    }
183
 
184
    item_x = (menu_width - item_x) / 2;
185
 
186
    if (choice >= max_choice){
187
        scroll = first_item = choice - max_choice + 1;
188
        choice = max_choice-1;
189
    }
190
 
191
    /* Print the menu */
192
    for (i=0; i < max_choice; i++) {
193
        print_item (menu, items[(first_item + i) * 2 + 1], i, i == choice,
194
                    (items[(first_item + i)*2][0] != ':'));
195
    }
196
 
197
    wnoutrefresh (menu);
198
 
199
    print_arrows(dialog, item_no, scroll,
200
                 box_y, box_x+item_x+1, menu_height);
201
 
202
    print_buttons (dialog, height, width, 0);
203
 
204
    while (key != ESC) {
205
        key = wgetch(dialog);
206
 
207
        if (key < 256 && isalpha(key)) key = tolower(key);
208
 
209
        if (strchr("ynm", key))
210
                i = max_choice;
211
        else {
212
        for (i = choice+1; i < max_choice; i++) {
213
                j = first_alpha(items[(scroll+i)*2+1], "YyNnMm");
214
                if (key == tolower(items[(scroll+i)*2+1][j]))
215
                        break;
216
        }
217
        if (i == max_choice)
218
                for (i = 0; i < max_choice; i++) {
219
                        j = first_alpha(items[(scroll+i)*2+1], "YyNnMm");
220
                        if (key == tolower(items[(scroll+i)*2+1][j]))
221
                                break;
222
                }
223
        }
224
 
225
        if (i < max_choice ||
226
            key == KEY_UP || key == KEY_DOWN ||
227
            key == '-' || key == '+' ||
228
            key == KEY_PPAGE || key == KEY_NPAGE) {
229
 
230
            print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,
231
                       (items[(scroll+choice)*2][0] != ':'));
232
 
233
            if (key == KEY_UP || key == '-') {
234
                if (choice < 2 && scroll) {
235
                    /* Scroll menu down */
236
                    scrollok (menu, TRUE);
237
                    wscrl (menu, -1);
238
                    scrollok (menu, FALSE);
239
 
240
                    scroll--;
241
 
242
                    print_item (menu, items[scroll * 2 + 1], 0, FALSE,
243
                               (items[scroll*2][0] != ':'));
244
                } else
245
                    choice = MAX(choice - 1, 0);
246
 
247
            } else if (key == KEY_DOWN || key == '+')  {
248
 
249
                print_item (menu, items[(scroll+choice)*2+1], choice, FALSE,
250
                                (items[(scroll+choice)*2][0] != ':'));
251
 
252
                if ((choice > max_choice-3) &&
253
                    (scroll + max_choice < item_no)
254
                   ) {
255
                    /* Scroll menu up */
256
                    scrollok (menu, TRUE);
257
                    scroll (menu);
258
                    scrollok (menu, FALSE);
259
 
260
                    scroll++;
261
 
262
                    print_item (menu, items[(scroll+max_choice-1)*2+1],
263
                               max_choice-1, FALSE,
264
                               (items[(scroll+max_choice-1)*2][0] != ':'));
265
                } else
266
                    choice = MIN(choice+1, max_choice-1);
267
 
268
            } else if (key == KEY_PPAGE) {
269
                scrollok (menu, TRUE);
270
                for (i=0; (i < max_choice) && (scroll > 0); i++) {
271
                    wscrl (menu, -1);
272
                    scroll--;
273
                    print_item (menu, items[scroll * 2 + 1], 0, FALSE,
274
                               (items[scroll*2][0] != ':'));
275
                }
276
                scrollok (menu, FALSE);
277
                choice = 0;
278
 
279
            } else if (key == KEY_NPAGE) {
280
                scrollok (menu, TRUE);
281
                for (i=0; (i < max_choice) && (scroll+max_choice < item_no); i++) {
282
                    scroll(menu);
283
                    scroll++;
284
                    print_item (menu, items[(scroll+max_choice-1)*2+1],
285
                                max_choice-1, FALSE,
286
                                (items[(scroll+max_choice-1)*2][0] != ':'));
287
                }
288
                scrollok (menu, FALSE);
289
                choice = 0;
290
 
291
            } else
292
                choice = i;
293
 
294
            print_item (menu, items[(scroll+choice)*2+1], choice, TRUE,
295
                       (items[(scroll+choice)*2][0] != ':'));
296
 
297
            print_arrows(dialog, item_no, scroll,
298
                         box_y, box_x+item_x+1, menu_height);
299
 
300
            wnoutrefresh (menu);
301
            wrefresh (dialog);
302
 
303
            continue;           /* wait for another key press */
304
        }
305
 
306
        switch (key) {
307
        case KEY_LEFT:
308
        case TAB:
309
        case KEY_RIGHT:
310
            button = ((key == KEY_LEFT ? --button : ++button) < 0)
311
                        ? 2 : (button > 2 ? 0 : button);
312
 
313
            print_buttons(dialog, height, width, button);
314
            wrefresh (dialog);
315
            break;
316
        case ' ':
317
        case 's':
318
        case 'y':
319
        case 'n':
320
        case 'm':
321
            delwin (dialog);
322
            fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
323
            switch (key) {
324
            case 's': return 3;
325
            case 'y': return 3;
326
            case 'n': return 4;
327
            case 'm': return 5;
328
            case ' ': return 6;
329
            }
330
            return 0;
331
        case 'h':
332
        case '?':
333
            button = 2;
334
        case '\n':
335
            delwin (dialog);
336
            if (button == 2)
337
                fprintf(stderr, "%s \"%s\"\n",
338
                        items[(scroll + choice) * 2],
339
                        items[(scroll + choice) * 2 + 1] +
340
                        first_alpha(items[(scroll + choice) * 2 + 1],""));
341
            else
342
                fprintf(stderr, "%s\n", items[(scroll + choice) * 2]);
343
 
344
            return button;
345
        case 'e':
346
        case 'x':
347
            key = ESC;
348
        case ESC:
349
            break;
350
        }
351
    }
352
 
353
    delwin (dialog);
354
    return -1;                  /* ESC pressed */
355
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.