From eaafd2f8b7acf211feb30c86db037fffe22ee565 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Tue, 4 Mar 2003 23:34:52 +0000 Subject: [PATCH] added support for GTK2 label mnemonics (patch #689573) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@19484 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/gtk/control.h | 3 +++ include/wx/gtk1/control.h | 3 +++ src/gtk/button.cpp | 5 +++++ src/gtk/checkbox.cpp | 12 +++++++---- src/gtk/control.cpp | 42 +++++++++++++++++++++++++++++++++++++++ src/gtk/radiobut.cpp | 5 +++++ src/gtk1/button.cpp | 5 +++++ src/gtk1/checkbox.cpp | 12 +++++++---- src/gtk1/control.cpp | 42 +++++++++++++++++++++++++++++++++++++++ src/gtk1/radiobut.cpp | 5 +++++ 11 files changed, 127 insertions(+), 8 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 9e5c3f4ee4..58054505ba 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -69,6 +69,7 @@ Unix: wxGTK: +- added support for label mnemonics to GTK+2 build (Michael Moss) - added native wxMessageDialog implementation for GTK+2 build - fixed wxMenu::Remove (John Skiff and Benjamin Williams) - made wxTextCtrl::EmulateKeyPress() work for Delete and Backspace diff --git a/include/wx/gtk/control.h b/include/wx/gtk/control.h index 38b59dcf29..17db76e571 100644 --- a/include/wx/gtk/control.h +++ b/include/wx/gtk/control.h @@ -55,6 +55,9 @@ public: protected: virtual wxSize DoGetBestSize() const; +#ifdef __WXGTK20__ + wxString PrepareLabelMnemonics( const wxString &label ) const; +#endif wxString m_label; char m_chAccel; // enabled to avoid breaking binary compatibility later on diff --git a/include/wx/gtk1/control.h b/include/wx/gtk1/control.h index 38b59dcf29..17db76e571 100644 --- a/include/wx/gtk1/control.h +++ b/include/wx/gtk1/control.h @@ -55,6 +55,9 @@ public: protected: virtual wxSize DoGetBestSize() const; +#ifdef __WXGTK20__ + wxString PrepareLabelMnemonics( const wxString &label ) const; +#endif wxString m_label; char m_chAccel; // enabled to avoid breaking binary compatibility later on diff --git a/src/gtk/button.cpp b/src/gtk/button.cpp index fae04c1372..b61143a0ef 100644 --- a/src/gtk/button.cpp +++ b/src/gtk/button.cpp @@ -175,7 +175,12 @@ void wxButton::SetLabel( const wxString &label ) wxControl::SetLabel( label ); +#ifdef __WXGTK20__ + wxString label2 = PrepareLabelMnemonics( label ); + gtk_label_set_text_with_mnemonic( GTK_LABEL( BUTTON_CHILD(m_widget) ), wxGTK_CONV( label2 ) ); +#else gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget) ), wxGTK_CONV( GetLabel() ) ); +#endif } bool wxButton::Enable( bool enable ) diff --git a/src/gtk/checkbox.cpp b/src/gtk/checkbox.cpp index 0916de575b..c49c46ccff 100644 --- a/src/gtk/checkbox.cpp +++ b/src/gtk/checkbox.cpp @@ -85,8 +85,6 @@ bool wxCheckBox::Create(wxWindow *parent, return FALSE; } - wxControl::SetLabel( label ); - if ( style & wxALIGN_RIGHT ) { // VZ: as I don't know a way to create a right aligned checkbox with @@ -94,7 +92,7 @@ bool wxCheckBox::Create(wxWindow *parent, // left of it m_widgetCheckbox = gtk_check_button_new(); - m_widgetLabel = gtk_label_new( wxGTK_CONV( m_label ) ); + m_widgetLabel = gtk_label_new(""); gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5); m_widget = gtk_hbox_new(FALSE, 0); @@ -106,10 +104,11 @@ bool wxCheckBox::Create(wxWindow *parent, } else { - m_widgetCheckbox = gtk_check_button_new_with_label( wxGTK_CONV( m_label ) ); + m_widgetCheckbox = gtk_check_button_new_with_label(""); m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox ); m_widget = m_widgetCheckbox; } + SetLabel( label ); gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox), "clicked", @@ -166,7 +165,12 @@ void wxCheckBox::SetLabel( const wxString& label ) wxControl::SetLabel( label ); +#ifdef __WXGTK20__ + wxString label2 = PrepareLabelMnemonics( label ); + gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) ); +#else gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) ); +#endif } bool wxCheckBox::Enable( bool enable ) diff --git a/src/gtk/control.cpp b/src/gtk/control.cpp index 72e2da93ef..8801219ee3 100644 --- a/src/gtk/control.cpp +++ b/src/gtk/control.cpp @@ -83,5 +83,47 @@ wxSize wxControl::DoGetBestSize() const return wxSize(req.width, req.height); } +wxString wxControl::PrepareLabelMnemonics( const wxString &label ) const +{ + //Format mnemonics properly for GTK2. This can be called from GTK1.x, but + //it's not very useful because mnemonics don't exist prior to GTK2. + wxString label2; + for (size_t i = 0; i < label.Len(); i++) + { + if (label.GetChar(i) == wxT('&')) + { + //Mnemonic escape sequence "&&" is a literal "&" in the output. + if (label.GetChar(i + 1) == wxT('&')) + { + label2 << wxT('&'); + i++; + } + //Handle special case of "&_" (i.e. "_" is the mnemonic). + //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a + //dash for now. + else if (label.GetChar(i + 1) == wxT('_')) + { + label2 << wxT("_-"); + i++; + } + //Replace WX mnemonic indicator "&" with GTK indicator "_". + else + { + label2 << wxT('_'); + } + } + else if (label.GetChar(i) == wxT('_')) + { + //Escape any underlines in the string so GTK doesn't use them. + label2 << wxT("__"); + } + else + { + label2 << label.GetChar(i); + } + } + return label2; +} + #endif // wxUSE_CONTROLS diff --git a/src/gtk/radiobut.cpp b/src/gtk/radiobut.cpp index fad83bc1b1..d22bf804c2 100644 --- a/src/gtk/radiobut.cpp +++ b/src/gtk/radiobut.cpp @@ -154,7 +154,12 @@ void wxRadioButton::SetLabel( const wxString& label ) wxControl::SetLabel( label ); GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) ); +#ifdef __WXGTK20__ + wxString label2 = PrepareLabelMnemonics( label ); + gtk_label_set_text_with_mnemonic( g_label, wxGTK_CONV( label2 ) ); +#else gtk_label_set( g_label, wxGTK_CONV( GetLabel() ) ); +#endif } void wxRadioButton::SetValue( bool val ) diff --git a/src/gtk1/button.cpp b/src/gtk1/button.cpp index fae04c1372..b61143a0ef 100644 --- a/src/gtk1/button.cpp +++ b/src/gtk1/button.cpp @@ -175,7 +175,12 @@ void wxButton::SetLabel( const wxString &label ) wxControl::SetLabel( label ); +#ifdef __WXGTK20__ + wxString label2 = PrepareLabelMnemonics( label ); + gtk_label_set_text_with_mnemonic( GTK_LABEL( BUTTON_CHILD(m_widget) ), wxGTK_CONV( label2 ) ); +#else gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget) ), wxGTK_CONV( GetLabel() ) ); +#endif } bool wxButton::Enable( bool enable ) diff --git a/src/gtk1/checkbox.cpp b/src/gtk1/checkbox.cpp index 0916de575b..c49c46ccff 100644 --- a/src/gtk1/checkbox.cpp +++ b/src/gtk1/checkbox.cpp @@ -85,8 +85,6 @@ bool wxCheckBox::Create(wxWindow *parent, return FALSE; } - wxControl::SetLabel( label ); - if ( style & wxALIGN_RIGHT ) { // VZ: as I don't know a way to create a right aligned checkbox with @@ -94,7 +92,7 @@ bool wxCheckBox::Create(wxWindow *parent, // left of it m_widgetCheckbox = gtk_check_button_new(); - m_widgetLabel = gtk_label_new( wxGTK_CONV( m_label ) ); + m_widgetLabel = gtk_label_new(""); gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5); m_widget = gtk_hbox_new(FALSE, 0); @@ -106,10 +104,11 @@ bool wxCheckBox::Create(wxWindow *parent, } else { - m_widgetCheckbox = gtk_check_button_new_with_label( wxGTK_CONV( m_label ) ); + m_widgetCheckbox = gtk_check_button_new_with_label(""); m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox ); m_widget = m_widgetCheckbox; } + SetLabel( label ); gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox), "clicked", @@ -166,7 +165,12 @@ void wxCheckBox::SetLabel( const wxString& label ) wxControl::SetLabel( label ); +#ifdef __WXGTK20__ + wxString label2 = PrepareLabelMnemonics( label ); + gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) ); +#else gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) ); +#endif } bool wxCheckBox::Enable( bool enable ) diff --git a/src/gtk1/control.cpp b/src/gtk1/control.cpp index 72e2da93ef..8801219ee3 100644 --- a/src/gtk1/control.cpp +++ b/src/gtk1/control.cpp @@ -83,5 +83,47 @@ wxSize wxControl::DoGetBestSize() const return wxSize(req.width, req.height); } +wxString wxControl::PrepareLabelMnemonics( const wxString &label ) const +{ + //Format mnemonics properly for GTK2. This can be called from GTK1.x, but + //it's not very useful because mnemonics don't exist prior to GTK2. + wxString label2; + for (size_t i = 0; i < label.Len(); i++) + { + if (label.GetChar(i) == wxT('&')) + { + //Mnemonic escape sequence "&&" is a literal "&" in the output. + if (label.GetChar(i + 1) == wxT('&')) + { + label2 << wxT('&'); + i++; + } + //Handle special case of "&_" (i.e. "_" is the mnemonic). + //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a + //dash for now. + else if (label.GetChar(i + 1) == wxT('_')) + { + label2 << wxT("_-"); + i++; + } + //Replace WX mnemonic indicator "&" with GTK indicator "_". + else + { + label2 << wxT('_'); + } + } + else if (label.GetChar(i) == wxT('_')) + { + //Escape any underlines in the string so GTK doesn't use them. + label2 << wxT("__"); + } + else + { + label2 << label.GetChar(i); + } + } + return label2; +} + #endif // wxUSE_CONTROLS diff --git a/src/gtk1/radiobut.cpp b/src/gtk1/radiobut.cpp index fad83bc1b1..d22bf804c2 100644 --- a/src/gtk1/radiobut.cpp +++ b/src/gtk1/radiobut.cpp @@ -154,7 +154,12 @@ void wxRadioButton::SetLabel( const wxString& label ) wxControl::SetLabel( label ); GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) ); +#ifdef __WXGTK20__ + wxString label2 = PrepareLabelMnemonics( label ); + gtk_label_set_text_with_mnemonic( g_label, wxGTK_CONV( label2 ) ); +#else gtk_label_set( g_label, wxGTK_CONV( GetLabel() ) ); +#endif } void wxRadioButton::SetValue( bool val ) -- 2.45.2