1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/textmeasure.h
3 // Purpose: declaration of wxTextMeasure class
4 // Author: Manuel Martin
7 // Copyright: (c) 1997-2012 wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_PRIVATE_TEXTMEASURE_H_
12 #define _WX_PRIVATE_TEXTMEASURE_H_
14 class WXDLLIMPEXP_FWD_CORE wxDC
;
15 class WXDLLIMPEXP_FWD_CORE wxFont
;
16 class WXDLLIMPEXP_FWD_CORE wxWindow
;
18 // ----------------------------------------------------------------------------
19 // wxTextMeasure: class used to measure text extent.
20 // ----------------------------------------------------------------------------
22 class wxTextMeasureBase
25 // The first ctor argument must be non-NULL, i.e. each object of this class
26 // is associated with either a valid wxDC or a valid wxWindow. The font can
27 // be NULL to use the current DC/window font or can be specified explicitly.
28 wxTextMeasureBase(const wxDC
*dc
, const wxFont
*theFont
);
29 wxTextMeasureBase(const wxWindow
*win
, const wxFont
*theFont
);
31 // Even though this class is not supposed to be used polymorphically, give
32 // it a virtual dtor to avoid compiler warnings.
33 virtual ~wxTextMeasureBase() { }
36 // Return the extent of a single line string.
37 void GetTextExtent(const wxString
& string
,
40 wxCoord
*descent
= NULL
,
41 wxCoord
*externalLeading
= NULL
);
43 // The same for a multiline (with '\n') string.
44 void GetMultiLineTextExtent(const wxString
& text
,
47 wxCoord
*heightOneLine
= NULL
);
49 // Find the dimensions of the largest string.
50 wxSize
GetLargestStringExtent(size_t n
, const wxString
* strings
);
51 wxSize
GetLargestStringExtent(const wxArrayString
& strings
)
53 return GetLargestStringExtent(strings
.size(), &strings
[0]);
56 // Fill the array with the widths for each "0..N" substrings for N from 1
59 // The scaleX argument is the horizontal scale used by wxDC and is only
60 // used in the generic implementation.
61 bool GetPartialTextExtents(const wxString
& text
,
66 // These functions are called by our public methods before and after each
67 // call to DoGetTextExtent(). Derived classes may override them to prepare
68 // for -- possibly several -- subsequent calls to DoGetTextExtent().
70 // As these calls must be always paired, they're never called directly but
71 // only by our friend MeasuringGuard class.
73 // NB: They're public only to allow VC6 to compile this code, there doesn't
74 // seem to be any way to give MeasuringGuard access to them (FIXME-VC6)
75 virtual void BeginMeasuring() { }
76 virtual void EndMeasuring() { }
79 // RAII wrapper for the two methods above.
83 MeasuringGuard(wxTextMeasureBase
& tm
) : m_tm(tm
)
85 m_tm
.BeginMeasuring();
94 wxTextMeasureBase
& m_tm
;
98 // The main function of this class, to be implemented in platform-specific
99 // way used by all our public methods.
101 // The width and height pointers here are never NULL and the input string
103 virtual void DoGetTextExtent(const wxString
& string
,
106 wxCoord
*descent
= NULL
,
107 wxCoord
*externalLeading
= NULL
) = 0;
109 // The real implementation of GetPartialTextExtents().
111 // On input, widths array contains text.length() zero elements and the text
112 // is guaranteed to be non-empty.
113 virtual bool DoGetPartialTextExtents(const wxString
& text
,
117 // Call either DoGetTextExtent() or wxDC::GetTextExtent() depending on the
118 // value of m_useDCImpl.
120 // This must be always used instead of calling DoGetTextExtent() directly!
121 void CallGetTextExtent(const wxString
& string
,
124 wxCoord
*descent
= NULL
,
125 wxCoord
*externalLeading
= NULL
);
127 // Return a valid font: if one was given to us in the ctor, use this one,
128 // otherwise use the current font of the associated wxDC or wxWindow.
129 wxFont
GetFont() const;
132 // Exactly one of m_dc and m_win is non-NULL for any given object of this
134 const wxDC
* const m_dc
;
135 const wxWindow
* const m_win
;
137 // If this is true, simply forward to wxDC::GetTextExtent() from our
138 // CallGetTextExtent() instead of calling our own DoGetTextExtent().
140 // We need this because our DoGetTextExtent() typically only works with
141 // native DCs, i.e. those having an HDC under Windows or using Pango under
142 // GTK+. However wxTextMeasure object can be constructed for any wxDC, not
143 // necessarily a native one and in this case we must call back into the DC
144 // implementation of text measuring itself.
147 // This one can be NULL or not.
148 const wxFont
* const m_font
;
150 wxDECLARE_NO_COPY_CLASS(wxTextMeasureBase
);
153 // Include the platform dependant class declaration, if any.
154 #if defined(__WXGTK20__)
155 #include "wx/gtk/private/textmeasure.h"
156 #elif defined(__WXMSW__)
157 #include "wx/msw/private/textmeasure.h"
158 #else // no platform-specific implementation of wxTextMeasure yet
159 #include "wx/generic/private/textmeasure.h"
161 #define wxUSE_GENERIC_TEXTMEASURE 1
164 #ifndef wxUSE_GENERIC_TEXTMEASURE
165 #define wxUSE_GENERIC_TEXTMEASURE 0
168 #endif // _WX_PRIVATE_TEXTMEASURE_H_