The Great wxRegion Refactoring:
[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) David 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 #include "wx/gdicmn.h"
19 #endif
20
21 #include "wx/os2/region.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 virtual ~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 // 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
96 /*!
97 * Create an empty region.
98 */
99 wxRegion::wxRegion()
100 {
101 m_refData = new wxRegionRefData;
102 } // end of wxRegion::wxRegion
103
104 wxRegion::wxRegion(
105 WXHRGN hRegion,
106 WXHDC hPS
107 )
108 {
109 m_refData = new wxRegionRefData;
110 M_REGION = (HRGN) hRegion;
111 (((wxRegionRefData*)m_refData)->m_hPS) = hPS;
112 } // end of wxRegion::wxRegion
113
114 wxRegion::wxRegion(
115 wxCoord x
116 , wxCoord y
117 , wxCoord vWidth
118 , wxCoord vHeight
119 )
120 {
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
154 wxRegion::wxRegion(
155 const wxPoint& rTopLeft
156 , const wxPoint& rBottomRight
157 )
158 {
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;
172 vRect.yBottom = rTopLeft.y;
173 vRect.yTop = rBottomRight.y;
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
191 wxRegion::wxRegion(
192 const wxRect& rRect
193 )
194 {
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 //
231 wxRegion::~wxRegion()
232 {
233 } // end of wxRegion::~wxRegion
234
235 wxObjectRefData *wxRegion::CreateData() const
236 {
237 return new wxRegionRefData;
238 }
239
240 wxObjectRefData *wxRegion::CloneData(const wxObjectRefData *data) const
241 {
242 return new wxRegionRefData(*(wxRegionRefData *)data);
243 }
244
245 //-----------------------------------------------------------------------------
246 //# Modify region
247 //-----------------------------------------------------------------------------
248
249 bool wxRegion::DoOffset( wxCoord x, wxCoord y )
250 {
251 if ( !x && !y )
252 {
253 // nothing to do
254 return true;
255 }
256
257 AllocExclusive();
258
259 #if 0
260 if ( ::OffsetRgn(GetHrgn(), x, y) == ERROR )
261 {
262 wxLogLastError(_T("OffsetRgn"));
263
264 return false;
265 }
266 #endif
267 return true;
268 }
269
270 //
271 // Clear current region
272 //
273 void wxRegion::Clear()
274 {
275 UnRef();
276 } // end of wxRegion::Clear
277
278 //
279 // Union region with this.
280 //
281 bool wxRegion::DoCombine( const wxRegion& rRegion, wxRegionOp eOp )
282 {
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
305 return false;
306 }
307 }
308 else // we have a valid region
309 {
310
311 LONG lMode = 0;
312
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);
342 }
343 return true;
344 } // end of wxRegion::Combine
345
346 //-----------------------------------------------------------------------------
347 //# Information on region
348 //-----------------------------------------------------------------------------
349
350 bool wxRegion::DoIsEqual(const wxRegion& region) const
351 {
352 return false;
353 }
354
355 //
356 // Outer bounds of region
357 //
358 bool wxRegion::DoGetBox(
359 wxCoord& x
360 , wxCoord& y
361 , wxCoord& vWidth
362 , wxCoord& vHeight
363 ) const
364 {
365 if (m_refData)
366 {
367 RECTL vRect;
368 APIRET rc;
369
370 rc = ::GpiQueryRegionBox( ((wxRegionRefData*)m_refData)->m_hPS
371 ,M_REGION
372 ,&vRect
373 );
374 x = vRect.xLeft;
375 y = vRect.yBottom;
376 vWidth = vRect.xRight - vRect.xLeft;
377 vHeight = vRect.yTop - vRect.yBottom;
378 return true;
379 }
380 else
381 {
382 x = y = vWidth = vHeight = 0L;
383 return false;
384 }
385 } // end of wxRegion::GetBox
386
387 //
388 // Is region empty?
389 //
390 bool wxRegion::IsEmpty() const
391 {
392 wxCoord x;
393 wxCoord y;
394 wxCoord vWidth;
395 wxCoord vHeight;
396
397 if (M_REGION == 0)
398 return true;
399
400 GetBox( x
401 ,y
402 ,vWidth
403 ,vHeight
404 );
405 return ((vWidth == 0) && (vHeight == 0));
406 } // end of wxRegion::IsEmpty
407
408 //-----------------------------------------------------------------------------
409 // Tests
410 //-----------------------------------------------------------------------------
411 //
412 // Does the region contain the point pt?
413 //
414 wxRegionContain wxRegion::DoContainsPoint( const wxPoint& rPoint ) const
415 {
416 POINTL vPoint = { rPoint.x, rPoint.y };
417
418 if (!m_refData)
419 return wxOutRegion;
420
421 LONG lInside = ::GpiPtInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
422 M_REGION,
423 &vPoint
424 );
425 if (lInside == PRGN_INSIDE)
426 return wxInRegion;
427 else
428 return wxOutRegion;
429 } // end of wxRegion::Contains
430
431 //
432 // Does the region contain the rectangle (x, y, w, h)?
433 //
434 wxRegionContain wxRegion::DoContainsRect(const wxRect& rect) const
435 {
436 if (!m_refData)
437 return wxOutRegion;
438
439 RECTL vRect;
440 vRect.xLeft = r.x;
441 vRect.xRight = r.x + r.width;
442 vRect.yTop = r.y + r.height;
443 vRect.yBottom = r.y;
444
445 LONG lInside = ::GpiRectInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
446 M_REGION,
447 &vRect
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 }
456 } // end of wxRegion::Contains
457
458 //
459 // Get internal region handle
460 //
461 WXHRGN wxRegion::GetHRGN() const
462 {
463 if (!m_refData)
464 return (WXHRGN) 0;
465 return (WXHRGN) M_REGION;
466 }
467
468 //
469 // Set a new PS, this means we have to recreate the old region in the new
470 // PS
471 //
472 void wxRegion::SetPS(
473 HPS hPS
474 )
475 {
476 RGNRECT vRgnData;
477 PRECTL pRect = NULL;
478
479 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
480 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
481 ,((wxRegionRefData*)m_refData)->m_hRegion
482 ,NULL
483 ,&vRgnData
484 ,NULL
485 ))
486 {
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
492 ,NULL
493 ,&vRgnData
494 ,pRect
495 ))
496 {
497 //
498 // First destroy the region out of the old PS
499 // and then create it in the new and set the new to current
500 //
501 ::GpiDestroyRegion( ((wxRegionRefData*)m_refData)->m_hPS
502 ,M_REGION
503 );
504 ((wxRegionRefData*)m_refData)->m_hRegion = ::GpiCreateRegion( hPS
505 ,vRgnData.crcReturned
506 ,pRect
507 );
508 ((wxRegionRefData*)m_refData)->m_hPS = hPS;
509 }
510 delete [] pRect;
511 }
512 } // end of wxRegion::SetPS
513
514 ///////////////////////////////////////////////////////////////////////////////
515 // //
516 // wxRegionIterator //
517 // //
518 ///////////////////////////////////////////////////////////////////////////////
519
520 //
521 // Initialize empty iterator
522 //
523 wxRegionIterator::wxRegionIterator()
524 : m_lCurrent(0)
525 , m_lNumRects(0)
526 , m_pRects(NULL)
527 {
528 } // end of wxRegionIterator::wxRegionIterator
529
530 wxRegionIterator::~wxRegionIterator()
531 {
532 if (m_pRects)
533 delete[] m_pRects;
534 } // end of wxRegionIterator::~wxRegionIterator
535
536 //
537 // Initialize iterator for region
538 //
539 wxRegionIterator::wxRegionIterator(
540 const wxRegion& rRegion
541 )
542 {
543 m_pRects = NULL;
544 Reset(rRegion);
545 } // end of wxRegionIterator::wxRegionIterator
546
547 //
548 // Reset iterator for a new /e region.
549 //
550 void wxRegionIterator::Reset(
551 const wxRegion& rRegion
552 )
553 {
554 m_lCurrent = 0;
555 m_lNumRects = 0;
556 m_vRegion = rRegion;
557
558 if (m_pRects)
559 delete[] m_pRects;
560
561 m_pRects = NULL;
562
563 if (m_vRegion.Empty())
564 m_lNumRects = 0;
565 else
566 {
567 RGNRECT vRgnData;
568 PRECTL pRect;
569
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
576 ))
577 {
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
588 ))
589 {
590 #if 0
591 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
592 ,vRgnData.crcReturned
593 ,pRect
594 );
595 #endif
596 for( LONG i = 0; i < m_lNumRects; i++)
597 {
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;
602 }
603 #if 0
604 ((wxRegionRefData*)m_refData)->m_hPS = ((wxRegionRefData*)rRegion.m_refData)->m_hPS;
605 #endif
606 }
607 }
608 }
609 } // end of wxRegionIterator::Reset
610
611 //
612 // Increment iterator. The rectangle returned is the one after the
613 // incrementation.
614 //
615 void wxRegionIterator::operator++ ()
616 {
617 if (m_lCurrent < m_lNumRects)
618 ++m_lCurrent;
619 } // end of wxRegionIterator::operator ++
620
621 //
622 // Increment iterator. The rectangle returned is the one before the
623 // incrementation.
624 //
625 void wxRegionIterator::operator++ (int)
626 {
627 if (m_lCurrent < m_lNumRects)
628 ++m_lCurrent;
629 } // end of wxRegionIterator::operator++
630
631 wxCoord wxRegionIterator::GetX() const
632 {
633 if (m_lCurrent < m_lNumRects)
634 return m_pRects[m_lCurrent].x;
635 return 0L;
636 } // end of wxRegionIterator::GetX
637
638 wxCoord wxRegionIterator::GetY() const
639 {
640 if (m_lCurrent < m_lNumRects)
641 return m_pRects[m_lCurrent].y;
642 return 0L;
643 } // end of wxRegionIterator::GetY
644
645 wxCoord wxRegionIterator::GetW() const
646 {
647 if (m_lCurrent < m_lNumRects)
648 return m_pRects[m_lCurrent].width ;
649 return 0L;
650 } // end of wxRegionIterator::GetW
651
652 wxCoord wxRegionIterator::GetH() const
653 {
654 if (m_lCurrent < m_lNumRects)
655 return m_pRects[m_lCurrent].height;
656 return 0L;
657 } // end of wxRegionIterator::GetH