]> git.saurik.com Git - wxWidgets.git/blame - src/msw/nonownedwnd.cpp
Correct making the newly inserted menu item owner drawn in some cases.
[wxWidgets.git] / src / msw / nonownedwnd.cpp
CommitLineData
5bd0ee99
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/nonownedwnd.cpp
3// Purpose: wxNonOwnedWindow implementation for MSW.
4// Author: Vadim Zeitlin
5// Created: 2011-10-09 (extracted from src/msw/toplevel.cpp)
5bd0ee99
VZ
6// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7// Licence: wxWindows licence
8///////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
18// for compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
22 #pragma hdrstop
23#endif
24
164db177
VZ
25// This class can't be implemented and hence is not used under Win CE.
26#ifndef __WXWINCE__
27
5bd0ee99 28#ifndef WX_PRECOMP
97b18a77 29 #include "wx/dcclient.h"
3648be1f
VZ
30 #include "wx/frame.h" // Only for wxFRAME_SHAPED.
31 #include "wx/region.h"
32 #include "wx/msw/private.h"
5bd0ee99
VZ
33#endif // WX_PRECOMP
34
35#include "wx/nonownedwnd.h"
36
3bcb0be2
VZ
37#if wxUSE_GRAPHICS_CONTEXT
38 #include "wx/msw/wrapgdip.h"
39 #include "wx/graphics.h"
40#endif // wxUSE_GRAPHICS_CONTEXT
41
46ea442c
VZ
42#include "wx/scopedptr.h"
43
5bd0ee99
VZ
44// ============================================================================
45// wxNonOwnedWindow implementation
46// ============================================================================
47
46ea442c 48bool wxNonOwnedWindow::DoClearShape()
5bd0ee99 49{
46ea442c 50 if (::SetWindowRgn(GetHwnd(), NULL, TRUE) == 0)
5bd0ee99 51 {
46ea442c
VZ
52 wxLogLastError(wxT("SetWindowRgn"));
53 return false;
5bd0ee99
VZ
54 }
55
46ea442c
VZ
56 return true;
57}
58
59bool wxNonOwnedWindow::DoSetRegionShape(const wxRegion& region)
60{
5bd0ee99
VZ
61 // Windows takes ownership of the region, so
62 // we'll have to make a copy of the region to give to it.
63 DWORD noBytes = ::GetRegionData(GetHrgnOf(region), 0, NULL);
64 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
65 ::GetRegionData(GetHrgnOf(region), noBytes, rgnData);
66 HRGN hrgn = ::ExtCreateRegion(NULL, noBytes, rgnData);
67 delete[] (char*) rgnData;
68
69 // SetWindowRgn expects the region to be in coordinates
6a68fe84
VZ
70 // relative to the window, not the client area.
71 const wxPoint clientOrigin = GetClientAreaOrigin();
72 ::OffsetRgn(hrgn, -clientOrigin.x, -clientOrigin.y);
5bd0ee99
VZ
73
74 // Now call the shape API with the new region.
75 if (::SetWindowRgn(GetHwnd(), hrgn, TRUE) == 0)
76 {
77 wxLogLastError(wxT("SetWindowRgn"));
78 return false;
79 }
80 return true;
81}
82
46ea442c
VZ
83#if wxUSE_GRAPHICS_CONTEXT
84
85#include "wx/msw/wrapgdip.h"
86
87// This class contains data used only when SetPath(wxGraphicsPath) is called.
88//
89// Notice that it derives from wxEvtHandler solely to allow Connect()-ing its
90// OnPaint() method to the window, we could get rid of this inheritance once
91// Bind() can be used in wx sources.
92class wxNonOwnedWindowShapeImpl : public wxEvtHandler
93{
94public:
95 wxNonOwnedWindowShapeImpl(wxNonOwnedWindow* win, const wxGraphicsPath& path) :
96 m_win(win),
97 m_path(path)
98 {
99 // Create the region corresponding to this path and set it as windows
100 // shape.
101 wxScopedPtr<wxGraphicsContext> context(wxGraphicsContext::Create(win));
102 Region gr(static_cast<GraphicsPath*>(m_path.GetNativePath()));
103 win->SetShape(
104 wxRegion(
105 gr.GetHRGN(static_cast<Graphics*>(context->GetNativeContext()))
106 )
107 );
108
109
110 // Connect to the paint event to draw the border.
111 //
112 // TODO: Do this only optionally?
113 m_win->Connect
114 (
115 wxEVT_PAINT,
116 wxPaintEventHandler(wxNonOwnedWindowShapeImpl::OnPaint),
117 NULL,
118 this
119 );
120 }
121
122 virtual ~wxNonOwnedWindowShapeImpl()
123 {
124 m_win->Disconnect
125 (
126 wxEVT_PAINT,
127 wxPaintEventHandler(wxNonOwnedWindowShapeImpl::OnPaint),
128 NULL,
129 this
130 );
131 }
132
133private:
134 void OnPaint(wxPaintEvent& event)
135 {
136 event.Skip();
137
138 wxPaintDC dc(m_win);
139 wxScopedPtr<wxGraphicsContext> context(wxGraphicsContext::Create(dc));
140 context->SetPen(wxPen(*wxLIGHT_GREY, 2));
141 context->StrokePath(m_path);
142 }
143
144 wxNonOwnedWindow* const m_win;
145 wxGraphicsPath m_path;
146
147 wxDECLARE_NO_COPY_CLASS(wxNonOwnedWindowShapeImpl);
148};
149
150wxNonOwnedWindow::wxNonOwnedWindow()
151{
152 m_shapeImpl = NULL;
153}
154
155wxNonOwnedWindow::~wxNonOwnedWindow()
156{
157 delete m_shapeImpl;
158}
159
160bool wxNonOwnedWindow::DoSetPathShape(const wxGraphicsPath& path)
161{
162 delete m_shapeImpl;
163 m_shapeImpl = new wxNonOwnedWindowShapeImpl(this, path);
164
165 return true;
166}
167
168#else // !wxUSE_GRAPHICS_CONTEXT
169
170// Trivial ctor and dtor as we don't have anything to do when wxGraphicsContext
171// is not used but still define them here to avoid adding even more #if checks
172// to the header, it it doesn't do any harm even though it's not needed.
173wxNonOwnedWindow::wxNonOwnedWindow()
174{
175}
176
177wxNonOwnedWindow::~wxNonOwnedWindow()
178{
179}
180
181#endif // wxUSE_GRAPHICS_CONTEXT/!wxUSE_GRAPHICS_CONTEXT
182
5bd0ee99 183#endif // !__WXWINCE__