diff options
Diffstat (limited to 'checklist.c')
-rw-r--r-- | checklist.c | 387 |
1 files changed, 170 insertions, 217 deletions
diff --git a/checklist.c b/checklist.c index 3904e90..0ae18a6 100644 --- a/checklist.c +++ b/checklist.c @@ -1,9 +1,9 @@ /* - * $Id: checklist.c,v 1.135 2012/07/01 16:30:04 Zoltan.Kelemen Exp $ + * $Id: checklist.c,v 1.151 2013/03/23 10:51:48 tom Exp $ * * checklist.c -- implements the checklist box * - * Copyright 2000-2011,2012 Thomas E. Dickey + * Copyright 2000-2012,2013 Thomas E. Dickey * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License, version 2.1 @@ -29,45 +29,32 @@ #include <dialog.h> #include <dlg_keys.h> -static int list_width, check_x, item_x, checkflag; - #define MIN_HIGH (1 + (5 * MARGIN)) -#define LLEN(n) ((n) * CHECKBOX_TAGS) -#define ItemData(i) &items[LLEN(i)] -#define ItemName(i) items[LLEN(i)] -#define ItemText(i) items[LLEN(i) + 1] -#define ItemStatus(i) items[LLEN(i) + 2] -#define ItemHelp(i) items[LLEN(i) + 3] - -static void -print_arrows(WINDOW *win, - int box_x, - int box_y, - int scrollamt, - int choice, - int item_no, - int list_height) -{ - dlg_draw_scrollbar(win, - (long) (scrollamt), - (long) (scrollamt), - (long) (scrollamt + choice), - (long) (item_no), - box_x + check_x, - box_x + list_width, - box_y, - box_y + list_height + 1, - menubox_border2_attr, - menubox_border_attr); -} +typedef struct { + /* the outer-window */ + WINDOW *dialog; + int box_y; + int box_x; + int check_x; + int item_x; + int checkflag; + int use_height; + int use_width; + /* the inner-window */ + WINDOW *list; + DIALOG_LISTITEM *items; + int item_no; + const char *states; +} ALL_DATA; /* * Print list item. The 'selected' parameter is true if 'choice' is the * current item. That one is colored differently from the other items. */ static void -print_item(WINDOW *win, +print_item(ALL_DATA * data, + WINDOW *win, DIALOG_LISTITEM * item, const char *states, int choice, @@ -75,58 +62,84 @@ print_item(WINDOW *win, { chtype save = dlg_get_attrs(win); int i; - chtype attr = A_NORMAL; - const int *cols; - const int *indx; - int limit; + bool both = (!dialog_vars.no_tags && !dialog_vars.no_items); + bool first = TRUE; + int climit = (getmaxx(win) - data->check_x + 1); + const char *show = (dialog_vars.no_items + ? item->name + : item->text); /* Clear 'residue' of last item */ (void) wattrset(win, menubox_attr); (void) wmove(win, choice, 0); - for (i = 0; i < list_width; i++) + for (i = 0; i < data->use_width; i++) (void) waddch(win, ' '); - (void) wmove(win, choice, check_x); + (void) wmove(win, choice, data->check_x); (void) wattrset(win, selected ? check_selected_attr : check_attr); (void) wprintw(win, - (checkflag == FLAG_CHECK) ? "[%c]" : "(%c)", + (data->checkflag == FLAG_CHECK) ? "[%c]" : "(%c)", states[item->state]); (void) wattrset(win, menubox_attr); (void) waddch(win, ' '); - if (strlen(item->name) != 0) { - - indx = dlg_index_wchars(item->name); + if (both) { + dlg_print_listitem(win, item->name, climit, first, selected); + first = FALSE; + } - (void) wattrset(win, selected ? tag_key_selected_attr : tag_key_attr); - (void) waddnstr(win, item->name, indx[1]); + (void) wmove(win, choice, data->item_x); + dlg_print_listitem(win, show, climit, first, selected); - if ((int) strlen(item->name) > indx[1]) { - limit = dlg_limit_columns(item->name, (item_x - check_x - 6), 1); - if (limit > 1) { - (void) wattrset(win, selected ? tag_selected_attr : tag_attr); - (void) waddnstr(win, - item->name + indx[1], - indx[limit] - indx[1]); - } - } + if (selected) { + dlg_item_help(item->help); } + (void) wattrset(win, save); +} - if (strlen(item->text) != 0) { - cols = dlg_index_columns(item->text); - limit = dlg_limit_columns(item->text, (getmaxx(win) - item_x + 1), 0); +static void +print_list(ALL_DATA * data, int choice, int scrollamt, int max_choice) +{ + int i; + int cur_y, cur_x; - if (limit > 0) { - (void) wmove(win, choice, item_x); - (void) wattrset(win, selected ? item_selected_attr : item_attr); - dlg_print_text(win, item->text, cols[limit], &attr); - } + getyx(data->dialog, cur_y, cur_x); + for (i = 0; i < max_choice; i++) { + print_item(data, + data->list, + &data->items[i + scrollamt], + data->states, + i, i == choice); } + (void) wnoutrefresh(data->list); - if (selected) { - dlg_item_help(item->help); + dlg_draw_scrollbar(data->dialog, + (long) (scrollamt), + (long) (scrollamt), + (long) (scrollamt + max_choice), + (long) (data->item_no), + data->box_x + data->check_x, + data->box_x + data->use_width, + data->box_y, + data->box_y + data->use_height + 1, + menubox_border2_attr, + menubox_border_attr); + + (void) wmove(data->dialog, cur_y, cur_x); +} + +static bool +check_hotkey(DIALOG_LISTITEM * items, int choice) +{ + bool result = FALSE; + + if (dlg_match_char(dlg_last_getc(), + (dialog_vars.no_tags + ? items[choice].text + : items[choice].name))) { + result = TRUE; } - (void) wattrset(win, save); + return result; } /* @@ -176,22 +189,26 @@ dlg_checklist(const char *title, int old_height = height; int old_width = width; #endif - int i, j, key2, found, x, y, cur_x, cur_y, box_x, box_y; + ALL_DATA all; + int i, j, key2, found, x, y, cur_x, cur_y; int key = 0, fkey; int button = dialog_state.visit_items ? -1 : dlg_default_button(); int choice = dlg_default_listitem(items); int scrollamt = 0; int max_choice; int was_mouse; - int use_height; - int use_width, name_width, text_width; + int use_width, list_width, name_width, text_width; int result = DLG_EXIT_UNKNOWN; int num_states; - WINDOW *dialog, *list; + WINDOW *dialog; char *prompt = dlg_strclone(cprompt); const char **buttons = dlg_ok_labels(); const char *widget_name; + memset(&all, 0, sizeof(all)); + all.items = items; + all.item_no = item_no; + dlg_does_output(); dlg_tab_correct_str(prompt); @@ -220,15 +237,17 @@ dlg_checklist(const char *title, retry: #endif - use_height = list_height; + all.use_height = list_height; use_width = dlg_calc_list_width(item_no, items) + 10; use_width = MAX(26, use_width); - if (use_height == 0) { + if (all.use_height == 0) { /* calculate height without items (4) */ dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, use_width); - dlg_calc_listh(&height, &use_height, item_no); + dlg_calc_listh(&height, &all.use_height, item_no); } else { - dlg_auto_size(title, prompt, &height, &width, MIN_HIGH + use_height, use_width); + dlg_auto_size(title, prompt, + &height, &width, + MIN_HIGH + all.use_height, use_width); } dlg_button_layout(buttons, &width); dlg_print_size(height, width); @@ -238,13 +257,15 @@ dlg_checklist(const char *title, if (states == 0 || strlen(states) < 2) states = " *"; num_states = (int) strlen(states); + all.states = states; - checkflag = flag; + all.checkflag = flag; x = dlg_box_x_ordinate(width); y = dlg_box_y_ordinate(height); dialog = dlg_new_window(height, width, y, x); + all.dialog = dialog; dlg_register_window(dialog, widget_name, binding); dlg_register_buttons(dialog, widget_name, buttons); @@ -257,30 +278,31 @@ dlg_checklist(const char *title, (void) wattrset(dialog, dialog_attr); dlg_print_autowrap(dialog, prompt, height, width); - list_width = width - 6; + all.use_width = width - 6; getyx(dialog, cur_y, cur_x); - box_y = cur_y + 1; - box_x = (width - list_width) / 2 - 1; + all.box_y = cur_y + 1; + all.box_x = (width - all.use_width) / 2 - 1; /* * After displaying the prompt, we know how much space we really have. * Limit the list to avoid overwriting the ok-button. */ - if (use_height + MIN_HIGH > height - cur_y) - use_height = height - MIN_HIGH - cur_y; - if (use_height <= 0) - use_height = 1; + if (all.use_height + MIN_HIGH > height - cur_y) + all.use_height = height - MIN_HIGH - cur_y; + if (all.use_height <= 0) + all.use_height = 1; - max_choice = MIN(use_height, item_no); + max_choice = MIN(all.use_height, item_no); + max_choice = MAX(max_choice, 1); /* create new window for the list */ - list = dlg_sub_window(dialog, use_height, list_width, - y + box_y + 1, x + box_x + 1); + all.list = dlg_sub_window(dialog, all.use_height, all.use_width, + y + all.box_y + 1, x + all.box_x + 1); /* draw a box around the list items */ - dlg_draw_box(dialog, box_y, box_x, - use_height + 2 * MARGIN, - list_width + 2 * MARGIN, + dlg_draw_box(dialog, all.box_y, all.box_x, + all.use_height + 2 * MARGIN, + all.use_width + 2 * MARGIN, menubox_border_attr, menubox_border2_attr); text_width = 0; @@ -295,48 +317,54 @@ dlg_checklist(const char *title, * one or both of them. If the name is no wider than 1/4 of the list, * leave it intact. */ - use_width = (list_width - 6); - if (text_width + name_width > use_width) { - int need = (int) (0.25 * use_width); - if (name_width > need) { - int want = (int) (use_width * ((double) name_width) / - (text_width + name_width)); - name_width = (want > need) ? want : need; + use_width = (all.use_width - 6); + if (dialog_vars.no_tags) { + list_width = MIN(all.use_width, text_width); + } else if (dialog_vars.no_items) { + list_width = MIN(all.use_width, name_width); + } else { + if (text_width >= 0 + && name_width >= 0 + && use_width > 0 + && text_width + name_width > use_width) { + int need = (int) (0.25 * use_width); + if (name_width > need) { + int want = (int) (use_width * ((double) name_width) / + (text_width + name_width)); + name_width = (want > need) ? want : need; + } + text_width = use_width - name_width; } - text_width = use_width - name_width; + list_width = (text_width + name_width); } - check_x = (use_width - (text_width + name_width)) / 2; - item_x = name_width + check_x + 6; + all.check_x = (use_width - list_width) / 2; + all.item_x = ((dialog_vars.no_tags + ? 0 + : (dialog_vars.no_items + ? 0 + : (2 + name_width))) + + all.check_x + 4); /* ensure we are scrolled to show the current choice */ - if (choice >= (max_choice + scrollamt)) { - scrollamt = choice - max_choice + 1; + scrollamt = MIN(scrollamt, max_choice + item_no - 1); + if (choice >= (max_choice + scrollamt - 1)) { + scrollamt = MAX(0, choice - max_choice + 1); choice = max_choice - 1; } - /* Print the list */ - for (i = 0; i < max_choice; i++) { - print_item(list, - &items[i + scrollamt], - states, - i, i == choice); - } - (void) wnoutrefresh(list); + print_list(&all, choice, scrollamt, max_choice); /* register the new window, along with its borders */ - dlg_mouse_mkbigregion(box_y + 1, box_x, use_height, list_width + 2, + dlg_mouse_mkbigregion(all.box_y + 1, all.box_x, + all.use_height, all.use_width + 2, KEY_MAX, 1, 1, 1 /* by lines */ ); - print_arrows(dialog, - box_x, box_y, - scrollamt, max_choice, item_no, use_height); - dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); dlg_trace_win(dialog); while (result == DLG_EXIT_UNKNOWN) { if (button < 0) /* --visit-items */ - wmove(dialog, box_y + choice + 1, box_x + check_x + 2); + wmove(dialog, all.box_y + choice + 1, all.box_x + all.check_x + 2); key = dlg_mouse_wgetch(dialog, &fkey); if (dlg_result_key(key, fkey, &result)) @@ -350,19 +378,8 @@ dlg_checklist(const char *title, getyx(dialog, cur_y, cur_x); i = (key - KEY_MAX); if (i < max_choice) { - /* De-highlight current item */ - print_item(list, - &items[scrollamt + choice], - states, - choice, FALSE); - /* Highlight new item */ choice = (key - KEY_MAX); - print_item(list, - &items[scrollamt + choice], - states, - choice, TRUE); - (void) wnoutrefresh(list); - (void) wmove(dialog, cur_y, cur_x); + print_list(&all, choice, scrollamt, max_choice); key = ' '; /* force the selected item to toggle */ } else { @@ -386,13 +403,15 @@ dlg_checklist(const char *title, if (next >= num_states) next = 0; - getyx(dialog, cur_y, cur_x); if (flag == FLAG_CHECK) { /* checklist? */ + getyx(dialog, cur_y, cur_x); items[current].state = next; - print_item(list, + print_item(&all, all.list, &items[scrollamt + choice], states, choice, TRUE); + (void) wnoutrefresh(all.list); + (void) wmove(dialog, cur_y, cur_x); } else { /* radiolist */ for (i = 0; i < item_no; i++) { if (i != current) { @@ -400,23 +419,19 @@ dlg_checklist(const char *title, } } if (items[current].state) { + getyx(dialog, cur_y, cur_x); items[current].state = next ? next : 1; - print_item(list, + print_item(&all, all.list, &items[current], states, choice, TRUE); + (void) wnoutrefresh(all.list); + (void) wmove(dialog, cur_y, cur_x); } else { items[current].state = 1; - for (i = 0; i < max_choice; i++) - print_item(list, - &items[scrollamt + i], - states, - i, i == choice); + print_list(&all, choice, scrollamt, max_choice); } } - (void) wnoutrefresh(list); - (void) wmove(dialog, cur_y, cur_x); - wrefresh(dialog); continue; /* wait for another key press */ } @@ -429,7 +444,7 @@ dlg_checklist(const char *title, if (!fkey) { if (button < 0 || !dialog_state.visit_items) { for (j = scrollamt + choice + 1; j < item_no; j++) { - if (dlg_match_char(dlg_last_getc(), items[j].name)) { + if (check_hotkey(items, j)) { found = TRUE; i = j - scrollamt; break; @@ -437,7 +452,7 @@ dlg_checklist(const char *title, } if (!found) { for (j = 0; j <= scrollamt + choice; j++) { - if (dlg_match_char(dlg_last_getc(), items[j].name)) { + if (check_hotkey(items, j)) { found = TRUE; i = j - scrollamt; break; @@ -507,82 +522,17 @@ dlg_checklist(const char *title, if (i != choice) { getyx(dialog, cur_y, cur_x); if (i < 0 || i >= max_choice) { -#if defined(NCURSES_VERSION_MAJOR) && NCURSES_VERSION_MAJOR < 5 - /* - * Using wscrl to assist ncurses scrolling is not needed - * in version 5.x - */ - if (i == -1) { - if (use_height > 1) { - /* De-highlight current first item */ - print_item(list, - &items[scrollamt], - states, - 0, FALSE); - scrollok(list, TRUE); - wscrl(list, -1); - scrollok(list, FALSE); - } - scrollamt--; - print_item(list, - &items[scrollamt], - states, - 0, TRUE); - } else if (i == max_choice) { - if (use_height > 1) { - /* De-highlight current last item before scrolling up */ - print_item(list, - &items[scrollamt + max_choice - 1], - states, - max_choice - 1, FALSE); - scrollok(list, TRUE); - wscrl(list, 1); - scrollok(list, FALSE); - } - scrollamt++; - print_item(list, - &items[scrollamt + max_choice - 1], - states, - max_choice - 1, TRUE); - } else -#endif - { - if (i < 0) { - scrollamt += i; - choice = 0; - } else { - choice = max_choice - 1; - scrollamt += (i - max_choice + 1); - } - for (i = 0; i < max_choice; i++) { - print_item(list, - &items[scrollamt + i], - states, - i, i == choice); - } + if (i < 0) { + scrollamt += i; + choice = 0; + } else { + choice = max_choice - 1; + scrollamt += (i - max_choice + 1); } - (void) wnoutrefresh(list); - print_arrows(dialog, - box_x, box_y, - scrollamt, max_choice, item_no, use_height); + print_list(&all, choice, scrollamt, max_choice); } else { - /* De-highlight current item */ - print_item(list, - &items[scrollamt + choice], - states, - choice, FALSE); - /* Highlight new item */ choice = i; - print_item(list, - &items[scrollamt + choice], - states, - choice, TRUE); - (void) wnoutrefresh(list); - print_arrows(dialog, - box_x, box_y, - scrollamt, max_choice, item_no, use_height); - (void) wmove(dialog, cur_y, cur_x); - wrefresh(dialog); + print_list(&all, choice, scrollamt, max_choice); } } continue; /* wait for another key press */ @@ -651,7 +601,7 @@ dialog_checklist(const char *title, int flag) { int result; - int i; + int i, j; DIALOG_LISTITEM *listitems; bool separate_output = ((flag == FLAG_CHECK) && (dialog_vars.separate_output)); @@ -661,13 +611,15 @@ dialog_checklist(const char *title, listitems = dlg_calloc(DIALOG_LISTITEM, (size_t) item_no + 1); assert_ptr(listitems, "dialog_checklist"); - for (i = 0; i < item_no; ++i) { - listitems[i].name = ItemName(i); - listitems[i].text = ItemText(i); + for (i = j = 0; i < item_no; ++i) { + listitems[i].name = items[j++]; + listitems[i].text = (dialog_vars.no_items + ? dlg_strempty() + : items[j++]); + listitems[i].state = !dlg_strcmp(items[j++], "on"); listitems[i].help = ((dialog_vars.item_help) - ? ItemHelp(i) + ? items[j++] : dlg_strempty()); - listitems[i].state = !dlg_strcmp(ItemStatus(i), "on"); } dlg_align_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no); @@ -733,6 +685,7 @@ dialog_checklist(const char *title, } } } + dlg_add_last_key(separate_output); } dlg_free_columns(&listitems[0].text, (int) sizeof(DIALOG_LISTITEM), item_no); |