]>
git.saurik.com Git - wxWidgets.git/blob - src/common/textmeasurecmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textmeasurecmn.cpp
3 // Purpose: wxTextMeasureBase implementation
4 // Author: Manuel Martin
7 // Copyright: (c) 1997-2012 wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
24 #include "wx/window.h"
27 #include "wx/private/textmeasure.h"
29 // ============================================================================
30 // wxTextMeasureBase implementation
31 // ============================================================================
33 wxTextMeasureBase::wxTextMeasureBase(const wxDC
*dc
, const wxFont
*theFont
)
38 wxASSERT_MSG( dc
, wxS("wxTextMeasure needs a valid wxDC") );
41 wxTextMeasureBase::wxTextMeasureBase(const wxWindow
*win
, const wxFont
*theFont
)
46 wxASSERT_MSG( win
, wxS("wxTextMeasure needs a valid wxWindow") );
49 void wxTextMeasureBase::GetTextExtent(const wxString
& string
,
53 wxCoord
*externalLeading
)
55 // To make the code simpler, make sure that the width and height pointers
56 // are always valid, even if they point to dummy variables.
57 int unusedWidth
, unusedHeight
;
61 height
= &unusedHeight
;
71 MeasuringGuard
guard(*this);
73 DoGetTextExtent(string
, width
, height
, descent
, externalLeading
);
76 void wxTextMeasureBase::GetMultiLineTextExtent(const wxString
& text
,
79 wxCoord
*heightOneLine
)
81 MeasuringGuard
guard(*this);
83 wxCoord widthTextMax
= 0, widthLine
,
84 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
87 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
89 if ( pc
== text
.end() || *pc
== wxS('\n') )
91 if ( curLine
.empty() )
93 // we can't use GetTextExtent - it will return 0 for both width
94 // and height and an empty line should count in height
97 // assume that this line has the same height as the previous
99 if ( !heightLineDefault
)
100 heightLineDefault
= heightLine
;
102 if ( !heightLineDefault
)
104 // but we don't know it yet - choose something reasonable
106 DoGetTextExtent(wxS("W"), &dummy
, &heightLineDefault
);
109 heightTextTotal
+= heightLineDefault
;
113 DoGetTextExtent(curLine
, &widthLine
, &heightLine
);
114 if ( widthLine
> widthTextMax
)
115 widthTextMax
= widthLine
;
116 heightTextTotal
+= heightLine
;
119 if ( pc
== text
.end() )
135 *width
= widthTextMax
;
137 *height
= heightTextTotal
;
139 *heightOneLine
= heightLine
;
142 void wxTextMeasureBase::GetLargestStringExtent(const wxVector
<wxString
>& strings
,
146 MeasuringGuard
guard(*this);
148 wxCoord w
, h
, widthMax
= 0, heightMax
= 0;
149 for ( wxVector
<wxString
>::const_iterator i
= strings
.begin();
153 DoGetTextExtent(*i
, &w
, &h
);
167 bool wxTextMeasureBase::GetPartialTextExtents(const wxString
& text
,
175 MeasuringGuard
guard(*this);
177 widths
.Add(0, text
.length());
179 return DoGetPartialTextExtents(text
, widths
, scaleX
);