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