From 56fab0aabb00513f45df6b28362d43e53479af7d Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Thu, 10 Jan 2019 22:24:45 +0100 Subject: [PATCH] Improve wxDataView on DPI change Fix the row heights after a DPI change and adjust the column widths. Use DPIChangedEvent instead of MSWUpdateFontOnDPIChange because the child controls (m_clientArea, m_headerArea) need to update their font sizes first. --- include/wx/dataview.h | 6 ++--- include/wx/generic/dataview.h | 2 ++ src/generic/datavgen.cpp | 49 ++++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/include/wx/dataview.h b/include/wx/dataview.h index 193682e6d7..f6bc5a4a93 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -895,9 +895,9 @@ public: void SetDropEffect( wxDragResult effect ) { m_dropEffect = effect; } wxDragResult GetDropEffect() const { return m_dropEffect; } // for plaforms (currently only OSX) that support Drag/Drop insertion of items, - // this is the proposed child index for the insertion - void SetProposedDropIndex(int index) { m_proposedDropIndex = index; } - int GetProposedDropIndex() const { return m_proposedDropIndex;} + // this is the proposed child index for the insertion + void SetProposedDropIndex(int index) { m_proposedDropIndex = index; } + int GetProposedDropIndex() const { return m_proposedDropIndex;} #endif // wxUSE_DRAG_AND_DROP virtual wxEvent *Clone() const wxOVERRIDE { return new wxDataViewEvent(*this); } diff --git a/include/wx/generic/dataview.h b/include/wx/generic/dataview.h index 6742e048ba..b6fc60857e 100644 --- a/include/wx/generic/dataview.h +++ b/include/wx/generic/dataview.h @@ -316,6 +316,8 @@ protected: virtual void DoEnableSystemTheme(bool enable, wxWindow* window) wxOVERRIDE; + void OnDPIChanged(wxDPIChangedEvent& event); + public: // utility functions not part of the API // returns the "best" width for the idx-th column diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index dcb6792ca5..bca358a7e0 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -193,7 +193,7 @@ int wxDataViewColumn::GetWidth() const switch ( m_width ) { case wxCOL_WIDTH_DEFAULT: - return wxDVC_DEFAULT_WIDTH; + return wxWindow::FromDIP(wxDVC_DEFAULT_WIDTH, m_owner); case wxCOL_WIDTH_AUTOSIZE: wxCHECK_MSG( m_owner, wxDVC_DEFAULT_WIDTH, "no owner control" ); @@ -709,8 +709,7 @@ public: bool Cleared(); void Resort() { - if ( m_rowHeightCache ) - m_rowHeightCache->Clear(); + ClearRowHeightCache(); if (!IsVirtualList()) { @@ -718,6 +717,11 @@ public: } UpdateDisplay(); } + void ClearRowHeightCache() + { + if ( m_rowHeightCache ) + m_rowHeightCache->Clear(); + } SortOrder GetSortOrder() const { @@ -1188,7 +1192,8 @@ wxSize wxDataViewTextRenderer::GetSize() const return GetTextExtent(m_text); } else - return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); + return GetView()->FromDIP(wxSize(wxDVC_DEFAULT_RENDERER_SIZE, + wxDVC_DEFAULT_RENDERER_SIZE)); } // --------------------------------------------------------- @@ -1251,7 +1256,8 @@ wxSize wxDataViewBitmapRenderer::GetSize() const else if (m_icon.IsOk()) return wxSize( m_icon.GetWidth(), m_icon.GetHeight() ); - return wxSize(wxDVC_DEFAULT_RENDERER_SIZE,wxDVC_DEFAULT_RENDERER_SIZE); + return GetView()->FromDIP(wxSize(wxDVC_DEFAULT_RENDERER_SIZE, + wxDVC_DEFAULT_RENDERER_SIZE)); } // --------------------------------------------------------- @@ -2770,11 +2776,8 @@ bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem & parent, const wxData } else { - if ( m_rowHeightCache ) - { - // specific position (row) is unclear, so clear whole height cache - m_rowHeightCache->Clear(); - } + // specific position (row) is unclear, so clear whole height cache + ClearRowHeightCache(); wxDataViewTreeNode *parentNode = FindNode(parent); @@ -3039,8 +3042,7 @@ bool wxDataViewMainWindow::Cleared() m_selection.Clear(); m_currentRow = (unsigned)-1; - if ( m_rowHeightCache ) - m_rowHeightCache->Clear(); + ClearRowHeightCache(); if (GetModel()) { @@ -5107,6 +5109,7 @@ void wxDataViewMainWindow::UpdateColumnSizes() wxIMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase); wxBEGIN_EVENT_TABLE(wxDataViewCtrl, wxDataViewCtrlBase) EVT_SIZE(wxDataViewCtrl::OnSize) + EVT_DPI_CHANGED(wxDataViewCtrl::OnDPIChanged) wxEND_EVENT_TABLE() wxDataViewCtrl::~wxDataViewCtrl() @@ -5251,6 +5254,28 @@ void wxDataViewCtrl::OnSize( wxSizeEvent &WXUNUSED(event) ) } } +void wxDataViewCtrl::OnDPIChanged(wxDPIChangedEvent& event) +{ + if ( m_clientArea ) + { + m_clientArea->ClearRowHeightCache(); + m_clientArea->SetRowHeight(m_clientArea->GetDefaultRowHeight()); + } + + for ( unsigned i = 0; i < m_cols.size(); ++i ) + { + int minWidth = m_cols[i]->GetMinWidth(); + if ( minWidth > 0 ) + minWidth = minWidth * event.GetNewDPI().x / event.GetOldDPI().x; + m_cols[i]->SetMinWidth(minWidth); + + int width = m_cols[i]->WXGetManuallySetWidth(); + if ( width > 0 ) + width = width * event.GetNewDPI().x / event.GetOldDPI().x; + m_cols[i]->SetWidth(width); + } +} + void wxDataViewCtrl::SetFocus() { if (m_clientArea)