From c1a950e25fec427e0e1f1979e8e2b93c102edefe Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Mon, 24 Jun 2024 16:18:56 +0200 Subject: [PATCH] Navigation in menubar: wrap around when using arrow past end or start of menubar. See "Menu navigation in FLTK is different from Ubuntu X11 navigation" in fltk.general. Menubar wrapping is what macOS and gnome applications do. --- src/Fl_Menu.cxx | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Fl_Menu.cxx b/src/Fl_Menu.cxx index ed362a921..8d7e7a4c5 100644 --- a/src/Fl_Menu.cxx +++ b/src/Fl_Menu.cxx @@ -694,14 +694,17 @@ 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 selectes the first item. + // Fl_Menu_Button can generate menu=-1. This line fixes it and selects the first item. if (menu==-1) menu = 0; menuwindow &m = *(pp.p[menu]); int item = (menu == pp.menu_number) ? pp.item_number : m.selected; - while (++item < m.numitems) { - const Fl_Menu_Item* m1 = m.menu->next(item); - if (m1->activevisible()) {setitem(m1, menu, item); return 1;} + while (m.numitems >= 2) { + while (++item < m.numitems) { + const Fl_Menu_Item* m1 = m.menu->next(item); + if (m1->activevisible()) {setitem(m1, menu, item); return 1;} + } + item = -1; } return 0; } @@ -713,10 +716,12 @@ static int backward(int menu) { // previous item in menu menu if possible menustate &pp = *p; menuwindow &m = *(pp.p[menu]); int item = (menu == pp.menu_number) ? pp.item_number : m.selected; - if (item < 0) item = m.numitems; - while (--item >= 0) { - const Fl_Menu_Item* m1 = m.menu->next(item); - if (m1->activevisible()) {setitem(m1, menu, item); return 1;} + while (m.numitems >= 2) { + while (--item >= 0) { + const Fl_Menu_Item* m1 = m.menu->next(item); + if (m1->activevisible()) {setitem(m1, menu, item); return 1;} + } + item = m.numitems; } return 0; }