]> git.saurik.com Git - wxWidgets.git/blob - src/common/textmeasurecmn.cpp
Just fix header comments of wxTextMeasure-related files, no real changes.
[wxWidgets.git] / src / common / textmeasurecmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textmeasurecmn.cpp
3 // Purpose: wxTextMeasureBase implementation
4 // Author: Manuel Martin
5 // Created: 2012-10-05
6 // RCS-ID: $Id:
7 // Copyright: (c) 1997-2012 wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/dc.h"
24 #include "wx/window.h"
25 #endif //WX_PRECOMP
26
27 #include "wx/private/textmeasure.h"
28
29 // ============================================================================
30 // wxTextMeasureBase implementation
31 // ============================================================================
32
33 wxTextMeasureBase::wxTextMeasureBase(const wxDC *dc, const wxFont *theFont)
34 : m_dc(dc),
35 m_win(NULL),
36 m_font(theFont)
37 {
38 wxASSERT_MSG( dc, wxS("wxTextMeasure needs a valid wxDC") );
39 }
40
41 wxTextMeasureBase::wxTextMeasureBase(const wxWindow *win, const wxFont *theFont)
42 : m_dc(NULL),
43 m_win(win),
44 m_font(theFont)
45 {
46 wxASSERT_MSG( win, wxS("wxTextMeasure needs a valid wxWindow") );
47 }
48
49 void wxTextMeasureBase::GetTextExtent(const wxString& string,
50 wxCoord *width,
51 wxCoord *height,
52 wxCoord *descent,
53 wxCoord *externalLeading)
54 {
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;
58 if ( !width )
59 width = &unusedWidth;
60 if ( !height )
61 height = &unusedHeight;
62
63 if ( string.empty() )
64 {
65 *width =
66 *height = 0;
67
68 return;
69 }
70
71 MeasuringGuard guard(*this);
72
73 DoGetTextExtent(string, width, height, descent, externalLeading);
74 }
75
76 void wxTextMeasureBase::GetMultiLineTextExtent(const wxString& text,
77 wxCoord *width,
78 wxCoord *height,
79 wxCoord *heightOneLine)
80 {
81 MeasuringGuard guard(*this);
82
83 wxCoord widthTextMax = 0, widthLine,
84 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
85
86 wxString curLine;
87 for ( wxString::const_iterator pc = text.begin(); ; ++pc )
88 {
89 if ( pc == text.end() || *pc == wxS('\n') )
90 {
91 if ( curLine.empty() )
92 {
93 // we can't use GetTextExtent - it will return 0 for both width
94 // and height and an empty line should count in height
95 // calculation
96
97 // assume that this line has the same height as the previous
98 // one
99 if ( !heightLineDefault )
100 heightLineDefault = heightLine;
101
102 if ( !heightLineDefault )
103 {
104 // but we don't know it yet - choose something reasonable
105 int dummy;
106 DoGetTextExtent(wxS("W"), &dummy, &heightLineDefault);
107 }
108
109 heightTextTotal += heightLineDefault;
110 }
111 else
112 {
113 DoGetTextExtent(curLine, &widthLine, &heightLine);
114 if ( widthLine > widthTextMax )
115 widthTextMax = widthLine;
116 heightTextTotal += heightLine;
117 }
118
119 if ( pc == text.end() )
120 {
121 break;
122 }
123 else // '\n'
124 {
125 curLine.clear();
126 }
127 }
128 else
129 {
130 curLine += *pc;
131 }
132 }
133
134 if ( width )
135 *width = widthTextMax;
136 if ( height )
137 *height = heightTextTotal;
138 if ( heightOneLine )
139 *heightOneLine = heightLine;
140 }
141
142 void wxTextMeasureBase::GetLargestStringExtent(const wxVector<wxString>& strings,
143 wxCoord *width,
144 wxCoord *height)
145 {
146 MeasuringGuard guard(*this);
147
148 wxCoord w, h, widthMax = 0, heightMax = 0;
149 for ( wxVector<wxString>::const_iterator i = strings.begin();
150 i != strings.end();
151 ++i )
152 {
153 DoGetTextExtent(*i, &w, &h);
154
155 if ( w > widthMax )
156 widthMax = w;
157 if ( h > heightMax )
158 heightMax = h;
159 }
160
161 if ( width )
162 *width = widthMax;
163 if ( height )
164 *height = heightMax;
165 }
166
167 bool wxTextMeasureBase::GetPartialTextExtents(const wxString& text,
168 wxArrayInt& widths,
169 double scaleX)
170 {
171 widths.Empty();
172 if ( text.empty() )
173 return true;
174
175 MeasuringGuard guard(*this);
176
177 widths.Add(0, text.length());
178
179 return DoGetPartialTextExtents(text, widths, scaleX);
180 }