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