]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/private/textmeasure.h | |
3 | // Purpose: declaration of wxTextMeasure class | |
4 | // Author: Manuel Martin | |
5 | // Created: 2012-10-05 | |
6 | // RCS-ID: $Id: | |
7 | // Copyright: (c) 1997-2012 wxWidgets team | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_PRIVATE_TEXTMEASURE_H_ | |
12 | #define _WX_PRIVATE_TEXTMEASURE_H_ | |
13 | ||
14 | class WXDLLIMPEXP_FWD_CORE wxDC; | |
15 | class WXDLLIMPEXP_FWD_CORE wxFont; | |
16 | class WXDLLIMPEXP_FWD_CORE wxWindow; | |
17 | ||
18 | // ---------------------------------------------------------------------------- | |
19 | // wxTextMeasure: class used to measure text extent. | |
20 | // ---------------------------------------------------------------------------- | |
21 | ||
22 | class wxTextMeasureBase | |
23 | { | |
24 | public: | |
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); | |
30 | ||
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() { } | |
34 | ||
35 | ||
36 | // Return the extent of a single line string. | |
37 | void GetTextExtent(const wxString& string, | |
38 | wxCoord *width, | |
39 | wxCoord *height, | |
40 | wxCoord *descent = NULL, | |
41 | wxCoord *externalLeading = NULL); | |
42 | ||
43 | // The same for a multiline (with '\n') string. | |
44 | void GetMultiLineTextExtent(const wxString& text, | |
45 | wxCoord *width, | |
46 | wxCoord *height, | |
47 | wxCoord *heightOneLine = NULL); | |
48 | ||
49 | // Find the dimensions of the largest string. | |
50 | wxSize GetLargestStringExtent(size_t n, const wxString* strings); | |
51 | wxSize GetLargestStringExtent(const wxArrayString& strings) | |
52 | { | |
53 | return GetLargestStringExtent(strings.size(), &strings[0]); | |
54 | } | |
55 | ||
56 | // Fill the array with the widths for each "0..N" substrings for N from 1 | |
57 | // to text.length(). | |
58 | // | |
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, | |
62 | wxArrayInt& widths, | |
63 | double scaleX); | |
64 | ||
65 | ||
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(). | |
69 | // | |
70 | // As these calls must be always paired, they're never called directly but | |
71 | // only by our friend MeasuringGuard class. | |
72 | // | |
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() { } | |
77 | ||
78 | // This is another method which is only used by MeasuringGuard. | |
79 | bool IsUsingDCImpl() const { return m_useDCImpl; } | |
80 | ||
81 | protected: | |
82 | // RAII wrapper for the two methods above. | |
83 | class MeasuringGuard | |
84 | { | |
85 | public: | |
86 | MeasuringGuard(wxTextMeasureBase& tm) : m_tm(tm) | |
87 | { | |
88 | // BeginMeasuring() should only be called if we have a native DC, | |
89 | // so don't call it if we delegate to a DC of unknown type. | |
90 | if ( !m_tm.IsUsingDCImpl() ) | |
91 | m_tm.BeginMeasuring(); | |
92 | } | |
93 | ||
94 | ~MeasuringGuard() | |
95 | { | |
96 | if ( !m_tm.IsUsingDCImpl() ) | |
97 | m_tm.EndMeasuring(); | |
98 | } | |
99 | ||
100 | private: | |
101 | wxTextMeasureBase& m_tm; | |
102 | }; | |
103 | ||
104 | ||
105 | // The main function of this class, to be implemented in platform-specific | |
106 | // way used by all our public methods. | |
107 | // | |
108 | // The width and height pointers here are never NULL and the input string | |
109 | // is not empty. | |
110 | virtual void DoGetTextExtent(const wxString& string, | |
111 | wxCoord *width, | |
112 | wxCoord *height, | |
113 | wxCoord *descent = NULL, | |
114 | wxCoord *externalLeading = NULL) = 0; | |
115 | ||
116 | // The real implementation of GetPartialTextExtents(). | |
117 | // | |
118 | // On input, widths array contains text.length() zero elements and the text | |
119 | // is guaranteed to be non-empty. | |
120 | virtual bool DoGetPartialTextExtents(const wxString& text, | |
121 | wxArrayInt& widths, | |
122 | double scaleX) = 0; | |
123 | ||
124 | // Call either DoGetTextExtent() or wxDC::GetTextExtent() depending on the | |
125 | // value of m_useDCImpl. | |
126 | // | |
127 | // This must be always used instead of calling DoGetTextExtent() directly! | |
128 | void CallGetTextExtent(const wxString& string, | |
129 | wxCoord *width, | |
130 | wxCoord *height, | |
131 | wxCoord *descent = NULL, | |
132 | wxCoord *externalLeading = NULL); | |
133 | ||
134 | // Return a valid font: if one was given to us in the ctor, use this one, | |
135 | // otherwise use the current font of the associated wxDC or wxWindow. | |
136 | wxFont GetFont() const; | |
137 | ||
138 | ||
139 | // Exactly one of m_dc and m_win is non-NULL for any given object of this | |
140 | // class. | |
141 | const wxDC* const m_dc; | |
142 | const wxWindow* const m_win; | |
143 | ||
144 | // If this is true, simply forward to wxDC::GetTextExtent() from our | |
145 | // CallGetTextExtent() instead of calling our own DoGetTextExtent(). | |
146 | // | |
147 | // We need this because our DoGetTextExtent() typically only works with | |
148 | // native DCs, i.e. those having an HDC under Windows or using Pango under | |
149 | // GTK+. However wxTextMeasure object can be constructed for any wxDC, not | |
150 | // necessarily a native one and in this case we must call back into the DC | |
151 | // implementation of text measuring itself. | |
152 | bool m_useDCImpl; | |
153 | ||
154 | // This one can be NULL or not. | |
155 | const wxFont* const m_font; | |
156 | ||
157 | wxDECLARE_NO_COPY_CLASS(wxTextMeasureBase); | |
158 | }; | |
159 | ||
160 | // Include the platform dependant class declaration, if any. | |
161 | #if defined(__WXGTK20__) | |
162 | #include "wx/gtk/private/textmeasure.h" | |
163 | #elif defined(__WXMSW__) | |
164 | #include "wx/msw/private/textmeasure.h" | |
165 | #else // no platform-specific implementation of wxTextMeasure yet | |
166 | #include "wx/generic/private/textmeasure.h" | |
167 | ||
168 | #define wxUSE_GENERIC_TEXTMEASURE 1 | |
169 | #endif | |
170 | ||
171 | #ifndef wxUSE_GENERIC_TEXTMEASURE | |
172 | #define wxUSE_GENERIC_TEXTMEASURE 0 | |
173 | #endif | |
174 | ||
175 | #endif // _WX_PRIVATE_TEXTMEASURE_H_ |