Remove all lines containing cvs/svn "$Id$" keyword.
[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 // Copyright: (c) Ron Lee
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_DCBUFFER_H_
12 #define _WX_DCBUFFER_H_
13
14 #include "wx/dcmemory.h"
15 #include "wx/dcclient.h"
16 #include "wx/window.h"
17
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
22 #else
23 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 0
24 #endif
25
26
27 // ----------------------------------------------------------------------------
28 // Double buffering helper.
29 // ----------------------------------------------------------------------------
30
31 // Assumes the buffer bitmap covers the entire scrolled window,
32 // and prepares the window DC accordingly
33 #define wxBUFFER_VIRTUAL_AREA 0x01
34
35 // Assumes the buffer bitmap only covers the client area;
36 // does not prepare the window DC
37 #define wxBUFFER_CLIENT_AREA 0x02
38
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
42
43 class WXDLLIMPEXP_CORE wxBufferedDC : public wxMemoryDC
44 {
45 public:
46 // Default ctor, must subsequently call Init for two stage construction.
47 wxBufferedDC()
48 : m_dc(NULL),
49 m_buffer(NULL),
50 m_style(0)
51 {
52 }
53
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)
59 {
60 Init(dc, buffer, style);
61 }
62
63 // Construct a wxBufferedDC with an internal buffer of 'area'
64 // (where area is usually something like the size of the window
65 // being buffered)
66 wxBufferedDC(wxDC *dc, const wxSize& area, int style = wxBUFFER_CLIENT_AREA)
67 : m_dc(NULL), m_buffer(NULL)
68 {
69 Init(dc, area, style);
70 }
71
72 // The usually desired action in the dtor is to blit the buffer.
73 virtual ~wxBufferedDC()
74 {
75 if ( m_dc )
76 UnMask();
77 }
78
79 // These reimplement the actions of the ctors for two stage creation
80 void Init(wxDC *dc,
81 wxBitmap& buffer = wxNullBitmap,
82 int style = wxBUFFER_CLIENT_AREA)
83 {
84 InitCommon(dc, style);
85
86 m_buffer = &buffer;
87
88 UseBuffer();
89 }
90
91 void Init(wxDC *dc, const wxSize &area, int style = wxBUFFER_CLIENT_AREA)
92 {
93 InitCommon(dc, style);
94
95 UseBuffer(area.x, area.y);
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 // Set and get the style
107 void SetStyle(int style) { m_style = style; }
108 int GetStyle() const { return m_style & ~wxBUFFER_USES_SHARED_BUFFER; }
109
110 private:
111 // common part of Init()s
112 void InitCommon(wxDC *dc, int style)
113 {
114 wxASSERT_MSG( !m_dc, wxT("wxBufferedDC already initialised") );
115
116 m_dc = dc;
117 m_style = style;
118 }
119
120 // check that the bitmap is valid and use it
121 void UseBuffer(wxCoord w = -1, wxCoord h = -1);
122
123 // the underlying DC to which we copy everything drawn on this one in
124 // UnMask()
125 //
126 // NB: Without the existence of a wxNullDC, this must be a pointer, else it
127 // could probably be a reference.
128 wxDC *m_dc;
129
130 // the buffer (selected in this DC), initially invalid
131 wxBitmap *m_buffer;
132
133 // the buffering style
134 int m_style;
135
136 wxSize m_area;
137
138 DECLARE_DYNAMIC_CLASS(wxBufferedDC)
139 wxDECLARE_NO_COPY_CLASS(wxBufferedDC);
140 };
141
142
143 // ----------------------------------------------------------------------------
144 // Double buffered PaintDC.
145 // ----------------------------------------------------------------------------
146
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
150 {
151 public:
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)
154 : m_paintdc(window)
155 {
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 );
159
160 if( buffer.IsOk() )
161 Init(&m_paintdc, buffer, style);
162 else
163 Init(&m_paintdc, GetBufferedSize(window, style), style);
164 }
165
166 // If no bitmap is supplied by the user, a temporary one will be created.
167 wxBufferedPaintDC(wxWindow *window, int style = wxBUFFER_CLIENT_AREA)
168 : m_paintdc(window)
169 {
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 );
173
174 Init(&m_paintdc, GetBufferedSize(window, style), style);
175 }
176
177 // default copy ctor ok.
178
179 virtual ~wxBufferedPaintDC()
180 {
181 // We must UnMask here, else by the time the base class
182 // does it, the PaintDC will have already been destroyed.
183 UnMask();
184 }
185
186 protected:
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)
190 {
191 return style & wxBUFFER_VIRTUAL_AREA ? window->GetVirtualSize()
192 : window->GetClientSize();
193 }
194
195 private:
196 wxPaintDC m_paintdc;
197
198 DECLARE_ABSTRACT_CLASS(wxBufferedPaintDC)
199 wxDECLARE_NO_COPY_CLASS(wxBufferedPaintDC);
200 };
201
202
203
204 //
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.
209 //
210 #if wxALWAYS_NATIVE_DOUBLE_BUFFER
211 #define wxAutoBufferedPaintDCBase wxPaintDC
212 #else
213 #define wxAutoBufferedPaintDCBase wxBufferedPaintDC
214 #endif
215
216 class WXDLLIMPEXP_CORE wxAutoBufferedPaintDC : public wxAutoBufferedPaintDCBase
217 {
218 public:
219
220 wxAutoBufferedPaintDC(wxWindow* win)
221 : wxAutoBufferedPaintDCBase(win)
222 {
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."
226 );
227 }
228
229 virtual ~wxAutoBufferedPaintDC() { }
230
231 private:
232 wxDECLARE_NO_COPY_CLASS(wxAutoBufferedPaintDC);
233 };
234
235
236
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)
241 {
242 if ( window->IsDoubleBuffered() )
243 return new wxPaintDC(window);
244 else
245 return new wxBufferedPaintDC(window);
246 }
247
248 #endif // _WX_DCBUFFER_H_