]>
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") );
40 // By default, use wxDC version, we'll explicitly reset this to false in
41 // the derived classes if the DC is of native variety.
45 wxTextMeasureBase::wxTextMeasureBase(const wxWindow
*win
, const wxFont
*theFont
)
50 wxASSERT_MSG( win
, wxS("wxTextMeasure needs a valid wxWindow") );
52 // We don't have any wxDC so we can't forward to it.
56 void wxTextMeasureBase::CallGetTextExtent(const wxString
& string
,
60 wxCoord
*externalLeading
)
63 m_dc
->GetTextExtent(string
, width
, height
, descent
, externalLeading
);
65 DoGetTextExtent(string
, width
, height
, descent
, externalLeading
);
68 void wxTextMeasureBase::GetTextExtent(const wxString
& string
,
72 wxCoord
*externalLeading
)
74 // To make the code simpler, make sure that the width and height pointers
75 // are always valid, even if they point to dummy variables.
76 int unusedWidth
, unusedHeight
;
80 height
= &unusedHeight
;
90 MeasuringGuard
guard(*this);
92 CallGetTextExtent(string
, width
, height
, descent
, externalLeading
);
95 void wxTextMeasureBase::GetMultiLineTextExtent(const wxString
& text
,
98 wxCoord
*heightOneLine
)
100 MeasuringGuard
guard(*this);
102 wxCoord widthTextMax
= 0, widthLine
,
103 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
106 for ( wxString::const_iterator pc
= text
.begin(); ; ++pc
)
108 if ( pc
== text
.end() || *pc
== wxS('\n') )
110 if ( curLine
.empty() )
112 // we can't use GetTextExtent - it will return 0 for both width
113 // and height and an empty line should count in height
116 // assume that this line has the same height as the previous
118 if ( !heightLineDefault
)
119 heightLineDefault
= heightLine
;
121 if ( !heightLineDefault
)
123 // but we don't know it yet - choose something reasonable
125 CallGetTextExtent(wxS("W"), &dummy
, &heightLineDefault
);
128 heightTextTotal
+= heightLineDefault
;
132 CallGetTextExtent(curLine
, &widthLine
, &heightLine
);
133 if ( widthLine
> widthTextMax
)
134 widthTextMax
= widthLine
;
135 heightTextTotal
+= heightLine
;
138 if ( pc
== text
.end() )
154 *width
= widthTextMax
;
156 *height
= heightTextTotal
;
158 *heightOneLine
= heightLine
;
161 wxSize
wxTextMeasureBase::GetLargestStringExtent(size_t n
,
162 const wxString
* strings
)
164 MeasuringGuard
guard(*this);
166 wxCoord w
, h
, widthMax
= 0, heightMax
= 0;
167 for ( size_t i
= 0; i
< n
; ++i
)
169 CallGetTextExtent(strings
[i
], &w
, &h
);
177 return wxSize(widthMax
, heightMax
);
180 bool wxTextMeasureBase::GetPartialTextExtents(const wxString
& text
,
188 MeasuringGuard
guard(*this);
190 widths
.Add(0, text
.length());
192 return DoGetPartialTextExtents(text
, widths
, scaleX
);