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 wxObjectRefData
*wxRegion::CreateData() const
225 return new wxRegionRefData
;
228 wxObjectRefData
*wxRegion::CloneData(wxObjectRefData
*data
) const
230 return new wxRegionRefData(*(wxRegionRefData
*)data
);
233 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
237 bool wxRegion::Offset(
251 if ( ::OffsetRgn(GetHrgn(), x
, y
) == ERROR
)
253 wxLogLastError(_T("OffsetRgn"));
262 // Clear current region
264 void wxRegion::Clear()
267 } // end of wxRegion::Clear
270 // Combine rectangle (x, y, w, h) with this.
272 bool wxRegion::Combine(
283 // If ref count is 1, that means it's 'ours' anyway so no action.
288 vRect
.xRight
= x
+ vWidth
;
290 vRect
.yTop
= y
+ vHeight
;
292 HRGN hRgn
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
321 bool bSuccess
= ::GpiCombineRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
327 ::GpiDestroyRegion ( ((wxRegionRefData
*)m_refData
)->m_hPS
332 } // end of wxRegion::Combine
335 // Union region with this.
337 bool wxRegion::Combine(
338 const wxRegion
& rRegion
372 return (::GpiCombineRegion( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
375 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
378 } // end of wxRegion::Combine
380 bool wxRegion::Combine(
385 return Combine( rRect
.GetLeft()
391 } // end of wxRegion::Combine
393 //-----------------------------------------------------------------------------
394 //# Information on region
395 //-----------------------------------------------------------------------------
398 // Outer bounds of region
400 void wxRegion::GetBox(
412 rc
= ::GpiQueryRegionBox( ((wxRegionRefData
*)m_refData
)->m_hPS
418 vWidth
= vRect
.xRight
- vRect
.xLeft
;
419 vHeight
= vRect
.yTop
- vRect
.yBottom
;
423 x
= y
= vWidth
= vHeight
= 0L;
425 } // end of wxRegion::GetBox
427 wxRect
wxRegion::GetBox() const
431 return wxRect(x
, y
, w
, h
);
437 bool wxRegion::Empty() const
452 return ((vWidth
== 0) && (vHeight
== 0));
453 } // end of wxRegion::Empty
455 //-----------------------------------------------------------------------------
457 //-----------------------------------------------------------------------------
460 // Does the region contain the point (x,y)?
461 wxRegionContain
wxRegion::Contains(
475 LONG lInside
= ::GpiPtInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
479 if (lInside
== PRGN_INSIDE
)
482 } // end of wxRegion::Contains
485 // Does the region contain the point pt?
487 wxRegionContain
wxRegion::Contains(
488 const wxPoint
& rPoint
491 POINTL vPoint
= { rPoint
.x
, rPoint
.y
};
496 LONG lInside
= ::GpiPtInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
500 if (lInside
== PRGN_INSIDE
)
504 } // end of wxRegion::Contains
507 // Does the region contain the rectangle (x, y, w, h)?
509 wxRegionContain
wxRegion::Contains(
523 vRect
.xRight
= x
+ vWidth
;
524 vRect
.yBottom
= y
+ vHeight
;
526 if (PRGN_INSIDE
== ::GpiRectInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
533 } // end of wxRegion::Contains
536 // Does the region contain the rectangle rect
538 wxRegionContain
wxRegion::Contains(
552 vWidth
= rRect
.GetWidth();
553 vHeight
= rRect
.GetHeight();
559 } // end of wxRegion::Contains
562 // Get internal region handle
564 WXHRGN
wxRegion::GetHRGN() const
568 return (WXHRGN
) M_REGION
;
572 // Set a new PS, this means we have to recreate the old region in the new
575 void wxRegion::SetPS(
582 if (::GpiQueryRegionRects( ((wxRegionRefData
*)m_refData
)->m_hPS
583 ,((wxRegionRefData
*)m_refData
)->m_hRegion
589 pRect
= new RECTL
[vRgnData
.crcReturned
];
590 vRgnData
.crc
= vRgnData
.crcReturned
;
591 vRgnData
.ircStart
= 1;
592 if (::GpiQueryRegionRects( ((wxRegionRefData
*)m_refData
)->m_hPS
593 ,((wxRegionRefData
*)m_refData
)->m_hRegion
600 // First destroy the region out of the old PS
601 // and then create it in the new and set the new to current
603 ::GpiDestroyRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
606 ((wxRegionRefData
*)m_refData
)->m_hRegion
= ::GpiCreateRegion( hPS
607 ,vRgnData
.crcReturned
610 ((wxRegionRefData
*)m_refData
)->m_hPS
= hPS
;
614 } // end of wxRegion::SetPS
616 ///////////////////////////////////////////////////////////////////////////////
618 // wxRegionIterator //
620 ///////////////////////////////////////////////////////////////////////////////
623 // Initialize empty iterator
625 wxRegionIterator::wxRegionIterator()
630 } // end of wxRegionIterator::wxRegionIterator
632 wxRegionIterator::~wxRegionIterator()
636 } // end of wxRegionIterator::~wxRegionIterator
639 // Initialize iterator for region
641 wxRegionIterator::wxRegionIterator(
642 const wxRegion
& rRegion
647 } // end of wxRegionIterator::wxRegionIterator
650 // Reset iterator for a new /e region.
652 void wxRegionIterator::Reset(
653 const wxRegion
& rRegion
664 if (m_vRegion
.Empty())
671 if (::GpiQueryRegionRects( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
// Pres space
672 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
// Handle of region to query
673 ,NULL
// Return all RECTs
674 ,&vRgnData
// Will contain number or RECTs in region
675 ,NULL
// NULL to return number of RECTs
678 pRect
= new RECTL
[vRgnData
.crcReturned
];
679 m_pRects
= new wxRect
[vRgnData
.crcReturned
];
680 vRgnData
.crc
= vRgnData
.crcReturned
;
681 m_lNumRects
= vRgnData
.crcReturned
;
682 vRgnData
.ircStart
= 1;
683 if (::GpiQueryRegionRects( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
// Pres space of source
684 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
// Handle of source region
685 ,NULL
// Return all RECTs
686 ,&vRgnData
// Operations set to return rects
687 ,pRect
// Will contain the actual RECTS
690 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
691 ,vRgnData
.crcReturned
694 for( LONG i
= 0; i
< m_lNumRects
; i
++)
696 m_pRects
[i
].x
= pRect
[i
].xLeft
;
697 m_pRects
[i
].width
= pRect
[i
].xRight
- pRect
[i
].xLeft
;
698 m_pRects
[i
].y
= pRect
[i
].yBottom
;
699 m_pRects
[i
].height
= pRect
[i
].yTop
- pRect
[i
].yBottom
;
701 ((wxRegionRefData
*)m_refData
)->m_hPS
= ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
;
705 } // end of wxRegionIterator::Reset
708 // Increment iterator. The rectangle returned is the one after the
711 void wxRegionIterator::operator++ ()
713 if (m_lCurrent
< m_lNumRects
)
715 } // end of wxRegionIterator::operator ++
718 // Increment iterator. The rectangle returned is the one before the
721 void wxRegionIterator::operator++ (int)
723 if (m_lCurrent
< m_lNumRects
)
725 } // end of wxRegionIterator::operator++
727 wxCoord
wxRegionIterator::GetX() const
729 if (m_lCurrent
< m_lNumRects
)
730 return m_pRects
[m_lCurrent
].x
;
732 } // end of wxRegionIterator::GetX
734 wxCoord
wxRegionIterator::GetY() const
736 if (m_lCurrent
< m_lNumRects
)
737 return m_pRects
[m_lCurrent
].y
;
739 } // end of wxRegionIterator::GetY
741 wxCoord
wxRegionIterator::GetW() const
743 if (m_lCurrent
< m_lNumRects
)
744 return m_pRects
[m_lCurrent
].width
;
746 } // end of wxRegionIterator::GetW
748 wxCoord
wxRegionIterator::GetH() const
750 if (m_lCurrent
< m_lNumRects
)
751 return m_pRects
[m_lCurrent
].height
;
753 } // end of wxRegionIterator::GetH