Fix colours and fonts of wxStaticBox label window in wxMSW

Inherit from wxCompositeWindowSettersOnly<> to make sure all the usual
setters, such as SetForegroundColour() and SetFont(), called on
wxStaticBox are propagated to the label window too.

However also prevent SetBackgroundColour() from being propagated
unnecessarily -- because the checkbox already inherits the parent
background colour by default in wxMSW anyhow -- and still override
SetFont() to adjust the label window position after the font change,
otherwise it could be truncated after increasing the font size, for
example.

Because of these issues, wxCompositeWindowSettersOnly is not ideally
suited for its use here, but on balance it still seems to be better to
use it rather than reimplement parts of its functionality here.
This commit is contained in:
Vadim Zeitlin
2017-12-24 21:48:51 +01:00
parent 329af399eb
commit fdf47e8e12
2 changed files with 56 additions and 3 deletions

View File

@@ -11,11 +11,16 @@
#ifndef _WX_MSW_STATBOX_H_
#define _WX_MSW_STATBOX_H_
#include "wx/compositewin.h"
// Group box
class WXDLLIMPEXP_CORE wxStaticBox : public wxStaticBoxBase
class WXDLLIMPEXP_CORE wxStaticBox : public wxCompositeWindowSettersOnly<wxStaticBoxBase>
{
public:
wxStaticBox() { }
wxStaticBox()
: wxCompositeWindowSettersOnly<wxStaticBoxBase>()
{
}
wxStaticBox(wxWindow *parent, wxWindowID id,
const wxString& label,
@@ -23,6 +28,7 @@ public:
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxStaticBoxNameStr)
: wxCompositeWindowSettersOnly<wxStaticBoxBase>()
{
Create(parent, id, label, pos, size, style, name);
}
@@ -33,6 +39,7 @@ public:
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString &name = wxStaticBoxNameStr)
: wxCompositeWindowSettersOnly<wxStaticBoxBase>()
{
Create(parent, id, label, pos, size, style, name);
}
@@ -54,6 +61,9 @@ public:
/// Implementation only
virtual void GetBordersForSizer(int *borderTop, int *borderOther) const wxOVERRIDE;
virtual bool SetBackgroundColour(const wxColour& colour) wxOVERRIDE;
virtual bool SetFont(const wxFont& font) wxOVERRIDE;
virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const wxOVERRIDE;
// returns true if the platform should explicitly apply a theme border
@@ -66,6 +76,8 @@ public:
virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) wxOVERRIDE;
protected:
virtual wxWindowList GetCompositeWindowParts() const wxOVERRIDE;
// return the region with all the windows inside this static box excluded
virtual WXHRGN MSWGetRegionWithoutChildren();
@@ -80,6 +92,9 @@ protected:
void OnPaint(wxPaintEvent& event);
private:
void PositionLabelWindow();
wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxStaticBox);
};

View File

@@ -123,11 +123,25 @@ bool wxStaticBox::Create(wxWindow* parent,
m_labelWin = labelWin;
m_labelWin->Reparent(this);
m_labelWin->Move(FromDIP(LABEL_HORZ_OFFSET), 0);
PositionLabelWindow();
return true;
}
void wxStaticBox::PositionLabelWindow()
{
m_labelWin->SetSize(m_labelWin->GetBestSize());
m_labelWin->Move(FromDIP(LABEL_HORZ_OFFSET), 0);
}
wxWindowList wxStaticBox::GetCompositeWindowParts() const
{
wxWindowList parts;
if ( m_labelWin )
parts.push_back(m_labelWin);
return parts;
}
WXDWORD wxStaticBox::MSWGetStyle(long style, WXDWORD *exstyle) const
{
long styleWin = wxStaticBoxBase::MSWGetStyle(style, exstyle);
@@ -191,6 +205,30 @@ void wxStaticBox::GetBordersForSizer(int *borderTop, int *borderOther) const
*borderTop += FromDIP(LABEL_VERT_BORDER);
}
bool wxStaticBox::SetBackgroundColour(const wxColour& colour)
{
// Do _not_ call the immediate base class method, we don't need to set the
// label window (which is the only sub-window of this composite window)
// background explicitly because it will almost always be a wxCheckBox or
// wxRadioButton which inherits its background from the box anyhow, so
// setting it would be at best useless.
return wxStaticBoxBase::SetBackgroundColour(colour);
}
bool wxStaticBox::SetFont(const wxFont& font)
{
if ( !wxCompositeWindowSettersOnly<wxStaticBoxBase>::SetFont(font) )
return false;
// We need to reposition the label as its size may depend on the font.
if ( m_labelWin )
{
PositionLabelWindow();
}
return true;
}
WXLRESULT wxStaticBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
{
if ( nMsg == WM_NCHITTEST )