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