]> git.saurik.com Git - wxWidgets.git/blame - src/common/dcbufcmn.cpp
don't crash on weird line endings like \r\r\n
[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
27#ifndef WX_PRECOMP
28#endif
29
30#include "wx/dcbuffer.h"
31
32
33// ============================================================================
34// implementation
35// ============================================================================
36
37// ============================================================================
38// wxSharedDCBufferManager
39// Helper class to free shared buffer when the app exists.
40// ============================================================================
41
42class wxSharedDCBufferManager
43{
44 friend class wxBufferedDC;
45public:
46
47 wxSharedDCBufferManager()
48 {
49 m_buffer = NULL;
50 }
51
52 ~wxSharedDCBufferManager()
53 {
54 delete m_buffer;
55 }
56
57 wxBitmap* GetBuffer(wxWindow* win, const wxSize& area)
58 {
59 int width = area.x;
60 int height = area.y;
61
62 if ( width <= 0 )
63 win->GetClientSize(&width, &height);
64
65 if ( !m_buffer ||
66 width > m_buffer->GetWidth() ||
67 height > m_buffer->GetHeight() )
68 {
69 delete m_buffer;
70
71 // Create slightly larger bitmap so we don't need to
72 // be reallocating constantly when the user enlarges
73 // the frame for the first time.
74 m_buffer = new wxBitmap(width+20, height+20);
75 }
76
77 return m_buffer;
78 }
79
80private:
81 wxBitmap* m_buffer;
82};
83
84static wxSharedDCBufferManager gs_sharedDCBufferManager;
85
86// ============================================================================
87// wxBufferedDC
88// ============================================================================
89
90// Blits the buffer to the dc, and detaches the dc from the buffer (so it
91// can be effectively used once only).
92//
93// Usually called in the dtor or by the dtor of derived classes if the
94// BufferedDC must blit before the derived class (which may own the dc it's
95// blitting to) is destroyed.
96void wxBufferedDC::UnMask()
97{
98 if ( m_buffer )
99 {
100 wxASSERT_MSG( m_mainDc != NULL,
101 _T("No underlying DC associated with wxBufferedDC (anymore)") );
102
103 wxDC* bufferDc = DetachDC();
104
105 wxASSERT( bufferDc->IsKindOf(CLASSINFO(wxMemoryDC)) );
106
107 wxCoord x=0, y=0;
108
109 if (m_style & wxBUFFER_CLIENT_AREA)
110 bufferDc->GetDeviceOrigin(& x, & y);
111
112 m_mainDc->Blit( 0, 0,
113 m_buffer->GetWidth(), m_buffer->GetHeight(), bufferDc,
114 -x, -y );
115 m_mainDc = NULL;
116 m_buffer = NULL;
117 delete bufferDc;
118 }
119}
120
121void wxBufferedDC::PrepareBuffer(wxWindow* win, const wxSize& area)
122{
123 m_buffer = gs_sharedDCBufferManager.GetBuffer(win, area);
124}
125
126void wxBufferedDC::UseBuffer()
127{
128 wxASSERT(m_buffer);
129
130 wxMemoryDC* memoryDc = new wxMemoryDC(m_mainDc);
131 memoryDc->SelectObject(*m_buffer);
132
133 AttachDC(memoryDc);
134}
135
136