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