Dash:
[t2.git] / misc / confdialog / checklist.c
blobbd4e8d2eb63168dc197ae37c8be265aa93e84359
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: misc/confdialog/checklist.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
8 *
9 * More information can be found in the files COPYING and README.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License. A copy of the
14 * GNU General Public License can be found in the file COPYING.
15 * --- T2-COPYRIGHT-NOTE-END ---
18 * checklist.c -- implements the checklist box
20 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
21 * Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension
22 * Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two
23 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
24 * MODIFIED FOR ROCK LINUX CONFIG BY: Clifford Wolf (clifford@clifford.at)
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version 2
29 * of the License, or (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
41 #include "dialog.h"
43 static int list_width, check_x, item_x, checkflag;
46 * Print list item
48 static void
49 print_item (WINDOW * win, const char *item, int status,
50 int choice, int selected)
52 int i;
54 /* Clear 'residue' of last item */
55 wattrset (win, menubox_attr);
56 wmove (win, choice, 0);
57 for (i = 0; i < list_width; i++)
58 waddch (win, ' ');
60 wmove (win, choice, check_x);
61 wattrset (win, selected ? check_selected_attr : check_attr);
62 if (checkflag == FLAG_CHECK)
63 wprintw (win, "[%c]", status ? 'X' : ' ');
64 else
65 wprintw (win, "(%c)", status ? 'X' : ' ');
67 wattrset (win, selected ? tag_selected_attr : tag_attr);
68 mvwaddch(win, choice, item_x, item[0]);
69 wattrset (win, selected ? item_selected_attr : item_attr);
70 waddstr (win, (char *)item+1);
71 if (selected) {
72 wmove (win, choice, check_x+1);
73 wrefresh (win);
78 * Print the scroll indicators.
80 static void
81 print_arrows (WINDOW * win, int choice, int item_no, int scroll,
82 int y, int x, int height)
84 wmove(win, y, x);
86 if (scroll > 0) {
87 wattrset (win, uarrow_attr);
88 waddch (win, ACS_UARROW);
89 waddstr (win, "(-)");
91 else {
92 wattrset (win, menubox_attr);
93 waddch (win, ACS_HLINE);
94 waddch (win, ACS_HLINE);
95 waddch (win, ACS_HLINE);
96 waddch (win, ACS_HLINE);
99 y = y + height + 1;
100 wmove(win, y, x);
102 if ((height < item_no) && (scroll + choice < item_no - 1)) {
103 wattrset (win, darrow_attr);
104 waddch (win, ACS_DARROW);
105 waddstr (win, "(+)");
107 else {
108 wattrset (win, menubox_border_attr);
109 waddch (win, ACS_HLINE);
110 waddch (win, ACS_HLINE);
111 waddch (win, ACS_HLINE);
112 waddch (win, ACS_HLINE);
117 * Display the termination buttons
119 static void
120 print_buttons( WINDOW *dialog, int height, int width, int selected)
122 int x = width / 2 - 5;
123 int y = height - 2;
125 print_button (dialog, "Select", y, x, 1);
127 wmove(dialog, y, x+1);
128 wrefresh (dialog);
132 * Display a dialog box with a list of options that can be turned on or off
133 * The `flag' parameter is used to select between radiolist and checklist.
136 dialog_checklist (const char *title, const char *prompt, int height, int width,
137 int list_height, int item_no, const char * const * items, int flag)
140 int i, x, y, box_x, box_y;
141 int key = 0, button = 0, choice = 0, scroll = 0, max_choice, *status;
142 WINDOW *dialog, *list;
144 checkflag = flag;
146 /* Allocate space for storing item on/off status */
147 if ((status = malloc (sizeof (int) * item_no)) == NULL) {
148 endwin ();
149 fprintf (stderr,
150 "\nCan't allocate memory in dialog_checklist().\n");
151 exit (-1);
154 /* Initializes status */
155 for (i = 0; i < item_no; i++) {
156 status[i] = !strcasecmp (items[i * 3 + 2], "on");
157 if (!choice && status[i])
158 choice = i;
161 max_choice = MIN (list_height, item_no);
163 /* center dialog box on screen */
164 x = (COLS - width) / 2;
165 y = (LINES - height) / 2;
167 draw_shadow (stdscr, y, x, height, width);
169 dialog = newwin (height, width, y, x);
170 keypad (dialog, TRUE);
172 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
173 wattrset (dialog, border_attr);
174 mvwaddch (dialog, height-3, 0, ACS_LTEE);
175 for (i = 0; i < width - 2; i++)
176 waddch (dialog, ACS_HLINE);
177 wattrset (dialog, dialog_attr);
178 waddch (dialog, ACS_RTEE);
180 if (title != NULL && strlen(title) >= width-2 ) {
181 /* truncate long title -- mec */
182 char * title2 = malloc(width-2+1);
183 memcpy( title2, title, width-2 );
184 title2[width-2] = '\0';
185 title = title2;
188 if (title != NULL) {
189 wattrset (dialog, title_attr);
190 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
191 waddstr (dialog, (char *)title);
192 waddch (dialog, ' ');
195 wattrset (dialog, dialog_attr);
196 print_autowrap (dialog, prompt, width - 2, 1, 3);
198 list_width = width - 6;
199 box_y = height - list_height - 5;
200 box_x = (width - list_width) / 2 - 1;
202 /* create new window for the list */
203 list = subwin (dialog, list_height, list_width, y+box_y+1, x+box_x+1);
205 keypad (list, TRUE);
207 /* draw a box around the list items */
208 draw_box (dialog, box_y, box_x, list_height + 2, list_width + 2,
209 menubox_border_attr, menubox_attr);
211 /* Find length of longest item in order to center checklist */
212 check_x = 0;
213 for (i = 0; i < item_no; i++)
214 check_x = MAX (check_x, + strlen (items[i * 3 + 1]) + 4);
216 check_x = (list_width - check_x) / 2;
217 item_x = check_x + 4;
219 if (choice >= list_height) {
220 scroll = choice - list_height + 1;
221 choice -= scroll;
224 /* Print the list */
225 for (i = 0; i < max_choice; i++) {
226 print_item (list, items[(scroll+i) * 3 + 1],
227 status[i+scroll], i, i == choice);
230 print_arrows(dialog, choice, item_no, scroll,
231 box_y, box_x + check_x + 5, list_height);
233 print_buttons(dialog, height, width, 0);
235 wnoutrefresh (list);
236 wnoutrefresh (dialog);
237 doupdate ();
239 while (key != ESC) {
240 key = wgetch (dialog);
242 for (i = 0; i < max_choice; i++)
243 if (toupper(key) == toupper(items[(scroll+i)*3+1][0]))
244 break;
247 if ( i < max_choice || key == KEY_UP || key == KEY_DOWN ||
248 key == '+' || key == '-' ) {
249 if (key == KEY_UP || key == '-') {
250 if (!choice) {
251 if (!scroll)
252 continue;
253 /* Scroll list down */
254 if (list_height > 1) {
255 /* De-highlight current first item */
256 print_item (list, items[scroll * 3 + 1],
257 status[scroll], 0, FALSE);
258 scrollok (list, TRUE);
259 wscrl (list, -1);
260 scrollok (list, FALSE);
262 scroll--;
263 print_item (list, items[scroll * 3 + 1],
264 status[scroll], 0, TRUE);
265 wnoutrefresh (list);
267 print_arrows(dialog, choice, item_no, scroll,
268 box_y, box_x + check_x + 5, list_height);
270 wrefresh (dialog);
272 continue; /* wait for another key press */
273 } else
274 i = choice - 1;
275 } else if (key == KEY_DOWN || key == '+') {
276 if (choice == max_choice - 1) {
277 if (scroll + choice >= item_no - 1)
278 continue;
279 /* Scroll list up */
280 if (list_height > 1) {
281 /* De-highlight current last item before scrolling up */
282 print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
283 status[scroll + max_choice - 1],
284 max_choice - 1, FALSE);
285 scrollok (list, TRUE);
286 scroll (list);
287 scrollok (list, FALSE);
289 scroll++;
290 print_item (list, items[(scroll + max_choice - 1) * 3 + 1],
291 status[scroll + max_choice - 1],
292 max_choice - 1, TRUE);
293 wnoutrefresh (list);
295 print_arrows(dialog, choice, item_no, scroll,
296 box_y, box_x + check_x + 5, list_height);
298 wrefresh (dialog);
300 continue; /* wait for another key press */
301 } else
302 i = choice + 1;
304 if (i != choice) {
305 /* De-highlight current item */
306 print_item (list, items[(scroll + choice) * 3 + 1],
307 status[scroll + choice], choice, FALSE);
308 /* Highlight new item */
309 choice = i;
310 print_item (list, items[(scroll + choice) * 3 + 1],
311 status[scroll + choice], choice, TRUE);
312 wnoutrefresh (list);
313 wrefresh (dialog);
315 continue; /* wait for another key press */
317 switch (key) {
318 case 'H':
319 case 'h':
320 case '?':
321 delwin (dialog);
322 free (status);
323 return 1;
324 case 'S':
325 case 's':
326 case ' ':
327 case '\n':
328 if (!button) {
329 if (flag == FLAG_CHECK) {
330 status[scroll + choice] = !status[scroll + choice];
331 wmove (list, choice, check_x);
332 wattrset (list, check_selected_attr);
333 wprintw (list, "[%c]", status[scroll + choice] ? 'X' : ' ');
334 } else {
335 if (!status[scroll + choice]) {
336 for (i = 0; i < item_no; i++)
337 status[i] = 0;
338 status[scroll + choice] = 1;
339 for (i = 0; i < max_choice; i++)
340 print_item (list, items[(scroll + i) * 3 + 1],
341 status[scroll + i], i, i == choice);
344 wnoutrefresh (list);
345 wrefresh (dialog);
347 for (i = 0; i < item_no; i++) {
348 if (status[i]) {
349 if (flag == FLAG_CHECK) {
350 fprintf (stderr, "\"%s\" ", items[i * 3]);
351 } else {
352 fprintf (stderr, "%s", items[i * 3]);
358 delwin (dialog);
359 free (status);
360 return button;
361 case 'X':
362 case 'x':
363 key = ESC;
364 case ESC:
365 break;
368 /* Now, update everything... */
369 doupdate ();
373 delwin (dialog);
374 free (status);
375 return -1; /* ESC pressed */