]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/textmeasure.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/textmeasure.cpp
3 // Purpose: wxTextMeasure implementation for wxMSW
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"
26 #include "wx/msw/private.h"
29 #include "wx/window.h"
33 #include "wx/private/textmeasure.h"
35 #include "wx/msw/dc.h"
37 // ============================================================================
38 // wxTextMeasure implementation
39 // ============================================================================
41 void wxTextMeasure::Init()
48 wxClassInfo
* const ci
= m_dc
->GetImpl()->GetClassInfo();
50 if ( ci
->IsKindOf(wxCLASSINFO(wxMSWDCImpl
)))
57 void wxTextMeasure::BeginMeasuring()
61 m_hdc
= m_dc
->GetHDC();
63 // Non-native wxDC subclasses should override their DoGetTextExtent()
65 wxASSERT_MSG( m_hdc
, wxS("Must not be used with non-native wxDCs") );
69 m_hdc
= ::GetDC(GetHwndOf(m_win
));
72 // We need to set the font if it's explicitly specified, of course, but
73 // also if we're associated with a window because the window HDC created
74 // above has the default font selected into it and not the font of the
76 if ( m_font
|| m_win
)
77 m_hfontOld
= (HFONT
)::SelectObject(m_hdc
, GetHfontOf(GetFont()));
80 void wxTextMeasure::EndMeasuring()
84 ::SelectObject(m_hdc
, m_hfontOld
);
89 ::ReleaseDC(GetHwndOf(m_win
), m_hdc
);
90 //else: our HDC belongs to m_dc, don't touch it
95 // Notice we don't check here the font. It is supposed to be OK before the call.
96 void wxTextMeasure::DoGetTextExtent(const wxString
& string
,
100 wxCoord
*externalLeading
)
103 const size_t len
= string
.length();
104 if ( !::GetTextExtentPoint32(m_hdc
, string
.t_str(), len
, &sizeRect
) )
106 wxLogLastError(wxT("GetTextExtentPoint32()"));
109 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
110 // the result computed by GetTextExtentPoint32() may be too small as it
111 // accounts for under/overhang of the first/last character while we want
112 // just the bounding rect for this string so adjust the width as needed
113 // (using API not available in 2002 SDKs of WinCE)
117 const wxChar chFirst
= *string
.begin();
118 if ( ::GetCharABCWidths(m_hdc
, chFirst
, chFirst
, &widthABC
) )
120 if ( widthABC
.abcA
< 0 )
121 sizeRect
.cx
-= widthABC
.abcA
;
125 const wxChar chLast
= *string
.rbegin();
126 ::GetCharABCWidths(m_hdc
, chLast
, chLast
, &widthABC
);
128 //else: we already have the width of the last character
130 if ( widthABC
.abcC
< 0 )
131 sizeRect
.cx
-= widthABC
.abcC
;
133 //else: GetCharABCWidths() failed, not a TrueType font?
135 #endif // !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
137 *width
= sizeRect
.cx
;
138 *height
= sizeRect
.cy
;
140 if ( descent
|| externalLeading
)
143 ::GetTextMetrics(m_hdc
, &tm
);
145 *descent
= tm
.tmDescent
;
146 if ( externalLeading
)
147 *externalLeading
= tm
.tmExternalLeading
;
151 bool wxTextMeasure::DoGetPartialTextExtents(const wxString
& text
,
156 return wxTextMeasureBase::DoGetPartialTextExtents(text
, widths
, scaleX
);
158 static int maxLenText
= -1;
159 static int maxWidth
= -1;
161 if (maxLenText
== -1)
163 // Win9x and WinNT+ have different limits
164 int version
= wxGetOsVersion();
165 maxLenText
= version
== wxOS_WINDOWS_NT
? 65535 : 8192;
166 maxWidth
= version
== wxOS_WINDOWS_NT
? INT_MAX
: 32767;
169 int len
= text
.length();
170 if ( len
> maxLenText
)
175 if ( !::GetTextExtentExPoint(m_hdc
,
176 text
.t_str(), // string to check
179 &fit
, // [out] count of chars
181 &widths
[0], // array to fill
184 wxLogLastError(wxT("GetTextExtentExPoint"));