]>
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
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"
27 #include "wx/msw/dc.h"
30 #include "wx/window.h"
34 #include "wx/private/textmeasure.h"
36 // ============================================================================
37 // wxTextMeasure implementation
38 // ============================================================================
40 void wxTextMeasure::Init()
46 void wxTextMeasure::BeginMeasuring()
50 m_hdc
= m_dc
->GetHDC();
52 // Non-native wxDC subclasses should override their DoGetTextExtent()
54 wxASSERT_MSG( m_hdc
, wxS("Must not be used with non-native wxDCs") );
58 m_hdc
= ::GetDC(GetHwndOf(m_win
));
62 m_hfontOld
= (HFONT
)::SelectObject(m_hdc
, GetHfontOf(*m_font
));
65 void wxTextMeasure::EndMeasuring()
69 ::SelectObject(m_hdc
, m_hfontOld
);
74 ::ReleaseDC(GetHwndOf(m_win
), m_hdc
);
75 //else: our HDC belongs to m_dc, don't touch it
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
,
85 wxCoord
*externalLeading
)
88 const size_t len
= string
.length();
89 if ( !::GetTextExtentPoint32(m_hdc
, string
.t_str(), len
, &sizeRect
) )
91 wxLogLastError(wxT("GetTextExtentPoint32()"));
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)
102 const wxChar chFirst
= *string
.begin();
103 if ( ::GetCharABCWidths(m_hdc
, chFirst
, chFirst
, &widthABC
) )
105 if ( widthABC
.abcA
< 0 )
106 sizeRect
.cx
-= widthABC
.abcA
;
110 const wxChar chLast
= *string
.rbegin();
111 ::GetCharABCWidths(m_hdc
, chLast
, chLast
, &widthABC
);
113 //else: we already have the width of the last character
115 if ( widthABC
.abcC
< 0 )
116 sizeRect
.cx
-= widthABC
.abcC
;
118 //else: GetCharABCWidths() failed, not a TrueType font?
120 #endif // !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
122 *width
= sizeRect
.cx
;
123 *height
= sizeRect
.cy
;
125 if ( descent
|| externalLeading
)
128 ::GetTextMetrics(m_hdc
, &tm
);
130 *descent
= tm
.tmDescent
;
131 if ( externalLeading
)
132 *externalLeading
= tm
.tmExternalLeading
;
136 bool wxTextMeasure::DoGetPartialTextExtents(const wxString
& text
,
138 double WXUNUSED(scaleX
))
140 static int maxLenText
= -1;
141 static int maxWidth
= -1;
143 if (maxLenText
== -1)
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;
151 int len
= text
.length();
152 if ( len
> maxLenText
)
157 if ( !::GetTextExtentExPoint(m_hdc
,
158 text
.t_str(), // string to check
161 &fit
, // [out] count of chars
163 &widths
[0], // array to fill
166 wxLogLastError(wxT("GetTextExtentExPoint"));