]> git.saurik.com Git - wxWidgets.git/blob - include/wx/private/textmeasure.h
a5432143668db509feffe98e5829376fc65710fe
[wxWidgets.git] / include / wx / private / textmeasure.h
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.
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 protected:
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 virtual void BeginMeasuring() { }
72 virtual void EndMeasuring() { }
73
74 friend class MeasuringGuard;
75
76 // RAII wrapper for the two methods above.
77 class MeasuringGuard
78 {
79 public:
80 MeasuringGuard(wxTextMeasureBase& tm) : m_tm(tm)
81 {
82 m_tm.BeginMeasuring();
83 }
84
85 ~MeasuringGuard()
86 {
87 m_tm.EndMeasuring();
88 }
89
90 private:
91 wxTextMeasureBase& m_tm;
92 };
93
94
95 // The main function of this class, to be implemented in platform-specific
96 // way used by all our public methods.
97 //
98 // The width and height pointers here are never NULL and the input string
99 // is not empty.
100 virtual void DoGetTextExtent(const wxString& string,
101 wxCoord *width,
102 wxCoord *height,
103 wxCoord *descent = NULL,
104 wxCoord *externalLeading = NULL) = 0;
105
106 // The real implementation of GetPartialTextExtents().
107 //
108 // On input, widths array contains text.length() zero elements and the text
109 // is guaranteed to be non-empty.
110 virtual bool DoGetPartialTextExtents(const wxString& text,
111 wxArrayInt& widths,
112 double scaleX) = 0;
113
114 // Call either DoGetTextExtent() or wxDC::GetTextExtent() depending on the
115 // value of m_useDCImpl.
116 //
117 // This must be always used instead of calling DoGetTextExtent() directly!
118 void CallGetTextExtent(const wxString& string,
119 wxCoord *width,
120 wxCoord *height,
121 wxCoord *descent = NULL,
122 wxCoord *externalLeading = NULL);
123
124
125 // Exactly one of m_dc and m_win is non-NULL for any given object of this
126 // class.
127 const wxDC* const m_dc;
128 const wxWindow* const m_win;
129
130 // If this is true, simply forward to wxDC::GetTextExtent() from our
131 // CallGetTextExtent() instead of calling our own DoGetTextExtent().
132 //
133 // We need this because our DoGetTextExtent() typically only works with
134 // native DCs, i.e. those having an HDC under Windows or using Pango under
135 // GTK+. However wxTextMeasure object can be constructed for any wxDC, not
136 // necessarily a native one and in this case we must call back into the DC
137 // implementation of text measuring itself.
138 bool m_useDCImpl;
139
140 // This one can be NULL or not.
141 const wxFont* const m_font;
142
143 wxDECLARE_NO_COPY_CLASS(wxTextMeasureBase);
144 };
145
146 // Include the platform dependant class declaration, if any.
147 #if defined(__WXGTK20__)
148 #include "wx/gtk/private/textmeasure.h"
149 #elif defined(__WXMSW__)
150 #include "wx/msw/private/textmeasure.h"
151 #else // no platform-specific implementation of wxTextMeasure yet
152 #include "wx/generic/private/textmeasure.h"
153
154 #define wxUSE_GENERIC_TEXTMEASURE 1
155 #endif
156
157 #ifndef wxUSE_GENERIC_TEXTMEASURE
158 #define wxUSE_GENERIC_TEXTMEASURE 0
159 #endif
160
161 #endif // _WX_PRIVATE_TEXTMEASURE_H_