From fd2cf1f4e282e6b834458b372f1b3217dab17bd5 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Thu, 10 Jan 2019 21:54:16 +0100 Subject: [PATCH] Fix wx[Check]ListBox font and margin on DPI change These control are drawn using a wxDC. When the DPI changes, call SetFont to update the font of the wxDC. First call wxListBoxBase::SetFont() so m_font is updated to the new DPI, then use this font in the wxDC. For wxCheckListBox update the margins to fit the changed checkbox size. --- include/wx/msw/checklst.h | 2 ++ include/wx/msw/listbox.h | 2 ++ src/msw/checklst.cpp | 15 ++++++++++++++- src/msw/listbox.cpp | 16 ++++++++++++---- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/wx/msw/checklst.h b/include/wx/msw/checklst.h index 8fa50253ba..91180c6a3e 100644 --- a/include/wx/msw/checklst.h +++ b/include/wx/msw/checklst.h @@ -81,6 +81,8 @@ protected: ProcessCommand(event); } + virtual void MSWUpdateFontOnDPIChange(const wxSize& newDPI) wxOVERRIDE; + wxSize DoGetBestClientSize() const wxOVERRIDE; wxDECLARE_EVENT_TABLE(); diff --git a/include/wx/msw/listbox.h b/include/wx/msw/listbox.h index 3ba0175320..3c910e325e 100644 --- a/include/wx/msw/listbox.h +++ b/include/wx/msw/listbox.h @@ -181,6 +181,8 @@ protected: return wxSize(w, h); } + virtual void MSWUpdateFontOnDPIChange(const wxSize& newDPI) wxOVERRIDE; + // free memory (common part of Clear() and dtor) void Free(); diff --git a/src/msw/checklst.cpp b/src/msw/checklst.cpp index 753bccc335..911dd1cb86 100644 --- a/src/msw/checklst.cpp +++ b/src/msw/checklst.cpp @@ -255,7 +255,20 @@ bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) } return false; - } +} + +void wxCheckListBox::MSWUpdateFontOnDPIChange(const wxSize& newDPI) +{ + wxCheckListBoxBase::MSWUpdateFontOnDPIChange(newDPI); + + wxSize size = wxRendererNative::Get().GetCheckBoxSize(this); + size.x += 2 * CHECKMARK_EXTRA_SPACE + CHECKMARK_LABEL_SPACE; + + for ( unsigned int i = 0; i < GetCount(); ++i ) + { + GetItem(i)->SetMarginWidth(size.GetWidth()); + } +} // check items // ----------- diff --git a/src/msw/listbox.cpp b/src/msw/listbox.cpp index 02f2ecb9e5..128f679d2c 100644 --- a/src/msw/listbox.cpp +++ b/src/msw/listbox.cpp @@ -190,6 +190,14 @@ WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const return msStyle; } +void wxListBox::MSWUpdateFontOnDPIChange(const wxSize& newDPI) +{ + wxListBoxBase::MSWUpdateFontOnDPIChange(newDPI); + + if ( m_font.IsOk() ) + SetFont(m_font); +} + void wxListBox::OnInternalIdle() { wxWindow::OnInternalIdle(); @@ -695,22 +703,22 @@ bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) bool wxListBox::SetFont(const wxFont &font) { + wxListBoxBase::SetFont(font); + if ( HasFlag(wxLB_OWNERDRAW) ) { const unsigned count = m_aItems.GetCount(); for ( unsigned i = 0; i < count; i++ ) - m_aItems[i]->SetFont(font); + m_aItems[i]->SetFont(m_font); // Non owner drawn list boxes update the item height on their own, but // we need to do it manually in the owner drawn case. wxClientDC dc(this); - dc.SetFont(font); + dc.SetFont(m_font); SendMessage(GetHwnd(), LB_SETITEMHEIGHT, 0, dc.GetCharHeight() + 2 * LISTBOX_EXTRA_SPACE); } - wxListBoxBase::SetFont(font); - return true; }