Fixed wxBufferedPaintDC for scrolled windows
[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 // RCS-ID: $Id$
8 // Copyright: (c) Ron Lee
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DCBUFFER_H_
13 #define _WX_DCBUFFER_H_
14
15 #include "wx/dcmemory.h"
16 #include "wx/dcclient.h"
17 #include "wx/window.h"
18
19 // ----------------------------------------------------------------------------
20 // Double buffering helper.
21 // ----------------------------------------------------------------------------
22
23 class wxBufferedDC : public wxMemoryDC
24 {
25 public:
26 // Default ctor, must subsequently call Init for two stage construction.
27 wxBufferedDC() : m_dc( 0 )
28 {
29 }
30
31 // Construct a wxBufferedDC using a user supplied buffer.
32 wxBufferedDC(wxDC *dc, const wxBitmap &buffer)
33 : m_dc( dc ),
34 m_buffer( buffer )
35 {
36 UseBuffer();
37 }
38
39 // Construct a wxBufferedDC with an internal buffer of 'area'
40 // (where area is usually something like the size of the window
41 // being buffered)
42 wxBufferedDC(wxDC *dc, const wxSize &area)
43 : m_dc( dc ),
44 m_buffer( area.GetWidth(), area.GetHeight() )
45 {
46 UseBuffer();
47 }
48
49 // default copy ctor ok.
50
51 // The usually desired action in the dtor is to blit the buffer.
52 virtual ~wxBufferedDC()
53 {
54 if ( m_dc ) UnMask();
55 }
56
57 // These reimplement the actions of the ctors for two stage creation, but
58 // are not used by the ctors themselves to save a few cpu cycles.
59 void Init(wxDC *dc, const wxBitmap &buffer)
60 {
61 wxASSERT_MSG( m_dc == 0 && m_buffer == wxNullBitmap,
62 _T("wxBufferedDC already initialised") );
63 m_dc = dc;
64 m_buffer = buffer;
65 UseBuffer();
66 }
67
68 void Init(wxDC *dc, const wxSize &area)
69 {
70 Init(dc, wxBitmap(area.GetWidth(), area.GetHeight()));
71 }
72
73 // Blits the buffer to the dc, and detaches the dc from the buffer (so it
74 // can be effectively used once only).
75 //
76 // Usually called in the dtor or by the dtor of derived classes if the
77 // BufferedDC must blit before the derived class (which may own the dc it's
78 // blitting to) is destroyed.
79 void UnMask()
80 {
81 wxASSERT_MSG( m_dc != 0,
82 _T("No underlying DC associated with wxBufferedDC (anymore)") );
83
84 wxCoord x, y;
85 GetDeviceOrigin(& x, & y);
86
87 m_dc->Blit( 0, 0,
88 m_buffer.GetWidth(), m_buffer.GetHeight(), this,
89 -x, -y );
90 m_dc = NULL;
91 }
92
93 private:
94 // check that the bitmap is valid and use it
95 void UseBuffer()
96 {
97 wxASSERT_MSG( m_buffer.Ok(), _T("invalid bitmap in wxBufferedDC") );
98
99 SelectObject(m_buffer);
100 }
101
102 // the underlying DC to which we copy everything drawn on this one in
103 // UnMask()
104 //
105 // NB: Without the existence of a wxNullDC, this must be a pointer, else it
106 // could probably be a reference.
107 wxDC *m_dc;
108
109 // the buffer (selected in this DC)
110 wxBitmap m_buffer;
111
112 DECLARE_NO_COPY_CLASS(wxBufferedDC)
113 };
114
115
116 // ----------------------------------------------------------------------------
117 // Double buffered PaintDC.
118 // ----------------------------------------------------------------------------
119
120 // Creates a double buffered wxPaintDC, optionally allowing the
121 // user to specify their own buffer to use.
122 class wxBufferedPaintDC : public wxBufferedDC
123 {
124 public:
125 // If no bitmap is supplied by the user, a temporary one wil; be created.
126 wxBufferedPaintDC(wxWindow *window, const wxBitmap& buffer = wxNullBitmap)
127 : m_paintdc(window)
128 {
129 if( buffer != wxNullBitmap )
130 Init(&m_paintdc, buffer);
131 else
132 Init(&m_paintdc, window->GetClientSize());
133 }
134
135 // default copy ctor ok.
136
137 virtual ~wxBufferedPaintDC()
138 {
139 // We must UnMask here, else by the time the base class
140 // does it, the PaintDC will have already been destroyed.
141 UnMask();
142 }
143
144 private:
145 wxPaintDC m_paintdc;
146
147 DECLARE_NO_COPY_CLASS(wxBufferedPaintDC)
148 };
149
150 #endif // _WX_DCBUFFER_H_
151