]> git.saurik.com Git - wxWidgets.git/blame - src/os2/region.cpp
more informative assert message
[wxWidgets.git] / src / os2 / region.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// File: src/os2/region.cpp
0e320a79 3// Purpose: Region class
409c9842
DW
4// Author: David Webster
5// Modified by:
6// Created: 10/15/99
19193a2c 7// RCS-ID: $Id$
744c5946 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
409c9842
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
0e320a79 14
670f9935
WS
15#ifndef WX_PRECOMP
16 #include "wx/app.h"
cdccdfab 17 #include "wx/window.h"
dd05139a 18 #include "wx/gdicmn.h"
670f9935
WS
19#endif
20
409c9842 21#include "wx/os2/region.h"
0e320a79 22
409c9842
DW
23#include "wx/os2/private.h"
24
670f9935
WS
25IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
26IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
0e320a79
DW
27
28//-----------------------------------------------------------------------------
29// wxRegionRefData implementation
30//-----------------------------------------------------------------------------
31
32class WXDLLEXPORT wxRegionRefData : public wxGDIRefData {
33public:
409c9842
DW
34 wxRegionRefData()
35 {
8d854fa9 36 m_hRegion = 0;
19193a2c 37 m_hPS = 0;
409c9842 38 }
0e320a79 39
8d854fa9 40 wxRegionRefData(const wxRegionRefData& rData)
409c9842 41 {
8d854fa9
DW
42 RGNRECT vRgnData;
43 PRECTL pRect = NULL;
44
53e8757b 45 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
8d854fa9
DW
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
51 ))
52 {
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
61 ))
62 {
63 m_hRegion = ::GpiCreateRegion( rData.m_hPS
64 ,vRgnData.crcReturned
65 ,pRect
66 );
67 m_hPS = rData.m_hPS;
68 }
69 delete [] pRect;
70 }
409c9842 71 }
0e320a79 72
d3c7fc99 73 virtual ~wxRegionRefData()
409c9842 74 {
8d854fa9 75 ::GpiDestroyRegion(m_hPS, m_hRegion);
409c9842 76 }
04701dd9 77
8d854fa9
DW
78 HRGN m_hRegion;
79 HPS m_hPS;
0e320a79
DW
80};
81
8d854fa9 82#define M_REGION (((wxRegionRefData*)m_refData)->m_hRegion)
6ed98c6a 83#define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_hRegion)
0e320a79
DW
84
85//-----------------------------------------------------------------------------
86// wxRegion
87//-----------------------------------------------------------------------------
88
744c5946
SN
89// General remark:
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.
95
0e320a79
DW
96/*!
97 * Create an empty region.
98 */
99wxRegion::wxRegion()
100{
101 m_refData = new wxRegionRefData;
8d854fa9 102} // end of wxRegion::wxRegion
0e320a79 103
8d854fa9 104wxRegion::wxRegion(
19193a2c
KB
105 WXHRGN hRegion,
106 WXHDC hPS
8d854fa9 107)
409c9842
DW
108{
109 m_refData = new wxRegionRefData;
110 M_REGION = (HRGN) hRegion;
19193a2c 111 (((wxRegionRefData*)m_refData)->m_hPS) = hPS;
8d854fa9
DW
112} // end of wxRegion::wxRegion
113
114wxRegion::wxRegion(
115 wxCoord x
116, wxCoord y
117, wxCoord vWidth
118, wxCoord vHeight
119)
0e320a79 120{
8d854fa9
DW
121 RECTL vRect;
122 SIZEL vSize = {0, 0};
123 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
124 HDC hDC = ::DevOpenDC( vHabmain
125 ,OD_MEMORY
126 ,"*"
127 ,5L
128 ,(PDEVOPENDATA)&vDop
129 ,NULLHANDLE
130 );
131
132
133 vRect.xLeft = x;
134 vRect.xRight = x + vWidth;
135 vRect.yBottom = y;
136 vRect.yTop = y + vHeight;
137
138 m_refData = new wxRegionRefData;
139
140 //
141 // Need a PS to create a Region
142 //
143 ((wxRegionRefData*)m_refData)->m_hPS = ::GpiCreatePS( vHabmain
144 ,hDC
145 ,&vSize
146 ,PU_PELS | GPIT_MICRO | GPIA_ASSOC
147 );
148 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)m_refData)->m_hPS
149 ,1
150 ,&vRect
151 );
152} // end of wxRegion::wxRegion
153
154wxRegion::wxRegion(
155 const wxPoint& rTopLeft
156, const wxPoint& rBottomRight
157)
0e320a79 158{
8d854fa9
DW
159 RECTL vRect;
160 SIZEL vSize = {0, 0};
161 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
162 HDC hDC = ::DevOpenDC( vHabmain
163 ,OD_MEMORY
164 ,"*"
165 ,5L
166 ,(PDEVOPENDATA)&vDop
167 ,NULLHANDLE
168 );
169
170 vRect.xLeft = rTopLeft.x;
171 vRect.xRight = rBottomRight.x;
744c5946
SN
172 vRect.yBottom = rTopLeft.y;
173 vRect.yTop = rBottomRight.y;
8d854fa9
DW
174
175 m_refData = new wxRegionRefData;
176
177 //
178 // Need a PS to create a Region
179 //
180 ((wxRegionRefData*)m_refData)->m_hPS = ::GpiCreatePS( vHabmain
181 ,hDC
182 ,&vSize
183 ,PU_PELS | GPIT_MICRO | GPIA_ASSOC
184 );
185 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)m_refData)->m_hPS
186 ,1
187 ,&vRect
188 );
189} // end of wxRegion::wxRegion
190
191wxRegion::wxRegion(
192 const wxRect& rRect
193)
0e320a79 194{
8d854fa9
DW
195 RECTL vRect;
196 SIZEL vSize = {0, 0};
197 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
198 HDC hDC = ::DevOpenDC( vHabmain
199 ,OD_MEMORY
200 ,"*"
201 ,5L
202 ,(PDEVOPENDATA)&vDop
203 ,NULLHANDLE
204 );
205
206
207 vRect.xLeft = rRect.x;
208 vRect.xRight = rRect.x + rRect.width;
209 vRect.yBottom = rRect.y;
210 vRect.yTop = rRect.y + rRect.height;
211
212 m_refData = new wxRegionRefData;
213
214 //
215 // Need a PS to create a Region
216 //
217 ((wxRegionRefData*)m_refData)->m_hPS = ::GpiCreatePS( vHabmain
218 ,hDC
219 ,&vSize
220 ,PU_PELS | GPIT_MICRO | GPIA_ASSOC
221 );
222 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)m_refData)->m_hPS
223 ,1
224 ,&vRect
225 );
226} // end of wxRegion::wxRegion
227
228//
229// Destroy the region.
230//
0e320a79
DW
231wxRegion::~wxRegion()
232{
8d854fa9 233} // end of wxRegion::~wxRegion
0e320a79 234
58238121
DW
235wxObjectRefData *wxRegion::CreateData() const
236{
237 return new wxRegionRefData;
238}
239
70b7bcd1 240wxObjectRefData *wxRegion::CloneData(const wxObjectRefData *data) const
58238121
DW
241{
242 return new wxRegionRefData(*(wxRegionRefData *)data);
243}
244
0e320a79
DW
245//-----------------------------------------------------------------------------
246//# Modify region
247//-----------------------------------------------------------------------------
248
8a16d737 249bool wxRegion::DoOffset( wxCoord x, wxCoord y )
58238121
DW
250{
251 if ( !x && !y )
252 {
253 // nothing to do
670f9935 254 return true;
58238121
DW
255 }
256
257 AllocExclusive();
258
259#if 0
260 if ( ::OffsetRgn(GetHrgn(), x, y) == ERROR )
261 {
262 wxLogLastError(_T("OffsetRgn"));
263
670f9935 264 return false;
58238121
DW
265 }
266#endif
670f9935 267 return true;
58238121
DW
268}
269
8d854fa9
DW
270//
271// Clear current region
272//
0e320a79
DW
273void wxRegion::Clear()
274{
275 UnRef();
8d854fa9
DW
276} // end of wxRegion::Clear
277
8d854fa9
DW
278//
279// Union region with this.
280//
8a16d737 281bool wxRegion::DoCombine( const wxRegion& rRegion, wxRegionOp eOp )
0e320a79 282{
6ed98c6a
DW
283 //
284 // We can't use the API functions if we don't have a valid region handle
285 //
286 if (!m_refData)
287 {
288 // combining with an empty/invalid region works differently depending
289 // on the operation
290 switch (eOp)
291 {
292 case wxRGN_COPY:
293 case wxRGN_OR:
294 case wxRGN_XOR:
295 *this = rRegion;
296 break;
297
298 default:
299 wxFAIL_MSG( _T("unknown region operation") );
300 // fall through
301
302 case wxRGN_AND:
303 case wxRGN_DIFF:
304 // leave empty/invalid
670f9935 305 return false;
6ed98c6a
DW
306 }
307 }
308 else // we have a valid region
309 {
0e320a79 310
6ed98c6a 311 LONG lMode = 0;
8d854fa9 312
6ed98c6a
DW
313 switch (eOp)
314 {
315 case wxRGN_AND:
316 lMode = CRGN_AND;
317 break;
318
319 case wxRGN_OR:
320 lMode = CRGN_OR;
321 break;
322
323 case wxRGN_XOR:
324 lMode = CRGN_XOR;
325 break;
326
327 case wxRGN_DIFF:
328 lMode = CRGN_DIFF;
329 break;
330
331 case wxRGN_COPY:
332 default:
333 lMode = CRGN_COPY;
334 break;
335 }
336 return (::GpiCombineRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
337 ,M_REGION
338 ,M_REGION
339 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion
340 ,lMode
341 ) != RGN_ERROR);
0e320a79 342 }
670f9935 343 return true;
8d854fa9
DW
344} // end of wxRegion::Combine
345
0e320a79
DW
346//-----------------------------------------------------------------------------
347//# Information on region
348//-----------------------------------------------------------------------------
349
56617718 350bool wxRegion::DoIsEqual(const wxRegion& WXUNUSED(region)) const
8a16d737
VZ
351{
352 return false;
353}
354
8d854fa9 355//
0e320a79 356// Outer bounds of region
8d854fa9 357//
8a16d737 358bool wxRegion::DoGetBox(
8d854fa9
DW
359 wxCoord& x
360, wxCoord& y
361, wxCoord& vWidth
362, wxCoord& vHeight
363) const
0e320a79 364{
8d854fa9
DW
365 if (m_refData)
366 {
367 RECTL vRect;
19193a2c 368 APIRET rc;
8d854fa9 369
19193a2c 370 rc = ::GpiQueryRegionBox( ((wxRegionRefData*)m_refData)->m_hPS
8d854fa9
DW
371 ,M_REGION
372 ,&vRect
373 );
374 x = vRect.xLeft;
4a46a5df 375 y = vRect.yBottom;
8d854fa9
DW
376 vWidth = vRect.xRight - vRect.xLeft;
377 vHeight = vRect.yTop - vRect.yBottom;
8a16d737 378 return true;
409c9842 379 }
8d854fa9
DW
380 else
381 {
382 x = y = vWidth = vHeight = 0L;
8a16d737 383 return false;
8d854fa9
DW
384 }
385} // end of wxRegion::GetBox
0e320a79 386
8d854fa9 387//
0e320a79 388// Is region empty?
8d854fa9 389//
8a16d737 390bool wxRegion::IsEmpty() const
0e320a79 391{
670f9935
WS
392 wxCoord x;
393 wxCoord y;
394 wxCoord vWidth;
395 wxCoord vHeight;
8d854fa9
DW
396
397 if (M_REGION == 0)
670f9935 398 return true;
8d854fa9
DW
399
400 GetBox( x
401 ,y
402 ,vWidth
403 ,vHeight
404 );
405 return ((vWidth == 0) && (vHeight == 0));
8a16d737 406} // end of wxRegion::IsEmpty
0e320a79
DW
407
408//-----------------------------------------------------------------------------
8d854fa9 409// Tests
0e320a79 410//-----------------------------------------------------------------------------
8d854fa9 411//
0e320a79 412// Does the region contain the point pt?
8d854fa9 413//
56617718 414wxRegionContain wxRegion::DoContainsPoint( wxCoord x, wxCoord y ) const
0e320a79 415{
56617718 416 POINTL vPoint = { x, y };
8d854fa9 417
409c9842
DW
418 if (!m_refData)
419 return wxOutRegion;
0e320a79 420
744c5946
SN
421 LONG lInside = ::GpiPtInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
422 M_REGION,
423 &vPoint
8a16d737 424 );
8d854fa9 425 if (lInside == PRGN_INSIDE)
0e320a79
DW
426 return wxInRegion;
427 else
428 return wxOutRegion;
8d854fa9 429} // end of wxRegion::Contains
0e320a79 430
8d854fa9 431//
0e320a79 432// Does the region contain the rectangle (x, y, w, h)?
8d854fa9 433//
8a16d737 434wxRegionContain wxRegion::DoContainsRect(const wxRect& rect) const
0e320a79 435{
409c9842
DW
436 if (!m_refData)
437 return wxOutRegion;
0e320a79 438
744c5946 439 RECTL vRect;
56617718
WS
440 vRect.xLeft = rect.x;
441 vRect.xRight = rect.x + rect.width;
442 vRect.yTop = rect.y + rect.height;
443 vRect.yBottom = rect.y;
0e320a79 444
744c5946
SN
445 LONG lInside = ::GpiRectInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
446 M_REGION,
8a16d737 447 &vRect
744c5946
SN
448 );
449 switch (lInside)
450 {
451 case RRGN_INSIDE : return wxInRegion;
452 case RRGN_PARTIAL : return wxPartRegion;
453 case RRGN_ERROR :
454 default : return wxOutRegion;
455 }
56617718 456
8d854fa9
DW
457} // end of wxRegion::Contains
458
459//
409c9842 460// Get internal region handle
8d854fa9 461//
409c9842
DW
462WXHRGN wxRegion::GetHRGN() const
463{
464 if (!m_refData)
465 return (WXHRGN) 0;
466 return (WXHRGN) M_REGION;
467}
468
44227537
DW
469//
470// Set a new PS, this means we have to recreate the old region in the new
471// PS
472//
473void wxRegion::SetPS(
474 HPS hPS
475)
476{
477 RGNRECT vRgnData;
478 PRECTL pRect = NULL;
479
53e8757b 480 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
44227537
DW
481 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
482 ,((wxRegionRefData*)m_refData)->m_hRegion
483 ,NULL
484 ,&vRgnData
485 ,NULL
486 ))
487 {
488 pRect = new RECTL[vRgnData.crcReturned];
489 vRgnData.crc = vRgnData.crcReturned;
490 vRgnData.ircStart = 1;
491 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
492 ,((wxRegionRefData*)m_refData)->m_hRegion
493 ,NULL
494 ,&vRgnData
495 ,pRect
496 ))
497 {
498 //
499 // First destroy the region out of the old PS
500 // and then create it in the new and set the new to current
501 //
502 ::GpiDestroyRegion( ((wxRegionRefData*)m_refData)->m_hPS
503 ,M_REGION
504 );
505 ((wxRegionRefData*)m_refData)->m_hRegion = ::GpiCreateRegion( hPS
506 ,vRgnData.crcReturned
507 ,pRect
508 );
509 ((wxRegionRefData*)m_refData)->m_hPS = hPS;
510 }
511 delete [] pRect;
512 }
513} // end of wxRegion::SetPS
514
0e320a79 515///////////////////////////////////////////////////////////////////////////////
409c9842
DW
516// //
517// wxRegionIterator //
518// //
0e320a79
DW
519///////////////////////////////////////////////////////////////////////////////
520
8d854fa9
DW
521//
522// Initialize empty iterator
523//
524wxRegionIterator::wxRegionIterator()
525: m_lCurrent(0)
526, m_lNumRects(0)
527, m_pRects(NULL)
0e320a79 528{
8d854fa9 529} // end of wxRegionIterator::wxRegionIterator
0e320a79
DW
530
531wxRegionIterator::~wxRegionIterator()
532{
8d854fa9
DW
533 if (m_pRects)
534 delete[] m_pRects;
535} // end of wxRegionIterator::~wxRegionIterator
536
537//
538// Initialize iterator for region
539//
540wxRegionIterator::wxRegionIterator(
541 const wxRegion& rRegion
542)
0e320a79 543{
8d854fa9
DW
544 m_pRects = NULL;
545 Reset(rRegion);
546} // end of wxRegionIterator::wxRegionIterator
547
548//
549// Reset iterator for a new /e region.
550//
551void wxRegionIterator::Reset(
552 const wxRegion& rRegion
553)
0e320a79 554{
8d854fa9 555 m_lCurrent = 0;
0b483d6e 556 m_lNumRects = 0;
8d854fa9 557 m_vRegion = rRegion;
0e320a79 558
8d854fa9
DW
559 if (m_pRects)
560 delete[] m_pRects;
0e320a79 561
8d854fa9 562 m_pRects = NULL;
0e320a79 563
8d854fa9
DW
564 if (m_vRegion.Empty())
565 m_lNumRects = 0;
409c9842 566 else
0e320a79 567 {
8d854fa9
DW
568 RGNRECT vRgnData;
569 PRECTL pRect;
570
53e8757b 571 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
8d854fa9
DW
572 if (::GpiQueryRegionRects( ((wxRegionRefData*)rRegion.m_refData)->m_hPS // Pres space
573 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion // Handle of region to query
574 ,NULL // Return all RECTs
575 ,&vRgnData // Will contain number or RECTs in region
576 ,NULL // NULL to return number of RECTs
577 ))
578 {
579 pRect = new RECTL[vRgnData.crcReturned];
580 m_pRects = new wxRect[vRgnData.crcReturned];
581 vRgnData.crc = vRgnData.crcReturned;
582 m_lNumRects = vRgnData.crcReturned;
583 vRgnData.ircStart = 1;
584 if (::GpiQueryRegionRects( ((wxRegionRefData*)rRegion.m_refData)->m_hPS // Pres space of source
585 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion // Handle of source region
586 ,NULL // Return all RECTs
587 ,&vRgnData // Operations set to return rects
588 ,pRect // Will contain the actual RECTS
589 ))
590 {
53e8757b 591#if 0
8d854fa9
DW
592 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
593 ,vRgnData.crcReturned
594 ,pRect
595 );
53e8757b 596#endif
8d854fa9
DW
597 for( LONG i = 0; i < m_lNumRects; i++)
598 {
599 m_pRects[i].x = pRect[i].xLeft;
600 m_pRects[i].width = pRect[i].xRight - pRect[i].xLeft;
601 m_pRects[i].y = pRect[i].yBottom;
602 m_pRects[i].height = pRect[i].yTop - pRect[i].yBottom;
603 }
53e8757b 604#if 0
8d854fa9 605 ((wxRegionRefData*)m_refData)->m_hPS = ((wxRegionRefData*)rRegion.m_refData)->m_hPS;
53e8757b 606#endif
8d854fa9
DW
607 }
608 }
0e320a79 609 }
8d854fa9 610} // end of wxRegionIterator::Reset
0e320a79 611
8d854fa9
DW
612//
613// Increment iterator. The rectangle returned is the one after the
614// incrementation.
615//
616void wxRegionIterator::operator++ ()
0e320a79 617{
8d854fa9
DW
618 if (m_lCurrent < m_lNumRects)
619 ++m_lCurrent;
620} // end of wxRegionIterator::operator ++
621
622//
623// Increment iterator. The rectangle returned is the one before the
624// incrementation.
625//
626void wxRegionIterator::operator++ (int)
0e320a79 627{
8d854fa9
DW
628 if (m_lCurrent < m_lNumRects)
629 ++m_lCurrent;
630} // end of wxRegionIterator::operator++
0e320a79 631
0b7e7739 632wxCoord wxRegionIterator::GetX() const
0e320a79 633{
8d854fa9
DW
634 if (m_lCurrent < m_lNumRects)
635 return m_pRects[m_lCurrent].x;
636 return 0L;
637} // end of wxRegionIterator::GetX
0e320a79 638
0b7e7739 639wxCoord wxRegionIterator::GetY() const
0e320a79 640{
8d854fa9
DW
641 if (m_lCurrent < m_lNumRects)
642 return m_pRects[m_lCurrent].y;
643 return 0L;
644} // end of wxRegionIterator::GetY
0e320a79 645
0b7e7739 646wxCoord wxRegionIterator::GetW() const
0e320a79 647{
8d854fa9
DW
648 if (m_lCurrent < m_lNumRects)
649 return m_pRects[m_lCurrent].width ;
650 return 0L;
651} // end of wxRegionIterator::GetW
0e320a79 652
0b7e7739 653wxCoord wxRegionIterator::GetH() const
0e320a79 654{
8d854fa9
DW
655 if (m_lCurrent < m_lNumRects)
656 return m_pRects[m_lCurrent].height;
657 return 0L;
658} // end of wxRegionIterator::GetH