]> git.saurik.com Git - wxWidgets.git/blob - src/os2/region.cpp
call Init() from wxBufferedDC ctors to ensure that layout direction is inherited...
[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& 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