]>
Commit | Line | Data |
---|---|---|
67e2efca RL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/dcbuffer.h | |
3 | // Purpose: wxBufferedDC class | |
4 | // Author: Ron Lee <ron@debian.org> | |
1cc8c8b7 | 5 | // Modified by: Vadim Zeitlin (refactored, added bg preservation) |
67e2efca RL |
6 | // Created: 16/03/02 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Ron Lee | |
65571936 | 9 | // Licence: wxWindows licence |
67e2efca RL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_DCBUFFER_H_ | |
13 | #define _WX_DCBUFFER_H_ | |
14 | ||
67e2efca | 15 | #include "wx/dcmemory.h" |
aeb500e6 | 16 | #include "wx/dcclient.h" |
d910fe28 | 17 | #include "wx/window.h" |
67e2efca | 18 | |
2e992e06 VZ |
19 | // Split platforms into two groups - those which have well-working |
20 | // double-buffering by default, and those which do not. | |
77f62aa7 | 21 | #if defined(__WXMAC__) || defined(__WXGTK20__) || defined(__WXDFB__) |
2e992e06 VZ |
22 | #define wxALWAYS_NATIVE_DOUBLE_BUFFER 1 |
23 | #else | |
24 | #define wxALWAYS_NATIVE_DOUBLE_BUFFER 0 | |
25 | #endif | |
26 | ||
27 | ||
1cc8c8b7 VZ |
28 | // ---------------------------------------------------------------------------- |
29 | // Double buffering helper. | |
30 | // ---------------------------------------------------------------------------- | |
67e2efca | 31 | |
265a3864 JS |
32 | // Assumes the buffer bitmap covers the entire scrolled window, |
33 | // and prepares the window DC accordingly | |
34 | #define wxBUFFER_VIRTUAL_AREA 0x01 | |
35 | ||
36 | // Assumes the buffer bitmap only covers the client area; | |
37 | // does not prepare the window DC | |
38 | #define wxBUFFER_CLIENT_AREA 0x02 | |
39 | ||
653752be | 40 | class WXDLLEXPORT wxBufferedDC : public wxMemoryDC |
1cc8c8b7 VZ |
41 | { |
42 | public: | |
43 | // Default ctor, must subsequently call Init for two stage construction. | |
b1ed79a9 VZ |
44 | wxBufferedDC() |
45 | : m_dc(NULL), | |
b1ed79a9 | 46 | m_style(0) |
1cc8c8b7 VZ |
47 | { |
48 | } | |
67e2efca | 49 | |
1cc8c8b7 | 50 | // Construct a wxBufferedDC using a user supplied buffer. |
116627da | 51 | wxBufferedDC(wxDC *dc, |
c4a34cf8 | 52 | const wxBitmap& buffer = wxNullBitmap, |
116627da | 53 | int style = wxBUFFER_CLIENT_AREA) |
1a9a3ce9 | 54 | : m_dc(NULL) |
dbd22c6a | 55 | { |
c4a34cf8 | 56 | Init(dc, buffer, style); |
dbd22c6a | 57 | } |
67e2efca | 58 | |
1cc8c8b7 VZ |
59 | // Construct a wxBufferedDC with an internal buffer of 'area' |
60 | // (where area is usually something like the size of the window | |
61 | // being buffered) | |
c4a34cf8 | 62 | wxBufferedDC(wxDC *dc, const wxSize& area, int style = wxBUFFER_CLIENT_AREA) |
1a9a3ce9 | 63 | : m_dc(NULL) |
dbd22c6a | 64 | { |
c4a34cf8 | 65 | Init(dc, area, style); |
dbd22c6a | 66 | } |
67e2efca | 67 | |
1cc8c8b7 | 68 | // The usually desired action in the dtor is to blit the buffer. |
dbd22c6a RL |
69 | virtual ~wxBufferedDC() |
70 | { | |
b1ed79a9 VZ |
71 | if ( m_dc ) |
72 | UnMask(); | |
dbd22c6a | 73 | } |
67e2efca | 74 | |
c4a34cf8 | 75 | // These reimplement the actions of the ctors for two stage creation |
116627da | 76 | void Init(wxDC *dc, |
b1ed79a9 | 77 | const wxBitmap& buffer = wxNullBitmap, |
116627da | 78 | int style = wxBUFFER_CLIENT_AREA) |
dbd22c6a | 79 | { |
b1ed79a9 VZ |
80 | InitCommon(dc, style); |
81 | ||
1a9a3ce9 | 82 | m_buffer = buffer; |
b1ed79a9 | 83 | |
dbd22c6a RL |
84 | UseBuffer(); |
85 | } | |
86 | ||
19c9f36f | 87 | void Init(wxDC *dc, const wxSize &area, int style = wxBUFFER_CLIENT_AREA) |
dbd22c6a | 88 | { |
b1ed79a9 VZ |
89 | InitCommon(dc, style); |
90 | ||
77f62aa7 | 91 | UseBuffer(area.x, area.y); |
dbd22c6a | 92 | } |
67e2efca | 93 | |
1cc8c8b7 VZ |
94 | // Blits the buffer to the dc, and detaches the dc from the buffer (so it |
95 | // can be effectively used once only). | |
96 | // | |
97 | // Usually called in the dtor or by the dtor of derived classes if the | |
98 | // BufferedDC must blit before the derived class (which may own the dc it's | |
99 | // blitting to) is destroyed. | |
19c9f36f RD |
100 | void UnMask() |
101 | { | |
1a9a3ce9 VZ |
102 | wxCHECK_RET( m_dc, _T("no underlying wxDC?") ); |
103 | wxASSERT_MSG( m_buffer.IsOk(), _T("invalid backing store") ); | |
19c9f36f | 104 | |
b1ed79a9 VZ |
105 | wxCoord x = 0, |
106 | y = 0; | |
19c9f36f | 107 | |
b1ed79a9 | 108 | if ( m_style & wxBUFFER_CLIENT_AREA ) |
19c9f36f RD |
109 | GetDeviceOrigin(&x, &y); |
110 | ||
1a9a3ce9 | 111 | m_dc->Blit(0, 0, m_buffer.GetWidth(), m_buffer.GetHeight(), |
b1ed79a9 | 112 | this, -x, -y ); |
19c9f36f RD |
113 | m_dc = NULL; |
114 | } | |
67e2efca | 115 | |
265a3864 JS |
116 | // Set and get the style |
117 | void SetStyle(int style) { m_style = style; } | |
118 | int GetStyle() const { return m_style; } | |
119 | ||
1cc8c8b7 | 120 | private: |
b1ed79a9 VZ |
121 | // common part of Init()s |
122 | void InitCommon(wxDC *dc, int style) | |
123 | { | |
1a9a3ce9 | 124 | wxASSERT_MSG( !m_dc, _T("wxBufferedDC already initialised") ); |
b1ed79a9 VZ |
125 | |
126 | m_dc = dc; | |
127 | m_style = style; | |
128 | ||
129 | // inherit the same layout direction as the original DC | |
64e4759f KO |
130 | if (dc && dc->IsOk()) |
131 | SetLayoutDirection(dc->GetLayoutDirection()); | |
b1ed79a9 VZ |
132 | } |
133 | ||
19c9f36f | 134 | // check that the bitmap is valid and use it |
77f62aa7 | 135 | void UseBuffer(wxCoord w = -1, wxCoord h = -1); |
1cc8c8b7 | 136 | |
19c9f36f RD |
137 | // the underlying DC to which we copy everything drawn on this one in |
138 | // UnMask() | |
74f4dabb VZ |
139 | // |
140 | // NB: Without the existence of a wxNullDC, this must be a pointer, else it | |
141 | // could probably be a reference. | |
19c9f36f | 142 | wxDC *m_dc; |
67e2efca | 143 | |
1a9a3ce9 VZ |
144 | // the buffer (selected in this DC), initially invalid |
145 | wxBitmap m_buffer; | |
22f3361e | 146 | |
265a3864 | 147 | // the buffering style |
19c9f36f | 148 | int m_style; |
265a3864 | 149 | |
653752be | 150 | DECLARE_DYNAMIC_CLASS(wxBufferedDC) |
22f3361e | 151 | DECLARE_NO_COPY_CLASS(wxBufferedDC) |
67e2efca RL |
152 | }; |
153 | ||
154 | ||
1cc8c8b7 VZ |
155 | // ---------------------------------------------------------------------------- |
156 | // Double buffered PaintDC. | |
157 | // ---------------------------------------------------------------------------- | |
67e2efca RL |
158 | |
159 | // Creates a double buffered wxPaintDC, optionally allowing the | |
160 | // user to specify their own buffer to use. | |
653752be | 161 | class WXDLLEXPORT wxBufferedPaintDC : public wxBufferedDC |
67e2efca | 162 | { |
67e2efca | 163 | public: |
265a3864 JS |
164 | // If no bitmap is supplied by the user, a temporary one will be created. |
165 | wxBufferedPaintDC(wxWindow *window, const wxBitmap& buffer, int style = wxBUFFER_CLIENT_AREA) | |
a66f2f42 | 166 | : m_paintdc(window) |
1cc8c8b7 | 167 | { |
265a3864 JS |
168 | // If we're buffering the virtual window, scale the paint DC as well |
169 | if (style & wxBUFFER_VIRTUAL_AREA) | |
170 | window->PrepareDC( m_paintdc ); | |
171 | ||
55ccdb93 | 172 | if( buffer.IsOk() ) |
265a3864 | 173 | Init(&m_paintdc, buffer, style); |
dbd22c6a | 174 | else |
19c9f36f | 175 | Init(&m_paintdc, window->GetClientSize(), style); |
265a3864 JS |
176 | } |
177 | ||
178 | // If no bitmap is supplied by the user, a temporary one will be created. | |
179 | wxBufferedPaintDC(wxWindow *window, int style = wxBUFFER_CLIENT_AREA) | |
180 | : m_paintdc(window) | |
181 | { | |
182 | // If we're using the virtual window, scale the paint DC as well | |
183 | if (style & wxBUFFER_VIRTUAL_AREA) | |
184 | window->PrepareDC( m_paintdc ); | |
185 | ||
19c9f36f | 186 | Init(&m_paintdc, window->GetClientSize(), style); |
1cc8c8b7 | 187 | } |
67e2efca RL |
188 | |
189 | // default copy ctor ok. | |
190 | ||
1cc8c8b7 VZ |
191 | virtual ~wxBufferedPaintDC() |
192 | { | |
193 | // We must UnMask here, else by the time the base class | |
194 | // does it, the PaintDC will have already been destroyed. | |
195 | UnMask(); | |
196 | } | |
197 | ||
198 | private: | |
1cc8c8b7 | 199 | wxPaintDC m_paintdc; |
fc7a2a60 | 200 | |
653752be | 201 | DECLARE_ABSTRACT_CLASS(wxBufferedPaintDC) |
fc7a2a60 | 202 | DECLARE_NO_COPY_CLASS(wxBufferedPaintDC) |
67e2efca RL |
203 | }; |
204 | ||
67e2efca | 205 | |
2e992e06 VZ |
206 | |
207 | // | |
208 | // wxAutoBufferedPaintDC is a wxPaintDC in toolkits which have double- | |
209 | // buffering by default. Otherwise it is a wxBufferedPaintDC. Thus, | |
210 | // you can only expect it work with a simple constructor that | |
211 | // accepts single wxWindow* argument. | |
212 | // | |
213 | #if wxALWAYS_NATIVE_DOUBLE_BUFFER | |
214 | #define wxAutoBufferedPaintDCBase wxPaintDC | |
215 | #else | |
216 | #define wxAutoBufferedPaintDCBase wxBufferedPaintDC | |
217 | #endif | |
218 | ||
219 | ||
220 | #ifdef __WXDEBUG__ | |
221 | ||
222 | class wxAutoBufferedPaintDC : public wxAutoBufferedPaintDCBase | |
223 | { | |
224 | public: | |
225 | ||
226 | wxAutoBufferedPaintDC(wxWindow* win) | |
227 | : wxAutoBufferedPaintDCBase(win) | |
228 | { | |
229 | TestWinStyle(win); | |
230 | } | |
231 | ||
232 | virtual ~wxAutoBufferedPaintDC() { } | |
233 | ||
234 | private: | |
235 | ||
236 | void TestWinStyle(wxWindow* win) | |
237 | { | |
238 | // Help the user to get the double-buffering working properly. | |
239 | wxASSERT_MSG( win->GetBackgroundStyle() == wxBG_STYLE_CUSTOM, | |
77f62aa7 | 240 | wxT("In constructor, you need to call SetBackgroundStyle(wxBG_STYLE_CUSTOM), ") |
2e992e06 VZ |
241 | wxT("and also, if needed, paint the background manually in the paint event handler.")); |
242 | } | |
243 | ||
244 | DECLARE_NO_COPY_CLASS(wxAutoBufferedPaintDC) | |
245 | }; | |
246 | ||
247 | #else // !__WXDEBUG__ | |
248 | ||
249 | // In release builds, just use typedef | |
250 | typedef wxAutoBufferedPaintDCBase wxAutoBufferedPaintDC; | |
251 | ||
252 | #endif | |
253 | ||
254 | ||
19c9f36f RD |
255 | // Check if the window is natively double buffered and will return a wxPaintDC |
256 | // if it is, a wxBufferedPaintDC otherwise. It is the caller's responsibility | |
257 | // to delete the wxDC pointer when finished with it. | |
258 | inline wxDC* wxAutoBufferedPaintDCFactory(wxWindow* window) | |
259 | { | |
260 | if ( window->IsDoubleBuffered() ) | |
261 | return new wxPaintDC(window); | |
262 | else | |
263 | return new wxBufferedPaintDC(window); | |
264 | } | |
19c9f36f | 265 | |
2e992e06 | 266 | #endif // _WX_DCBUFFER_H_ |