]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dcbuffer.h
6916ff7eefdf2b3775b09285cf82248163ca632c
[wxWidgets.git] / include / wx / dcbuffer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/dcbuffer.h
3 // Purpose: wxBufferedDC class
4 // Author: Ron Lee <ron@debian.org>
5 // Modified by: Vadim Zeitlin (refactored, added bg preservation)
6 // Created: 16/03/02
7 // RCS-ID: $Id$
8 // Copyright: (c) Ron Lee
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DCBUFFER_H_
13 #define _WX_DCBUFFER_H_
14
15 #include "wx/dcmemory.h"
16 #include "wx/dcclient.h"
17 #include "wx/window.h"
18
19 // Split platforms into two groups - those which have well-working
20 // double-buffering by default, and those which do not.
21 #if defined(__WXMAC__) || defined(__WXGTK20__)
22 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 1
23 #else
24 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 0
25 #endif
26
27
28 // ----------------------------------------------------------------------------
29 // Double buffering helper.
30 // ----------------------------------------------------------------------------
31
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
40 class wxBufferedDC : public wxMemoryDC
41 {
42 public:
43 // Default ctor, must subsequently call Init for two stage construction.
44 wxBufferedDC() : m_dc( 0 ), m_style(0)
45 {
46 }
47
48 // Construct a wxBufferedDC using a user supplied buffer.
49 wxBufferedDC(wxDC *dc,
50 const wxBitmap &buffer = wxNullBitmap,
51 int style = wxBUFFER_CLIENT_AREA)
52 : m_dc( dc ),
53 m_buffer( buffer ),
54 m_style(style)
55 {
56 UseBuffer();
57 }
58
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)
62 wxBufferedDC(wxDC *dc, const wxSize &area, int style = wxBUFFER_CLIENT_AREA)
63 : m_dc( dc ),
64 m_buffer( area.GetWidth(), area.GetHeight() ),
65 m_style(style)
66
67 {
68 UseBuffer();
69 }
70
71 // default copy ctor ok.
72
73 // The usually desired action in the dtor is to blit the buffer.
74 virtual ~wxBufferedDC()
75 {
76 if ( m_dc ) UnMask();
77 }
78
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.
81 void Init(wxDC *dc,
82 const wxBitmap &buffer=wxNullBitmap,
83 int style = wxBUFFER_CLIENT_AREA)
84 {
85 wxASSERT_MSG( m_dc == 0 && m_buffer == wxNullBitmap,
86 _T("wxBufferedDC already initialised") );
87 m_dc = dc;
88 m_buffer = buffer;
89 m_style = style;
90 UseBuffer();
91 }
92
93 void Init(wxDC *dc, const wxSize &area, int style = wxBUFFER_CLIENT_AREA)
94 {
95 Init(dc, wxBitmap(area.GetWidth(), area.GetHeight()), style);
96 }
97
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.
104 void UnMask()
105 {
106 wxASSERT_MSG( m_dc != 0,
107 _T("No underlying DC associated with wxBufferedDC (anymore)") );
108
109 wxCoord x=0, y=0;
110
111 if (m_style & wxBUFFER_CLIENT_AREA)
112 GetDeviceOrigin(&x, &y);
113
114 m_dc->Blit( 0, 0,
115 m_buffer.GetWidth(), m_buffer.GetHeight(), this,
116 -x, -y );
117 m_dc = NULL;
118 }
119
120 // Set and get the style
121 void SetStyle(int style) { m_style = style; }
122 int GetStyle() const { return m_style; }
123
124 private:
125 // check that the bitmap is valid and use it
126 void UseBuffer()
127 {
128 if (!m_buffer.Ok())
129 {
130 wxCoord w, h;
131 m_dc->GetSize(&w, &h);
132 m_buffer = wxBitmap(w, h);
133 }
134
135 SelectObject(m_buffer);
136 }
137
138 // the underlying DC to which we copy everything drawn on this one in
139 // UnMask()
140 //
141 // NB: Without the existence of a wxNullDC, this must be a pointer, else it
142 // could probably be a reference.
143 wxDC *m_dc;
144
145 // the buffer (selected in this DC)
146 wxBitmap m_buffer;
147
148 // the buffering style
149 int m_style;
150
151 DECLARE_NO_COPY_CLASS(wxBufferedDC)
152 };
153
154
155 // ----------------------------------------------------------------------------
156 // Double buffered PaintDC.
157 // ----------------------------------------------------------------------------
158
159 // Creates a double buffered wxPaintDC, optionally allowing the
160 // user to specify their own buffer to use.
161 class wxBufferedPaintDC : public wxBufferedDC
162 {
163 public:
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)
166 : m_paintdc(window)
167 {
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
172 if( buffer != wxNullBitmap )
173 Init(&m_paintdc, buffer, style);
174 else
175 Init(&m_paintdc, window->GetClientSize(), style);
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
186 Init(&m_paintdc, window->GetClientSize(), style);
187 }
188
189 // default copy ctor ok.
190
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:
199 wxPaintDC m_paintdc;
200
201 DECLARE_NO_COPY_CLASS(wxBufferedPaintDC)
202 };
203
204
205
206 //
207 // wxAutoBufferedPaintDC is a wxPaintDC in toolkits which have double-
208 // buffering by default. Otherwise it is a wxBufferedPaintDC. Thus,
209 // you can only expect it work with a simple constructor that
210 // accepts single wxWindow* argument.
211 //
212 #if wxALWAYS_NATIVE_DOUBLE_BUFFER
213 #define wxAutoBufferedPaintDCBase wxPaintDC
214 #else
215 #define wxAutoBufferedPaintDCBase wxBufferedPaintDC
216 #endif
217
218
219 #ifdef __WXDEBUG__
220
221 class wxAutoBufferedPaintDC : public wxAutoBufferedPaintDCBase
222 {
223 public:
224
225 wxAutoBufferedPaintDC(wxWindow* win)
226 : wxAutoBufferedPaintDCBase(win)
227 {
228 TestWinStyle(win);
229 }
230
231 virtual ~wxAutoBufferedPaintDC() { }
232
233 private:
234
235 void TestWinStyle(wxWindow* win)
236 {
237 // Help the user to get the double-buffering working properly.
238 wxASSERT_MSG( win->GetBackgroundStyle() == wxBG_STYLE_CUSTOM,
239 wxT("In constructor, you need to call GetBackgroundStyle(wxBG_STYLE_CUSTOM), ")
240 wxT("and also, if needed, paint the background manually in the paint event handler."));
241 }
242
243 DECLARE_NO_COPY_CLASS(wxAutoBufferedPaintDC)
244 };
245
246 #else // !__WXDEBUG__
247
248 // In release builds, just use typedef
249 typedef wxAutoBufferedPaintDCBase wxAutoBufferedPaintDC;
250
251 #endif
252
253
254 // Check if the window is natively double buffered and will return a wxPaintDC
255 // if it is, a wxBufferedPaintDC otherwise. It is the caller's responsibility
256 // to delete the wxDC pointer when finished with it.
257 inline wxDC* wxAutoBufferedPaintDCFactory(wxWindow* window)
258 {
259 if ( window->IsDoubleBuffered() )
260 return new wxPaintDC(window);
261 else
262 return new wxBufferedPaintDC(window);
263 }
264
265
266 #endif // _WX_DCBUFFER_H_