1 /* 2 * $Id: checklist.c,v 1.127 2011/06/29 23:04:09 tom Exp $ 3 * 4 * checklist.c -- implements the checklist box 5 * 6 * Copyright 2000-2010,2011 Thomas E. Dickey 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU Lesser General Public License, version 2.1 10 * as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this program; if not, write to 19 * Free Software Foundation, Inc. 20 * 51 Franklin St., Fifth Floor 21 * Boston, MA 02110, USA. 22 * 23 * An earlier version of this program lists as authors: 24 * Savio Lam (lam836@cs.cuhk.hk) 25 * Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension 26 * Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two 27 */ 28 29 #include <dialog.h> 30 #include <dlg_keys.h> 31 32 static int list_width, check_x, item_x, checkflag; 33 34 #define MIN_HIGH (1 + (5 * MARGIN)) 35 36 #define LLEN(n) ((n) * CHECKBOX_TAGS) 37 #define ItemData(i) &items[LLEN(i)] 38 #define ItemName(i) items[LLEN(i)] 39 #define ItemText(i) items[LLEN(i) + 1] 40 #define ItemStatus(i) items[LLEN(i) + 2] 41 #define ItemHelp(i) items[LLEN(i) + 3] 42 43 static void 44 print_arrows(WINDOW *win, 45 int box_x, 46 int box_y, 47 int scrollamt, 48 int choice, 49 int item_no, 50 int list_height) 51 { 52 dlg_draw_scrollbar(win, 53 (long) (scrollamt), 54 (long) (scrollamt), 55 (long) (scrollamt + choice), 56 (long) (item_no), 57 box_x + check_x, 58 box_x + list_width, 59 box_y, 60 box_y + list_height + 1, 61 menubox_attr, 62 menubox_border_attr); 63 } 64 65 /* 66 * Print list item. The 'selected' parameter is true if 'choice' is the 67 * current item. That one is colored differently from the other items. 68 */ 69 static void 70 print_item(WINDOW *win, 71 DIALOG_LISTITEM * item, 72 const char *states, 73 int choice, 74 int selected) 75 { 76 chtype save = dlg_get_attrs(win); 77 int i; 78 chtype attr = A_NORMAL; 79 const int *cols; 80 const int *indx; 81 int limit; 82 83 /* Clear 'residue' of last item */ 84 wattrset(win, menubox_attr); 85 (void) wmove(win, choice, 0); 86 for (i = 0; i < list_width; i++) 87 (void) waddch(win, ' '); 88 89 (void) wmove(win, choice, check_x); 90 wattrset(win, selected ? check_selected_attr : check_attr); 91 (void) wprintw(win, 92 (checkflag == FLAG_CHECK) ? "[%c]" : "(%c)", 93 states[item->state]); 94 wattrset(win, menubox_attr); 95 (void) waddch(win, ' '); 96 97 if (strlen(item->name) != 0) { 98 99 indx = dlg_index_wchars(item->name); 100 101 wattrset(win, selected ? tag_key_selected_attr : tag_key_attr); 102 (void) waddnstr(win, item->name, indx[1]); 103 104 if ((int) strlen(item->name) > indx[1]) { 105 limit = dlg_limit_columns(item->name, (item_x - check_x - 6), 1); 106 if (limit > 1) { 107 wattrset(win, selected ? tag_selected_attr : tag_attr); 108 (void) waddnstr(win, 109 item->name + indx[1], 110 indx[limit] - indx[1]); 111 } 112 } 113 } 114 115 if (strlen(item->text) != 0) { 116 cols = dlg_index_columns(item->text); 117 limit = dlg_limit_columns(item->text, (getmaxx(win) - item_x + 1), 0); 118 119 if (limit > 0) { 120 (void) wmove(win, choice, item_x); 121 wattrset(win, selected ? item_selected_attr : item_attr); 122 dlg_print_text(win, item->text, cols[limit], &attr); 123 } 124 } 125 126 if (selected) { 127 dlg_item_help(item->help); 128 } 129 wattrset(win, save); 130 } 131 132 /* 133 * This is an alternate interface to 'checklist' which allows the application 134 * to read the list item states back directly without putting them in the 135 * output buffer. It also provides for more than two states over which the 136 * check/radio box can display. 137 */ 138 int 139 dlg_checklist(const char *title, 140 const char *cprompt, 141 int height, 142 int width, 143 int list_height, 144 int item_no, 145 DIALOG_LISTITEM * items, 146 const char *states, 147 int flag, 148 int *current_item) 149 { 150 /* *INDENT-OFF* */ 151 static DLG_KEYS_BINDING binding[] = { 152 HELPKEY_BINDINGS, 153 ENTERKEY_BINDINGS, 154 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), 155 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), 156 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), 157 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ), 158 DLG_KEYS_DATA( DLGK_ITEM_FIRST, KEY_HOME ), 159 DLG_KEYS_DATA( DLGK_ITEM_LAST, KEY_END ), 160 DLG_KEYS_DATA( DLGK_ITEM_LAST, KEY_LL ), 161 DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+' ), 162 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), 163 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), 164 DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ), 165 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), 166 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), 167 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), 168 DLG_KEYS_DATA( DLGK_PAGE_NEXT, DLGK_MOUSE(KEY_NPAGE) ), 169 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE ), 170 DLG_KEYS_DATA( DLGK_PAGE_PREV, DLGK_MOUSE(KEY_PPAGE) ), 171 END_KEYS_BINDING 172 }; 173 /* *INDENT-ON* */ 174 175 #ifdef KEY_RESIZE 176 int old_height = height; 177 int old_width = width; 178 #endif 179 int i, j, key2, found, x, y, cur_x, cur_y, box_x, box_y; 180 int key = 0, fkey; 181 int button = dialog_state.visit_items ? -1 : dlg_defaultno_button(); 182 int choice = dlg_default_listitem(items); 183 int scrollamt = 0; 184 int max_choice; 185 int was_mouse; 186 int use_height; 187 int use_width, name_width, text_width; 188 int result = DLG_EXIT_UNKNOWN; 189 int num_states; 190 WINDOW *dialog, *list; 191 char *prompt = dlg_strclone(cprompt); 192 const char **buttons = dlg_ok_labels(); 193 194 dlg_does_output(); 195 dlg_tab_correct_str(prompt); 196 197 /* 198 * If this is a radiobutton list, ensure that no more than one item is 199 * selected initially. Allow none to be selected, since some users may 200 * wish to provide this flavor. 201 */ 202 if (flag == FLAG_RADIO) { 203 bool first = TRUE; 204 205 for (i = 0; i < item_no; i++) { 206 if (items[i].state) { 207 if (first) { 208 first = FALSE; 209 } else { 210 items[i].state = 0; 211 } 212 } 213 } 214 } 215 #ifdef KEY_RESIZE 216 retry: 217 #endif 218 219 use_height = list_height; 220 if (use_height == 0) { 221 use_width = dlg_calc_list_width(item_no, items) + 10; 222 /* calculate height without items (4) */ 223 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MAX(26, use_width)); 224 dlg_calc_listh(&height, &use_height, item_no); 225 } else { 226 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + use_height, 26); 227 } 228 dlg_button_layout(buttons, &width); 229 dlg_print_size(height, width); 230 dlg_ctl_size(height, width); 231 232 /* we need at least two states */ 233 if (states == 0 || strlen(states) < 2) 234 states = " *"; 235 num_states = (int) strlen(states); 236 237 checkflag = flag; 238 239 x = dlg_box_x_ordinate(width); 240 y = dlg_box_y_ordinate(height); 241 242 dialog = dlg_new_window(height, width, y, x); 243 dlg_register_window(dialog, "checklist", binding); 244 dlg_register_buttons(dialog, "checklist", buttons); 245 246 dlg_mouse_setbase(x, y); 247 248 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 249 dlg_draw_bottom_box(dialog); 250 dlg_draw_title(dialog, title); 251 252 wattrset(dialog, dialog_attr); 253 dlg_print_autowrap(dialog, prompt, height, width); 254 255 list_width = width - 6; 256 getyx(dialog, cur_y, cur_x); 257 box_y = cur_y + 1; 258 box_x = (width - list_width) / 2 - 1; 259 260 /* 261 * After displaying the prompt, we know how much space we really have. 262 * Limit the list to avoid overwriting the ok-button. 263 */ 264 if (use_height + MIN_HIGH > height - cur_y) 265 use_height = height - MIN_HIGH - cur_y; 266 if (use_height <= 0) 267 use_height = 1; 268 269 max_choice = MIN(use_height, item_no); 270 271 /* create new window for the list */ 272 list = dlg_sub_window(dialog, use_height, list_width, 273 y + box_y + 1, x + box_x + 1); 274 275 /* draw a box around the list items */ 276 dlg_draw_box(dialog, box_y, box_x, 277 use_height + 2 * MARGIN, 278 list_width + 2 * MARGIN, 279 menubox_border_attr, menubox_attr); 280 281 text_width = 0; 282 name_width = 0; 283 /* Find length of longest item to center checklist */ 284 for (i = 0; i < item_no; i++) { 285 text_width = MAX(text_width, dlg_count_columns(items[i].text)); 286 name_width = MAX(name_width, dlg_count_columns(items[i].name)); 287 } 288 289 /* If the name+text is wider than the list is allowed, then truncate 290 * one or both of them. If the name is no wider than 1/4 of the list, 291 * leave it intact. 292 */ 293 use_width = (list_width - 6); 294 if (text_width + name_width > use_width) { 295 int need = (int) (0.25 * use_width); 296 if (name_width > need) { 297 int want = (int) (use_width * ((double) name_width) / 298 (text_width + name_width)); 299 name_width = (want > need) ? want : need; 300 } 301 text_width = use_width - name_width; 302 } 303 304 check_x = (use_width - (text_width + name_width)) / 2; 305 item_x = name_width + check_x + 6; 306 307 /* ensure we are scrolled to show the current choice */ 308 if (choice >= (max_choice + scrollamt)) { 309 scrollamt = choice - max_choice + 1; 310 choice = max_choice - 1; 311 } 312 /* Print the list */ 313 for (i = 0; i < max_choice; i++) { 314 print_item(list, 315 &items[i + scrollamt], 316 states, 317 i, i == choice); 318 } 319 (void) wnoutrefresh(list); 320 321 /* register the new window, along with its borders */ 322 dlg_mouse_mkbigregion(box_y + 1, box_x, use_height, list_width + 2, 323 KEY_MAX, 1, 1, 1 /* by lines */ ); 324 325 print_arrows(dialog, 326 box_x, box_y, 327 scrollamt, max_choice, item_no, use_height); 328 329 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); 330 331 while (result == DLG_EXIT_UNKNOWN) { 332 if (button < 0) /* --visit-items */ 333 wmove(dialog, box_y + choice + 1, box_x + check_x + 2); 334 335 key = dlg_mouse_wgetch(dialog, &fkey); 336 if (dlg_result_key(key, fkey, &result)) 337 break; 338 339 was_mouse = (fkey && is_DLGK_MOUSE(key)); 340 if (was_mouse) 341 key -= M_EVENT; 342 343 if (was_mouse && (key >= KEY_MAX)) { 344 getyx(dialog, cur_y, cur_x); 345 i = (key - KEY_MAX); 346 if (i < max_choice) { 347 /* De-highlight current item */ 348 print_item(list, 349 &items[scrollamt + choice], 350 states, 351 choice, FALSE); 352 /* Highlight new item */ 353 choice = (key - KEY_MAX); 354 print_item(list, 355 &items[scrollamt + choice], 356 states, 357 choice, TRUE); 358 (void) wnoutrefresh(list); 359 (void) wmove(dialog, cur_y, cur_x); 360 361 key = ' '; /* force the selected item to toggle */ 362 } else { 363 beep(); 364 continue; 365 } 366 fkey = FALSE; 367 } else if (was_mouse && key >= KEY_MIN) { 368 key = dlg_lookup_key(dialog, key, &fkey); 369 } 370 371 /* 372 * A space toggles the item status. We handle either a checklist 373 * (any number of items can be selected) or radio list (zero or one 374 * items can be selected). 375 */ 376 if (key == ' ') { 377 int current = scrollamt + choice; 378 int next = items[current].state + 1; 379 380 if (next >= num_states) 381 next = 0; 382 383 getyx(dialog, cur_y, cur_x); 384 if (flag == FLAG_CHECK) { /* checklist? */ 385 items[current].state = next; 386 print_item(list, 387 &items[scrollamt + choice], 388 states, 389 choice, TRUE); 390 } else { /* radiolist */ 391 for (i = 0; i < item_no; i++) { 392 if (i != current) { 393 items[i].state = 0; 394 } 395 } 396 if (items[current].state) { 397 items[current].state = next ? next : 1; 398 print_item(list, 399 &items[current], 400 states, 401 choice, TRUE); 402 } else { 403 items[current].state = 1; 404 for (i = 0; i < max_choice; i++) 405 print_item(list, 406 &items[scrollamt + i], 407 states, 408 i, i == choice); 409 } 410 } 411 (void) wnoutrefresh(list); 412 (void) wmove(dialog, cur_y, cur_x); 413 wrefresh(dialog); 414 continue; /* wait for another key press */ 415 } 416 417 /* 418 * Check if key pressed matches first character of any item tag in 419 * list. If there is more than one match, we will cycle through 420 * each one as the same key is pressed repeatedly. 421 */ 422 found = FALSE; 423 if (!fkey) { 424 if (button < 0 || !dialog_state.visit_items) { 425 for (j = scrollamt + choice + 1; j < item_no; j++) { 426 if (dlg_match_char(dlg_last_getc(), items[j].name)) { 427 found = TRUE; 428 i = j - scrollamt; 429 break; 430 } 431 } 432 if (!found) { 433 for (j = 0; j <= scrollamt + choice; j++) { 434 if (dlg_match_char(dlg_last_getc(), items[j].name)) { 435 found = TRUE; 436 i = j - scrollamt; 437 break; 438 } 439 } 440 } 441 if (found) 442 dlg_flush_getc(); 443 } else if ((j = dlg_char_to_button(key, buttons)) >= 0) { 444 button = j; 445 ungetch('\n'); 446 continue; 447 } 448 } 449 450 /* 451 * A single digit (1-9) positions the selection to that line in the 452 * current screen. 453 */ 454 if (!found 455 && (key <= '9') 456 && (key > '0') 457 && (key - '1' < max_choice)) { 458 found = TRUE; 459 i = key - '1'; 460 } 461 462 if (!found) { 463 if (fkey) { 464 found = TRUE; 465 switch (key) { 466 case DLGK_ITEM_FIRST: 467 i = -scrollamt; 468 break; 469 case DLGK_ITEM_LAST: 470 i = item_no - 1 - scrollamt; 471 break; 472 case DLGK_PAGE_PREV: 473 if (choice) 474 i = 0; 475 else if (scrollamt != 0) 476 i = -MIN(scrollamt, max_choice); 477 else 478 continue; 479 break; 480 case DLGK_PAGE_NEXT: 481 i = MIN(choice + max_choice, item_no - scrollamt - 1); 482 break; 483 case DLGK_ITEM_PREV: 484 i = choice - 1; 485 if (choice == 0 && scrollamt == 0) 486 continue; 487 break; 488 case DLGK_ITEM_NEXT: 489 i = choice + 1; 490 if (scrollamt + choice >= item_no - 1) 491 continue; 492 break; 493 default: 494 found = FALSE; 495 break; 496 } 497 } 498 } 499 500 if (found) { 501 if (i != choice) { 502 getyx(dialog, cur_y, cur_x); 503 if (i < 0 || i >= max_choice) { 504 #if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5 505 /* 506 * Using wscrl to assist ncurses scrolling is not needed 507 * in version 5.x 508 */ 509 if (i == -1) { 510 if (use_height > 1) { 511 /* De-highlight current first item */ 512 print_item(list, 513 &items[scrollamt], 514 states, 515 0, FALSE); 516 scrollok(list, TRUE); 517 wscrl(list, -1); 518 scrollok(list, FALSE); 519 } 520 scrollamt--; 521 print_item(list, 522 &items[scrollamt], 523 states, 524 0, TRUE); 525 } else if (i == max_choice) { 526 if (use_height > 1) { 527 /* De-highlight current last item before scrolling up */ 528 print_item(list, 529 &items[scrollamt + max_choice - 1], 530 states, 531 max_choice - 1, FALSE); 532 scrollok(list, TRUE); 533 wscrl(list, 1); 534 scrollok(list, FALSE); 535 } 536 scrollamt++; 537 print_item(list, 538 &items[scrollamt + max_choice - 1], 539 states, 540 max_choice - 1, TRUE); 541 } else 542 #endif 543 { 544 if (i < 0) { 545 scrollamt += i; 546 choice = 0; 547 } else { 548 choice = max_choice - 1; 549 scrollamt += (i - max_choice + 1); 550 } 551 for (i = 0; i < max_choice; i++) { 552 print_item(list, 553 &items[scrollamt + i], 554 states, 555 i, i == choice); 556 } 557 } 558 (void) wnoutrefresh(list); 559 print_arrows(dialog, 560 box_x, box_y, 561 scrollamt, max_choice, item_no, use_height); 562 } else { 563 /* De-highlight current item */ 564 print_item(list, 565 &items[scrollamt + choice], 566 states, 567 choice, FALSE); 568 /* Highlight new item */ 569 choice = i; 570 print_item(list, 571 &items[scrollamt + choice], 572 states, 573 choice, TRUE); 574 (void) wnoutrefresh(list); 575 print_arrows(dialog, 576 box_x, box_y, 577 scrollamt, max_choice, item_no, use_height); 578 (void) wmove(dialog, cur_y, cur_x); 579 wrefresh(dialog); 580 } 581 } 582 continue; /* wait for another key press */ 583 } 584 585 if (fkey) { 586 switch (key) { 587 case DLGK_ENTER: 588 result = dlg_enter_buttoncode(button); 589 break; 590 case DLGK_FIELD_PREV: 591 button = dlg_prev_button(buttons, button); 592 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, 593 FALSE, width); 594 break; 595 case DLGK_FIELD_NEXT: 596 button = dlg_next_button(buttons, button); 597 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, 598 FALSE, width); 599 break; 600 #ifdef KEY_RESIZE 601 case KEY_RESIZE: 602 /* reset data */ 603 height = old_height; 604 width = old_width; 605 /* repaint */ 606 dlg_clear(); 607 dlg_del_window(dialog); 608 refresh(); 609 dlg_mouse_free_regions(); 610 goto retry; 611 #endif 612 default: 613 if (was_mouse) { 614 if ((key2 = dlg_ok_buttoncode(key)) >= 0) { 615 result = key2; 616 break; 617 } 618 beep(); 619 } 620 } 621 } else { 622 beep(); 623 } 624 } 625 626 dlg_del_window(dialog); 627 dlg_mouse_free_regions(); 628 free(prompt); 629 *current_item = (scrollamt + choice); 630 return result; 631 } 632 633 /* 634 * Display a dialog box with a list of options that can be turned on or off 635 * The `flag' parameter is used to select between radiolist and checklist. 636 */ 637 int 638 dialog_checklist(const char *title, 639 const char *cprompt, 640 int height, 641 int width, 642 int list_height, 643 int item_no, 644 char **items, 645 int flag) 646 { 647 int result; 648 int i; 649 DIALOG_LISTITEM *listitems; 650 bool separate_output = ((flag == FLAG_CHECK) 651 && (dialog_vars.separate_output)); 652 bool show_status = FALSE; 653 int current = 0; 654 655 listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1); 656 assert_ptr(listitems, "dialog_checklist"); 657 658 for (i = 0; i < item_no; ++i) { 659 listitems[i].name = ItemName(i); 660 listitems[i].text = ItemText(i); 661 listitems[i].help = ((dialog_vars.item_help) 662 ? ItemHelp(i) 663 : dlg_strempty()); 664 listitems[i].state = !dlg_strcmp(ItemStatus(i), "on"); 665 } 666 dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no); 667 668 result = dlg_checklist(title, 669 cprompt, 670 height, 671 width, 672 list_height, 673 item_no, 674 listitems, 675 NULL, 676 flag, 677 ¤t); 678 679 switch (result) { 680 case DLG_EXIT_OK: /* FALLTHRU */ 681 case DLG_EXIT_EXTRA: 682 show_status = TRUE; 683 break; 684 case DLG_EXIT_HELP: 685 dlg_add_result("HELP "); 686 show_status = dialog_vars.help_status; 687 if (USE_ITEM_HELP(listitems[current].help)) { 688 if (show_status) { 689 if (separate_output) { 690 dlg_add_string(listitems[current].help); 691 dlg_add_separator(); 692 } else { 693 dlg_add_quoted(listitems[current].help); 694 } 695 } else { 696 dlg_add_string(listitems[current].help); 697 } 698 result = DLG_EXIT_ITEM_HELP; 699 } else { 700 if (show_status) { 701 if (separate_output) { 702 dlg_add_string(listitems[current].name); 703 dlg_add_separator(); 704 } else { 705 dlg_add_quoted(listitems[current].name); 706 } 707 } else { 708 dlg_add_string(listitems[current].name); 709 } 710 } 711 break; 712 } 713 714 if (show_status) { 715 for (i = 0; i < item_no; i++) { 716 if (listitems[i].state) { 717 if (separate_output) { 718 dlg_add_string(listitems[i].name); 719 dlg_add_separator(); 720 } else { 721 if (dlg_need_separator()) 722 dlg_add_separator(); 723 dlg_add_string(listitems[i].name); 724 } 725 } 726 } 727 } 728 729 dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no); 730 free(listitems); 731 return result; 732 } 733