kconfig: make lxdialog/menubox.c more readable
Utilising a small macro for print_item made wonders for readability for this file. Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
This commit is contained in:
@@ -64,8 +64,8 @@ static int menu_width;
|
|||||||
/*
|
/*
|
||||||
* Print menu item
|
* Print menu item
|
||||||
*/
|
*/
|
||||||
static void print_item(WINDOW * win, const char *item, int choice,
|
static void do_print_item(WINDOW * win, const char *item, int choice,
|
||||||
int selected, int hotkey)
|
int selected, int hotkey)
|
||||||
{
|
{
|
||||||
int j;
|
int j;
|
||||||
char *menu_item = malloc(menu_width + 1);
|
char *menu_item = malloc(menu_width + 1);
|
||||||
@@ -99,6 +99,12 @@ static void print_item(WINDOW * win, const char *item, int choice,
|
|||||||
wrefresh(win);
|
wrefresh(win);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define print_item(index, choice, selected) \
|
||||||
|
do {\
|
||||||
|
int hotkey = (items[(index) * 2][0] != ':'); \
|
||||||
|
do_print_item(menu, items[(index) * 2 + 1], choice, selected, hotkey); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Print the scroll indicators.
|
* Print the scroll indicators.
|
||||||
*/
|
*/
|
||||||
@@ -251,9 +257,7 @@ int dialog_menu(const char *title, const char *prompt, int height, int width,
|
|||||||
|
|
||||||
/* Print the menu */
|
/* Print the menu */
|
||||||
for (i = 0; i < max_choice; i++) {
|
for (i = 0; i < max_choice; i++) {
|
||||||
print_item(menu, items[(first_item + i) * 2 + 1], i,
|
print_item(first_item + i, i, i == choice);
|
||||||
i == choice,
|
|
||||||
(items[(first_item + i) * 2][0] != ':'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wnoutrefresh(menu);
|
wnoutrefresh(menu);
|
||||||
@@ -292,34 +296,27 @@ int dialog_menu(const char *title, const char *prompt, int height, int width,
|
|||||||
key == '-' || key == '+' ||
|
key == '-' || key == '+' ||
|
||||||
key == KEY_PPAGE || key == KEY_NPAGE) {
|
key == KEY_PPAGE || key == KEY_NPAGE) {
|
||||||
/* Remove highligt of current item */
|
/* Remove highligt of current item */
|
||||||
print_item(menu, items[(scroll + choice) * 2 + 1],
|
print_item(scroll + choice, choice, FALSE);
|
||||||
choice, FALSE,
|
|
||||||
(items[(scroll + choice) * 2][0] != ':'));
|
|
||||||
|
|
||||||
if (key == KEY_UP || key == '-') {
|
if (key == KEY_UP || key == '-') {
|
||||||
if (choice < 2 && scroll) {
|
if (choice < 2 && scroll) {
|
||||||
/* Scroll menu down */
|
/* Scroll menu down */
|
||||||
do_scroll(menu, &scroll, -1);
|
do_scroll(menu, &scroll, -1);
|
||||||
|
|
||||||
print_item(menu, items[scroll * 2 + 1], 0, FALSE,
|
print_item(scroll, 0, FALSE);
|
||||||
(items[scroll * 2][0] != ':'));
|
|
||||||
} else
|
} else
|
||||||
choice = MAX(choice - 1, 0);
|
choice = MAX(choice - 1, 0);
|
||||||
|
|
||||||
} else if (key == KEY_DOWN || key == '+') {
|
} else if (key == KEY_DOWN || key == '+') {
|
||||||
|
print_item(scroll+choice, choice, FALSE);
|
||||||
print_item(menu,
|
|
||||||
items[(scroll + choice) * 2 + 1], choice, FALSE,
|
|
||||||
(items[(scroll + choice) * 2][0] != ':'));
|
|
||||||
|
|
||||||
if ((choice > max_choice - 3) &&
|
if ((choice > max_choice - 3) &&
|
||||||
(scroll + max_choice < item_no)) {
|
(scroll + max_choice < item_no)) {
|
||||||
/* Scroll menu up */
|
/* Scroll menu up */
|
||||||
do_scroll(menu, &scroll, 1);
|
do_scroll(menu, &scroll, 1);
|
||||||
|
|
||||||
print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
|
print_item(scroll+max_choice - 1,
|
||||||
max_choice - 1, FALSE,
|
max_choice - 1, FALSE);
|
||||||
(items [(scroll + max_choice - 1) * 2][0] != ':'));
|
|
||||||
} else
|
} else
|
||||||
choice = MIN(choice + 1, max_choice - 1);
|
choice = MIN(choice + 1, max_choice - 1);
|
||||||
|
|
||||||
@@ -328,8 +325,7 @@ int dialog_menu(const char *title, const char *prompt, int height, int width,
|
|||||||
for (i = 0; (i < max_choice); i++) {
|
for (i = 0; (i < max_choice); i++) {
|
||||||
if (scroll > 0) {
|
if (scroll > 0) {
|
||||||
do_scroll(menu, &scroll, -1);
|
do_scroll(menu, &scroll, -1);
|
||||||
print_item(menu, items[scroll * 2 + 1], 0, FALSE,
|
print_item(scroll, 0, FALSE);
|
||||||
(items[scroll * 2][0] != ':'));
|
|
||||||
} else {
|
} else {
|
||||||
if (choice > 0)
|
if (choice > 0)
|
||||||
choice--;
|
choice--;
|
||||||
@@ -340,9 +336,8 @@ int dialog_menu(const char *title, const char *prompt, int height, int width,
|
|||||||
for (i = 0; (i < max_choice); i++) {
|
for (i = 0; (i < max_choice); i++) {
|
||||||
if (scroll + max_choice < item_no) {
|
if (scroll + max_choice < item_no) {
|
||||||
do_scroll(menu, &scroll, 1);
|
do_scroll(menu, &scroll, 1);
|
||||||
print_item(menu, items[(scroll + max_choice - 1) * 2 + 1],
|
print_item(scroll+max_choice-1,
|
||||||
max_choice - 1, FALSE,
|
max_choice - 1, FALSE);
|
||||||
(items [(scroll + max_choice - 1) * 2][0] != ':'));
|
|
||||||
} else {
|
} else {
|
||||||
if (choice + 1 < max_choice)
|
if (choice + 1 < max_choice)
|
||||||
choice++;
|
choice++;
|
||||||
@@ -351,8 +346,7 @@ int dialog_menu(const char *title, const char *prompt, int height, int width,
|
|||||||
} else
|
} else
|
||||||
choice = i;
|
choice = i;
|
||||||
|
|
||||||
print_item(menu, items[(scroll + choice) * 2 + 1],
|
print_item(scroll + choice, choice, TRUE);
|
||||||
choice, TRUE, (items[(scroll + choice) * 2][0] != ':'));
|
|
||||||
|
|
||||||
print_arrows(dialog, item_no, scroll,
|
print_arrows(dialog, item_no, scroll,
|
||||||
box_y, box_x + ITEM_IDENT + 1, menu_height);
|
box_y, box_x + ITEM_IDENT + 1, menu_height);
|
||||||
|
Reference in New Issue
Block a user