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
// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7
// Licence: wxWindows licence
8
///////////////////////////////////////////////////////////////////////////////
10
// ============================================================================
12
// ============================================================================
14
// ----------------------------------------------------------------------------
16
// ----------------------------------------------------------------------------
18
// for compilers that support precompilation, includes "wx.h".
19
#include "wx/wxprec.h"
25
// This class can't be implemented and hence is not used under Win CE.
29
#include "wx/dcclient.h"
30
#include "wx/frame.h" // Only for wxFRAME_SHAPED.
31
#include "wx/region.h"
32
#include "wx/msw/private.h"
35
#include "wx/nonownedwnd.h"
37
#if wxUSE_GRAPHICS_CONTEXT
38
#include "wx/msw/wrapgdip.h"
39
#include "wx/graphics.h"
40
#endif // wxUSE_GRAPHICS_CONTEXT
42
#include "wx/scopedptr.h"
44
// ============================================================================
45
// wxNonOwnedWindow implementation
46
// ============================================================================
48
bool wxNonOwnedWindow::DoClearShape()
50
if (::SetWindowRgn(GetHwnd(), NULL, TRUE) == 0)
52
wxLogLastError(wxT("SetWindowRgn"));
59
bool wxNonOwnedWindow::DoSetRegionShape(const wxRegion& region)
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;
69
// SetWindowRgn expects the region to be in coordinates
70
// relative to the window, not the client area.
71
const wxPoint clientOrigin = GetClientAreaOrigin();
72
::OffsetRgn(hrgn, -clientOrigin.x, -clientOrigin.y);
74
// Now call the shape API with the new region.
75
if (::SetWindowRgn(GetHwnd(), hrgn, TRUE) == 0)
77
wxLogLastError(wxT("SetWindowRgn"));
83
#if wxUSE_GRAPHICS_CONTEXT
85
#include "wx/msw/wrapgdip.h"
87
// This class contains data used only when SetPath(wxGraphicsPath) is called.
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.
92
class wxNonOwnedWindowShapeImpl : public wxEvtHandler
95
wxNonOwnedWindowShapeImpl(wxNonOwnedWindow* win, const wxGraphicsPath& path) :
99
// Create the region corresponding to this path and set it as windows
101
wxScopedPtr<wxGraphicsContext> context(wxGraphicsContext::Create(win));
102
Region gr(static_cast<GraphicsPath*>(m_path.GetNativePath()));
105
gr.GetHRGN(static_cast<Graphics*>(context->GetNativeContext()))
110
// Connect to the paint event to draw the border.
112
// TODO: Do this only optionally?
116
wxPaintEventHandler(wxNonOwnedWindowShapeImpl::OnPaint),
122
virtual ~wxNonOwnedWindowShapeImpl()
127
wxPaintEventHandler(wxNonOwnedWindowShapeImpl::OnPaint),
134
void OnPaint(wxPaintEvent& event)
139
wxScopedPtr<wxGraphicsContext> context(wxGraphicsContext::Create(dc));
140
context->SetPen(wxPen(*wxLIGHT_GREY, 2));
141
context->StrokePath(m_path);
144
wxNonOwnedWindow* const m_win;
145
wxGraphicsPath m_path;
147
wxDECLARE_NO_COPY_CLASS(wxNonOwnedWindowShapeImpl);
150
wxNonOwnedWindow::wxNonOwnedWindow()
155
wxNonOwnedWindow::~wxNonOwnedWindow()
160
bool wxNonOwnedWindow::DoSetPathShape(const wxGraphicsPath& path)
163
m_shapeImpl = new wxNonOwnedWindowShapeImpl(this, path);
168
#else // !wxUSE_GRAPHICS_CONTEXT
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.
173
wxNonOwnedWindow::wxNonOwnedWindow()
177
wxNonOwnedWindow::~wxNonOwnedWindow()
181
#endif // wxUSE_GRAPHICS_CONTEXT/!wxUSE_GRAPHICS_CONTEXT
183
#endif // !__WXWINCE__