Fixed implementation of wxToolBarTool::SetLabel (MSW).

Because label is implemented in the control tool as a separate wxStaticText object which exists only if label is non-empty so we need to handle appropriately also the cases when non-empty label is set to empty and vice versa.
Setting a new label for the button tool with TB_SETBUTTONINFO would require to do some additional actions for other items in the toolbar (manual re-positioning items in the control tools, updating stretchable spacers) so it is easier just to re-create the toolbar with Realize().

See #17567
This commit is contained in:
Artur Wieczorek
2016-06-17 22:40:55 +02:00
parent e45a9543d6
commit 1373241f21

View File

@@ -148,14 +148,16 @@ public:
{ {
if ( IsControl() && !m_label.empty() ) if ( IsControl() && !m_label.empty() )
{ {
// create a control to render the control's label // Create a control to render the control's label.
// It has the same witdh as the control.
wxSize size(control->GetSize().GetWidth(), wxDefaultCoord);
m_staticText = new wxStaticText m_staticText = new wxStaticText
( (
m_tbar, m_tbar,
wxID_ANY, wxID_ANY,
m_label, m_label,
wxDefaultPosition, wxDefaultPosition,
wxDefaultSize, size,
wxALIGN_CENTRE | wxST_NO_AUTORESIZE wxALIGN_CENTRE | wxST_NO_AUTORESIZE
); );
} }
@@ -172,20 +174,53 @@ public:
delete m_staticText; delete m_staticText;
} }
virtual void SetLabel(const wxString& label) virtual void SetLabel(const wxString& label) wxOVERRIDE
{ {
wxASSERT_MSG( IsControl() || IsButton(),
wxS("Label can be set for control or button tool only") );
if ( label == m_label ) if ( label == m_label )
return; return;
wxToolBarToolBase::SetLabel(label); wxToolBarToolBase::SetLabel(label);
if ( m_staticText ) if ( IsControl() )
m_staticText->SetLabel(label); {
if ( m_staticText )
// we need to update the label shown in the toolbar because it has a {
// pointer to the internal buffer of the old label if ( !label.empty() )
// {
// TODO: use TB_SETBUTTONINFO m_staticText->SetLabel(label);
}
else
{
delete m_staticText;
m_staticText = NULL;
}
}
else
{
if ( !label.empty() )
{
// Create a control to render the control's label.
// It has the same witdh as the control.
wxSize size(m_control->GetSize().GetWidth(), wxDefaultCoord);
m_staticText = new wxStaticText(m_tbar, wxID_ANY, label,
wxDefaultPosition, size,
wxALIGN_CENTRE | wxST_NO_AUTORESIZE);
}
}
}
else if ( IsButton() )
{
// Because new label can have different length than the old one
// so updating button's label with TB_SETBUTTONINFO would require
// also manual re-positionining items in the control tools located
// to the right in the toolbar and recalculation of stretchable
// spacers so it is easier just to recreate the toolbar with
// Realize(). Performance penalty should be negligible.
m_tbar->Realize();
}
} }
wxStaticText* GetStaticText() wxStaticText* GetStaticText()