1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBufferedDC class
4 // Author: Ron Lee <ron@debian.org>
5 // Modified by: Vadim Zeitlin (refactored, added bg preservation)
7 // Copyright: (c) Ron Lee
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_DCBUFFER_H_
12 #define _WX_DCBUFFER_H_
14 #include "wx/dcmemory.h"
15 #include "wx/dcclient.h"
16 #include "wx/window.h"
18 // Split platforms into two groups - those which have well-working
19 // double-buffering by default, and those which do not.
20 #if defined(__WXMAC__) || defined(__WXGTK20__) || defined(__WXDFB__)
21 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 1
23 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 0
27 // ----------------------------------------------------------------------------
28 // Double buffering helper.
29 // ----------------------------------------------------------------------------
31 // Assumes the buffer bitmap covers the entire scrolled window,
32 // and prepares the window DC accordingly
33 #define wxBUFFER_VIRTUAL_AREA 0x01
35 // Assumes the buffer bitmap only covers the client area;
36 // does not prepare the window DC
37 #define wxBUFFER_CLIENT_AREA 0x02
39 // Set when not using specific buffer bitmap. Note that this
40 // is private style and not returned by GetStyle.
41 #define wxBUFFER_USES_SHARED_BUFFER 0x04
43 class WXDLLIMPEXP_CORE wxBufferedDC
: public wxMemoryDC
46 // Default ctor, must subsequently call Init for two stage construction.
54 // Construct a wxBufferedDC using a user supplied buffer.
55 wxBufferedDC(wxDC
*dc
,
56 wxBitmap
& buffer
= wxNullBitmap
,
57 int style
= wxBUFFER_CLIENT_AREA
)
58 : m_dc(NULL
), m_buffer(NULL
)
60 Init(dc
, buffer
, style
);
63 // Construct a wxBufferedDC with an internal buffer of 'area'
64 // (where area is usually something like the size of the window
66 wxBufferedDC(wxDC
*dc
, const wxSize
& area
, int style
= wxBUFFER_CLIENT_AREA
)
67 : m_dc(NULL
), m_buffer(NULL
)
69 Init(dc
, area
, style
);
72 // The usually desired action in the dtor is to blit the buffer.
73 virtual ~wxBufferedDC()
79 // These reimplement the actions of the ctors for two stage creation
81 wxBitmap
& buffer
= wxNullBitmap
,
82 int style
= wxBUFFER_CLIENT_AREA
)
84 InitCommon(dc
, style
);
91 void Init(wxDC
*dc
, const wxSize
&area
, int style
= wxBUFFER_CLIENT_AREA
)
93 InitCommon(dc
, style
);
95 UseBuffer(area
.x
, area
.y
);
98 // Blits the buffer to the dc, and detaches the dc from the buffer (so it
99 // can be effectively used once only).
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.
106 // Set and get the style
107 void SetStyle(int style
) { m_style
= style
; }
108 int GetStyle() const { return m_style
& ~wxBUFFER_USES_SHARED_BUFFER
; }
111 // common part of Init()s
112 void InitCommon(wxDC
*dc
, int style
)
114 wxASSERT_MSG( !m_dc
, wxT("wxBufferedDC already initialised") );
120 // check that the bitmap is valid and use it
121 void UseBuffer(wxCoord w
= -1, wxCoord h
= -1);
123 // the underlying DC to which we copy everything drawn on this one in
126 // NB: Without the existence of a wxNullDC, this must be a pointer, else it
127 // could probably be a reference.
130 // the buffer (selected in this DC), initially invalid
133 // the buffering style
138 DECLARE_DYNAMIC_CLASS(wxBufferedDC
)
139 wxDECLARE_NO_COPY_CLASS(wxBufferedDC
);
143 // ----------------------------------------------------------------------------
144 // Double buffered PaintDC.
145 // ----------------------------------------------------------------------------
147 // Creates a double buffered wxPaintDC, optionally allowing the
148 // user to specify their own buffer to use.
149 class WXDLLIMPEXP_CORE wxBufferedPaintDC
: public wxBufferedDC
152 // If no bitmap is supplied by the user, a temporary one will be created.
153 wxBufferedPaintDC(wxWindow
*window
, wxBitmap
& buffer
, int style
= wxBUFFER_CLIENT_AREA
)
156 // If we're buffering the virtual window, scale the paint DC as well
157 if (style
& wxBUFFER_VIRTUAL_AREA
)
158 window
->PrepareDC( m_paintdc
);
161 Init(&m_paintdc
, buffer
, style
);
163 Init(&m_paintdc
, GetBufferedSize(window
, style
), style
);
166 // If no bitmap is supplied by the user, a temporary one will be created.
167 wxBufferedPaintDC(wxWindow
*window
, int style
= wxBUFFER_CLIENT_AREA
)
170 // If we're using the virtual window, scale the paint DC as well
171 if (style
& wxBUFFER_VIRTUAL_AREA
)
172 window
->PrepareDC( m_paintdc
);
174 Init(&m_paintdc
, GetBufferedSize(window
, style
), style
);
177 // default copy ctor ok.
179 virtual ~wxBufferedPaintDC()
181 // We must UnMask here, else by the time the base class
182 // does it, the PaintDC will have already been destroyed.
187 // return the size needed by the buffer: this depends on whether we're
188 // buffering just the currently shown part or the total (scrolled) window
189 static wxSize
GetBufferedSize(wxWindow
*window
, int style
)
191 return style
& wxBUFFER_VIRTUAL_AREA
? window
->GetVirtualSize()
192 : window
->GetClientSize();
198 DECLARE_ABSTRACT_CLASS(wxBufferedPaintDC
)
199 wxDECLARE_NO_COPY_CLASS(wxBufferedPaintDC
);
205 // wxAutoBufferedPaintDC is a wxPaintDC in toolkits which have double-
206 // buffering by default. Otherwise it is a wxBufferedPaintDC. Thus,
207 // you can only expect it work with a simple constructor that
208 // accepts single wxWindow* argument.
210 #if wxALWAYS_NATIVE_DOUBLE_BUFFER
211 #define wxAutoBufferedPaintDCBase wxPaintDC
213 #define wxAutoBufferedPaintDCBase wxBufferedPaintDC
216 class WXDLLIMPEXP_CORE wxAutoBufferedPaintDC
: public wxAutoBufferedPaintDCBase
220 wxAutoBufferedPaintDC(wxWindow
* win
)
221 : wxAutoBufferedPaintDCBase(win
)
223 wxASSERT_MSG( win
->GetBackgroundStyle() == wxBG_STYLE_PAINT
,
224 "You need to call SetBackgroundStyle(wxBG_STYLE_PAINT) in ctor, "
225 "and also, if needed, paint the background in wxEVT_PAINT handler."
229 virtual ~wxAutoBufferedPaintDC() { }
232 wxDECLARE_NO_COPY_CLASS(wxAutoBufferedPaintDC
);
237 // Check if the window is natively double buffered and will return a wxPaintDC
238 // if it is, a wxBufferedPaintDC otherwise. It is the caller's responsibility
239 // to delete the wxDC pointer when finished with it.
240 inline wxDC
* wxAutoBufferedPaintDCFactory(wxWindow
* window
)
242 if ( window
->IsDoubleBuffered() )
243 return new wxPaintDC(window
);
245 return new wxBufferedPaintDC(window
);
248 #endif // _WX_DCBUFFER_H_