From 5e761ad99f4c83c8fb0d624ba47d4d7b1f3cc03e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 31 Mar 2020 16:59:49 +0200 Subject: [PATCH] Add minimal DPI change handler to wxGrid This handler redoes wxGrid layout and refreshes it to at least avoid ugly display artifacts when moving wxGrid window between displays with different DPI. --- include/wx/generic/grid.h | 7 +++++++ src/generic/grid.cpp | 21 +++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index 3e728afa23..c88b2a6122 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -2415,6 +2415,13 @@ protected: friend class wxGridHeaderCtrl; private: + // This is called from both Create() and OnDPIChanged() to (re)initialize + // the values in pixels, which depend on the current DPI. + void InitPixelFields(); + + // Event handler for DPI change event recomputes pixel values and relays + // out the grid. + void OnDPIChanged(wxDPIChangedEvent& event); // implement wxScrolledCanvas method to return m_gridWin size virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size) wxOVERRIDE; diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index bcb3ca6a98..1036e1bee6 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -2317,6 +2317,7 @@ void wxGridWindow::OnFocus(wxFocusEvent& event) wxBEGIN_EVENT_TABLE( wxGrid, wxScrolledCanvas ) EVT_SIZE( wxGrid::OnSize ) + EVT_DPI_CHANGED( wxGrid::OnDPIChanged ) EVT_KEY_DOWN( wxGrid::OnKeyDown ) EVT_KEY_UP( wxGrid::OnKeyUp ) EVT_CHAR ( wxGrid::OnChar ) @@ -2457,8 +2458,11 @@ void wxGrid::Create() m_labelBackgroundColour = m_rowLabelWin->GetBackgroundColour(); m_labelTextColour = m_rowLabelWin->GetForegroundColour(); - // now that we have the grid window, use its font to compute the default - // row height + InitPixelFields(); +} + +void wxGrid::InitPixelFields() +{ m_defaultRowHeight = m_gridWin->GetCharHeight(); #if defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXQT__) // see also text ctrl sizing in ShowCellEditControl() m_defaultRowHeight += 8; @@ -5346,6 +5350,19 @@ void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event)) } } +void wxGrid::OnDPIChanged(wxDPIChangedEvent& event) +{ + InitPixelFields(); + + InvalidateBestSize(); + + CalcWindowSizes(); + + Refresh(); + + event.Skip(); +} + void wxGrid::OnKeyDown( wxKeyEvent& event ) { if ( m_inOnKeyDown )