]> git.saurik.com Git - wxWidgets.git/blame - src/common/dcbufcmn.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / src / common / dcbufcmn.cpp
CommitLineData
2e992e06
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/dcbufcmn.cpp
3// Purpose: Buffered DC implementation
4// Author: Ron Lee, Jaakko Salli
5// Modified by:
6// Created: Sep-20-2006
7// RCS-ID: $Id$
8// Copyright: (c) wxWidgets team
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
576bdf85
PC
27#include "wx/dcbuffer.h"
28
2e992e06 29#ifndef WX_PRECOMP
37747da8 30 #include "wx/module.h"
2e992e06
VZ
31#endif
32
2e992e06
VZ
33// ============================================================================
34// implementation
35// ============================================================================
36
48819440
RR
37IMPLEMENT_DYNAMIC_CLASS(wxBufferedDC,wxMemoryDC)
38IMPLEMENT_ABSTRACT_CLASS(wxBufferedPaintDC,wxBufferedDC)
39
1a9a3ce9
VZ
40// ----------------------------------------------------------------------------
41// wxSharedDCBufferManager: helper class maintaining backing store bitmap
42// ----------------------------------------------------------------------------
77f62aa7 43
37747da8 44class wxSharedDCBufferManager : public wxModule
77f62aa7 45{
77f62aa7 46public:
77f62aa7 47 wxSharedDCBufferManager() { }
77f62aa7 48
37747da8
VZ
49 virtual bool OnInit() { return true; }
50 virtual void OnExit() { wxDELETE(ms_buffer); }
51
3754c856 52 static wxBitmap* GetBuffer(int w, int h)
77f62aa7 53 {
125817d0
VZ
54 if ( ms_usingSharedBuffer )
55 return new wxBitmap(w, h);
56
37747da8
VZ
57 if ( !ms_buffer ||
58 w > ms_buffer->GetWidth() ||
59 h > ms_buffer->GetHeight() )
77f62aa7 60 {
37747da8 61 delete ms_buffer;
7c9f643a
VZ
62
63 // we must always return a valid bitmap but creating a bitmap of
64 // size 0 would fail, so create a 1*1 bitmap in this case
65 if ( !w )
66 w = 1;
67 if ( !h )
68 h = 1;
69
37747da8 70 ms_buffer = new wxBitmap(w, h);
77f62aa7 71 }
125817d0
VZ
72
73 ms_usingSharedBuffer = true;
3754c856 74 return ms_buffer;
77f62aa7
RD
75 }
76
125817d0
VZ
77 static void ReleaseBuffer(wxBitmap* buffer)
78 {
79 if ( buffer == ms_buffer )
80 {
81 wxASSERT_MSG( ms_usingSharedBuffer, wxT("shared buffer already released") );
82 ms_usingSharedBuffer = false;
83 }
84 else
85 {
86 delete buffer;
87 }
88 }
89
77f62aa7 90private:
37747da8 91 static wxBitmap *ms_buffer;
125817d0 92 static bool ms_usingSharedBuffer;
77f62aa7 93
37747da8
VZ
94 DECLARE_DYNAMIC_CLASS(wxSharedDCBufferManager)
95};
77f62aa7 96
3754c856 97wxBitmap* wxSharedDCBufferManager::ms_buffer = NULL;
125817d0 98bool wxSharedDCBufferManager::ms_usingSharedBuffer = false;
576bdf85 99
37747da8 100IMPLEMENT_DYNAMIC_CLASS(wxSharedDCBufferManager, wxModule)
77f62aa7
RD
101
102// ============================================================================
103// wxBufferedDC
104// ============================================================================
105
106void wxBufferedDC::UseBuffer(wxCoord w, wxCoord h)
107{
fe576f48
VZ
108 wxCHECK_RET( w >= -1 && h >= -1, "Invalid buffer size" );
109
3754c856 110 if ( !m_buffer || !m_buffer->IsOk() )
77f62aa7
RD
111 {
112 if ( w == -1 || h == -1 )
113 m_dc->GetSize(&w, &h);
114
37747da8 115 m_buffer = wxSharedDCBufferManager::GetBuffer(w, h);
125817d0 116 m_style |= wxBUFFER_USES_SHARED_BUFFER;
0681e07a 117 m_area.Set(w,h);
77f62aa7 118 }
0681e07a
RD
119 else
120 m_area = m_buffer->GetSize();
77f62aa7 121
3754c856 122 SelectObject(*m_buffer);
a9682760
VZ
123
124 // now that the DC is valid we can inherit the attributes (fonts, colours,
125 // layout direction, ...) from the original DC
455bec65 126 if ( m_dc && m_dc->IsOk() )
a9682760 127 CopyAttributes(*m_dc);
77f62aa7 128}
2e992e06 129
125817d0
VZ
130void wxBufferedDC::UnMask()
131{
9a83f860
VZ
132 wxCHECK_RET( m_dc, wxT("no underlying wxDC?") );
133 wxASSERT_MSG( m_buffer && m_buffer->IsOk(), wxT("invalid backing store") );
125817d0
VZ
134
135 wxCoord x = 0,
136 y = 0;
137
e6933ff9
RD
138 // Ensure the scale matches the device
139 SetUserScale(1.0, 1.0);
ce00f59b 140
125817d0
VZ
141 if ( m_style & wxBUFFER_CLIENT_AREA )
142 GetDeviceOrigin(&x, &y);
143
0681e07a
RD
144 // It's possible that the buffer may be bigger than the area that needs to
145 // be drawn (the client size of the window is smaller than the bitmap, or
146 // a shared bitmap has been reused for a smaller area, etc.) so avoid
147 // blitting too much if possible, but only use the real DC size if the
148 // wxBUFFER_VIRTUAL_AREA style is not set.
149 int width = m_area.GetWidth(),
150 height = m_area.GetHeight();
4581c913 151
0681e07a
RD
152 if (! m_style & wxBUFFER_VIRTUAL_AREA)
153 {
154 int widthDC,
155 heightDC;
156 m_dc->GetSize(&widthDC, &heightDC);
157 width = wxMin(width, widthDC);
158 height = wxMin(height, heightDC);
159 }
4581c913 160
0681e07a 161 m_dc->Blit(0, 0, width, height, this, -x, -y);
125817d0
VZ
162 m_dc = NULL;
163
164 if ( m_style & wxBUFFER_USES_SHARED_BUFFER )
165 wxSharedDCBufferManager::ReleaseBuffer(m_buffer);
166}