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