]>
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
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"
25 #include "wx/msw/private.h"
28 #include "wx/window.h"
32 #include "wx/private/textmeasure.h"
34 #include "wx/msw/dc.h"
36 // ============================================================================
37 // wxTextMeasure implementation
38 // ============================================================================
40 void wxTextMeasure::Init()
47 wxClassInfo
* const ci
= m_dc
->GetImpl()->GetClassInfo();
49 if ( ci
->IsKindOf(wxCLASSINFO(wxMSWDCImpl
)))
56 void wxTextMeasure::BeginMeasuring()
60 m_hdc
= m_dc
->GetHDC();
62 // Non-native wxDC subclasses should override their DoGetTextExtent()
64 wxASSERT_MSG( m_hdc
, wxS("Must not be used with non-native wxDCs") );
68 m_hdc
= ::GetDC(GetHwndOf(m_win
));
71 // We need to set the font if it's explicitly specified, of course, but
72 // also if we're associated with a window because the window HDC created
73 // above has the default font selected into it and not the font of the
75 if ( m_font
|| m_win
)
76 m_hfontOld
= (HFONT
)::SelectObject(m_hdc
, GetHfontOf(GetFont()));
79 void wxTextMeasure::EndMeasuring()
83 ::SelectObject(m_hdc
, m_hfontOld
);
88 ::ReleaseDC(GetHwndOf(m_win
), m_hdc
);
89 //else: our HDC belongs to m_dc, don't touch it
94 // Notice we don't check here the font. It is supposed to be OK before the call.
95 void wxTextMeasure::DoGetTextExtent(const wxString
& string
,
99 wxCoord
*externalLeading
)
102 const size_t len
= string
.length();
103 if ( !::GetTextExtentPoint32(m_hdc
, string
.t_str(), len
, &sizeRect
) )
105 wxLogLastError(wxT("GetTextExtentPoint32()"));
108 #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
109 // the result computed by GetTextExtentPoint32() may be too small as it
110 // accounts for under/overhang of the first/last character while we want
111 // just the bounding rect for this string so adjust the width as needed
112 // (using API not available in 2002 SDKs of WinCE)
116 const wxChar chFirst
= *string
.begin();
117 if ( ::GetCharABCWidths(m_hdc
, chFirst
, chFirst
, &widthABC
) )
119 if ( widthABC
.abcA
< 0 )
120 sizeRect
.cx
-= widthABC
.abcA
;
124 const wxChar chLast
= *string
.rbegin();
125 ::GetCharABCWidths(m_hdc
, chLast
, chLast
, &widthABC
);
127 //else: we already have the width of the last character
129 if ( widthABC
.abcC
< 0 )
130 sizeRect
.cx
-= widthABC
.abcC
;
132 //else: GetCharABCWidths() failed, not a TrueType font?
134 #endif // !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
136 *width
= sizeRect
.cx
;
137 *height
= sizeRect
.cy
;
139 if ( descent
|| externalLeading
)
142 ::GetTextMetrics(m_hdc
, &tm
);
144 *descent
= tm
.tmDescent
;
145 if ( externalLeading
)
146 *externalLeading
= tm
.tmExternalLeading
;
150 bool wxTextMeasure::DoGetPartialTextExtents(const wxString
& text
,
155 return wxTextMeasureBase::DoGetPartialTextExtents(text
, widths
, scaleX
);
157 static int maxLenText
= -1;
158 static int maxWidth
= -1;
160 if (maxLenText
== -1)
162 // Win9x and WinNT+ have different limits
163 int version
= wxGetOsVersion();
164 maxLenText
= version
== wxOS_WINDOWS_NT
? 65535 : 8192;
165 maxWidth
= version
== wxOS_WINDOWS_NT
? INT_MAX
: 32767;
168 int len
= text
.length();
169 if ( len
> maxLenText
)
174 if ( !::GetTextExtentExPoint(m_hdc
,
175 text
.t_str(), // string to check
178 &fit
, // [out] count of chars
180 &widths
[0], // array to fill
183 wxLogLastError(wxT("GetTextExtentExPoint"));