]>
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 | ||
125817d0 VZ |
40 | // Set when not using specific buffer bitmap. Note that this |
41 | // is private style and not returned by GetStyle. | |
42 | #define wxBUFFER_USES_SHARED_BUFFER 0x04 | |
43 | ||
888dde65 | 44 | class WXDLLIMPEXP_CORE wxBufferedDC : public wxMemoryDC |
1cc8c8b7 VZ |
45 | { |
46 | public: | |
47 | // Default ctor, must subsequently call Init for two stage construction. | |
b1ed79a9 VZ |
48 | wxBufferedDC() |
49 | : m_dc(NULL), | |
3754c856 | 50 | m_buffer(NULL), |
b1ed79a9 | 51 | m_style(0) |
1cc8c8b7 VZ |
52 | { |
53 | } | |
67e2efca | 54 | |
1cc8c8b7 | 55 | // Construct a wxBufferedDC using a user supplied buffer. |
116627da | 56 | wxBufferedDC(wxDC *dc, |
3754c856 | 57 | wxBitmap& buffer = wxNullBitmap, |
116627da | 58 | int style = wxBUFFER_CLIENT_AREA) |
3754c856 | 59 | : m_dc(NULL), m_buffer(NULL) |
dbd22c6a | 60 | { |
c4a34cf8 | 61 | Init(dc, buffer, style); |
dbd22c6a | 62 | } |
67e2efca | 63 | |
1cc8c8b7 VZ |
64 | // Construct a wxBufferedDC with an internal buffer of 'area' |
65 | // (where area is usually something like the size of the window | |
66 | // being buffered) | |
c4a34cf8 | 67 | wxBufferedDC(wxDC *dc, const wxSize& area, int style = wxBUFFER_CLIENT_AREA) |
3754c856 | 68 | : m_dc(NULL), m_buffer(NULL) |
dbd22c6a | 69 | { |
c4a34cf8 | 70 | Init(dc, area, style); |
dbd22c6a | 71 | } |
67e2efca | 72 | |
1cc8c8b7 | 73 | // The usually desired action in the dtor is to blit the buffer. |
dbd22c6a RL |
74 | virtual ~wxBufferedDC() |
75 | { | |
b1ed79a9 VZ |
76 | if ( m_dc ) |
77 | UnMask(); | |
dbd22c6a | 78 | } |
67e2efca | 79 | |
c4a34cf8 | 80 | // These reimplement the actions of the ctors for two stage creation |
116627da | 81 | void Init(wxDC *dc, |
3754c856 | 82 | wxBitmap& buffer = wxNullBitmap, |
116627da | 83 | int style = wxBUFFER_CLIENT_AREA) |
dbd22c6a | 84 | { |
b1ed79a9 VZ |
85 | InitCommon(dc, style); |
86 | ||
3754c856 | 87 | m_buffer = &buffer; |
b1ed79a9 | 88 | |
dbd22c6a RL |
89 | UseBuffer(); |
90 | } | |
91 | ||
19c9f36f | 92 | void Init(wxDC *dc, const wxSize &area, int style = wxBUFFER_CLIENT_AREA) |
dbd22c6a | 93 | { |
b1ed79a9 VZ |
94 | InitCommon(dc, style); |
95 | ||
77f62aa7 | 96 | UseBuffer(area.x, area.y); |
dbd22c6a | 97 | } |
67e2efca | 98 | |
1cc8c8b7 VZ |
99 | // Blits the buffer to the dc, and detaches the dc from the buffer (so it |
100 | // can be effectively used once only). | |
101 | // | |
102 | // Usually called in the dtor or by the dtor of derived classes if the | |
103 | // BufferedDC must blit before the derived class (which may own the dc it's | |
104 | // blitting to) is destroyed. | |
125817d0 | 105 | void UnMask(); |
67e2efca | 106 | |
265a3864 JS |
107 | // Set and get the style |
108 | void SetStyle(int style) { m_style = style; } | |
125817d0 | 109 | int GetStyle() const { return m_style & ~wxBUFFER_USES_SHARED_BUFFER; } |
265a3864 | 110 | |
1cc8c8b7 | 111 | private: |
b1ed79a9 VZ |
112 | // common part of Init()s |
113 | void InitCommon(wxDC *dc, int style) | |
114 | { | |
1a9a3ce9 | 115 | wxASSERT_MSG( !m_dc, _T("wxBufferedDC already initialised") ); |
b1ed79a9 VZ |
116 | |
117 | m_dc = dc; | |
118 | m_style = style; | |
119 | ||
120 | // inherit the same layout direction as the original DC | |
64e4759f KO |
121 | if (dc && dc->IsOk()) |
122 | SetLayoutDirection(dc->GetLayoutDirection()); | |
b1ed79a9 VZ |
123 | } |
124 | ||
19c9f36f | 125 | // check that the bitmap is valid and use it |
77f62aa7 | 126 | void UseBuffer(wxCoord w = -1, wxCoord h = -1); |
1cc8c8b7 | 127 | |
19c9f36f RD |
128 | // the underlying DC to which we copy everything drawn on this one in |
129 | // UnMask() | |
74f4dabb VZ |
130 | // |
131 | // NB: Without the existence of a wxNullDC, this must be a pointer, else it | |
132 | // could probably be a reference. | |
19c9f36f | 133 | wxDC *m_dc; |
67e2efca | 134 | |
1a9a3ce9 | 135 | // the buffer (selected in this DC), initially invalid |
3754c856 | 136 | wxBitmap *m_buffer; |
22f3361e | 137 | |
265a3864 | 138 | // the buffering style |
19c9f36f | 139 | int m_style; |
265a3864 | 140 | |
653752be | 141 | DECLARE_DYNAMIC_CLASS(wxBufferedDC) |
22f3361e | 142 | DECLARE_NO_COPY_CLASS(wxBufferedDC) |
67e2efca RL |
143 | }; |
144 | ||
145 | ||
1cc8c8b7 VZ |
146 | // ---------------------------------------------------------------------------- |
147 | // Double buffered PaintDC. | |
148 | // ---------------------------------------------------------------------------- | |
67e2efca RL |
149 | |
150 | // Creates a double buffered wxPaintDC, optionally allowing the | |
151 | // user to specify their own buffer to use. | |
888dde65 | 152 | class WXDLLIMPEXP_CORE wxBufferedPaintDC : public wxBufferedDC |
67e2efca | 153 | { |
67e2efca | 154 | public: |
265a3864 | 155 | // If no bitmap is supplied by the user, a temporary one will be created. |
3754c856 | 156 | wxBufferedPaintDC(wxWindow *window, wxBitmap& buffer, int style = wxBUFFER_CLIENT_AREA) |
a66f2f42 | 157 | : m_paintdc(window) |
1cc8c8b7 | 158 | { |
265a3864 JS |
159 | // If we're buffering the virtual window, scale the paint DC as well |
160 | if (style & wxBUFFER_VIRTUAL_AREA) | |
161 | window->PrepareDC( m_paintdc ); | |
162 | ||
55ccdb93 | 163 | if( buffer.IsOk() ) |
265a3864 | 164 | Init(&m_paintdc, buffer, style); |
dbd22c6a | 165 | else |
d38819f5 | 166 | Init(&m_paintdc, GetBufferedSize(window, style), style); |
265a3864 JS |
167 | } |
168 | ||
169 | // If no bitmap is supplied by the user, a temporary one will be created. | |
170 | wxBufferedPaintDC(wxWindow *window, int style = wxBUFFER_CLIENT_AREA) | |
171 | : m_paintdc(window) | |
172 | { | |
173 | // If we're using the virtual window, scale the paint DC as well | |
174 | if (style & wxBUFFER_VIRTUAL_AREA) | |
175 | window->PrepareDC( m_paintdc ); | |
176 | ||
d38819f5 | 177 | Init(&m_paintdc, GetBufferedSize(window, style), style); |
1cc8c8b7 | 178 | } |
67e2efca RL |
179 | |
180 | // default copy ctor ok. | |
181 | ||
1cc8c8b7 VZ |
182 | virtual ~wxBufferedPaintDC() |
183 | { | |
184 | // We must UnMask here, else by the time the base class | |
185 | // does it, the PaintDC will have already been destroyed. | |
186 | UnMask(); | |
187 | } | |
188 | ||
d38819f5 VZ |
189 | protected: |
190 | // return the size needed by the buffer: this depends on whether we're | |
191 | // buffering just the currently shown part or the total (scrolled) window | |
192 | static wxSize GetBufferedSize(wxWindow *window, int style) | |
193 | { | |
194 | return style & wxBUFFER_VIRTUAL_AREA ? window->GetVirtualSize() | |
195 | : window->GetClientSize(); | |
196 | } | |
197 | ||
1cc8c8b7 | 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 | ||
888dde65 | 222 | class WXDLLIMPEXP_CORE wxAutoBufferedPaintDC : public wxAutoBufferedPaintDCBase |
2e992e06 VZ |
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 | |
888dde65 | 250 | typedef WXDLLIMPEXP_CORE wxAutoBufferedPaintDCBase wxAutoBufferedPaintDC; |
2e992e06 VZ |
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_ |