]> git.saurik.com Git - wxWidgets.git/blob - src/os2/region.cpp
compilation fix for !WX_PRECOMP (missing wx/app.h)
[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::Offset( 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 // Combine rectangle (x, y, w, h) with this.
280 //
281 bool wxRegion::Combine(
282 wxCoord x
283 , wxCoord y
284 , wxCoord vWidth
285 , wxCoord vHeight
286 , wxRegionOp eOp
287 )
288 {
289 return Combine(wxRegion(x, y, vWidth, vHeight), eOp);
290 } // end of wxRegion::Combine
291
292 //
293 // Union region with this.
294 //
295 bool wxRegion::Combine( const wxRegion& rRegion, wxRegionOp eOp )
296 {
297 //
298 // We can't use the API functions if we don't have a valid region handle
299 //
300 if (!m_refData)
301 {
302 // combining with an empty/invalid region works differently depending
303 // on the operation
304 switch (eOp)
305 {
306 case wxRGN_COPY:
307 case wxRGN_OR:
308 case wxRGN_XOR:
309 *this = rRegion;
310 break;
311
312 default:
313 wxFAIL_MSG( _T("unknown region operation") );
314 // fall through
315
316 case wxRGN_AND:
317 case wxRGN_DIFF:
318 // leave empty/invalid
319 return false;
320 }
321 }
322 else // we have a valid region
323 {
324
325 LONG lMode = 0;
326
327 switch (eOp)
328 {
329 case wxRGN_AND:
330 lMode = CRGN_AND;
331 break;
332
333 case wxRGN_OR:
334 lMode = CRGN_OR;
335 break;
336
337 case wxRGN_XOR:
338 lMode = CRGN_XOR;
339 break;
340
341 case wxRGN_DIFF:
342 lMode = CRGN_DIFF;
343 break;
344
345 case wxRGN_COPY:
346 default:
347 lMode = CRGN_COPY;
348 break;
349 }
350 return (::GpiCombineRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
351 ,M_REGION
352 ,M_REGION
353 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion
354 ,lMode
355 ) != RGN_ERROR);
356 }
357 return true;
358 } // end of wxRegion::Combine
359
360 bool wxRegion::Combine(
361 const wxRect& rRect
362 , wxRegionOp eOp
363 )
364 {
365 return Combine( rRect.GetLeft()
366 ,rRect.GetTop()
367 ,rRect.GetWidth()
368 ,rRect.GetHeight()
369 ,eOp
370 );
371 } // end of wxRegion::Combine
372
373 //-----------------------------------------------------------------------------
374 //# Information on region
375 //-----------------------------------------------------------------------------
376
377 //
378 // Outer bounds of region
379 //
380 void wxRegion::GetBox(
381 wxCoord& x
382 , wxCoord& y
383 , wxCoord& vWidth
384 , wxCoord& vHeight
385 ) const
386 {
387 if (m_refData)
388 {
389 RECTL vRect;
390 APIRET rc;
391
392 rc = ::GpiQueryRegionBox( ((wxRegionRefData*)m_refData)->m_hPS
393 ,M_REGION
394 ,&vRect
395 );
396 x = vRect.xLeft;
397 y = vRect.yBottom;
398 vWidth = vRect.xRight - vRect.xLeft;
399 vHeight = vRect.yTop - vRect.yBottom;
400 }
401 else
402 {
403 x = y = vWidth = vHeight = 0L;
404 }
405 } // end of wxRegion::GetBox
406
407 wxRect wxRegion::GetBox() const
408 {
409 wxCoord x, y, w, h;
410 GetBox(x, y, w, h);
411 return wxRect(x, y, w, h);
412 }
413
414 //
415 // Is region empty?
416 //
417 bool wxRegion::Empty() const
418 {
419 wxCoord x;
420 wxCoord y;
421 wxCoord vWidth;
422 wxCoord vHeight;
423
424 if (M_REGION == 0)
425 return true;
426
427 GetBox( x
428 ,y
429 ,vWidth
430 ,vHeight
431 );
432 return ((vWidth == 0) && (vHeight == 0));
433 } // end of wxRegion::Empty
434
435 //-----------------------------------------------------------------------------
436 // Tests
437 //-----------------------------------------------------------------------------
438 //
439 // Does the region contain the point pt?
440 //
441 wxRegionContain wxRegion::Contains( const wxPoint& rPoint ) const
442 {
443 POINTL vPoint = { rPoint.x, rPoint.y };
444
445 if (!m_refData)
446 return wxOutRegion;
447
448 LONG lInside = ::GpiPtInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
449 M_REGION,
450 &vPoint
451 );
452 if (lInside == PRGN_INSIDE)
453 return wxInRegion;
454 else
455 return wxOutRegion;
456 } // end of wxRegion::Contains
457
458 //
459 // Does the region contain the rectangle (x, y, w, h)?
460 //
461 wxRegionContain wxRegion::Contains( wxCoord x,
462 wxCoord y,
463 wxCoord vWidth,
464 wxCoord vHeight ) const
465 {
466 if (!m_refData)
467 return wxOutRegion;
468
469 RECTL vRect;
470 vRect.xLeft = x;
471 vRect.xRight = x + vWidth;
472 vRect.yTop = y + vHeight;
473 vRect.yBottom = y;
474
475 LONG lInside = ::GpiRectInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
476 M_REGION,
477 &vRect
478 );
479 switch (lInside)
480 {
481 case RRGN_INSIDE : return wxInRegion;
482 case RRGN_PARTIAL : return wxPartRegion;
483 case RRGN_ERROR :
484 default : return wxOutRegion;
485 }
486 } // end of wxRegion::Contains
487
488 //
489 // Get internal region handle
490 //
491 WXHRGN wxRegion::GetHRGN() const
492 {
493 if (!m_refData)
494 return (WXHRGN) 0;
495 return (WXHRGN) M_REGION;
496 }
497
498 //
499 // Set a new PS, this means we have to recreate the old region in the new
500 // PS
501 //
502 void wxRegion::SetPS(
503 HPS hPS
504 )
505 {
506 RGNRECT vRgnData;
507 PRECTL pRect = NULL;
508
509 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
510 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
511 ,((wxRegionRefData*)m_refData)->m_hRegion
512 ,NULL
513 ,&vRgnData
514 ,NULL
515 ))
516 {
517 pRect = new RECTL[vRgnData.crcReturned];
518 vRgnData.crc = vRgnData.crcReturned;
519 vRgnData.ircStart = 1;
520 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
521 ,((wxRegionRefData*)m_refData)->m_hRegion
522 ,NULL
523 ,&vRgnData
524 ,pRect
525 ))
526 {
527 //
528 // First destroy the region out of the old PS
529 // and then create it in the new and set the new to current
530 //
531 ::GpiDestroyRegion( ((wxRegionRefData*)m_refData)->m_hPS
532 ,M_REGION
533 );
534 ((wxRegionRefData*)m_refData)->m_hRegion = ::GpiCreateRegion( hPS
535 ,vRgnData.crcReturned
536 ,pRect
537 );
538 ((wxRegionRefData*)m_refData)->m_hPS = hPS;
539 }
540 delete [] pRect;
541 }
542 } // end of wxRegion::SetPS
543
544 ///////////////////////////////////////////////////////////////////////////////
545 // //
546 // wxRegionIterator //
547 // //
548 ///////////////////////////////////////////////////////////////////////////////
549
550 //
551 // Initialize empty iterator
552 //
553 wxRegionIterator::wxRegionIterator()
554 : m_lCurrent(0)
555 , m_lNumRects(0)
556 , m_pRects(NULL)
557 {
558 } // end of wxRegionIterator::wxRegionIterator
559
560 wxRegionIterator::~wxRegionIterator()
561 {
562 if (m_pRects)
563 delete[] m_pRects;
564 } // end of wxRegionIterator::~wxRegionIterator
565
566 //
567 // Initialize iterator for region
568 //
569 wxRegionIterator::wxRegionIterator(
570 const wxRegion& rRegion
571 )
572 {
573 m_pRects = NULL;
574 Reset(rRegion);
575 } // end of wxRegionIterator::wxRegionIterator
576
577 //
578 // Reset iterator for a new /e region.
579 //
580 void wxRegionIterator::Reset(
581 const wxRegion& rRegion
582 )
583 {
584 m_lCurrent = 0;
585 m_lNumRects = 0;
586 m_vRegion = rRegion;
587
588 if (m_pRects)
589 delete[] m_pRects;
590
591 m_pRects = NULL;
592
593 if (m_vRegion.Empty())
594 m_lNumRects = 0;
595 else
596 {
597 RGNRECT vRgnData;
598 PRECTL pRect;
599
600 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
601 if (::GpiQueryRegionRects( ((wxRegionRefData*)rRegion.m_refData)->m_hPS // Pres space
602 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion // Handle of region to query
603 ,NULL // Return all RECTs
604 ,&vRgnData // Will contain number or RECTs in region
605 ,NULL // NULL to return number of RECTs
606 ))
607 {
608 pRect = new RECTL[vRgnData.crcReturned];
609 m_pRects = new wxRect[vRgnData.crcReturned];
610 vRgnData.crc = vRgnData.crcReturned;
611 m_lNumRects = vRgnData.crcReturned;
612 vRgnData.ircStart = 1;
613 if (::GpiQueryRegionRects( ((wxRegionRefData*)rRegion.m_refData)->m_hPS // Pres space of source
614 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion // Handle of source region
615 ,NULL // Return all RECTs
616 ,&vRgnData // Operations set to return rects
617 ,pRect // Will contain the actual RECTS
618 ))
619 {
620 #if 0
621 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
622 ,vRgnData.crcReturned
623 ,pRect
624 );
625 #endif
626 for( LONG i = 0; i < m_lNumRects; i++)
627 {
628 m_pRects[i].x = pRect[i].xLeft;
629 m_pRects[i].width = pRect[i].xRight - pRect[i].xLeft;
630 m_pRects[i].y = pRect[i].yBottom;
631 m_pRects[i].height = pRect[i].yTop - pRect[i].yBottom;
632 }
633 #if 0
634 ((wxRegionRefData*)m_refData)->m_hPS = ((wxRegionRefData*)rRegion.m_refData)->m_hPS;
635 #endif
636 }
637 }
638 }
639 } // end of wxRegionIterator::Reset
640
641 //
642 // Increment iterator. The rectangle returned is the one after the
643 // incrementation.
644 //
645 void wxRegionIterator::operator++ ()
646 {
647 if (m_lCurrent < m_lNumRects)
648 ++m_lCurrent;
649 } // end of wxRegionIterator::operator ++
650
651 //
652 // Increment iterator. The rectangle returned is the one before the
653 // incrementation.
654 //
655 void wxRegionIterator::operator++ (int)
656 {
657 if (m_lCurrent < m_lNumRects)
658 ++m_lCurrent;
659 } // end of wxRegionIterator::operator++
660
661 wxCoord wxRegionIterator::GetX() const
662 {
663 if (m_lCurrent < m_lNumRects)
664 return m_pRects[m_lCurrent].x;
665 return 0L;
666 } // end of wxRegionIterator::GetX
667
668 wxCoord wxRegionIterator::GetY() const
669 {
670 if (m_lCurrent < m_lNumRects)
671 return m_pRects[m_lCurrent].y;
672 return 0L;
673 } // end of wxRegionIterator::GetY
674
675 wxCoord wxRegionIterator::GetW() const
676 {
677 if (m_lCurrent < m_lNumRects)
678 return m_pRects[m_lCurrent].width ;
679 return 0L;
680 } // end of wxRegionIterator::GetW
681
682 wxCoord wxRegionIterator::GetH() const
683 {
684 if (m_lCurrent < m_lNumRects)
685 return m_pRects[m_lCurrent].height;
686 return 0L;
687 } // end of wxRegionIterator::GetH