1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dcbufcmn.cpp
3 // Purpose: Buffered DC implementation
4 // Author: Ron Lee, Jaakko Salli
6 // Created: Sep-20-2006
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/dcbuffer.h"
30 #include "wx/module.h"
33 // ============================================================================
35 // ============================================================================
37 // ----------------------------------------------------------------------------
38 // wxSharedDCBufferManager: helper class maintaining backing store bitmap
39 // ----------------------------------------------------------------------------
41 class wxSharedDCBufferManager
: public wxModule
44 wxSharedDCBufferManager() { }
46 virtual bool OnInit() { return true; }
47 virtual void OnExit() { wxDELETE(ms_buffer
); }
49 static wxBitmap
* GetBuffer(int w
, int h
)
51 if ( ms_usingSharedBuffer
)
52 return new wxBitmap(w
, h
);
55 w
> ms_buffer
->GetWidth() ||
56 h
> ms_buffer
->GetHeight() )
59 ms_buffer
= new wxBitmap(w
, h
);
62 ms_usingSharedBuffer
= true;
66 static void ReleaseBuffer(wxBitmap
* buffer
)
68 if ( buffer
== ms_buffer
)
70 wxASSERT_MSG( ms_usingSharedBuffer
, wxT("shared buffer already released") );
71 ms_usingSharedBuffer
= false;
80 static wxBitmap
*ms_buffer
;
81 static bool ms_usingSharedBuffer
;
83 DECLARE_DYNAMIC_CLASS(wxSharedDCBufferManager
)
86 wxBitmap
* wxSharedDCBufferManager::ms_buffer
= NULL
;
87 bool wxSharedDCBufferManager::ms_usingSharedBuffer
= false;
89 IMPLEMENT_DYNAMIC_CLASS(wxSharedDCBufferManager
, wxModule
)
91 // ============================================================================
93 // ============================================================================
95 void wxBufferedDC::UseBuffer(wxCoord w
, wxCoord h
)
97 if ( !m_buffer
|| !m_buffer
->IsOk() )
99 if ( w
== -1 || h
== -1 )
100 m_dc
->GetSize(&w
, &h
);
102 m_buffer
= wxSharedDCBufferManager::GetBuffer(w
, h
);
103 m_style
|= wxBUFFER_USES_SHARED_BUFFER
;
106 SelectObject(*m_buffer
);
109 void wxBufferedDC::UnMask()
111 wxCHECK_RET( m_dc
, _T("no underlying wxDC?") );
112 wxASSERT_MSG( m_buffer
&& m_buffer
->IsOk(), _T("invalid backing store") );
117 if ( m_style
& wxBUFFER_CLIENT_AREA
)
118 GetDeviceOrigin(&x
, &y
);
120 m_dc
->Blit(0, 0, m_buffer
->GetWidth(), m_buffer
->GetHeight(),
124 if ( m_style
& wxBUFFER_USES_SHARED_BUFFER
)
125 wxSharedDCBufferManager::ReleaseBuffer(m_buffer
);