]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dcmemory.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dcmemory.cpp
3 // Purpose: wxMemoryDC class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/dcmemory.h"
27 #include "wx/msw/dcmemory.h"
34 #include "wx/msw/private.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 IMPLEMENT_ABSTRACT_CLASS(wxMemoryDCImpl
, wxMSWDCImpl
)
42 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC
*owner
)
43 : wxMSWDCImpl( owner
)
45 CreateCompatible(NULL
);
49 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC
*owner
, wxBitmap
& bitmap
)
50 : wxMSWDCImpl( owner
)
52 CreateCompatible(NULL
);
57 wxMemoryDCImpl::wxMemoryDCImpl( wxMemoryDC
*owner
, wxDC
*dc
)
58 : wxMSWDCImpl( owner
)
60 wxCHECK_RET( dc
, wxT("NULL dc in wxMemoryDC ctor") );
67 void wxMemoryDCImpl::Init()
71 SetBrush(*wxWHITE_BRUSH
);
74 // the background mode is only used for text background and is set in
75 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT
76 ::SetBkMode( GetHdc(), TRANSPARENT
);
80 bool wxMemoryDCImpl::CreateCompatible(wxDC
*dc
)
82 wxDCImpl
*impl
= dc
? dc
->GetImpl() : NULL
;
83 wxMSWDCImpl
*msw_impl
= wxDynamicCast( impl
, wxMSWDCImpl
);
90 m_hDC
= (WXHDC
)::CreateCompatibleDC(dc
? GetHdcOf(*msw_impl
) : NULL
);
92 // as we created the DC, we must delete it in the dtor
100 void wxMemoryDCImpl::DoSelect( const wxBitmap
& bitmap
)
102 // select old bitmap out of the device context
105 ::SelectObject(GetHdc(), (HBITMAP
) m_oldBitmap
);
106 if ( m_selectedBitmap
.IsOk() )
108 m_selectedBitmap
.SetSelectedInto(NULL
);
109 m_selectedBitmap
= wxNullBitmap
;
113 // check for whether the bitmap is already selected into a device context
114 wxASSERT_MSG( !bitmap
.GetSelectedInto() ||
115 (bitmap
.GetSelectedInto() == GetOwner()),
116 wxT("Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)") );
118 m_selectedBitmap
= bitmap
;
119 WXHBITMAP hBmp
= m_selectedBitmap
.GetHBITMAP();
123 m_selectedBitmap
.SetSelectedInto(GetOwner());
124 hBmp
= (WXHBITMAP
)::SelectObject(GetHdc(), (HBITMAP
)hBmp
);
128 wxLogLastError(wxT("SelectObject(memDC, bitmap)"));
130 wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
132 else if ( !m_oldBitmap
)
138 void wxMemoryDCImpl::DoGetSize(int *width
, int *height
) const
140 if ( m_selectedBitmap
.IsOk() )
142 *width
= m_selectedBitmap
.GetWidth();
143 *height
= m_selectedBitmap
.GetHeight();
152 // the rest of this file deals with drawing rectangles workaround, disabled by
155 #define wxUSE_MEMORY_DC_DRAW_RECTANGLE 0
157 #if wxUSE_MEMORY_DC_DRAW_RECTANGLE
159 // For some reason, drawing a rectangle on a memory DC has problems.
160 // Use this substitute if we can.
161 static void wxDrawRectangle(wxDC
& dc
, wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
163 wxBrush
brush(dc
.GetBrush());
164 wxPen
pen(dc
.GetPen());
165 if (brush
.IsOk() && brush
.GetStyle() != wxTRANSPARENT
)
167 HBRUSH hBrush
= (HBRUSH
) brush
.GetResourceHandle() ;
171 rect
.left
= x
; rect
.top
= y
;
172 rect
.right
= x
+ width
- 1;
173 rect
.bottom
= y
+ height
- 1;
174 ::FillRect((HDC
) dc
.GetHDC(), &rect
, hBrush
);
178 if (pen
.IsOk() && pen
.GetStyle() != wxTRANSPARENT
)
180 dc
.DrawLine(x
, y
, x
+ width
, y
);
181 dc
.DrawLine(x
, y
, x
, y
+ height
);
182 dc
.DrawLine(x
, y
+height
, x
+width
, y
+ height
);
183 dc
.DrawLine(x
+width
, y
+height
, x
+width
, y
);
187 #endif // wxUSE_MEMORY_DC_DRAW_RECTANGLE
189 void wxMemoryDCImpl::DoDrawRectangle(wxCoord x
, wxCoord y
, wxCoord width
, wxCoord height
)
191 // Set this to 1 to work around an apparent video driver bug
192 // (visible with e.g. 70x70 rectangle on a memory DC; see Drawing sample)
193 #if wxUSE_MEMORY_DC_DRAW_RECTANGLE
194 if (m_brush
.IsOk() && m_pen
.IsOk() &&
195 (m_brush
.GetStyle() == wxSOLID
|| m_brush
.GetStyle() == wxTRANSPARENT
) &&
196 (m_pen
.GetStyle() == wxSOLID
|| m_pen
.GetStyle() == wxTRANSPARENT
) &&
197 (GetLogicalFunction() == wxCOPY
))
199 wxDrawRectangle(* this, x
, y
, width
, height
);
202 #endif // wxUSE_MEMORY_DC_DRAW_RECTANGLE
204 wxMSWDCImpl::DoDrawRectangle(x
, y
, width
, height
);