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