From 3f33657fb721645292f798480e9e0b46d64aedf8 Mon Sep 17 00:00:00 2001
From: Mark Tiefenbruck <mark@fluxbox.org>
Date: Sun, 21 Sep 2008 22:32:02 -0700
Subject: fix active menu item when inserting or removing other items

---
 src/FbTk/Menu.cc     | 9 +++++++--
 src/FbTk/Menu.hh     | 2 +-
 src/WorkspaceMenu.cc | 6 +++---
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/FbTk/Menu.cc b/src/FbTk/Menu.cc
index 7d50302..7cb5599 100644
--- a/src/FbTk/Menu.cc
+++ b/src/FbTk/Menu.cc
@@ -219,6 +219,8 @@ int Menu::insert(MenuItem *item, int pos) {
     } else {
         menuitems.insert(menuitems.begin() + pos, item);
         fixMenuItemIndices();
+        if (m_active_index >= pos)
+            m_active_index++;
     }
     m_need_update = true; // we need to redraw the menu
     return menuitems.size();
@@ -233,7 +235,7 @@ int Menu::remove(unsigned int index) {
     if (index >= menuitems.size()) {
 #ifdef DEBUG
         cout << "Bad index (" << index << ") given to Menu::remove()"
-                  << " -- should be between 0 and " << menuitems.size()
+                  << " -- should be between 0 and " << menuitems.size()-1
                   << " inclusive." << endl;
 #endif // DEBUG
         return -1;
@@ -271,6 +273,9 @@ int Menu::remove(unsigned int index) {
     else if (static_cast<unsigned int>(m_which_sub) > index)
         m_which_sub--;
 
+    if (static_cast<unsigned int>(m_active_index) > index)
+        m_active_index--;
+
     m_need_update = true; // we need to redraw the menu
 
     return menuitems.size();
@@ -366,7 +371,7 @@ void Menu::enableTitle() {
     setTitleVisibility(true);
 }
 
-void Menu::updateMenu(int active_index) {
+void Menu::updateMenu() {
     if (m_title_vis) {
         menu.item_w = theme()->titleFont().textWidth(menu.label,
                                                     menu.label.size());
diff --git a/src/FbTk/Menu.hh b/src/FbTk/Menu.hh
index ee57a23..519a78b 100644
--- a/src/FbTk/Menu.hh
+++ b/src/FbTk/Menu.hh
@@ -109,7 +109,7 @@ public:
     void setLabel(const FbString &labelstr);
     /// move menu to x,y
     virtual void move(int x, int y);
-    virtual void updateMenu(int active_index = -1);
+    virtual void updateMenu();
     void setItemSelected(unsigned int index, bool val);
     void setItemEnabled(unsigned int index, bool val);
     void setMinimumSublevels(int m) { menu.minsub = m; }
diff --git a/src/WorkspaceMenu.cc b/src/WorkspaceMenu.cc
index c343920..f03dd3c 100644
--- a/src/WorkspaceMenu.cc
+++ b/src/WorkspaceMenu.cc
@@ -80,7 +80,7 @@ void WorkspaceMenu::workspaceInfoChanged( BScreen& screen ) {
         insert(mb_menu, workspace + IDX_AFTER_ICONS);
     }
 
-    updateMenu(-1);
+    updateMenu();
 }
 
 void WorkspaceMenu::workspaceChanged(BScreen& screen) {
@@ -89,12 +89,12 @@ void WorkspaceMenu::workspaceChanged(BScreen& screen) {
         item = find(i + IDX_AFTER_ICONS);
         if (item && item->isSelected()) {
             setItemSelected(i + IDX_AFTER_ICONS, false);
-            updateMenu(i + IDX_AFTER_ICONS);
+            updateMenu();
             break;
         }
     }
     setItemSelected(screen.currentWorkspace()->workspaceID() + IDX_AFTER_ICONS, true);
-    updateMenu(screen.currentWorkspace()->workspaceID() + IDX_AFTER_ICONS);
+    updateMenu();
 }
 
 void WorkspaceMenu::init(BScreen &screen) {
-- 
cgit v0.11.2