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