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.
This commit is contained in:
Maarten Bent
2019-01-10 21:54:16 +01:00
parent 123da53306
commit fd2cf1f4e2
4 changed files with 30 additions and 5 deletions

View File

@@ -81,6 +81,8 @@ protected:
ProcessCommand(event); ProcessCommand(event);
} }
virtual void MSWUpdateFontOnDPIChange(const wxSize& newDPI) wxOVERRIDE;
wxSize DoGetBestClientSize() const wxOVERRIDE; wxSize DoGetBestClientSize() const wxOVERRIDE;
wxDECLARE_EVENT_TABLE(); wxDECLARE_EVENT_TABLE();

View File

@@ -181,6 +181,8 @@ protected:
return wxSize(w, h); return wxSize(w, h);
} }
virtual void MSWUpdateFontOnDPIChange(const wxSize& newDPI) wxOVERRIDE;
// free memory (common part of Clear() and dtor) // free memory (common part of Clear() and dtor)
void Free(); void Free();

View File

@@ -257,6 +257,19 @@ bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
return false; 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 // check items
// ----------- // -----------

View File

@@ -190,6 +190,14 @@ WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
return msStyle; return msStyle;
} }
void wxListBox::MSWUpdateFontOnDPIChange(const wxSize& newDPI)
{
wxListBoxBase::MSWUpdateFontOnDPIChange(newDPI);
if ( m_font.IsOk() )
SetFont(m_font);
}
void wxListBox::OnInternalIdle() void wxListBox::OnInternalIdle()
{ {
wxWindow::OnInternalIdle(); wxWindow::OnInternalIdle();
@@ -695,22 +703,22 @@ bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
bool wxListBox::SetFont(const wxFont &font) bool wxListBox::SetFont(const wxFont &font)
{ {
wxListBoxBase::SetFont(font);
if ( HasFlag(wxLB_OWNERDRAW) ) if ( HasFlag(wxLB_OWNERDRAW) )
{ {
const unsigned count = m_aItems.GetCount(); const unsigned count = m_aItems.GetCount();
for ( unsigned i = 0; i < count; i++ ) 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 // Non owner drawn list boxes update the item height on their own, but
// we need to do it manually in the owner drawn case. // we need to do it manually in the owner drawn case.
wxClientDC dc(this); wxClientDC dc(this);
dc.SetFont(font); dc.SetFont(m_font);
SendMessage(GetHwnd(), LB_SETITEMHEIGHT, 0, SendMessage(GetHwnd(), LB_SETITEMHEIGHT, 0,
dc.GetCharHeight() + 2 * LISTBOX_EXTRA_SPACE); dc.GetCharHeight() + 2 * LISTBOX_EXTRA_SPACE);
} }
wxListBoxBase::SetFont(font);
return true; return true;
} }