]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
27 | #ifndef WX_PRECOMP | |
d59ceba5 | 28 | #include "wx/utils.h" |
3f1c6a14 | 29 | #include "wx/log.h" |
2bda0e17 KB |
30 | #endif |
31 | ||
c45a644e | 32 | #include "wx/msw/private.h" |
2bda0e17 | 33 | |
c45a644e | 34 | #include "wx/dcmemory.h" |
2bda0e17 | 35 | |
d59ceba5 VZ |
36 | // ---------------------------------------------------------------------------- |
37 | // wxWin macros | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
983a3844 | 40 | IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC) |
2bda0e17 | 41 | |
d59ceba5 VZ |
42 | // ============================================================================ |
43 | // implementation | |
44 | // ============================================================================ | |
2bda0e17 | 45 | |
d59ceba5 VZ |
46 | // ---------------------------------------------------------------------------- |
47 | // wxMemoryDC | |
48 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 49 | |
d59ceba5 VZ |
50 | wxMemoryDC::wxMemoryDC() |
51 | { | |
7ba4fbeb | 52 | CreateCompatible(NULL); |
c45a644e | 53 | |
7ba4fbeb | 54 | Init(); |
2bda0e17 KB |
55 | } |
56 | ||
7ba4fbeb | 57 | wxMemoryDC::wxMemoryDC(wxDC *dc) |
2bda0e17 | 58 | { |
7ba4fbeb | 59 | wxCHECK_RET( dc, _T("NULL dc in wxMemoryDC ctor") ); |
2bda0e17 | 60 | |
7ba4fbeb | 61 | dc->BeginDrawing(); |
2bda0e17 | 62 | |
7ba4fbeb | 63 | CreateCompatible(dc); |
2bda0e17 | 64 | |
7ba4fbeb | 65 | dc->EndDrawing(); |
c45a644e | 66 | |
7ba4fbeb VZ |
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 | } | |
2bda0e17 KB |
81 | } |
82 | ||
7ba4fbeb | 83 | bool wxMemoryDC::CreateCompatible(wxDC *dc) |
2bda0e17 | 84 | { |
7ba4fbeb VZ |
85 | m_hDC = (WXHDC)::CreateCompatibleDC(dc ? GetHdcOf(*dc) : NULL); |
86 | ||
87 | // as we created the DC, we must delete it in the dtor | |
d71cc120 | 88 | m_bOwnsDC = true; |
7ba4fbeb VZ |
89 | |
90 | m_ok = m_hDC != 0; | |
91 | ||
92 | return m_ok; | |
2bda0e17 KB |
93 | } |
94 | ||
95 | void wxMemoryDC::SelectObject(const wxBitmap& bitmap) | |
96 | { | |
d59ceba5 VZ |
97 | // select old bitmap out of the device context |
98 | if ( m_oldBitmap ) | |
2bda0e17 | 99 | { |
d59ceba5 VZ |
100 | ::SelectObject(GetHdc(), (HBITMAP) m_oldBitmap); |
101 | if ( m_selectedBitmap.Ok() ) | |
102 | { | |
3ca22d5e | 103 | #ifdef __WXDEBUG__ |
d59ceba5 | 104 | m_selectedBitmap.SetSelectedInto(NULL); |
3ca22d5e | 105 | #endif |
d59ceba5 VZ |
106 | m_selectedBitmap = wxNullBitmap; |
107 | } | |
108 | } | |
109 | ||
110 | // check for whether the bitmap is already selected into a device context | |
336b8a42 VZ |
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)") ); | |
d59ceba5 VZ |
114 | |
115 | m_selectedBitmap = bitmap; | |
116 | WXHBITMAP hBmp = m_selectedBitmap.GetHBITMAP(); | |
117 | if ( !hBmp ) | |
118 | return; | |
119 | ||
3ca22d5e | 120 | #ifdef __WXDEBUG__ |
d59ceba5 | 121 | m_selectedBitmap.SetSelectedInto(this); |
3ca22d5e | 122 | #endif |
d59ceba5 VZ |
123 | hBmp = (WXHBITMAP)::SelectObject(GetHdc(), (HBITMAP)hBmp); |
124 | ||
125 | if ( !hBmp ) | |
126 | { | |
f6bcfd97 | 127 | wxLogLastError(wxT("SelectObject(memDC, bitmap)")); |
d59ceba5 VZ |
128 | |
129 | wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC")); | |
130 | } | |
131 | else if ( !m_oldBitmap ) | |
132 | { | |
133 | m_oldBitmap = hBmp; | |
2bda0e17 | 134 | } |
2bda0e17 KB |
135 | } |
136 | ||
953ccd3d | 137 | void wxMemoryDC::DoGetSize(int *width, int *height) const |
2bda0e17 | 138 | { |
d59ceba5 VZ |
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 | } | |
2bda0e17 KB |
149 | } |
150 | ||
983a3844 VZ |
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 | ||
d6f0a4b3 JS |
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 | ||
983a3844 VZ |
186 | #endif // wxUSE_MEMORY_DC_DRAW_RECTANGLE |
187 | ||
d6f0a4b3 JS |
188 | void wxMemoryDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) |
189 | { | |
f2506310 JS |
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) | |
983a3844 | 192 | #if wxUSE_MEMORY_DC_DRAW_RECTANGLE |
d6f0a4b3 JS |
193 | if (m_brush.Ok() && m_pen.Ok() && |
194 | (m_brush.GetStyle() == wxSOLID || m_brush.GetStyle() == wxTRANSPARENT) && | |
f2506310 JS |
195 | (m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT) && |
196 | (GetLogicalFunction() == wxCOPY)) | |
d6f0a4b3 JS |
197 | { |
198 | wxDrawRectangle(* this, x, y, width, height); | |
199 | } | |
200 | else | |
983a3844 | 201 | #endif // wxUSE_MEMORY_DC_DRAW_RECTANGLE |
d6f0a4b3 JS |
202 | { |
203 | wxDC::DoDrawRectangle(x, y, width, height); | |
204 | } | |
d6f0a4b3 JS |
205 | } |
206 |