Don't store wxImage/wxBitmap objects on the heap in wxImageFileProperty

To simplify managing the life cycle of the objects.
This commit is contained in:
Artur Wieczorek
2022-04-10 13:31:14 +02:00
parent fd11c4e50a
commit 9542e86770
3 changed files with 15 additions and 23 deletions

View File

@@ -296,11 +296,11 @@ public:
const wxRect& rect, wxPGPaintData& paintdata ) wxOVERRIDE; const wxRect& rect, wxPGPaintData& paintdata ) wxOVERRIDE;
protected: protected:
wxBitmap* m_pBitmap; // final thumbnail area wxBitmap m_bitmap; // final thumbnail area
wxImage* m_pImage; // intermediate thumbnail area wxImage m_image; // intermediate thumbnail area
private: private:
// Initialize m_pImage using the current file name. // Initialize m_image using the current file name.
void LoadImageFromFile(); void LoadImageFromFile();
}; };

View File

@@ -250,8 +250,8 @@ public:
const wxRect& rect, wxPGPaintData& paintdata ); const wxRect& rect, wxPGPaintData& paintdata );
protected: protected:
wxBitmap* m_pBitmap; // final thumbnail area wxBitmap m_bitmap; // final thumbnail area
wxImage* m_pImage; // intermediate thumbnail area wxImage m_image; // intermediate thumbnail area
}; };

View File

@@ -1838,18 +1838,11 @@ wxImageFileProperty::wxImageFileProperty( const wxString& label, const wxString&
{ {
m_wildcard = wxPGGetDefaultImageWildcard(); m_wildcard = wxPGGetDefaultImageWildcard();
m_pImage = NULL;
m_pBitmap = NULL;
LoadImageFromFile(); LoadImageFromFile();
} }
wxImageFileProperty::~wxImageFileProperty() wxImageFileProperty::~wxImageFileProperty()
{ {
if ( m_pBitmap )
delete m_pBitmap;
if ( m_pImage )
delete m_pImage;
} }
void wxImageFileProperty::OnSetValue() void wxImageFileProperty::OnSetValue()
@@ -1857,8 +1850,8 @@ void wxImageFileProperty::OnSetValue()
wxFileProperty::OnSetValue(); wxFileProperty::OnSetValue();
// Delete old image // Delete old image
wxDELETE(m_pImage); m_image = wxNullImage;
wxDELETE(m_pBitmap); m_bitmap = wxNullBitmap;
LoadImageFromFile(); LoadImageFromFile();
} }
@@ -1870,7 +1863,7 @@ void wxImageFileProperty::LoadImageFromFile()
// Create the image thumbnail // Create the image thumbnail
if ( filename.FileExists() ) if ( filename.FileExists() )
{ {
m_pImage = new wxImage( filename.GetFullPath() ); m_image.LoadFile(filename.GetFullPath());
} }
} }
@@ -1883,25 +1876,24 @@ void wxImageFileProperty::OnCustomPaint( wxDC& dc,
const wxRect& rect, const wxRect& rect,
wxPGPaintData& ) wxPGPaintData& )
{ {
if ( m_pBitmap || (m_pImage && m_pImage->IsOk() ) ) if ( m_bitmap.IsOk() || m_image.IsOk() )
{ {
// Draw the thumbnail // Draw the thumbnail
// Create the bitmap here because required size is not known in OnSetValue(). // Create the bitmap here because required size is not known in OnSetValue().
// Delete the cache if required size changed // Delete the cache if required size changed
if ( m_pBitmap && (m_pBitmap->GetWidth() != rect.width || m_pBitmap->GetHeight() != rect.height) ) if ( m_bitmap.IsOk() && (m_bitmap.GetWidth() != rect.width || m_bitmap.GetHeight() != rect.height) )
{ {
delete m_pBitmap; m_bitmap = wxNullBitmap;
m_pBitmap = NULL;
} }
if ( !m_pBitmap ) if ( !m_bitmap.IsOk() )
{ {
m_pImage->Rescale( rect.width, rect.height ); m_image.Rescale( rect.width, rect.height );
m_pBitmap = new wxBitmap( *m_pImage ); m_bitmap = wxBitmap(m_image, dc);
} }
dc.DrawBitmap( *m_pBitmap, rect.x, rect.y, false ); dc.DrawBitmap( m_bitmap, rect.x, rect.y, false );
} }
else else
{ {