Resize wxListCtrl columns on DPI change

Fix font of custom attributes in wxListCtrl on DPI change.
This commit is contained in:
Maarten Bent
2019-01-13 15:12:25 +01:00
parent 56fab0aabb
commit 2c6d132efe
2 changed files with 61 additions and 5 deletions

View File

@@ -394,6 +394,10 @@ protected:
virtual void DoSetToolTip(wxToolTip *tip) wxOVERRIDE; virtual void DoSetToolTip(wxToolTip *tip) wxOVERRIDE;
#endif // wxUSE_TOOLTIPS #endif // wxUSE_TOOLTIPS
virtual void MSWUpdateFontOnDPIChange(const wxSize& newDPI) wxOVERRIDE;
void OnDPIChanged(wxDPIChangedEvent& event);
wxSize MSWGetBestViewRect(int x, int y) const; wxSize MSWGetBestViewRect(int x, int y) const;
// Implement base class pure virtual methods. // Implement base class pure virtual methods.

View File

@@ -265,6 +265,7 @@ private:
wxBEGIN_EVENT_TABLE(wxListCtrl, wxListCtrlBase) wxBEGIN_EVENT_TABLE(wxListCtrl, wxListCtrlBase)
EVT_PAINT(wxListCtrl::OnPaint) EVT_PAINT(wxListCtrl::OnPaint)
EVT_CHAR_HOOK(wxListCtrl::OnCharHook) EVT_CHAR_HOOK(wxListCtrl::OnCharHook)
EVT_DPI_CHANGED(wxListCtrl::OnDPIChanged)
wxEND_EVENT_TABLE() wxEND_EVENT_TABLE()
// ============================================================================ // ============================================================================
@@ -421,6 +422,46 @@ WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
return wstyle; return wstyle;
} }
void wxListCtrl::MSWUpdateFontOnDPIChange(const wxSize& newDPI)
{
wxListCtrlBase::MSWUpdateFontOnDPIChange(newDPI);
for ( int i = 0; i < GetItemCount(); i++ )
{
wxMSWListItemData *data = MSWGetItemData(i);
if ( data && data->attr && data->attr->HasFont() )
{
wxFont f = data->attr->GetFont();
f.WXAdjustToPPI(newDPI);
SetItemFont(i, f);
}
}
if ( m_headerCustomDraw && m_headerCustomDraw->m_attr.HasFont() )
{
wxItemAttr item(m_headerCustomDraw->m_attr);
wxFont f = item.GetFont();
f.WXAdjustToPPI(newDPI);
item.SetFont(f);
// reset the item attribute first so wxListCtrl::SetHeaderAttr
// will detect the font change
SetHeaderAttr(wxItemAttr());
SetHeaderAttr(item);
}
}
void wxListCtrl::OnDPIChanged(wxDPIChangedEvent &event)
{
const int numCols = GetColumnCount();
for ( int i = 0; i < numCols; ++i ) {
int width = GetColumnWidth(i);
if ( width > 0 )
width = width * event.GetNewDPI().x / event.GetOldDPI().x;
SetColumnWidth(i, width);
}
}
#if WXWIN_COMPATIBILITY_3_0 #if WXWIN_COMPATIBILITY_3_0
// Deprecated // Deprecated
void wxListCtrl::UpdateStyle() void wxListCtrl::UpdateStyle()
@@ -632,11 +673,16 @@ bool wxListCtrl::SetHeaderAttr(const wxItemAttr& attr)
// Don't just reset the font if no font is specified, as the header // Don't just reset the font if no font is specified, as the header
// uses the same font as the listview control and not the ugly // uses the same font as the listview control and not the ugly
// default GUI font by default. // default GUI font by default.
const wxFont& font = attr.HasFont() ? attr.GetFont() : GetFont(); if ( attr.HasFont() )
{
// We need to tell the header about its new font to let it compute wxSetWindowFont(hwndHdr, attr.GetFont());
// its new height. }
wxSetWindowFont(hwndHdr, font); else
{
// make sure m_font is valid before using its HFONT reference
SetFont(GetFont());
wxSetWindowFont(hwndHdr, m_font);
}
} }
// Refreshing the listview makes it notice the change in height of its // Refreshing the listview makes it notice the change in height of its
@@ -953,6 +999,12 @@ bool wxListCtrl::SetItem(wxListItem& info)
data->attr->AssignFrom(attrNew); data->attr->AssignFrom(attrNew);
else else
data->attr = new wxItemAttr(attrNew); data->attr = new wxItemAttr(attrNew);
if ( data->attr->HasFont() ) {
wxFont f = data->attr->GetFont();
f.WXAdjustToPPI(GetDPI());
data->attr->SetFont(f);
}
} }
} }