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