From 98f297837a2f2ba63ec7d1c8c8fbf9cae490744a Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Sat, 16 Apr 2005 16:53:55 +0000 Subject: [PATCH] Readded test for wxMenuItem::SetLabel() to sample. Support for hotkeys in names functions for GTK+2. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@33671 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/menu/menu.cpp | 3 +++ src/gtk/menu.cpp | 52 +++++++++++++++++++++++++++++++++---------- src/gtk1/menu.cpp | 52 +++++++++++++++++++++++++++++++++---------- 3 files changed, 83 insertions(+), 24 deletions(-) diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index e8e5bd4eb1..3a87b4dc18 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -438,6 +438,8 @@ MyFrame::MyFrame() menuMenu->AppendSeparator(); menuMenu->Append(Menu_Menu_GetInfo, _T("Get menu item in&fo\tAlt-F"), _T("Show the state of the last menu item")); + menuMenu->Append(Menu_Menu_SetLabel, _T("Set menu item label\tAlt-L"), + _T("Set the label of a menu item")); #if wxUSE_TEXTDLG menuMenu->AppendSeparator(); menuMenu->Append(Menu_Menu_FindItem, _T("Find menu item from label"), @@ -799,6 +801,7 @@ void MyFrame::OnSetLabelMenuItem(wxCommandEvent& WXUNUSED(event)) item->GetLabel(), this ); + label.Replace( _T("\\t"), _T("\t") ); if ( !label.empty() ) { diff --git a/src/gtk/menu.cpp b/src/gtk/menu.cpp index 00919081b2..0c44d0a74d 100644 --- a/src/gtk/menu.cpp +++ b/src/gtk/menu.cpp @@ -57,7 +57,7 @@ extern void wxapp_install_idle_handler(); extern bool g_isIdle; #if wxUSE_ACCEL -static wxString GetHotKey( const wxMenuItem& item ); +static wxString GetGtkHotKey( const wxMenuItem& item ); #endif //----------------------------------------------------------------------------- @@ -815,14 +815,17 @@ void wxMenuItem::SetText( const wxString& str ) { // Some optimization to avoid flicker wxString oldLabel = m_text; - oldLabel = wxStripMenuCodes(oldLabel.BeforeFirst('\t')); + oldLabel = wxStripMenuCodes(oldLabel); oldLabel.Replace(wxT("_"), wxT("")); - wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t')); - if (oldLabel == label1) - return; + wxString label1 = wxStripMenuCodes(str); + // Make sure we can change a hotkey even if the label is unaltered + wxString oldhotkey = GetHotKey(); DoSetText(str); + if (oldLabel == label1 && oldhotkey == GetHotKey()) + return; + if (m_menuItem) { GtkLabel *label; @@ -842,6 +845,32 @@ void wxMenuItem::SetText( const wxString& str ) gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) ); #endif } + +#ifdef __WXGTK20__ + guint accel_key; + GdkModifierType accel_mods; + wxCharBuffer oldbuf = wxGTK_CONV( oldhotkey ); + gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods); + if (accel_key != 0) + { + gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem), + m_parentMenu->m_accel, + accel_key, + accel_mods ); + } + + wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) ); + gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); + if (accel_key != 0) + { + gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem), + "activate", + m_parentMenu->m_accel, + accel_key, + accel_mods, + GTK_ACCEL_VISIBLE); + } +#endif } // it's valid for this function to be called even if m_menuItem == NULL @@ -873,8 +902,6 @@ void wxMenuItem::DoSetText( const wxString& str ) ++pc; } - // wxPrintf( wxT("DoSetText(): str %s m_text %s\n"), str.c_str(), m_text.c_str() ); - m_hotKey = wxT(""); if(*pc == wxT('\t')) @@ -882,6 +909,8 @@ void wxMenuItem::DoSetText( const wxString& str ) pc++; m_hotKey = pc; } + + // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() ); } #if wxUSE_ACCEL @@ -1100,7 +1129,7 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) // accelerator for the item, as specified by its label // (ex. Ctrl+O for open) - gtk_accelerator_parse(GetHotKey(*mitem).c_str(), &accel_key, + gtk_accelerator_parse(GetGtkHotKey(*mitem).c_str(), &accel_key, &accel_mods); if (accel_key != GDK_VoidSymbol) { @@ -1271,10 +1300,9 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) guint accel_key; GdkModifierType accel_mods; - wxCharBuffer buf = wxGTK_CONV( GetHotKey(*mitem) ); - - // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetHotKey(*mitem).c_str() ); + wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) ); + // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() ); gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); if (accel_key != 0) { @@ -1364,7 +1392,7 @@ int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const #if wxUSE_ACCEL -static wxString GetHotKey( const wxMenuItem& item ) +static wxString GetGtkHotKey( const wxMenuItem& item ) { wxString hotkey; diff --git a/src/gtk1/menu.cpp b/src/gtk1/menu.cpp index 00919081b2..0c44d0a74d 100644 --- a/src/gtk1/menu.cpp +++ b/src/gtk1/menu.cpp @@ -57,7 +57,7 @@ extern void wxapp_install_idle_handler(); extern bool g_isIdle; #if wxUSE_ACCEL -static wxString GetHotKey( const wxMenuItem& item ); +static wxString GetGtkHotKey( const wxMenuItem& item ); #endif //----------------------------------------------------------------------------- @@ -815,14 +815,17 @@ void wxMenuItem::SetText( const wxString& str ) { // Some optimization to avoid flicker wxString oldLabel = m_text; - oldLabel = wxStripMenuCodes(oldLabel.BeforeFirst('\t')); + oldLabel = wxStripMenuCodes(oldLabel); oldLabel.Replace(wxT("_"), wxT("")); - wxString label1 = wxStripMenuCodes(str.BeforeFirst('\t')); - if (oldLabel == label1) - return; + wxString label1 = wxStripMenuCodes(str); + // Make sure we can change a hotkey even if the label is unaltered + wxString oldhotkey = GetHotKey(); DoSetText(str); + if (oldLabel == label1 && oldhotkey == GetHotKey()) + return; + if (m_menuItem) { GtkLabel *label; @@ -842,6 +845,32 @@ void wxMenuItem::SetText( const wxString& str ) gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) ); #endif } + +#ifdef __WXGTK20__ + guint accel_key; + GdkModifierType accel_mods; + wxCharBuffer oldbuf = wxGTK_CONV( oldhotkey ); + gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods); + if (accel_key != 0) + { + gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem), + m_parentMenu->m_accel, + accel_key, + accel_mods ); + } + + wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) ); + gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); + if (accel_key != 0) + { + gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem), + "activate", + m_parentMenu->m_accel, + accel_key, + accel_mods, + GTK_ACCEL_VISIBLE); + } +#endif } // it's valid for this function to be called even if m_menuItem == NULL @@ -873,8 +902,6 @@ void wxMenuItem::DoSetText( const wxString& str ) ++pc; } - // wxPrintf( wxT("DoSetText(): str %s m_text %s\n"), str.c_str(), m_text.c_str() ); - m_hotKey = wxT(""); if(*pc == wxT('\t')) @@ -882,6 +909,8 @@ void wxMenuItem::DoSetText( const wxString& str ) pc++; m_hotKey = pc; } + + // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() ); } #if wxUSE_ACCEL @@ -1100,7 +1129,7 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) // accelerator for the item, as specified by its label // (ex. Ctrl+O for open) - gtk_accelerator_parse(GetHotKey(*mitem).c_str(), &accel_key, + gtk_accelerator_parse(GetGtkHotKey(*mitem).c_str(), &accel_key, &accel_mods); if (accel_key != GDK_VoidSymbol) { @@ -1271,10 +1300,9 @@ bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) guint accel_key; GdkModifierType accel_mods; - wxCharBuffer buf = wxGTK_CONV( GetHotKey(*mitem) ); - - // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetHotKey(*mitem).c_str() ); + wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) ); + // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() ); gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); if (accel_key != 0) { @@ -1364,7 +1392,7 @@ int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const #if wxUSE_ACCEL -static wxString GetHotKey( const wxMenuItem& item ) +static wxString GetGtkHotKey( const wxMenuItem& item ) { wxString hotkey; -- 2.45.2