Add support for GTK2 label mnemonics

Backported from head


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@20036 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2003-04-06 17:55:52 +00:00
parent 91c04f6445
commit e5490e6eb8
11 changed files with 131 additions and 8 deletions

View File

@@ -207,6 +207,7 @@ wxGTK:
- fixed a memory leak in generic wxFileDialog
- implemented wxTextCtrl::Remove/Replace() for GTK2
- improved X11 ShowFullScreen
- added support for GTK2 label mnemonics
wxMSW:

View File

@@ -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

View File

@@ -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

View File

@@ -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 )

View File

@@ -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 )

View File

@@ -83,5 +83,49 @@ wxSize wxControl::DoGetBestSize() const
return wxSize(req.width, req.height);
}
#ifdef __WXGTK20__
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
#endif // wxUSE_CONTROLS

View File

@@ -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 )

View File

@@ -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 )

View File

@@ -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 )

View File

@@ -83,5 +83,49 @@ wxSize wxControl::DoGetBestSize() const
return wxSize(req.width, req.height);
}
#ifdef __WXGTK20__
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
#endif // wxUSE_CONTROLS

View File

@@ -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 )