From d57c688d8928e3fb8144e5bb0a01efa2aa36ca38 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 15 Jul 2020 01:23:29 +0200 Subject: [PATCH] Return non-zero height from GetMultiLineTextExtent("") in wxMSW This restores the previous behaviour inadvertently changed by bfeae1922d (Minor optimizations in GetMultiLineTextExtent(), 2020-06-10) and makes it official by documenting it and adding tests checking for it. It wasn't completely obviously if this was intentional or accidental before, but at least wxStaticText itself relied on the old behaviour, and chances are that so did some code outside the library, so make this part of the API now. See #18825. --- interface/wx/dc.h | 6 ++++++ src/common/textmeasurecmn.cpp | 8 +++++++- tests/graphics/measuring.cpp | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/interface/wx/dc.h b/interface/wx/dc.h index 114b9cc951..527d97eb64 100644 --- a/interface/wx/dc.h +++ b/interface/wx/dc.h @@ -892,6 +892,12 @@ public: used for the text extent calculation, otherwise the currently selected font is used. + If @a string is empty, its horizontal extent is 0 but, for convenience + when using this function for allocating enough space for a possibly + multi-line string, its vertical extent is the same as the height of an + empty line of text. Please note that this behaviour differs from that + of GetTextExtent(). + @note This function works with both single-line and multi-line strings. @beginWxPerlOnly diff --git a/src/common/textmeasurecmn.cpp b/src/common/textmeasurecmn.cpp index e060fb18b5..8ad8e0a312 100644 --- a/src/common/textmeasurecmn.cpp +++ b/src/common/textmeasurecmn.cpp @@ -129,7 +129,13 @@ void wxTextMeasureBase::GetMultiLineTextExtent(const wxString& text, // actually multiline specially here, to skip iteration above in this case. if ( text.find('\n') == wxString::npos ) { - CallGetTextExtent(text, width, height); + // This case needs to be handled specially as we're supposed to return + // a non-zero height even for empty string. + if ( text.empty() ) + *height = GetEmptyLineHeight(); + else + CallGetTextExtent(text, width, height); + if ( heightOneLine ) *heightOneLine = *height; return; diff --git a/tests/graphics/measuring.cpp b/tests/graphics/measuring.cpp index ad654cfe96..bb185247af 100644 --- a/tests/graphics/measuring.cpp +++ b/tests/graphics/measuring.cpp @@ -77,6 +77,12 @@ TEST_CASE("wxDC::GetTextExtent", "[dc][text-extent]") CHECK( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y ); + // Check that empty lines get counted + CHECK( dc.GetMultiLineTextExtent("\n\n\n").y >= 3*sz.y ); + + // And even empty strings count like one line. + CHECK( dc.GetMultiLineTextExtent(wxString()) == wxSize(0, sz.y) ); + // Test the functions with some other DC kinds also. #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT wxPostScriptDC psdc;