]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dcbuffer.h
Don't define __STRICT_ANSI__, we should build both with and without it.
[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 6// Created: 16/03/02
67e2efca 7// Copyright: (c) Ron Lee
65571936 8// Licence: wxWindows licence
67e2efca
RL
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_DCBUFFER_H_
12#define _WX_DCBUFFER_H_
13
67e2efca 14#include "wx/dcmemory.h"
aeb500e6 15#include "wx/dcclient.h"
d910fe28 16#include "wx/window.h"
67e2efca 17
2e992e06
VZ
18// Split platforms into two groups - those which have well-working
19// double-buffering by default, and those which do not.
77f62aa7 20#if defined(__WXMAC__) || defined(__WXGTK20__) || defined(__WXDFB__)
2e992e06
VZ
21 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 1
22#else
23 #define wxALWAYS_NATIVE_DOUBLE_BUFFER 0
24#endif
25
26
1cc8c8b7
VZ
27// ----------------------------------------------------------------------------
28// Double buffering helper.
29// ----------------------------------------------------------------------------
67e2efca 30
265a3864
JS
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
125817d0
VZ
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
888dde65 43class WXDLLIMPEXP_CORE wxBufferedDC : public wxMemoryDC
1cc8c8b7
VZ
44{
45public:
46 // Default ctor, must subsequently call Init for two stage construction.
b1ed79a9
VZ
47 wxBufferedDC()
48 : m_dc(NULL),
3754c856 49 m_buffer(NULL),
b1ed79a9 50 m_style(0)
1cc8c8b7
VZ
51 {
52 }
67e2efca 53
1cc8c8b7 54 // Construct a wxBufferedDC using a user supplied buffer.
116627da 55 wxBufferedDC(wxDC *dc,
3754c856 56 wxBitmap& buffer = wxNullBitmap,
116627da 57 int style = wxBUFFER_CLIENT_AREA)
3754c856 58 : m_dc(NULL), m_buffer(NULL)
dbd22c6a 59 {
c4a34cf8 60 Init(dc, buffer, style);
dbd22c6a 61 }
67e2efca 62
1cc8c8b7
VZ
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)
c4a34cf8 66 wxBufferedDC(wxDC *dc, const wxSize& area, int style = wxBUFFER_CLIENT_AREA)
3754c856 67 : m_dc(NULL), m_buffer(NULL)
dbd22c6a 68 {
c4a34cf8 69 Init(dc, area, style);
dbd22c6a 70 }
67e2efca 71
1cc8c8b7 72 // The usually desired action in the dtor is to blit the buffer.
dbd22c6a
RL
73 virtual ~wxBufferedDC()
74 {
b1ed79a9
VZ
75 if ( m_dc )
76 UnMask();
dbd22c6a 77 }
67e2efca 78
c4a34cf8 79 // These reimplement the actions of the ctors for two stage creation
116627da 80 void Init(wxDC *dc,
3754c856 81 wxBitmap& buffer = wxNullBitmap,
116627da 82 int style = wxBUFFER_CLIENT_AREA)
dbd22c6a 83 {
b1ed79a9
VZ
84 InitCommon(dc, style);
85
3754c856 86 m_buffer = &buffer;
b1ed79a9 87
dbd22c6a
RL
88 UseBuffer();
89 }
90
19c9f36f 91 void Init(wxDC *dc, const wxSize &area, int style = wxBUFFER_CLIENT_AREA)
dbd22c6a 92 {
b1ed79a9
VZ
93 InitCommon(dc, style);
94
77f62aa7 95 UseBuffer(area.x, area.y);
dbd22c6a 96 }
67e2efca 97
1cc8c8b7
VZ
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.
125817d0 104 void UnMask();
67e2efca 105
265a3864
JS
106 // Set and get the style
107 void SetStyle(int style) { m_style = style; }
125817d0 108 int GetStyle() const { return m_style & ~wxBUFFER_USES_SHARED_BUFFER; }
265a3864 109
1cc8c8b7 110private:
b1ed79a9
VZ
111 // common part of Init()s
112 void InitCommon(wxDC *dc, int style)
113 {
9a83f860 114 wxASSERT_MSG( !m_dc, wxT("wxBufferedDC already initialised") );
b1ed79a9
VZ
115
116 m_dc = dc;
117 m_style = style;
b1ed79a9
VZ
118 }
119
19c9f36f 120 // check that the bitmap is valid and use it
77f62aa7 121 void UseBuffer(wxCoord w = -1, wxCoord h = -1);
1cc8c8b7 122
19c9f36f
RD
123 // the underlying DC to which we copy everything drawn on this one in
124 // UnMask()
74f4dabb
VZ
125 //
126 // NB: Without the existence of a wxNullDC, this must be a pointer, else it
127 // could probably be a reference.
19c9f36f 128 wxDC *m_dc;
67e2efca 129
1a9a3ce9 130 // the buffer (selected in this DC), initially invalid
3754c856 131 wxBitmap *m_buffer;
22f3361e 132
265a3864 133 // the buffering style
19c9f36f 134 int m_style;
265a3864 135
0681e07a
RD
136 wxSize m_area;
137
653752be 138 DECLARE_DYNAMIC_CLASS(wxBufferedDC)
c0c133e1 139 wxDECLARE_NO_COPY_CLASS(wxBufferedDC);
67e2efca
RL
140};
141
142
1cc8c8b7
VZ
143// ----------------------------------------------------------------------------
144// Double buffered PaintDC.
145// ----------------------------------------------------------------------------
67e2efca
RL
146
147// Creates a double buffered wxPaintDC, optionally allowing the
148// user to specify their own buffer to use.
888dde65 149class WXDLLIMPEXP_CORE wxBufferedPaintDC : public wxBufferedDC
67e2efca 150{
67e2efca 151public:
265a3864 152 // If no bitmap is supplied by the user, a temporary one will be created.
3754c856 153 wxBufferedPaintDC(wxWindow *window, wxBitmap& buffer, int style = wxBUFFER_CLIENT_AREA)
a66f2f42 154 : m_paintdc(window)
1cc8c8b7 155 {
265a3864
JS
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
55ccdb93 160 if( buffer.IsOk() )
265a3864 161 Init(&m_paintdc, buffer, style);
dbd22c6a 162 else
d38819f5 163 Init(&m_paintdc, GetBufferedSize(window, style), style);
265a3864
JS
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
d38819f5 174 Init(&m_paintdc, GetBufferedSize(window, style), style);
1cc8c8b7 175 }
67e2efca
RL
176
177 // default copy ctor ok.
178
1cc8c8b7
VZ
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
d38819f5
VZ
186protected:
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
1cc8c8b7 195private:
1cc8c8b7 196 wxPaintDC m_paintdc;
fc7a2a60 197
653752be 198 DECLARE_ABSTRACT_CLASS(wxBufferedPaintDC)
c0c133e1 199 wxDECLARE_NO_COPY_CLASS(wxBufferedPaintDC);
67e2efca
RL
200};
201
67e2efca 202
2e992e06
VZ
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
888dde65 216class WXDLLIMPEXP_CORE wxAutoBufferedPaintDC : public wxAutoBufferedPaintDCBase
2e992e06
VZ
217{
218public:
219
220 wxAutoBufferedPaintDC(wxWindow* win)
221 : wxAutoBufferedPaintDCBase(win)
222 {
9c61c5b0
VZ
223 wxASSERT_MSG( win->GetBackgroundStyle() == wxBG_STYLE_PAINT,
224 "You need to call SetBackgroundStyle(wxBG_STYLE_PAINT) in ctor, "
657a8a35
VZ
225 "and also, if needed, paint the background in wxEVT_PAINT handler."
226 );
2e992e06
VZ
227 }
228
229 virtual ~wxAutoBufferedPaintDC() { }
230
231private:
c0c133e1 232 wxDECLARE_NO_COPY_CLASS(wxAutoBufferedPaintDC);
2e992e06
VZ
233};
234
2e992e06
VZ
235
236
19c9f36f
RD
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.
240inline wxDC* wxAutoBufferedPaintDCFactory(wxWindow* window)
241{
242 if ( window->IsDoubleBuffered() )
243 return new wxPaintDC(window);
244 else
245 return new wxBufferedPaintDC(window);
246}
19c9f36f 247
2e992e06 248#endif // _WX_DCBUFFER_H_