/////////////////////////////////////////////////////////////////////////////
-// Name: msw/region.cpp
-// Purpose: Region handling for wxWindows/X11
-// Author: Markus Holzem
+// Name: src/msw/region.cpp
+// Purpose: wxRegion implementation using Win32 API
+// Author: Vadim Zeitlin
// Modified by:
// Created: Fri Oct 24 10:46:34 MET 1997
-// RCS-ID: $Id$
-// Copyright: (c) 1997 Julian Smart and Markus Holzem
+// RCS-ID: $Id$
+// Copyright: (c) 1997-2002 wxWidgets team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
-#pragma implementation "region.h"
-#endif
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
-#pragma hdrstop
+ #pragma hdrstop
#endif
-#include "wx/msw/region.h"
-#include "wx/gdicmn.h"
+#include "wx/region.h"
-#include <windows.h>
-
-#if !USE_SHARED_LIBRARY
- IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
- IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
+#ifndef WX_PRECOMP
+ #include "wx/gdicmn.h"
#endif
-//-----------------------------------------------------------------------------
+#include "wx/msw/private.h"
+
+IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
+IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
+
+// ----------------------------------------------------------------------------
// wxRegionRefData implementation
-//-----------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
-class WXDLLEXPORT wxRegionRefData : public wxGDIRefData {
+class WXDLLEXPORT wxRegionRefData : public wxGDIRefData
+{
public:
- wxRegionRefData(void)
- {
+ wxRegionRefData()
+ {
m_region = 0;
- }
+ }
- wxRegionRefData(const wxRegionRefData& data)
- {
-#if defined(__WIN32__)
+ wxRegionRefData(const wxRegionRefData& data) : wxGDIRefData()
+ {
+#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
DWORD noBytes = ::GetRegionData(data.m_region, 0, NULL);
RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
- ::GetRegionData(data.m_region, noBytes, rgnData);
+ ::GetRegionData(data.m_region, noBytes, rgnData);
m_region = ::ExtCreateRegion(NULL, noBytes, rgnData);
delete[] (char*) rgnData;
#else
::GetRgnBox(data.m_region, &rect);
m_region = ::CreateRectRgnIndirect(&rect);
#endif
- }
+ }
- ~wxRegionRefData(void)
- {
- ::DeleteObject(m_region);
+ virtual ~wxRegionRefData()
+ {
+ ::DeleteObject(m_region);
m_region = 0;
- }
+ }
+
+ HRGN m_region;
- HRGN m_region;
+private:
+// Cannot use
+// wxDECLARE_NO_COPY_CLASS(wxRegionRefData);
+// because copy constructor is explicitly declared above;
+// but no copy assignment operator is defined, so declare
+// it private to prevent the compiler from defining it:
+ wxRegionRefData& operator=(const wxRegionRefData&);
};
#define M_REGION (((wxRegionRefData*)m_refData)->m_region)
+#define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_region)
-//-----------------------------------------------------------------------------
-// wxRegion
-//-----------------------------------------------------------------------------
+// ============================================================================
+// wxRegion implementation
+// ============================================================================
-/*!
- * Create an empty region.
- */
-wxRegion::wxRegion(void)
+// ----------------------------------------------------------------------------
+// ctors and dtor
+// ----------------------------------------------------------------------------
+
+wxRegion::wxRegion()
{
- m_refData = new wxRegionRefData;
- M_REGION = ::CreateRectRgn(0, 0, 0, 0);
+ m_refData = NULL;
}
wxRegion::wxRegion(WXHRGN hRegion)
M_REGION = (HRGN) hRegion;
}
-wxRegion::wxRegion(long x, long y, long w, long h)
+wxRegion::wxRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
{
m_refData = new wxRegionRefData;
M_REGION = ::CreateRectRgn(x, y, x + w, y + h);
wxRegion::wxRegion(const wxRect& rect)
{
m_refData = new wxRegionRefData;
- M_REGION = ::CreateRectRgn(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
+ M_REGION = ::CreateRectRgn(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
}
-/*!
- * Destroy the region.
- */
-wxRegion::~wxRegion(void)
+wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode fillStyle)
+{
+#if defined(__WXMICROWIN__) || defined(__WXWINCE__)
+ wxUnusedVar(n);
+ wxUnusedVar(points);
+ wxUnusedVar(fillStyle);
+ m_refData = NULL;
+ M_REGION = NULL;
+#else
+ m_refData = new wxRegionRefData;
+ M_REGION = ::CreatePolygonRgn
+ (
+ (POINT*)points,
+ n,
+ fillStyle == wxODDEVEN_RULE ? ALTERNATE : WINDING
+ );
+#endif
+}
+
+wxRegion::~wxRegion()
{
// m_refData unrefed in ~wxObject
}
-//-----------------------------------------------------------------------------
-//# Modify region
-//-----------------------------------------------------------------------------
+wxGDIRefData *wxRegion::CreateGDIRefData() const
+{
+ return new wxRegionRefData;
+}
+
+wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
+{
+ return new wxRegionRefData(*(wxRegionRefData *)data);
+}
+
+// ----------------------------------------------------------------------------
+// wxRegion operations
+// ----------------------------------------------------------------------------
-//! Clear current region
-void wxRegion::Clear(void)
+// Clear current region
+void wxRegion::Clear()
{
- UnRef();
+ UnRef();
}
-//! Combine rectangle (x, y, w, h) with this.
-bool wxRegion::Combine(long x, long y, long width, long height, wxRegionOp op)
+bool wxRegion::DoOffset(wxCoord x, wxCoord y)
{
- // Don't change shared data
- if (!m_refData) {
- m_refData = new wxRegionRefData();
- } else if (m_refData->GetRefCount() > 1) {
- wxRegionRefData* ref = (wxRegionRefData*)m_refData;
- UnRef();
- m_refData = new wxRegionRefData(*ref);
- }
- // If ref count is 1, that means it's 'ours' anyway so no action.
-
- HRGN rectRegion = ::CreateRectRgn(x, y, x + width, y + height);
-
- int mode = 0;
- switch (op)
+ const HRGN hrgn = GetHrgn();
+ wxCHECK_MSG( hrgn, false, wxT("invalid wxRegion") );
+
+ if ( !x && !y )
{
- case wxRGN_AND: mode = RGN_AND; break ;
- case wxRGN_OR: mode = RGN_OR; break ;
- case wxRGN_XOR: mode = RGN_XOR; break ;
- case wxRGN_DIFF: mode = RGN_DIFF; break ;
- case wxRGN_COPY:
- default:
- mode = RGN_COPY; break ;
+ // nothing to do
+ return true;
}
- bool success = (ERROR != ::CombineRgn(M_REGION, M_REGION, rectRegion, mode));
+ AllocExclusive();
- ::DeleteObject(rectRegion);
+ if ( ::OffsetRgn(hrgn, x, y) == ERROR )
+ {
+ wxLogLastError(wxT("OffsetRgn"));
- return success;
+ return false;
+ }
+
+ return true;
}
-//! Union /e region with this.
-bool wxRegion::Combine(const wxRegion& region, wxRegionOp op)
+// combine another region with this one
+bool wxRegion::DoCombine(const wxRegion& rgn, wxRegionOp op)
{
- if (region.Empty())
- return FALSE;
-
- // Don't change shared data
- if (!m_refData) {
- m_refData = new wxRegionRefData();
- } else if (m_refData->GetRefCount() > 1) {
- wxRegionRefData* ref = (wxRegionRefData*)m_refData;
- UnRef();
- m_refData = new wxRegionRefData(*ref);
- }
-
- int mode = 0;
- switch (op)
+ // we can't use the API functions if we don't have a valid region handle
+ if ( !m_refData )
{
- case wxRGN_AND: mode = RGN_AND; break ;
- case wxRGN_OR: mode = RGN_OR; break ;
- case wxRGN_XOR: mode = RGN_XOR; break ;
- case wxRGN_DIFF: mode = RGN_DIFF; break ;
- case wxRGN_COPY:
- default:
- mode = RGN_COPY; break ;
+ // combining with an empty/invalid region works differently depending
+ // on the operation
+ switch ( op )
+ {
+ case wxRGN_COPY:
+ case wxRGN_OR:
+ case wxRGN_XOR:
+ *this = rgn;
+ break;
+
+ default:
+ wxFAIL_MSG( wxT("unknown region operation") );
+ // fall through
+
+ case wxRGN_AND:
+ case wxRGN_DIFF:
+ // leave empty/invalid
+ return false;
+ }
}
+ else // we have a valid region
+ {
+ AllocExclusive();
- return (ERROR != ::CombineRgn(M_REGION, M_REGION, ((wxRegionRefData*)region.m_refData)->m_region, mode));
-}
+ int mode;
+ switch ( op )
+ {
+ case wxRGN_AND:
+ mode = RGN_AND;
+ break;
-bool wxRegion::Combine(const wxRect& rect, wxRegionOp op)
-{
- return Combine(rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight(), op);
+ case wxRGN_OR:
+ mode = RGN_OR;
+ break;
+
+ case wxRGN_XOR:
+ mode = RGN_XOR;
+ break;
+
+ case wxRGN_DIFF:
+ mode = RGN_DIFF;
+ break;
+
+ default:
+ wxFAIL_MSG( wxT("unknown region operation") );
+ // fall through
+
+ case wxRGN_COPY:
+ mode = RGN_COPY;
+ break;
+ }
+
+ if ( ::CombineRgn(M_REGION, M_REGION, M_REGION_OF(rgn), mode) == ERROR )
+ {
+ wxLogLastError(wxT("CombineRgn"));
+
+ return false;
+ }
+ }
+
+ return true;
}
-//-----------------------------------------------------------------------------
-//# Information on region
-//-----------------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+// wxRegion bounding box
+// ----------------------------------------------------------------------------
// Outer bounds of region
-void wxRegion::GetBox(long& x, long& y, long&w, long &h) const
+bool wxRegion::DoGetBox(wxCoord& x, wxCoord& y, wxCoord&w, wxCoord &h) const
{
- if (m_refData) {
+ if (m_refData)
+ {
RECT rect;
::GetRgnBox(M_REGION, & rect);
- x = rect.left;
- y = rect.top;
- w = rect.right - rect.left;
- h = rect.bottom - rect.top;
- } else {
- x = y = w = h = 0;
- }
-}
+ x = rect.left;
+ y = rect.top;
+ w = rect.right - rect.left;
+ h = rect.bottom - rect.top;
-wxRect wxRegion::GetBox(void) const
-{
- long x, y, w, h;
- GetBox(x, y, w, h);
- return wxRect(x, y, w, h);
+ return true;
+ }
+ else
+ {
+ x = y = w = h = 0;
+
+ return false;
+ }
}
// Is region empty?
-bool wxRegion::Empty(void) const
+bool wxRegion::IsEmpty() const
{
- if (M_REGION == 0)
- return TRUE;
- long x, y, w, h;
+ wxCoord x, y, w, h;
GetBox(x, y, w, h);
- return ((w == 0) && (h == 0));
+ return (w == 0) && (h == 0);
}
-//-----------------------------------------------------------------------------
-//# Tests
-//-----------------------------------------------------------------------------
-
-// Does the region contain the point (x,y)?
-wxRegionContain wxRegion::Contains(long x, long y) const
+bool wxRegion::DoIsEqual(const wxRegion& region) const
{
- if (!m_refData)
- return wxOutRegion;
-
- if (::PtInRegion(M_REGION, (int) x, (int) y))
- return wxInRegion;
- else
- return wxOutRegion;
+ return ::EqualRgn(M_REGION, M_REGION_OF(region)) != 0;
}
-// Does the region contain the point pt?
-wxRegionContain wxRegion::Contains(const wxPoint& pt) const
-{
- if (!m_refData)
- return wxOutRegion;
+// ----------------------------------------------------------------------------
+// wxRegion hit testing
+// ----------------------------------------------------------------------------
- if (::PtInRegion(M_REGION, (int) pt.x, (int) pt.y))
- return wxInRegion;
- else
+// Does the region contain the point (x,y)?
+wxRegionContain wxRegion::DoContainsPoint(wxCoord x, wxCoord y) const
+{
+ if (!m_refData)
return wxOutRegion;
+
+ return ::PtInRegion(M_REGION, (int) x, (int) y) ? wxInRegion : wxOutRegion;
}
// Does the region contain the rectangle (x, y, w, h)?
-wxRegionContain wxRegion::Contains(long x, long y, long w, long h) const
+wxRegionContain wxRegion::DoContainsRect(const wxRect& rect) const
{
- if (!m_refData)
- return wxOutRegion;
-
- RECT rect;
- rect.left = x;
- rect.top = y;
- rect.right = x + w;
- rect.bottom = y + h;
-
- if (::RectInRegion(M_REGION, & rect))
- return wxInRegion;
- else
+ if (!m_refData)
return wxOutRegion;
-}
-// Does the region contain the rectangle rect
-wxRegionContain wxRegion::Contains(const wxRect& rect) const
-{
- if (!m_refData)
- return wxOutRegion;
-
- long x, y, w, h;
- x = rect.x;
- y = rect.y;
- w = rect.GetWidth();
- h = rect.GetHeight();
- return Contains(x, y, w, h);
+ RECT rc;
+ wxCopyRectToRECT(rect, rc);
+
+ return ::RectInRegion(M_REGION, &rc) ? wxInRegion : wxOutRegion;
}
// Get internal region handle
WXHRGN wxRegion::GetHRGN() const
{
- if (!m_refData)
- return (WXHRGN) 0;
- return (WXHRGN) M_REGION;
+ return (WXHRGN)(m_refData ? M_REGION : 0);
}
-///////////////////////////////////////////////////////////////////////////////
-// //
-// wxRegionIterator //
-// //
-///////////////////////////////////////////////////////////////////////////////
+// ============================================================================
+// wxRegionIterator implementation
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// wxRegionIterator ctors/dtor
+// ----------------------------------------------------------------------------
-/*!
- * Initialize empty iterator
- */
-wxRegionIterator::wxRegionIterator(void) : m_current(0), m_numRects(0), m_rects(NULL)
+void wxRegionIterator::Init()
{
+ m_current =
+ m_numRects = 0;
+
+ m_rects = NULL;
}
-wxRegionIterator::~wxRegionIterator(void)
+wxRegionIterator::~wxRegionIterator()
{
- if (m_rects)
- delete[] m_rects;
+ delete [] m_rects;
}
-/*!
- * Initialize iterator for region
- */
+// Initialize iterator for region
wxRegionIterator::wxRegionIterator(const wxRegion& region)
{
m_rects = NULL;
- Reset(region);
+ Reset(region);
}
-/*!
- * Reset iterator for a new /e region.
- */
-void wxRegionIterator::Reset(const wxRegion& region)
+wxRegionIterator& wxRegionIterator::operator=(const wxRegionIterator& ri)
{
- m_current = 0;
- m_region = region;
+ delete [] m_rects;
- if (m_rects)
- delete[] m_rects;
+ m_current = ri.m_current;
+ m_numRects = ri.m_numRects;
+ if ( m_numRects )
+ {
+ m_rects = new wxRect[m_numRects];
+ for ( long n = 0; n < m_numRects; n++ )
+ m_rects[n] = ri.m_rects[n];
+ }
+ else
+ {
+ m_rects = NULL;
+ }
- m_rects = NULL;
+ return *this;
+}
+
+// ----------------------------------------------------------------------------
+// wxRegionIterator operations
+// ----------------------------------------------------------------------------
+
+// Reset iterator for a new region.
+void wxRegionIterator::Reset(const wxRegion& region)
+{
+ m_current = 0;
+ m_region = region;
- if (m_region.Empty())
- m_numRects = 0;
- else
+ wxDELETEA(m_rects);
+
+ if (m_region.Empty())
+ m_numRects = 0;
+ else
{
-#if defined(__WIN32__)
DWORD noBytes = ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, 0, NULL);
RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
- ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, noBytes, rgnData);
+ ::GetRegionData(((wxRegionRefData*)region.m_refData)->m_region, noBytes, rgnData);
RGNDATAHEADER* header = (RGNDATAHEADER*) rgnData;
m_rects = new wxRect[header->nCount];
- RECT* rect = (RECT*) (rgnData + sizeof(RGNDATAHEADER)) ;
+ RECT* rect = (RECT*) ((char*)rgnData + sizeof(RGNDATAHEADER));
size_t i;
for (i = 0; i < header->nCount; i++)
{
m_rects[i] = wxRect(rect->left, rect->top,
rect->right - rect->left, rect->bottom - rect->top);
- rect += sizeof(RECT);
+ rect ++; // Advances pointer by sizeof(RECT)
}
m_numRects = header->nCount;
delete[] (char*) rgnData;
-#else
- RECT rect;
- ::GetRgnBox(((wxRegionRefData*)region.m_refData)->m_region, &rect);
- m_rects = new wxRect[1];
- m_rects[0].x = rect.left;
- m_rects[0].y = rect.top;
- m_rects[0].width = rect.right - rect.left;
- m_rects[0].height = rect.bottom - rect.top;
-
- m_numRects = 1;
-#endif
}
}
-/*!
- * Increment iterator. The rectangle returned is the one after the
- * incrementation.
- */
-void wxRegionIterator::operator ++ (void)
+wxRegionIterator& wxRegionIterator::operator++()
{
- if (m_current < m_numRects)
- ++m_current;
+ if (m_current < m_numRects)
+ ++m_current;
+
+ return *this;
}
-/*!
- * Increment iterator. The rectangle returned is the one before the
- * incrementation.
- */
-void wxRegionIterator::operator ++ (int)
+wxRegionIterator wxRegionIterator::operator ++ (int)
{
- if (m_current < m_numRects)
- ++m_current;
+ wxRegionIterator tmp = *this;
+ if (m_current < m_numRects)
+ ++m_current;
+
+ return tmp;
}
-long wxRegionIterator::GetX(void) const
+// ----------------------------------------------------------------------------
+// wxRegionIterator accessors
+// ----------------------------------------------------------------------------
+
+wxCoord wxRegionIterator::GetX() const
{
- if (m_current < m_numRects)
- return m_rects[m_current].x;
- return 0;
+ wxCHECK_MSG( m_current < m_numRects, 0, wxT("invalid wxRegionIterator") );
+
+ return m_rects[m_current].x;
}
-long wxRegionIterator::GetY(void) const
+wxCoord wxRegionIterator::GetY() const
{
- if (m_current < m_numRects)
- return m_rects[m_current].y;
- return 0;
+ wxCHECK_MSG( m_current < m_numRects, 0, wxT("invalid wxRegionIterator") );
+
+ return m_rects[m_current].y;
}
-long wxRegionIterator::GetW(void) const
+wxCoord wxRegionIterator::GetW() const
{
- if (m_current < m_numRects)
- return m_rects[m_current].width ;
- return 0;
+ wxCHECK_MSG( m_current < m_numRects, 0, wxT("invalid wxRegionIterator") );
+
+ return m_rects[m_current].width;
}
-long wxRegionIterator::GetH(void) const
+wxCoord wxRegionIterator::GetH() const
{
- if (m_current < m_numRects)
- return m_rects[m_current].height;
- return 0;
-}
+ wxCHECK_MSG( m_current < m_numRects, 0, wxT("invalid wxRegionIterator") );
+ return m_rects[m_current].height;
+}