]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcmemory.cpp
simplify code so it always returns the same object
[wxWidgets.git] / src / msw / dcmemory.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
f38924e8 2// Name: src/msw/dcmemory.cpp
2bda0e17
KB
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
f38924e8 27#include "wx/dcmemory.h"
888dde65 28#include "wx/msw/dcmemory.h"
f38924e8 29
2bda0e17 30#ifndef WX_PRECOMP
d59ceba5 31 #include "wx/utils.h"
3f1c6a14 32 #include "wx/log.h"
2bda0e17
KB
33#endif
34
c45a644e 35#include "wx/msw/private.h"
2bda0e17 36
d59ceba5 37// ----------------------------------------------------------------------------
888dde65 38// wxMemoryDCImpl
d59ceba5
VZ
39// ----------------------------------------------------------------------------
40
888dde65 41IMPLEMENT_ABSTRACT_CLASS(wxMemoryDCImpl, wxMSWDCImpl)
2bda0e17 42
03647350 43wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner )
888dde65
RR
44 : wxMSWDCImpl( owner )
45{
03647350
VZ
46 CreateCompatible(NULL);
47 Init();
888dde65 48}
2bda0e17 49
03647350
VZ
50wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner, wxBitmap& bitmap )
51 : wxMSWDCImpl( owner )
52{
53 CreateCompatible(NULL);
54 Init();
888dde65
RR
55 DoSelect(bitmap);
56}
2bda0e17 57
888dde65 58wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC *owner, wxDC *dc )
03647350 59 : wxMSWDCImpl( owner )
2bda0e17 60{
9a83f860 61 wxCHECK_RET( dc, wxT("NULL dc in wxMemoryDC ctor") );
2bda0e17 62
7ba4fbeb 63 CreateCompatible(dc);
2bda0e17 64
7ba4fbeb
VZ
65 Init();
66}
67
888dde65 68void wxMemoryDCImpl::Init()
7ba4fbeb
VZ
69{
70 if ( m_ok )
71 {
72 SetBrush(*wxWHITE_BRUSH);
73 SetPen(*wxBLACK_PEN);
74
75 // the background mode is only used for text background and is set in
76 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT
77 ::SetBkMode( GetHdc(), TRANSPARENT );
78 }
2bda0e17
KB
79}
80
888dde65 81bool wxMemoryDCImpl::CreateCompatible(wxDC *dc)
2bda0e17 82{
19a1d9ee 83 wxDCImpl *impl = dc ? dc->GetImpl() : NULL ;
888dde65 84 wxMSWDCImpl *msw_impl = wxDynamicCast( impl, wxMSWDCImpl );
19a1d9ee 85 if ( dc && !msw_impl)
888dde65
RR
86 {
87 m_ok = false;
88 return false;
89 }
90
91 m_hDC = (WXHDC)::CreateCompatibleDC(dc ? GetHdcOf(*msw_impl) : NULL);
7ba4fbeb
VZ
92
93 // as we created the DC, we must delete it in the dtor
d71cc120 94 m_bOwnsDC = true;
7ba4fbeb
VZ
95
96 m_ok = m_hDC != 0;
97
98 return m_ok;
2bda0e17
KB
99}
100
888dde65 101void wxMemoryDCImpl::DoSelect( const wxBitmap& bitmap )
2bda0e17 102{
d59ceba5
VZ
103 // select old bitmap out of the device context
104 if ( m_oldBitmap )
2bda0e17 105 {
d59ceba5 106 ::SelectObject(GetHdc(), (HBITMAP) m_oldBitmap);
a1b806b9 107 if ( m_selectedBitmap.IsOk() )
d59ceba5
VZ
108 {
109 m_selectedBitmap.SetSelectedInto(NULL);
110 m_selectedBitmap = wxNullBitmap;
111 }
112 }
113
114 // check for whether the bitmap is already selected into a device context
336b8a42 115 wxASSERT_MSG( !bitmap.GetSelectedInto() ||
196fc5f2 116 (bitmap.GetSelectedInto() == GetOwner()),
336b8a42 117 wxT("Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)") );
d59ceba5
VZ
118
119 m_selectedBitmap = bitmap;
120 WXHBITMAP hBmp = m_selectedBitmap.GetHBITMAP();
121 if ( !hBmp )
122 return;
123
196fc5f2 124 m_selectedBitmap.SetSelectedInto(GetOwner());
d59ceba5
VZ
125 hBmp = (WXHBITMAP)::SelectObject(GetHdc(), (HBITMAP)hBmp);
126
127 if ( !hBmp )
128 {
f6bcfd97 129 wxLogLastError(wxT("SelectObject(memDC, bitmap)"));
d59ceba5
VZ
130
131 wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
132 }
133 else if ( !m_oldBitmap )
134 {
135 m_oldBitmap = hBmp;
2bda0e17 136 }
2bda0e17
KB
137}
138
888dde65 139void wxMemoryDCImpl::DoGetSize(int *width, int *height) const
2bda0e17 140{
a1b806b9 141 if ( m_selectedBitmap.IsOk() )
d59ceba5
VZ
142 {
143 *width = m_selectedBitmap.GetWidth();
144 *height = m_selectedBitmap.GetHeight();
145 }
146 else
147 {
148 *width = 0;
149 *height = 0;
150 }
2bda0e17
KB
151}
152
983a3844
VZ
153// the rest of this file deals with drawing rectangles workaround, disabled by
154// default
155
156#define wxUSE_MEMORY_DC_DRAW_RECTANGLE 0
157
158#if wxUSE_MEMORY_DC_DRAW_RECTANGLE
159
d6f0a4b3
JS
160// For some reason, drawing a rectangle on a memory DC has problems.
161// Use this substitute if we can.
162static void wxDrawRectangle(wxDC& dc, wxCoord x, wxCoord y, wxCoord width, wxCoord height)
163{
164 wxBrush brush(dc.GetBrush());
165 wxPen pen(dc.GetPen());
a1b806b9 166 if (brush.IsOk() && brush.GetStyle() != wxTRANSPARENT)
d6f0a4b3
JS
167 {
168 HBRUSH hBrush = (HBRUSH) brush.GetResourceHandle() ;
169 if (hBrush)
170 {
171 RECT rect;
172 rect.left = x; rect.top = y;
173 rect.right = x + width - 1;
174 rect.bottom = y + height - 1;
175 ::FillRect((HDC) dc.GetHDC(), &rect, hBrush);
176 }
177 }
178 width --; height --;
a1b806b9 179 if (pen.IsOk() && pen.GetStyle() != wxTRANSPARENT)
d6f0a4b3
JS
180 {
181 dc.DrawLine(x, y, x + width, y);
182 dc.DrawLine(x, y, x, y + height);
183 dc.DrawLine(x, y+height, x+width, y + height);
184 dc.DrawLine(x+width, y+height, x+width, y);
185 }
186}
187
983a3844
VZ
188#endif // wxUSE_MEMORY_DC_DRAW_RECTANGLE
189
888dde65 190void wxMemoryDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
d6f0a4b3 191{
f2506310
JS
192 // Set this to 1 to work around an apparent video driver bug
193 // (visible with e.g. 70x70 rectangle on a memory DC; see Drawing sample)
983a3844 194#if wxUSE_MEMORY_DC_DRAW_RECTANGLE
a1b806b9 195 if (m_brush.IsOk() && m_pen.IsOk() &&
d6f0a4b3 196 (m_brush.GetStyle() == wxSOLID || m_brush.GetStyle() == wxTRANSPARENT) &&
f2506310
JS
197 (m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT) &&
198 (GetLogicalFunction() == wxCOPY))
d6f0a4b3
JS
199 {
200 wxDrawRectangle(* this, x, y, width, height);
201 }
202 else
983a3844 203#endif // wxUSE_MEMORY_DC_DRAW_RECTANGLE
d6f0a4b3 204 {
888dde65 205 wxMSWDCImpl::DoDrawRectangle(x, y, width, height);
d6f0a4b3 206 }
d6f0a4b3 207}