]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcmemory.cpp
fix memory leak (coverity checker CID 53)
[wxWidgets.git] / src / msw / dcmemory.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: dcmemory.cpp
3// Purpose: wxMemoryDC class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
d59ceba5
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
d59ceba5 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
27#ifndef WX_PRECOMP
d59ceba5 28 #include "wx/utils.h"
3f1c6a14 29 #include "wx/log.h"
2bda0e17
KB
30#endif
31
c45a644e 32#include "wx/msw/private.h"
2bda0e17 33
c45a644e 34#include "wx/dcmemory.h"
2bda0e17 35
d59ceba5
VZ
36// ----------------------------------------------------------------------------
37// wxWin macros
38// ----------------------------------------------------------------------------
39
983a3844 40IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC)
2bda0e17 41
d59ceba5
VZ
42// ============================================================================
43// implementation
44// ============================================================================
2bda0e17 45
d59ceba5
VZ
46// ----------------------------------------------------------------------------
47// wxMemoryDC
48// ----------------------------------------------------------------------------
2bda0e17 49
d59ceba5
VZ
50wxMemoryDC::wxMemoryDC()
51{
7ba4fbeb 52 CreateCompatible(NULL);
c45a644e 53
7ba4fbeb 54 Init();
2bda0e17
KB
55}
56
7ba4fbeb 57wxMemoryDC::wxMemoryDC(wxDC *dc)
2bda0e17 58{
7ba4fbeb 59 wxCHECK_RET( dc, _T("NULL dc in wxMemoryDC ctor") );
2bda0e17 60
7ba4fbeb 61 CreateCompatible(dc);
2bda0e17 62
7ba4fbeb
VZ
63 Init();
64}
65
66void wxMemoryDC::Init()
67{
68 if ( m_ok )
69 {
70 SetBrush(*wxWHITE_BRUSH);
71 SetPen(*wxBLACK_PEN);
72
73 // the background mode is only used for text background and is set in
74 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT
75 ::SetBkMode( GetHdc(), TRANSPARENT );
76 }
2bda0e17
KB
77}
78
7ba4fbeb 79bool wxMemoryDC::CreateCompatible(wxDC *dc)
2bda0e17 80{
7ba4fbeb
VZ
81 m_hDC = (WXHDC)::CreateCompatibleDC(dc ? GetHdcOf(*dc) : NULL);
82
83 // as we created the DC, we must delete it in the dtor
d71cc120 84 m_bOwnsDC = true;
7ba4fbeb
VZ
85
86 m_ok = m_hDC != 0;
87
88 return m_ok;
2bda0e17
KB
89}
90
91void wxMemoryDC::SelectObject(const wxBitmap& bitmap)
92{
d59ceba5
VZ
93 // select old bitmap out of the device context
94 if ( m_oldBitmap )
2bda0e17 95 {
d59ceba5
VZ
96 ::SelectObject(GetHdc(), (HBITMAP) m_oldBitmap);
97 if ( m_selectedBitmap.Ok() )
98 {
3ca22d5e 99#ifdef __WXDEBUG__
d59ceba5 100 m_selectedBitmap.SetSelectedInto(NULL);
3ca22d5e 101#endif
d59ceba5
VZ
102 m_selectedBitmap = wxNullBitmap;
103 }
104 }
105
106 // check for whether the bitmap is already selected into a device context
336b8a42
VZ
107 wxASSERT_MSG( !bitmap.GetSelectedInto() ||
108 (bitmap.GetSelectedInto() == this),
109 wxT("Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)") );
d59ceba5
VZ
110
111 m_selectedBitmap = bitmap;
112 WXHBITMAP hBmp = m_selectedBitmap.GetHBITMAP();
113 if ( !hBmp )
114 return;
115
3ca22d5e 116#ifdef __WXDEBUG__
d59ceba5 117 m_selectedBitmap.SetSelectedInto(this);
3ca22d5e 118#endif
d59ceba5
VZ
119 hBmp = (WXHBITMAP)::SelectObject(GetHdc(), (HBITMAP)hBmp);
120
121 if ( !hBmp )
122 {
f6bcfd97 123 wxLogLastError(wxT("SelectObject(memDC, bitmap)"));
d59ceba5
VZ
124
125 wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
126 }
127 else if ( !m_oldBitmap )
128 {
129 m_oldBitmap = hBmp;
2bda0e17 130 }
2bda0e17
KB
131}
132
953ccd3d 133void wxMemoryDC::DoGetSize(int *width, int *height) const
2bda0e17 134{
d59ceba5
VZ
135 if ( m_selectedBitmap.Ok() )
136 {
137 *width = m_selectedBitmap.GetWidth();
138 *height = m_selectedBitmap.GetHeight();
139 }
140 else
141 {
142 *width = 0;
143 *height = 0;
144 }
2bda0e17
KB
145}
146
983a3844
VZ
147// the rest of this file deals with drawing rectangles workaround, disabled by
148// default
149
150#define wxUSE_MEMORY_DC_DRAW_RECTANGLE 0
151
152#if wxUSE_MEMORY_DC_DRAW_RECTANGLE
153
d6f0a4b3
JS
154// For some reason, drawing a rectangle on a memory DC has problems.
155// Use this substitute if we can.
156static void wxDrawRectangle(wxDC& dc, wxCoord x, wxCoord y, wxCoord width, wxCoord height)
157{
158 wxBrush brush(dc.GetBrush());
159 wxPen pen(dc.GetPen());
160 if (brush.Ok() && brush.GetStyle() != wxTRANSPARENT)
161 {
162 HBRUSH hBrush = (HBRUSH) brush.GetResourceHandle() ;
163 if (hBrush)
164 {
165 RECT rect;
166 rect.left = x; rect.top = y;
167 rect.right = x + width - 1;
168 rect.bottom = y + height - 1;
169 ::FillRect((HDC) dc.GetHDC(), &rect, hBrush);
170 }
171 }
172 width --; height --;
173 if (pen.Ok() && pen.GetStyle() != wxTRANSPARENT)
174 {
175 dc.DrawLine(x, y, x + width, y);
176 dc.DrawLine(x, y, x, y + height);
177 dc.DrawLine(x, y+height, x+width, y + height);
178 dc.DrawLine(x+width, y+height, x+width, y);
179 }
180}
181
983a3844
VZ
182#endif // wxUSE_MEMORY_DC_DRAW_RECTANGLE
183
d6f0a4b3
JS
184void wxMemoryDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
185{
f2506310
JS
186 // Set this to 1 to work around an apparent video driver bug
187 // (visible with e.g. 70x70 rectangle on a memory DC; see Drawing sample)
983a3844 188#if wxUSE_MEMORY_DC_DRAW_RECTANGLE
d6f0a4b3
JS
189 if (m_brush.Ok() && m_pen.Ok() &&
190 (m_brush.GetStyle() == wxSOLID || m_brush.GetStyle() == wxTRANSPARENT) &&
f2506310
JS
191 (m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT) &&
192 (GetLogicalFunction() == wxCOPY))
d6f0a4b3
JS
193 {
194 wxDrawRectangle(* this, x, y, width, height);
195 }
196 else
983a3844 197#endif // wxUSE_MEMORY_DC_DRAW_RECTANGLE
d6f0a4b3
JS
198 {
199 wxDC::DoDrawRectangle(x, y, width, height);
200 }
d6f0a4b3
JS
201}
202