1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Region class
4 // Author: David Webster
8 // Copyright: (c) Davdi Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #include "wx/os2/region.h"
17 #include "wx/gdicmn.h"
19 #include "wx/window.h"
20 #include "wx/os2/private.h"
22 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
23 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
25 //-----------------------------------------------------------------------------
26 // wxRegionRefData implementation
27 //-----------------------------------------------------------------------------
29 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
37 wxRegionRefData(const wxRegionRefData
& rData
)
42 if (::GpiQueryRegionRects( rData
.m_hPS
// Pres space
43 ,rData
.m_hRegion
// Handle of region to query
44 ,NULL
// Return all RECTs
45 ,&vRgnData
// Will contain number or RECTs in region
46 ,NULL
// NULL to return number of RECTs
49 pRect
= new RECTL
[vRgnData
.crcReturned
];
50 vRgnData
.crc
= vRgnData
.crcReturned
;
51 vRgnData
.ircStart
= 1;
52 if (::GpiQueryRegionRects( rData
.m_hPS
// Pres space of source
53 ,rData
.m_hRegion
// Handle of source region
54 ,NULL
// Return all RECTs
55 ,&vRgnData
// Operations set to return rects
56 ,pRect
// Will contain the actual RECTS
59 m_hRegion
= ::GpiCreateRegion( rData
.m_hPS
71 ::GpiDestroyRegion(m_hPS
, m_hRegion
);
78 #define M_REGION (((wxRegionRefData*)m_refData)->m_hRegion)
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
85 * Create an empty region.
89 m_refData
= new wxRegionRefData
;
90 } // end of wxRegion::wxRegion
97 m_refData
= new wxRegionRefData
;
98 M_REGION
= (HRGN
) hRegion
;
99 (((wxRegionRefData
*)m_refData
)->m_hPS
) = hPS
;
100 } // end of wxRegion::wxRegion
110 SIZEL vSize
= {0, 0};
111 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
112 HDC hDC
= ::DevOpenDC( vHabmain
122 vRect
.xRight
= x
+ vWidth
;
124 vRect
.yTop
= y
+ vHeight
;
126 m_refData
= new wxRegionRefData
;
129 // Need a PS to create a Region
131 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
134 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
136 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
140 } // end of wxRegion::wxRegion
143 const wxPoint
& rTopLeft
144 , const wxPoint
& rBottomRight
148 SIZEL vSize
= {0, 0};
149 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
150 HDC hDC
= ::DevOpenDC( vHabmain
158 vRect
.xLeft
= rTopLeft
.x
;
159 vRect
.xRight
= rBottomRight
.x
;
160 vRect
.yBottom
= rBottomRight
.y
;
161 vRect
.yTop
= rTopLeft
.y
;
163 m_refData
= new wxRegionRefData
;
166 // Need a PS to create a Region
168 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
171 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
173 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
177 } // end of wxRegion::wxRegion
184 SIZEL vSize
= {0, 0};
185 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
186 HDC hDC
= ::DevOpenDC( vHabmain
195 vRect
.xLeft
= rRect
.x
;
196 vRect
.xRight
= rRect
.x
+ rRect
.width
;
197 vRect
.yBottom
= rRect
.y
;
198 vRect
.yTop
= rRect
.y
+ rRect
.height
;
200 m_refData
= new wxRegionRefData
;
203 // Need a PS to create a Region
205 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
208 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
210 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
214 } // end of wxRegion::wxRegion
217 // Destroy the region.
219 wxRegion::~wxRegion()
221 } // end of wxRegion::~wxRegion
223 //-----------------------------------------------------------------------------
225 //-----------------------------------------------------------------------------
228 // Clear current region
230 void wxRegion::Clear()
233 } // end of wxRegion::Clear
236 // Combine rectangle (x, y, w, h) with this.
238 bool wxRegion::Combine(
247 // Don't change shared data
251 m_refData
= new wxRegionRefData();
253 else if (m_refData
->GetRefCount() > 1)
255 wxRegionRefData
* pRef
= (wxRegionRefData
*)m_refData
;
258 m_refData
= new wxRegionRefData(*pRef
);
262 // If ref count is 1, that means it's 'ours' anyway so no action.
267 vRect
.xRight
= x
+ vWidth
;
269 vRect
.yTop
= y
+ vHeight
;
271 HRGN hRgn
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
300 bool bSuccess
= ::GpiCombineRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
306 ::GpiDestroyRegion ( ((wxRegionRefData
*)m_refData
)->m_hPS
311 } // end of wxRegion::Combine
314 // Union region with this.
316 bool wxRegion::Combine(
317 const wxRegion
& rRegion
325 // Don't change shared data
329 m_refData
= new wxRegionRefData();
331 else if (m_refData
->GetRefCount() > 1)
333 wxRegionRefData
* pRef
= (wxRegionRefData
*)m_refData
;
336 m_refData
= new wxRegionRefData(*pRef
);
364 return (::GpiCombineRegion( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
367 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
370 } // end of wxRegion::Combine
372 bool wxRegion::Combine(
377 return Combine( rRect
.GetLeft()
383 } // end of wxRegion::Combine
385 //-----------------------------------------------------------------------------
386 //# Information on region
387 //-----------------------------------------------------------------------------
390 // Outer bounds of region
392 void wxRegion::GetBox(
404 rc
= ::GpiQueryRegionBox( ((wxRegionRefData
*)m_refData
)->m_hPS
410 vWidth
= vRect
.xRight
- vRect
.xLeft
;
411 vHeight
= vRect
.yTop
- vRect
.yBottom
;
415 x
= y
= vWidth
= vHeight
= 0L;
417 } // end of wxRegion::GetBox
419 wxRect
wxRegion::GetBox() const
423 return wxRect(x
, y
, w
, h
);
429 bool wxRegion::Empty() const
444 return ((vWidth
== 0) && (vHeight
== 0));
445 } // end of wxRegion::Empty
447 //-----------------------------------------------------------------------------
449 //-----------------------------------------------------------------------------
452 // Does the region contain the point (x,y)?
453 wxRegionContain
wxRegion::Contains(
467 LONG lInside
= ::GpiPtInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
471 if (lInside
== PRGN_INSIDE
)
474 } // end of wxRegion::Contains
477 // Does the region contain the point pt?
479 wxRegionContain
wxRegion::Contains(
480 const wxPoint
& rPoint
483 POINTL vPoint
= { rPoint
.x
, rPoint
.y
};
488 LONG lInside
= ::GpiPtInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
492 if (lInside
== PRGN_INSIDE
)
496 } // end of wxRegion::Contains
499 // Does the region contain the rectangle (x, y, w, h)?
501 wxRegionContain
wxRegion::Contains(
515 vRect
.xRight
= x
+ vWidth
;
516 vRect
.yBottom
= y
+ vHeight
;
518 if (PRGN_INSIDE
== ::GpiRectInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
525 } // end of wxRegion::Contains
528 // Does the region contain the rectangle rect
530 wxRegionContain
wxRegion::Contains(
544 vWidth
= rRect
.GetWidth();
545 vHeight
= rRect
.GetHeight();
551 } // end of wxRegion::Contains
554 // Get internal region handle
556 WXHRGN
wxRegion::GetHRGN() const
560 return (WXHRGN
) M_REGION
;
564 // Set a new PS, this means we have to recreate the old region in the new
567 void wxRegion::SetPS(
574 if (::GpiQueryRegionRects( ((wxRegionRefData
*)m_refData
)->m_hPS
575 ,((wxRegionRefData
*)m_refData
)->m_hRegion
581 pRect
= new RECTL
[vRgnData
.crcReturned
];
582 vRgnData
.crc
= vRgnData
.crcReturned
;
583 vRgnData
.ircStart
= 1;
584 if (::GpiQueryRegionRects( ((wxRegionRefData
*)m_refData
)->m_hPS
585 ,((wxRegionRefData
*)m_refData
)->m_hRegion
592 // First destroy the region out of the old PS
593 // and then create it in the new and set the new to current
595 ::GpiDestroyRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
598 ((wxRegionRefData
*)m_refData
)->m_hRegion
= ::GpiCreateRegion( hPS
599 ,vRgnData
.crcReturned
602 ((wxRegionRefData
*)m_refData
)->m_hPS
= hPS
;
606 } // end of wxRegion::SetPS
608 ///////////////////////////////////////////////////////////////////////////////
610 // wxRegionIterator //
612 ///////////////////////////////////////////////////////////////////////////////
615 // Initialize empty iterator
617 wxRegionIterator::wxRegionIterator()
622 } // end of wxRegionIterator::wxRegionIterator
624 wxRegionIterator::~wxRegionIterator()
628 } // end of wxRegionIterator::~wxRegionIterator
631 // Initialize iterator for region
633 wxRegionIterator::wxRegionIterator(
634 const wxRegion
& rRegion
639 } // end of wxRegionIterator::wxRegionIterator
642 // Reset iterator for a new /e region.
644 void wxRegionIterator::Reset(
645 const wxRegion
& rRegion
656 if (m_vRegion
.Empty())
663 if (::GpiQueryRegionRects( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
// Pres space
664 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
// Handle of region to query
665 ,NULL
// Return all RECTs
666 ,&vRgnData
// Will contain number or RECTs in region
667 ,NULL
// NULL to return number of RECTs
670 pRect
= new RECTL
[vRgnData
.crcReturned
];
671 m_pRects
= new wxRect
[vRgnData
.crcReturned
];
672 vRgnData
.crc
= vRgnData
.crcReturned
;
673 m_lNumRects
= vRgnData
.crcReturned
;
674 vRgnData
.ircStart
= 1;
675 if (::GpiQueryRegionRects( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
// Pres space of source
676 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
// Handle of source region
677 ,NULL
// Return all RECTs
678 ,&vRgnData
// Operations set to return rects
679 ,pRect
// Will contain the actual RECTS
682 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
683 ,vRgnData
.crcReturned
686 for( LONG i
= 0; i
< m_lNumRects
; i
++)
688 m_pRects
[i
].x
= pRect
[i
].xLeft
;
689 m_pRects
[i
].width
= pRect
[i
].xRight
- pRect
[i
].xLeft
;
690 m_pRects
[i
].y
= pRect
[i
].yBottom
;
691 m_pRects
[i
].height
= pRect
[i
].yTop
- pRect
[i
].yBottom
;
693 ((wxRegionRefData
*)m_refData
)->m_hPS
= ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
;
697 } // end of wxRegionIterator::Reset
700 // Increment iterator. The rectangle returned is the one after the
703 void wxRegionIterator::operator++ ()
705 if (m_lCurrent
< m_lNumRects
)
707 } // end of wxRegionIterator::operator ++
710 // Increment iterator. The rectangle returned is the one before the
713 void wxRegionIterator::operator++ (int)
715 if (m_lCurrent
< m_lNumRects
)
717 } // end of wxRegionIterator::operator++
719 wxCoord
wxRegionIterator::GetX() const
721 if (m_lCurrent
< m_lNumRects
)
722 return m_pRects
[m_lCurrent
].x
;
724 } // end of wxRegionIterator::GetX
726 wxCoord
wxRegionIterator::GetY() const
728 if (m_lCurrent
< m_lNumRects
)
729 return m_pRects
[m_lCurrent
].y
;
731 } // end of wxRegionIterator::GetY
733 wxCoord
wxRegionIterator::GetW() const
735 if (m_lCurrent
< m_lNumRects
)
736 return m_pRects
[m_lCurrent
].width
;
738 } // end of wxRegionIterator::GetW
740 wxCoord
wxRegionIterator::GetH() const
742 if (m_lCurrent
< m_lNumRects
)
743 return m_pRects
[m_lCurrent
].height
;
745 } // end of wxRegionIterator::GetH