work on beacon
[aversive.git] / config / scripts / lxdialog / checklist.c
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 }
55
56 /*
57  * Print the scroll indicators.
58  */
59 static void
60 print_arrows (WINDOW * win, int choice, int item_no, int scroll,
61                 int y, int x, int height)
62 {
63     wmove(win, y, x);
64
65     if (scroll > 0) {
66         wattrset (win, uarrow_attr);
67         waddch (win, ACS_UARROW);
68         waddstr (win, "(-)");
69     }
70     else {
71         wattrset (win, menubox_attr);
72         waddch (win, ACS_HLINE);
73         waddch (win, ACS_HLINE);
74         waddch (win, ACS_HLINE);
75         waddch (win, ACS_HLINE);
76     }
77
78    y = y + height + 1;
79    wmove(win, y, x);
80
81    if ((height < item_no) && (scroll + choice < item_no - 1)) {
82         wattrset (win, darrow_attr);
83         waddch (win, ACS_DARROW);
84         waddstr (win, "(+)");
85     }
86     else {
87         wattrset (win, menubox_border_attr);
88         waddch (win, ACS_HLINE);
89         waddch (win, ACS_HLINE);
90         waddch (win, ACS_HLINE);
91         waddch (win, ACS_HLINE);
92    }
93 }
94
95 /*
96  *  Display the termination buttons
97  */
98 static void
99 print_buttons( WINDOW *dialog, int height, int width, int selected)
100 {
101     int x = width / 2 - 11;
102     int y = height - 2;
103
104     print_button (dialog, "Select", y, x, selected == 0);
105     print_button (dialog, " Help ", y, x + 14, selected == 1);
106
107     wmove(dialog, y, x+1 + 14*selected);
108     wrefresh (dialog);
109 }
110
111 /*
112  * Display a dialog box with a list of options that can be turned on or off
113  * The `flag' parameter is used to select between radiolist and checklist.
114  */
115 int
116 dialog_checklist (const char *title, const char *prompt, int height, int width,
117         int list_height, int item_no, const char * const * items, int flag)
118         
119 {
120     int i, x, y, box_x, box_y;
121     int key = 0, button = 0, choice = 0, scroll = 0, max_choice, *status;
122     WINDOW *dialog, *list;
123
124     checkflag = flag;
125
126     /* Allocate space for storing item on/off status */
127     if ((status = malloc (sizeof (int) * item_no)) == NULL) {
128         endwin ();
129         fprintf (stderr,
130                  "\nCan't allocate memory in dialog_checklist().\n");
131         exit (-1);
132     }
133
134     /* Initializes status */
135     for (i = 0; i < item_no; i++) {
136         status[i] = !strcasecmp (items[i * 3 + 2], "on");
137         if (!choice && status[i])
138             choice = i;
139     }
140
141     max_choice = MIN (list_height, item_no);
142
143     /* center dialog box on screen */
144     x = (COLS - width) / 2;
145     y = (LINES - height) / 2;
146
147     draw_shadow (stdscr, y, x, height, width);
148
149     dialog = newwin (height, width, y, x);
150     keypad (dialog, TRUE);
151
152     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
153     wattrset (dialog, border_attr);
154     mvwaddch (dialog, height-3, 0, ACS_LTEE);
155     for (i = 0; i < width - 2; i++)
156         waddch (dialog, ACS_HLINE);
157     wattrset (dialog, dialog_attr);
158     waddch (dialog, ACS_RTEE);
159
160     if (title != NULL && strlen(title) >= width-2 ) {
161         /* truncate long title -- mec */
162         char * title2 = malloc(width-2+1);
163         memcpy( title2, title, width-2 );
164         title2[width-2] = '\0';
165         title = title2;
166     }
167
168     if (title != NULL) {
169         wattrset (dialog, title_attr);
170         mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
171         waddstr (dialog, (char *)title);
172         waddch (dialog, ' ');
173     }
174
175     wattrset (dialog, dialog_attr);
176     print_autowrap (dialog, prompt, width - 2, 1, 3);
177
178     list_width = width - 6;
179     box_y = height - list_height - 5;
180     box_x = (width - list_width) / 2 - 1;
181
182     /* create new window for the list */
183     list = subwin (dialog, list_height, list_width, y+box_y+1, x+box_x+1);
184
185     keypad (list, TRUE);
186
187     /* draw a box around the list items */
188     draw_box (dialog, box_y, box_x, list_height + 2, list_width + 2,
189               menubox_border_attr, menubox_attr);
190
191     /* Find length of longest item in order to center checklist */
192     check_x = 0;
193     for (i = 0; i < item_no; i++) 
194         check_x = MAX (check_x, + strlen (items[i * 3 + 1]) + 4);
195
196     check_x = (list_width - check_x) / 2;
197     item_x = check_x + 4;
198
199     if (choice >= list_height) {
200         scroll = choice - list_height + 1;
201         choice -= scroll;
202     }
203
204     /* Print the list */
205     for (i = 0; i < max_choice; i++) {
206         print_item (list, items[(scroll+i) * 3 + 1],
207                     status[i+scroll], i, i == choice);
208     }
209
210     wnoutrefresh (list);
211
212     print_arrows(dialog, choice, item_no, scroll,
213                         box_y, box_x + check_x + 5, list_height);
214
215     print_buttons(dialog, height, width, 0);
216
217     while (key != ESC) {
218         key = wgetch (dialog);
219
220         for (i = 0; i < max_choice; i++)
221             if (toupper(key) == toupper(items[(scroll+i)*3+1][0]))
222                 break;
223
224
225         if ( i < max_choice || key == KEY_UP || key == KEY_DOWN || 
226             key == '+' || key == '-' ) {
227             if (key == KEY_UP || key == '-') {
228                 if (!choice) {
229                     if (!scroll)
230                         continue;
231                     /* Scroll list down */
232                     if (list_height > 1) {
233                         /* De-highlight current first item */
234                         print_item (list, items[scroll * 3 + 1],
235                                         status[scroll], 0, FALSE);
236                         scrollok (list, TRUE);
237                         wscrl (list, -1);
238                         scrollok (list, FALSE);
239                     }
240                     scroll--;
241                     print_item (list, items[scroll * 3 + 1],
242                                 status[scroll], 0, TRUE);
243                     wnoutrefresh (list);
244
245                     print_arrows(dialog, choice, item_no, scroll,
246                                 box_y, box_x + check_x + 5, list_height);
247
248                     wrefresh (dialog);
249
250                     continue;   /* wait for another key press */
251                 } else
252                     i = choice - 1;
253             } else if (key == KEY_DOWN || key == '+') {
254                 if (choice == max_choice - 1) {
255                     if (scroll + choice >= item_no - 1)
256                         continue;
257                     /* Scroll list up */
258                     if (list_height > 1) {
259                         /* De-highlight current last item before scrolling up */
260                         print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
261                                     status[scroll + max_choice - 1],
262                                     max_choice - 1, FALSE);
263                         scrollok (list, TRUE);
264                         scroll (list);
265                         scrollok (list, FALSE);
266                     }
267                     scroll++;
268                     print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
269                                 status[scroll + max_choice - 1],
270                                 max_choice - 1, TRUE);
271                     wnoutrefresh (list);
272
273                     print_arrows(dialog, choice, item_no, scroll,
274                                 box_y, box_x + check_x + 5, list_height);
275
276                     wrefresh (dialog);
277
278                     continue;   /* wait for another key press */
279                 } else
280                     i = choice + 1;
281             }
282             if (i != choice) {
283                 /* De-highlight current item */
284                 print_item (list, items[(scroll + choice) * 3 + 1],
285                             status[scroll + choice], choice, FALSE);
286                 /* Highlight new item */
287                 choice = i;
288                 print_item (list, items[(scroll + choice) * 3 + 1],
289                             status[scroll + choice], choice, TRUE);
290                 wnoutrefresh (list);
291                 wrefresh (dialog);
292             }
293             continue;           /* wait for another key press */
294         }
295         switch (key) {
296         case 'H':
297         case 'h':
298         case '?':
299             delwin (dialog);
300             free (status);
301             return 1;
302         case TAB:
303         case KEY_LEFT:
304         case KEY_RIGHT:
305             button = ((key == KEY_LEFT ? --button : ++button) < 0)
306                         ? 1 : (button > 1 ? 0 : button);
307
308             print_buttons(dialog, height, width, button);
309             wrefresh (dialog);
310             break;
311         case 'S':
312         case 's':
313         case ' ':
314         case '\n':
315             if (!button) {
316                 if (flag == FLAG_CHECK) {
317                     status[scroll + choice] = !status[scroll + choice];
318                     wmove (list, choice, check_x);
319                     wattrset (list, check_selected_attr);
320                     wprintw (list, "[%c]", status[scroll + choice] ? 'X' : ' ');
321                 } else {
322                     if (!status[scroll + choice]) {
323                         for (i = 0; i < item_no; i++)
324                             status[i] = 0;
325                         status[scroll + choice] = 1;
326                         for (i = 0; i < max_choice; i++)
327                             print_item (list, items[(scroll + i) * 3 + 1],
328                                         status[scroll + i], i, i == choice);
329                     }
330                 }
331                 wnoutrefresh (list);
332                 wrefresh (dialog);
333             
334                 for (i = 0; i < item_no; i++) {
335                     if (status[i]) {
336                         if (flag == FLAG_CHECK) {
337                             fprintf (stderr, "\"%s\" ", items[i * 3]);
338                         } else {
339                             fprintf (stderr, "%s", items[i * 3]);
340                         }
341
342                     }
343                 }
344             }
345             delwin (dialog);
346             free (status);
347             return button;
348         case 'X':
349         case 'x':
350             key = ESC;
351         case ESC:
352             break;
353         }
354     }
355
356     delwin (dialog);
357     free (status);
358     return -1;                  /* ESC pressed */
359 }