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