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
This commit is contained in:
Vadim Zeitlin
2011-05-10 08:50:47 +00:00
parent 89511b4268
commit a082862987
2 changed files with 37 additions and 20 deletions

View File

@@ -101,7 +101,6 @@ private:
// common part of Append (if pos == -1) and Insert // common part of Append (if pos == -1) and Insert
bool GtkAppend(wxMenuItem *item, int pos=-1); bool GtkAppend(wxMenuItem *item, int pos=-1);
GtkWidget *m_prevRadio;
DECLARE_DYNAMIC_CLASS(wxMenu) DECLARE_DYNAMIC_CLASS(wxMenu)
}; };

View File

@@ -698,8 +698,6 @@ void wxMenu::Init()
gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff); 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 // append the title as the very first entry if we have it
if ( !m_title.empty() ) if ( !m_title.empty() )
{ {
@@ -750,8 +748,6 @@ wxString wxMenu::GetTitle() const
bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
{ {
GtkWidget *menuItem; GtkWidget *menuItem;
GtkWidget* prevRadio = m_prevRadio;
m_prevRadio = NULL;
switch (mitem->GetKind()) switch (mitem->GetKind())
{ {
case wxITEM_SEPARATOR: case wxITEM_SEPARATOR:
@@ -762,11 +758,44 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos)
break; break;
case wxITEM_RADIO: 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<size_t>(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; GSList* group = NULL;
if (prevRadio) if ( radioGroupItem )
group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(prevRadio)); {
group = gtk_radio_menu_item_get_group(
GTK_RADIO_MENU_ITEM(radioGroupItem->GetMenuItem())
);
}
menuItem = gtk_radio_menu_item_new_with_label(group, ""); menuItem = gtk_radio_menu_item_new_with_label(group, "");
m_prevRadio = menuItem;
} }
break; break;
default: default:
@@ -835,14 +864,10 @@ wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
{ {
if ( !wxMenuBase::DoInsert(pos, item) )
return NULL;
// TODO
if ( !GtkAppend(item, (int)pos) ) if ( !GtkAppend(item, (int)pos) )
return NULL; return NULL;
return item; return wxMenuBase::DoInsert(pos, item);
} }
wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
@@ -851,13 +876,6 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
return NULL; return NULL;
GtkWidget * const mitem = item->GetMenuItem(); 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_menu_item_set_submenu(GTK_MENU_ITEM(mitem), NULL);
gtk_widget_destroy(mitem); gtk_widget_destroy(mitem);
item->SetMenuItem(NULL); item->SetMenuItem(NULL);