]> git.saurik.com Git - wxWidgets.git/blob - src/msw/textmeasure.cpp
Fix crash in wxDC::GetMultiLineTextExtent() after last commit.
[wxWidgets.git] / src / msw / textmeasure.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/textmeasure.cpp
3 // Purpose: wxTextMeasure implementation for wxMSW
4 // Author: Manuel Martin
5 // Created: 2012-19-05
6 // RCS-ID:
7 // Copyright:
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 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/msw/private.h"
27 #include "wx/msw/dc.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/window.h"
31 #include "wx/font.h"
32 #endif //WX_PRECOMP
33
34 #include "wx/private/textmeasure.h"
35
36 // ============================================================================
37 // wxTextMeasure implementation
38 // ============================================================================
39
40 void wxTextMeasure::Init()
41 {
42 m_hdc = NULL;
43 m_hfontOld = NULL;
44 }
45
46 void wxTextMeasure::BeginMeasuring()
47 {
48 if ( m_dc )
49 {
50 m_hdc = m_dc->GetHDC();
51
52 // Non-native wxDC subclasses should override their DoGetTextExtent()
53 // and other methods.
54 wxASSERT_MSG( m_hdc, wxS("Must not be used with non-native wxDCs") );
55 }
56 else if ( m_win )
57 {
58 m_hdc = ::GetDC(GetHwndOf(m_win));
59 }
60
61 if ( m_font )
62 m_hfontOld = (HFONT)::SelectObject(m_hdc, GetHfontOf(*m_font));
63 }
64
65 void wxTextMeasure::EndMeasuring()
66 {
67 if ( m_hfontOld )
68 {
69 ::SelectObject(m_hdc, m_hfontOld);
70 m_hfontOld = NULL;
71 }
72
73 if ( m_win )
74 ::ReleaseDC(GetHwndOf(m_win), m_hdc);
75 //else: our HDC belongs to m_dc, don't touch it
76
77 m_hdc = NULL;
78 }
79
80 // Notice we don't check here the font. It is supposed to be OK before the call.
81 void wxTextMeasure::DoGetTextExtent(const wxString& string,
82 wxCoord *width,
83 wxCoord *height,
84 wxCoord *descent,
85 wxCoord *externalLeading)
86 {
87 SIZE sizeRect;
88 const size_t len = string.length();
89 if ( !::GetTextExtentPoint32(m_hdc, string.t_str(), len, &sizeRect) )
90 {
91 wxLogLastError(wxT("GetTextExtentPoint32()"));
92 }
93
94 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
95 // the result computed by GetTextExtentPoint32() may be too small as it
96 // accounts for under/overhang of the first/last character while we want
97 // just the bounding rect for this string so adjust the width as needed
98 // (using API not available in 2002 SDKs of WinCE)
99 if ( len > 0 )
100 {
101 ABC widthABC;
102 const wxChar chFirst = *string.begin();
103 if ( ::GetCharABCWidths(m_hdc, chFirst, chFirst, &widthABC) )
104 {
105 if ( widthABC.abcA < 0 )
106 sizeRect.cx -= widthABC.abcA;
107
108 if ( len > 1 )
109 {
110 const wxChar chLast = *string.rbegin();
111 ::GetCharABCWidths(m_hdc, chLast, chLast, &widthABC);
112 }
113 //else: we already have the width of the last character
114
115 if ( widthABC.abcC < 0 )
116 sizeRect.cx -= widthABC.abcC;
117 }
118 //else: GetCharABCWidths() failed, not a TrueType font?
119 }
120 #endif // !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
121
122 *width = sizeRect.cx;
123 *height = sizeRect.cy;
124
125 if ( descent || externalLeading )
126 {
127 TEXTMETRIC tm;
128 ::GetTextMetrics(m_hdc, &tm);
129 if ( descent )
130 *descent = tm.tmDescent;
131 if ( externalLeading )
132 *externalLeading = tm.tmExternalLeading;
133 }
134 }
135
136 bool wxTextMeasure::DoGetPartialTextExtents(const wxString& text,
137 wxArrayInt& widths,
138 double WXUNUSED(scaleX))
139 {
140 static int maxLenText = -1;
141 static int maxWidth = -1;
142
143 if (maxLenText == -1)
144 {
145 // Win9x and WinNT+ have different limits
146 int version = wxGetOsVersion();
147 maxLenText = version == wxOS_WINDOWS_NT ? 65535 : 8192;
148 maxWidth = version == wxOS_WINDOWS_NT ? INT_MAX : 32767;
149 }
150
151 int len = text.length();
152 if ( len > maxLenText )
153 len = maxLenText;
154
155 int fit = 0;
156 SIZE sz = {0,0};
157 if ( !::GetTextExtentExPoint(m_hdc,
158 text.t_str(), // string to check
159 len,
160 maxWidth,
161 &fit, // [out] count of chars
162 // that will fit
163 &widths[0], // array to fill
164 &sz) )
165 {
166 wxLogLastError(wxT("GetTextExtentExPoint"));
167
168 return false;
169 }
170
171 return true;
172 }