]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/dcbufcmn.cpp
added wxTo/FromString(wxColour) (part of patch 1753875)
[wxWidgets.git] / src / common / dcbufcmn.cpp
index fe807524121e2f394b7c9276dca28012c5dd5b5a..b38b4f1f67921c65093f3bcbc69c3517a42b30e8 100644 (file)
     #pragma hdrstop
 #endif
 
-#ifndef WX_PRECOMP
-#endif
-
 #include "wx/dcbuffer.h"
 
+#ifndef WX_PRECOMP
+    #include "wx/module.h"
+#endif
 
 // ============================================================================
 // implementation
 // ============================================================================
 
-// ============================================================================
-// wxSharedDCBufferManager
-//   Helper class to free shared buffer when the app exists.
-// ============================================================================
+// ----------------------------------------------------------------------------
+// wxSharedDCBufferManager: helper class maintaining backing store bitmap
+// ----------------------------------------------------------------------------
 
-class wxSharedDCBufferManager
+class wxSharedDCBufferManager : public wxModule
 {
-    friend class wxBufferedDC;
 public:
+    wxSharedDCBufferManager() { }
 
-    wxSharedDCBufferManager()
-    {
-        m_buffer = NULL;
-    }
+    virtual bool OnInit() { return true; }
+    virtual void OnExit() { wxDELETE(ms_buffer); }
 
-    ~wxSharedDCBufferManager()
+    static wxBitmap* GetBuffer(int w, int h)
     {
-        delete m_buffer;
-    }
+        if ( ms_usingSharedBuffer )
+            return new wxBitmap(w, h);
 
-    wxBitmap* GetBuffer(wxWindow* win, const wxSize& area)
-    {
-        int width = area.x;
-        int height = area.y;
+        if ( !ms_buffer ||
+                w > ms_buffer->GetWidth() ||
+                    h > ms_buffer->GetHeight() )
+        {
+            delete ms_buffer;
+            ms_buffer = new wxBitmap(w, h);
+        }
 
-        if ( width <= 0 )
-            win->GetClientSize(&width, &height);
+        ms_usingSharedBuffer = true;
+        return ms_buffer;
+    }
 
-        if ( !m_buffer ||
-             width > m_buffer->GetWidth() ||
-             height > m_buffer->GetHeight() )
+    static void ReleaseBuffer(wxBitmap* buffer)
+    {
+        if ( buffer == ms_buffer )
         {
-            delete m_buffer;
-
-            // Create slightly larger bitmap so we don't need to
-            // be reallocating constantly when the user enlarges
-            // the frame for the first time.
-            m_buffer = new wxBitmap(width+20, height+20);
+            wxASSERT_MSG( ms_usingSharedBuffer, wxT("shared buffer already released") );
+            ms_usingSharedBuffer = false;
+        }
+        else
+        {
+            delete buffer;
         }
-
-        return m_buffer;
     }
 
 private:
-    wxBitmap*   m_buffer;
+    static wxBitmap *ms_buffer;
+    static bool ms_usingSharedBuffer;
+
+    DECLARE_DYNAMIC_CLASS(wxSharedDCBufferManager)
 };
 
-static wxSharedDCBufferManager gs_sharedDCBufferManager;
+wxBitmap* wxSharedDCBufferManager::ms_buffer = NULL;
+bool wxSharedDCBufferManager::ms_usingSharedBuffer = false;
+
+IMPLEMENT_DYNAMIC_CLASS(wxSharedDCBufferManager, wxModule)
 
 // ============================================================================
 // wxBufferedDC
 // ============================================================================
 
-// Blits the buffer to the dc, and detaches the dc from the buffer (so it
-// can be effectively used once only).
-//
-// Usually called in the dtor or by the dtor of derived classes if the
-// BufferedDC must blit before the derived class (which may own the dc it's
-// blitting to) is destroyed.
-void wxBufferedDC::UnMask()
+void wxBufferedDC::UseBuffer(wxCoord w, wxCoord h)
 {
-    if ( m_buffer )
+    if ( !m_buffer || !m_buffer->IsOk() )
     {
-        wxASSERT_MSG( m_mainDc != NULL,
-                      _T("No underlying DC associated with wxBufferedDC (anymore)") );
-
-        wxDC* bufferDc = DetachDC();
+        if ( w == -1 || h == -1 )
+            m_dc->GetSize(&w, &h);
 
-        wxASSERT( bufferDc->IsKindOf(CLASSINFO(wxMemoryDC)) );
-
-        wxCoord x=0, y=0;
-
-        if (m_style & wxBUFFER_CLIENT_AREA)
-            bufferDc->GetDeviceOrigin(& x, & y);
-
-        m_mainDc->Blit( 0, 0,
-                        m_buffer->GetWidth(), m_buffer->GetHeight(), bufferDc,
-                        -x, -y );
-        m_mainDc = NULL;
-        m_buffer = NULL;
-        delete bufferDc;
+        m_buffer = wxSharedDCBufferManager::GetBuffer(w, h);
+        m_style |= wxBUFFER_USES_SHARED_BUFFER;
     }
-}
 
-void wxBufferedDC::PrepareBuffer(wxWindow* win, const wxSize& area)
-{
-    m_buffer = gs_sharedDCBufferManager.GetBuffer(win, area);
+    SelectObject(*m_buffer);
 }
 
-void wxBufferedDC::UseBuffer()
+void wxBufferedDC::UnMask()
 {
-    wxASSERT(m_buffer);
+    wxCHECK_RET( m_dc, _T("no underlying wxDC?") );
+    wxASSERT_MSG( m_buffer && m_buffer->IsOk(), _T("invalid backing store") );
 
-    wxMemoryDC* memoryDc = new wxMemoryDC(m_mainDc);
-    memoryDc->SelectObject(*m_buffer);
+    wxCoord x = 0,
+            y = 0;
 
-    AttachDC(memoryDc);
-}
+    if ( m_style & wxBUFFER_CLIENT_AREA )
+        GetDeviceOrigin(&x, &y);
 
+    m_dc->Blit(0, 0, m_buffer->GetWidth(), m_buffer->GetHeight(),
+               this, -x, -y );
+    m_dc = NULL;
 
+    if ( m_style & wxBUFFER_USES_SHARED_BUFFER )
+        wxSharedDCBufferManager::ReleaseBuffer(m_buffer);
+}