1 /////////////////////////////////////////////////////////////////////////////
2 // File: src/os2/region.cpp
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"
19 #include "wx/os2/region.h"
20 #include "wx/gdicmn.h"
22 #include "wx/window.h"
23 #include "wx/os2/private.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxRegion
, wxGDIObject
)
26 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator
, wxObject
)
28 //-----------------------------------------------------------------------------
29 // wxRegionRefData implementation
30 //-----------------------------------------------------------------------------
32 class WXDLLEXPORT wxRegionRefData
: public wxGDIRefData
{
40 wxRegionRefData(const wxRegionRefData
& rData
)
45 vRgnData
.ulDirection
= RECTDIR_LFRT_TOPBOT
;
46 if (::GpiQueryRegionRects( rData
.m_hPS
// Pres space
47 ,rData
.m_hRegion
// Handle of region to query
48 ,NULL
// Return all RECTs
49 ,&vRgnData
// Will contain number or RECTs in region
50 ,NULL
// NULL to return number of RECTs
53 pRect
= new RECTL
[vRgnData
.crcReturned
];
54 vRgnData
.crc
= vRgnData
.crcReturned
;
55 vRgnData
.ircStart
= 1;
56 if (::GpiQueryRegionRects( rData
.m_hPS
// Pres space of source
57 ,rData
.m_hRegion
// Handle of source region
58 ,NULL
// Return all RECTs
59 ,&vRgnData
// Operations set to return rects
60 ,pRect
// Will contain the actual RECTS
63 m_hRegion
= ::GpiCreateRegion( rData
.m_hPS
75 ::GpiDestroyRegion(m_hPS
, m_hRegion
);
82 #define M_REGION (((wxRegionRefData*)m_refData)->m_hRegion)
83 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_hRegion)
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
90 * Create an empty region.
94 m_refData
= new wxRegionRefData
;
95 } // end of wxRegion::wxRegion
102 m_refData
= new wxRegionRefData
;
103 M_REGION
= (HRGN
) hRegion
;
104 (((wxRegionRefData
*)m_refData
)->m_hPS
) = hPS
;
105 } // end of wxRegion::wxRegion
115 SIZEL vSize
= {0, 0};
116 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
117 HDC hDC
= ::DevOpenDC( vHabmain
127 vRect
.xRight
= x
+ vWidth
;
129 vRect
.yTop
= y
+ vHeight
;
131 m_refData
= new wxRegionRefData
;
134 // Need a PS to create a Region
136 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
139 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
141 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
145 } // end of wxRegion::wxRegion
148 const wxPoint
& rTopLeft
149 , const wxPoint
& rBottomRight
153 SIZEL vSize
= {0, 0};
154 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
155 HDC hDC
= ::DevOpenDC( vHabmain
163 vRect
.xLeft
= rTopLeft
.x
;
164 vRect
.xRight
= rBottomRight
.x
;
165 vRect
.yBottom
= rBottomRight
.y
;
166 vRect
.yTop
= rTopLeft
.y
;
168 m_refData
= new wxRegionRefData
;
171 // Need a PS to create a Region
173 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
176 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
178 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
182 } // end of wxRegion::wxRegion
189 SIZEL vSize
= {0, 0};
190 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
191 HDC hDC
= ::DevOpenDC( vHabmain
200 vRect
.xLeft
= rRect
.x
;
201 vRect
.xRight
= rRect
.x
+ rRect
.width
;
202 vRect
.yBottom
= rRect
.y
;
203 vRect
.yTop
= rRect
.y
+ rRect
.height
;
205 m_refData
= new wxRegionRefData
;
208 // Need a PS to create a Region
210 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
213 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
215 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
219 } // end of wxRegion::wxRegion
222 // Destroy the region.
224 wxRegion::~wxRegion()
226 } // end of wxRegion::~wxRegion
228 wxObjectRefData
*wxRegion::CreateData() const
230 return new wxRegionRefData
;
233 wxObjectRefData
*wxRegion::CloneData(const wxObjectRefData
*data
) const
235 return new wxRegionRefData(*(wxRegionRefData
*)data
);
238 //-----------------------------------------------------------------------------
240 //-----------------------------------------------------------------------------
242 bool wxRegion::Offset( wxCoord x
, wxCoord y
)
253 if ( ::OffsetRgn(GetHrgn(), x
, y
) == ERROR
)
255 wxLogLastError(_T("OffsetRgn"));
264 // Clear current region
266 void wxRegion::Clear()
269 } // end of wxRegion::Clear
272 // Combine rectangle (x, y, w, h) with this.
274 bool wxRegion::Combine(
282 return Combine(wxRegion(x
, y
, vWidth
, vHeight
), eOp
);
283 } // end of wxRegion::Combine
286 // Union region with this.
288 bool wxRegion::Combine( const wxRegion
& rRegion
, wxRegionOp eOp
)
291 // We can't use the API functions if we don't have a valid region handle
295 // combining with an empty/invalid region works differently depending
306 wxFAIL_MSG( _T("unknown region operation") );
311 // leave empty/invalid
315 else // we have a valid region
343 return (::GpiCombineRegion( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
346 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
351 } // end of wxRegion::Combine
353 bool wxRegion::Combine(
358 return Combine( rRect
.GetLeft()
364 } // end of wxRegion::Combine
366 //-----------------------------------------------------------------------------
367 //# Information on region
368 //-----------------------------------------------------------------------------
371 // Outer bounds of region
373 void wxRegion::GetBox(
385 rc
= ::GpiQueryRegionBox( ((wxRegionRefData
*)m_refData
)->m_hPS
391 vWidth
= vRect
.xRight
- vRect
.xLeft
;
392 vHeight
= vRect
.yTop
- vRect
.yBottom
;
396 x
= y
= vWidth
= vHeight
= 0L;
398 } // end of wxRegion::GetBox
400 wxRect
wxRegion::GetBox() const
404 return wxRect(x
, y
, w
, h
);
410 bool wxRegion::Empty() const
425 return ((vWidth
== 0) && (vHeight
== 0));
426 } // end of wxRegion::Empty
428 //-----------------------------------------------------------------------------
430 //-----------------------------------------------------------------------------
433 // Does the region contain the point (x,y)?
434 wxRegionContain
wxRegion::Contains(
447 LONG lInside
= ::GpiPtInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
451 if (lInside
== PRGN_INSIDE
)
454 } // end of wxRegion::Contains
457 // Does the region contain the point pt?
459 wxRegionContain
wxRegion::Contains(
460 const wxPoint
& rPoint
463 POINTL vPoint
= { rPoint
.x
, rPoint
.y
};
468 LONG lInside
= ::GpiPtInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
472 if (lInside
== PRGN_INSIDE
)
476 } // end of wxRegion::Contains
479 // Does the region contain the rectangle (x, y, w, h)?
481 wxRegionContain
wxRegion::Contains(
495 vRect
.xRight
= x
+ vWidth
;
496 vRect
.yBottom
= y
+ vHeight
;
498 if (PRGN_INSIDE
== ::GpiRectInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
505 } // end of wxRegion::Contains
508 // Does the region contain the rectangle rect
510 wxRegionContain
wxRegion::Contains(
524 vWidth
= rRect
.GetWidth();
525 vHeight
= rRect
.GetHeight();
531 } // end of wxRegion::Contains
534 // Get internal region handle
536 WXHRGN
wxRegion::GetHRGN() const
540 return (WXHRGN
) M_REGION
;
544 // Set a new PS, this means we have to recreate the old region in the new
547 void wxRegion::SetPS(
554 vRgnData
.ulDirection
= RECTDIR_LFRT_TOPBOT
;
555 if (::GpiQueryRegionRects( ((wxRegionRefData
*)m_refData
)->m_hPS
556 ,((wxRegionRefData
*)m_refData
)->m_hRegion
562 pRect
= new RECTL
[vRgnData
.crcReturned
];
563 vRgnData
.crc
= vRgnData
.crcReturned
;
564 vRgnData
.ircStart
= 1;
565 if (::GpiQueryRegionRects( ((wxRegionRefData
*)m_refData
)->m_hPS
566 ,((wxRegionRefData
*)m_refData
)->m_hRegion
573 // First destroy the region out of the old PS
574 // and then create it in the new and set the new to current
576 ::GpiDestroyRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
579 ((wxRegionRefData
*)m_refData
)->m_hRegion
= ::GpiCreateRegion( hPS
580 ,vRgnData
.crcReturned
583 ((wxRegionRefData
*)m_refData
)->m_hPS
= hPS
;
587 } // end of wxRegion::SetPS
589 ///////////////////////////////////////////////////////////////////////////////
591 // wxRegionIterator //
593 ///////////////////////////////////////////////////////////////////////////////
596 // Initialize empty iterator
598 wxRegionIterator::wxRegionIterator()
603 } // end of wxRegionIterator::wxRegionIterator
605 wxRegionIterator::~wxRegionIterator()
609 } // end of wxRegionIterator::~wxRegionIterator
612 // Initialize iterator for region
614 wxRegionIterator::wxRegionIterator(
615 const wxRegion
& rRegion
620 } // end of wxRegionIterator::wxRegionIterator
623 // Reset iterator for a new /e region.
625 void wxRegionIterator::Reset(
626 const wxRegion
& rRegion
638 if (m_vRegion
.Empty())
645 vRgnData
.ulDirection
= RECTDIR_LFRT_TOPBOT
;
646 if (::GpiQueryRegionRects( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
// Pres space
647 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
// Handle of region to query
648 ,NULL
// Return all RECTs
649 ,&vRgnData
// Will contain number or RECTs in region
650 ,NULL
// NULL to return number of RECTs
653 pRect
= new RECTL
[vRgnData
.crcReturned
];
654 m_pRects
= new wxRect
[vRgnData
.crcReturned
];
655 vRgnData
.crc
= vRgnData
.crcReturned
;
656 m_lNumRects
= vRgnData
.crcReturned
;
657 vRgnData
.ircStart
= 1;
658 if (::GpiQueryRegionRects( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
// Pres space of source
659 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
// Handle of source region
660 ,NULL
// Return all RECTs
661 ,&vRgnData
// Operations set to return rects
662 ,pRect
// Will contain the actual RECTS
666 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
667 ,vRgnData
.crcReturned
671 for( LONG i
= 0; i
< m_lNumRects
; i
++)
673 m_pRects
[i
].x
= pRect
[i
].xLeft
;
674 m_pRects
[i
].width
= pRect
[i
].xRight
- pRect
[i
].xLeft
;
675 m_pRects
[i
].y
= pRect
[i
].yBottom
;
676 m_pRects
[i
].height
= pRect
[i
].yTop
- pRect
[i
].yBottom
;
679 ((wxRegionRefData
*)m_refData
)->m_hPS
= ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
;
684 } // end of wxRegionIterator::Reset
687 // Increment iterator. The rectangle returned is the one after the
690 void wxRegionIterator::operator++ ()
692 if (m_lCurrent
< m_lNumRects
)
694 } // end of wxRegionIterator::operator ++
697 // Increment iterator. The rectangle returned is the one before the
700 void wxRegionIterator::operator++ (int)
702 if (m_lCurrent
< m_lNumRects
)
704 } // end of wxRegionIterator::operator++
706 wxCoord
wxRegionIterator::GetX() const
708 if (m_lCurrent
< m_lNumRects
)
709 return m_pRects
[m_lCurrent
].x
;
711 } // end of wxRegionIterator::GetX
713 wxCoord
wxRegionIterator::GetY() const
715 if (m_lCurrent
< m_lNumRects
)
716 return m_pRects
[m_lCurrent
].y
;
718 } // end of wxRegionIterator::GetY
720 wxCoord
wxRegionIterator::GetW() const
722 if (m_lCurrent
< m_lNumRects
)
723 return m_pRects
[m_lCurrent
].width
;
725 } // end of wxRegionIterator::GetW
727 wxCoord
wxRegionIterator::GetH() const
729 if (m_lCurrent
< m_lNumRects
)
730 return m_pRects
[m_lCurrent
].height
;
732 } // end of wxRegionIterator::GetH