From a5bb451448bc5abdadd4ded3f3bc18dbbf07fedd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 23 Jul 2010 23:32:52 +0000 Subject: [PATCH] Allow passing multi-line strings to wxDC::DrawText(), even under MSW. Native wxMSW wxDC::DrawText() implementation doesn't support multi-line strings so use the generic wxDC::DrawLabel() code instead. Drawing multi-line strings now works at least in wxGTK and wxMSW, to be tested for the other platforms. Closes #12239. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65058 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 2 +- interface/wx/dc.h | 7 ++++++- samples/drawing/drawing.cpp | 3 +++ src/common/dcbase.cpp | 6 ++++++ src/msw/dc.cpp | 12 ++++++++++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index cd10d56..7537912 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -406,7 +406,7 @@ Major new features in this release MSW: - Fix Cygwin 1.7 build (David Gangola). - +- Allow using wxDC::DrawText() with multiline texts. 2.9.1: ------ diff --git a/interface/wx/dc.h b/interface/wx/dc.h index 71085fb..f95103d 100644 --- a/interface/wx/dc.h +++ b/interface/wx/dc.h @@ -596,7 +596,12 @@ public: The coordinates refer to the top-left corner of the rectangle bounding the string. See GetTextExtent() for how to get the dimensions of a text - string, which can be used to position the text more precisely. + string, which can be used to position the text more precisely and + DrawLabel() if you need to align the string differently. + + Starting from wxWidgets 2.9.2 @a text parameter can be a multi-line + string, i.e. contain new line characters, and will be rendered + correctly. @note The current @ref GetLogicalFunction() "logical function" is ignored by this function. diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index 80437e1..d48e299 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -789,6 +789,9 @@ void MyCanvas::DrawText(wxDC& dc) y += height; dc.DrawRectangle( 110, y, 100, height ); dc.DrawText( wxT("Another visible text"), 110, y ); + + y += height; + dc.DrawText("And\nmore\ntext on\nmultiple\nlines", 110, y); } static const struct diff --git a/src/common/dcbase.cpp b/src/common/dcbase.cpp index 415b717..807c0ca 100644 --- a/src/common/dcbase.cpp +++ b/src/common/dcbase.cpp @@ -1205,6 +1205,12 @@ void wxDC::DrawLabel(const wxString& text, yUnderscore = 0; // split the string into lines and draw each of them separately + // + // NB: while wxDC::DrawText() on some platforms supports drawing multi-line + // strings natively, this is not the case for all of them, notably not + // wxMSW which uses this function for multi-line texts, so we may only + // call DrawText() for single-line strings from here to avoid infinite + // recursion. wxString curLine; for ( wxString::const_iterator pc = text.begin(); ; ++pc ) { diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index 675447f..298e6c8 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -1303,6 +1303,18 @@ void wxMSWDCImpl::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool void wxMSWDCImpl::DoDrawText(const wxString& text, wxCoord x, wxCoord y) { + // For compatibility with other ports (notably wxGTK) and because it's + // genuinely useful, we allow passing multiline strings to DrawText(). + // However there is no native MSW function to draw them directly so we + // instead reuse the generic DrawLabel() method to render them. Of course, + // DrawLabel() itself will call back to us but with single line strings + // only so there won't be any infinite recursion here. + if ( text.find('\n') != wxString::npos ) + { + GetOwner()->DrawLabel(text, wxRect(x, y, 0, 0)); + return; + } + WXMICROWIN_CHECK_HDC DrawAnyText(text, x, y); -- 2.7.4