1 // SPDX-License-Identifier: GPL-2.0+
3 * checklist.c -- implements the checklist box
13 static int list_width, check_x, item_x;
18 static void print_item(WINDOW * win, int choice, int selected)
21 char *list_item = malloc(list_width + 1);
23 strncpy(list_item, item_str(), list_width - item_x);
24 list_item[list_width - item_x] = '\0';
26 /* Clear 'residue' of last item */
27 wattrset(win, dlg.menubox.atr);
28 wmove(win, choice, 0);
29 for (i = 0; i < list_width; i++)
32 wmove(win, choice, check_x);
33 wattrset(win, selected ? dlg.check_selected.atr
35 if (!item_is_tag(':'))
36 wprintw(win, "(%c)", item_is_tag('X') ? 'X' : ' ');
38 wattrset(win, selected ? dlg.tag_selected.atr : dlg.tag.atr);
39 mvwaddch(win, choice, item_x, list_item[0]);
40 wattrset(win, selected ? dlg.item_selected.atr : dlg.item.atr);
41 waddstr(win, list_item + 1);
43 wmove(win, choice, check_x + 1);
50 * Print the scroll indicators.
52 static void print_arrows(WINDOW * win, int choice, int item_no, int scroll,
53 int y, int x, int height)
58 wattrset(win, dlg.uarrow.atr);
59 waddch(win, ACS_UARROW);
62 wattrset(win, dlg.menubox.atr);
63 waddch(win, ACS_HLINE);
64 waddch(win, ACS_HLINE);
65 waddch(win, ACS_HLINE);
66 waddch(win, ACS_HLINE);
72 if ((height < item_no) && (scroll + choice < item_no - 1)) {
73 wattrset(win, dlg.darrow.atr);
74 waddch(win, ACS_DARROW);
77 wattrset(win, dlg.menubox_border.atr);
78 waddch(win, ACS_HLINE);
79 waddch(win, ACS_HLINE);
80 waddch(win, ACS_HLINE);
81 waddch(win, ACS_HLINE);
86 * Display the termination buttons
88 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
90 int x = width / 2 - 11;
93 print_button(dialog, gettext("Select"), y, x, selected == 0);
94 print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
96 wmove(dialog, y, x + 1 + 14 * selected);
101 * Display a dialog box with a list of options that can be turned on or off
102 * in the style of radiolist (only one option turned on at a time).
104 int dialog_checklist(const char *title, const char *prompt, int height,
105 int width, int list_height)
107 int i, x, y, box_x, box_y;
108 int key = 0, button = 0, choice = 0, scroll = 0, max_choice;
109 WINDOW *dialog, *list;
111 /* which item to highlight */
113 if (item_is_tag('X'))
115 if (item_is_selected()) {
122 if (getmaxy(stdscr) < (height + CHECKLIST_HEIGTH_MIN))
123 return -ERRDISPLAYTOOSMALL;
124 if (getmaxx(stdscr) < (width + CHECKLIST_WIDTH_MIN))
125 return -ERRDISPLAYTOOSMALL;
127 max_choice = MIN(list_height, item_count());
129 /* center dialog box on screen */
130 x = (getmaxx(stdscr) - width) / 2;
131 y = (getmaxy(stdscr) - height) / 2;
133 draw_shadow(stdscr, y, x, height, width);
135 dialog = newwin(height, width, y, x);
136 keypad(dialog, TRUE);
138 draw_box(dialog, 0, 0, height, width,
139 dlg.dialog.atr, dlg.border.atr);
140 wattrset(dialog, dlg.border.atr);
141 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
142 for (i = 0; i < width - 2; i++)
143 waddch(dialog, ACS_HLINE);
144 wattrset(dialog, dlg.dialog.atr);
145 waddch(dialog, ACS_RTEE);
147 print_title(dialog, title, width);
149 wattrset(dialog, dlg.dialog.atr);
150 print_autowrap(dialog, prompt, width - 2, 1, 3);
152 list_width = width - 6;
153 box_y = height - list_height - 5;
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,
162 /* draw a box around the list items */
163 draw_box(dialog, box_y, box_x, list_height + 2, list_width + 2,
164 dlg.menubox_border.atr, dlg.menubox.atr);
166 /* Find length of longest item in order to center checklist */
169 check_x = MAX(check_x, strlen(item_str()) + 4);
170 check_x = MIN(check_x, list_width);
172 check_x = (list_width - check_x) / 2;
173 item_x = check_x + 4;
175 if (choice >= list_height) {
176 scroll = choice - list_height + 1;
181 for (i = 0; i < max_choice; i++) {
182 item_set(scroll + i);
183 print_item(list, i, i == choice);
186 print_arrows(dialog, choice, item_count(), scroll,
187 box_y, box_x + check_x + 5, list_height);
189 print_buttons(dialog, height, width, 0);
191 wnoutrefresh(dialog);
195 while (key != KEY_ESC) {
196 key = wgetch(dialog);
198 for (i = 0; i < max_choice; i++) {
199 item_set(i + scroll);
200 if (toupper(key) == toupper(item_str()[0]))
204 if (i < max_choice || key == KEY_UP || key == KEY_DOWN ||
205 key == '+' || key == '-') {
206 if (key == KEY_UP || key == '-') {
210 /* Scroll list down */
211 if (list_height > 1) {
212 /* De-highlight current first item */
214 print_item(list, 0, FALSE);
215 scrollok(list, TRUE);
217 scrollok(list, FALSE);
221 print_item(list, 0, TRUE);
222 print_arrows(dialog, choice, item_count(),
223 scroll, box_y, box_x + check_x + 5, list_height);
225 wnoutrefresh(dialog);
228 continue; /* wait for another key press */
231 } else if (key == KEY_DOWN || key == '+') {
232 if (choice == max_choice - 1) {
233 if (scroll + choice >= item_count() - 1)
236 if (list_height > 1) {
237 /* De-highlight current last item before scrolling up */
238 item_set(scroll + max_choice - 1);
242 scrollok(list, TRUE);
244 scrollok(list, FALSE);
247 item_set(scroll + max_choice - 1);
248 print_item(list, max_choice - 1, TRUE);
250 print_arrows(dialog, choice, item_count(),
251 scroll, box_y, box_x + check_x + 5, list_height);
253 wnoutrefresh(dialog);
256 continue; /* wait for another key press */
261 /* De-highlight current item */
262 item_set(scroll + choice);
263 print_item(list, choice, FALSE);
264 /* Highlight new item */
266 item_set(scroll + choice);
267 print_item(list, choice, TRUE);
268 wnoutrefresh(dialog);
271 continue; /* wait for another key press */
284 item_set_selected(0);
285 item_set(scroll + choice);
286 item_set_selected(1);
293 button = ((key == KEY_LEFT ? --button : ++button) < 0)
294 ? 1 : (button > 1 ? 0 : button);
296 print_buttons(dialog, height, width, button);
304 key = on_key_esc(dialog);
313 /* Now, update everything... */
318 return key; /* ESC pressed */