Added some bad hacks to make region processing work at least a bit.
[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 bool bOK = FALSE;
443 POINTL vPoint;
444
445 vPoint.x = x;
446 vPoint.y = y;
447
448 if (!m_refData)
449 return wxOutRegion;
450
451 LONG lInside = ::GpiPtInRegion( ((wxRegionRefData*)m_refData)->m_hPS
452 ,M_REGION
453 ,&vPoint
454 );
455 if (lInside == PRGN_INSIDE)
456 return wxInRegion;
457 return wxOutRegion;
458 } // end of wxRegion::Contains
459
460 //
461 // Does the region contain the point pt?
462 //
463 wxRegionContain wxRegion::Contains(
464 const wxPoint& rPoint
465 ) const
466 {
467 POINTL vPoint = { rPoint.x, rPoint.y };
468
469 if (!m_refData)
470 return wxOutRegion;
471
472 LONG lInside = ::GpiPtInRegion( ((wxRegionRefData*)m_refData)->m_hPS
473 ,M_REGION
474 ,&vPoint
475 );
476 if (lInside == PRGN_INSIDE)
477 return wxInRegion;
478 else
479 return wxOutRegion;
480 } // end of wxRegion::Contains
481
482 //
483 // Does the region contain the rectangle (x, y, w, h)?
484 //
485 wxRegionContain wxRegion::Contains(
486 wxCoord x
487 , wxCoord y
488 , wxCoord vWidth
489 , wxCoord vHeight
490 ) const
491 {
492 RECTL vRect;
493
494 if (!m_refData)
495 return wxOutRegion;
496
497 vRect.xLeft = x;
498 vRect.yTop = y;
499 vRect.xRight = x + vWidth;
500 vRect.yBottom = y + vHeight;
501
502 if (PRGN_INSIDE == ::GpiRectInRegion( ((wxRegionRefData*)m_refData)->m_hPS
503 ,M_REGION
504 ,&vRect
505 ))
506 return wxInRegion;
507 else
508 return wxOutRegion;
509 } // end of wxRegion::Contains
510
511 //
512 // Does the region contain the rectangle rect
513 //
514 wxRegionContain wxRegion::Contains(
515 const wxRect& rRect
516 ) const
517 {
518 if (!m_refData)
519 return wxOutRegion;
520
521 wxCoord x;
522 wxCoord y;
523 wxCoord vWidth;
524 wxCoord vHeight;
525
526 x = rRect.x;
527 y = rRect.y;
528 vWidth = rRect.GetWidth();
529 vHeight = rRect.GetHeight();
530 return Contains( x
531 ,y
532 ,vWidth
533 ,vHeight
534 );
535 } // end of wxRegion::Contains
536
537 //
538 // Get internal region handle
539 //
540 WXHRGN wxRegion::GetHRGN() const
541 {
542 if (!m_refData)
543 return (WXHRGN) 0;
544 return (WXHRGN) M_REGION;
545 }
546
547 //
548 // Set a new PS, this means we have to recreate the old region in the new
549 // PS
550 //
551 void wxRegion::SetPS(
552 HPS hPS
553 )
554 {
555 RGNRECT vRgnData;
556 PRECTL pRect = NULL;
557
558 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
559 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
560 ,((wxRegionRefData*)m_refData)->m_hRegion
561 ,NULL
562 ,&vRgnData
563 ,NULL
564 ))
565 {
566 pRect = new RECTL[vRgnData.crcReturned];
567 vRgnData.crc = vRgnData.crcReturned;
568 vRgnData.ircStart = 1;
569 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
570 ,((wxRegionRefData*)m_refData)->m_hRegion
571 ,NULL
572 ,&vRgnData
573 ,pRect
574 ))
575 {
576 //
577 // First destroy the region out of the old PS
578 // and then create it in the new and set the new to current
579 //
580 ::GpiDestroyRegion( ((wxRegionRefData*)m_refData)->m_hPS
581 ,M_REGION
582 );
583 ((wxRegionRefData*)m_refData)->m_hRegion = ::GpiCreateRegion( hPS
584 ,vRgnData.crcReturned
585 ,pRect
586 );
587 ((wxRegionRefData*)m_refData)->m_hPS = hPS;
588 }
589 delete [] pRect;
590 }
591 } // end of wxRegion::SetPS
592
593 ///////////////////////////////////////////////////////////////////////////////
594 // //
595 // wxRegionIterator //
596 // //
597 ///////////////////////////////////////////////////////////////////////////////
598
599 //
600 // Initialize empty iterator
601 //
602 wxRegionIterator::wxRegionIterator()
603 : m_lCurrent(0)
604 , m_lNumRects(0)
605 , m_pRects(NULL)
606 {
607 } // end of wxRegionIterator::wxRegionIterator
608
609 wxRegionIterator::~wxRegionIterator()
610 {
611 if (m_pRects)
612 delete[] m_pRects;
613 } // end of wxRegionIterator::~wxRegionIterator
614
615 //
616 // Initialize iterator for region
617 //
618 wxRegionIterator::wxRegionIterator(
619 const wxRegion& rRegion
620 )
621 {
622 m_pRects = NULL;
623 Reset(rRegion);
624 } // end of wxRegionIterator::wxRegionIterator
625
626 //
627 // Reset iterator for a new /e region.
628 //
629 void wxRegionIterator::Reset(
630 const wxRegion& rRegion
631 )
632 {
633 m_lCurrent = 0;
634 m_lNumRects = 0;
635 m_vRegion = rRegion;
636
637 if (m_pRects)
638 delete[] m_pRects;
639
640 m_pRects = NULL;
641
642 if (m_vRegion.Empty())
643 m_lNumRects = 0;
644 else
645 {
646 RGNRECT vRgnData;
647 PRECTL pRect;
648
649 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
650 if (::GpiQueryRegionRects( ((wxRegionRefData*)rRegion.m_refData)->m_hPS // Pres space
651 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion // Handle of region to query
652 ,NULL // Return all RECTs
653 ,&vRgnData // Will contain number or RECTs in region
654 ,NULL // NULL to return number of RECTs
655 ))
656 {
657 pRect = new RECTL[vRgnData.crcReturned];
658 m_pRects = new wxRect[vRgnData.crcReturned];
659 vRgnData.crc = vRgnData.crcReturned;
660 m_lNumRects = vRgnData.crcReturned;
661 vRgnData.ircStart = 1;
662 if (::GpiQueryRegionRects( ((wxRegionRefData*)rRegion.m_refData)->m_hPS // Pres space of source
663 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion // Handle of source region
664 ,NULL // Return all RECTs
665 ,&vRgnData // Operations set to return rects
666 ,pRect // Will contain the actual RECTS
667 ))
668 {
669 #if 0
670 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
671 ,vRgnData.crcReturned
672 ,pRect
673 );
674 #endif
675 for( LONG i = 0; i < m_lNumRects; i++)
676 {
677 m_pRects[i].x = pRect[i].xLeft;
678 m_pRects[i].width = pRect[i].xRight - pRect[i].xLeft;
679 m_pRects[i].y = pRect[i].yBottom;
680 m_pRects[i].height = pRect[i].yTop - pRect[i].yBottom;
681 }
682 #if 0
683 ((wxRegionRefData*)m_refData)->m_hPS = ((wxRegionRefData*)rRegion.m_refData)->m_hPS;
684 #endif
685 }
686 }
687 }
688 } // end of wxRegionIterator::Reset
689
690 //
691 // Increment iterator. The rectangle returned is the one after the
692 // incrementation.
693 //
694 void wxRegionIterator::operator++ ()
695 {
696 if (m_lCurrent < m_lNumRects)
697 ++m_lCurrent;
698 } // end of wxRegionIterator::operator ++
699
700 //
701 // Increment iterator. The rectangle returned is the one before the
702 // incrementation.
703 //
704 void wxRegionIterator::operator++ (int)
705 {
706 if (m_lCurrent < m_lNumRects)
707 ++m_lCurrent;
708 } // end of wxRegionIterator::operator++
709
710 wxCoord wxRegionIterator::GetX() const
711 {
712 if (m_lCurrent < m_lNumRects)
713 return m_pRects[m_lCurrent].x;
714 return 0L;
715 } // end of wxRegionIterator::GetX
716
717 wxCoord wxRegionIterator::GetY() const
718 {
719 if (m_lCurrent < m_lNumRects)
720 return m_pRects[m_lCurrent].y;
721 return 0L;
722 } // end of wxRegionIterator::GetY
723
724 wxCoord wxRegionIterator::GetW() const
725 {
726 if (m_lCurrent < m_lNumRects)
727 return m_pRects[m_lCurrent].width ;
728 return 0L;
729 } // end of wxRegionIterator::GetW
730
731 wxCoord wxRegionIterator::GetH() const
732 {
733 if (m_lCurrent < m_lNumRects)
734 return m_pRects[m_lCurrent].height;
735 return 0L;
736 } // end of wxRegionIterator::GetH
737