]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dc.cpp
call RemoveChild() in ~wxWindowBase instead of doing it in each ~wxWindow for each...
[wxWidgets.git] / src / mac / carbon / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dc.cpp
3 // Purpose: wxDC class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dc.h"
14 #endif
15
16 #include "wx/dc.h"
17 #include "wx/app.h"
18 #include "wx/mac/uma.h"
19 #include "wx/dcmemory.h"
20 #include "wx/dcprint.h"
21 #include "wx/region.h"
22 #include "wx/image.h"
23 #include "wx/log.h"
24
25 #if __MSL__ >= 0x6000
26 #include "math.h"
27 using namespace std ;
28 #endif
29
30 #include "wx/mac/private.h"
31 #include "ATSUnicode.h"
32 #include "TextCommon.h"
33 #include "TextEncodingConverter.h"
34 #include "FixMath.h"
35 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
37 #endif
38
39 //-----------------------------------------------------------------------------
40 // constants
41 //-----------------------------------------------------------------------------
42
43 #define mm2inches 0.0393700787402
44 #define inches2mm 25.4
45 #define mm2twips 56.6929133859
46 #define twips2mm 0.0176388888889
47 #define mm2pt 2.83464566929
48 #define pt2mm 0.352777777778
49 #if !defined( __DARWIN__ ) || defined(__MWERKS__)
50 #ifndef M_PI
51 const double M_PI = 3.14159265358979 ;
52 #endif
53 #endif
54 const double RAD2DEG = 180.0 / M_PI;
55 const short kEmulatedMode = -1 ;
56 const short kUnsupportedMode = -2 ;
57
58 extern TECObjectRef s_TECNativeCToUnicode ;
59
60 // set to 0 if problems arise
61 #define wxMAC_EXPERIMENTAL_DC 1
62
63 wxMacPortSetter::wxMacPortSetter( const wxDC* dc ) :
64 m_ph( (GrafPtr) dc->m_macPort )
65 {
66 wxASSERT( dc->Ok() ) ;
67 m_dc = dc ;
68 dc->MacSetupPort(&m_ph) ;
69 }
70 wxMacPortSetter::~wxMacPortSetter()
71 {
72 m_dc->MacCleanupPort(&m_ph) ;
73 }
74
75 #if wxMAC_EXPERIMENTAL_DC
76 class wxMacFastPortSetter
77 {
78 public :
79 wxMacFastPortSetter( const wxDC *dc )
80 {
81 wxASSERT( dc->Ok() ) ;
82 GetPort( &m_oldPort ) ;
83 SetPort( (GrafPtr) dc->m_macPort ) ;
84 m_dc = dc ;
85 dc->MacSetupPort( NULL ) ;
86 }
87 ~wxMacFastPortSetter()
88 {
89 SetPort( m_oldPort ) ;
90 m_dc->MacCleanupPort( NULL ) ;
91 }
92 private :
93 GrafPtr m_oldPort ;
94 const wxDC* m_dc ;
95 } ;
96
97 #else
98 typedef wxMacPortSetter wxMacFastPortSetter ;
99 #endif
100
101 wxMacWindowClipper::wxMacWindowClipper( const wxWindow* win )
102 {
103 m_formerClip = NewRgn() ;
104 m_newClip = NewRgn() ;
105 GetClip( m_formerClip ) ;
106
107 if ( win )
108 {
109 #if 0
110 // this clipping area was set to the parent window's drawing area, lead to problems
111 // with MacOSX controls drawing outside their wx' rectangle
112 RgnHandle insidergn = NewRgn() ;
113 int x = 0 , y = 0;
114 wxWindow *parent = win->GetParent() ;
115 parent->MacWindowToRootWindow( &x,&y ) ;
116 wxSize size = parent->GetSize() ;
117 SetRectRgn( insidergn , parent->MacGetLeftBorderSize() , parent->MacGetTopBorderSize() ,
118 size.x - parent->MacGetRightBorderSize(),
119 size.y - parent->MacGetBottomBorderSize()) ;
120 CopyRgn( (RgnHandle) parent->MacGetVisibleRegion(false).GetWXHRGN() , m_newClip ) ;
121 SectRgn( m_newClip , insidergn , m_newClip ) ;
122 OffsetRgn( m_newClip , x , y ) ;
123 SetClip( m_newClip ) ;
124 DisposeRgn( insidergn ) ;
125 #else
126 int x = 0 , y = 0;
127 win->MacWindowToRootWindow( &x,&y ) ;
128 CopyRgn( (RgnHandle) ((wxWindow*)win)->MacGetVisibleRegion().GetWXHRGN() , m_newClip ) ;
129 OffsetRgn( m_newClip , x , y ) ;
130 SetClip( m_newClip ) ;
131 #endif
132 }
133 }
134
135 wxMacWindowClipper::~wxMacWindowClipper()
136 {
137 SetClip( m_formerClip ) ;
138 DisposeRgn( m_newClip ) ;
139 DisposeRgn( m_formerClip ) ;
140 }
141
142 //-----------------------------------------------------------------------------
143 // Local functions
144 //-----------------------------------------------------------------------------
145 static inline double dmin(double a, double b) { return a < b ? a : b; }
146 static inline double dmax(double a, double b) { return a > b ? a : b; }
147 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
148
149 //-----------------------------------------------------------------------------
150 // wxDC
151 //-----------------------------------------------------------------------------
152 // this function emulates all wx colour manipulations, used to verify the implementation
153 // by setting the mode in the blitting functions to kEmulatedMode
154 void wxMacCalculateColour( int logical_func , const RGBColor &srcColor , RGBColor &dstColor ) ;
155
156 void wxMacCalculateColour( int logical_func , const RGBColor &srcColor , RGBColor &dstColor )
157 {
158 switch ( logical_func )
159 {
160 case wxAND: // src AND dst
161 dstColor.red = dstColor.red & srcColor.red ;
162 dstColor.green = dstColor.green & srcColor.green ;
163 dstColor.blue = dstColor.blue & srcColor.blue ;
164 break ;
165 case wxAND_INVERT: // (NOT src) AND dst
166 dstColor.red = dstColor.red & ~srcColor.red ;
167 dstColor.green = dstColor.green & ~srcColor.green ;
168 dstColor.blue = dstColor.blue & ~srcColor.blue ;
169 break ;
170 case wxAND_REVERSE:// src AND (NOT dst)
171 dstColor.red = ~dstColor.red & srcColor.red ;
172 dstColor.green = ~dstColor.green & srcColor.green ;
173 dstColor.blue = ~dstColor.blue & srcColor.blue ;
174 break ;
175 case wxCLEAR: // 0
176 dstColor.red = 0 ;
177 dstColor.green = 0 ;
178 dstColor.blue = 0 ;
179 break ;
180 case wxCOPY: // src
181 dstColor.red = srcColor.red ;
182 dstColor.green = srcColor.green ;
183 dstColor.blue = srcColor.blue ;
184 break ;
185 case wxEQUIV: // (NOT src) XOR dst
186 dstColor.red = dstColor.red ^ ~srcColor.red ;
187 dstColor.green = dstColor.green ^ ~srcColor.green ;
188 dstColor.blue = dstColor.blue ^ ~srcColor.blue ;
189 break ;
190 case wxINVERT: // NOT dst
191 dstColor.red = ~dstColor.red ;
192 dstColor.green = ~dstColor.green ;
193 dstColor.blue = ~dstColor.blue ;
194 break ;
195 case wxNAND: // (NOT src) OR (NOT dst)
196 dstColor.red = ~dstColor.red | ~srcColor.red ;
197 dstColor.green = ~dstColor.green | ~srcColor.green ;
198 dstColor.blue = ~dstColor.blue | ~srcColor.blue ;
199 break ;
200 case wxNOR: // (NOT src) AND (NOT dst)
201 dstColor.red = ~dstColor.red & ~srcColor.red ;
202 dstColor.green = ~dstColor.green & ~srcColor.green ;
203 dstColor.blue = ~dstColor.blue & ~srcColor.blue ;
204 break ;
205 case wxNO_OP: // dst
206 break ;
207 case wxOR: // src OR dst
208 dstColor.red = dstColor.red | srcColor.red ;
209 dstColor.green = dstColor.green | srcColor.green ;
210 dstColor.blue = dstColor.blue | srcColor.blue ;
211 break ;
212 case wxOR_INVERT: // (NOT src) OR dst
213 dstColor.red = dstColor.red | ~srcColor.red ;
214 dstColor.green = dstColor.green | ~srcColor.green ;
215 dstColor.blue = dstColor.blue | ~srcColor.blue ;
216 break ;
217 case wxOR_REVERSE: // src OR (NOT dst)
218 dstColor.red = ~dstColor.red | srcColor.red ;
219 dstColor.green = ~dstColor.green | srcColor.green ;
220 dstColor.blue = ~dstColor.blue | srcColor.blue ;
221 break ;
222 case wxSET: // 1
223 dstColor.red = 0xFFFF ;
224 dstColor.green = 0xFFFF ;
225 dstColor.blue = 0xFFFF ;
226 break ;
227 case wxSRC_INVERT: // (NOT src)
228 dstColor.red = ~srcColor.red ;
229 dstColor.green = ~srcColor.green ;
230 dstColor.blue = ~srcColor.blue ;
231 break ;
232 case wxXOR: // src XOR dst
233 dstColor.red = dstColor.red ^ srcColor.red ;
234 dstColor.green = dstColor.green ^ srcColor.green ;
235 dstColor.blue = dstColor.blue ^ srcColor.blue ;
236 break ;
237 }
238 }
239
240 wxDC::wxDC()
241 {
242 m_ok = FALSE;
243 m_colour = TRUE;
244 m_mm_to_pix_x = mm2pt;
245 m_mm_to_pix_y = mm2pt;
246 m_internalDeviceOriginX = 0;
247 m_internalDeviceOriginY = 0;
248 m_externalDeviceOriginX = 0;
249 m_externalDeviceOriginY = 0;
250 m_logicalScaleX = 1.0;
251 m_logicalScaleY = 1.0;
252 m_userScaleX = 1.0;
253 m_userScaleY = 1.0;
254 m_scaleX = 1.0;
255 m_scaleY = 1.0;
256 m_needComputeScaleX = FALSE;
257 m_needComputeScaleY = FALSE;
258 m_macPort = NULL ;
259 m_macMask = NULL ;
260 m_ok = FALSE ;
261 m_macFontInstalled = false ;
262 m_macBrushInstalled = false ;
263 m_macPenInstalled = false ;
264 m_macLocalOrigin.x = m_macLocalOrigin.y = 0 ;
265 m_macBoundaryClipRgn = NewRgn() ;
266 m_macCurrentClipRgn = NewRgn() ;
267 SetRectRgn( (RgnHandle) m_macBoundaryClipRgn , -32000 , -32000 , 32000 , 32000 ) ;
268 SetRectRgn( (RgnHandle) m_macCurrentClipRgn , -32000 , -32000 , 32000 , 32000 ) ;
269 m_pen = *wxBLACK_PEN;
270 m_font = *wxNORMAL_FONT;
271 m_brush = *wxWHITE_BRUSH;
272 #ifdef __WXDEBUG__
273 // needed to debug possible errors with two active drawing methods at the same time on
274 // the same DC
275 m_macCurrentPortStateHelper = NULL ;
276 #endif
277 m_macATSUIStyle = NULL ;
278 m_macAliasWasEnabled = false;
279 m_macForegroundPixMap = NULL ;
280 m_macBackgroundPixMap = NULL ;
281 }
282
283 wxDC::~wxDC(void)
284 {
285 DisposeRgn( (RgnHandle) m_macBoundaryClipRgn ) ;
286 DisposeRgn( (RgnHandle) m_macCurrentClipRgn ) ;
287 }
288
289 void wxDC::MacSetupPort(wxMacPortStateHelper* help) const
290 {
291 #ifdef __WXDEBUG__
292 wxASSERT( m_macCurrentPortStateHelper == NULL ) ;
293 m_macCurrentPortStateHelper = help ;
294 #endif
295 SetClip( (RgnHandle) m_macCurrentClipRgn);
296 #if ! wxMAC_EXPERIMENTAL_DC
297 m_macFontInstalled = false ;
298 m_macBrushInstalled = false ;
299 m_macPenInstalled = false ;
300 #endif
301 }
302 void wxDC::MacCleanupPort(wxMacPortStateHelper* help) const
303 {
304 #ifdef __WXDEBUG__
305 wxASSERT( m_macCurrentPortStateHelper == help ) ;
306 m_macCurrentPortStateHelper = NULL ;
307 #endif
308 if( m_macATSUIStyle )
309 {
310 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
311 m_macATSUIStyle = NULL ;
312 }
313 if ( m_macAliasWasEnabled )
314 {
315 SetAntiAliasedTextEnabled(m_macFormerAliasState, m_macFormerAliasSize);
316 m_macAliasWasEnabled = false ;
317 }
318 if ( m_macForegroundPixMap )
319 {
320 Pattern blackColor ;
321 ::PenPat(GetQDGlobalsBlack(&blackColor));
322 DisposePixPat( (PixPatHandle) m_macForegroundPixMap ) ;
323 m_macForegroundPixMap = NULL ;
324 }
325 if ( m_macBackgroundPixMap )
326 {
327 Pattern whiteColor ;
328 ::BackPat(GetQDGlobalsWhite(&whiteColor));
329 DisposePixPat( (PixPatHandle) m_macBackgroundPixMap ) ;
330 m_macBackgroundPixMap = NULL ;
331 }
332 }
333
334 void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
335 {
336 wxCHECK_RET( Ok(), wxT("invalid window dc") );
337 wxCHECK_RET( bmp.Ok(), wxT("invalid bitmap") );
338 wxMacFastPortSetter helper(this) ;
339 wxCoord xx = XLOG2DEVMAC(x);
340 wxCoord yy = YLOG2DEVMAC(y);
341 wxCoord w = bmp.GetWidth();
342 wxCoord h = bmp.GetHeight();
343 wxCoord ww = XLOG2DEVREL(w);
344 wxCoord hh = YLOG2DEVREL(h);
345 // Set up drawing mode
346 short mode = (m_logicalFunction == wxCOPY ? srcCopy :
347 //m_logicalFunction == wxCLEAR ? WHITENESS :
348 //m_logicalFunction == wxSET ? BLACKNESS :
349 m_logicalFunction == wxINVERT ? hilite :
350 //m_logicalFunction == wxAND ? MERGECOPY :
351 m_logicalFunction == wxOR ? srcOr :
352 m_logicalFunction == wxSRC_INVERT ? notSrcCopy :
353 m_logicalFunction == wxXOR ? srcXor :
354 m_logicalFunction == wxOR_REVERSE ? notSrcOr :
355 //m_logicalFunction == wxAND_REVERSE ? SRCERASE :
356 //m_logicalFunction == wxSRC_OR ? srcOr :
357 //m_logicalFunction == wxSRC_AND ? SRCAND :
358 srcCopy );
359 if ( bmp.GetBitmapType() == kMacBitmapTypePict ) {
360 Rect bitmaprect = { 0 , 0 , hh, ww };
361 ::OffsetRect( &bitmaprect, xx, yy ) ;
362 ::DrawPicture( (PicHandle) bmp.GetPict(), &bitmaprect ) ;
363 }
364 else if ( bmp.GetBitmapType() == kMacBitmapTypeGrafWorld )
365 {
366 GWorldPtr bmapworld = MAC_WXHBITMAP( bmp.GetHBITMAP() );
367 PixMapHandle bmappixels ;
368 // Set foreground and background colours (for bitmaps depth = 1)
369 if(bmp.GetDepth() == 1)
370 {
371 RGBColor fore = MAC_WXCOLORREF(m_textForegroundColour.GetPixel());
372 RGBColor back = MAC_WXCOLORREF(m_textBackgroundColour.GetPixel());
373 RGBForeColor(&fore);
374 RGBBackColor(&back);
375 }
376 else
377 {
378 RGBColor white = { 0xFFFF, 0xFFFF,0xFFFF} ;
379 RGBColor black = { 0,0,0} ;
380 RGBForeColor( &black ) ;
381 RGBBackColor( &white ) ;
382 }
383 bmappixels = GetGWorldPixMap( bmapworld ) ;
384 wxCHECK_RET(LockPixels(bmappixels),
385 wxT("DoDrawBitmap: Unable to lock pixels"));
386 Rect source = { 0, 0, h, w };
387 Rect dest = { yy, xx, yy + hh, xx + ww };
388 if ( useMask && bmp.GetMask() )
389 {
390 if( LockPixels(GetGWorldPixMap(MAC_WXHBITMAP(bmp.GetMask()->GetMaskBitmap()))))
391 {
392 CopyDeepMask
393 (
394 GetPortBitMapForCopyBits(bmapworld),
395 GetPortBitMapForCopyBits(MAC_WXHBITMAP(bmp.GetMask()->GetMaskBitmap())),
396 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ),
397 &source, &source, &dest, mode, NULL
398 );
399 UnlockPixels(GetGWorldPixMap(MAC_WXHBITMAP(bmp.GetMask()->GetMaskBitmap())));
400 }
401 }
402 else {
403 CopyBits( GetPortBitMapForCopyBits( bmapworld ),
404 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ),
405 &source, &dest, mode, NULL ) ;
406 }
407 UnlockPixels( bmappixels ) ;
408 }
409 else if ( bmp.GetBitmapType() == kMacBitmapTypeIcon )
410 {
411 Rect bitmaprect = { 0 , 0 , bmp.GetHeight(), bmp.GetWidth() } ;
412 OffsetRect( &bitmaprect, xx, yy ) ;
413 PlotCIconHandle( &bitmaprect , atNone , ttNone , MAC_WXHICON(bmp.GetHICON()) ) ;
414 }
415 m_macPenInstalled = false ;
416 m_macBrushInstalled = false ;
417 m_macFontInstalled = false ;
418 }
419
420 void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
421 {
422 wxCHECK_RET(Ok(), wxT("Invalid dc wxDC::DoDrawIcon"));
423 wxCHECK_RET(icon.Ok(), wxT("Invalid icon wxDC::DoDrawIcon"));
424 DoDrawBitmap( icon , x , y , icon.GetMask() != NULL ) ;
425 }
426
427 void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
428 {
429 wxCHECK_RET(Ok(), wxT("wxDC::DoSetClippingRegion Invalid DC"));
430 wxCoord xx, yy, ww, hh;
431 xx = XLOG2DEVMAC(x);
432 yy = YLOG2DEVMAC(y);
433 ww = XLOG2DEVREL(width);
434 hh = YLOG2DEVREL(height);
435 SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
436 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
437 if( m_clipping )
438 {
439 m_clipX1 = wxMax( m_clipX1 , xx );
440 m_clipY1 = wxMax( m_clipY1 , yy );
441 m_clipX2 = wxMin( m_clipX2, (xx + ww));
442 m_clipY2 = wxMin( m_clipY2, (yy + hh));
443 }
444 else
445 {
446 m_clipping = TRUE;
447 m_clipX1 = xx;
448 m_clipY1 = yy;
449 m_clipX2 = xx + ww;
450 m_clipY2 = yy + hh;
451 }
452 }
453
454 void wxDC::DoSetClippingRegionAsRegion( const wxRegion &region )
455 {
456 wxCHECK_RET( Ok(), wxT("invalid window dc") ) ;
457 if (region.Empty())
458 {
459 DestroyClippingRegion();
460 return;
461 }
462 wxMacFastPortSetter helper(this) ;
463 wxCoord x, y, w, h;
464 region.GetBox( x, y, w, h );
465 wxCoord xx, yy, ww, hh;
466 xx = XLOG2DEVMAC(x);
467 yy = YLOG2DEVMAC(y);
468 ww = XLOG2DEVREL(w);
469 hh = YLOG2DEVREL(h);
470 // if we have a scaling that we cannot map onto native regions
471 // we must use the box
472 if ( ww != w || hh != h )
473 {
474 wxDC::DoSetClippingRegion( x, y, w, h );
475 }
476 else
477 {
478 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
479 if ( xx != x || yy != y )
480 {
481 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
482 }
483 SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
484 if( m_clipping )
485 {
486 m_clipX1 = wxMax( m_clipX1 , xx );
487 m_clipY1 = wxMax( m_clipY1 , yy );
488 m_clipX2 = wxMin( m_clipX2, (xx + ww));
489 m_clipY2 = wxMin( m_clipY2, (yy + hh));
490 }
491 else
492 {
493 m_clipping = TRUE;
494 m_clipX1 = xx;
495 m_clipY1 = yy;
496 m_clipX2 = xx + ww;
497 m_clipY2 = yy + hh;
498 }
499 }
500 }
501
502 void wxDC::DestroyClippingRegion()
503 {
504 wxMacFastPortSetter helper(this) ;
505 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
506 m_clipping = FALSE;
507 }
508
509 void wxDC::DoGetSizeMM( int* width, int* height ) const
510 {
511 int w = 0;
512 int h = 0;
513 GetSize( &w, &h );
514 *width = long( double(w) / (m_scaleX*m_mm_to_pix_x) );
515 *height = long( double(h) / (m_scaleY*m_mm_to_pix_y) );
516 }
517
518 void wxDC::SetTextForeground( const wxColour &col )
519 {
520 wxCHECK_RET(Ok(), wxT("Invalid DC"));
521 m_textForegroundColour = col;
522 m_macFontInstalled = false ;
523 }
524
525 void wxDC::SetTextBackground( const wxColour &col )
526 {
527 wxCHECK_RET(Ok(), wxT("Invalid DC"));
528 m_textBackgroundColour = col;
529 m_macFontInstalled = false ;
530 }
531
532 void wxDC::SetMapMode( int mode )
533 {
534 switch (mode)
535 {
536 case wxMM_TWIPS:
537 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
538 break;
539 case wxMM_POINTS:
540 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
541 break;
542 case wxMM_METRIC:
543 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
544 break;
545 case wxMM_LOMETRIC:
546 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
547 break;
548 default:
549 case wxMM_TEXT:
550 SetLogicalScale( 1.0, 1.0 );
551 break;
552 }
553 if (mode != wxMM_TEXT)
554 {
555 m_needComputeScaleX = TRUE;
556 m_needComputeScaleY = TRUE;
557 }
558 }
559
560 void wxDC::SetUserScale( double x, double y )
561 {
562 // allow negative ? -> no
563 m_userScaleX = x;
564 m_userScaleY = y;
565 ComputeScaleAndOrigin();
566 }
567
568 void wxDC::SetLogicalScale( double x, double y )
569 {
570 // allow negative ?
571 m_logicalScaleX = x;
572 m_logicalScaleY = y;
573 ComputeScaleAndOrigin();
574 }
575
576 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
577 {
578 m_logicalOriginX = x * m_signX; // is this still correct ?
579 m_logicalOriginY = y * m_signY;
580 ComputeScaleAndOrigin();
581 }
582
583 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
584 {
585 m_externalDeviceOriginX = x;
586 m_externalDeviceOriginY = y;
587 ComputeScaleAndOrigin();
588 }
589
590 #if 0
591 void wxDC::SetInternalDeviceOrigin( long x, long y )
592 {
593 m_internalDeviceOriginX = x;
594 m_internalDeviceOriginY = y;
595 ComputeScaleAndOrigin();
596 }
597 void wxDC::GetInternalDeviceOrigin( long *x, long *y )
598 {
599 if (x) *x = m_internalDeviceOriginX;
600 if (y) *y = m_internalDeviceOriginY;
601 }
602 #endif
603
604 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
605 {
606 m_signX = (xLeftRight ? 1 : -1);
607 m_signY = (yBottomUp ? -1 : 1);
608 ComputeScaleAndOrigin();
609 }
610
611 wxSize wxDC::GetPPI() const
612 {
613 return wxSize(72, 72);
614 }
615
616 int wxDC::GetDepth() const
617 {
618 if ( IsPortColor( (CGrafPtr) m_macPort ) )
619 {
620 return ( (**GetPortPixMap( (CGrafPtr) m_macPort)).pixelSize ) ;
621 }
622 return 1 ;
623 }
624
625 void wxDC::ComputeScaleAndOrigin()
626 {
627 // CMB: copy scale to see if it changes
628 double origScaleX = m_scaleX;
629 double origScaleY = m_scaleY;
630 m_scaleX = m_logicalScaleX * m_userScaleX;
631 m_scaleY = m_logicalScaleY * m_userScaleY;
632 m_deviceOriginX = m_internalDeviceOriginX + m_externalDeviceOriginX;
633 m_deviceOriginY = m_internalDeviceOriginY + m_externalDeviceOriginY;
634 // CMB: if scale has changed call SetPen to recalulate the line width
635 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
636 {
637 // this is a bit artificial, but we need to force wxDC to think
638 // the pen has changed
639 wxPen* pen = & GetPen();
640 wxPen tempPen;
641 m_pen = tempPen;
642 SetPen(* pen);
643 }
644 }
645
646 void wxDC::SetPalette( const wxPalette& palette )
647 {
648 }
649
650 void wxDC::SetBackgroundMode( int mode )
651 {
652 m_backgroundMode = mode ;
653 }
654
655 void wxDC::SetFont( const wxFont &font )
656 {
657 m_font = font;
658 m_macFontInstalled = false ;
659 }
660
661 void wxDC::SetPen( const wxPen &pen )
662 {
663 if ( m_pen == pen )
664 return ;
665 m_pen = pen;
666 m_macPenInstalled = false ;
667 }
668
669 void wxDC::SetBrush( const wxBrush &brush )
670 {
671 if (m_brush == brush)
672 return;
673 m_brush = brush;
674 m_macBrushInstalled = false ;
675 }
676
677 void wxDC::SetBackground( const wxBrush &brush )
678 {
679 if (m_backgroundBrush == brush)
680 return;
681 m_backgroundBrush = brush;
682 if (!m_backgroundBrush.Ok())
683 return;
684 m_macBrushInstalled = false ;
685 }
686
687 void wxDC::SetLogicalFunction( int function )
688 {
689 if (m_logicalFunction == function)
690 return;
691 m_logicalFunction = function ;
692 m_macFontInstalled = false ;
693 m_macBrushInstalled = false ;
694 m_macPenInstalled = false ;
695 }
696
697 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
698 const wxColour & col, int style);
699
700 bool wxDC::DoFloodFill(wxCoord x, wxCoord y,
701 const wxColour& col, int style)
702 {
703 return wxDoFloodFill(this, x, y, col, style);
704 }
705
706 bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
707 {
708 wxCHECK_MSG( Ok(), false, wxT("wxDC::DoGetPixel Invalid DC") );
709 wxMacFastPortSetter helper(this) ;
710 RGBColor colour;
711 GetCPixel( XLOG2DEVMAC(x), YLOG2DEVMAC(y), &colour );
712 // Convert from Mac colour to wx
713 col->Set( colour.red >> 8,
714 colour.green >> 8,
715 colour.blue >> 8);
716 return true ;
717 }
718
719 void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
720 {
721 wxCHECK_RET(Ok(), wxT("Invalid DC"));
722 wxMacFastPortSetter helper(this) ;
723 if (m_pen.GetStyle() != wxTRANSPARENT)
724 {
725 MacInstallPen() ;
726 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
727 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2;
728 wxCoord xx1 = XLOG2DEVMAC(x1) - offset;
729 wxCoord yy1 = YLOG2DEVMAC(y1) - offset;
730 wxCoord xx2 = XLOG2DEVMAC(x2) - offset;
731 wxCoord yy2 = YLOG2DEVMAC(y2) - offset;
732 if ((m_pen.GetCap() == wxCAP_ROUND) &&
733 (m_pen.GetWidth() <= 1))
734 {
735 // Implement LAST_NOT for MAC at least for
736 // orthogonal lines. RR.
737 if (xx1 == xx2)
738 {
739 if (yy1 < yy2)
740 yy2--;
741 if (yy1 > yy2)
742 yy2++;
743 }
744 if (yy1 == yy2)
745 {
746 if (xx1 < xx2)
747 xx2--;
748 if (xx1 > xx2)
749 xx2++;
750 }
751 }
752 ::MoveTo(xx1, yy1);
753 ::LineTo(xx2, yy2);
754 }
755 }
756
757 void wxDC::DoCrossHair( wxCoord x, wxCoord y )
758 {
759 wxCHECK_RET( Ok(), wxT("wxDC::DoCrossHair Invalid window dc") );
760 wxMacFastPortSetter helper(this) ;
761 if (m_pen.GetStyle() != wxTRANSPARENT)
762 {
763 int w = 0;
764 int h = 0;
765 GetSize( &w, &h );
766 wxCoord xx = XLOG2DEVMAC(x);
767 wxCoord yy = YLOG2DEVMAC(y);
768 MacInstallPen();
769 ::MoveTo( XLOG2DEVMAC(0), yy );
770 ::LineTo( XLOG2DEVMAC(w), yy );
771 ::MoveTo( xx, YLOG2DEVMAC(0) );
772 ::LineTo( xx, YLOG2DEVMAC(h) );
773 CalcBoundingBox(x, y);
774 CalcBoundingBox(x+w, y+h);
775 }
776 }
777
778 /*
779 * To draw arcs properly the angles need to be converted from the WX style:
780 * Angles start on the +ve X axis and go anti-clockwise (As you would draw on
781 * a normal axis on paper).
782 * TO
783 * the Mac style:
784 * Angles start on the +ve y axis and go clockwise.
785 */
786
787 static double wxConvertWXangleToMACangle(double angle)
788 {
789 double newAngle = 90 - angle ;
790 if ( newAngle < 0 )
791 newAngle += 360 ;
792 return newAngle ;
793 }
794
795 void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
796 wxCoord x2, wxCoord y2,
797 wxCoord xc, wxCoord yc )
798 {
799 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawArc Invalid DC"));
800 wxMacFastPortSetter helper(this) ;
801 wxCoord xx1 = XLOG2DEVMAC(x1);
802 wxCoord yy1 = YLOG2DEVMAC(y1);
803 wxCoord xx2 = XLOG2DEVMAC(x2);
804 wxCoord yy2 = YLOG2DEVMAC(y2);
805 wxCoord xxc = XLOG2DEVMAC(xc);
806 wxCoord yyc = YLOG2DEVMAC(yc);
807 double dx = xx1 - xxc;
808 double dy = yy1 - yyc;
809 double radius = sqrt((double)(dx*dx+dy*dy));
810 wxCoord rad = (wxCoord)radius;
811 double radius1, radius2;
812 if (xx1 == xx2 && yy1 == yy2)
813 {
814 radius1 = 0.0;
815 radius2 = 360.0;
816 }
817 else if (radius == 0.0)
818 {
819 radius1 = radius2 = 0.0;
820 }
821 else
822 {
823 radius1 = (xx1 - xxc == 0) ?
824 (yy1 - yyc < 0) ? 90.0 : -90.0 :
825 -atan2(double(yy1-yyc), double(xx1-xxc)) * RAD2DEG;
826 radius2 = (xx2 - xxc == 0) ?
827 (yy2 - yyc < 0) ? 90.0 : -90.0 :
828 -atan2(double(yy2-yyc), double(xx2-xxc)) * RAD2DEG;
829 }
830 wxCoord alpha2 = wxCoord(radius2 - radius1);
831 wxCoord alpha1 = wxCoord(wxConvertWXangleToMACangle(radius1));
832 if( (xx1 > xx2) || (yy1 > yy2) ) {
833 alpha2 *= -1;
834 }
835 Rect r = { yyc - rad, xxc - rad, yyc + rad, xxc + rad };
836 if(m_brush.GetStyle() != wxTRANSPARENT) {
837 MacInstallBrush();
838 PaintArc(&r, alpha1, alpha2);
839 }
840 if(m_pen.GetStyle() != wxTRANSPARENT) {
841 MacInstallPen();
842 FrameArc(&r, alpha1, alpha2);
843 }
844 }
845
846 void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
847 double sa, double ea )
848 {
849 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawEllepticArc Invalid DC"));
850 wxMacFastPortSetter helper(this) ;
851 Rect r;
852 double angle = sa - ea; // Order important Mac in opposite direction to wx
853 // we have to make sure that the filling is always counter-clockwise
854 if ( angle > 0 )
855 angle -= 360 ;
856 wxCoord xx = XLOG2DEVMAC(x);
857 wxCoord yy = YLOG2DEVMAC(y);
858 wxCoord ww = m_signX * XLOG2DEVREL(w);
859 wxCoord hh = m_signY * YLOG2DEVREL(h);
860 // handle -ve width and/or height
861 if (ww < 0) { ww = -ww; xx = xx - ww; }
862 if (hh < 0) { hh = -hh; yy = yy - hh; }
863 sa = wxConvertWXangleToMACangle(sa);
864 r.top = yy;
865 r.left = xx;
866 r.bottom = yy + hh;
867 r.right = xx + ww;
868 if(m_brush.GetStyle() != wxTRANSPARENT) {
869 MacInstallBrush();
870 PaintArc(&r, (short)sa, (short)angle);
871 }
872 if(m_pen.GetStyle() != wxTRANSPARENT) {
873 MacInstallPen();
874 FrameArc(&r, (short)sa, (short)angle);
875 }
876 }
877
878 void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
879 {
880 wxCHECK_RET(Ok(), wxT("Invalid DC"));
881 wxMacFastPortSetter helper(this) ;
882 if (m_pen.GetStyle() != wxTRANSPARENT)
883 {
884 wxCoord xx1 = XLOG2DEVMAC(x);
885 wxCoord yy1 = YLOG2DEVMAC(y);
886 RGBColor pencolor = MAC_WXCOLORREF( m_pen.GetColour().GetPixel());
887 ::SetCPixel( xx1,yy1,&pencolor) ;
888 CalcBoundingBox(x, y);
889 }
890 }
891
892 void wxDC::DoDrawLines(int n, wxPoint points[],
893 wxCoord xoffset, wxCoord yoffset)
894 {
895 wxCHECK_RET(Ok(), wxT("Invalid DC"));
896 wxMacFastPortSetter helper(this) ;
897 if (m_pen.GetStyle() == wxTRANSPARENT)
898 return;
899 MacInstallPen() ;
900 wxCoord offset = ( (m_pen.GetWidth() == 0 ? 1 :
901 m_pen.GetWidth() ) * (wxCoord)m_scaleX - 1) / 2 ;
902 wxCoord x1, x2 , y1 , y2 ;
903 x1 = XLOG2DEVMAC(points[0].x + xoffset);
904 y1 = YLOG2DEVMAC(points[0].y + yoffset);
905 ::MoveTo(x1 - offset, y1 - offset );
906 for (int i = 0; i < n-1; i++)
907 {
908 x2 = XLOG2DEVMAC(points[i+1].x + xoffset);
909 y2 = YLOG2DEVMAC(points[i+1].y + yoffset);
910 ::LineTo( x2 - offset, y2 - offset );
911 }
912 }
913
914 void wxDC::DoDrawPolygon(int n, wxPoint points[],
915 wxCoord xoffset, wxCoord yoffset,
916 int fillStyle )
917 {
918 wxCHECK_RET(Ok(), wxT("Invalid DC"));
919 wxMacFastPortSetter helper(this) ;
920 wxCoord x1, x2 , y1 , y2 ;
921 if ( m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT )
922 return ;
923 PolyHandle polygon = OpenPoly();
924 x2 = x1 = XLOG2DEVMAC(points[0].x + xoffset);
925 y2 = y1 = YLOG2DEVMAC(points[0].y + yoffset);
926 ::MoveTo(x1,y1);
927 for (int i = 1; i < n; i++)
928 {
929 x2 = XLOG2DEVMAC(points[i].x + xoffset);
930 y2 = YLOG2DEVMAC(points[i].y + yoffset);
931 ::LineTo(x2, y2);
932 }
933 // close the polyline if necessary
934 if ( x1 != x2 || y1 != y2 )
935 {
936 ::LineTo(x1,y1 ) ;
937 }
938 ClosePoly();
939 if (m_brush.GetStyle() != wxTRANSPARENT)
940 {
941 MacInstallBrush();
942 ::PaintPoly( polygon );
943 }
944 if (m_pen.GetStyle() != wxTRANSPARENT)
945 {
946 MacInstallPen() ;
947 ::FramePoly( polygon ) ;
948 }
949 KillPoly( polygon );
950 }
951
952 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
953 {
954 wxCHECK_RET(Ok(), wxT("Invalid DC"));
955 wxMacFastPortSetter helper(this) ;
956 wxCoord xx = XLOG2DEVMAC(x);
957 wxCoord yy = YLOG2DEVMAC(y);
958 wxCoord ww = m_signX * XLOG2DEVREL(width);
959 wxCoord hh = m_signY * YLOG2DEVREL(height);
960 // CMB: draw nothing if transformed w or h is 0
961 if (ww == 0 || hh == 0)
962 return;
963 // CMB: handle -ve width and/or height
964 if (ww < 0)
965 {
966 ww = -ww;
967 xx = xx - ww;
968 }
969 if (hh < 0)
970 {
971 hh = -hh;
972 yy = yy - hh;
973 }
974 Rect rect = { yy , xx , yy + hh , xx + ww } ;
975 if (m_brush.GetStyle() != wxTRANSPARENT)
976 {
977 MacInstallBrush() ;
978 ::PaintRect( &rect ) ;
979 }
980 if (m_pen.GetStyle() != wxTRANSPARENT)
981 {
982 MacInstallPen() ;
983 ::FrameRect( &rect ) ;
984 }
985 }
986
987 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
988 wxCoord width, wxCoord height,
989 double radius)
990 {
991 wxCHECK_RET(Ok(), wxT("Invalid DC"));
992 wxMacFastPortSetter helper(this) ;
993 if (radius < 0.0)
994 radius = - radius * ((width < height) ? width : height);
995 wxCoord xx = XLOG2DEVMAC(x);
996 wxCoord yy = YLOG2DEVMAC(y);
997 wxCoord ww = m_signX * XLOG2DEVREL(width);
998 wxCoord hh = m_signY * YLOG2DEVREL(height);
999 // CMB: draw nothing if transformed w or h is 0
1000 if (ww == 0 || hh == 0)
1001 return;
1002 // CMB: handle -ve width and/or height
1003 if (ww < 0)
1004 {
1005 ww = -ww;
1006 xx = xx - ww;
1007 }
1008 if (hh < 0)
1009 {
1010 hh = -hh;
1011 yy = yy - hh;
1012 }
1013 Rect rect = { yy , xx , yy + hh , xx + ww } ;
1014 if (m_brush.GetStyle() != wxTRANSPARENT)
1015 {
1016 MacInstallBrush() ;
1017 ::PaintRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
1018 }
1019 if (m_pen.GetStyle() != wxTRANSPARENT)
1020 {
1021 MacInstallPen() ;
1022 ::FrameRoundRect( &rect , int(radius * 2) , int(radius * 2) ) ;
1023 }
1024 }
1025
1026 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1027 {
1028 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1029 wxMacFastPortSetter helper(this) ;
1030 wxCoord xx = XLOG2DEVMAC(x);
1031 wxCoord yy = YLOG2DEVMAC(y);
1032 wxCoord ww = m_signX * XLOG2DEVREL(width);
1033 wxCoord hh = m_signY * YLOG2DEVREL(height);
1034 // CMB: draw nothing if transformed w or h is 0
1035 if (ww == 0 || hh == 0)
1036 return;
1037 // CMB: handle -ve width and/or height
1038 if (ww < 0)
1039 {
1040 ww = -ww;
1041 xx = xx - ww;
1042 }
1043 if (hh < 0)
1044 {
1045 hh = -hh;
1046 yy = yy - hh;
1047 }
1048 Rect rect = { yy , xx , yy + hh , xx + ww } ;
1049 if (m_brush.GetStyle() != wxTRANSPARENT)
1050 {
1051 MacInstallBrush() ;
1052 ::PaintOval( &rect ) ;
1053 }
1054 if (m_pen.GetStyle() != wxTRANSPARENT)
1055 {
1056 MacInstallPen() ;
1057 ::FrameOval( &rect ) ;
1058 }
1059 }
1060
1061 bool wxDC::CanDrawBitmap(void) const
1062 {
1063 return true ;
1064 }
1065
1066 bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
1067 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
1068 wxCoord xsrcMask, wxCoord ysrcMask )
1069 {
1070 wxCHECK_MSG(Ok(), false, wxT("wxDC::DoBlit Illegal dc"));
1071 wxCHECK_MSG(source->Ok(), false, wxT("wxDC::DoBlit Illegal source DC"));
1072 if ( logical_func == wxNO_OP )
1073 return TRUE ;
1074 if (xsrcMask == -1 && ysrcMask == -1)
1075 {
1076 xsrcMask = xsrc; ysrcMask = ysrc;
1077 }
1078 // correct the parameter in case this dc does not have a mask at all
1079 if ( useMask && !source->m_macMask )
1080 useMask = false ;
1081 Rect srcrect , dstrect ;
1082 srcrect.top = source->YLOG2DEVMAC(ysrc) ;
1083 srcrect.left = source->XLOG2DEVMAC(xsrc) ;
1084 srcrect.right = source->XLOG2DEVMAC(xsrc + width ) ;
1085 srcrect.bottom = source->YLOG2DEVMAC(ysrc + height) ;
1086 dstrect.top = YLOG2DEVMAC(ydest) ;
1087 dstrect.left = XLOG2DEVMAC(xdest) ;
1088 dstrect.bottom = YLOG2DEVMAC(ydest + height ) ;
1089 dstrect.right = XLOG2DEVMAC(xdest + width ) ;
1090 short mode = kUnsupportedMode ;
1091 bool invertDestinationFirst = false ;
1092 switch ( logical_func )
1093 {
1094 case wxAND: // src AND dst
1095 mode = adMin ; // ok
1096 break ;
1097 case wxAND_INVERT: // (NOT src) AND dst
1098 mode = notSrcOr ; // ok
1099 break ;
1100 case wxAND_REVERSE:// src AND (NOT dst)
1101 invertDestinationFirst = true ;
1102 mode = srcOr ;
1103 break ;
1104 case wxCLEAR: // 0
1105 mode = kEmulatedMode ;
1106 break ;
1107 case wxCOPY: // src
1108 mode = srcCopy ; // ok
1109 break ;
1110 case wxEQUIV: // (NOT src) XOR dst
1111 mode = srcXor ; // ok
1112 break ;
1113 case wxINVERT: // NOT dst
1114 mode = kEmulatedMode ; //or hilite ;
1115 break ;
1116 case wxNAND: // (NOT src) OR (NOT dst)
1117 invertDestinationFirst = true ;
1118 mode = srcBic ;
1119 break ;
1120 case wxNOR: // (NOT src) AND (NOT dst)
1121 invertDestinationFirst = true ;
1122 mode = notSrcOr ;
1123 break ;
1124 case wxNO_OP: // dst
1125 mode = kEmulatedMode ; // this has already been handled upon entry
1126 break ;
1127 case wxOR: // src OR dst
1128 mode = notSrcBic ;
1129 break ;
1130 case wxOR_INVERT: // (NOT src) OR dst
1131 mode = srcBic ;
1132 break ;
1133 case wxOR_REVERSE: // src OR (NOT dst)
1134 invertDestinationFirst = true ;
1135 mode = notSrcBic ;
1136 break ;
1137 case wxSET: // 1
1138 mode = kEmulatedMode ;
1139 break ;
1140 case wxSRC_INVERT: // (NOT src)
1141 mode = notSrcCopy ; // ok
1142 break ;
1143 case wxXOR: // src XOR dst
1144 mode = notSrcXor ; // ok
1145 break ;
1146 default :
1147 break ;
1148 }
1149 if ( mode == kUnsupportedMode )
1150 {
1151 wxFAIL_MSG(wxT("unsupported blitting mode" ));
1152 return FALSE ;
1153 }
1154 CGrafPtr sourcePort = (CGrafPtr) source->m_macPort ;
1155 PixMapHandle bmappixels = GetGWorldPixMap( sourcePort ) ;
1156 if ( LockPixels(bmappixels) )
1157 {
1158 wxMacFastPortSetter helper(this) ;
1159 if ( source->GetDepth() == 1 )
1160 {
1161 RGBForeColor( &MAC_WXCOLORREF(m_textForegroundColour.GetPixel()) ) ;
1162 RGBBackColor( &MAC_WXCOLORREF(m_textBackgroundColour.GetPixel()) ) ;
1163 }
1164 else
1165 {
1166 // the modes need this, otherwise we'll end up having really nice colors...
1167 RGBColor white = { 0xFFFF, 0xFFFF,0xFFFF} ;
1168 RGBColor black = { 0,0,0} ;
1169 RGBForeColor( &black ) ;
1170 RGBBackColor( &white ) ;
1171 }
1172 if ( useMask && source->m_macMask )
1173 {
1174 if ( mode == srcCopy )
1175 {
1176 if ( LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) )
1177 {
1178 CopyMask( GetPortBitMapForCopyBits( sourcePort ) ,
1179 GetPortBitMapForCopyBits( MAC_WXHBITMAP(source->m_macMask) ) ,
1180 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1181 &srcrect, &srcrect , &dstrect ) ;
1182 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1183 }
1184 }
1185 else
1186 {
1187 RgnHandle clipRgn = NewRgn() ;
1188 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1189 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1190 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(source->m_macMask) ) ) ;
1191 OffsetRgn( clipRgn , -srcrect.left + dstrect.left, -srcrect.top + dstrect.top ) ;
1192 if ( mode == kEmulatedMode )
1193 {
1194 Pattern pat ;
1195 ::PenPat(GetQDGlobalsBlack(&pat));
1196 if ( logical_func == wxSET )
1197 {
1198 RGBColor col= { 0xFFFF, 0xFFFF, 0xFFFF } ;
1199 ::RGBForeColor( &col ) ;
1200 ::PaintRgn( clipRgn ) ;
1201 }
1202 else if ( logical_func == wxCLEAR )
1203 {
1204 RGBColor col= { 0x0000, 0x0000, 0x0000 } ;
1205 ::RGBForeColor( &col ) ;
1206 ::PaintRgn( clipRgn ) ;
1207 }
1208 else if ( logical_func == wxINVERT )
1209 {
1210 MacInvertRgn( clipRgn ) ;
1211 }
1212 else
1213 {
1214 for ( int y = 0 ; y < srcrect.right - srcrect.left ; ++y )
1215 {
1216 for ( int x = 0 ; x < srcrect.bottom - srcrect.top ; ++x )
1217 {
1218 Point dstPoint = { dstrect.top + y , dstrect.left + x } ;
1219 Point srcPoint = { srcrect.top + y , srcrect.left + x } ;
1220 if ( PtInRgn( dstPoint , clipRgn ) )
1221 {
1222 RGBColor srcColor ;
1223 RGBColor dstColor ;
1224 SetPort( (GrafPtr) sourcePort ) ;
1225 GetCPixel( srcPoint.h , srcPoint.v , &srcColor) ;
1226 SetPort( (GrafPtr) m_macPort ) ;
1227 GetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1228 wxMacCalculateColour( logical_func , srcColor , dstColor ) ;
1229 SetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1230 }
1231 }
1232 }
1233 }
1234 }
1235 else
1236 {
1237 if ( invertDestinationFirst )
1238 {
1239 MacInvertRgn( clipRgn ) ;
1240 }
1241 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1242 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1243 &srcrect, &dstrect, mode, clipRgn ) ;
1244 }
1245 DisposeRgn( clipRgn ) ;
1246 }
1247 }
1248 else
1249 {
1250 RgnHandle clipRgn = NewRgn() ;
1251 SetRectRgn( clipRgn , dstrect.left , dstrect.top , dstrect.right , dstrect.bottom ) ;
1252 if ( mode == kEmulatedMode )
1253 {
1254 Pattern pat ;
1255 ::PenPat(GetQDGlobalsBlack(&pat));
1256 if ( logical_func == wxSET )
1257 {
1258 RGBColor col= { 0xFFFF, 0xFFFF, 0xFFFF } ;
1259 ::RGBForeColor( &col ) ;
1260 ::PaintRgn( clipRgn ) ;
1261 }
1262 else if ( logical_func == wxCLEAR )
1263 {
1264 RGBColor col= { 0x0000, 0x0000, 0x0000 } ;
1265 ::RGBForeColor( &col ) ;
1266 ::PaintRgn( clipRgn ) ;
1267 }
1268 else if ( logical_func == wxINVERT )
1269 {
1270 MacInvertRgn( clipRgn ) ;
1271 }
1272 else
1273 {
1274 for ( int y = 0 ; y < srcrect.right - srcrect.left ; ++y )
1275 {
1276 for ( int x = 0 ; x < srcrect.bottom - srcrect.top ; ++x )
1277 {
1278 Point dstPoint = { dstrect.top + y , dstrect.left + x } ;
1279 Point srcPoint = { srcrect.top + y , srcrect.left + x } ;
1280 {
1281 RGBColor srcColor ;
1282 RGBColor dstColor ;
1283 SetPort( (GrafPtr) sourcePort ) ;
1284 GetCPixel( srcPoint.h , srcPoint.v , &srcColor) ;
1285 SetPort( (GrafPtr) m_macPort ) ;
1286 GetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1287 wxMacCalculateColour( logical_func , srcColor , dstColor ) ;
1288 SetCPixel( dstPoint.h , dstPoint.v , &dstColor ) ;
1289 }
1290 }
1291 }
1292 }
1293 }
1294 else
1295 {
1296 if ( invertDestinationFirst )
1297 {
1298 MacInvertRgn( clipRgn ) ;
1299 }
1300 CopyBits( GetPortBitMapForCopyBits( sourcePort ) ,
1301 GetPortBitMapForCopyBits( MAC_WXHBITMAP(m_macPort) ) ,
1302 &srcrect, &dstrect, mode, NULL ) ;
1303 }
1304 DisposeRgn( clipRgn ) ;
1305 }
1306 UnlockPixels( bmappixels ) ;
1307 }
1308 m_macPenInstalled = false ;
1309 m_macBrushInstalled = false ;
1310 m_macFontInstalled = false ;
1311 return TRUE;
1312 }
1313
1314 #ifndef FixedToInt
1315 // as macro in FixMath.h for 10.3
1316 inline Fixed IntToFixed( int inInt )
1317 {
1318 return (((SInt32) inInt) << 16);
1319 }
1320
1321 inline int FixedToInt( Fixed inFixed )
1322 {
1323 return (((SInt32) inFixed) >> 16);
1324 }
1325 #endif
1326
1327 void wxDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
1328 double angle)
1329 {
1330 wxCHECK_RET( Ok(), wxT("wxDC::DoDrawRotatedText Invalid window dc") );
1331
1332 if (angle == 0.0 )
1333 {
1334 DrawText(str, x, y);
1335 return;
1336 }
1337
1338 if ( str.Length() == 0 )
1339 return ;
1340
1341 wxMacFastPortSetter helper(this) ;
1342 MacInstallFont() ;
1343
1344 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1345 if ( 0 )
1346 {
1347 m_macFormerAliasState = IsAntiAliasedTextEnabled(&m_macFormerAliasSize);
1348 SetAntiAliasedTextEnabled(true, SInt16(m_scaleY * font->m_macFontSize));
1349 m_macAliasWasEnabled = true ;
1350 }
1351 OSStatus status = noErr ;
1352 ATSUTextLayout atsuLayout ;
1353 UniCharCount chars = str.Length() ;
1354 #if wxUSE_UNICODE
1355 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) (const wxChar*) str , 0 , str.Length() , str.Length() , 1 ,
1356 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1357 #else
1358 wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1359 int wlen = wxWcslen( wchar.data() ) ;
1360 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) wchar.data() , 0 , wlen , wlen , 1 ,
1361 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1362 #endif
1363 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
1364 int iAngle = int( angle );
1365 int drawX = XLOG2DEVMAC(x) ;
1366 int drawY = YLOG2DEVMAC(y) ;
1367
1368 ATSUTextMeasurement textBefore ;
1369 ATSUTextMeasurement textAfter ;
1370 ATSUTextMeasurement ascent ;
1371 ATSUTextMeasurement descent ;
1372
1373
1374 if ( abs(iAngle) > 0 )
1375 {
1376 Fixed atsuAngle = IntToFixed( iAngle ) ;
1377 ATSUAttributeTag atsuTags[] =
1378 {
1379 kATSULineRotationTag ,
1380 } ;
1381 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1382 {
1383 sizeof( Fixed ) ,
1384 } ;
1385 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1386 {
1387 &atsuAngle ,
1388 } ;
1389 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags)/sizeof(ATSUAttributeTag),
1390 atsuTags, atsuSizes, atsuValues ) ;
1391 }
1392 status = ::ATSUMeasureText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1393 &textBefore , &textAfter, &ascent , &descent );
1394
1395 drawX += (int)(sin(angle/RAD2DEG) * FixedToInt(ascent));
1396 drawY += (int)(cos(angle/RAD2DEG) * FixedToInt(ascent));
1397 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1398 IntToFixed(drawX) , IntToFixed(drawY) );
1399 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
1400 Rect rect ;
1401 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1402 IntToFixed(drawX) , IntToFixed(drawY) , &rect );
1403 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1404 OffsetRect( &rect , -m_macLocalOrigin.x , -m_macLocalOrigin.y ) ;
1405 CalcBoundingBox(XDEV2LOG(rect.left), YDEV2LOG(rect.top) );
1406 CalcBoundingBox(XDEV2LOG(rect.right), YDEV2LOG(rect.bottom) );
1407 ::ATSUDisposeTextLayout(atsuLayout);
1408 }
1409
1410 void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
1411 {
1412 wxCHECK_RET(Ok(), wxT("wxDC::DoDrawText Invalid DC"));
1413
1414 wxMacFastPortSetter helper(this) ;
1415 long xx = XLOG2DEVMAC(x);
1416 long yy = YLOG2DEVMAC(y);
1417 #if TARGET_CARBON
1418 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
1419 if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || m_font.GetNoAntiAliasing() )
1420 useDrawThemeText = false ;
1421 #endif
1422 MacInstallFont() ;
1423 if ( 0 )
1424 {
1425 m_macFormerAliasState = IsAntiAliasedTextEnabled(&m_macFormerAliasSize);
1426 SetAntiAliasedTextEnabled(true, 8);
1427 m_macAliasWasEnabled = true ;
1428 }
1429 FontInfo fi ;
1430 ::GetFontInfo( &fi ) ;
1431 #if TARGET_CARBON
1432 if ( !useDrawThemeText )
1433 #endif
1434 yy += fi.ascent ;
1435 ::MoveTo( xx , yy );
1436 if ( m_backgroundMode == wxTRANSPARENT )
1437 {
1438 ::TextMode( srcOr) ;
1439 }
1440 else
1441 {
1442 ::TextMode( srcCopy ) ;
1443 }
1444 int length = strtext.Length() ;
1445
1446 int laststop = 0 ;
1447 int i = 0 ;
1448 int line = 0 ;
1449 {
1450 while( i < length )
1451 {
1452 if( strtext[i] == 13 || strtext[i] == 10)
1453 {
1454 wxString linetext = strtext.Mid( laststop , i - laststop ) ;
1455 #if TARGET_CARBON
1456 if ( useDrawThemeText )
1457 {
1458 Rect frame = { yy + line*(fi.descent + fi.ascent + fi.leading) ,xx , yy + (line+1)*(fi.descent + fi.ascent + fi.leading) , xx + 10000 } ;
1459 wxMacCFStringHolder mString( linetext ) ;
1460 if ( m_backgroundMode != wxTRANSPARENT )
1461 {
1462 Point bounds={0,0} ;
1463 Rect background = frame ;
1464 SInt16 baseline ;
1465 ::GetThemeTextDimensions( mString,
1466 kThemeCurrentPortFont,
1467 kThemeStateActive,
1468 false,
1469 &bounds,
1470 &baseline );
1471 background.right = background.left + bounds.h ;
1472 background.bottom = background.top + bounds.v ;
1473 ::EraseRect( &background ) ;
1474 }
1475 ::DrawThemeTextBox( mString,
1476 kThemeCurrentPortFont,
1477 kThemeStateActive,
1478 false,
1479 &frame,
1480 teJustLeft,
1481 nil );
1482 line++ ;
1483 }
1484 else
1485 #endif
1486 {
1487 wxCharBuffer text = linetext.mb_str(wxConvLocal) ;
1488 ::DrawText( text , 0 , strlen(text) ) ;
1489 line++ ;
1490 ::MoveTo( xx , yy + line*(fi.descent + fi.ascent + fi.leading) );
1491 }
1492 laststop = i+1 ;
1493 }
1494 i++ ;
1495 }
1496 wxString linetext = strtext.Mid( laststop , i - laststop ) ;
1497 #if TARGET_CARBON
1498 if ( useDrawThemeText )
1499 {
1500 Rect frame = { yy + line*(fi.descent + fi.ascent + fi.leading) ,xx , yy + (line+1)*(fi.descent + fi.ascent + fi.leading) , xx + 10000 } ;
1501 wxMacCFStringHolder mString( linetext ) ;
1502
1503 if ( m_backgroundMode != wxTRANSPARENT )
1504 {
1505 Point bounds={0,0} ;
1506 Rect background = frame ;
1507 SInt16 baseline ;
1508 ::GetThemeTextDimensions( mString,
1509 kThemeCurrentPortFont,
1510 kThemeStateActive,
1511 false,
1512 &bounds,
1513 &baseline );
1514 background.right = background.left + bounds.h ;
1515 background.bottom = background.top + bounds.v ;
1516 ::EraseRect( &background ) ;
1517 }
1518 ::DrawThemeTextBox( mString,
1519 kThemeCurrentPortFont,
1520 kThemeStateActive,
1521 false,
1522 &frame,
1523 teJustLeft,
1524 nil );
1525 }
1526 else
1527 #endif
1528 {
1529 wxCharBuffer text = linetext.mb_str(wxConvLocal) ;
1530 ::DrawText( text , 0 , strlen(text) ) ;
1531 }
1532 }
1533 ::TextMode( srcOr ) ;
1534 }
1535
1536 bool wxDC::CanGetTextExtent() const
1537 {
1538 wxCHECK_MSG(Ok(), false, wxT("Invalid DC"));
1539 return true ;
1540 }
1541
1542 void wxDC::DoGetTextExtent( const wxString &strtext, wxCoord *width, wxCoord *height,
1543 wxCoord *descent, wxCoord *externalLeading ,
1544 wxFont *theFont ) const
1545 {
1546 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1547 wxMacFastPortSetter helper(this) ;
1548 wxFont formerFont = m_font ;
1549 if ( theFont )
1550 {
1551 // work around the constness
1552 *((wxFont*)(&m_font)) = *theFont ;
1553 }
1554 MacInstallFont() ;
1555 FontInfo fi ;
1556 ::GetFontInfo( &fi ) ;
1557 #if TARGET_CARBON
1558 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
1559 if ( UMAGetSystemVersion() < 0x1000 || IsKindOf(CLASSINFO( wxPrinterDC ) ) || ((wxFont*)&m_font)->GetNoAntiAliasing() )
1560 useGetThemeText = false ;
1561 #endif
1562 if ( height )
1563 *height = YDEV2LOGREL( fi.descent + fi.ascent ) ;
1564 if ( descent )
1565 *descent =YDEV2LOGREL( fi.descent );
1566 if ( externalLeading )
1567 *externalLeading = YDEV2LOGREL( fi.leading ) ;
1568 int length = strtext.Length() ;
1569
1570 int laststop = 0 ;
1571 int i = 0 ;
1572 int curwidth = 0 ;
1573 if ( width )
1574 {
1575 *width = 0 ;
1576 while( i < length )
1577 {
1578 if( strtext[i] == 13 || strtext[i] == 10)
1579 {
1580 wxString linetext = strtext.Mid( laststop , i - laststop ) ;
1581 if ( height )
1582 *height += YDEV2LOGREL( fi.descent + fi.ascent + fi.leading ) ;
1583 #if TARGET_CARBON
1584 if ( useGetThemeText )
1585 {
1586 Point bounds={0,0} ;
1587 SInt16 baseline ;
1588 wxMacCFStringHolder mString( linetext ) ;
1589 ::GetThemeTextDimensions( mString,
1590 kThemeCurrentPortFont,
1591 kThemeStateActive,
1592 false,
1593 &bounds,
1594 &baseline );
1595 curwidth = bounds.h ;
1596 }
1597 else
1598 #endif
1599 {
1600 wxCharBuffer text = linetext.mb_str(wxConvLocal) ;
1601 curwidth = ::TextWidth( text , 0 , strlen(text) ) ;
1602 }
1603 if ( curwidth > *width )
1604 *width = XDEV2LOGREL( curwidth ) ;
1605 laststop = i+1 ;
1606 }
1607 i++ ;
1608 }
1609
1610 wxString linetext = strtext.Mid( laststop , i - laststop ) ;
1611 #if TARGET_CARBON
1612 if ( useGetThemeText )
1613 {
1614 Point bounds={0,0} ;
1615 SInt16 baseline ;
1616 wxMacCFStringHolder mString( linetext ) ;
1617 ::GetThemeTextDimensions( mString,
1618 kThemeCurrentPortFont,
1619 kThemeStateActive,
1620 false,
1621 &bounds,
1622 &baseline );
1623 curwidth = bounds.h ;
1624 }
1625 else
1626 #endif
1627 {
1628 wxCharBuffer text = linetext.mb_str(wxConvLocal) ;
1629 curwidth = ::TextWidth( text , 0 , strlen(text) ) ;
1630 }
1631 if ( curwidth > *width )
1632 *width = XDEV2LOGREL( curwidth ) ;
1633 }
1634 if ( theFont )
1635 {
1636 // work around the constness
1637 *((wxFont*)(&m_font)) = formerFont ;
1638 m_macFontInstalled = false ;
1639 }
1640 }
1641
1642 wxCoord wxDC::GetCharWidth(void) const
1643 {
1644 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1645 wxMacFastPortSetter helper(this) ;
1646 MacInstallFont() ;
1647 int width = 0 ;
1648 #if TARGET_CARBON
1649 bool useGetThemeText = ( GetThemeTextDimensions != (void*) kUnresolvedCFragSymbolAddress ) ;
1650 if ( UMAGetSystemVersion() < 0x1000 || ((wxFont*)&m_font)->GetNoAntiAliasing() )
1651 useGetThemeText = false ;
1652 #endif
1653 char text[] = "H" ;
1654 #if TARGET_CARBON
1655 if ( useGetThemeText )
1656 {
1657 Point bounds={0,0} ;
1658 SInt16 baseline ;
1659 CFStringRef mString = CFStringCreateWithBytes( NULL , (UInt8*) text , 1 , CFStringGetSystemEncoding(), false ) ;
1660 ::GetThemeTextDimensions( mString,
1661 kThemeCurrentPortFont,
1662 kThemeStateActive,
1663 false,
1664 &bounds,
1665 &baseline );
1666 CFRelease( mString ) ;
1667 width = bounds.h ;
1668 }
1669 else
1670 #endif
1671 {
1672 width = ::TextWidth( text , 0 , 1 ) ;
1673 }
1674 return YDEV2LOGREL(width) ;
1675 }
1676
1677 wxCoord wxDC::GetCharHeight(void) const
1678 {
1679 wxCHECK_MSG(Ok(), 1, wxT("Invalid DC"));
1680 wxMacFastPortSetter helper(this) ;
1681 MacInstallFont() ;
1682 FontInfo fi ;
1683 ::GetFontInfo( &fi ) ;
1684 return YDEV2LOGREL( fi.descent + fi.ascent );
1685 }
1686
1687 void wxDC::Clear(void)
1688 {
1689 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1690 wxMacFastPortSetter helper(this) ;
1691 Rect rect = { -31000 , -31000 , 31000 , 31000 } ;
1692 if (m_backgroundBrush.GetStyle() != wxTRANSPARENT)
1693 {
1694 ::PenNormal() ;
1695 //MacInstallBrush() ;
1696 MacSetupBackgroundForCurrentPort( m_backgroundBrush ) ;
1697 ::EraseRect( &rect ) ;
1698 }
1699 }
1700
1701 void wxDC::MacInstallFont() const
1702 {
1703 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1704 // if ( m_macFontInstalled )
1705 // return ;
1706 Pattern blackColor ;
1707 MacSetupBackgroundForCurrentPort(m_backgroundBrush) ;
1708 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
1709 if ( font )
1710 {
1711 ::TextFont( font->m_macFontNum ) ;
1712 ::TextSize( short(m_scaleY * font->m_macFontSize) ) ;
1713 ::TextFace( font->m_macFontStyle ) ;
1714 m_macFontInstalled = true ;
1715 m_macBrushInstalled = false ;
1716 m_macPenInstalled = false ;
1717 RGBColor forecolor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel());
1718 RGBColor backcolor = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel());
1719 ::RGBForeColor( &forecolor );
1720 ::RGBBackColor( &backcolor );
1721 }
1722 else
1723 {
1724 FontFamilyID fontId ;
1725 Str255 fontName ;
1726 SInt16 fontSize ;
1727 Style fontStyle ;
1728 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
1729 GetFNum( fontName, &fontId );
1730 ::TextFont( fontId ) ;
1731 ::TextSize( short(m_scaleY * fontSize) ) ;
1732 ::TextFace( fontStyle ) ;
1733 // todo reset after spacing changes - or store the current spacing somewhere
1734 m_macFontInstalled = true ;
1735 m_macBrushInstalled = false ;
1736 m_macPenInstalled = false ;
1737 RGBColor forecolor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel());
1738 RGBColor backcolor = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel());
1739 ::RGBForeColor( &forecolor );
1740 ::RGBBackColor( &backcolor );
1741 }
1742 short mode = patCopy ;
1743 // todo :
1744 switch( m_logicalFunction )
1745 {
1746 case wxCOPY: // src
1747 mode = patCopy ;
1748 break ;
1749 case wxINVERT: // NOT dst
1750 ::PenPat(GetQDGlobalsBlack(&blackColor));
1751 mode = patXor ;
1752 break ;
1753 case wxXOR: // src XOR dst
1754 mode = patXor ;
1755 break ;
1756 case wxOR_REVERSE: // src OR (NOT dst)
1757 mode = notPatOr ;
1758 break ;
1759 case wxSRC_INVERT: // (NOT src)
1760 mode = notPatCopy ;
1761 break ;
1762 case wxAND: // src AND dst
1763 mode = adMin ;
1764 break ;
1765 // unsupported TODO
1766 case wxCLEAR: // 0
1767 case wxAND_REVERSE:// src AND (NOT dst)
1768 case wxAND_INVERT: // (NOT src) AND dst
1769 case wxNO_OP: // dst
1770 case wxNOR: // (NOT src) AND (NOT dst)
1771 case wxEQUIV: // (NOT src) XOR dst
1772 case wxOR_INVERT: // (NOT src) OR dst
1773 case wxNAND: // (NOT src) OR (NOT dst)
1774 case wxOR: // src OR dst
1775 case wxSET: // 1
1776 // case wxSRC_OR: // source _bitmap_ OR destination
1777 // case wxSRC_AND: // source _bitmap_ AND destination
1778 break ;
1779 }
1780 ::PenMode( mode ) ;
1781 OSStatus status = noErr ;
1782 Fixed atsuSize = IntToFixed( int(m_scaleY * font->m_macFontSize) ) ;
1783 Style qdStyle = font->m_macFontStyle ;
1784 ATSUFontID atsuFont = font->m_macATSUFontID ;
1785 status = ::ATSUCreateStyle((ATSUStyle *)&m_macATSUIStyle) ;
1786 wxASSERT_MSG( status == noErr , wxT("couldn't create ATSU style") ) ;
1787 ATSUAttributeTag atsuTags[] =
1788 {
1789 kATSUFontTag ,
1790 kATSUSizeTag ,
1791 // kATSUColorTag ,
1792 // kATSUBaselineClassTag ,
1793 kATSUVerticalCharacterTag,
1794 kATSUQDBoldfaceTag ,
1795 kATSUQDItalicTag ,
1796 kATSUQDUnderlineTag ,
1797 kATSUQDCondensedTag ,
1798 kATSUQDExtendedTag ,
1799 } ;
1800 ByteCount atsuSizes[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1801 {
1802 sizeof( ATSUFontID ) ,
1803 sizeof( Fixed ) ,
1804 // sizeof( RGBColor ) ,
1805 // sizeof( BslnBaselineClass ) ,
1806 sizeof( ATSUVerticalCharacterType),
1807 sizeof( Boolean ) ,
1808 sizeof( Boolean ) ,
1809 sizeof( Boolean ) ,
1810 sizeof( Boolean ) ,
1811 sizeof( Boolean ) ,
1812 } ;
1813 Boolean kTrue = true ;
1814 Boolean kFalse = false ;
1815 //BslnBaselineClass kBaselineDefault = kBSLNHangingBaseline ;
1816 ATSUVerticalCharacterType kHorizontal = kATSUStronglyHorizontal;
1817 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags)/sizeof(ATSUAttributeTag)] =
1818 {
1819 &atsuFont ,
1820 &atsuSize ,
1821 // &MAC_WXCOLORREF( m_textForegroundColour.GetPixel() ) ,
1822 // &kBaselineDefault ,
1823 &kHorizontal,
1824 (qdStyle & bold) ? &kTrue : &kFalse ,
1825 (qdStyle & italic) ? &kTrue : &kFalse ,
1826 (qdStyle & underline) ? &kTrue : &kFalse ,
1827 (qdStyle & condense) ? &kTrue : &kFalse ,
1828 (qdStyle & extend) ? &kTrue : &kFalse ,
1829 } ;
1830 status = ::ATSUSetAttributes((ATSUStyle)m_macATSUIStyle, sizeof(atsuTags)/sizeof(ATSUAttributeTag) ,
1831 atsuTags, atsuSizes, atsuValues);
1832 wxASSERT_MSG( status == noErr , wxT("couldn't set create ATSU style") ) ;
1833 }
1834
1835 Pattern gPatterns[] =
1836 { // hatch patterns
1837 { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF } ,
1838 { 0x01 , 0x02 , 0x04 , 0x08 , 0x10 , 0x20 , 0x40 , 0x80 } ,
1839 { 0x80 , 0x40 , 0x20 , 0x10 , 0x08 , 0x04 , 0x02 , 0x01 } ,
1840 { 0x10 , 0x10 , 0x10 , 0xFF , 0x10 , 0x10 , 0x10 , 0x10 } ,
1841 { 0x00 , 0x00 , 0x00 , 0xFF , 0x00 , 0x00 , 0x00 , 0x00 } ,
1842 { 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 , 0x10 } ,
1843 { 0x81 , 0x42 , 0x24 , 0x18 , 0x18 , 0x24 , 0x42 , 0x81 } ,
1844 // dash patterns
1845 { 0xCC , 0x99 , 0x33 , 0x66 , 0xCC , 0x99 , 0x33 , 0x66 } , // DOT
1846 { 0xFE , 0xFD , 0xFB , 0xF7 , 0xEF , 0xDF , 0xBF , 0x7F } , // LONG_DASH
1847 { 0xEE , 0xDD , 0xBB , 0x77 , 0xEE , 0xDD , 0xBB , 0x77 } , // SHORT_DASH
1848 { 0xDE , 0xBD , 0x7B , 0xF6 , 0xED , 0xDB , 0xB7 , 0x6F } , // DOT_DASH
1849 } ;
1850
1851 static void wxMacGetPattern(int penStyle, Pattern *pattern)
1852 {
1853 int index = 0; // solid pattern by default
1854 switch(penStyle)
1855 {
1856 // hatches
1857 case wxBDIAGONAL_HATCH: index = 1; break;
1858 case wxFDIAGONAL_HATCH: index = 2; break;
1859 case wxCROSS_HATCH: index = 3; break;
1860 case wxHORIZONTAL_HATCH: index = 4; break;
1861 case wxVERTICAL_HATCH: index = 5; break;
1862 case wxCROSSDIAG_HATCH: index = 6; break;
1863 // dashes
1864 case wxDOT: index = 7; break;
1865 case wxLONG_DASH: index = 8; break;
1866 case wxSHORT_DASH: index = 9; break;
1867 case wxDOT_DASH: index = 10; break;
1868 }
1869 *pattern = gPatterns[index];
1870 }
1871
1872 void wxDC::MacInstallPen() const
1873 {
1874 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1875 Pattern blackColor;
1876 // if ( m_macPenInstalled )
1877 // return ;
1878 RGBColor forecolor = MAC_WXCOLORREF( m_pen.GetColour().GetPixel());
1879 RGBColor backcolor = MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel());
1880 ::RGBForeColor( &forecolor );
1881 ::RGBBackColor( &backcolor );
1882 ::PenNormal() ;
1883 int penWidth = (int) (m_pen.GetWidth() * m_scaleX) ; ;
1884 // null means only one pixel, at whatever resolution
1885 if ( penWidth == 0 )
1886 penWidth = 1 ;
1887 ::PenSize(penWidth, penWidth);
1888
1889 int penStyle = m_pen.GetStyle();
1890 Pattern pat;
1891 if (penStyle == wxUSER_DASH)
1892 {
1893 // FIXME: there should be exactly 8 items in the dash
1894 wxDash* dash ;
1895 int number = m_pen.GetDashes(&dash) ;
1896 int index = 0;
1897 for ( int i = 0 ; i < 8 ; ++i )
1898 {
1899 pat.pat[i] = dash[index] ;
1900 if (index < number - 1)
1901 index++;
1902 }
1903 }
1904 else
1905 {
1906 wxMacGetPattern(penStyle, &pat);
1907 }
1908 ::PenPat(&pat);
1909
1910 short mode = patCopy ;
1911 // todo :
1912 switch( m_logicalFunction )
1913 {
1914 case wxCOPY: // only foreground color, leave background (thus not patCopy)
1915 mode = patOr ;
1916 break ;
1917 case wxINVERT: // NOT dst
1918 // ::PenPat(GetQDGlobalsBlack(&blackColor));
1919 mode = patXor ;
1920 break ;
1921 case wxXOR: // src XOR dst
1922 mode = patXor ;
1923 break ;
1924 case wxOR_REVERSE: // src OR (NOT dst)
1925 mode = notPatOr ;
1926 break ;
1927 case wxSRC_INVERT: // (NOT src)
1928 mode = notPatCopy ;
1929 break ;
1930 case wxAND: // src AND dst
1931 mode = adMin ;
1932 break ;
1933 // unsupported TODO
1934 case wxCLEAR: // 0
1935 case wxAND_REVERSE:// src AND (NOT dst)
1936 case wxAND_INVERT: // (NOT src) AND dst
1937 case wxNO_OP: // dst
1938 case wxNOR: // (NOT src) AND (NOT dst)
1939 case wxEQUIV: // (NOT src) XOR dst
1940 case wxOR_INVERT: // (NOT src) OR dst
1941 case wxNAND: // (NOT src) OR (NOT dst)
1942 case wxOR: // src OR dst
1943 case wxSET: // 1
1944 // case wxSRC_OR: // source _bitmap_ OR destination
1945 // case wxSRC_AND: // source _bitmap_ AND destination
1946 break ;
1947 }
1948 ::PenMode( mode ) ;
1949 m_macPenInstalled = true ;
1950 m_macBrushInstalled = false ;
1951 m_macFontInstalled = false ;
1952 }
1953
1954 void wxDC::MacSetupBackgroundForCurrentPort(const wxBrush& background )
1955 {
1956 Pattern whiteColor ;
1957 switch( background.MacGetBrushKind() )
1958 {
1959 case kwxMacBrushTheme :
1960 {
1961 ::SetThemeBackground( background.GetMacTheme() , wxDisplayDepth() , true ) ;
1962 break ;
1963 }
1964 case kwxMacBrushThemeBackground :
1965 {
1966 Rect extent ;
1967 ThemeBackgroundKind bg = background.GetMacThemeBackground( &extent ) ;
1968 ::ApplyThemeBackground( bg , &extent ,kThemeStateActive , wxDisplayDepth() , true ) ;
1969 break ;
1970 }
1971 case kwxMacBrushColour :
1972 {
1973 ::RGBBackColor( &MAC_WXCOLORREF( background.GetColour().GetPixel()) );
1974 int brushStyle = background.GetStyle();
1975 if (brushStyle == wxSOLID)
1976 ::BackPat(GetQDGlobalsWhite(&whiteColor));
1977 else if (IS_HATCH(brushStyle))
1978 {
1979 Pattern pat ;
1980 wxMacGetPattern(brushStyle, &pat);
1981 ::BackPat(&pat);
1982 }
1983 else
1984 {
1985 ::BackPat(GetQDGlobalsWhite(&whiteColor));
1986 }
1987 break ;
1988 }
1989 }
1990 }
1991
1992 void wxDC::MacInstallBrush() const
1993 {
1994 wxCHECK_RET(Ok(), wxT("Invalid DC"));
1995 Pattern blackColor ;
1996 // if ( m_macBrushInstalled )
1997 // return ;
1998 // foreground
1999 bool backgroundTransparent = (GetBackgroundMode() == wxTRANSPARENT) ;
2000 ::RGBForeColor( &MAC_WXCOLORREF( m_brush.GetColour().GetPixel()) );
2001 ::RGBBackColor( &MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel()) );
2002 int brushStyle = m_brush.GetStyle();
2003 if (brushStyle == wxSOLID)
2004 {
2005 ::PenPat(GetQDGlobalsBlack(&blackColor));
2006 }
2007 else if (IS_HATCH(brushStyle))
2008 {
2009 Pattern pat ;
2010 wxMacGetPattern(brushStyle, &pat);
2011 ::PenPat(&pat);
2012 }
2013 else if ( m_brush.GetStyle() == wxSTIPPLE || m_brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
2014 {
2015 // we force this in order to be compliant with wxMSW
2016 backgroundTransparent = false ;
2017 // for these the text fore (and back for MASK_OPAQUE) colors are used
2018 wxBitmap* bitmap = m_brush.GetStipple() ;
2019 int width = bitmap->GetWidth() ;
2020 int height = bitmap->GetHeight() ;
2021 GWorldPtr gw = NULL ;
2022 if ( m_brush.GetStyle() == wxSTIPPLE )
2023 gw = MAC_WXHBITMAP(bitmap->GetHBITMAP()) ;
2024 else
2025 gw = MAC_WXHBITMAP(bitmap->GetMask()->GetMaskBitmap()) ;
2026 PixMapHandle gwpixmaphandle = GetGWorldPixMap( gw ) ;
2027 LockPixels( gwpixmaphandle ) ;
2028 bool isMonochrome = !IsPortColor( gw ) ;
2029 if ( !isMonochrome )
2030 {
2031 if ( (**gwpixmaphandle).pixelSize == 1 )
2032 isMonochrome = true ;
2033 }
2034 if ( isMonochrome && width == 8 && height == 8 )
2035 {
2036 ::RGBForeColor( &MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) );
2037 ::RGBForeColor( &MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) );
2038 BitMap* gwbitmap = (BitMap*) *gwpixmaphandle ; // since the color depth is 1 it is a BitMap
2039 UInt8 *gwbits = (UInt8*) gwbitmap->baseAddr ;
2040 int alignment = gwbitmap->rowBytes & 0x7FFF ;
2041 Pattern pat ;
2042 for ( int i = 0 ; i < 8 ; ++i )
2043 {
2044 pat.pat[i] = gwbits[i*alignment+0] ;
2045 }
2046 UnlockPixels( GetGWorldPixMap( gw ) ) ;
2047 ::PenPat( &pat ) ;
2048 }
2049 else
2050 {
2051 // this will be the code to handle power of 2 patterns, we will have to arrive at a nice
2052 // caching scheme before putting this into production
2053 Handle image;
2054 long imageSize;
2055 PixPatHandle pixpat = NewPixPat() ;
2056 CopyPixMap(gwpixmaphandle, (**pixpat).patMap);
2057 imageSize = GetPixRowBytes((**pixpat).patMap) *
2058 ((**(**pixpat).patMap).bounds.bottom -
2059 (**(**pixpat).patMap).bounds.top);
2060 PtrToHand( (**gwpixmaphandle).baseAddr, &image, imageSize );
2061 (**pixpat).patData = image;
2062 if ( isMonochrome )
2063 {
2064 CTabHandle ctable = ((**((**pixpat).patMap)).pmTable) ;
2065 ColorSpecPtr ctspec = (ColorSpecPtr) &(**ctable).ctTable ;
2066 if ( ctspec[0].rgb.red == 0x0000 )
2067 {
2068 ctspec[1].rgb = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) ;
2069 ctspec[0].rgb = MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) ;
2070 }
2071 else
2072 {
2073 ctspec[0].rgb = MAC_WXCOLORREF( m_textBackgroundColour.GetPixel()) ;
2074 ctspec[1].rgb = MAC_WXCOLORREF( m_textForegroundColour.GetPixel()) ;
2075 }
2076 ::CTabChanged( ctable ) ;
2077 }
2078 ::PenPixPat(pixpat);
2079 m_macForegroundPixMap = pixpat ;
2080 }
2081 UnlockPixels( gwpixmaphandle ) ;
2082 }
2083 else
2084 {
2085 ::PenPat(GetQDGlobalsBlack(&blackColor));
2086 }
2087 short mode = patCopy ;
2088 switch( m_logicalFunction )
2089 {
2090 case wxCOPY: // src
2091 if ( backgroundTransparent )
2092 mode = patOr ;
2093 else
2094 mode = patCopy ;
2095 break ;
2096 case wxINVERT: // NOT dst
2097 if ( !backgroundTransparent )
2098 {
2099 ::PenPat(GetQDGlobalsBlack(&blackColor));
2100 }
2101 mode = patXor ;
2102 break ;
2103 case wxXOR: // src XOR dst
2104 mode = patXor ;
2105 break ;
2106 case wxOR_REVERSE: // src OR (NOT dst)
2107 mode = notPatOr ;
2108 break ;
2109 case wxSRC_INVERT: // (NOT src)
2110 mode = notPatCopy ;
2111 break ;
2112 case wxAND: // src AND dst
2113 mode = adMin ;
2114 break ;
2115 // unsupported TODO
2116 case wxCLEAR: // 0
2117 case wxAND_REVERSE:// src AND (NOT dst)
2118 case wxAND_INVERT: // (NOT src) AND dst
2119 case wxNO_OP: // dst
2120 case wxNOR: // (NOT src) AND (NOT dst)
2121 case wxEQUIV: // (NOT src) XOR dst
2122 case wxOR_INVERT: // (NOT src) OR dst
2123 case wxNAND: // (NOT src) OR (NOT dst)
2124 case wxOR: // src OR dst
2125 case wxSET: // 1
2126 // case wxSRC_OR: // source _bitmap_ OR destination
2127 // case wxSRC_AND: // source _bitmap_ AND destination
2128 break ;
2129 }
2130 ::PenMode( mode ) ;
2131 m_macBrushInstalled = true ;
2132 m_macPenInstalled = false ;
2133 m_macFontInstalled = false ;
2134 }
2135
2136 // ---------------------------------------------------------------------------
2137 // coordinates transformations
2138 // ---------------------------------------------------------------------------
2139
2140 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2141 {
2142 return ((wxDC *)this)->XDEV2LOG(x);
2143 }
2144
2145 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2146 {
2147 return ((wxDC *)this)->YDEV2LOG(y);
2148 }
2149
2150 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2151 {
2152 return ((wxDC *)this)->XDEV2LOGREL(x);
2153 }
2154
2155 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2156 {
2157 return ((wxDC *)this)->YDEV2LOGREL(y);
2158 }
2159
2160 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2161 {
2162 return ((wxDC *)this)->XLOG2DEV(x);
2163 }
2164
2165 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2166 {
2167 return ((wxDC *)this)->YLOG2DEV(y);
2168 }
2169
2170 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2171 {
2172 return ((wxDC *)this)->XLOG2DEVREL(x);
2173 }
2174
2175 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2176 {
2177 return ((wxDC *)this)->YLOG2DEVREL(y);
2178 }