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