]>
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
6 // Copyright: (c) 1997-2012 wxWidgets team
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
23 #include "wx/window.h"
26 #include "wx/private/textmeasure.h"
28 // ============================================================================
29 // wxTextMeasureBase implementation
30 // ============================================================================
32 wxTextMeasureBase::wxTextMeasureBase(const wxDC
*dc
, const wxFont
*theFont
)
37 wxASSERT_MSG( dc
, wxS("wxTextMeasure needs a valid wxDC") );
40 wxTextMeasureBase::wxTextMeasureBase(const wxWindow
*win
, const wxFont
*theFont
)
45 wxASSERT_MSG( win
, wxS("wxTextMeasure needs a valid wxWindow") );
48 void wxTextMeasureBase::GetTextExtent(const wxString
& string
,
52 wxCoord
*externalLeading
)
54 // To make the code simpler, make sure that the width and height pointers
55 // are always valid, even if they point to dummy variables.
56 int unusedWidth
, unusedHeight
;
60 height
= &unusedHeight
;
70 MeasuringGuard
guard(*this);
72 DoGetTextExtent(string
, width
, height
, descent
, externalLeading
);
75 void wxTextMeasureBase::GetMultiLineTextExtent(const wxString
& text
,
78 wxCoord
*heightOneLine
)
80 MeasuringGuard
guard(*this);
82 wxCoord widthTextMax
= 0, widthLine
,
83 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
86 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
88 if ( pc
== text
.end() || *pc
== wxS('\n') )
90 if ( curLine
.empty() )
92 // we can't use GetTextExtent - it will return 0 for both width
93 // and height and an empty line should count in height
96 // assume that this line has the same height as the previous
98 if ( !heightLineDefault
)
99 heightLineDefault
= heightLine
;
101 if ( !heightLineDefault
)
103 // but we don't know it yet - choose something reasonable
105 DoGetTextExtent(wxS("W"), &dummy
, &heightLineDefault
);
108 heightTextTotal
+= heightLineDefault
;
112 DoGetTextExtent(curLine
, &widthLine
, &heightLine
);
113 if ( widthLine
> widthTextMax
)
114 widthTextMax
= widthLine
;
115 heightTextTotal
+= heightLine
;
118 if ( pc
== text
.end() )
134 *width
= widthTextMax
;
136 *height
= heightTextTotal
;
138 *heightOneLine
= heightLine
;
141 void wxTextMeasureBase::GetLargestStringExtent(const wxVector
<wxString
>& strings
,
145 MeasuringGuard
guard(*this);
147 wxCoord w
, h
, widthMax
= 0, heightMax
= 0;
148 for ( wxVector
<wxString
>::const_iterator i
= strings
.begin();
152 DoGetTextExtent(*i
, &w
, &h
);
166 bool wxTextMeasureBase::GetPartialTextExtents(const wxString
& text
,
174 MeasuringGuard
guard(*this);
176 widths
.Add(0, text
.length());
178 return DoGetPartialTextExtents(text
, widths
, scaleX
);