Factor out text measurement from wxDC and wxWindow into wxTextMeasure.
[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 // 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 #include "wx/vector.h"
14
15 class WXDLLIMPEXP_FWD_CORE wxDC;
16 class WXDLLIMPEXP_FWD_CORE wxFont;
17 class WXDLLIMPEXP_FWD_CORE wxWindow;
18
19 // ----------------------------------------------------------------------------
20 // wxTextMeasure: class used to measure text extent.
21 // ----------------------------------------------------------------------------
22
23 class wxTextMeasureBase
24 {
25 public:
26 // The first ctor argument must be non-NULL, i.e. each object of this class
27 // is associated with either a valid wxDC or a valid wxWindow.
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 void GetLargestStringExtent(const wxVector<wxString>& strings,
51 wxCoord *width,
52 wxCoord *height);
53
54 // Fill the array with the widths for each "0..N" substrings for N from 1
55 // to text.length().
56 //
57 // The scaleX argument is the horizontal scale used by wxDC and is only
58 // used in the generic implementation.
59 bool GetPartialTextExtents(const wxString& text,
60 wxArrayInt& widths,
61 double scaleX);
62
63 protected:
64 // These functions are called by our public methods before and after each
65 // call to DoGetTextExtent(). Derived classes may override them to prepare
66 // for -- possibly several -- subsequent calls to DoGetTextExtent().
67 //
68 // As these calls must be always paired, they're never called directly but
69 // only by our friend MeasuringGuard class.
70 virtual void BeginMeasuring() { }
71 virtual void EndMeasuring() { }
72
73 // RAII wrapper for the two methods above.
74 class MeasuringGuard
75 {
76 public:
77 MeasuringGuard(wxTextMeasureBase& tm) : m_tm(tm)
78 {
79 m_tm.BeginMeasuring();
80 }
81
82 ~MeasuringGuard()
83 {
84 m_tm.EndMeasuring();
85 }
86
87 private:
88 wxTextMeasureBase& m_tm;
89 };
90
91
92 // The main function of this class, to be implemented in platform-specific
93 // way used by all our public methods except GetLargestStringExtents().
94 //
95 // The width and height pointers here are never NULL and the input string
96 // is not empty.
97 virtual void DoGetTextExtent(const wxString& string,
98 wxCoord *width,
99 wxCoord *height,
100 wxCoord *descent = NULL,
101 wxCoord *externalLeading = NULL) = 0;
102
103 // The real implementation of GetPartialTextExtents().
104 //
105 // On input, widths array contains text.length() zero elements and the text
106 // is guaranteed to be non-empty.
107 virtual bool DoGetPartialTextExtents(const wxString& text,
108 wxArrayInt& widths,
109 double scaleX) = 0;
110
111
112 // Exactly one of m_dc and m_win is non-NULL for any given object of this
113 // class.
114 const wxDC* const m_dc;
115 const wxWindow* const m_win;
116
117 // This one can be NULL or not.
118 const wxFont* const m_font;
119
120 wxDECLARE_NO_COPY_CLASS(wxTextMeasureBase);
121 };
122
123 // Include the platform dependant class declaration, if any.
124 #if defined(__WXGTK20__)
125 #include "wx/gtk/private/textmeasure.h"
126 #elif defined(__WXMSW__)
127 #include "wx/msw/private/textmeasure.h"
128 #else // no platform-specific implementation of wxTextMeasure yet
129 #include "wx/generic/private/textmeasure.h"
130
131 #define wxUSE_GENERIC_TEXTMEASURE 1
132 #endif
133
134 #ifndef wxUSE_GENERIC_TEXTMEASURE
135 #define wxUSE_GENERIC_TEXTMEASURE 0
136 #endif
137
138 #endif // _WX_PRIVATE_TEXTMEASURE_H_