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