From de20880064f76e8742397cb4cc73db9d25edab50 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 11 Mar 2014 15:20:56 +0000 Subject: [PATCH] Fix accelerators in appended submenu items in wxGTK. They were not previously taken into account because we didn't call gtk_widget_add_accelerator() on the right GtkAccelGroup. Closes #16050. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_3_0_BRANCH@76113 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + src/gtk/menu.cpp | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 2d1e008219..3074d1c7d3 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -612,6 +612,7 @@ wxGTK: - Fix wxStaticBoxSizer size calculation. - Fix AUI panel re-docking. - Add support for wxDD_DIR_MUST_EXIST to wxDirDialog. +- Fix handling of the accelerators for sub menu items (Chaobin Zhang). - Fix disappearing mouse clicks when using wxTreeCtrl. - Fix calls to Move{Before,After}InTabOrder() after parent window creation. diff --git a/src/gtk/menu.cpp b/src/gtk/menu.cpp index f5194b89f0..3c5426dc80 100644 --- a/src/gtk/menu.cpp +++ b/src/gtk/menu.cpp @@ -86,6 +86,36 @@ static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event) win->HandleWindowEvent( event ); } +// Return the top level menu containing this menu (possibly this menu itself). +static wxMenu* GetRootParentMenu(wxMenu* menu) +{ + while ( menu->GetParent() ) + menu = menu->GetParent(); + + return menu; +} + +// Call SetGtkLabel() to update the labels of all the items in this items sub +// menu, recursively. +static void UpdateSubMenuItemLabels(wxMenuItem* itemMenu) +{ + wxMenu* const menu = itemMenu->GetSubMenu(); + wxCHECK_RET( menu, "should only be called for sub menus" ); + + const wxMenuItemList& items = menu->GetMenuItems(); + for ( wxMenuItemList::const_iterator + it = items.begin(); it != items.end(); ++it ) + { + wxMenuItem* const item = *it; + if ( !item->IsSeparator() ) + { + item->SetGtkLabel(); + if ( item->IsSubMenu() ) + UpdateSubMenuItemLabels(item); + } + } +} + //----------------------------------------------------------------------------- // wxMenuBar //----------------------------------------------------------------------------- @@ -658,7 +688,7 @@ void wxMenuItem::SetItemLabel( const wxString& str ) if (accel_key) { gtk_widget_remove_accelerator( - m_menuItem, m_parentMenu->m_accel, accel_key, accel_mods); + m_menuItem, GetRootParentMenu(m_parentMenu)->m_accel, accel_key, accel_mods); } } #endif // wxUSE_ACCEL @@ -679,7 +709,7 @@ void wxMenuItem::SetGtkLabel() if (accel_key) { gtk_widget_add_accelerator( - m_menuItem, "activate", m_parentMenu->m_accel, + m_menuItem, "activate", GetRootParentMenu(m_parentMenu)->m_accel, accel_key, accel_mods, GTK_ACCEL_VISIBLE); } #endif // wxUSE_ACCEL @@ -931,6 +961,9 @@ void wxMenu::GtkAppend(wxMenuItem* mitem, int pos) if ( !mitem->IsSeparator() ) { mitem->SetGtkLabel(); + if ( mitem->IsSubMenu() ) + UpdateSubMenuItemLabels(mitem); + g_signal_connect (menuItem, "select", G_CALLBACK(menuitem_select), mitem); g_signal_connect (menuItem, "deselect",