From f31b9b27f40a84af50e4ff138c8b7e554615979e Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sun, 12 Apr 2020 17:39:39 +0200 Subject: [PATCH] Rescale wxPGProperty bitmap while drawing Property bitmap should be stored in its original size and rescaling to the required size should be done only while drawing the property. This way bitmap size will always fit to the actual wxPG line height. --- src/propgrid/property.cpp | 49 ++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/propgrid/property.cpp b/src/propgrid/property.cpp index facf98e1b0..ea207b6719 100644 --- a/src/propgrid/property.cpp +++ b/src/propgrid/property.cpp @@ -1313,7 +1313,20 @@ bool wxPGProperty::SetValueFromInt( long number, int argFlags ) wxSize wxPGProperty::OnMeasureImage( int WXUNUSED(item) ) const { if ( m_valueBitmap ) - return wxSize(m_valueBitmap->GetWidth(), wxDefaultCoord); + { + wxPropertyGrid* pg = GetGrid(); + double scale = 1.0; + if ( pg ) + { + int hMax = pg->GetImageSize().GetHeight(); + if ( m_valueBitmap->GetHeight() > hMax ) + { + scale = (double)hMax / m_valueBitmap->GetHeight(); + } + } + + return wxSize(wxRound(scale*m_valueBitmap->GetWidth()), wxDefaultCoord); + } return wxSize(0,0); } @@ -1343,11 +1356,23 @@ void wxPGProperty::OnCustomPaint( wxDC& dc, const wxRect& rect, wxPGPaintData& ) { - wxBitmap* bmp = m_valueBitmap; + wxCHECK_RET( m_valueBitmap && m_valueBitmap->IsOk(), wxS("invalid bitmap") ); - wxCHECK_RET( bmp && bmp->IsOk(), wxS("invalid bitmap") ); + wxBitmap scaledBmp; + int yOfs; + if ( m_valueBitmap->GetHeight() <= rect.height ) + { + scaledBmp = *m_valueBitmap; + yOfs = (rect.height - m_valueBitmap->GetHeight()) / 2; + } + else + { + double scale = (double)rect.height / m_valueBitmap->GetHeight(); + scaledBmp = wxPropertyGrid::RescaleBitmap(*m_valueBitmap, scale, scale); + yOfs = 0; + } - dc.DrawBitmap(*bmp,rect.x,rect.y); + dc.DrawBitmap(scaledBmp,rect.x, rect.y + yOfs); } const wxPGEditor* wxPGProperty::DoGetEditorClass() const @@ -2160,7 +2185,6 @@ bool wxPGProperty::RecreateEditor() void wxPGProperty::SetValueImage( wxBitmap& bmp ) { - // We need PG to obtain default image size wxCHECK_RET( GetGrid(), wxS("Cannot set image for detached property") ); @@ -2168,20 +2192,7 @@ void wxPGProperty::SetValueImage( wxBitmap& bmp ) if ( bmp.IsOk() ) { - // Resize the image - wxSize maxSz = GetGrid()->GetImageSize(); - wxSize imSz = bmp.GetSize(); - - if ( imSz.y != maxSz.y ) - { - double scaleY = (double)maxSz.y / (double)imSz.y; - m_valueBitmap = new wxBitmap(wxPropertyGrid::RescaleBitmap(bmp, scaleY, scaleY)); - } - else - { - m_valueBitmap = new wxBitmap(bmp); - } - + m_valueBitmap = new wxBitmap(bmp); m_flags |= wxPG_PROP_CUSTOMIMAGE; } else