From 7382e99bbb1d5c0e0c1a8d0ea97a69007473e1ab Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Apr 2022 19:07:47 +0100 Subject: [PATCH] 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. --- src/msw/bitmap.cpp | 7 +------ tests/graphics/bitmap.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 91e1c281b4..990bc08030 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -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) diff --git a/tests/graphics/bitmap.cpp b/tests/graphics/bitmap.cpp index 432f45cd95..0507ea4032 100644 --- a/tests/graphics/bitmap.cpp +++ b/tests/graphics/bitmap.cpp @@ -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)