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