Blame 4Pane-HEAD-0001-When-getting-a-pointer-to-a-menubar-menu-don-t-searc.patch

adb72cf
From 5b4ce2be04f00e001bad6c4d6c59ff3791b1125c Mon Sep 17 00:00:00 2001
adb72cf
From: dghart <dghart@users.sourceforge.net>
adb72cf
Date: Wed, 10 Dec 2014 20:42:12 +0000
adb72cf
Subject: [PATCH] When getting a pointer to a menubar menu, don't search by
adb72cf
 title
adb72cf
adb72cf
Using wxMenuBar::FindMenu to find the index of a menu isn't as mnemonic-safe as it should be. There are two problems:
adb72cf
1) If the search string isn't localised, it will fail if there's a translation.
adb72cf
2) Even if it is, there's a potential problem with mnemonic location, as translators are allowed to use a non-default letter.
adb72cf
adb72cf
This _shouldn't_ matter as wx removes the mnemonic before matching. However it seems that in Japanese, the mnemonic is appended
adb72cf
(e.g. Bookmarks(&B))
adb72cf
and unsurprisingly wxStripMenuCodes() can't cope with this. So store the position while loading the menubar; which isn't elegant, but it is safe.
adb72cf
---
adb72cf
 Accelerators.cpp |  4 ++++
adb72cf
 Bookmarks.cpp    | 13 +++++++++----
adb72cf
 Bookmarks.h      |  5 ++++-
adb72cf
 Tools.cpp        | 14 ++++++++------
adb72cf
 Tools.h          |  3 +++
adb72cf
 5 files changed, 28 insertions(+), 11 deletions(-)
adb72cf
adb72cf
diff --git a/Accelerators.cpp b/Accelerators.cpp
adb72cf
index eb75627..d470c91 100644
adb72cf
--- a/Accelerators.cpp
adb72cf
+++ b/Accelerators.cpp
adb72cf
@@ -16,6 +16,7 @@
adb72cf
 
adb72cf
 #include "Externs.h"
adb72cf
 #include "Configure.h"
adb72cf
+#include "Bookmarks.h"
adb72cf
 #include "Accelerators.h"
adb72cf
 
adb72cf
 
adb72cf
@@ -490,6 +491,9 @@ subitemslistarray[0] = columnitems; subitemslistcount[0] = sizeof(columnitems)/p
adb72cf
 
adb72cf
                                     // Make arrays for menus & for submenus
adb72cf
 const wxString menutitles[] = { _("&File"), _("&Edit"), _("&View"), _("&Tabs"), _("&Bookmarks"), _("&Archive"), _("&Mount"), _("Too&ls"), _("&Options"), _("&Help") };
adb72cf
+Bookmarks::SetMenuIndex(4); // *** remember to change these if their position changes! ***
adb72cf
+LaunchMiscTools::SetMenuIndex(7);
adb72cf
+
adb72cf
 const wxString submenutitles[] = { _("&Columns to Display"), _("&Load a Tab Template") };
adb72cf
 int submenID[2];  submenID[0] = SHCUT_FINISH + 1; submenID[1] = LoadTabTemplateMenu;  // "Columns to Display"  hasn't a set id, so use SHCUT_FINISH+1
adb72cf
 wxMenu* menuarray[ menuno ]; wxMenu* submenuarray[ submenuno ];
adb72cf
diff --git a/Bookmarks.cpp b/Bookmarks.cpp
adb72cf
index e0e2e7d..65ae21c 100644
adb72cf
--- a/Bookmarks.cpp
adb72cf
+++ b/Bookmarks.cpp
adb72cf
@@ -318,6 +318,8 @@ END_EVENT_TABLE()
adb72cf
 
adb72cf
 //--------------------------------------------------------------------------------------------------------------------
adb72cf
 
adb72cf
+int Bookmarks::m_menuindex = -1;
adb72cf
+
adb72cf
 Bookmarks::Bookmarks()
adb72cf
 {
adb72cf
 LoadBookmarks();
adb72cf
@@ -336,8 +338,9 @@ MenuStruct.ClearData();                           // Make sure the arrays are em
adb72cf
 bookmarkID = ID_FIRST_BOOKMARK;                   // Similarly reset bookmarkID
adb72cf
 MenuStruct.ID = GetNextID();                      //   & use it indirectly to id MenuStruct
adb72cf
 
adb72cf
-wxMenu* menu = MenuBar->GetMenu(MenuBar->FindMenu(_("Bookmarks")));  // Find the menu for the group
adb72cf
-if (menu==NULL)  { wxLogError(_("Couldn't find menu!?")); return; }
adb72cf
+wxCHECK_RET(m_menuindex > wxNOT_FOUND, wxT("Bookmarks menu index not set")); // Should have been set when the bar was loaded
adb72cf
+wxMenu* menu = MenuBar->GetMenu(m_menuindex);     // Find the menu for the group
adb72cf
+if (!menu)  { wxLogError(_("Couldn't find menu!?")); return; }
adb72cf
 
adb72cf
 MenuStruct.pathname =  wxT("/Bookmarks");
adb72cf
 MenuStruct.FullLabel.Empty();                     // Needed for re-entry during Save/Load
adb72cf
@@ -435,8 +438,10 @@ config->Flush();
adb72cf
 
adb72cf
 wxMenuBar* MenuBar = MyFrame::mainframe->GetMenuBar();                  // We now have to destroy the menu structure, so it can be born again
adb72cf
 if (MenuBar==NULL)  { wxLogError(_("Couldn't load Menubar!?")); return; }
adb72cf
-  
adb72cf
-wxMenu* mainmenu = MenuBar->GetMenu(MenuBar->FindMenu(_("Bookmarks"))); // Find the menu for the group
adb72cf
+
adb72cf
+wxCHECK_RET(m_menuindex > wxNOT_FOUND, wxT("Bookmarks menu index not set")); // Should have been set when the bar was loaded
adb72cf
+wxMenu* mainmenu = MenuBar->GetMenu(m_menuindex); // Find the menu for the group
adb72cf
+if (!mainmenu)  { wxLogError(_("Couldn't find menu!?")); return; }
adb72cf
 
adb72cf
 for (size_t count=mainmenu->GetMenuItemCount(); count > 3 ; --count)    // Skip the 1st 3 items, they're Add, Manage & a Separator
adb72cf
   mainmenu->Destroy(mainmenu->GetMenuItems().Item(count-1)->GetData()); // Destroy all the others, including submenus
adb72cf
diff --git a/Bookmarks.h b/Bookmarks.h
adb72cf
index b476a4d..a52109c 100644
adb72cf
--- a/Bookmarks.h
adb72cf
+++ b/Bookmarks.h
adb72cf
@@ -173,6 +173,8 @@ bool Cut(wxTreeItemId item);
adb72cf
 bool Paste(wxTreeItemId item, bool NoDupCheck=false, bool duplicating=false);
adb72cf
 bool Copy(wxTreeItemId item);
adb72cf
 
adb72cf
+static void SetMenuIndex(int index) { m_menuindex = index; }
adb72cf
+
adb72cf
 wxDialog* adddlg;                      // These 2 ptrs are used to access their dialogs from other methods
adb72cf
 MyBookmarkDialog* mydlg;
adb72cf
 BMClipboard bmclip;                    // Stores the TreeItemData & structs for pasting
adb72cf
@@ -182,7 +184,8 @@ wxConfigBase* config;
adb72cf
 MyBmTree* tree;                        // The treectrl used in ManageBookmarks
adb72cf
 bool m_altered;                        // Do we have anything to save?
adb72cf
 
adb72cf
-unsigned int bookmarkID;               // Actually it ID's folders & separators too
adb72cf
+static int m_menuindex;                // Passed to wxMenuBar::GetMenu so we can ID the menu without using its label
adb72cf
+unsigned int bookmarkID;               // Actually it ID.s folders & separators too
adb72cf
 unsigned int GetNextID(){ if (bookmarkID == ID__LAST_BOOKMARK) bookmarkID=ID_FIRST_BOOKMARK;  // Recycle
adb72cf
                           return bookmarkID++; }                                              // Return next vacant bookmarkID
adb72cf
 
adb72cf
diff --git a/Tools.cpp b/Tools.cpp
adb72cf
index 60b5879..f5ec1fe 100644
adb72cf
--- a/Tools.cpp
adb72cf
+++ b/Tools.cpp
adb72cf
@@ -2678,6 +2678,8 @@ BriefLogStatus bls(msg + msgA + msgB);
adb72cf
 }
adb72cf
 //-----------------------------------------------------------------------------------------------------------------------
adb72cf
 
adb72cf
+int LaunchMiscTools::m_menuindex = -1;
adb72cf
+
adb72cf
 void LaunchMiscTools::ClearData()
adb72cf
 {
adb72cf
 for (int n = (int)ToolArray.GetCount();   n > 0; --n)  { UserDefinedTool* item = ToolArray.Item(n-1); delete item; ToolArray.RemoveAt(n-1); }
adb72cf
@@ -2691,9 +2693,9 @@ LoadArrays();
adb72cf
 if (!FolderArray.GetCount()) return;                // No data, not event the "Run a Program" menu
adb72cf
 
adb72cf
 if (tools == NULL)                                  // If so, we're loading the menubar menu, so find it
adb72cf
-  { int id = MyFrame::mainframe->GetMenuBar()->FindMenu(_("Too&ls")); // Find the Tools menu id
adb72cf
-    if (id == wxNOT_FOUND) return;
adb72cf
-    tools = MyFrame::mainframe->GetMenuBar()->GetMenu(id);           // Get ptr to the menu itself
adb72cf
+  { wxCHECK_RET(m_menuindex > wxNOT_FOUND, wxT("Tools menu index not set")); // Should have been set when the bar was loaded
adb72cf
+    tools = MyFrame::mainframe->GetMenuBar()->GetMenu(m_menuindex);
adb72cf
+    wxCHECK_RET(tools, wxT("Couldn't find the Tools menu"));
adb72cf
     static bool alreadyloaded=false; if (!alreadyloaded) { tools->AppendSeparator(); alreadyloaded=true; }  // We don't want multiple separators due to reloads
adb72cf
   }
adb72cf
 
adb72cf
@@ -2777,9 +2779,9 @@ config->DeleteGroup(wxT("/Tools/Launch"));           // Delete current info (oth
adb72cf
 Save(wxT("/Tools/Launch"), config);                  // Recursively save
adb72cf
 config->Flush();
adb72cf
 
adb72cf
-int id = MyFrame::mainframe->GetMenuBar()->FindMenu(_("Tools")); // Find the id of the Tools menu proper
adb72cf
-if (id == wxNOT_FOUND) return;
adb72cf
-wxMenu* tools = MyFrame::mainframe->GetMenuBar()->GetMenu(id);   //   & hence the menu itself
adb72cf
+wxCHECK_RET(m_menuindex > wxNOT_FOUND, wxT("Tools menu index not set")); // Should have been set when the bar was loaded
adb72cf
+wxMenu* tools = MyFrame::mainframe->GetMenuBar()->GetMenu(m_menuindex);
adb72cf
+wxCHECK_RET(tools, wxT("Couldn't find the Tools menu"));
adb72cf
 
adb72cf
 int sm = tools->FindItem(FolderArray[0]->Label);     // Find the id menu of the relevant submenu in the Tools menu
adb72cf
 if (sm == wxNOT_FOUND) return;
adb72cf
diff --git a/Tools.h b/Tools.h
adb72cf
index d6a7f58..6554a82 100644
adb72cf
--- a/Tools.h
adb72cf
+++ b/Tools.h
adb72cf
@@ -494,6 +494,8 @@ ArrayOfUserDefinedTools* GetToolarray(){ return &ToolArray; }
adb72cf
 ArrayOfToolFolders* GetFolderArray(){ return &FolderArray; }
adb72cf
 size_t GetLastCommand(){ return LastCommandNo; }
adb72cf
 
adb72cf
+static void SetMenuIndex(int index) { m_menuindex = index; }
adb72cf
+
adb72cf
 protected:
adb72cf
 void Load(wxString name);                     // Load (sub)menu 'name' from config
adb72cf
 void ClearData();
adb72cf
@@ -505,6 +507,7 @@ bool SetCorrectPaneptrs(const wxChar* &pc, MyGenericDirCtrl* &first, MyGenericDi
adb72cf
 ArrayOfUserDefinedTools ToolArray;
adb72cf
 ArrayOfToolFolders FolderArray;
adb72cf
 int EventId;                                  // Holds the first spare ID, from SHCUT_TOOLS_LAUNCH to SHCUT_TOOLS_LAUNCH_LAST-1
adb72cf
+static int m_menuindex;                       // Passed to wxMenuBar::GetMenu so we can ID the menu without using its label
adb72cf
 size_t EntriesIndex;
adb72cf
 size_t FolderIndex;
adb72cf
 size_t LastCommandNo;                         // Holds the last tool used, so it can be repeated
adb72cf
-- 
adb72cf
2.1.0
adb72cf