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