Skip to content

Commit

Permalink
Fix inconsistencies with Tab/Backspace handling in menus (fltk#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannye authored Dec 9, 2024
1 parent ead9078 commit 4a017e8
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/Fl_Menu.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit 4a017e8

Please sign in to comment.