From 0f29501b663574dc6f5f43dfb8a7f16fb5a8e4ea Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Thu, 1 Dec 2011 06:57:44 +0000 Subject: [PATCH] Fix crash when appending menuitem after removing it from another menu. Also remove unused return value from GtkAppend git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@69878 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/gtk/menu.h | 4 +-- src/gtk/menu.cpp | 68 ++++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/include/wx/gtk/menu.h b/include/wx/gtk/menu.h index 943dacce35..4f5cf6f187 100644 --- a/include/wx/gtk/menu.h +++ b/include/wx/gtk/menu.h @@ -51,7 +51,7 @@ public: private: // common part of Append and Insert - bool GtkAppend(wxMenu *menu, const wxString& title, int pos=-1); + void GtkAppend(wxMenu* menu, const wxString& title, int pos = -1); void Init(size_t n, wxMenu *menus[], const wxString titles[], long style); @@ -99,7 +99,7 @@ private: void Init(); // common part of Append (if pos == -1) and Insert - bool GtkAppend(wxMenuItem *item, int pos=-1); + void GtkAppend(wxMenuItem* item, int pos = -1); DECLARE_DYNAMIC_CLASS(wxMenu) diff --git a/src/gtk/menu.cpp b/src/gtk/menu.cpp index 3c99374528..ae2d4df832 100644 --- a/src/gtk/menu.cpp +++ b/src/gtk/menu.cpp @@ -230,13 +230,15 @@ void wxMenuBar::Detach() bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) { - if ( !wxMenuBarBase::Append( menu, title ) ) - return false; - - return GtkAppend(menu, title); + if (wxMenuBarBase::Append(menu, title)) + { + GtkAppend(menu, title); + return true; + } + return false; } -bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos) +void wxMenuBar::GtkAppend(wxMenu* menu, const wxString& title, int pos) { menu->SetLayoutDirection(GetLayoutDirection()); @@ -251,7 +253,10 @@ bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos) const wxString str(wxStripMenuCodes(item->GetItemLabel())); if ( item->IsSubMenu() ) - return GtkAppend(item->GetSubMenu(), str, pos); + { + GtkAppend(item->GetSubMenu(), str, pos); + return; + } menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) ); @@ -282,21 +287,16 @@ bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos) if ( m_menuBarFrame ) AttachToFrame( menu, m_menuBarFrame ); - - return true; } bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) { - if ( !wxMenuBarBase::Insert(pos, menu, title) ) - return false; - - // TODO - - if ( !GtkAppend(menu, title, (int)pos) ) - return false; - - return true; + if (wxMenuBarBase::Insert(pos, menu, title)) + { + GtkAppend(menu, title, int(pos)); + return true; + } + return false; } wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) @@ -745,7 +745,7 @@ wxString wxMenu::GetTitle() const return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle()); } -bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) +void wxMenu::GtkAppend(wxMenuItem* mitem, int pos) { GtkWidget *menuItem; switch (mitem->GetKind()) @@ -763,9 +763,9 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) wxMenuItem* radioGroupItem = NULL; const size_t numItems = GetMenuItemCount(); - const size_t n = pos == -1 ? numItems - : static_cast(pos); - if ( n > 0 ) + const size_t n = pos == -1 ? numItems - 1 : size_t(pos); + + if (n != 0) { wxMenuItem* const itemPrev = FindItemByPosition(n - 1); if ( itemPrev->GetKind() == wxITEM_RADIO ) @@ -776,9 +776,9 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) } } - if ( n < numItems ) + if (radioGroupItem == NULL && n != numItems - 1) { - wxMenuItem* const itemNext = FindItemByPosition(n); + wxMenuItem* const itemNext = FindItemByPosition(n + 1); if ( itemNext->GetKind() == wxITEM_RADIO ) { // Inserting an item before an existing radio item @@ -850,24 +850,26 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) mitem); } } - - return true; } wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem) { - if (!GtkAppend(mitem)) - return NULL; - - return wxMenuBase::DoAppend(mitem); + if (wxMenuBase::DoAppend(mitem)) + { + GtkAppend(mitem); + return mitem; + } + return NULL; } wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) { - if ( !GtkAppend(item, (int)pos) ) - return NULL; - - return wxMenuBase::DoInsert(pos, item); + if (wxMenuBase::DoInsert(pos, item)) + { + GtkAppend(item, int(pos)); + return item; + } + return NULL; } wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) -- 2.45.2