diff --git a/src/Fl_Menu.cxx b/src/Fl_Menu.cxx index 7350f4af5e..2a5bcd31ee 100644 --- a/src/Fl_Menu.cxx +++ b/src/Fl_Menu.cxx @@ -698,40 +698,44 @@ static void setitem(int m, int n) { } static int forward(int menu) { // go to next item in menu menu if possible - menustate &pp = *p; - // Fl_Menu_Button can generate menu=-1. This line fixes it and selects the first item. - if (menu==-1) + // `menu` is -1 if no item is currently selected, so use the first menu + if (menu < 0) menu = 0; + menustate &pp = *p; menuwindow &m = *(pp.p[menu]); int item = (menu == pp.menu_number) ? pp.item_number : m.selected; - int item0 = item; + bool wrapped = false; do { while (++item < m.numitems) { const Fl_Menu_Item* m1 = m.menu->next(item); if (m1->activevisible()) {setitem(m1, menu, item); return 1;} } - if (item0 == -1) break; + if (wrapped) break; item = -1; + wrapped = true; } - while (pp.menubar && Fl::event_key() == FL_Right); + while (Fl::event_key() != FL_Down); return 0; } static int backward(int menu) { // previous item in menu menu if possible - // `menu` is -1 if no item is currently selected, we return 0 - if (menu<0) - return 0; + // `menu` is -1 if no item is currently selected, so use the first menu + if (menu < 0) + menu = 0; menustate &pp = *p; menuwindow &m = *(pp.p[menu]); int item = (menu == pp.menu_number) ? pp.item_number : m.selected; + bool wrapped = false; do { while (--item >= 0) { const Fl_Menu_Item* m1 = m.menu->next(item); if (m1->activevisible()) {setitem(m1, menu, item); return 1;} } + if (wrapped) break; item = m.numitems; + wrapped = true; } - while (pp.menubar && Fl::event_key() == FL_Left); + while (Fl::event_key() != FL_Up); return 0; } @@ -799,10 +803,7 @@ int menuwindow::handle_part1(int e) { switch (Fl::event_key()) { case FL_BackSpace: BACKTAB: - if (!backward(pp.menu_number)) { - pp.item_number = -1; - backward(pp.menu_number); - } + backward(pp.menu_number); return 1; case FL_Up: if (pp.menubar && pp.menu_number == 0) { @@ -815,12 +816,10 @@ int menuwindow::handle_part1(int e) { return 1; case FL_Tab: if (Fl::event_shift()) goto BACKTAB; + if (pp.menubar && pp.menu_number == 0) goto RIGHT; case FL_Down: if (pp.menu_number || !pp.menubar) { - if (!forward(pp.menu_number) && Fl::event_key()==FL_Tab) { - pp.item_number = -1; - forward(pp.menu_number); - } + forward(pp.menu_number); } else if (pp.menu_number < pp.nummenus-1) { forward(pp.menu_number+1); }