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