From 1ec7ae9a6f3b6c1078d0a80a8e0304d143ced4ec Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 14 Jul 2020 15:43:23 +0200 Subject: [PATCH 1/4] Use wxBrush objects instead of pointers in wxGenericTreeCtrl There is no need for double indirection, wxBrush object is already pointer-like internally. No real changes, just simplify/optimize the code a little. --- include/wx/generic/treectlg.h | 7 ++++--- src/generic/treectlg.cpp | 13 +++---------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/include/wx/generic/treectlg.h b/include/wx/generic/treectlg.h index e8c9abb17c..1892000920 100644 --- a/include/wx/generic/treectlg.h +++ b/include/wx/generic/treectlg.h @@ -13,8 +13,9 @@ #if wxUSE_TREECTRL -#include "wx/scrolwin.h" +#include "wx/brush.h" #include "wx/pen.h" +#include "wx/scrolwin.h" // ----------------------------------------------------------------------------- // forward declaration @@ -243,8 +244,8 @@ protected: unsigned short m_indent; int m_lineHeight; wxPen m_dottedPen; - wxBrush *m_hilightBrush, - *m_hilightUnfocusedBrush; + wxBrush m_hilightBrush, + m_hilightUnfocusedBrush; bool m_hasFocus; bool m_dirty; bool m_ownsImageListButtons; diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index 9c445298d2..cb09103d89 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -937,8 +937,6 @@ void wxGenericTreeCtrl::Init() m_indent = 15; m_spacing = 18; - m_hilightBrush = NULL; - m_hilightUnfocusedBrush = NULL; m_imageListButtons = NULL; m_ownsImageListButtons = false; @@ -995,9 +993,6 @@ bool wxGenericTreeCtrl::Create(wxWindow *parent, wxGenericTreeCtrl::~wxGenericTreeCtrl() { - delete m_hilightBrush; - delete m_hilightUnfocusedBrush; - DeleteAllItems(); delete m_renameTimer; @@ -1020,10 +1015,8 @@ void wxGenericTreeCtrl::OnSysColourChanged(wxSysColourChangedEvent&) if (!m_hasExplicitFont) SetOwnFont(attr.font); - delete m_hilightBrush; - m_hilightBrush = new wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)); - delete m_hilightUnfocusedBrush; - m_hilightUnfocusedBrush = new wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW)); + m_hilightBrush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)); + m_hilightUnfocusedBrush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW)); m_dottedPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT), 1, wxPENSTYLE_DOT); @@ -2544,7 +2537,7 @@ void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc) if ( item->IsSelected() ) { - dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush)); + dc.SetBrush(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush); drawItemBackground = true; } else From 0d7ab26e7d2b42a6150f28f859af19eeda85dc9b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 14 Jul 2020 15:46:05 +0200 Subject: [PATCH 2/4] Factor out wxGenericTreeCtrl::InitVisualAttributes() No real changes, just avoid calling an event handler directly. --- include/wx/generic/treectlg.h | 9 ++++++++- src/generic/treectlg.cpp | 5 ++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/wx/generic/treectlg.h b/include/wx/generic/treectlg.h index 1892000920..af48e36853 100644 --- a/include/wx/generic/treectlg.h +++ b/include/wx/generic/treectlg.h @@ -354,7 +354,14 @@ protected: private: bool m_hasExplicitFont; - void OnSysColourChanged(wxSysColourChangedEvent&); + void OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) + { + InitVisualAttributes(); + } + + // (Re)initialize colours, fonts, pens, brushes used by the control using + // the current system colours and font. + void InitVisualAttributes(); // Reset the state of the last find (i.e. keyboard incremental search) // operation. diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index cb09103d89..befce9c8a9 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -983,8 +983,7 @@ bool wxGenericTreeCtrl::Create(wxWindow *parent, } m_hasExplicitFont = m_hasFont; - wxSysColourChangedEvent evt; - OnSysColourChanged(evt); + InitVisualAttributes(); SetInitialSize(size); @@ -1007,7 +1006,7 @@ void wxGenericTreeCtrl::EnableBellOnNoMatch( bool on ) m_findBell = on; } -void wxGenericTreeCtrl::OnSysColourChanged(wxSysColourChangedEvent&) +void wxGenericTreeCtrl::InitVisualAttributes() { const wxVisualAttributes attr(GetDefaultAttributes()); SetOwnForegroundColour(attr.colFg); From 8535cde836e27465a4d4e4be993343b039dba4bd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 14 Jul 2020 15:47:35 +0200 Subject: [PATCH 3/4] Remove apparently unnecessary m_hasExplicitFont Just use wxWindow::m_hasFont instead. --- include/wx/generic/treectlg.h | 2 -- src/generic/treectlg.cpp | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/include/wx/generic/treectlg.h b/include/wx/generic/treectlg.h index af48e36853..2388eedc9c 100644 --- a/include/wx/generic/treectlg.h +++ b/include/wx/generic/treectlg.h @@ -352,8 +352,6 @@ protected: virtual wxSize DoGetBestSize() const wxOVERRIDE; private: - bool m_hasExplicitFont; - void OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) { InitVisualAttributes(); diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index befce9c8a9..ec53ebf4a0 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -982,7 +982,6 @@ bool wxGenericTreeCtrl::Create(wxWindow *parent, m_spacing = 10; } - m_hasExplicitFont = m_hasFont; InitVisualAttributes(); SetInitialSize(size); @@ -1011,7 +1010,7 @@ void wxGenericTreeCtrl::InitVisualAttributes() const wxVisualAttributes attr(GetDefaultAttributes()); SetOwnForegroundColour(attr.colFg); SetOwnBackgroundColour(attr.colBg); - if (!m_hasExplicitFont) + if (!m_hasFont) SetOwnFont(attr.font); m_hilightBrush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)); From 9cd3ab5ebd0a05230e044a9479517fe5be1a3fcd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 14 Jul 2020 17:34:24 +0200 Subject: [PATCH 4/4] Improve wxGenericTreeCtrl colours/fonts updating on theme change Don't override user-specified colours neither, previously we only did it for the font for some reason. Also do override the colours and the font when the theme changes, which didn't happen after the previous commit because calling SetOwnXXX() changes m_hasXXX to true, so we need to explicitly reset it back. --- src/generic/treectlg.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index ec53ebf4a0..7bae8ce932 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -1007,11 +1007,30 @@ void wxGenericTreeCtrl::EnableBellOnNoMatch( bool on ) void wxGenericTreeCtrl::InitVisualAttributes() { + // We want to use the default system colours/fonts here unless the user + // explicitly configured something different. We also need to reset the + // various m_hasXXX variables to false to prevent them from being left set + // to "true", as otherwise we wouldn't update the colours/fonts the next + // time the system colours change. const wxVisualAttributes attr(GetDefaultAttributes()); - SetOwnForegroundColour(attr.colFg); - SetOwnBackgroundColour(attr.colBg); - if (!m_hasFont) + if ( !m_hasFgCol ) + { + SetOwnForegroundColour(attr.colFg); + m_hasFgCol = false; + } + + if ( !m_hasBgCol ) + { + SetOwnBackgroundColour(attr.colBg); + m_hasBgCol = false; + } + + if ( !m_hasFont ) + { SetOwnFont(attr.font); + m_hasFont = false; + } + m_hilightBrush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)); m_hilightUnfocusedBrush = wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW));