From fdf47e8e1279f6becf013e1025c091a54caac894 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 24 Dec 2017 21:48:51 +0100 Subject: [PATCH] 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. --- include/wx/msw/statbox.h | 19 +++++++++++++++++-- src/msw/statbox.cpp | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/include/wx/msw/statbox.h b/include/wx/msw/statbox.h index 89eb8fcdde..294f1519ae 100644 --- a/include/wx/msw/statbox.h +++ b/include/wx/msw/statbox.h @@ -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 { public: - wxStaticBox() { } + wxStaticBox() + : wxCompositeWindowSettersOnly() + { + } 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() { 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() { 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); }; diff --git a/src/msw/statbox.cpp b/src/msw/statbox.cpp index 9ca1b792bd..97a48d7bb6 100644 --- a/src/msw/statbox.cpp +++ b/src/msw/statbox.cpp @@ -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::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 )