]> git.saurik.com Git - wxWidgets.git/blame - src/os2/region.cpp
Fix install_name_tool calls in OS X "make install".
[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
744c5946 7// Copyright: (c) David Webster
65571936 8// Licence: wxWindows licence
0e320a79
DW
9/////////////////////////////////////////////////////////////////////////////
10
409c9842
DW
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
0e320a79 13
670f9935
WS
14#ifndef WX_PRECOMP
15 #include "wx/app.h"
cdccdfab 16 #include "wx/window.h"
dd05139a 17 #include "wx/gdicmn.h"
670f9935
WS
18#endif
19
409c9842 20#include "wx/os2/region.h"
0e320a79 21
409c9842
DW
22#include "wx/os2/private.h"
23
670f9935
WS
24IMPLEMENT_DYNAMIC_CLASS(wxRegion, wxGDIObject)
25IMPLEMENT_DYNAMIC_CLASS(wxRegionIterator, wxObject)
0e320a79
DW
26
27//-----------------------------------------------------------------------------
28// wxRegionRefData implementation
29//-----------------------------------------------------------------------------
30
31class WXDLLEXPORT wxRegionRefData : public wxGDIRefData {
32public:
409c9842
DW
33 wxRegionRefData()
34 {
8d854fa9 35 m_hRegion = 0;
19193a2c 36 m_hPS = 0;
409c9842 37 }
0e320a79 38
8d854fa9 39 wxRegionRefData(const wxRegionRefData& rData)
409c9842 40 {
8d854fa9
DW
41 RGNRECT vRgnData;
42 PRECTL pRect = NULL;
43
53e8757b 44 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
8d854fa9
DW
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 }
409c9842 70 }
0e320a79 71
d3c7fc99 72 virtual ~wxRegionRefData()
409c9842 73 {
8d854fa9 74 ::GpiDestroyRegion(m_hPS, m_hRegion);
409c9842 75 }
04701dd9 76
8d854fa9
DW
77 HRGN m_hRegion;
78 HPS m_hPS;
0e320a79
DW
79};
80
8d854fa9 81#define M_REGION (((wxRegionRefData*)m_refData)->m_hRegion)
6ed98c6a 82#define M_REGION_OF(rgn) (((wxRegionRefData*)(rgn.m_refData))->m_hRegion)
0e320a79
DW
83
84//-----------------------------------------------------------------------------
85// wxRegion
86//-----------------------------------------------------------------------------
87
744c5946
SN
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
0e320a79
DW
95/*!
96 * Create an empty region.
97 */
98wxRegion::wxRegion()
99{
100 m_refData = new wxRegionRefData;
8d854fa9 101} // end of wxRegion::wxRegion
0e320a79 102
8d854fa9 103wxRegion::wxRegion(
19193a2c
KB
104 WXHRGN hRegion,
105 WXHDC hPS
8d854fa9 106)
409c9842
DW
107{
108 m_refData = new wxRegionRefData;
109 M_REGION = (HRGN) hRegion;
19193a2c 110 (((wxRegionRefData*)m_refData)->m_hPS) = hPS;
8d854fa9
DW
111} // end of wxRegion::wxRegion
112
113wxRegion::wxRegion(
114 wxCoord x
115, wxCoord y
116, wxCoord vWidth
117, wxCoord vHeight
118)
0e320a79 119{
8d854fa9
DW
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
ec02a2f7
WS
153wxRegion::wxRegion(const wxPoint& rTopLeft,
154 const wxPoint& rBottomRight)
0e320a79 155{
8d854fa9
DW
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;
744c5946
SN
169 vRect.yBottom = rTopLeft.y;
170 vRect.yTop = rBottomRight.y;
8d854fa9
DW
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
ec02a2f7 188wxRegion::wxRegion(const wxRect& rRect)
0e320a79 189{
8d854fa9
DW
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
ec02a2f7 222
e39b5fa4 223wxRegion::wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode WXUNUSED(fillStyle))
ec02a2f7
WS
224{
225 // TO DO
226}
8d854fa9
DW
227
228//
229// Destroy the region.
230//
0e320a79
DW
231wxRegion::~wxRegion()
232{
8d854fa9 233} // end of wxRegion::~wxRegion
0e320a79 234
4b3f61d1 235wxGDIRefData *wxRegion::CreateGDIRefData() const
58238121
DW
236{
237 return new wxRegionRefData;
238}
239
4b3f61d1 240wxGDIRefData *wxRegion::CloneGDIRefData(const wxGDIRefData *data) const
58238121
DW
241{
242 return new wxRegionRefData(*(wxRegionRefData *)data);
243}
244
0e320a79
DW
245//-----------------------------------------------------------------------------
246//# Modify region
247//-----------------------------------------------------------------------------
248
8a16d737 249bool wxRegion::DoOffset( 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 {
9a83f860 262 wxLogLastError(wxT("OffsetRgn"));
58238121 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
8d854fa9
DW
278//
279// Union region with this.
280//
8a16d737 281bool wxRegion::DoCombine( const wxRegion& rRegion, wxRegionOp eOp )
0e320a79 282{
6ed98c6a
DW
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:
9a83f860 299 wxFAIL_MSG( wxT("unknown region operation") );
6ed98c6a
DW
300 // fall through
301
302 case wxRGN_AND:
303 case wxRGN_DIFF:
304 // leave empty/invalid
670f9935 305 return false;
6ed98c6a
DW
306 }
307 }
308 else // we have a valid region
309 {
0e320a79 310
6ed98c6a 311 LONG lMode = 0;
8d854fa9 312
6ed98c6a
DW
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);
0e320a79 342 }
670f9935 343 return true;
8d854fa9
DW
344} // end of wxRegion::Combine
345
0e320a79
DW
346//-----------------------------------------------------------------------------
347//# Information on region
348//-----------------------------------------------------------------------------
349
56617718 350bool wxRegion::DoIsEqual(const wxRegion& WXUNUSED(region)) const
8a16d737
VZ
351{
352 return false;
353}
354
8d854fa9 355//
0e320a79 356// Outer bounds of region
8d854fa9 357//
8a16d737 358bool wxRegion::DoGetBox(
8d854fa9
DW
359 wxCoord& x
360, wxCoord& y
361, wxCoord& vWidth
362, wxCoord& vHeight
363) const
0e320a79 364{
8d854fa9
DW
365 if (m_refData)
366 {
367 RECTL vRect;
19193a2c 368 APIRET rc;
8d854fa9 369
19193a2c 370 rc = ::GpiQueryRegionBox( ((wxRegionRefData*)m_refData)->m_hPS
8d854fa9
DW
371 ,M_REGION
372 ,&vRect
373 );
374 x = vRect.xLeft;
4a46a5df 375 y = vRect.yBottom;
8d854fa9
DW
376 vWidth = vRect.xRight - vRect.xLeft;
377 vHeight = vRect.yTop - vRect.yBottom;
8a16d737 378 return true;
409c9842 379 }
8d854fa9
DW
380 else
381 {
382 x = y = vWidth = vHeight = 0L;
8a16d737 383 return false;
8d854fa9
DW
384 }
385} // end of wxRegion::GetBox
0e320a79 386
8d854fa9 387//
0e320a79 388// Is region empty?
8d854fa9 389//
8a16d737 390bool wxRegion::IsEmpty() const
0e320a79 391{
670f9935
WS
392 wxCoord x;
393 wxCoord y;
394 wxCoord vWidth;
395 wxCoord vHeight;
8d854fa9
DW
396
397 if (M_REGION == 0)
670f9935 398 return true;
8d854fa9
DW
399
400 GetBox( x
401 ,y
402 ,vWidth
403 ,vHeight
404 );
405 return ((vWidth == 0) && (vHeight == 0));
8a16d737 406} // end of wxRegion::IsEmpty
0e320a79
DW
407
408//-----------------------------------------------------------------------------
8d854fa9 409// Tests
0e320a79 410//-----------------------------------------------------------------------------
8d854fa9 411//
0e320a79 412// Does the region contain the point pt?
8d854fa9 413//
56617718 414wxRegionContain wxRegion::DoContainsPoint( wxCoord x, wxCoord y ) const
0e320a79 415{
56617718 416 POINTL vPoint = { x, y };
8d854fa9 417
409c9842
DW
418 if (!m_refData)
419 return wxOutRegion;
0e320a79 420
744c5946
SN
421 LONG lInside = ::GpiPtInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
422 M_REGION,
423 &vPoint
8a16d737 424 );
8d854fa9 425 if (lInside == PRGN_INSIDE)
0e320a79
DW
426 return wxInRegion;
427 else
428 return wxOutRegion;
8d854fa9 429} // end of wxRegion::Contains
0e320a79 430
8d854fa9 431//
0e320a79 432// Does the region contain the rectangle (x, y, w, h)?
8d854fa9 433//
8a16d737 434wxRegionContain wxRegion::DoContainsRect(const wxRect& rect) const
0e320a79 435{
409c9842
DW
436 if (!m_refData)
437 return wxOutRegion;
0e320a79 438
744c5946 439 RECTL vRect;
56617718
WS
440 vRect.xLeft = rect.x;
441 vRect.xRight = rect.x + rect.width;
442 vRect.yTop = rect.y + rect.height;
443 vRect.yBottom = rect.y;
0e320a79 444
744c5946
SN
445 LONG lInside = ::GpiRectInRegion( ((wxRegionRefData*)m_refData)->m_hPS,
446 M_REGION,
8a16d737 447 &vRect
744c5946
SN
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 }
56617718 456
8d854fa9
DW
457} // end of wxRegion::Contains
458
459//
409c9842 460// Get internal region handle
8d854fa9 461//
409c9842
DW
462WXHRGN wxRegion::GetHRGN() const
463{
464 if (!m_refData)
465 return (WXHRGN) 0;
466 return (WXHRGN) M_REGION;
467}
468
44227537
DW
469//
470// Set a new PS, this means we have to recreate the old region in the new
471// PS
472//
473void wxRegion::SetPS(
474 HPS hPS
475)
476{
477 RGNRECT vRgnData;
478 PRECTL pRect = NULL;
479
53e8757b 480 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
44227537
DW
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
0e320a79 515///////////////////////////////////////////////////////////////////////////////
409c9842
DW
516// //
517// wxRegionIterator //
518// //
0e320a79
DW
519///////////////////////////////////////////////////////////////////////////////
520
8d854fa9
DW
521//
522// Initialize empty iterator
523//
524wxRegionIterator::wxRegionIterator()
525: m_lCurrent(0)
526, m_lNumRects(0)
527, m_pRects(NULL)
0e320a79 528{
8d854fa9 529} // end of wxRegionIterator::wxRegionIterator
0e320a79
DW
530
531wxRegionIterator::~wxRegionIterator()
532{
8d854fa9
DW
533 if (m_pRects)
534 delete[] m_pRects;
535} // end of wxRegionIterator::~wxRegionIterator
536
537//
538// Initialize iterator for region
539//
540wxRegionIterator::wxRegionIterator(
541 const wxRegion& rRegion
542)
0e320a79 543{
8d854fa9
DW
544 m_pRects = NULL;
545 Reset(rRegion);
546} // end of wxRegionIterator::wxRegionIterator
547
548//
549// Reset iterator for a new /e region.
550//
551void wxRegionIterator::Reset(
552 const wxRegion& rRegion
553)
0e320a79 554{
8d854fa9 555 m_lCurrent = 0;
0b483d6e 556 m_lNumRects = 0;
8d854fa9 557 m_vRegion = rRegion;
0e320a79 558
8d854fa9
DW
559 if (m_pRects)
560 delete[] m_pRects;
0e320a79 561
8d854fa9 562 m_pRects = NULL;
0e320a79 563
8d854fa9
DW
564 if (m_vRegion.Empty())
565 m_lNumRects = 0;
409c9842 566 else
0e320a79 567 {
8d854fa9
DW
568 RGNRECT vRgnData;
569 PRECTL pRect;
570
53e8757b 571 vRgnData.ulDirection = RECTDIR_LFRT_TOPBOT;
8d854fa9
DW
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 {
53e8757b 591#if 0
8d854fa9
DW
592 M_REGION = ::GpiCreateRegion( ((wxRegionRefData*)rRegion.m_refData)->m_hPS
593 ,vRgnData.crcReturned
594 ,pRect
595 );
53e8757b 596#endif
8d854fa9
DW
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 }
53e8757b 604#if 0
8d854fa9 605 ((wxRegionRefData*)m_refData)->m_hPS = ((wxRegionRefData*)rRegion.m_refData)->m_hPS;
53e8757b 606#endif
8d854fa9
DW
607 }
608 }
0e320a79 609 }
8d854fa9 610} // end of wxRegionIterator::Reset
0e320a79 611
8d854fa9
DW
612//
613// Increment iterator. The rectangle returned is the one after the
614// incrementation.
615//
616void wxRegionIterator::operator++ ()
0e320a79 617{
8d854fa9
DW
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//
626void wxRegionIterator::operator++ (int)
0e320a79 627{
8d854fa9
DW
628 if (m_lCurrent < m_lNumRects)
629 ++m_lCurrent;
630} // end of wxRegionIterator::operator++
0e320a79 631
0b7e7739 632wxCoord wxRegionIterator::GetX() const
0e320a79 633{
8d854fa9
DW
634 if (m_lCurrent < m_lNumRects)
635 return m_pRects[m_lCurrent].x;
636 return 0L;
637} // end of wxRegionIterator::GetX
0e320a79 638
0b7e7739 639wxCoord wxRegionIterator::GetY() const
0e320a79 640{
8d854fa9
DW
641 if (m_lCurrent < m_lNumRects)
642 return m_pRects[m_lCurrent].y;
643 return 0L;
644} // end of wxRegionIterator::GetY
0e320a79 645
0b7e7739 646wxCoord wxRegionIterator::GetW() const
0e320a79 647{
8d854fa9
DW
648 if (m_lCurrent < m_lNumRects)
649 return m_pRects[m_lCurrent].width ;
650 return 0L;
651} // end of wxRegionIterator::GetW
0e320a79 652
0b7e7739 653wxCoord wxRegionIterator::GetH() const
0e320a79 654{
8d854fa9
DW
655 if (m_lCurrent < m_lNumRects)
656 return m_pRects[m_lCurrent].height;
657 return 0L;
658} // end of wxRegionIterator::GetH