Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  checklist.c -- implements the checklist box
0004  *
0005  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
0006  *     Stuart Herbert - S.Herbert@sheffield.ac.uk: radiolist extension
0007  *     Alessandro Rubini - rubini@ipvvis.unipv.it: merged the two
0008  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
0009  */
0010 
0011 #include "dialog.h"
0012 
0013 static int list_width, check_x, item_x;
0014 
0015 /*
0016  * Print list item
0017  */
0018 static void print_item(WINDOW * win, int choice, int selected)
0019 {
0020     int i;
0021     char *list_item = malloc(list_width + 1);
0022 
0023     strncpy(list_item, item_str(), list_width - item_x);
0024     list_item[list_width - item_x] = '\0';
0025 
0026     /* Clear 'residue' of last item */
0027     wattrset(win, dlg.menubox.atr);
0028     wmove(win, choice, 0);
0029     for (i = 0; i < list_width; i++)
0030         waddch(win, ' ');
0031 
0032     wmove(win, choice, check_x);
0033     wattrset(win, selected ? dlg.check_selected.atr
0034          : dlg.check.atr);
0035     if (!item_is_tag(':'))
0036         wprintw(win, "(%c)", item_is_tag('X') ? 'X' : ' ');
0037 
0038     wattrset(win, selected ? dlg.tag_selected.atr : dlg.tag.atr);
0039     mvwaddch(win, choice, item_x, list_item[0]);
0040     wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
0041     waddstr(win, list_item + 1);
0042     if (selected) {
0043         wmove(win, choice, check_x + 1);
0044         wrefresh(win);
0045     }
0046     free(list_item);
0047 }
0048 
0049 /*
0050  * Print the scroll indicators.
0051  */
0052 static void print_arrows(WINDOW * win, int choice, int item_no, int scroll,
0053          int y, int x, int height)
0054 {
0055     wmove(win, y, x);
0056 
0057     if (scroll > 0) {
0058         wattrset(win, dlg.uarrow.atr);
0059         waddch(win, ACS_UARROW);
0060         waddstr(win, "(-)");
0061     } else {
0062         wattrset(win, dlg.menubox.atr);
0063         waddch(win, ACS_HLINE);
0064         waddch(win, ACS_HLINE);
0065         waddch(win, ACS_HLINE);
0066         waddch(win, ACS_HLINE);
0067     }
0068 
0069     y = y + height + 1;
0070     wmove(win, y, x);
0071 
0072     if ((height < item_no) && (scroll + choice < item_no - 1)) {
0073         wattrset(win, dlg.darrow.atr);
0074         waddch(win, ACS_DARROW);
0075         waddstr(win, "(+)");
0076     } else {
0077         wattrset(win, dlg.menubox_border.atr);
0078         waddch(win, ACS_HLINE);
0079         waddch(win, ACS_HLINE);
0080         waddch(win, ACS_HLINE);
0081         waddch(win, ACS_HLINE);
0082     }
0083 }
0084 
0085 /*
0086  *  Display the termination buttons
0087  */
0088 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
0089 {
0090     int x = width / 2 - 11;
0091     int y = height - 2;
0092 
0093     print_button(dialog, "Select", y, x, selected == 0);
0094     print_button(dialog, " Help ", y, x + 14, selected == 1);
0095 
0096     wmove(dialog, y, x + 1 + 14 * selected);
0097     wrefresh(dialog);
0098 }
0099 
0100 /*
0101  * Display a dialog box with a list of options that can be turned on or off
0102  * in the style of radiolist (only one option turned on at a time).
0103  */
0104 int dialog_checklist(const char *title, const char *prompt, int height,
0105              int width, int list_height)
0106 {
0107     int i, x, y, box_x, box_y;
0108     int key = 0, button = 0, choice = 0, scroll = 0, max_choice;
0109     WINDOW *dialog, *list;
0110 
0111     /* which item to highlight */
0112     item_foreach() {
0113         if (item_is_tag('X'))
0114             choice = item_n();
0115         if (item_is_selected()) {
0116             choice = item_n();
0117             break;
0118         }
0119     }
0120 
0121 do_resize:
0122     if (getmaxy(stdscr) < (height + CHECKLIST_HEIGTH_MIN))
0123         return -ERRDISPLAYTOOSMALL;
0124     if (getmaxx(stdscr) < (width + CHECKLIST_WIDTH_MIN))
0125         return -ERRDISPLAYTOOSMALL;
0126 
0127     max_choice = MIN(list_height, item_count());
0128 
0129     /* center dialog box on screen */
0130     x = (getmaxx(stdscr) - width) / 2;
0131     y = (getmaxy(stdscr) - height) / 2;
0132 
0133     draw_shadow(stdscr, y, x, height, width);
0134 
0135     dialog = newwin(height, width, y, x);
0136     keypad(dialog, TRUE);
0137 
0138     draw_box(dialog, 0, 0, height, width,
0139          dlg.dialog.atr, dlg.border.atr);
0140     wattrset(dialog, dlg.border.atr);
0141     mvwaddch(dialog, height - 3, 0, ACS_LTEE);
0142     for (i = 0; i < width - 2; i++)
0143         waddch(dialog, ACS_HLINE);
0144     wattrset(dialog, dlg.dialog.atr);
0145     waddch(dialog, ACS_RTEE);
0146 
0147     print_title(dialog, title, width);
0148 
0149     wattrset(dialog, dlg.dialog.atr);
0150     print_autowrap(dialog, prompt, width - 2, 1, 3);
0151 
0152     list_width = width - 6;
0153     box_y = height - list_height - 5;
0154     box_x = (width - list_width) / 2 - 1;
0155 
0156     /* create new window for the list */
0157     list = subwin(dialog, list_height, list_width, y + box_y + 1,
0158               x + box_x + 1);
0159 
0160     keypad(list, TRUE);
0161 
0162     /* draw a box around the list items */
0163     draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2,
0164          dlg.menubox_border.atr, dlg.menubox.atr);
0165 
0166     /* Find length of longest item in order to center checklist */
0167     check_x = 0;
0168     item_foreach()
0169         check_x = MAX(check_x, strlen(item_str()) + 4);
0170     check_x = MIN(check_x, list_width);
0171 
0172     check_x = (list_width - check_x) / 2;
0173     item_x = check_x + 4;
0174 
0175     if (choice >= list_height) {
0176         scroll = choice - list_height + 1;
0177         choice -= scroll;
0178     }
0179 
0180     /* Print the list */
0181     for (i = 0; i < max_choice; i++) {
0182         item_set(scroll + i);
0183         print_item(list, i, i == choice);
0184     }
0185 
0186     print_arrows(dialog, choice, item_count(), scroll,
0187              box_y, box_x + check_x + 5, list_height);
0188 
0189     print_buttons(dialog, height, width, 0);
0190 
0191     wnoutrefresh(dialog);
0192     wnoutrefresh(list);
0193     doupdate();
0194 
0195     while (key != KEY_ESC) {
0196         key = wgetch(dialog);
0197 
0198         for (i = 0; i < max_choice; i++) {
0199             item_set(i + scroll);
0200             if (toupper(key) == toupper(item_str()[0]))
0201                 break;
0202         }
0203 
0204         if (i < max_choice || key == KEY_UP || key == KEY_DOWN ||
0205             key == '+' || key == '-') {
0206             if (key == KEY_UP || key == '-') {
0207                 if (!choice) {
0208                     if (!scroll)
0209                         continue;
0210                     /* Scroll list down */
0211                     if (list_height > 1) {
0212                         /* De-highlight current first item */
0213                         item_set(scroll);
0214                         print_item(list, 0, FALSE);
0215                         scrollok(list, TRUE);
0216                         wscrl(list, -1);
0217                         scrollok(list, FALSE);
0218                     }
0219                     scroll--;
0220                     item_set(scroll);
0221                     print_item(list, 0, TRUE);
0222                     print_arrows(dialog, choice, item_count(),
0223                              scroll, box_y, box_x + check_x + 5, list_height);
0224 
0225                     wnoutrefresh(dialog);
0226                     wrefresh(list);
0227 
0228                     continue;   /* wait for another key press */
0229                 } else
0230                     i = choice - 1;
0231             } else if (key == KEY_DOWN || key == '+') {
0232                 if (choice == max_choice - 1) {
0233                     if (scroll + choice >= item_count() - 1)
0234                         continue;
0235                     /* Scroll list up */
0236                     if (list_height > 1) {
0237                         /* De-highlight current last item before scrolling up */
0238                         item_set(scroll + max_choice - 1);
0239                         print_item(list,
0240                                 max_choice - 1,
0241                                 FALSE);
0242                         scrollok(list, TRUE);
0243                         wscrl(list, 1);
0244                         scrollok(list, FALSE);
0245                     }
0246                     scroll++;
0247                     item_set(scroll + max_choice - 1);
0248                     print_item(list, max_choice - 1, TRUE);
0249 
0250                     print_arrows(dialog, choice, item_count(),
0251                              scroll, box_y, box_x + check_x + 5, list_height);
0252 
0253                     wnoutrefresh(dialog);
0254                     wrefresh(list);
0255 
0256                     continue;   /* wait for another key press */
0257                 } else
0258                     i = choice + 1;
0259             }
0260             if (i != choice) {
0261                 /* De-highlight current item */
0262                 item_set(scroll + choice);
0263                 print_item(list, choice, FALSE);
0264                 /* Highlight new item */
0265                 choice = i;
0266                 item_set(scroll + choice);
0267                 print_item(list, choice, TRUE);
0268                 wnoutrefresh(dialog);
0269                 wrefresh(list);
0270             }
0271             continue;   /* wait for another key press */
0272         }
0273         switch (key) {
0274         case 'H':
0275         case 'h':
0276         case '?':
0277             button = 1;
0278             /* fall-through */
0279         case 'S':
0280         case 's':
0281         case ' ':
0282         case '\n':
0283             item_foreach()
0284                 item_set_selected(0);
0285             item_set(scroll + choice);
0286             item_set_selected(1);
0287             delwin(list);
0288             delwin(dialog);
0289             return button;
0290         case TAB:
0291         case KEY_LEFT:
0292         case KEY_RIGHT:
0293             button = ((key == KEY_LEFT ? --button : ++button) < 0)
0294                 ? 1 : (button > 1 ? 0 : button);
0295 
0296             print_buttons(dialog, height, width, button);
0297             wrefresh(dialog);
0298             break;
0299         case 'X':
0300         case 'x':
0301             key = KEY_ESC;
0302             break;
0303         case KEY_ESC:
0304             key = on_key_esc(dialog);
0305             break;
0306         case KEY_RESIZE:
0307             delwin(list);
0308             delwin(dialog);
0309             on_key_resize();
0310             goto do_resize;
0311         }
0312 
0313         /* Now, update everything... */
0314         doupdate();
0315     }
0316     delwin(list);
0317     delwin(dialog);
0318     return key;     /* ESC pressed */
0319 }