Resize wxListCtrl columns on DPI change
Fix font of custom attributes in wxListCtrl on DPI change.
This commit is contained in:
@@ -394,6 +394,10 @@ protected:
|
||||
virtual void DoSetToolTip(wxToolTip *tip) wxOVERRIDE;
|
||||
#endif // wxUSE_TOOLTIPS
|
||||
|
||||
virtual void MSWUpdateFontOnDPIChange(const wxSize& newDPI) wxOVERRIDE;
|
||||
|
||||
void OnDPIChanged(wxDPIChangedEvent& event);
|
||||
|
||||
wxSize MSWGetBestViewRect(int x, int y) const;
|
||||
|
||||
// Implement base class pure virtual methods.
|
||||
|
@@ -265,6 +265,7 @@ private:
|
||||
wxBEGIN_EVENT_TABLE(wxListCtrl, wxListCtrlBase)
|
||||
EVT_PAINT(wxListCtrl::OnPaint)
|
||||
EVT_CHAR_HOOK(wxListCtrl::OnCharHook)
|
||||
EVT_DPI_CHANGED(wxListCtrl::OnDPIChanged)
|
||||
wxEND_EVENT_TABLE()
|
||||
|
||||
// ============================================================================
|
||||
@@ -421,6 +422,46 @@ WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
|
||||
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
|
||||
// Deprecated
|
||||
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
|
||||
// uses the same font as the listview control and not the ugly
|
||||
// default GUI font by default.
|
||||
const wxFont& font = attr.HasFont() ? attr.GetFont() : GetFont();
|
||||
|
||||
// We need to tell the header about its new font to let it compute
|
||||
// its new height.
|
||||
wxSetWindowFont(hwndHdr, font);
|
||||
if ( attr.HasFont() )
|
||||
{
|
||||
wxSetWindowFont(hwndHdr, attr.GetFont());
|
||||
}
|
||||
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
|
||||
@@ -953,6 +999,12 @@ bool wxListCtrl::SetItem(wxListItem& info)
|
||||
data->attr->AssignFrom(attrNew);
|
||||
else
|
||||
data->attr = new wxItemAttr(attrNew);
|
||||
|
||||
if ( data->attr->HasFont() ) {
|
||||
wxFont f = data->attr->GetFont();
|
||||
f.WXAdjustToPPI(GetDPI());
|
||||
data->attr->SetFont(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user