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