diff options
Diffstat (limited to 'gnu/lib/libdialog/radiolist.c')
-rw-r--r-- | gnu/lib/libdialog/radiolist.c | 113 |
1 files changed, 61 insertions, 52 deletions
diff --git a/gnu/lib/libdialog/radiolist.c b/gnu/lib/libdialog/radiolist.c index 6e8c348dfa9b1..f894e9276d308 100644 --- a/gnu/lib/libdialog/radiolist.c +++ b/gnu/lib/libdialog/radiolist.c @@ -40,9 +40,10 @@ int dialog_radiolist(unsigned char *title, unsigned char *prompt, int height, int width, int list_height, int cnt, void *it, unsigned char *result) { - int i, j, x, y, cur_x, cur_y, box_x, box_y, key = 0, button = 0, choice = 0, - l, k, scroll = 0, max_choice, *status, item_no = 0, was_on = 0; + int i, j, x, y, cur_x, cur_y, box_x, box_y, key = 0, button, choice, + l, k, scroll, max_choice, *status, item_no = 0, was_on = 0; int redraw_menu = FALSE; + int rval = 0; char okButton, cancelButton; WINDOW *dialog, *list; unsigned char **items = NULL; @@ -56,6 +57,7 @@ dialog_radiolist(unsigned char *title, unsigned char *prompt, int height, int wi } draw: + button = choice = scroll = 0; /* Previous calling syntax, e.g. just a list of strings? */ if (cnt >= 0) { items = it; @@ -98,30 +100,30 @@ draw: item_x = 0; /* Find length of longest item in order to center radiolist */ for (i = 0; i < item_no; i++) { - l = strlen(items[i*3]); + l = strlen(items[i * 3]); for (j = 0; j < item_no; j++) { - k = strlen(items[j*3 + 1]); + k = strlen(items[j * 3 + 1]); check_x = MAX(check_x, l + k + 6); } item_x = MAX(item_x, l); } if (height < 0) - height = strheight(prompt)+list_height+4+2; + height = strheight(prompt) + list_height + 4 + 2; if (width < 0) { i = strwidth(prompt); j = ((title != NULL) ? strwidth(title) : 0); - width = MAX(i,j); - width = MAX(width,check_x+4)+4; + width = MAX(i, j); + width = MAX(width, check_x + 4) + 4; } - width = MAX(width,24); + width = MAX(width, 24); if (width > COLS) width = COLS; if (height > LINES) height = LINES; /* center dialog box on screen */ - x = DialogX ? DialogX : (COLS - width)/2; - y = DialogY ? DialogY : (LINES - height)/2; + x = DialogX ? DialogX : (COLS - width) / 2; + y = DialogY ? DialogY : (LINES - height) / 2; #ifdef HAVE_NCURSES if (use_shadow) @@ -130,45 +132,46 @@ draw: dialog = newwin(height, width, y, x); if (dialog == NULL) { endwin(); - fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height,width,y,x); + fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n", height, width, y, x); return -1; } keypad(dialog, TRUE); draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); wattrset(dialog, border_attr); - wmove(dialog, height-3, 0); + wmove(dialog, height - 3, 0); waddch(dialog, ACS_LTEE); - for (i = 0; i < width-2; i++) + for (i = 0; i < width - 2; i++) waddch(dialog, ACS_HLINE); wattrset(dialog, dialog_attr); waddch(dialog, ACS_RTEE); - wmove(dialog, height-2, 1); - for (i = 0; i < width-2; i++) + wmove(dialog, height - 2, 1); + for (i = 0; i < width - 2; i++) waddch(dialog, ' '); if (title != NULL) { wattrset(dialog, title_attr); - wmove(dialog, 0, (width - strlen(title))/2 - 1); + wmove(dialog, 0, (width - strlen(title)) / 2 - 1); waddch(dialog, ' '); waddstr(dialog, title); waddch(dialog, ' '); } wattrset(dialog, dialog_attr); wmove(dialog, 1, 2); - print_autowrap(dialog, prompt, height-1, width-2, width, 1, 2, TRUE, FALSE); + print_autowrap(dialog, prompt, height - 1, width - 2, width, 1, 2, TRUE, FALSE); - list_width = width-6; + list_width = width - 6; getyx(dialog, cur_y, cur_x); box_y = cur_y + 1; - box_x = (width - list_width)/2 - 1; + box_x = (width - list_width) / 2 - 1; /* create new window for the list */ list = subwin(dialog, list_height, list_width, y + box_y + 1, x + box_x + 1); if (list == NULL) { delwin(dialog); endwin(); - fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", list_height,list_width,y+box_y+1,x+box_x+1); + fprintf(stderr, "\nsubwin(dialog,%d,%d,%d,%d) failed, maybe wrong dims\n", list_height, list_width, + y + box_y + 1,x + box_x + 1); return -1; } keypad(list, TRUE); @@ -181,14 +184,14 @@ draw: /* Print the list */ for (i = 0; i < max_choice; i++) - print_item(list, items[i*3], items[i*3 + 1], status[i], i, i == choice, DREF(ditems, i)); + print_item(list, items[i * 3], items[i * 3 + 1], status[i], i, i == choice, DREF(ditems, i)); wnoutrefresh(list); print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y); display_helpline(dialog, height-1, width); - x = width/2-11; - y = height-2; + x = width/ 2 - 11; + y = height - 2; if (ditems && result) { cancelButton = toupper(ditems[CANCEL_BUTTON].prompt[0]); print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, @@ -203,8 +206,8 @@ draw: okButton = 'O'; print_button(dialog, " OK ", y, x, TRUE); } + wrefresh(dialog); - while (key != ESC) { key = wgetch(dialog); @@ -233,12 +236,13 @@ draw: } } } - delwin(list); - delwin(dialog); - return 0; + rval = 0; + key = ESC; + break; } + /* Shortcut to cancel */ - else if (toupper(key) == cancelButton) { + if (toupper(key) == cancelButton) { if (ditems && result && ditems[CANCEL_BUTTON].fire) { int st; WINDOW *save; @@ -251,9 +255,9 @@ draw: } delwin(save); } - delwin(list); - delwin(dialog); - return 1; + rval = 1; + key = ESC; + break; } /* Check if key pressed matches first character of any item tag in list */ @@ -262,23 +266,25 @@ draw: break; if (i < max_choice || (key >= '1' && key <= MIN('9', '0' + max_choice)) || - key == KEY_UP || key == KEY_DOWN || key == ' ' || key == '+' || key == '-' || key == '' || key == '') { + KEY_IS_UP(key) || KEY_IS_DOWN(key) || key == ' ') { if (key >= '1' && key <= MIN('9', '0' + max_choice)) i = key - '1'; - else if (key == KEY_UP || key == '-' || key == '') { + else if (KEY_IS_UP(key)) { if (!choice) { if (scroll) { /* Scroll list down */ getyx(dialog, cur_y, cur_x); /* Save cursor position */ if (list_height > 1) { /* De-highlight current first item before scrolling down */ - print_item(list, items[scroll*3], items[scroll*3 + 1], status[scroll], 0, FALSE, DREF(ditems, scroll)); + print_item(list, items[scroll*3], items[scroll*3 + 1], status[scroll], 0, + FALSE, DREF(ditems, scroll)); scrollok(list, TRUE); wscrl(list, -1); scrollok(list, FALSE); } scroll--; - print_item(list, items[scroll*3], items[scroll*3 + 1], status[scroll], 0, TRUE, DREF(ditems, scroll)); + print_item(list, items[scroll*3], items[scroll*3 + 1], status[scroll], 0, + TRUE, DREF(ditems, scroll)); wnoutrefresh(list); print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y); wrefresh(dialog); @@ -288,22 +294,26 @@ draw: else i = choice - 1; } - else if (key == KEY_DOWN || key == '+' || key == '') { + else if (KEY_IS_DOWN(key)) { if (choice == max_choice - 1) { if (scroll + choice < item_no - 1) { /* Scroll list up */ getyx(dialog, cur_y, cur_x); /* Save cursor position */ if (list_height > 1) { /* De-highlight current last item before scrolling up */ - print_item(list, items[(scroll + max_choice - 1) * 3], items[(scroll + max_choice - 1) * 3 + 1], - status[scroll + max_choice - 1], max_choice - 1, FALSE, DREF(ditems, scroll + max_choice - 1)); + print_item(list, items[(scroll + max_choice - 1) * 3], + items[(scroll + max_choice - 1) * 3 + 1], + status[scroll + max_choice - 1], max_choice - 1, + FALSE, DREF(ditems, scroll + max_choice - 1)); scrollok(list, TRUE); scroll(list); scrollok(list, FALSE); } scroll++; - print_item(list, items[(scroll + max_choice - 1) * 3], items[(scroll + max_choice - 1) * 3 + 1], - status[scroll + max_choice - 1], max_choice - 1, TRUE, DREF(ditems, scroll + max_choice - 1)); + print_item(list, items[(scroll + max_choice - 1) * 3], + items[(scroll + max_choice - 1) * 3 + 1], + status[scroll + max_choice - 1], max_choice - 1, + TRUE, DREF(ditems, scroll + max_choice - 1)); wnoutrefresh(list); print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y); wrefresh(dialog); @@ -327,7 +337,9 @@ draw: touchwin(save); wrefresh(save); } + delwin(save); if (st & DITEM_REDRAW) { + wclear(list); for (i = 0; i < item_no; i++) status[i] = ditems[i].checked ? ditems[i].checked(&ditems[i]) : FALSE; @@ -340,7 +352,6 @@ draw: wmove(dialog, cur_y, cur_x); /* Restore cursor to previous position */ wrefresh(dialog); } - delwin(save); if (st & DITEM_LEAVE_MENU) { /* Allow a fire action to take us out of the menu */ key = ESC; @@ -363,8 +374,8 @@ draw: } getyx(dialog, cur_y, cur_x); /* Save cursor position */ for (i = 0; i < max_choice; i++) - print_item(list, items[(scroll + i) * 3], items[(scroll + i) * 3 + 1], status[scroll + i], i, i == choice, - DREF(ditems, scroll + i)); + print_item(list, items[(scroll + i) * 3], items[(scroll + i) * 3 + 1], + status[scroll + i], i, i == choice, DREF(ditems, scroll + i)); wnoutrefresh(list); wmove(dialog, cur_y, cur_x); /* Restore cursor to previous position */ wrefresh(dialog); @@ -464,24 +475,24 @@ draw: } } } - delwin(list); - delwin(dialog); - return button; + rval = button; + key = ESC; break; case ESC: + rval = -1; break; case KEY_F(1): case '?': display_helpfile(); - break; + break; } if (redraw_menu) { for (i = 0; i < max_choice; i++) - print_item(list, items[(scroll + i) * 3], items[(scroll + i) * 3 + 1], status[scroll + i], i, i == choice, - DREF(ditems, scroll + i)); + print_item(list, items[(scroll + i) * 3], items[(scroll + i) * 3 + 1], status[scroll + i], + i, i == choice, DREF(ditems, scroll + i)); wnoutrefresh(list); print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y); wrefresh(dialog); @@ -491,10 +502,8 @@ draw: delwin(list); delwin(dialog); - return -1; /* ESC pressed */ + return rval; /* ESC pressed */ } -/* End of dialog_radiolist() */ - /* * Print list item |