]> git.saurik.com Git - wxWidgets.git/blame - src/os2/region.cpp
move generic part of AdjustSize() and status bar methods to the base class
[wxWidgets.git] / src / os2 / region.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// File: src/os2/region.cpp
0e320a79 3// Purpose: Region class
409c9842
DW
4// Author: David Webster
5// Modified by:
6// Created: 10/15/99
19193a2c 7// RCS-ID: $Id$
744c5946 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
409c9842
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
0e320a79 14
670f9935
WS
15#ifndef WX_PRECOMP
16 #include "wx/app.h"
cdccdfab 17 #include "wx/window.h"
dd05139a 18 #include "wx/gdicmn.h"
670f9935
WS
19#endif
20
409c9842 21#include "wx/os2/region.h"
0e320a79 22
409c9842
DW
23#include "wx/os2/private.h"
24
670f9935
WS
25IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
26IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
0e320a79
DW
27
28//-----------------------------------------------------------------------------
29// wxRegionRefData implementation
30//-----------------------------------------------------------------------------
31
32class WXDLLEXPORT wxRegionRefData : public wxGDIRefData {
33public:
409c9842
DW
34 wxRegionRefData()
35 {
8d854fa9 36 m_hRegion = 0;
19193a2c 37 m_hPS = 0;
409c9842 38 }
0e320a79 39
8d854fa9 40 wxRegionRefData(const wxRegionRefData& rData)
409c9842 41 {
8d854fa9
DW
42 RGNRECT vRgnData;
43 PRECTL pRect = NULL;
44
53e8757b 45 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
8d854fa9
DW
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 }
409c9842 71 }
0e320a79 72
d3c7fc99 73 virtual ~wxRegionRefData()
409c9842 74 {
8d854fa9 75 ::GpiDestroyRegion(m_hPS, m_hRegion);
409c9842 76 }
04701dd9 77
8d854fa9
DW
78 HRGN m_hRegion;
79 HPS m_hPS;
0e320a79
DW
80};
81
8d854fa9 82#define M_REGION (((wxRegionRefData*)m_refData)->m_hRegion)
6ed98c6a 83#define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_hRegion)
0e320a79
DW
84
85//-----------------------------------------------------------------------------
86// wxRegion
87//-----------------------------------------------------------------------------
88
744c5946
SN
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
0e320a79
DW
96/*!
97 * Create an empty region.
98 */
99wxRegion::wxRegion()
100{
101 m_refData = new wxRegionRefData;
8d854fa9 102} // end of wxRegion::wxRegion
0e320a79 103
8d854fa9 104wxRegion::wxRegion(
19193a2c
KB
105 WXHRGN hRegion,
106 WXHDC hPS
8d854fa9 107)
409c9842
DW
108{
109 m_refData = new wxRegionRefData;
110 M_REGION = (HRGN) hRegion;
19193a2c 111 (((wxRegionRefData*)m_refData)->m_hPS) = hPS;
8d854fa9
DW
112} // end of wxRegion::wxRegion
113
114wxRegion::wxRegion(
115 wxCoord x
116, wxCoord y
117, wxCoord vWidth
118, wxCoord vHeight
119)
0e320a79 120{
8d854fa9
DW
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
154wxRegion::wxRegion(
155 const wxPoint& rTopLeft
156, const wxPoint& rBottomRight
157)
0e320a79 158{
8d854fa9
DW
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;
744c5946
SN
172 vRect.yBottom = rTopLeft.y;
173 vRect.yTop = rBottomRight.y;
8d854fa9
DW
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
191wxRegion::wxRegion(
192 const wxRect& rRect
193)
0e320a79 194{
8d854fa9
DW
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//
0e320a79
DW
231wxRegion::~wxRegion()
232{
8d854fa9 233} // end of wxRegion::~wxRegion
0e320a79 234
58238121
DW
235wxObjectRefData *wxRegion::CreateData() const
236{
237 return new wxRegionRefData;
238}
239
70b7bcd1 240wxObjectRefData *wxRegion::CloneData(const wxObjectRefData *data) const
58238121
DW
241{
242 return new wxRegionRefData(*(wxRegionRefData *)data);
243}
244
0e320a79
DW
245//-----------------------------------------------------------------------------
246//# Modify region
247//-----------------------------------------------------------------------------
248
670f9935 249bool wxRegion::Offset( wxCoord x, wxCoord y )
58238121
DW
250{
251 if ( !x && !y )
252 {
253 // nothing to do
670f9935 254 return true;
58238121
DW
255 }
256
257 AllocExclusive();
258
259#if 0
260 if ( ::OffsetRgn(GetHrgn(), x, y) == ERROR )
261 {
262 wxLogLastError(_T("OffsetRgn"));
263
670f9935 264 return false;
58238121
DW
265 }
266#endif
670f9935 267 return true;
58238121
DW
268}
269
8d854fa9
DW
270//
271// Clear current region
272//
0e320a79
DW
273void wxRegion::Clear()
274{
275 UnRef();
8d854fa9
DW
276} // end of wxRegion::Clear
277
278//
279// Combine rectangle (x, y, w, h) with this.
280//
281bool wxRegion::Combine(
282 wxCoord x
283, wxCoord y
284, wxCoord vWidth
285, wxCoord vHeight
286, wxRegionOp eOp
287)
0e320a79 288{
6ed98c6a 289 return Combine(wxRegion(x, y, vWidth, vHeight), eOp);
8d854fa9
DW
290} // end of wxRegion::Combine
291
292//
293// Union region with this.
294//
670f9935 295bool wxRegion::Combine( const wxRegion& rRegion, wxRegionOp eOp )
0e320a79 296{
6ed98c6a
DW
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
670f9935 319 return false;
6ed98c6a
DW
320 }
321 }
322 else // we have a valid region
323 {
0e320a79 324
6ed98c6a 325 LONG lMode = 0;
8d854fa9 326
6ed98c6a
DW
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);
0e320a79 356 }
670f9935 357 return true;
8d854fa9
DW
358} // end of wxRegion::Combine
359
360bool wxRegion::Combine(
361 const wxRect& rRect
362, wxRegionOp eOp
363)
0e320a79 364{
8d854fa9
DW
365 return Combine( rRect.GetLeft()
366 ,rRect.GetTop()
367 ,rRect.GetWidth()
368 ,rRect.GetHeight()
369 ,eOp
370 );
371} // end of wxRegion::Combine
0e320a79
DW
372
373//-----------------------------------------------------------------------------
374//# Information on region
375//-----------------------------------------------------------------------------
376
8d854fa9 377//
0e320a79 378// Outer bounds of region
8d854fa9
DW
379//
380void wxRegion::GetBox(
381 wxCoord& x
382, wxCoord& y
383, wxCoord& vWidth
384, wxCoord& vHeight
385) const
0e320a79 386{
8d854fa9
DW
387 if (m_refData)
388 {
389 RECTL vRect;
19193a2c 390 APIRET rc;
8d854fa9 391
19193a2c 392 rc = ::GpiQueryRegionBox( ((wxRegionRefData*)m_refData)->m_hPS
8d854fa9
DW
393 ,M_REGION
394 ,&vRect
395 );
396 x = vRect.xLeft;
4a46a5df 397 y = vRect.yBottom;
8d854fa9
DW
398 vWidth = vRect.xRight - vRect.xLeft;
399 vHeight = vRect.yTop - vRect.yBottom;
409c9842 400 }
8d854fa9
DW
401 else
402 {
403 x = y = vWidth = vHeight = 0L;
404 }
405} // end of wxRegion::GetBox
0e320a79
DW
406
407wxRect wxRegion::GetBox() const
408{
0b7e7739 409 wxCoord x, y, w, h;
0e320a79
DW
410 GetBox(x, y, w, h);
411 return wxRect(x, y, w, h);
412}
413
8d854fa9 414//
0e320a79 415// Is region empty?
8d854fa9 416//
0e320a79
DW
417bool wxRegion::Empty() const
418{
670f9935
WS
419 wxCoord x;
420 wxCoord y;
421 wxCoord vWidth;
422 wxCoord vHeight;
8d854fa9
DW
423
424 if (M_REGION == 0)
670f9935 425 return true;
8d854fa9
DW
426
427 GetBox( x
428 ,y
429 ,vWidth
430 ,vHeight
431 );
432 return ((vWidth == 0) && (vHeight == 0));
433} // end of wxRegion::Empty
0e320a79
DW
434
435//-----------------------------------------------------------------------------
8d854fa9 436// Tests
0e320a79 437//-----------------------------------------------------------------------------
8d854fa9 438//
0e320a79 439// Does the region contain the point pt?
8d854fa9 440//
744c5946 441wxRegionContain wxRegion::Contains( const wxPoint& rPoint ) const
0e320a79 442{
8d854fa9
DW
443 POINTL vPoint = { rPoint.x, rPoint.y };
444
409c9842
DW
445 if (!m_refData)
446 return wxOutRegion;
0e320a79 447
744c5946
SN
448 LONG lInside = ::GpiPtInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
449 M_REGION,
450 &vPoint
451 );
8d854fa9 452 if (lInside == PRGN_INSIDE)
0e320a79
DW
453 return wxInRegion;
454 else
455 return wxOutRegion;
8d854fa9 456} // end of wxRegion::Contains
0e320a79 457
8d854fa9 458//
0e320a79 459// Does the region contain the rectangle (x, y, w, h)?
8d854fa9 460//
744c5946
SN
461wxRegionContain wxRegion::Contains( wxCoord x,
462 wxCoord y,
463 wxCoord vWidth,
464 wxCoord vHeight ) const
0e320a79 465{
409c9842
DW
466 if (!m_refData)
467 return wxOutRegion;
0e320a79 468
744c5946 469 RECTL vRect;
8d854fa9 470 vRect.xLeft = x;
8d854fa9 471 vRect.xRight = x + vWidth;
744c5946
SN
472 vRect.yTop = y + vHeight;
473 vRect.yBottom = y;
0e320a79 474
744c5946
SN
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 }
8d854fa9
DW
486} // end of wxRegion::Contains
487
488//
409c9842 489// Get internal region handle
8d854fa9 490//
409c9842
DW
491WXHRGN wxRegion::GetHRGN() const
492{
493 if (!m_refData)
494 return (WXHRGN) 0;
495 return (WXHRGN) M_REGION;
496}
497
44227537
DW
498//
499// Set a new PS, this means we have to recreate the old region in the new
500// PS
501//
502void wxRegion::SetPS(
503 HPS hPS
504)
505{
506 RGNRECT vRgnData;
507 PRECTL pRect = NULL;
508
53e8757b 509 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
44227537
DW
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
0e320a79 544///////////////////////////////////////////////////////////////////////////////
409c9842
DW
545// //
546// wxRegionIterator //
547// //
0e320a79
DW
548///////////////////////////////////////////////////////////////////////////////
549
8d854fa9
DW
550//
551// Initialize empty iterator
552//
553wxRegionIterator::wxRegionIterator()
554: m_lCurrent(0)
555, m_lNumRects(0)
556, m_pRects(NULL)
0e320a79 557{
8d854fa9 558} // end of wxRegionIterator::wxRegionIterator
0e320a79
DW
559
560wxRegionIterator::~wxRegionIterator()
561{
8d854fa9
DW
562 if (m_pRects)
563 delete[] m_pRects;
564} // end of wxRegionIterator::~wxRegionIterator
565
566//
567// Initialize iterator for region
568//
569wxRegionIterator::wxRegionIterator(
570 const wxRegion& rRegion
571)
0e320a79 572{
8d854fa9
DW
573 m_pRects = NULL;
574 Reset(rRegion);
575} // end of wxRegionIterator::wxRegionIterator
576
577//
578// Reset iterator for a new /e region.
579//
580void wxRegionIterator::Reset(
581 const wxRegion& rRegion
582)
0e320a79 583{
8d854fa9 584 m_lCurrent = 0;
0b483d6e 585 m_lNumRects = 0;
8d854fa9 586 m_vRegion = rRegion;
0e320a79 587
8d854fa9
DW
588 if (m_pRects)
589 delete[] m_pRects;
0e320a79 590
8d854fa9 591 m_pRects = NULL;
0e320a79 592
8d854fa9
DW
593 if (m_vRegion.Empty())
594 m_lNumRects = 0;
409c9842 595 else
0e320a79 596 {
8d854fa9
DW
597 RGNRECT vRgnData;
598 PRECTL pRect;
599
53e8757b 600 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
8d854fa9
DW
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 {
53e8757b 620#if 0
8d854fa9
DW
621 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
622 ,vRgnData.crcReturned
623 ,pRect
624 );
53e8757b 625#endif
8d854fa9
DW
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 }
53e8757b 633#if 0
8d854fa9 634 ((wxRegionRefData*)m_refData)->m_hPS = ((wxRegionRefData*)rRegion.m_refData)->m_hPS;
53e8757b 635#endif
8d854fa9
DW
636 }
637 }
0e320a79 638 }
8d854fa9 639} // end of wxRegionIterator::Reset
0e320a79 640
8d854fa9
DW
641//
642// Increment iterator. The rectangle returned is the one after the
643// incrementation.
644//
645void wxRegionIterator::operator++ ()
0e320a79 646{
8d854fa9
DW
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//
655void wxRegionIterator::operator++ (int)
0e320a79 656{
8d854fa9
DW
657 if (m_lCurrent < m_lNumRects)
658 ++m_lCurrent;
659} // end of wxRegionIterator::operator++
0e320a79 660
0b7e7739 661wxCoord wxRegionIterator::GetX() const
0e320a79 662{
8d854fa9
DW
663 if (m_lCurrent < m_lNumRects)
664 return m_pRects[m_lCurrent].x;
665 return 0L;
666} // end of wxRegionIterator::GetX
0e320a79 667
0b7e7739 668wxCoord wxRegionIterator::GetY() const
0e320a79 669{
8d854fa9
DW
670 if (m_lCurrent < m_lNumRects)
671 return m_pRects[m_lCurrent].y;
672 return 0L;
673} // end of wxRegionIterator::GetY
0e320a79 674
0b7e7739 675wxCoord wxRegionIterator::GetW() const
0e320a79 676{
8d854fa9
DW
677 if (m_lCurrent < m_lNumRects)
678 return m_pRects[m_lCurrent].width ;
679 return 0L;
680} // end of wxRegionIterator::GetW
0e320a79 681
0b7e7739 682wxCoord wxRegionIterator::GetH() const
0e320a79 683{
8d854fa9
DW
684 if (m_lCurrent < m_lNumRects)
685 return m_pRects[m_lCurrent].height;
686 return 0L;
687} // end of wxRegionIterator::GetH