~ubuntu-branches/ubuntu/lucid/skyeye/lucid-proposed

« back to all changes in this revision

Viewing changes to utils/conf/scripts/lxdialog/checklist.c

  • Committer: Bazaar Package Importer
  • Author(s): Yu Guanghui
  • Date: 2007-08-07 13:25:49 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20070807132549-96159k1obat1fxr0
Tags: 1.2.3-1
* New upstream release
* Added NO_BFD=1, don't require libbfd now. (Closes:Bug#423933) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  checklist.c -- implements the checklist box
3
 
 *
4
 
 *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5
 
 *     Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension
6
 
 *     Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two
7
 
 *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
8
 
 *
9
 
 *  This program is free software; you can redistribute it and/or
10
 
 *  modify it under the terms of the GNU General Public License
11
 
 *  as published by the Free Software Foundation; either version 2
12
 
 *  of the License, or (at your option) any later version.
13
 
 *
14
 
 *  This program is distributed in the hope that it will be useful,
15
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
 *  GNU General Public License for more details.
18
 
 *
19
 
 *  You should have received a copy of the GNU General Public License
20
 
 *  along with this program; if not, write to the Free Software
21
 
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
 
 */
23
 
 
24
 
#include "dialog.h"
25
 
 
26
 
static int list_width, check_x, item_x, checkflag;
27
 
 
28
 
/*
29
 
 * Print list item
30
 
 */
31
 
static void
32
 
print_item (WINDOW * win, const char *item, int status,
33
 
            int choice, int selected)
34
 
{
35
 
    int i;
36
 
 
37
 
    /* Clear 'residue' of last item */
38
 
    wattrset (win, menubox_attr);
39
 
    wmove (win, choice, 0);
40
 
    for (i = 0; i < list_width; i++)
41
 
        waddch (win, ' ');
42
 
 
43
 
    wmove (win, choice, check_x);
44
 
    wattrset (win, selected ? check_selected_attr : check_attr);
45
 
    if (checkflag == FLAG_CHECK)
46
 
        wprintw (win, "[%c]", status ? 'X' : ' ');
47
 
    else
48
 
        wprintw (win, "(%c)", status ? 'X' : ' ');
49
 
 
50
 
    wattrset (win, selected ? tag_selected_attr : tag_attr);
51
 
    mvwaddch(win, choice, item_x, item[0]);
52
 
    wattrset (win, selected ? item_selected_attr : item_attr);
53
 
    waddstr (win, (char *)item+1);
54
 
    if (selected) {
55
 
        wmove (win, choice, check_x+1);
56
 
        wrefresh (win);
57
 
    }
58
 
}
59
 
 
60
 
/*
61
 
 * Print the scroll indicators.
62
 
 */
63
 
static void
64
 
print_arrows (WINDOW * win, int choice, int item_no, int scroll,
65
 
                int y, int x, int height)
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 + choice < item_no - 1)) {
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
 
 
99
 
/*
100
 
 *  Display the termination buttons
101
 
 */
102
 
static void
103
 
print_buttons( WINDOW *dialog, int height, int width, int selected)
104
 
{
105
 
    int x = width / 2 - 11;
106
 
    int y = height - 2;
107
 
 
108
 
    print_button (dialog, "Select", y, x, selected == 0);
109
 
    print_button (dialog, " Help ", y, x + 14, selected == 1);
110
 
 
111
 
    wmove(dialog, y, x+1 + 14*selected);
112
 
    wrefresh (dialog);
113
 
}
114
 
 
115
 
/*
116
 
 * Display a dialog box with a list of options that can be turned on or off
117
 
 * The `flag' parameter is used to select between radiolist and checklist.
118
 
 */
119
 
int
120
 
dialog_checklist (const char *title, const char *prompt, int height, int width,
121
 
        int list_height, int item_no, const char * const * items, int flag)
122
 
        
123
 
{
124
 
    int i, x, y, box_x, box_y;
125
 
    int key = 0, button = 0, choice = 0, scroll = 0, max_choice, *status;
126
 
    WINDOW *dialog, *list;
127
 
 
128
 
    checkflag = flag;
129
 
 
130
 
    /* Allocate space for storing item on/off status */
131
 
    if ((status = malloc (sizeof (int) * item_no)) == NULL) {
132
 
        endwin ();
133
 
        fprintf (stderr,
134
 
                 "\nCan't allocate memory in dialog_checklist().\n");
135
 
        exit (-1);
136
 
    }
137
 
 
138
 
    /* Initializes status */
139
 
    for (i = 0; i < item_no; i++) {
140
 
        status[i] = !strcasecmp (items[i * 3 + 2], "on");
141
 
        if (!choice && status[i])
142
 
            choice = i;
143
 
    }
144
 
 
145
 
    max_choice = MIN (list_height, item_no);
146
 
 
147
 
    /* center dialog box on screen */
148
 
    x = (COLS - width) / 2;
149
 
    y = (LINES - height) / 2;
150
 
 
151
 
    draw_shadow (stdscr, y, x, height, width);
152
 
 
153
 
    dialog = newwin (height, width, y, x);
154
 
    keypad (dialog, TRUE);
155
 
 
156
 
    draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
157
 
    wattrset (dialog, border_attr);
158
 
    mvwaddch (dialog, height-3, 0, ACS_LTEE);
159
 
    for (i = 0; i < width - 2; i++)
160
 
        waddch (dialog, ACS_HLINE);
161
 
    wattrset (dialog, dialog_attr);
162
 
    waddch (dialog, ACS_RTEE);
163
 
 
164
 
    if (title != NULL && strlen(title) >= width-2 ) {
165
 
        /* truncate long title -- mec */
166
 
        char * title2 = malloc(width-2+1);
167
 
        memcpy( title2, title, width-2 );
168
 
        title2[width-2] = '\0';
169
 
        title = title2;
170
 
    }
171
 
 
172
 
    if (title != NULL) {
173
 
        wattrset (dialog, title_attr);
174
 
        mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
175
 
        waddstr (dialog, (char *)title);
176
 
        waddch (dialog, ' ');
177
 
    }
178
 
 
179
 
    wattrset (dialog, dialog_attr);
180
 
    print_autowrap (dialog, prompt, width - 2, 1, 3);
181
 
 
182
 
    list_width = width - 6;
183
 
    box_y = height - list_height - 5;
184
 
    box_x = (width - list_width) / 2 - 1;
185
 
 
186
 
    /* create new window for the list */
187
 
    list = subwin (dialog, list_height, list_width, y+box_y+1, x+box_x+1);
188
 
 
189
 
    keypad (list, TRUE);
190
 
 
191
 
    /* draw a box around the list items */
192
 
    draw_box (dialog, box_y, box_x, list_height + 2, list_width + 2,
193
 
              menubox_border_attr, menubox_attr);
194
 
 
195
 
    /* Find length of longest item in order to center checklist */
196
 
    check_x = 0;
197
 
    for (i = 0; i < item_no; i++) 
198
 
        check_x = MAX (check_x, + strlen (items[i * 3 + 1]) + 4);
199
 
 
200
 
    check_x = (list_width - check_x) / 2;
201
 
    item_x = check_x + 4;
202
 
 
203
 
    if (choice >= list_height) {
204
 
        scroll = choice - list_height + 1;
205
 
        choice -= scroll;
206
 
    }
207
 
 
208
 
    /* Print the list */
209
 
    for (i = 0; i < max_choice; i++) {
210
 
        print_item (list, items[(scroll+i) * 3 + 1],
211
 
                    status[i+scroll], i, i == choice);
212
 
    }
213
 
 
214
 
    print_arrows(dialog, choice, item_no, scroll,
215
 
                        box_y, box_x + check_x + 5, list_height);
216
 
 
217
 
    print_buttons(dialog, height, width, 0);
218
 
 
219
 
    wnoutrefresh (list);
220
 
    wnoutrefresh (dialog);
221
 
    doupdate ();
222
 
 
223
 
    while (key != ESC) {
224
 
        key = wgetch (dialog);
225
 
 
226
 
        for (i = 0; i < max_choice; i++)
227
 
            if (toupper(key) == toupper(items[(scroll+i)*3+1][0]))
228
 
                break;
229
 
 
230
 
 
231
 
        if ( i < max_choice || key == KEY_UP || key == KEY_DOWN || 
232
 
            key == '+' || key == '-' ) {
233
 
            if (key == KEY_UP || key == '-') {
234
 
                if (!choice) {
235
 
                    if (!scroll)
236
 
                        continue;
237
 
                    /* Scroll list down */
238
 
                    if (list_height > 1) {
239
 
                        /* De-highlight current first item */
240
 
                        print_item (list, items[scroll * 3 + 1],
241
 
                                        status[scroll], 0, FALSE);
242
 
                        scrollok (list, TRUE);
243
 
                        wscrl (list, -1);
244
 
                        scrollok (list, FALSE);
245
 
                    }
246
 
                    scroll--;
247
 
                    print_item (list, items[scroll * 3 + 1],
248
 
                                status[scroll], 0, TRUE);
249
 
                    wnoutrefresh (list);
250
 
 
251
 
                    print_arrows(dialog, choice, item_no, scroll,
252
 
                                box_y, box_x + check_x + 5, list_height);
253
 
 
254
 
                    wrefresh (dialog);
255
 
 
256
 
                    continue;   /* wait for another key press */
257
 
                } else
258
 
                    i = choice - 1;
259
 
            } else if (key == KEY_DOWN || key == '+') {
260
 
                if (choice == max_choice - 1) {
261
 
                    if (scroll + choice >= item_no - 1)
262
 
                        continue;
263
 
                    /* Scroll list up */
264
 
                    if (list_height > 1) {
265
 
                        /* De-highlight current last item before scrolling up */
266
 
                        print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
267
 
                                    status[scroll + max_choice - 1],
268
 
                                    max_choice - 1, FALSE);
269
 
                        scrollok (list, TRUE);
270
 
                        scroll (list);
271
 
                        scrollok (list, FALSE);
272
 
                    }
273
 
                    scroll++;
274
 
                    print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
275
 
                                status[scroll + max_choice - 1],
276
 
                                max_choice - 1, TRUE);
277
 
                    wnoutrefresh (list);
278
 
 
279
 
                    print_arrows(dialog, choice, item_no, scroll,
280
 
                                box_y, box_x + check_x + 5, list_height);
281
 
 
282
 
                    wrefresh (dialog);
283
 
 
284
 
                    continue;   /* wait for another key press */
285
 
                } else
286
 
                    i = choice + 1;
287
 
            }
288
 
            if (i != choice) {
289
 
                /* De-highlight current item */
290
 
                print_item (list, items[(scroll + choice) * 3 + 1],
291
 
                            status[scroll + choice], choice, FALSE);
292
 
                /* Highlight new item */
293
 
                choice = i;
294
 
                print_item (list, items[(scroll + choice) * 3 + 1],
295
 
                            status[scroll + choice], choice, TRUE);
296
 
                wnoutrefresh (list);
297
 
                wrefresh (dialog);
298
 
            }
299
 
            continue;           /* wait for another key press */
300
 
        }
301
 
        switch (key) {
302
 
        case 'H':
303
 
        case 'h':
304
 
        case '?':
305
 
            delwin (dialog);
306
 
            free (status);
307
 
            return 1;
308
 
        case TAB:
309
 
        case KEY_LEFT:
310
 
        case KEY_RIGHT:
311
 
            button = ((key == KEY_LEFT ? --button : ++button) < 0)
312
 
                        ? 1 : (button > 1 ? 0 : button);
313
 
 
314
 
            print_buttons(dialog, height, width, button);
315
 
            wrefresh (dialog);
316
 
            break;
317
 
        case 'S':
318
 
        case 's':
319
 
        case ' ':
320
 
        case '\n':
321
 
            if (!button) {
322
 
                if (flag == FLAG_CHECK) {
323
 
                    status[scroll + choice] = !status[scroll + choice];
324
 
                    wmove (list, choice, check_x);
325
 
                    wattrset (list, check_selected_attr);
326
 
                    wprintw (list, "[%c]", status[scroll + choice] ? 'X' : ' ');
327
 
                } else {
328
 
                    if (!status[scroll + choice]) {
329
 
                        for (i = 0; i < item_no; i++)
330
 
                            status[i] = 0;
331
 
                        status[scroll + choice] = 1;
332
 
                        for (i = 0; i < max_choice; i++)
333
 
                            print_item (list, items[(scroll + i) * 3 + 1],
334
 
                                        status[scroll + i], i, i == choice);
335
 
                    }
336
 
                }
337
 
                wnoutrefresh (list);
338
 
                wrefresh (dialog);
339
 
            
340
 
                for (i = 0; i < item_no; i++) {
341
 
                    if (status[i]) {
342
 
                        if (flag == FLAG_CHECK) {
343
 
                            fprintf (stderr, "\"%s\" ", items[i * 3]);
344
 
                        } else {
345
 
                            fprintf (stderr, "%s", items[i * 3]);
346
 
                        }
347
 
 
348
 
                    }
349
 
                }
350
 
            }
351
 
            delwin (dialog);
352
 
            free (status);
353
 
            return button;
354
 
        case 'X':
355
 
        case 'x':
356
 
            key = ESC;
357
 
        case ESC:
358
 
            break;
359
 
        }
360
 
 
361
 
        /* Now, update everything... */
362
 
        doupdate ();
363
 
    }
364
 
    
365
 
 
366
 
    delwin (dialog);
367
 
    free (status);
368
 
    return -1;                  /* ESC pressed */
369
 
}