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