]> git.saurik.com Git - wxWidgets.git/blob - src/os2/region.cpp
Remove all lines containing cvs/svn "$Id$" keyword.
[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 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/app.h"
16 #include "wx/window.h"
17 #include "wx/gdicmn.h"
18 #endif
19
20 #include "wx/os2/region.h"
21
22 #include "wx/os2/private.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
25 IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
26
27 //-----------------------------------------------------------------------------
28 // wxRegionRefData implementation
29 //-----------------------------------------------------------------------------
30
31 class WXDLLEXPORT wxRegionRefData : public wxGDIRefData {
32 public:
33 wxRegionRefData()
34 {
35 m_hRegion = 0;
36 m_hPS = 0;
37 }
38
39 wxRegionRefData(const wxRegionRefData& rData)
40 {
41 RGNRECT vRgnData;
42 PRECTL pRect = NULL;
43
44 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
45 if (::GpiQueryRegionRects( rData.m_hPS // Pres space
46 ,rData.m_hRegion // Handle of region to query
47 ,NULL // Return all RECTs
48 ,&vRgnData // Will contain number or RECTs in region
49 ,NULL // NULL to return number of RECTs
50 ))
51 {
52 pRect = new RECTL[vRgnData.crcReturned];
53 vRgnData.crc = vRgnData.crcReturned;
54 vRgnData.ircStart = 1;
55 if (::GpiQueryRegionRects( rData.m_hPS // Pres space of source
56 ,rData.m_hRegion // Handle of source region
57 ,NULL // Return all RECTs
58 ,&vRgnData // Operations set to return rects
59 ,pRect // Will contain the actual RECTS
60 ))
61 {
62 m_hRegion = ::GpiCreateRegion( rData.m_hPS
63 ,vRgnData.crcReturned
64 ,pRect
65 );
66 m_hPS = rData.m_hPS;
67 }
68 delete [] pRect;
69 }
70 }
71
72 virtual ~wxRegionRefData()
73 {
74 ::GpiDestroyRegion(m_hPS, m_hRegion);
75 }
76
77 HRGN m_hRegion;
78 HPS m_hPS;
79 };
80
81 #define M_REGION (((wxRegionRefData*)m_refData)->m_hRegion)
82 #define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_hRegion)
83
84 //-----------------------------------------------------------------------------
85 // wxRegion
86 //-----------------------------------------------------------------------------
87
88 // General remark:
89 // wxRegion is always basically stored in wx coordinates. However, since
90 // OS/2's internal functions rely on "top > bottom", the values of top and
91 // bottom values of a region need to be interchanged, as compared to wx.
92 // This needs to be taken into account when feeding any rectangle to wx _or_
93 // when accessing the region data via GetBox, wxRegionIterator or otherwise.
94
95 /*!
96 * Create an empty region.
97 */
98 wxRegion::wxRegion()
99 {
100 m_refData = new wxRegionRefData;
101 } // end of wxRegion::wxRegion
102
103 wxRegion::wxRegion(
104 WXHRGN hRegion,
105 WXHDC hPS
106 )
107 {
108 m_refData = new wxRegionRefData;
109 M_REGION = (HRGN) hRegion;
110 (((wxRegionRefData*)m_refData)->m_hPS) = hPS;
111 } // end of wxRegion::wxRegion
112
113 wxRegion::wxRegion(
114 wxCoord x
115 , wxCoord y
116 , wxCoord vWidth
117 , wxCoord vHeight
118 )
119 {
120 RECTL vRect;
121 SIZEL vSize = {0, 0};
122 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
123 HDC hDC = ::DevOpenDC( vHabmain
124 ,OD_MEMORY
125 ,"*"
126 ,5L
127 ,(PDEVOPENDATA)&vDop
128 ,NULLHANDLE
129 );
130
131
132 vRect.xLeft = x;
133 vRect.xRight = x + vWidth;
134 vRect.yBottom = y;
135 vRect.yTop = y + vHeight;
136
137 m_refData = new wxRegionRefData;
138
139 //
140 // Need a PS to create a Region
141 //
142 ((wxRegionRefData*)m_refData)->m_hPS = ::GpiCreatePS( vHabmain
143 ,hDC
144 ,&vSize
145 ,PU_PELS | GPIT_MICRO | GPIA_ASSOC
146 );
147 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)m_refData)->m_hPS
148 ,1
149 ,&vRect
150 );
151 } // end of wxRegion::wxRegion
152
153 wxRegion::wxRegion(const wxPoint& rTopLeft,
154 const wxPoint& rBottomRight)
155 {
156 RECTL vRect;
157 SIZEL vSize = {0, 0};
158 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
159 HDC hDC = ::DevOpenDC( vHabmain
160 ,OD_MEMORY
161 ,"*"
162 ,5L
163 ,(PDEVOPENDATA)&vDop
164 ,NULLHANDLE
165 );
166
167 vRect.xLeft = rTopLeft.x;
168 vRect.xRight = rBottomRight.x;
169 vRect.yBottom = rTopLeft.y;
170 vRect.yTop = rBottomRight.y;
171
172 m_refData = new wxRegionRefData;
173
174 //
175 // Need a PS to create a Region
176 //
177 ((wxRegionRefData*)m_refData)->m_hPS = ::GpiCreatePS( vHabmain
178 ,hDC
179 ,&vSize
180 ,PU_PELS | GPIT_MICRO | GPIA_ASSOC
181 );
182 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)m_refData)->m_hPS
183 ,1
184 ,&vRect
185 );
186 } // end of wxRegion::wxRegion
187
188 wxRegion::wxRegion(const wxRect& rRect)
189 {
190 RECTL vRect;
191 SIZEL vSize = {0, 0};
192 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
193 HDC hDC = ::DevOpenDC( vHabmain
194 ,OD_MEMORY
195 ,"*"
196 ,5L
197 ,(PDEVOPENDATA)&vDop
198 ,NULLHANDLE
199 );
200
201
202 vRect.xLeft = rRect.x;
203 vRect.xRight = rRect.x + rRect.width;
204 vRect.yBottom = rRect.y;
205 vRect.yTop = rRect.y + rRect.height;
206
207 m_refData = new wxRegionRefData;
208
209 //
210 // Need a PS to create a Region
211 //
212 ((wxRegionRefData*)m_refData)->m_hPS = ::GpiCreatePS( vHabmain
213 ,hDC
214 ,&vSize
215 ,PU_PELS | GPIT_MICRO | GPIA_ASSOC
216 );
217 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)m_refData)->m_hPS
218 ,1
219 ,&vRect
220 );
221 } // end of wxRegion::wxRegion
222
223 wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode WXUNUSED(fillStyle))
224 {
225 // TO DO
226 }
227
228 //
229 // Destroy the region.
230 //
231 wxRegion::~wxRegion()
232 {
233 } // end of wxRegion::~wxRegion
234
235 wxGDIRefData *wxRegion::CreateGDIRefData() const
236 {
237 return new wxRegionRefData;
238 }
239
240 wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *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(wxT("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( wxT("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& WXUNUSED(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( wxCoord x, wxCoord y ) const
415 {
416 POINTL vPoint = { x, 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 = rect.x;
441 vRect.xRight = rect.x + rect.width;
442 vRect.yTop = rect.y + rect.height;
443 vRect.yBottom = rect.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
457 } // end of wxRegion::Contains
458
459 //
460 // Get internal region handle
461 //
462 WXHRGN wxRegion::GetHRGN() const
463 {
464 if (!m_refData)
465 return (WXHRGN) 0;
466 return (WXHRGN) M_REGION;
467 }
468
469 //
470 // Set a new PS, this means we have to recreate the old region in the new
471 // PS
472 //
473 void wxRegion::SetPS(
474 HPS hPS
475 )
476 {
477 RGNRECT vRgnData;
478 PRECTL pRect = NULL;
479
480 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
481 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
482 ,((wxRegionRefData*)m_refData)->m_hRegion
483 ,NULL
484 ,&vRgnData
485 ,NULL
486 ))
487 {
488 pRect = new RECTL[vRgnData.crcReturned];
489 vRgnData.crc = vRgnData.crcReturned;
490 vRgnData.ircStart = 1;
491 if (::GpiQueryRegionRects( ((wxRegionRefData*)m_refData)->m_hPS
492 ,((wxRegionRefData*)m_refData)->m_hRegion
493 ,NULL
494 ,&vRgnData
495 ,pRect
496 ))
497 {
498 //
499 // First destroy the region out of the old PS
500 // and then create it in the new and set the new to current
501 //
502 ::GpiDestroyRegion( ((wxRegionRefData*)m_refData)->m_hPS
503 ,M_REGION
504 );
505 ((wxRegionRefData*)m_refData)->m_hRegion = ::GpiCreateRegion( hPS
506 ,vRgnData.crcReturned
507 ,pRect
508 );
509 ((wxRegionRefData*)m_refData)->m_hPS = hPS;
510 }
511 delete [] pRect;
512 }
513 } // end of wxRegion::SetPS
514
515 ///////////////////////////////////////////////////////////////////////////////
516 // //
517 // wxRegionIterator //
518 // //
519 ///////////////////////////////////////////////////////////////////////////////
520
521 //
522 // Initialize empty iterator
523 //
524 wxRegionIterator::wxRegionIterator()
525 : m_lCurrent(0)
526 , m_lNumRects(0)
527 , m_pRects(NULL)
528 {
529 } // end of wxRegionIterator::wxRegionIterator
530
531 wxRegionIterator::~wxRegionIterator()
532 {
533 if (m_pRects)
534 delete[] m_pRects;
535 } // end of wxRegionIterator::~wxRegionIterator
536
537 //
538 // Initialize iterator for region
539 //
540 wxRegionIterator::wxRegionIterator(
541 const wxRegion& rRegion
542 )
543 {
544 m_pRects = NULL;
545 Reset(rRegion);
546 } // end of wxRegionIterator::wxRegionIterator
547
548 //
549 // Reset iterator for a new /e region.
550 //
551 void wxRegionIterator::Reset(
552 const wxRegion& rRegion
553 )
554 {
555 m_lCurrent = 0;
556 m_lNumRects = 0;
557 m_vRegion = rRegion;
558
559 if (m_pRects)
560 delete[] m_pRects;
561
562 m_pRects = NULL;
563
564 if (m_vRegion.Empty())
565 m_lNumRects = 0;
566 else
567 {
568 RGNRECT vRgnData;
569 PRECTL pRect;
570
571 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
572 if (::GpiQueryRegionRects( ((wxRegionRefData*)rRegion.m_refData)->m_hPS // Pres space
573 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion // Handle of region to query
574 ,NULL // Return all RECTs
575 ,&vRgnData // Will contain number or RECTs in region
576 ,NULL // NULL to return number of RECTs
577 ))
578 {
579 pRect = new RECTL[vRgnData.crcReturned];
580 m_pRects = new wxRect[vRgnData.crcReturned];
581 vRgnData.crc = vRgnData.crcReturned;
582 m_lNumRects = vRgnData.crcReturned;
583 vRgnData.ircStart = 1;
584 if (::GpiQueryRegionRects( ((wxRegionRefData*)rRegion.m_refData)->m_hPS // Pres space of source
585 ,((wxRegionRefData*)rRegion.m_refData)->m_hRegion // Handle of source region
586 ,NULL // Return all RECTs
587 ,&vRgnData // Operations set to return rects
588 ,pRect // Will contain the actual RECTS
589 ))
590 {
591 #if 0
592 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
593 ,vRgnData.crcReturned
594 ,pRect
595 );
596 #endif
597 for( LONG i = 0; i < m_lNumRects; i++)
598 {
599 m_pRects[i].x = pRect[i].xLeft;
600 m_pRects[i].width = pRect[i].xRight - pRect[i].xLeft;
601 m_pRects[i].y = pRect[i].yBottom;
602 m_pRects[i].height = pRect[i].yTop - pRect[i].yBottom;
603 }
604 #if 0
605 ((wxRegionRefData*)m_refData)->m_hPS = ((wxRegionRefData*)rRegion.m_refData)->m_hPS;
606 #endif
607 }
608 }
609 }
610 } // end of wxRegionIterator::Reset
611
612 //
613 // Increment iterator. The rectangle returned is the one after the
614 // incrementation.
615 //
616 void wxRegionIterator::operator++ ()
617 {
618 if (m_lCurrent < m_lNumRects)
619 ++m_lCurrent;
620 } // end of wxRegionIterator::operator ++
621
622 //
623 // Increment iterator. The rectangle returned is the one before the
624 // incrementation.
625 //
626 void wxRegionIterator::operator++ (int)
627 {
628 if (m_lCurrent < m_lNumRects)
629 ++m_lCurrent;
630 } // end of wxRegionIterator::operator++
631
632 wxCoord wxRegionIterator::GetX() const
633 {
634 if (m_lCurrent < m_lNumRects)
635 return m_pRects[m_lCurrent].x;
636 return 0L;
637 } // end of wxRegionIterator::GetX
638
639 wxCoord wxRegionIterator::GetY() const
640 {
641 if (m_lCurrent < m_lNumRects)
642 return m_pRects[m_lCurrent].y;
643 return 0L;
644 } // end of wxRegionIterator::GetY
645
646 wxCoord wxRegionIterator::GetW() const
647 {
648 if (m_lCurrent < m_lNumRects)
649 return m_pRects[m_lCurrent].width ;
650 return 0L;
651 } // end of wxRegionIterator::GetW
652
653 wxCoord wxRegionIterator::GetH() const
654 {
655 if (m_lCurrent < m_lNumRects)
656 return m_pRects[m_lCurrent].height;
657 return 0L;
658 } // end of wxRegionIterator::GetH