From d87abd132e037ff836e9744e1a79c265abd86b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Fri, 31 Jul 2020 16:36:50 +0200 Subject: [PATCH 1/3] Do not try to create bitmap for default constructed Caret If a Caret is default-constructed, the width and height are still 0, and the wxBitmap::Create implementations expect valid sizes. The same applies for the bitmap after a size change. --- src/generic/caret.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/generic/caret.cpp b/src/generic/caret.cpp index 0f5aa7ac51..603888570b 100644 --- a/src/generic/caret.cpp +++ b/src/generic/caret.cpp @@ -103,7 +103,8 @@ void wxCaret::InitGeneric() #ifndef wxHAS_CARET_USING_OVERLAYS m_xOld = m_yOld = -1; - m_bmpUnderCaret.Create(m_width, m_height); + if (m_width && m_height) + m_bmpUnderCaret.Create(m_width, m_height); #endif } @@ -174,7 +175,10 @@ void wxCaret::DoSize() m_overlay.Reset(); #else // Change bitmap size - m_bmpUnderCaret = wxBitmap(m_width, m_height); + if (m_width && m_height) + m_bmpUnderCaret = wxBitmap(m_width, m_height); + else + m_bmpUnderCaret = wxBitmap(); #endif if (countVisible > 0) { From b8a4b96243a3fe992c6c4840c69c16fbc3b07a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Mon, 31 Aug 2020 16:45:20 +0200 Subject: [PATCH 2/3] Create underlying bitmap for caret when it is explicitly created wxCaret::Create implies a size change, so call DoSize to initialize the underlying wxBitmap. --- include/wx/caret.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/wx/caret.h b/include/wx/caret.h index e6e9728ff8..b625b5f0ac 100644 --- a/include/wx/caret.h +++ b/include/wx/caret.h @@ -153,6 +153,7 @@ protected: m_window = window; m_width = width; m_height = height; + DoSize(); return true; } From 88f808e303c1649a4ffdeb7044b1a5dd96e6e1d3 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 1 Oct 2020 02:28:42 +0200 Subject: [PATCH 3/3] Add a unit test for wxCaret::Create() Verify that constructing wxCaret using its default ctor and Create() works too, now that it does -- previously it uses to result in a GTK warning and a crash in wxGTK. --- tests/controls/windowtest.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/controls/windowtest.cpp b/tests/controls/windowtest.cpp index 372317e67b..cfcc4a0f6d 100644 --- a/tests/controls/windowtest.cpp +++ b/tests/controls/windowtest.cpp @@ -136,10 +136,22 @@ TEST_CASE_METHOD(WindowTestCase, "Window::Mouse", "[window]") CHECK(m_window->GetCursor().IsOk()); #if wxUSE_CARET - //A plain window doesn't have a caret CHECK(!m_window->GetCaret()); - wxCaret* caret = new wxCaret(m_window, 16, 16); + wxCaret* caret; + + // Try creating the caret in two different, but normally equivalent, ways. + SECTION("Caret 1-step") + { + caret = new wxCaret(m_window, 16, 16); + } + + SECTION("Caret 2-step") + { + caret = new wxCaret(); + caret->Create(m_window, 16, 16); + } + m_window->SetCaret(caret); CHECK(m_window->GetCaret()->IsOk());