From a0828629870da7f34bfe1f1c73441fde1ad7ec73 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 10 May 2011 08:50:47 +0000 Subject: [PATCH] Fix inserting radio menu items in wxGTK too. After fixing the insertion of radio menu items in wxMSW, also do it for wxGTK to make the newly added unit test pass there as well. Remove the unneeded wxMenu::m_prevRadio which doesn't make any sense neither (just as the "current radio group" pointer removed from wxMSW code before) and simply use the radio group of the existing item this radio item is being inserted before or after instead. See #13200. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67721 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/gtk/menu.h | 1 - src/gtk/menu.cpp | 56 ++++++++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/include/wx/gtk/menu.h b/include/wx/gtk/menu.h index 506d1986c0..943dacce35 100644 --- a/include/wx/gtk/menu.h +++ b/include/wx/gtk/menu.h @@ -101,7 +101,6 @@ private: // common part of Append (if pos == -1) and Insert bool GtkAppend(wxMenuItem *item, int pos=-1); - GtkWidget *m_prevRadio; DECLARE_DYNAMIC_CLASS(wxMenu) }; diff --git a/src/gtk/menu.cpp b/src/gtk/menu.cpp index b506d195b6..3c99374528 100644 --- a/src/gtk/menu.cpp +++ b/src/gtk/menu.cpp @@ -698,8 +698,6 @@ void wxMenu::Init() gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff); } - m_prevRadio = NULL; - // append the title as the very first entry if we have it if ( !m_title.empty() ) { @@ -750,8 +748,6 @@ wxString wxMenu::GetTitle() const bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) { GtkWidget *menuItem; - GtkWidget* prevRadio = m_prevRadio; - m_prevRadio = NULL; switch (mitem->GetKind()) { case wxITEM_SEPARATOR: @@ -762,11 +758,44 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) break; case wxITEM_RADIO: { + // See if we need to create a new radio group for this item or + // add it to an existing one. + wxMenuItem* radioGroupItem = NULL; + + const size_t numItems = GetMenuItemCount(); + const size_t n = pos == -1 ? numItems + : static_cast(pos); + if ( n > 0 ) + { + wxMenuItem* const itemPrev = FindItemByPosition(n - 1); + if ( itemPrev->GetKind() == wxITEM_RADIO ) + { + // Appending an item after an existing radio item puts + // it into the same radio group. + radioGroupItem = itemPrev; + } + } + + if ( n < numItems ) + { + wxMenuItem* const itemNext = FindItemByPosition(n); + if ( itemNext->GetKind() == wxITEM_RADIO ) + { + // Inserting an item before an existing radio item + // also puts it into the existing radio group. + radioGroupItem = itemNext; + } + } + GSList* group = NULL; - if (prevRadio) - group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(prevRadio)); + if ( radioGroupItem ) + { + group = gtk_radio_menu_item_get_group( + GTK_RADIO_MENU_ITEM(radioGroupItem->GetMenuItem()) + ); + } + menuItem = gtk_radio_menu_item_new_with_label(group, ""); - m_prevRadio = menuItem; } break; default: @@ -835,14 +864,10 @@ wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem) wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) { - if ( !wxMenuBase::DoInsert(pos, item) ) - return NULL; - - // TODO if ( !GtkAppend(item, (int)pos) ) return NULL; - return item; + return wxMenuBase::DoInsert(pos, item); } wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) @@ -851,13 +876,6 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) return NULL; GtkWidget * const mitem = item->GetMenuItem(); - if ( m_prevRadio == mitem ) - { - // deleting an item starts a new radio group (has to as we shouldn't - // keep a deleted pointer anyhow) - m_prevRadio = NULL; - } - gtk_menu_item_set_submenu(GTK_MENU_ITEM(mitem), NULL); gtk_widget_destroy(mitem); item->SetMenuItem(NULL); -- 2.45.2