]>
Commit | Line | Data |
---|---|---|
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 | ||
27 | #include "wx/dcbuffer.h" | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/module.h" | |
31 | #endif | |
32 | ||
33 | // ============================================================================ | |
34 | // implementation | |
35 | // ============================================================================ | |
36 | ||
37 | IMPLEMENT_DYNAMIC_CLASS(wxBufferedDC,wxMemoryDC) | |
38 | IMPLEMENT_ABSTRACT_CLASS(wxBufferedPaintDC,wxBufferedDC) | |
39 | ||
40 | // ---------------------------------------------------------------------------- | |
41 | // wxSharedDCBufferManager: helper class maintaining backing store bitmap | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | class wxSharedDCBufferManager : public wxModule | |
45 | { | |
46 | public: | |
47 | wxSharedDCBufferManager() { } | |
48 | ||
49 | virtual bool OnInit() { return true; } | |
50 | virtual void OnExit() { wxDELETE(ms_buffer); } | |
51 | ||
52 | static wxBitmap* GetBuffer(int w, int h) | |
53 | { | |
54 | if ( ms_usingSharedBuffer ) | |
55 | return new wxBitmap(w, h); | |
56 | ||
57 | if ( !ms_buffer || | |
58 | w > ms_buffer->GetWidth() || | |
59 | h > ms_buffer->GetHeight() ) | |
60 | { | |
61 | delete ms_buffer; | |
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 | ||
70 | ms_buffer = new wxBitmap(w, h); | |
71 | } | |
72 | ||
73 | ms_usingSharedBuffer = true; | |
74 | return ms_buffer; | |
75 | } | |
76 | ||
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 | ||
90 | private: | |
91 | static wxBitmap *ms_buffer; | |
92 | static bool ms_usingSharedBuffer; | |
93 | ||
94 | DECLARE_DYNAMIC_CLASS(wxSharedDCBufferManager) | |
95 | }; | |
96 | ||
97 | wxBitmap* wxSharedDCBufferManager::ms_buffer = NULL; | |
98 | bool wxSharedDCBufferManager::ms_usingSharedBuffer = false; | |
99 | ||
100 | IMPLEMENT_DYNAMIC_CLASS(wxSharedDCBufferManager, wxModule) | |
101 | ||
102 | // ============================================================================ | |
103 | // wxBufferedDC | |
104 | // ============================================================================ | |
105 | ||
106 | void wxBufferedDC::UseBuffer(wxCoord w, wxCoord h) | |
107 | { | |
108 | if ( !m_buffer || !m_buffer->IsOk() ) | |
109 | { | |
110 | if ( w == -1 || h == -1 ) | |
111 | m_dc->GetSize(&w, &h); | |
112 | ||
113 | m_buffer = wxSharedDCBufferManager::GetBuffer(w, h); | |
114 | m_style |= wxBUFFER_USES_SHARED_BUFFER; | |
115 | } | |
116 | ||
117 | SelectObject(*m_buffer); | |
118 | ||
119 | // now that the DC is valid we can inherit the attributes (fonts, colours, | |
120 | // layout direction, ...) from the original DC | |
121 | if ( m_dc->IsOk() ) | |
122 | CopyAttributes(*m_dc); | |
123 | } | |
124 | ||
125 | void wxBufferedDC::UnMask() | |
126 | { | |
127 | wxCHECK_RET( m_dc, wxT("no underlying wxDC?") ); | |
128 | wxASSERT_MSG( m_buffer && m_buffer->IsOk(), wxT("invalid backing store") ); | |
129 | ||
130 | wxCoord x = 0, | |
131 | y = 0; | |
132 | ||
133 | if ( m_style & wxBUFFER_CLIENT_AREA ) | |
134 | GetDeviceOrigin(&x, &y); | |
135 | ||
136 | // avoid blitting too much: if we were created for a bigger bitmap (and | |
137 | // reused for a smaller one later) we should only blit the real bitmap area | |
138 | // and not the full allocated back buffer | |
139 | int widthDC, | |
140 | heightDC; | |
141 | ||
142 | m_dc->GetSize(&widthDC, &heightDC); | |
143 | ||
144 | int widthBuf = m_buffer->GetWidth(), | |
145 | heightBuf = m_buffer->GetHeight(); | |
146 | ||
147 | m_dc->Blit(0, 0, | |
148 | wxMin(widthDC, widthBuf), wxMin(heightDC, heightBuf), | |
149 | this, | |
150 | -x, -y); | |
151 | m_dc = NULL; | |
152 | ||
153 | if ( m_style & wxBUFFER_USES_SHARED_BUFFER ) | |
154 | wxSharedDCBufferManager::ReleaseBuffer(m_buffer); | |
155 | } |