From 3ed930c7366c876a7ea927d3035fdbd9dc93e9fb Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 23 May 2021 14:14:29 +0100 Subject: [PATCH] Use wxCompositeWindow for generic wxDataViewCtrl implementation This makes several methods that didn't have any effect before work correctly, including SetToolTip(), whose effect is now shown in the sample, but also SetCursor() and SetLayoutDirection(). Some methods would now actually work too well: SetForegroundColour() and SetBackgroundColour() implementations in wxCompositeWindow apply to all sub-windows, but in wxDataViewCtrl they are only supposed to affect the items, but not the header, so we need to override them to prevent the base class version from being used. It is still preferable to explicitly disable these two methods and inherit all the other ones (including any possibly added in the future) from wxCompositeWindow to implementing all the methods manually in wxDataViewCtrl itself. --- include/wx/generic/dataview.h | 13 +++++++++-- samples/dataview/dataview.cpp | 5 ++-- src/generic/datavgen.cpp | 43 +++++++++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/include/wx/generic/dataview.h b/include/wx/generic/dataview.h index b773882488..262d318696 100644 --- a/include/wx/generic/dataview.h +++ b/include/wx/generic/dataview.h @@ -12,6 +12,7 @@ #include "wx/defs.h" #include "wx/object.h" +#include "wx/compositewin.h" #include "wx/control.h" #include "wx/scrolwin.h" #include "wx/icon.h" @@ -180,8 +181,9 @@ private: // wxDataViewCtrl // --------------------------------------------------------- -class WXDLLIMPEXP_CORE wxDataViewCtrl : public wxDataViewCtrlBase, - public wxScrollHelper +class WXDLLIMPEXP_CORE wxDataViewCtrl + : public wxCompositeWindow, + public wxScrollHelper { friend class wxDataViewMainWindow; friend class wxDataViewHeaderWindowBase; @@ -192,6 +194,8 @@ class WXDLLIMPEXP_CORE wxDataViewCtrl : public wxDataViewCtrlBase, friend class wxDataViewCtrlAccessible; #endif // wxUSE_ACCESSIBILITY + typedef wxCompositeWindow BaseType; + public: wxDataViewCtrl() : wxScrollHelper(this) { @@ -264,6 +268,8 @@ public: virtual void SetFocus() wxOVERRIDE; virtual bool SetFont(const wxFont & font) wxOVERRIDE; + virtual bool SetForegroundColour(const wxColour& colour) wxOVERRIDE; + virtual bool SetBackgroundColour(const wxColour& colour) wxOVERRIDE; #if wxUSE_ACCESSIBILITY virtual bool Show(bool show = true) wxOVERRIDE; @@ -361,6 +367,9 @@ public: // utility functions not part of the API #endif // wxUSE_ACCESSIBILITY private: + // Implement pure virtual method inherited from wxCompositeWindow. + virtual wxWindowList GetCompositeWindowParts() const wxOVERRIDE; + virtual wxDataViewItem DoGetCurrentItem() const wxOVERRIDE; virtual void DoSetCurrentItem(const wxDataViewItem& item) wxOVERRIDE; diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index 41e821cb83..742002aab2 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -617,9 +617,6 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int wxSizer *firstPanelSz = new wxBoxSizer( wxVERTICAL ); m_ctrl[Page_Music]->SetMinSize(wxSize(-1, 200)); firstPanelSz->Add(m_ctrl[Page_Music], 1, wxGROW|wxALL, 5); - firstPanelSz->Add( - new wxStaticText(firstPanel, wxID_ANY, "Most of the cells above are editable!"), - 0, wxGROW|wxALL, 5); firstPanelSz->Add(button_sizer); firstPanelSz->Add(sizerCurrent); firstPanel->SetSizerAndFit(firstPanelSz); @@ -837,6 +834,8 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l // select initially the ninth symphony: m_ctrl[Page_Music]->Select(m_music_model->GetNinthItem()); + + m_ctrl[Page_Music]->SetToolTip("You may edit most of the cells here!"); } break; diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 164e2a6b0d..449f355668 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -5584,6 +5584,14 @@ bool wxDataViewCtrl::Create(wxWindow *parent, return true; } +wxWindowList wxDataViewCtrl::GetCompositeWindowParts() const +{ + wxWindowList parts; + parts.push_back(m_headerArea); // It's ok to add it even if it's null. + parts.push_back(m_clientArea); + return parts; +} + wxBorder wxDataViewCtrl::GetDefaultBorder() const { return wxBORDER_THEME; @@ -5679,15 +5687,11 @@ void wxDataViewCtrl::SetFocus() bool wxDataViewCtrl::SetFont(const wxFont & font) { - if (!wxControl::SetFont(font)) + if (!BaseType::SetFont(font)) return false; - if (m_headerArea) - m_headerArea->SetFont(font); - if (m_clientArea) { - m_clientArea->SetFont(font); m_clientArea->SetRowHeight(m_clientArea->GetDefaultRowHeight()); } @@ -5700,6 +5704,35 @@ bool wxDataViewCtrl::SetFont(const wxFont & font) return true; } +bool wxDataViewCtrl::SetForegroundColour(const wxColour& colour) +{ + // Previous versions of this class, not using wxCompositeWindow, as well as + // the native versions of this control, don't change the header foreground + // when this method is called and this could be more desirable in practice, + // as well we being more compatible, so skip calling the base class version + // that would change it as well and change only the main items area colour + // here too. + if ( !wxDataViewCtrlBase::SetForegroundColour(colour) ) + return false; + + if ( m_clientArea ) + m_clientArea->SetForegroundColour(colour); + + return true; +} + +bool wxDataViewCtrl::SetBackgroundColour(const wxColour& colour) +{ + // See SetForegroundColour() above. + if ( !wxDataViewCtrlBase::SetBackgroundColour(colour) ) + return false; + + if ( m_clientArea ) + m_clientArea->SetBackgroundColour(colour); + + return true; +} + #if wxUSE_ACCESSIBILITY bool wxDataViewCtrl::Show(bool show) {