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.
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
28 #include "dialog.priv.h"
31 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);
33 #define DREF(di, item) ((di) ? &((di)[(item)]) : NULL)
36 * Display a dialog box with a list of options that can be turned on or off
39 dialog_checklist(unsigned char *title, unsigned char *prompt, int height, int width,
40 int list_height, int cnt, void *it, unsigned char *result)
42 int i, j, x, y, cur_x, cur_y, old_x, old_y, box_x, box_y, key = 0, button,
43 choice, l, k, scroll, max_choice, item_no = 0, *status;
44 int redraw_menu = FALSE, cursor_reset = FALSE;
45 int rval = 0, onlist = 1, ok_space, cancel_space;
46 char okButton, cancelButton;
47 WINDOW *dialog, *list;
48 unsigned char **items = NULL;
49 dialogMenuItem *ditems;
50 int list_width, check_x, item_x;
52 /* Allocate space for storing item on/off status */
53 if ((status = alloca(sizeof(int) * abs(cnt))) == NULL) {
55 fprintf(stderr, "\nCan't allocate memory in dialog_checklist().\n");
60 choice = scroll = button = 0;
61 /* Previous calling syntax, e.g. just a list of strings? */
66 /* Initializes status */
67 for (i = 0; i < item_no; i++)
68 status[i] = !strcasecmp(items[i*3 + 2], "on");
70 /* It's the new specification format - fake the rest of the code out */
75 items = (unsigned char **)alloca((item_no * 3) * sizeof(unsigned char *));
77 /* Initializes status */
78 for (i = 0; i < item_no; i++) {
79 status[i] = ditems[i].checked ? ditems[i].checked(&ditems[i]) : FALSE;
80 items[i*3] = ditems[i].prompt;
81 items[i*3 + 1] = ditems[i].title;
82 items[i*3 + 2] = status[i] ? "on" : "off";
85 max_choice = MIN(list_height, item_no);
89 /* Find length of longest item in order to center checklist */
90 for (i = 0; i < item_no; i++) {
91 l = strlen(items[i*3]);
92 for (j = 0; j < item_no; j++) {
93 k = strlen(items[j*3 + 1]);
94 check_x = MAX(check_x, l + k + 6);
96 item_x = MAX(item_x, l);
99 height = strheight(prompt)+list_height+4+2;
101 i = strwidth(prompt);
102 j = ((title != NULL) ? strwidth(title) : 0);
104 width = MAX(width,check_x+4)+4;
106 width = MAX(width,24);
112 /* center dialog box on screen */
113 x = (COLS - width)/2;
114 y = (LINES - height)/2;
118 draw_shadow(stdscr, y, x, height, width);
120 dialog = newwin(height, width, y, x);
121 if (dialog == NULL) {
123 fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width, y, x);
126 keypad(dialog, TRUE);
128 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
129 wattrset(dialog, border_attr);
130 wmove(dialog, height-3, 0);
131 waddch(dialog, ACS_LTEE);
132 for (i = 0; i < width-2; i++)
133 waddch(dialog, ACS_HLINE);
134 wattrset(dialog, dialog_attr);
135 waddch(dialog, ACS_RTEE);
136 wmove(dialog, height-2, 1);
137 for (i = 0; i < width-2; i++)
141 wattrset(dialog, title_attr);
142 wmove(dialog, 0, (width - strlen(title))/2 - 1);
144 waddstr(dialog, title);
147 wattrset(dialog, dialog_attr);
149 print_autowrap(dialog, prompt, height - 1, width - 2, width, 1, 2, TRUE, FALSE);
151 list_width = width - 6;
152 getyx(dialog, cur_y, cur_x);
154 box_x = (width - list_width) / 2 - 1;
156 /* create new window for the list */
157 list = subwin(dialog, list_height, list_width, y + box_y + 1, x + box_x + 1);
161 fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", list_height, list_width,
162 y + box_y + 1, x + box_x + 1);
167 /* draw a box around the list items */
168 draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2, menubox_border_attr, menubox_attr);
170 check_x = (list_width - check_x) / 2;
171 item_x = check_x + item_x + 6;
174 for (i = 0; i < max_choice; i++)
175 print_item(list, items[i * 3], items[i * 3 + 1], status[i], i, i == choice, DREF(ditems, i), list_width, item_x, check_x);
177 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
179 display_helpline(dialog, height - 1, width);
183 /* Is this a fancy new style argument string where we get to override
184 * the buttons, or an old style one where they're fixed?
186 if (ditems && result) {
187 cancelButton = toupper(ditems[CANCEL_BUTTON].prompt[0]);
188 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);
189 okButton = toupper(ditems[OK_BUTTON].prompt[0]);
190 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : TRUE);
194 print_button(dialog, "Cancel", y, x + 14, FALSE);
196 print_button(dialog, " OK ", y, x, TRUE);
198 wnoutrefresh(dialog);
199 wmove(list, choice, check_x+1);
203 * XXX Black magic voodoo that allows printing to the checklist
204 * window. For some reason, if this "refresh" code is not in
205 * place, printing to the window from the selected callback
206 * prints "behind" the checklist window. There is probably a
207 * better way to do this.
209 draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2, menubox_border_attr, menubox_attr);
211 for (i = 0; i < max_choice; i++)
212 print_item(list, items[i * 3], items[i * 3 + 1], status[i], i, i == choice, DREF(ditems, i), list_width, item_x, check_x);
213 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
215 wmove(list, choice, check_x+1);
216 wnoutrefresh(dialog);
218 /* XXX Black magic XXX */
221 key = wgetch(dialog);
223 /* Shortcut to OK? */
224 if (toupper(key) == okButton) {
226 if (result && ditems[OK_BUTTON].fire) {
230 save = dupwin(newscr);
231 st = ditems[OK_BUTTON].fire(&ditems[OK_BUTTON]);
232 if (st & DITEM_RESTORE) {
241 for (i = 0; i < item_no; i++) {
243 strcat(result, items[i*3]);
244 strcat(result, "\n");
249 key = ESC; /* Lemme out! */
253 /* Shortcut to cancel? */
254 if (toupper(key) == cancelButton) {
255 if (ditems && result && ditems[CANCEL_BUTTON].fire) {
259 save = dupwin(newscr);
260 st = ditems[CANCEL_BUTTON].fire(&ditems[CANCEL_BUTTON]);
261 if (st & DITEM_RESTORE) {
264 wmove(dialog, cur_y, cur_x);
269 key = ESC; /* I gotta go! */
273 /* Check if key pressed matches first character of any item tag in list */
274 for (i = 0; i < max_choice; i++)
275 if (key != ' ' && key < 0x100 && toupper(key) == toupper(items[(scroll+i)*3][0]))
278 if (i < max_choice || (key >= '1' && key <= MIN('9', '0'+max_choice)) ||
279 KEY_IS_UP(key) || KEY_IS_DOWN(key) || ((key == ' ' || key == '\n' ||
280 key == '\r') && onlist)) {
282 /* if moving from buttons to the list, reset and redraw buttons */
287 if (ditems && result) {
288 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);
289 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
292 print_button(dialog, "Cancel", y, x + 14, button);
293 print_button(dialog, " OK ", y, x, !button);
295 wmove(list, choice, check_x+1);
296 wnoutrefresh(dialog);
300 if (key >= '1' && key <= MIN('9', '0'+max_choice))
303 else if (KEY_IS_UP(key)) {
306 /* Scroll list down */
307 getyx(dialog, cur_y, cur_x); /* Save cursor position */
308 if (list_height > 1) {
309 /* De-highlight current first item before scrolling down */
310 print_item(list, items[scroll * 3], items[scroll * 3 + 1], status[scroll], 0,
311 FALSE, DREF(ditems, scroll), list_width, item_x, check_x);
312 scrollok(list, TRUE);
314 scrollok(list, FALSE);
317 print_item(list, items[scroll*3], items[scroll*3 + 1], status[scroll], 0,
318 TRUE, DREF(ditems, scroll), list_width, item_x, check_x);
319 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
320 wmove(list, choice, check_x+1);
321 wnoutrefresh(dialog);
324 continue; /* wait for another key press */
329 else if (KEY_IS_DOWN(key)) {
330 if (choice == max_choice - 1) {
331 if (scroll + choice < item_no - 1) {
333 getyx(dialog, cur_y, cur_x); /* Save cursor position */
334 if (list_height > 1) {
335 /* De-highlight current last item before scrolling up */
336 print_item(list, items[(scroll + max_choice - 1) * 3],
337 items[(scroll + max_choice - 1) * 3 + 1],
338 status[scroll + max_choice - 1], max_choice - 1,
339 FALSE, DREF(ditems, scroll + max_choice - 1), list_width, item_x, check_x);
340 scrollok(list, TRUE);
342 scrollok(list, FALSE);
345 print_item(list, items[(scroll + max_choice - 1) * 3],
346 items[(scroll + max_choice - 1) * 3 + 1],
347 status[scroll + max_choice - 1], max_choice - 1, TRUE,
348 DREF(ditems, scroll + max_choice - 1), list_width, item_x, check_x);
349 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
350 wmove(list, choice, check_x+1);
351 wnoutrefresh(dialog);
354 continue; /* wait for another key press */
359 else if ((key == ' ' || key == '\n' || key == '\r') && onlist) { /* Toggle item status */
360 char lbra = 0, rbra = 0, mark = 0;
362 getyx(list, old_y, old_x); /* Save cursor position */
365 if (ditems[scroll + choice].fire) {
369 save = dupwin(newscr);
370 st = ditems[scroll + choice].fire(&ditems[scroll + choice]); /* Call "fire" action */
371 if (st & DITEM_RESTORE) {
376 if (st & DITEM_REDRAW) {
378 for (i = 0; i < item_no; i++)
379 status[i] = ditems[i].checked ? ditems[i].checked(&ditems[i]) : FALSE;
380 for (i = 0; i < max_choice; i++) {
381 print_item(list, items[(scroll + i) * 3], items[(scroll + i) * 3 + 1],
382 status[scroll + i], i, i == choice, DREF(ditems, scroll + i), list_width, item_x, check_x);
385 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4,
389 if (st & DITEM_LEAVE_MENU) {
390 /* Allow a fire action to take us out of the menu */
395 else if (st & DITEM_RECREATE) {
402 status[scroll + choice] = ditems[scroll + choice].checked ?
403 ditems[scroll + choice].checked(&ditems[scroll + choice]) : FALSE;
404 lbra = ditems[scroll + choice].lbra;
405 rbra = ditems[scroll + choice].rbra;
406 mark = ditems[scroll + choice].mark;
409 status[scroll + choice] = !status[scroll + choice];
410 wmove(list, choice, check_x);
411 wattrset(list, check_selected_attr);
418 wprintw(list, "%c%c%c", lbra, status[scroll + choice] ? mark : ' ', rbra);
419 wmove(list, old_y, old_x); /* Restore cursor to previous position */
421 continue; /* wait for another key press */
425 /* De-highlight current item */
426 getyx(dialog, cur_y, cur_x); /* Save cursor position */
427 print_item(list, items[(scroll + choice) * 3], items[(scroll + choice) * 3 + 1],
428 status[scroll + choice], choice, FALSE, DREF(ditems, scroll + choice), list_width, item_x, check_x);
430 /* Highlight new item */
432 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);
433 wmove(list, choice, check_x+1); /* Restore cursor to previous position */
436 continue; /* wait for another key press */
440 case KEY_PPAGE: /* can we go up? */
441 if (scroll > height - 4)
442 scroll -= (height-4);
452 case KEY_NPAGE: /* can we go down a full page? */
453 if (scroll + list_height >= item_no-1 - list_height) {
454 scroll = item_no - list_height;
459 scroll += list_height;
467 case KEY_HOME: /* go to the top */
475 case KEY_END: /* Go to the bottom */
476 scroll = item_no - list_height;
479 choice = max_choice - 1;
487 /* move to next component */
488 if (onlist) { /* on list, next is ok button */
490 if (ditems && result) {
491 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);
492 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
494 cancel_space = strlen(ditems[OK_BUTTON].prompt) + 6;
497 print_button(dialog, "Cancel", y, x + 14, button);
498 print_button(dialog, " OK ", y, x, !button);
503 wmove(dialog, y, x + cancel_space);
505 wmove(dialog, y, x + ok_space);
509 else if (button) { /* on cancel button, next is list */
515 /* on ok button, next is cancel button, same as left/right case */
521 if (ditems && result) {
522 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);
523 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
525 cancel_space = strlen(ditems[OK_BUTTON].prompt) + 6;
528 print_button(dialog, "Cancel", y, x + 14, button);
529 print_button(dialog, " OK ", y, x, !button);
534 wmove(dialog, y, x + cancel_space);
536 wmove(dialog, y, x + ok_space);
545 if (result && ditems[button ? CANCEL_BUTTON : OK_BUTTON].fire) {
547 WINDOW *save = dupwin(newscr);
549 st = ditems[button ? CANCEL_BUTTON : OK_BUTTON].fire(&ditems[button ? CANCEL_BUTTON : OK_BUTTON]);
550 if (st & DITEM_RESTORE) {
555 if (st == DITEM_FAILURE)
561 for (i = 0; i < item_no; i++) {
563 strcat(result, items[i*3]);
564 strcat(result, "\n");
569 key = ESC; /* Bail out! */
573 /* Let me outta here! */
586 getyx(list, old_y, old_x);
590 * Re-draw a box around the list items. It is required
591 * if amount of list items is smaller than height of listbox.
592 * Otherwise un-redrawn field will be filled with default
593 * screen attributes instead of dialog attributes.
595 draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2, menubox_border_attr, menubox_attr);
597 for (i = 0; i < max_choice; i++)
598 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);
599 print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y);
601 /* redraw buttons to fix highlighting */
602 if (ditems && result) {
603 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);
604 print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button);
607 print_button(dialog, "Cancel", y, x + 14, button);
608 print_button(dialog, " OK ", y, x, !button);
610 wnoutrefresh(dialog);
612 wmove(list, choice, check_x+1);
613 cursor_reset = FALSE;
616 wmove(list, old_y, old_x);
632 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)
636 /* Clear 'residue' of last item */
637 wattrset(win, menubox_attr);
638 wmove(win, choice, 0);
639 for (i = 0; i < list_width; i++)
641 wmove(win, choice, check_x);
642 wattrset(win, selected ? check_selected_attr : check_attr);
643 wprintw(win, "%c%c%c", me && me->lbra ? me->lbra : '[',
644 status ? me && me->mark ? me->mark : 'X' : ' ',
645 me && me->rbra ? me->rbra : ']');
646 wattrset(win, menubox_attr);
648 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
650 wattrset(win, selected ? tag_selected_attr : tag_attr);
651 waddstr(win, tag + 1);
652 wmove(win, choice, item_x);
653 wattrset(win, selected ? item_selected_attr : item_attr);
655 /* If have a selection handler for this, call it */
656 if (me && me->selected) {
658 me->selected(me, selected);
661 /* End of print_item() */