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