wxImage::Rotate corrections added; docview improvements (menus updated properly)
[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 and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dcmemory.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/utils.h"
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/msw/private.h"
37
38 #include "wx/dcmemory.h"
39
40 // ----------------------------------------------------------------------------
41 // wxWin macros
42 // ----------------------------------------------------------------------------
43
44 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC)
45
46 // ============================================================================
47 // implementation
48 // ============================================================================
49
50 // ----------------------------------------------------------------------------
51 // wxMemoryDC
52 // ----------------------------------------------------------------------------
53
54 wxMemoryDC::wxMemoryDC()
55 {
56 m_hDC = (WXHDC) ::CreateCompatibleDC((HDC) NULL);
57 m_ok = (m_hDC != 0);
58 m_bOwnsDC = TRUE;
59
60 SetBrush(*wxWHITE_BRUSH);
61 SetPen(*wxBLACK_PEN);
62
63 // the background mode is only used for text background and is set in
64 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT
65 ::SetBkMode( GetHdc(), TRANSPARENT );
66 }
67
68 wxMemoryDC::wxMemoryDC(wxDC *old_dc)
69 {
70 old_dc->BeginDrawing();
71
72 m_hDC = (WXHDC) ::CreateCompatibleDC(GetHdcOf(*old_dc));
73 m_ok = (m_hDC != 0);
74
75 old_dc->EndDrawing();
76
77 SetBrush(*wxWHITE_BRUSH);
78 SetPen(*wxBLACK_PEN);
79
80 // the background mode is only used for text background and is set in
81 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT
82 ::SetBkMode( GetHdc(), TRANSPARENT );
83 }
84
85 wxMemoryDC::~wxMemoryDC()
86 {
87 }
88
89 void wxMemoryDC::SelectObject(const wxBitmap& bitmap)
90 {
91 // select old bitmap out of the device context
92 if ( m_oldBitmap )
93 {
94 ::SelectObject(GetHdc(), (HBITMAP) m_oldBitmap);
95 if ( m_selectedBitmap.Ok() )
96 {
97 m_selectedBitmap.SetSelectedInto(NULL);
98 m_selectedBitmap = wxNullBitmap;
99 }
100 }
101
102 // check for whether the bitmap is already selected into a device context
103 wxCHECK_RET( !bitmap.GetSelectedInto() ||
104 (bitmap.GetSelectedInto() == this),
105 wxT("Bitmap is selected in another wxMemoryDC, delete the "
106 "first wxMemoryDC or use SelectObject(NULL)") );
107
108 m_selectedBitmap = bitmap;
109 WXHBITMAP hBmp = m_selectedBitmap.GetHBITMAP();
110 if ( !hBmp )
111 return;
112
113 m_selectedBitmap.SetSelectedInto(this);
114 hBmp = (WXHBITMAP)::SelectObject(GetHdc(), (HBITMAP)hBmp);
115
116 if ( !hBmp )
117 {
118 wxLogLastError("SelectObject(memDC, bitmap)");
119
120 wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
121 }
122 else if ( !m_oldBitmap )
123 {
124 m_oldBitmap = hBmp;
125 }
126 }
127
128 void wxMemoryDC::DoGetSize(int *width, int *height) const
129 {
130 if ( m_selectedBitmap.Ok() )
131 {
132 *width = m_selectedBitmap.GetWidth();
133 *height = m_selectedBitmap.GetHeight();
134 }
135 else
136 {
137 *width = 0;
138 *height = 0;
139 }
140 }
141
142 // For some reason, drawing a rectangle on a memory DC has problems.
143 // Use this substitute if we can.
144 static void wxDrawRectangle(wxDC& dc, wxCoord x, wxCoord y, wxCoord width, wxCoord height)
145 {
146 wxBrush brush(dc.GetBrush());
147 wxPen pen(dc.GetPen());
148 if (brush.Ok() && brush.GetStyle() != wxTRANSPARENT)
149 {
150 HBRUSH hBrush = (HBRUSH) brush.GetResourceHandle() ;
151 if (hBrush)
152 {
153 RECT rect;
154 rect.left = x; rect.top = y;
155 rect.right = x + width - 1;
156 rect.bottom = y + height - 1;
157 ::FillRect((HDC) dc.GetHDC(), &rect, hBrush);
158 }
159 }
160 width --; height --;
161 if (pen.Ok() && pen.GetStyle() != wxTRANSPARENT)
162 {
163 dc.DrawLine(x, y, x + width, y);
164 dc.DrawLine(x, y, x, y + height);
165 dc.DrawLine(x, y+height, x+width, y + height);
166 dc.DrawLine(x+width, y+height, x+width, y);
167 }
168 }
169
170 void wxMemoryDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
171 {
172 // Set this to 1 to work around an apparent video driver bug
173 // (visible with e.g. 70x70 rectangle on a memory DC; see Drawing sample)
174 #if 0
175 if (m_brush.Ok() && m_pen.Ok() &&
176 (m_brush.GetStyle() == wxSOLID || m_brush.GetStyle() == wxTRANSPARENT) &&
177 (m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT) &&
178 (GetLogicalFunction() == wxCOPY))
179 {
180 wxDrawRectangle(* this, x, y, width, height);
181 }
182 else
183 {
184 wxDC::DoDrawRectangle(x, y, width, height);
185 }
186 #else
187 wxDC::DoDrawRectangle(x, y, width, height);
188 #endif
189 }
190