diff options
author | eric <eric@FreeBSD.org> | 2001-07-18 05:21:37 +0000 |
---|---|---|
committer | eric <eric@FreeBSD.org> | 2001-07-18 05:21:37 +0000 |
commit | 8add8ad5e6825c9194d41c0c0834752ab813670e (patch) | |
tree | 45df167134c7d6b83d8a366ba206a62d45e7d802 /gnu/lib/libdialog/radiolist.c | |
parent | d329620fddc96c45ac2ce6cd2a74189d6ab7e106 (diff) | |
download | FreeBSD-src-8add8ad5e6825c9194d41c0c0834752ab813670e.zip FreeBSD-src-8add8ad5e6825c9194d41c0c0834752ab813670e.tar.gz |
Improve the interface provided by libdialog. Move a cursor around over
the components and trigger actions based on its position. This reduces
the need to remember the functions of various keys, and makes the
interface more consistant across library.
~
Diffstat (limited to 'gnu/lib/libdialog/radiolist.c')
-rw-r--r-- | gnu/lib/libdialog/radiolist.c | 161 |
1 files changed, 119 insertions, 42 deletions
diff --git a/gnu/lib/libdialog/radiolist.c b/gnu/lib/libdialog/radiolist.c index 1dec89e..e4e9372 100644 --- a/gnu/lib/libdialog/radiolist.c +++ b/gnu/lib/libdialog/radiolist.c @@ -22,7 +22,8 @@ */ #ifndef lint -static const char rcsid[] = "$FreeBSD$"; +static const char rcsid[] = + "$FreeBSD$"; #endif #include <dialog.h> @@ -43,10 +44,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, choice, - l, k, scroll, max_choice, *status, item_no = 0, was_on = 0; + int i, j, x, y, cur_x, cur_y, old_x, old_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; + int rval = 0, onlist = 1, ok_space, cancel_space; char okButton, cancelButton; WINDOW *dialog, *list; unsigned char **items = NULL; @@ -209,8 +210,10 @@ draw: okButton = 'O'; print_button(dialog, " OK ", y, x, TRUE); } + wnoutrefresh(dialog); + wmove(list, choice, check_x+1); + wrefresh(list); - wrefresh(dialog); while (key != ESC) { key = wgetch(dialog); @@ -267,8 +270,30 @@ draw: for (i = 0; i < max_choice; i++) if (key != ' ' && toupper(key) == toupper(items[(scroll + i) * 3][0])) break; + if (i < max_choice || (key >= '1' && key <= MIN('9', '0' + max_choice)) || - KEY_IS_UP(key) || KEY_IS_DOWN(key) || key == ' ') { + KEY_IS_UP(key) || KEY_IS_DOWN(key) || ((key == ' ' || key == '\r' || key == '\n') && onlist == 1)) { + + /* if moving from buttons to the list, reset and redraw buttons */ + if (!onlist) { + onlist = 1; + button = 0; + + if (ditems && result ) { + print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, + ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : button); + print_button(dialog, ditems[OK_BUTTON].prompt, y, x, + ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button); + } + else { + print_button(dialog, "Cancel", y, x + 14, button); + print_button(dialog, " OK ", y, x, !button); + } + } + wmove(list, choice, check_x+1); + wnoutrefresh(dialog); + wrefresh(list); + if (key >= '1' && key <= MIN('9', '0' + max_choice)) i = key - '1'; else if (KEY_IS_UP(key)) { @@ -325,7 +350,8 @@ draw: else i = choice + 1; } - else if (key == ' ') { /* Toggle item status */ + else if ((key == ' ' || key == '\r' || key == '\n') && onlist) { /* Toggle item status */ + getyx(list, old_y, old_x); /* Save cursor position */ if (status[scroll + choice]) continue; else if (ditems) { @@ -350,9 +376,8 @@ draw: 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); +/* wmove(list, old_y, old_x);*/ /* Restore cursor to previous position */ +/* wrefresh(list); */ } if (st & DITEM_LEAVE_MENU) { /* Allow a fire action to take us out of the menu */ @@ -374,28 +399,24 @@ draw: status[i] = 0; status[scroll + choice] = TRUE; } - 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)); - wnoutrefresh(list); - wmove(dialog, cur_y, cur_x); /* Restore cursor to previous position */ - wrefresh(dialog); + wmove(list, choice, check_x+1); /* Restore cursor position */ + wrefresh(list); continue; /* wait for another key press */ } if (i != choice) { /* De-highlight current item */ - getyx(dialog, cur_y, cur_x); /* Save cursor position */ print_item(list, items[(scroll + choice) * 3], items[(scroll + choice) * 3 +1], status[scroll + choice], choice, FALSE, DREF(ditems, scroll + choice)); /* Highlight new item */ choice = i; print_item(list, items[(scroll + choice) * 3], items[(scroll + choice) * 3 + 1], status[scroll + choice], choice, TRUE, DREF(ditems, scroll + choice)); - wnoutrefresh(list); - wmove(dialog, cur_y, cur_x); /* Restore cursor to previous position */ - wrefresh(dialog); + wmove(list, choice, check_x+1); /* Restore cursor position */ + wrefresh(list); } continue; /* wait for another key press */ } @@ -407,6 +428,10 @@ draw: else scroll = 0; redraw_menu = TRUE; + if (!onlist) { + onlist = 1; + button = 0; + } break; case KEY_NPAGE: @@ -418,12 +443,17 @@ draw: else scroll += list_height; redraw_menu = TRUE; + if (!onlist) { + onlist = 1; + button = 0; + } break; case KEY_HOME: scroll = 0; choice = 0; redraw_menu = TRUE; + onlist = 1; break; case KEY_END: @@ -432,54 +462,86 @@ draw: scroll = 0; choice = max_choice - 1; redraw_menu = TRUE; + onlist = 1; break; - case KEY_BTAB: case TAB: + case KEY_BTAB: + /* move to next component */ + if (onlist) { /* on list, next is ok button */ + onlist = 0; + if (ditems && result) + ok_space = 1; + else + ok_space = 3; + wmove(dialog, y, x + ok_space); + wrefresh(dialog); + break; + } + else if (button) { /* on cancel button, next is list */ + button = 0; + onlist = 1; + redraw_menu = TRUE; + break; + } + /* on ok button, next is cancel button, same as left/right case */ + case KEY_LEFT: case KEY_RIGHT: + onlist = 0; button = !button; if (ditems && result) { print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : button); print_button(dialog, ditems[OK_BUTTON].prompt, y, x, ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button); + ok_space = 1; + cancel_space = strlen(ditems[OK_BUTTON].prompt) + 6; } else { print_button(dialog, "Cancel", y, x + 14, button); print_button(dialog, " OK ", y, x, !button); + ok_space = 3; + cancel_space = 15; } + if (button) + wmove(dialog, y, x + cancel_space); + else + wmove(dialog, y, x + ok_space); wrefresh(dialog); break; - + + case ' ': case '\r': case '\n': - if (ditems) { - if (result && ditems[button ? CANCEL_BUTTON : OK_BUTTON].fire) { - int st; - WINDOW *save; + if (!onlist) { + if (ditems) { + if (result && ditems[button ? CANCEL_BUTTON : OK_BUTTON].fire) { + int st; + WINDOW *save; - save = dupwin(newscr); - st = ditems[button ? CANCEL_BUTTON : OK_BUTTON].fire(&ditems[button ? CANCEL_BUTTON : OK_BUTTON]); - if (st & DITEM_RESTORE) { - touchwin(save); - wrefresh(save); + save = dupwin(newscr); + st = ditems[button ? CANCEL_BUTTON : OK_BUTTON].fire(&ditems[button ? CANCEL_BUTTON : OK_BUTTON]); + if (st & DITEM_RESTORE) { + touchwin(save); + wrefresh(save); + } + delwin(save); } - delwin(save); } - } - else if (result) { - *result = '\0'; - for (i = 0; i < item_no; i++) { - if (status[i]) { - strcpy(result, items[i*3]); - break; + else if (result) { + *result = '\0'; + for (i = 0; i < item_no; i++) { + if (status[i]) { + strcpy(result, items[i*3]); + break; + } } } + rval = button; + key = ESC; + break; } - rval = button; - key = ESC; - break; case ESC: rval = -1; @@ -492,12 +554,27 @@ draw: } if (redraw_menu) { + getyx(list, old_y, old_x); + wclear(list); 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)); - wnoutrefresh(list); print_arrows(dialog, scroll, list_height, item_no, box_x, box_y, check_x + 4, cur_x, cur_y); - wrefresh(dialog); + + /* redraw buttons to fix highlighting */ + if (ditems && result) { + print_button(dialog, ditems[CANCEL_BUTTON].prompt, y, x + strlen(ditems[OK_BUTTON].prompt) + 5, + ditems[CANCEL_BUTTON].checked ? ditems[CANCEL_BUTTON].checked(&ditems[CANCEL_BUTTON]) : button); + print_button(dialog, ditems[OK_BUTTON].prompt, y, x, + ditems[OK_BUTTON].checked ? ditems[OK_BUTTON].checked(&ditems[OK_BUTTON]) : !button); + } + else { + print_button(dialog, "Cancel", y, x + 14, button); + print_button(dialog, " OK ", y, x, !button); + } + wnoutrefresh(dialog); + wmove(list, old_y, old_x); + wrefresh(list); redraw_menu = FALSE; } } |