diff --git a/docs/changes.txt b/docs/changes.txt index 156e0bf467..e590145773 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -68,6 +68,9 @@ Changes in behaviour not resulting in compilation errors - wxGTK wxTextCtrl doesn't generate any wxEVT_TEXT when it's created with non-empty value, for consistency with the other ports. +- wxDC::GetTextExtent() returns height of 0 for empty string in wxGTK and wxOSX + too now, for consistency with wxMSW and other kinds of wxDC. + - wxMSW wxToolBar height now adapts to the height of embedded controls, making the toolbar taller if necessary, rather than making the controls smaller. To return to the previous behaviour, you need to explicitly create controls of diff --git a/interface/wx/dc.h b/interface/wx/dc.h index 9a2cbe21da..114b9cc951 100644 --- a/interface/wx/dc.h +++ b/interface/wx/dc.h @@ -954,6 +954,8 @@ public: used for the text extent calculation. Otherwise the currently selected font is. + If @a string is empty, its extent is 0 in both directions, as expected. + @note This function only works with single-line strings. @beginWxPerlOnly diff --git a/src/common/dcgraph.cpp b/src/common/dcgraph.cpp index fa5a7558be..f519943100 100644 --- a/src/common/dcgraph.cpp +++ b/src/common/dcgraph.cpp @@ -1236,7 +1236,16 @@ void wxGCDCImpl::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord * wxDouble h , d , e , w; - m_graphicContext->GetTextExtent( str, &w, &h, &d, &e ); + // Don't pass non-NULL pointers for the parts we don't need, this could + // result in doing extra unnecessary work inside GetTextExtent(). + m_graphicContext->GetTextExtent + ( + str, + width ? &w : NULL, + height ? &h : NULL, + descent ? &d : NULL, + externalLeading ? &e : NULL + ); if ( height ) *height = (wxCoord)(h+0.5); diff --git a/src/generic/graphicc.cpp b/src/generic/graphicc.cpp index b98a62ecf7..0eb6646099 100644 --- a/src/generic/graphicc.cpp +++ b/src/generic/graphicc.cpp @@ -2866,7 +2866,7 @@ void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDoub fe.height = fe.ascent + fe.descent; } - if (height) + if (height && !str.empty()) *height = fe.height; if ( descent ) *descent = fe.descent; diff --git a/tests/graphics/measuring.cpp b/tests/graphics/measuring.cpp index 723ec9778a..ad654cfe96 100644 --- a/tests/graphics/measuring.cpp +++ b/tests/graphics/measuring.cpp @@ -33,6 +33,8 @@ #include "wx/dcps.h" #include "wx/metafile.h" +#include "asserthelper.h" + // ---------------------------------------------------------------------------- // helper for XXXTextExtent() methods // ---------------------------------------------------------------------------- @@ -52,6 +54,9 @@ struct GetTextExtentTester wxSize size = obj.GetTextExtent("Hello"); CHECK( size.x > 1 ); CHECK( size.y == y ); + + // Test that getting text extent of an empty string returns (0, 0). + CHECK( obj.GetTextExtent(wxString()) == wxSize() ); } };