Create bitmaps even when given a wxDC without an HDC in wxMSW

It seems wrong to just fail to create the bitmap entirely if a not
wxMSWDCImpl-derived wxDC is provided to wxBitmap::Create(size, wxDC)
overload, especially because no check is done to see if the associated
HDC is non-null for wxMSWDCImpl-derived classes.

Instead, simply create a DIB-based bitmap with the specified size, as
this seems more useful and less surprising and was already the behaviour
in the other ports.

Add a test ensuring that this remains the case in the future.
This commit is contained in:
Vadim Zeitlin
2022-04-10 19:07:47 +01:00
parent d08498eb16
commit 7382e99bbb
2 changed files with 12 additions and 6 deletions

View File

@@ -755,12 +755,7 @@ bool wxBitmap::Create(int width, int height, const wxDC& dc)
{
wxCHECK_MSG( dc.IsOk(), false, wxT("invalid HDC in wxBitmap::Create()") );
const wxMSWDCImpl *impl = wxDynamicCast( dc.GetImpl(), wxMSWDCImpl );
if (impl)
return DoCreate(width, height, -1, impl->GetHDC());
else
return false;
return DoCreate(width, height, -1, dc.GetHDC());
}
bool wxBitmap::CreateWithDIPSize(const wxSize& size, double scale, int depth)

View File

@@ -18,6 +18,7 @@
#include "wx/bitmap.h"
#include "wx/rawbmp.h"
#include "wx/dcmemory.h"
#include "wx/dcsvg.h"
#if wxUSE_GRAPHICS_CONTEXT
#include "wx/graphics.h"
#endif // wxUSE_GRAPHICS_CONTEXT
@@ -1690,6 +1691,16 @@ TEST_CASE("DC::Clear", "[bitmap][dc]")
}
}
TEST_CASE("Bitmap::DC", "[bitmap][dc]")
{
#if wxUSE_SVG
TempFile dummySVG("dummy.svg");
wxSVGFileDC dc(dummySVG.GetName());
wxBitmap bmp(10, 10, dc);
CHECK( bmp.IsOk() );
#endif // wxUSE_SVG
}
#if wxUSE_GRAPHICS_CONTEXT
inline void DrawScaledBmp(wxBitmap& bmp, float scale, wxGraphicsRenderer* renderer)