2 * checklist.c -- implements the checklist box
4 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * Substantial rennovation: 12/18/95, Jordan K. Hubbard
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static const char rcsid[] =
30 #include "dialog.priv.h"
33 static void print_item(WINDOW *win, unsigned char *tag, unsigned char *item, int status, int choice, int selected, dialogMenuItem *me, int list_width, int item_x, int check_x);
35 #define DREF(di, item) ((di) ? &((di)[(item)]) : NULL)
38 * Display a dialog box with a list of options that can be turned on or off
41 dialog_checklist(unsigned char *title, unsigned char *prompt, int height, int width,
42 int list_height, int cnt, void *it, unsigned char *result)
44 int i, j, x, y, cur_x, cur_y, old_x, old_y, box_x, box_y, key = 0, button,
45 choice, l, k, scroll, max_choice, item_no = 0, *status;
46 int redraw_menu = FALSE, cursor_reset = FALSE;
47 int rval = 0, onlist = 1, ok_space, cancel_space;
48 char okButton, cancelButton;
49 WINDOW *dialog, *list;
50 unsigned char **items = NULL;
51 dialogMenuItem *ditems;
52 int list_width, check_x, item_x;
54 /* Allocate space for storing item on/off status */
55 if ((status = alloca(sizeof(int) * abs(cnt))) == NULL) {
57 fprintf(stderr, "\nCan't allocate memory in dialog_checklist().\n");
62 choice = scroll = button = 0;
63 /* Previous calling syntax, e.g. just a list of strings? */
68 /* Initializes status */
69 for (i = 0; i < item_no; i++)
70 status[i] = !strcasecmp(items[i*3 + 2], "on");
72 /* It's the new specification format - fake the rest of the code out */
77 items = (unsigned char **)alloca((item_no * 3) * sizeof(unsigned char *));
79 /* Initializes status */
80 for (i = 0; i < item_no; i++) {
81 status[i] = ditems[i].checked ? ditems[i].checked(&ditems[i]) : FALSE;
82 items[i*3] = ditems[i].prompt;
83 items[i*3 + 1] = ditems[i].title;
84 items[i*3 + 2] = status[i] ? "on" : "off";
87 max_choice = MIN(list_height, item_no);
91 /* Find length of longest item in order to center checklist */
92 for (i = 0; i < item_no; i++) {
93 l = strlen(items[i*3]);
94 for (j = 0; j < item_no; j++) {
95 k = strlen(items[j*3 + 1]);
96 check_x = MAX(check_x, l + k + 6);
98 item_x = MAX(item_x, l);
101 height = strheight(prompt)+list_height+4+2;
103 i = strwidth(prompt);
104 j = ((title != NULL) ? strwidth(title) : 0);
106 width = MAX(width,check_x+4)+4;
108 width = MAX(width,24);
114 /* center dialog box on screen */
115 x = (COLS - width)/2;
116 y = (LINES - height)/2;
120 draw_shadow(stdscr, y, x, height, width);
122 dialog = newwin(height, width, y, x);
123 if (dialog == NULL) {
125 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width, y, x);
128 keypad(dialog, TRUE);
130 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
131 wattrset(dialog, border_attr);
132 wmove(dialog, height-3, 0);
133 waddch(dialog, ACS_LTEE);
134 for (i = 0; i < width-2; i++)
135 waddch(dialog, ACS_HLINE);
136 wattrset(dialog, dialog_attr);
137 waddch(dialog, ACS_RTEE);
138 wmove(dialog, height-2, 1);
139 for (i = 0; i < width-2; i++)
143 wattrset(dialog, title_attr);
144 wmove(dialog, 0, (width - strlen(title))/2 - 1);
146 waddstr(dialog, title);
149 wattrset(dialog, dialog_attr);
151 print_autowrap(dialog, prompt, height - 1, width - 2, width, 1, 2, TRUE, FALSE);
153 list_width = width - 6;
154 getyx(dialog, cur_y, cur_x);
156 box_x = (width - list_width) / 2 - 1;
158 /* create new window for the list */
159 list = subwin(dialog, list_height, list_width, y + box_y + 1, x + box_x + 1);
163 fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", list_height, list_width,
164 y + box_y + 1, x + box_x + 1);
169 /* draw a box around the list items */
170 draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2, menubox_border_attr, menubox_attr);
172 check_x = (list_width - check_x) / 2;
173 item_x = check_x + item_x + 6;
176 for (i = 0; i < max_choice; i++)
177 print_item(list, items[i * 3], items[i * 3 + 1], status[i], i, i == choice, DREF(ditems, i), list_width, item_x, check_x);
179 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
181 display_helpline(dialog, height - 1, width);
185 /* Is this a fancy new style argument string where we get to override
186 * the buttons, or an old style one where they're fixed?
188 if (ditems && result) {
189 cancelButton = toupper(ditems[CANCEL_BUTTON].prompt[0]);
190 print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : FALSE);
191 okButton = toupper(ditems[OK_BUTTON].prompt[0]);
192 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : TRUE);
196 print_button(dialog, "Cancel", y, x + 14, FALSE);
198 print_button(dialog, " OK ", y, x, TRUE);
200 wnoutrefresh(dialog);
201 wmove(list, choice, check_x+1);
205 key = wgetch(dialog);
207 /* Shortcut to OK? */
208 if (toupper(key) == okButton) {
210 if (result && ditems[OK_BUTTON].fire) {
214 save = dupwin(newscr);
215 st = ditems[OK_BUTTON].fire(&ditems[OK_BUTTON]);
216 if (st & DITEM_RESTORE) {
225 for (i = 0; i < item_no; i++) {
227 strcat(result, items[i*3]);
228 strcat(result, "\n");
233 key = ESC; /* Lemme out! */
237 /* Shortcut to cancel? */
238 if (toupper(key) == cancelButton) {
239 if (ditems && result && ditems[CANCEL_BUTTON].fire) {
243 save = dupwin(newscr);
244 st = ditems[CANCEL_BUTTON].fire(&ditems[CANCEL_BUTTON]);
245 if (st & DITEM_RESTORE) {
248 wmove(dialog, cur_y, cur_x);
253 key = ESC; /* I gotta go! */
257 /* Check if key pressed matches first character of any item tag in list */
258 for (i = 0; i < max_choice; i++)
259 if (key != ' ' && key < 0x100 && toupper(key) == toupper(items[(scroll+i)*3][0]))
262 if (i < max_choice || (key >= '1' && key <= MIN('9', '0'+max_choice)) ||
263 KEY_IS_UP(key) || KEY_IS_DOWN(key) || ((key == ' ' || key == '\n' ||
264 key == '\r') && onlist)) {
266 /* if moving from buttons to the list, reset and redraw buttons */
271 if (ditems && result) {
272 print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : button);
273 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
276 print_button(dialog, "Cancel", y, x + 14, button);
277 print_button(dialog, " OK ", y, x, !button);
279 wmove(list, choice, check_x+1);
280 wnoutrefresh(dialog);
284 if (key >= '1' && key <= MIN('9', '0'+max_choice))
287 else if (KEY_IS_UP(key)) {
290 /* Scroll list down */
291 getyx(dialog, cur_y, cur_x); /* Save cursor position */
292 if (list_height > 1) {
293 /* De-highlight current first item before scrolling down */
294 print_item(list, items[scroll * 3], items[scroll * 3 + 1], status[scroll], 0,
295 FALSE, DREF(ditems, scroll), list_width, item_x, check_x);
296 scrollok(list, TRUE);
298 scrollok(list, FALSE);
301 print_item(list, items[scroll*3], items[scroll*3 + 1], status[scroll], 0,
302 TRUE, DREF(ditems, scroll), list_width, item_x, check_x);
303 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
304 wmove(list, choice, check_x+1);
305 wnoutrefresh(dialog);
308 continue; /* wait for another key press */
313 else if (KEY_IS_DOWN(key)) {
314 if (choice == max_choice - 1) {
315 if (scroll + choice < item_no - 1) {
317 getyx(dialog, cur_y, cur_x); /* Save cursor position */
318 if (list_height > 1) {
319 /* De-highlight current last item before scrolling up */
320 print_item(list, items[(scroll + max_choice - 1) * 3],
321 items[(scroll + max_choice - 1) * 3 + 1],
322 status[scroll + max_choice - 1], max_choice - 1,
323 FALSE, DREF(ditems, scroll + max_choice - 1), list_width, item_x, check_x);
324 scrollok(list, TRUE);
326 scrollok(list, FALSE);
329 print_item(list, items[(scroll + max_choice - 1) * 3],
330 items[(scroll + max_choice - 1) * 3 + 1],
331 status[scroll + max_choice - 1], max_choice - 1, TRUE,
332 DREF(ditems, scroll + max_choice - 1), list_width, item_x, check_x);
333 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
334 wmove(list, choice, check_x+1);
335 wnoutrefresh(dialog);
338 continue; /* wait for another key press */
343 else if ((key == ' ' || key == '\n' || key == '\r') && onlist) { /* Toggle item status */
344 char lbra = 0, rbra = 0, mark = 0;
346 getyx(list, old_y, old_x); /* Save cursor position */
349 if (ditems[scroll + choice].fire) {
353 save = dupwin(newscr);
354 st = ditems[scroll + choice].fire(&ditems[scroll + choice]); /* Call "fire" action */
355 if (st & DITEM_RESTORE) {
360 if (st & DITEM_REDRAW) {
362 for (i = 0; i < item_no; i++)
363 status[i] = ditems[i].checked ? ditems[i].checked(&ditems[i]) : FALSE;
364 for (i = 0; i < max_choice; i++) {
365 print_item(list, items[(scroll + i) * 3], items[(scroll + i) * 3 + 1],
366 status[scroll + i], i, i == choice, DREF(ditems, scroll + i), list_width, item_x, check_x);
369 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4,
373 if (st & DITEM_LEAVE_MENU) {
374 /* Allow a fire action to take us out of the menu */
379 else if (st & DITEM_RECREATE) {
386 status[scroll + choice] = ditems[scroll + choice].checked ?
387 ditems[scroll + choice].checked(&ditems[scroll + choice]) : FALSE;
388 lbra = ditems[scroll + choice].lbra;
389 rbra = ditems[scroll + choice].rbra;
390 mark = ditems[scroll + choice].mark;
393 status[scroll + choice] = !status[scroll + choice];
394 wmove(list, choice, check_x);
395 wattrset(list, check_selected_attr);
402 wprintw(list, "%c%c%c", lbra, status[scroll + choice] ? mark : ' ', rbra);
403 wmove(list, old_y, old_x); /* Restore cursor to previous position */
405 continue; /* wait for another key press */
409 /* De-highlight current item */
410 getyx(dialog, cur_y, cur_x); /* Save cursor position */
411 print_item(list, items[(scroll + choice) * 3], items[(scroll + choice) * 3 + 1],
412 status[scroll + choice], choice, FALSE, DREF(ditems, scroll + choice), list_width, item_x, check_x);
414 /* Highlight new item */
416 print_item(list, items[(scroll + choice) * 3], items[(scroll + choice) * 3 + 1], status[scroll + choice], choice, TRUE, DREF(ditems, scroll + choice), list_width, item_x, check_x);
417 wmove(list, choice, check_x+1); /* Restore cursor to previous position */
420 continue; /* wait for another key press */
424 case KEY_PPAGE: /* can we go up? */
425 if (scroll > height - 4)
426 scroll -= (height-4);
436 case KEY_NPAGE: /* can we go down a full page? */
437 if (scroll + list_height >= item_no-1 - list_height) {
438 scroll = item_no - list_height;
443 scroll += list_height;
451 case KEY_HOME: /* go to the top */
459 case KEY_END: /* Go to the bottom */
460 scroll = item_no - list_height;
463 choice = max_choice - 1;
471 /* move to next component */
472 if (onlist) { /* on list, next is ok button */
474 if (ditems && result) {
475 print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : button);
476 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
478 cancel_space = strlen(ditems[OK_BUTTON].prompt) + 6;
481 print_button(dialog, "Cancel", y, x + 14, button);
482 print_button(dialog, " OK ", y, x, !button);
487 wmove(dialog, y, x + cancel_space);
489 wmove(dialog, y, x + ok_space);
493 else if (button) { /* on cancel button, next is list */
499 /* on ok button, next is cancel button, same as left/right case */
505 if (ditems && result) {
506 print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : button);
507 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
509 cancel_space = strlen(ditems[OK_BUTTON].prompt) + 6;
512 print_button(dialog, "Cancel", y, x + 14, button);
513 print_button(dialog, " OK ", y, x, !button);
518 wmove(dialog, y, x + cancel_space);
520 wmove(dialog, y, x + ok_space);
529 if (result && ditems[button ? CANCEL_BUTTON : OK_BUTTON].fire) {
531 WINDOW *save = dupwin(newscr);
533 st = ditems[button ? CANCEL_BUTTON : OK_BUTTON].fire(&ditems[button ? CANCEL_BUTTON : OK_BUTTON]);
534 if (st & DITEM_RESTORE) {
539 if (st == DITEM_FAILURE)
545 for (i = 0; i < item_no; i++) {
547 strcat(result, items[i*3]);
548 strcat(result, "\n");
553 key = ESC; /* Bail out! */
557 /* Let me outta here! */
570 getyx(list, old_y, old_x);
572 for (i = 0; i < max_choice; i++)
573 print_item(list, items[(scroll + i) * 3], items[(scroll + i) * 3 + 1], status[scroll + i], i, i == choice, DREF(ditems, scroll + i), list_width, item_x, check_x);
574 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
576 /* redraw buttons to fix highlighting */
577 if (ditems && result) {
578 print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : button);
579 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
582 print_button(dialog, "Cancel", y, x + 14, button);
583 print_button(dialog, " OK ", y, x, !button);
585 wnoutrefresh(dialog);
587 wmove(list, choice, check_x+1);
588 cursor_reset = FALSE;
591 wmove(list, old_y, old_x);
607 print_item(WINDOW *win, unsigned char *tag, unsigned char *item, int status, int choice, int selected, dialogMenuItem *me, int list_width, int item_x, int check_x)
611 /* Clear 'residue' of last item */
612 wattrset(win, menubox_attr);
613 wmove(win, choice, 0);
614 for (i = 0; i < list_width; i++)
616 wmove(win, choice, check_x);
617 wattrset(win, selected ? check_selected_attr : check_attr);
618 wprintw(win, "%c%c%c", me && me->lbra ? me->lbra : '[',
619 status ? me && me->mark ? me->mark : 'X' : ' ',
620 me && me->rbra ? me->rbra : ']');
621 wattrset(win, menubox_attr);
623 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
625 wattrset(win, selected ? tag_selected_attr : tag_attr);
626 waddstr(win, tag + 1);
627 wmove(win, choice, item_x);
628 wattrset(win, selected ? item_selected_attr : item_attr);
630 /* If have a selection handler for this, call it */
631 if (me && me->selected) {
633 me->selected(me, selected);
636 /* End of print_item() */