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