1 /////////////////////////////////////////////////////////////////////////////
2 // File: src/os2/region.cpp
3 // Purpose: Region class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/window.h"
18 #include "wx/gdicmn.h"
21 #include "wx/os2/region.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
73 virtual ~wxRegionRefData()
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 // wxRegion is always basically stored in wx coordinates. However, since
91 // OS/2's internal functions rely on "top > bottom", the values of top and
92 // bottom values of a region need to be interchanged, as compared to wx.
93 // This needs to be taken into account when feeding any rectangle to wx _or_
94 // when accessing the region data via GetBox, wxRegionIterator or otherwise.
97 * Create an empty region.
101 m_refData
= new wxRegionRefData
;
102 } // end of wxRegion::wxRegion
109 m_refData
= new wxRegionRefData
;
110 M_REGION
= (HRGN
) hRegion
;
111 (((wxRegionRefData
*)m_refData
)->m_hPS
) = hPS
;
112 } // end of wxRegion::wxRegion
122 SIZEL vSize
= {0, 0};
123 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
124 HDC hDC
= ::DevOpenDC( vHabmain
134 vRect
.xRight
= x
+ vWidth
;
136 vRect
.yTop
= y
+ vHeight
;
138 m_refData
= new wxRegionRefData
;
141 // Need a PS to create a Region
143 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
146 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
148 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
152 } // end of wxRegion::wxRegion
155 const wxPoint
& rTopLeft
156 , const wxPoint
& rBottomRight
160 SIZEL vSize
= {0, 0};
161 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
162 HDC hDC
= ::DevOpenDC( vHabmain
170 vRect
.xLeft
= rTopLeft
.x
;
171 vRect
.xRight
= rBottomRight
.x
;
172 vRect
.yBottom
= rTopLeft
.y
;
173 vRect
.yTop
= rBottomRight
.y
;
175 m_refData
= new wxRegionRefData
;
178 // Need a PS to create a Region
180 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
183 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
185 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
189 } // end of wxRegion::wxRegion
196 SIZEL vSize
= {0, 0};
197 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
198 HDC hDC
= ::DevOpenDC( vHabmain
207 vRect
.xLeft
= rRect
.x
;
208 vRect
.xRight
= rRect
.x
+ rRect
.width
;
209 vRect
.yBottom
= rRect
.y
;
210 vRect
.yTop
= rRect
.y
+ rRect
.height
;
212 m_refData
= new wxRegionRefData
;
215 // Need a PS to create a Region
217 ((wxRegionRefData
*)m_refData
)->m_hPS
= ::GpiCreatePS( vHabmain
220 ,PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
222 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
226 } // end of wxRegion::wxRegion
229 // Destroy the region.
231 wxRegion::~wxRegion()
233 } // end of wxRegion::~wxRegion
235 wxObjectRefData
*wxRegion::CreateData() const
237 return new wxRegionRefData
;
240 wxObjectRefData
*wxRegion::CloneData(const wxObjectRefData
*data
) const
242 return new wxRegionRefData(*(wxRegionRefData
*)data
);
245 //-----------------------------------------------------------------------------
247 //-----------------------------------------------------------------------------
249 bool wxRegion::DoOffset( wxCoord x
, wxCoord y
)
260 if ( ::OffsetRgn(GetHrgn(), x
, y
) == ERROR
)
262 wxLogLastError(_T("OffsetRgn"));
271 // Clear current region
273 void wxRegion::Clear()
276 } // end of wxRegion::Clear
279 // Union region with this.
281 bool wxRegion::DoCombine( const wxRegion
& rRegion
, wxRegionOp eOp
)
284 // We can't use the API functions if we don't have a valid region handle
288 // combining with an empty/invalid region works differently depending
299 wxFAIL_MSG( _T("unknown region operation") );
304 // leave empty/invalid
308 else // we have a valid region
336 return (::GpiCombineRegion( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
339 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
344 } // end of wxRegion::Combine
346 //-----------------------------------------------------------------------------
347 //# Information on region
348 //-----------------------------------------------------------------------------
350 bool wxRegion::DoIsEqual(const wxRegion
& region
) const
356 // Outer bounds of region
358 bool wxRegion::DoGetBox(
370 rc
= ::GpiQueryRegionBox( ((wxRegionRefData
*)m_refData
)->m_hPS
376 vWidth
= vRect
.xRight
- vRect
.xLeft
;
377 vHeight
= vRect
.yTop
- vRect
.yBottom
;
382 x
= y
= vWidth
= vHeight
= 0L;
385 } // end of wxRegion::GetBox
390 bool wxRegion::IsEmpty() const
405 return ((vWidth
== 0) && (vHeight
== 0));
406 } // end of wxRegion::IsEmpty
408 //-----------------------------------------------------------------------------
410 //-----------------------------------------------------------------------------
412 // Does the region contain the point pt?
414 wxRegionContain
wxRegion::DoContainsPoint( const wxPoint
& rPoint
) const
416 POINTL vPoint
= { rPoint
.x
, rPoint
.y
};
421 LONG lInside
= ::GpiPtInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
,
425 if (lInside
== PRGN_INSIDE
)
429 } // end of wxRegion::Contains
432 // Does the region contain the rectangle (x, y, w, h)?
434 wxRegionContain
wxRegion::DoContainsRect(const wxRect
& rect
) const
441 vRect
.xRight
= r
.x
+ r
.width
;
442 vRect
.yTop
= r
.y
+ r
.height
;
445 LONG lInside
= ::GpiRectInRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
,
451 case RRGN_INSIDE
: return wxInRegion
;
452 case RRGN_PARTIAL
: return wxPartRegion
;
454 default : return wxOutRegion
;
456 } // end of wxRegion::Contains
459 // Get internal region handle
461 WXHRGN
wxRegion::GetHRGN() const
465 return (WXHRGN
) M_REGION
;
469 // Set a new PS, this means we have to recreate the old region in the new
472 void wxRegion::SetPS(
479 vRgnData
.ulDirection
= RECTDIR_LFRT_TOPBOT
;
480 if (::GpiQueryRegionRects( ((wxRegionRefData
*)m_refData
)->m_hPS
481 ,((wxRegionRefData
*)m_refData
)->m_hRegion
487 pRect
= new RECTL
[vRgnData
.crcReturned
];
488 vRgnData
.crc
= vRgnData
.crcReturned
;
489 vRgnData
.ircStart
= 1;
490 if (::GpiQueryRegionRects( ((wxRegionRefData
*)m_refData
)->m_hPS
491 ,((wxRegionRefData
*)m_refData
)->m_hRegion
498 // First destroy the region out of the old PS
499 // and then create it in the new and set the new to current
501 ::GpiDestroyRegion( ((wxRegionRefData
*)m_refData
)->m_hPS
504 ((wxRegionRefData
*)m_refData
)->m_hRegion
= ::GpiCreateRegion( hPS
505 ,vRgnData
.crcReturned
508 ((wxRegionRefData
*)m_refData
)->m_hPS
= hPS
;
512 } // end of wxRegion::SetPS
514 ///////////////////////////////////////////////////////////////////////////////
516 // wxRegionIterator //
518 ///////////////////////////////////////////////////////////////////////////////
521 // Initialize empty iterator
523 wxRegionIterator::wxRegionIterator()
528 } // end of wxRegionIterator::wxRegionIterator
530 wxRegionIterator::~wxRegionIterator()
534 } // end of wxRegionIterator::~wxRegionIterator
537 // Initialize iterator for region
539 wxRegionIterator::wxRegionIterator(
540 const wxRegion
& rRegion
545 } // end of wxRegionIterator::wxRegionIterator
548 // Reset iterator for a new /e region.
550 void wxRegionIterator::Reset(
551 const wxRegion
& rRegion
563 if (m_vRegion
.Empty())
570 vRgnData
.ulDirection
= RECTDIR_LFRT_TOPBOT
;
571 if (::GpiQueryRegionRects( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
// Pres space
572 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
// Handle of region to query
573 ,NULL
// Return all RECTs
574 ,&vRgnData
// Will contain number or RECTs in region
575 ,NULL
// NULL to return number of RECTs
578 pRect
= new RECTL
[vRgnData
.crcReturned
];
579 m_pRects
= new wxRect
[vRgnData
.crcReturned
];
580 vRgnData
.crc
= vRgnData
.crcReturned
;
581 m_lNumRects
= vRgnData
.crcReturned
;
582 vRgnData
.ircStart
= 1;
583 if (::GpiQueryRegionRects( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
// Pres space of source
584 ,((wxRegionRefData
*)rRegion
.m_refData
)->m_hRegion
// Handle of source region
585 ,NULL
// Return all RECTs
586 ,&vRgnData
// Operations set to return rects
587 ,pRect
// Will contain the actual RECTS
591 M_REGION
= ::GpiCreateRegion( ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
592 ,vRgnData
.crcReturned
596 for( LONG i
= 0; i
< m_lNumRects
; i
++)
598 m_pRects
[i
].x
= pRect
[i
].xLeft
;
599 m_pRects
[i
].width
= pRect
[i
].xRight
- pRect
[i
].xLeft
;
600 m_pRects
[i
].y
= pRect
[i
].yBottom
;
601 m_pRects
[i
].height
= pRect
[i
].yTop
- pRect
[i
].yBottom
;
604 ((wxRegionRefData
*)m_refData
)->m_hPS
= ((wxRegionRefData
*)rRegion
.m_refData
)->m_hPS
;
609 } // end of wxRegionIterator::Reset
612 // Increment iterator. The rectangle returned is the one after the
615 void wxRegionIterator::operator++ ()
617 if (m_lCurrent
< m_lNumRects
)
619 } // end of wxRegionIterator::operator ++
622 // Increment iterator. The rectangle returned is the one before the
625 void wxRegionIterator::operator++ (int)
627 if (m_lCurrent
< m_lNumRects
)
629 } // end of wxRegionIterator::operator++
631 wxCoord
wxRegionIterator::GetX() const
633 if (m_lCurrent
< m_lNumRects
)
634 return m_pRects
[m_lCurrent
].x
;
636 } // end of wxRegionIterator::GetX
638 wxCoord
wxRegionIterator::GetY() const
640 if (m_lCurrent
< m_lNumRects
)
641 return m_pRects
[m_lCurrent
].y
;
643 } // end of wxRegionIterator::GetY
645 wxCoord
wxRegionIterator::GetW() const
647 if (m_lCurrent
< m_lNumRects
)
648 return m_pRects
[m_lCurrent
].width
;
650 } // end of wxRegionIterator::GetW
652 wxCoord
wxRegionIterator::GetH() const
654 if (m_lCurrent
< m_lNumRects
)
655 return m_pRects
[m_lCurrent
].height
;
657 } // end of wxRegionIterator::GetH