2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 * T2 SDE: misc/confdialog/checklist.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
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.
43 static int list_width
, check_x
, item_x
, checkflag
;
49 print_item (WINDOW
* win
, const char *item
, int status
,
50 int choice
, int selected
)
54 /* Clear 'residue' of last item */
55 wattrset (win
, menubox_attr
);
56 wmove (win
, choice
, 0);
57 for (i
= 0; i
< list_width
; i
++)
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' : ' ');
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);
72 wmove (win
, choice
, check_x
+1);
78 * Print the scroll indicators.
81 print_arrows (WINDOW
* win
, int choice
, int item_no
, int scroll
,
82 int y
, int x
, int height
)
87 wattrset (win
, uarrow_attr
);
88 waddch (win
, ACS_UARROW
);
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
);
102 if ((height
< item_no
) && (scroll
+ choice
< item_no
- 1)) {
103 wattrset (win
, darrow_attr
);
104 waddch (win
, ACS_DARROW
);
105 waddstr (win
, "(+)");
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
120 print_buttons( WINDOW
*dialog
, int height
, int width
, int selected
)
122 int x
= width
/ 2 - 5;
125 print_button (dialog
, "Select", y
, x
, 1);
127 wmove(dialog
, y
, x
+1);
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
;
146 /* Allocate space for storing item on/off status */
147 if ((status
= malloc (sizeof (int) * item_no
)) == NULL
) {
150 "\nCan't allocate memory in dialog_checklist().\n");
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
])
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';
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);
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 */
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;
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);
236 wnoutrefresh (dialog
);
240 key
= wgetch (dialog
);
242 for (i
= 0; i
< max_choice
; i
++)
243 if (toupper(key
) == toupper(items
[(scroll
+i
)*3+1][0]))
247 if ( i
< max_choice
|| key
== KEY_UP
|| key
== KEY_DOWN
||
248 key
== '+' || key
== '-' ) {
249 if (key
== KEY_UP
|| key
== '-') {
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
);
260 scrollok (list
, FALSE
);
263 print_item (list
, items
[scroll
* 3 + 1],
264 status
[scroll
], 0, TRUE
);
267 print_arrows(dialog
, choice
, item_no
, scroll
,
268 box_y
, box_x
+ check_x
+ 5, list_height
);
272 continue; /* wait for another key press */
275 } else if (key
== KEY_DOWN
|| key
== '+') {
276 if (choice
== max_choice
- 1) {
277 if (scroll
+ choice
>= item_no
- 1)
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
);
287 scrollok (list
, FALSE
);
290 print_item (list
, items
[(scroll
+ max_choice
- 1) * 3 + 1],
291 status
[scroll
+ max_choice
- 1],
292 max_choice
- 1, TRUE
);
295 print_arrows(dialog
, choice
, item_no
, scroll
,
296 box_y
, box_x
+ check_x
+ 5, list_height
);
300 continue; /* wait for another key press */
305 /* De-highlight current item */
306 print_item (list
, items
[(scroll
+ choice
) * 3 + 1],
307 status
[scroll
+ choice
], choice
, FALSE
);
308 /* Highlight new item */
310 print_item (list
, items
[(scroll
+ choice
) * 3 + 1],
311 status
[scroll
+ choice
], choice
, TRUE
);
315 continue; /* wait for another key press */
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' : ' ');
335 if (!status
[scroll
+ choice
]) {
336 for (i
= 0; i
< item_no
; i
++)
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
);
347 for (i
= 0; i
< item_no
; i
++) {
349 if (flag
== FLAG_CHECK
) {
350 fprintf (stderr
, "\"%s\" ", items
[i
* 3]);
352 fprintf (stderr
, "%s", items
[i
* 3]);
368 /* Now, update everything... */
375 return -1; /* ESC pressed */