1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxBufferedDC class
4 // Author: Ron Lee <ron@debian.org>
5 // Modified by: Vadim Zeitlin (refactored, added bg preservation)
8 // Copyright: (c) Ron Lee
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DCBUFFER_H_
13 #define _WX_DCBUFFER_H_
15 #include "wx/dcmemory.h"
16 #include "wx/dcclient.h"
17 #include "wx/window.h"
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__) || defined(__WXDFB__)
22 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 1
24 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 0
28 // ----------------------------------------------------------------------------
29 // Double buffering helper.
30 // ----------------------------------------------------------------------------
32 // Assumes the buffer bitmap covers the entire scrolled window,
33 // and prepares the window DC accordingly
34 #define wxBUFFER_VIRTUAL_AREA 0x01
36 // Assumes the buffer bitmap only covers the client area;
37 // does not prepare the window DC
38 #define wxBUFFER_CLIENT_AREA 0x02
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
44 class WXDLLIMPEXP_CORE wxBufferedDC
: public wxMemoryDC
47 // Default ctor, must subsequently call Init for two stage construction.
55 // Construct a wxBufferedDC using a user supplied buffer.
56 wxBufferedDC(wxDC
*dc
,
57 wxBitmap
& buffer
= wxNullBitmap
,
58 int style
= wxBUFFER_CLIENT_AREA
)
59 : m_dc(NULL
), m_buffer(NULL
)
61 Init(dc
, buffer
, style
);
64 // Construct a wxBufferedDC with an internal buffer of 'area'
65 // (where area is usually something like the size of the window
67 wxBufferedDC(wxDC
*dc
, const wxSize
& area
, int style
= wxBUFFER_CLIENT_AREA
)
68 : m_dc(NULL
), m_buffer(NULL
)
70 Init(dc
, area
, style
);
73 // The usually desired action in the dtor is to blit the buffer.
74 virtual ~wxBufferedDC()
80 // These reimplement the actions of the ctors for two stage creation
82 wxBitmap
& buffer
= wxNullBitmap
,
83 int style
= wxBUFFER_CLIENT_AREA
)
85 InitCommon(dc
, style
);
92 void Init(wxDC
*dc
, const wxSize
&area
, int style
= wxBUFFER_CLIENT_AREA
)
94 InitCommon(dc
, style
);
96 UseBuffer(area
.x
, area
.y
);
99 // Blits the buffer to the dc, and detaches the dc from the buffer (so it
100 // can be effectively used once only).
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.
107 // Set and get the style
108 void SetStyle(int style
) { m_style
= style
; }
109 int GetStyle() const { return m_style
& ~wxBUFFER_USES_SHARED_BUFFER
; }
112 // common part of Init()s
113 void InitCommon(wxDC
*dc
, int style
)
115 wxASSERT_MSG( !m_dc
, _T("wxBufferedDC already initialised") );
120 // inherit the same layout direction as the original DC
121 if (dc
&& dc
->IsOk())
122 SetLayoutDirection(dc
->GetLayoutDirection());
125 // check that the bitmap is valid and use it
126 void UseBuffer(wxCoord w
= -1, wxCoord h
= -1);
128 // the underlying DC to which we copy everything drawn on this one in
131 // NB: Without the existence of a wxNullDC, this must be a pointer, else it
132 // could probably be a reference.
135 // the buffer (selected in this DC), initially invalid
138 // the buffering style
141 DECLARE_DYNAMIC_CLASS(wxBufferedDC
)
142 wxDECLARE_NO_COPY_CLASS(wxBufferedDC
);
146 // ----------------------------------------------------------------------------
147 // Double buffered PaintDC.
148 // ----------------------------------------------------------------------------
150 // Creates a double buffered wxPaintDC, optionally allowing the
151 // user to specify their own buffer to use.
152 class WXDLLIMPEXP_CORE wxBufferedPaintDC
: public wxBufferedDC
155 // If no bitmap is supplied by the user, a temporary one will be created.
156 wxBufferedPaintDC(wxWindow
*window
, wxBitmap
& buffer
, int style
= wxBUFFER_CLIENT_AREA
)
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
);
164 Init(&m_paintdc
, buffer
, style
);
166 Init(&m_paintdc
, GetBufferedSize(window
, style
), style
);
169 // If no bitmap is supplied by the user, a temporary one will be created.
170 wxBufferedPaintDC(wxWindow
*window
, int style
= wxBUFFER_CLIENT_AREA
)
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
);
177 Init(&m_paintdc
, GetBufferedSize(window
, style
), style
);
180 // default copy ctor ok.
182 virtual ~wxBufferedPaintDC()
184 // We must UnMask here, else by the time the base class
185 // does it, the PaintDC will have already been destroyed.
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
)
194 return style
& wxBUFFER_VIRTUAL_AREA
? window
->GetVirtualSize()
195 : window
->GetClientSize();
201 DECLARE_ABSTRACT_CLASS(wxBufferedPaintDC
)
202 wxDECLARE_NO_COPY_CLASS(wxBufferedPaintDC
);
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.
213 #if wxALWAYS_NATIVE_DOUBLE_BUFFER
214 #define wxAutoBufferedPaintDCBase wxPaintDC
216 #define wxAutoBufferedPaintDCBase wxBufferedPaintDC
219 class WXDLLIMPEXP_CORE wxAutoBufferedPaintDC
: public wxAutoBufferedPaintDCBase
223 wxAutoBufferedPaintDC(wxWindow
* win
)
224 : wxAutoBufferedPaintDCBase(win
)
226 wxASSERT_MSG( win
->GetBackgroundStyle() == wxBG_STYLE_CUSTOM
,
227 "You need to call SetBackgroundStyle(wxBG_STYLE_CUSTOM) in ctor, "
228 "and also, if needed, paint the background in wxEVT_PAINT handler."
232 virtual ~wxAutoBufferedPaintDC() { }
235 wxDECLARE_NO_COPY_CLASS(wxAutoBufferedPaintDC
);
240 // Check if the window is natively double buffered and will return a wxPaintDC
241 // if it is, a wxBufferedPaintDC otherwise. It is the caller's responsibility
242 // to delete the wxDC pointer when finished with it.
243 inline wxDC
* wxAutoBufferedPaintDCFactory(wxWindow
* window
)
245 if ( window
->IsDoubleBuffered() )
246 return new wxPaintDC(window
);
248 return new wxBufferedPaintDC(window
);
251 #endif // _WX_DCBUFFER_H_