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