root/trunk/freewrt/tools/config/checklist.c

Revision 2424, 9.8 kB (checked in by tg, 5 years ago)

• don't highlight the first char of every item

– yellow text on lightgrey background is just not visible

(at least on my laptop display, GNU screen inside uxterm)

– nearly all of them do not work as hotkeys anyway

• add the ctags file to the list of svn-ignored while here

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