]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dccg.cpp
cleanup - reformat
[wxWidgets.git] / src / mac / carbon / dccg.cpp
CommitLineData
613a24f7
SC
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
20b69855
SC
12#include "wx/wxprec.h"
13
613a24f7 14#include "wx/dc.h"
20b69855
SC
15
16#if wxMAC_USE_CORE_GRAPHICS
17
613a24f7
SC
18#include "wx/app.h"
19#include "wx/mac/uma.h"
20#include "wx/dcmemory.h"
21#include "wx/dcprint.h"
22#include "wx/region.h"
23#include "wx/image.h"
24#include "wx/log.h"
25
20b69855 26
138861bc
VZ
27#ifdef __MSL__
28 #if __MSL__ >= 0x6000
29 #include "math.h"
d281db8b
SC
30 // in case our functions were defined outside std, we make it known all the same
31 namespace std { }
138861bc
VZ
32 using namespace std ;
33 #endif
613a24f7
SC
34#endif
35
36#include "wx/mac/private.h"
613a24f7 37
613a24f7 38IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject)
613a24f7 39
01b5ab33
SC
40#ifndef wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
41#define wxMAC_USE_CORE_GRAPHICS_BLEND_MODES 0
42#endif
43
613a24f7
SC
44//-----------------------------------------------------------------------------
45// constants
46//-----------------------------------------------------------------------------
47
48#if !defined( __DARWIN__ ) || defined(__MWERKS__)
49#ifndef M_PI
50const double M_PI = 3.14159265358979 ;
51#endif
52#endif
b21d67a0 53
613a24f7
SC
54const double RAD2DEG = 180.0 / M_PI;
55const short kEmulatedMode = -1 ;
56const short kUnsupportedMode = -2 ;
57
58extern TECObjectRef s_TECNativeCToUnicode ;
59
613a24f7 60
eb1a7cf9 61// TODO: update
b21d67a0
DS
62// The textctrl implementation still needs that (needs what?) for the non-HIView implementation
63//
613a24f7
SC
64wxMacWindowClipper::wxMacWindowClipper( const wxWindow* win ) :
65 wxMacPortSaver( (GrafPtr) GetWindowPort((WindowRef) win->MacGetTopLevelWindowRef()) )
66{
97071cdb 67 m_newPort = (GrafPtr) GetWindowPort((WindowRef) win->MacGetTopLevelWindowRef()) ;
613a24f7
SC
68 m_formerClip = NewRgn() ;
69 m_newClip = NewRgn() ;
70 GetClip( m_formerClip ) ;
97071cdb 71
613a24f7
SC
72 if ( win )
73 {
bcbbfab8 74 // guard against half constructed objects, this just leads to a empty clip
97071cdb 75 if ( win->GetPeer() )
bcbbfab8
SC
76 {
77 int x = 0 , y = 0;
eb1a7cf9 78 win->MacWindowToRootWindow( &x, &y ) ;
bcbbfab8
SC
79 // get area including focus rect
80 CopyRgn( (RgnHandle) ((wxWindow*)win)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip ) ;
81 if ( !EmptyRgn( m_newClip ) )
82 OffsetRgn( m_newClip , x , y ) ;
83 }
613a24f7
SC
84
85 SetClip( m_newClip ) ;
86 }
87}
88
89wxMacWindowClipper::~wxMacWindowClipper()
90{
91 SetPort( m_newPort ) ;
92 SetClip( m_formerClip ) ;
93 DisposeRgn( m_newClip ) ;
94 DisposeRgn( m_formerClip ) ;
95}
96
97wxMacWindowStateSaver::wxMacWindowStateSaver( const wxWindow* win ) :
98 wxMacWindowClipper( win )
99{
100 // the port is already set at this point
97071cdb 101 m_newPort = (GrafPtr) GetWindowPort((WindowRef) win->MacGetTopLevelWindowRef()) ;
613a24f7
SC
102 GetThemeDrawingState( &m_themeDrawingState ) ;
103}
104
105wxMacWindowStateSaver::~wxMacWindowStateSaver()
106{
107 SetPort( m_newPort ) ;
108 SetThemeDrawingState( m_themeDrawingState , true ) ;
109}
110
a8f234d2
SC
111// minimal implementation only used for appearance drawing < 10.3
112
113wxMacPortSetter::wxMacPortSetter( const wxDC* dc ) :
114 m_ph( (GrafPtr) dc->m_macPort )
115{
116 wxASSERT( dc->Ok() ) ;
117 m_dc = dc ;
b21d67a0 118
a8f234d2
SC
119// dc->MacSetupPort(&m_ph) ;
120}
97071cdb 121
a8f234d2
SC
122wxMacPortSetter::~wxMacPortSetter()
123{
124// m_dc->MacCleanupPort(&m_ph) ;
125}
126
613a24f7
SC
127//-----------------------------------------------------------------------------
128// Local functions
129//-----------------------------------------------------------------------------
20b69855 130
613a24f7
SC
131static inline double dmin(double a, double b) { return a < b ? a : b; }
132static inline double dmax(double a, double b) { return a > b ? a : b; }
133static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
134
135//-----------------------------------------------------------------------------
20b69855
SC
136// device context implementation
137//
138// more and more of the dc functionality should be implemented by calling
139// the appropricate wxMacCGContext, but we will have to do that step by step
140// also coordinate conversions should be moved to native matrix ops
613a24f7 141//-----------------------------------------------------------------------------
613a24f7 142
04732a7f
SC
143// we always stock two context states, one at entry, to be able to preserve the
144// state we were called with, the other one after changing to HI Graphics orientation
145// (this one is used for getting back clippings etc)
146
20b69855 147wxMacCGPath::wxMacCGPath()
613a24f7 148{
20b69855 149 m_path = CGPathCreateMutable() ;
613a24f7
SC
150}
151
20b69855 152wxMacCGPath::~wxMacCGPath()
613a24f7 153{
20b69855
SC
154 CGPathRelease( m_path ) ;
155}
613a24f7 156
b21d67a0 157// opens (starts) a new subpath
20b69855
SC
158void wxMacCGPath::MoveToPoint( wxCoord x1 , wxCoord y1 )
159{
160 CGPathMoveToPoint( m_path , NULL , x1 , y1 ) ;
161}
613a24f7 162
20b69855
SC
163void wxMacCGPath::AddLineToPoint( wxCoord x1 , wxCoord y1 )
164{
165 CGPathAddLineToPoint( m_path , NULL , x1 , y1 ) ;
166}
613a24f7 167
e828c96a
SC
168void wxMacCGPath::AddQuadCurveToPoint( wxCoord cx1, wxCoord cy1, wxCoord x1, wxCoord y1 )
169{
170 CGPathAddQuadCurveToPoint( m_path , NULL , cx1 , cy1 , x1 , y1 );
171}
172
20b69855 173void wxMacCGPath::AddRectangle( wxCoord x, wxCoord y, wxCoord w, wxCoord h )
613a24f7 174{
20b69855
SC
175 CGRect cgRect = { { x , y } , { w , h } } ;
176 CGPathAddRect( m_path , NULL , cgRect ) ;
177}
613a24f7 178
20b69855
SC
179void wxMacCGPath::AddCircle( wxCoord x, wxCoord y , wxCoord r )
180{
181 CGPathAddArc( m_path , NULL , x , y , r , 0.0 , 2 * M_PI , true ) ;
182}
613a24f7 183
20b69855
SC
184// closes the current subpath
185void wxMacCGPath::CloseSubpath()
186{
187 CGPathCloseSubpath( m_path ) ;
188}
613a24f7 189
20b69855 190CGPathRef wxMacCGPath::GetPath() const
613a24f7 191{
20b69855
SC
192 return m_path ;
193}
194
20b69855
SC
195wxMacCGContext::wxMacCGContext( CGrafPtr port )
196{
197 m_qdPort = port ;
cb4b0966 198 m_cgContext = NULL ;
20b69855
SC
199}
200
201wxMacCGContext::wxMacCGContext( CGContextRef cgcontext )
202{
203 m_qdPort = NULL ;
204 m_cgContext = cgcontext ;
b014adcc
SC
205 CGContextSaveGState( m_cgContext ) ;
206 CGContextSaveGState( m_cgContext ) ;
20b69855
SC
207}
208
209wxMacCGContext::wxMacCGContext()
210{
211 m_qdPort = NULL ;
212 m_cgContext = NULL ;
213}
613a24f7 214
20b69855
SC
215wxMacCGContext::~wxMacCGContext()
216{
b014adcc
SC
217 if ( m_cgContext )
218 {
68654a82 219 CGContextSynchronize( m_cgContext ) ;
b014adcc
SC
220 CGContextRestoreGState( m_cgContext ) ;
221 CGContextRestoreGState( m_cgContext ) ;
222 }
97071cdb 223
20b69855 224 if ( m_qdPort )
68654a82 225 CGContextRelease( m_cgContext ) ;
20b69855
SC
226}
227
228
229void wxMacCGContext::Clip( const wxRegion &region )
230{
97071cdb 231// ClipCGContextToRegion ( m_cgContext, &bounds , (RgnHandle) dc->m_macCurrentClipRgn ) ;
20b69855
SC
232}
233
234void wxMacCGContext::StrokePath( const wxGraphicPath *p )
235{
236 const wxMacCGPath* path = dynamic_cast< const wxMacCGPath*>( p ) ;
20b69855 237 CGContextAddPath( m_cgContext , path->GetPath() ) ;
20b69855
SC
238 CGContextStrokePath( m_cgContext ) ;
239}
240
241void wxMacCGContext::DrawPath( const wxGraphicPath *p , int fillStyle )
242{
243 const wxMacCGPath* path = dynamic_cast< const wxMacCGPath*>( p ) ;
244 CGPathDrawingMode mode = m_mode ;
97071cdb 245
20b69855
SC
246 if ( fillStyle == wxODDEVEN_RULE )
247 {
248 if ( mode == kCGPathFill )
249 mode = kCGPathEOFill ;
250 else if ( mode == kCGPathFillStroke )
251 mode = kCGPathEOFillStroke ;
252 }
97071cdb 253
20b69855 254 CGContextAddPath( m_cgContext , path->GetPath() ) ;
20b69855 255 CGContextDrawPath( m_cgContext , mode ) ;
613a24f7
SC
256}
257
20b69855 258void wxMacCGContext::FillPath( const wxGraphicPath *p , const wxColor &fillColor , int fillStyle )
613a24f7 259{
20b69855
SC
260 const wxMacCGPath* path = dynamic_cast< const wxMacCGPath*>( p ) ;
261 CGContextSaveGState( m_cgContext ) ;
262
263 RGBColor col = MAC_WXCOLORREF( fillColor.GetPixel() ) ;
264 CGContextSetRGBFillColor( m_cgContext , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
265 CGPathDrawingMode mode = kCGPathFill ;
266
267 if ( fillStyle == wxODDEVEN_RULE )
268 mode = kCGPathEOFill ;
269
270 CGContextBeginPath( m_cgContext ) ;
271 CGContextAddPath( m_cgContext , path->GetPath() ) ;
272 CGContextClosePath( m_cgContext ) ;
273 CGContextDrawPath( m_cgContext , mode ) ;
274
275 CGContextRestoreGState( m_cgContext ) ;
276}
613a24f7 277
cb4b0966
SC
278wxGraphicPath* wxMacCGContext::CreatePath()
279{
cb4b0966
SC
280 // make sure that we now have a real cgref, before doing
281 // anything with paths
a63b4755
SC
282 CGContextRef cg = GetNativeContext() ;
283 cg = NULL ;
75f0a06e 284
cb4b0966
SC
285 return new wxMacCGPath() ;
286}
287
288// in case we only got a QDPort only create a cgref now
289
290CGContextRef wxMacCGContext::GetNativeContext()
291{
97071cdb 292 if ( m_cgContext == NULL )
cb4b0966
SC
293 {
294 Rect bounds ;
295 GetPortBounds( (CGrafPtr) m_qdPort , &bounds ) ;
68654a82 296 OSStatus status = CreateCGContextForPort((CGrafPtr) m_qdPort , &m_cgContext) ;
b014adcc 297 CGContextSaveGState( m_cgContext ) ;
cb4b0966
SC
298
299 wxASSERT_MSG( status == noErr , wxT("Cannot nest wxDCs on the same window") ) ;
75f0a06e 300
cb4b0966
SC
301 CGContextTranslateCTM( m_cgContext , 0 , bounds.bottom - bounds.top ) ;
302 CGContextScaleCTM( m_cgContext , 1 , -1 ) ;
303
b014adcc 304 CGContextSaveGState( m_cgContext ) ;
cb4b0966
SC
305 SetPen( m_pen ) ;
306 SetBrush( m_brush ) ;
307 }
97071cdb 308
cb4b0966
SC
309 return m_cgContext ;
310}
311
312void wxMacCGContext::SetNativeContext( CGContextRef cg )
313{
0e71e845
SC
314 // we allow either setting or clearing but not replacing
315 wxASSERT( m_cgContext == NULL || cg == NULL ) ;
75f0a06e 316
0e71e845
SC
317 if ( cg )
318 CGContextSaveGState( cg ) ;
cb4b0966
SC
319 m_cgContext = cg ;
320}
20b69855 321
72ba915e 322#pragma mark -
b21d67a0 323#pragma mark wxMacCGPattern, ImagePattern, HatchPattern classes
72ba915e 324
b21d67a0 325// CGPattern wrapper class: always allocate on heap, never call destructor
72ba915e 326
24cd6f82 327class wxMacCGPattern
72ba915e 328{
24cd6f82 329public :
97071cdb
DS
330 wxMacCGPattern() {}
331
24cd6f82 332 // is guaranteed to be called only with a non-Null CGContextRef
75f0a06e 333 virtual void Render( CGContextRef ctxRef ) = 0 ;
72ba915e 334
24cd6f82 335 operator CGPatternRef() const { return m_patternRef ; }
97071cdb 336
24cd6f82
SC
337protected :
338 virtual ~wxMacCGPattern()
339 {
b21d67a0 340 // as this is called only when the m_patternRef is been released;
97071cdb 341 // don't release it again
24cd6f82 342 }
97071cdb 343
24cd6f82
SC
344 static void _Render( void *info, CGContextRef ctxRef )
345 {
346 wxMacCGPattern* self = (wxMacCGPattern*) info ;
347 if ( self && ctxRef )
348 self->Render( ctxRef ) ;
349 }
97071cdb 350
24cd6f82
SC
351 static void _Dispose( void *info )
352 {
353 wxMacCGPattern* self = (wxMacCGPattern*) info ;
354 delete self ;
355 }
97071cdb 356
24cd6f82 357 CGPatternRef m_patternRef ;
72ba915e 358
24cd6f82
SC
359 static const CGPatternCallbacks ms_Callbacks ;
360} ;
72ba915e 361
24cd6f82 362const CGPatternCallbacks wxMacCGPattern::ms_Callbacks = { 0, &wxMacCGPattern::_Render, &wxMacCGPattern::_Dispose };
72ba915e 363
24cd6f82 364class ImagePattern : public wxMacCGPattern
72ba915e 365{
24cd6f82 366public :
75f0a06e 367 ImagePattern( const wxBitmap* bmp , CGAffineTransform transform )
24cd6f82
SC
368 {
369 wxASSERT( bmp && bmp->Ok() ) ;
75f0a06e 370
24cd6f82
SC
371 Init( (CGImageRef) bmp->CGImageCreate() , transform ) ;
372 }
373
374 // ImagePattern takes ownership of CGImageRef passed in
375 ImagePattern( CGImageRef image , CGAffineTransform transform )
376 {
377 if ( image )
24cd6f82 378 CFRetain( image ) ;
97071cdb 379
24cd6f82
SC
380 Init( image , transform ) ;
381 }
382
75f0a06e 383 virtual void Render( CGContextRef ctxRef )
24cd6f82
SC
384 {
385 if (m_image != NULL)
386 HIViewDrawCGImage( ctxRef, &m_imageBounds, m_image );
387 }
72ba915e 388
24cd6f82 389protected :
b21d67a0 390 void Init( CGImageRef image, CGAffineTransform transform )
24cd6f82
SC
391 {
392 m_image = image ;
393 if ( m_image )
394 {
395 m_imageBounds = CGRectMake( 0.0, 0.0, (float)CGImageGetWidth( m_image ), (float)CGImageGetHeight( m_image ) ) ;
396 m_patternRef = CGPatternCreate( this , m_imageBounds, transform ,
397 m_imageBounds.size.width, m_imageBounds.size.height,
398 kCGPatternTilingNoDistortion, true , &wxMacCGPattern::ms_Callbacks ) ;
399 }
400 }
97071cdb 401
24cd6f82
SC
402 ~ImagePattern()
403 {
404 if ( m_image )
405 CGImageRelease( m_image ) ;
406 }
97071cdb 407
24cd6f82
SC
408 CGImageRef m_image ;
409 CGRect m_imageBounds ;
410} ;
72ba915e 411
24cd6f82 412class HatchPattern : public wxMacCGPattern
72ba915e 413{
24cd6f82 414public :
b21d67a0 415 HatchPattern( int hatchstyle, CGAffineTransform transform )
24cd6f82
SC
416 {
417 m_hatch = hatchstyle ;
418 m_imageBounds = CGRectMake( 0.0, 0.0, 8.0 , 8.0 ) ;
419 m_patternRef = CGPatternCreate( this , m_imageBounds, transform ,
420 m_imageBounds.size.width, m_imageBounds.size.height,
421 kCGPatternTilingNoDistortion, false , &wxMacCGPattern::ms_Callbacks ) ;
422 }
423
424 void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count )
425 {
426#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
427 if ( UMAGetSystemVersion() >= 0x1040 )
428 {
429 CGContextStrokeLineSegments( ctxRef , pts , count ) ;
430 }
431 else
432#endif
433 {
b21d67a0 434 CGContextBeginPath( ctxRef );
75f0a06e
DS
435 for (size_t i = 0; i < count; i += 2)
436 {
24cd6f82
SC
437 CGContextMoveToPoint(ctxRef, pts[i].x, pts[i].y);
438 CGContextAddLineToPoint(ctxRef, pts[i+1].x, pts[i+1].y);
439 }
440 CGContextStrokePath(ctxRef);
441 }
442 }
b21d67a0 443
75f0a06e 444 virtual void Render( CGContextRef ctxRef )
24cd6f82 445 {
97071cdb 446 switch ( m_hatch )
24cd6f82
SC
447 {
448 case wxBDIAGONAL_HATCH :
449 {
75f0a06e
DS
450 CGPoint pts[] =
451 {
24cd6f82
SC
452 { 8.0 , 0.0 } , { 0.0 , 8.0 }
453 };
454 StrokeLineSegments( ctxRef , pts , 2 ) ;
455 }
456 break ;
97071cdb 457
24cd6f82
SC
458 case wxCROSSDIAG_HATCH :
459 {
75f0a06e
DS
460 CGPoint pts[] =
461 {
24cd6f82
SC
462 { 0.0 , 0.0 } , { 8.0 , 8.0 } ,
463 { 8.0 , 0.0 } , { 0.0 , 8.0 }
464 };
465 StrokeLineSegments( ctxRef , pts , 4 ) ;
466 }
467 break ;
97071cdb 468
24cd6f82
SC
469 case wxFDIAGONAL_HATCH :
470 {
75f0a06e
DS
471 CGPoint pts[] =
472 {
24cd6f82
SC
473 { 0.0 , 0.0 } , { 8.0 , 8.0 }
474 };
475 StrokeLineSegments( ctxRef , pts , 2 ) ;
476 }
477 break ;
97071cdb 478
24cd6f82
SC
479 case wxCROSS_HATCH :
480 {
75f0a06e
DS
481 CGPoint pts[] =
482 {
24cd6f82
SC
483 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
484 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
485 };
486 StrokeLineSegments( ctxRef , pts , 4 ) ;
487 }
488 break ;
97071cdb 489
24cd6f82
SC
490 case wxHORIZONTAL_HATCH :
491 {
75f0a06e
DS
492 CGPoint pts[] =
493 {
24cd6f82
SC
494 { 0.0 , 4.0 } , { 8.0 , 4.0 } ,
495 };
496 StrokeLineSegments( ctxRef , pts , 2 ) ;
497 }
498 break ;
97071cdb 499
24cd6f82
SC
500 case wxVERTICAL_HATCH :
501 {
75f0a06e
DS
502 CGPoint pts[] =
503 {
24cd6f82
SC
504 { 4.0 , 0.0 } , { 4.0 , 8.0 } ,
505 };
506 StrokeLineSegments( ctxRef , pts , 2 ) ;
507 }
508 break ;
75f0a06e
DS
509
510 default:
511 break;
24cd6f82
SC
512 }
513 }
514
515protected :
97071cdb
DS
516 ~HatchPattern() {}
517
24cd6f82 518 CGRect m_imageBounds ;
b21d67a0 519 int m_hatch ;
24cd6f82 520} ;
72ba915e 521
b21d67a0
DS
522#pragma mark -
523
20b69855
SC
524void wxMacCGContext::SetPen( const wxPen &pen )
525{
cb4b0966
SC
526 m_pen = pen ;
527 if ( m_cgContext == NULL )
528 return ;
75f0a06e 529
20b69855
SC
530 bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
531 bool stroke = pen.GetStyle() != wxTRANSPARENT ;
cb4b0966 532
20b69855
SC
533#if 0
534 // we can benchmark performance, should go into a setting later
535 CGContextSetShouldAntialias( m_cgContext , false ) ;
536#endif
75f0a06e 537
613a24f7
SC
538 if ( fill | stroke )
539 {
75f0a06e 540 // set up brushes
613a24f7
SC
541 m_mode = kCGPathFill ; // just a default
542
613a24f7
SC
543 if ( stroke )
544 {
20b69855 545 RGBColor col = MAC_WXCOLORREF( pen.GetColour().GetPixel() ) ;
613a24f7 546 CGContextSetRGBStrokeColor( m_cgContext , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
24cd6f82 547
75f0a06e 548 // TODO * m_dc->m_scaleX
24cd6f82
SC
549 float penWidth = pen.GetWidth();
550 if (penWidth <= 0.0)
551 penWidth = 0.1;
552 CGContextSetLineWidth( m_cgContext , penWidth ) ;
613a24f7
SC
553
554 CGLineCap cap ;
97071cdb 555 switch ( pen.GetCap() )
613a24f7
SC
556 {
557 case wxCAP_ROUND :
558 cap = kCGLineCapRound ;
559 break ;
75f0a06e 560
613a24f7
SC
561 case wxCAP_PROJECTING :
562 cap = kCGLineCapSquare ;
563 break ;
75f0a06e 564
613a24f7
SC
565 case wxCAP_BUTT :
566 cap = kCGLineCapButt ;
567 break ;
75f0a06e 568
613a24f7
SC
569 default :
570 cap = kCGLineCapButt ;
571 break ;
572 }
613a24f7
SC
573
574 CGLineJoin join ;
97071cdb 575 switch ( pen.GetJoin() )
613a24f7
SC
576 {
577 case wxJOIN_BEVEL :
578 join = kCGLineJoinBevel ;
579 break ;
75f0a06e 580
613a24f7
SC
581 case wxJOIN_MITER :
582 join = kCGLineJoinMiter ;
583 break ;
75f0a06e 584
613a24f7
SC
585 case wxJOIN_ROUND :
586 join = kCGLineJoinRound ;
587 break ;
75f0a06e 588
613a24f7
SC
589 default :
590 join = kCGLineJoinMiter ;
591 break;
592 }
613a24f7 593
613a24f7
SC
594 m_mode = kCGPathStroke ;
595 int count = 0 ;
24cd6f82
SC
596
597 const float *lengths = NULL ;
598 float *userLengths = NULL ;
72ba915e 599
46bed958 600 const float dashUnit = penWidth < 1.0 ? 1.0 : penWidth;
24cd6f82 601
ec4a2b5d 602 const float dotted[] = { dashUnit , dashUnit + 2.0 };
24cd6f82
SC
603 const float short_dashed[] = { 9.0 , 6.0 };
604 const float dashed[] = { 19.0 , 9.0 };
605 const float dotted_dashed[] = { 9.0 , 6.0 , 3.0 , 3.0 };
72ba915e 606
97071cdb 607 switch ( pen.GetStyle() )
613a24f7
SC
608 {
609 case wxSOLID :
610 break ;
97071cdb 611
613a24f7 612 case wxDOT :
613a24f7
SC
613 lengths = dotted ;
614 count = WXSIZEOF(dotted);
615 break ;
97071cdb 616
613a24f7 617 case wxLONG_DASH :
613a24f7
SC
618 lengths = dashed ;
619 count = WXSIZEOF(dashed) ;
620 break ;
97071cdb 621
613a24f7 622 case wxSHORT_DASH :
613a24f7
SC
623 lengths = short_dashed ;
624 count = WXSIZEOF(short_dashed) ;
625 break ;
97071cdb 626
613a24f7 627 case wxDOT_DASH :
613a24f7
SC
628 lengths = dotted_dashed ;
629 count = WXSIZEOF(dotted_dashed);
630 break ;
97071cdb 631
613a24f7
SC
632 case wxUSER_DASH :
633 wxDash *dashes ;
20b69855 634 count = pen.GetDashes( &dashes ) ;
72ba915e 635 if ((dashes != NULL) && (count > 0))
613a24f7
SC
636 {
637 userLengths = new float[count] ;
ec4a2b5d 638 for ( int i = 0 ; i < count ; ++i )
72ba915e 639 {
46bed958 640 userLengths[i] = dashes[i] * dashUnit ;
24cd6f82
SC
641
642 if ( i % 2 == 1 && userLengths[i] < dashUnit + 2.0 )
643 userLengths[i] = dashUnit + 2.0 ;
644 else if ( i % 2 == 0 && userLengths[i] < dashUnit )
645 userLengths[i] = dashUnit ;
72ba915e 646 }
613a24f7
SC
647 }
648 lengths = userLengths ;
649 break ;
97071cdb 650
24cd6f82
SC
651 case wxSTIPPLE :
652 {
653 float alphaArray[1] = { 1.0 } ;
654 wxBitmap* bmp = pen.GetStipple() ;
655 if ( bmp && bmp->Ok() )
656 {
657 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) ) ;
658 CGContextSetStrokeColorSpace( m_cgContext , patternSpace ) ;
659 wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
660 CGContextSetStrokePattern( m_cgContext, pattern , alphaArray ) ;
661 }
662 }
663 break ;
97071cdb 664
613a24f7 665 default :
24cd6f82
SC
666 {
667 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ) ;
668 CGContextSetStrokeColorSpace( m_cgContext , patternSpace ) ;
669 wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( pen.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
670
671 RGBColor col = MAC_WXCOLORREF( pen.GetColour().GetPixel() ) ;
672 float colorArray[4] = { col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 } ;
673
674 CGContextSetStrokePattern( m_cgContext, pattern , colorArray ) ;
675 }
613a24f7
SC
676 break ;
677 }
678
72ba915e
SC
679 if ((lengths != NULL) && (count > 0))
680 {
72ba915e 681 CGContextSetLineDash( m_cgContext , 0 , lengths , count ) ;
24cd6f82
SC
682 // force the line cap, otherwise we get artifacts (overlaps) and just solid lines
683 cap = kCGLineCapButt ;
72ba915e
SC
684 }
685 else
686 {
687 CGContextSetLineDash( m_cgContext , 0 , NULL , 0 ) ;
688 }
97071cdb 689
24cd6f82 690 CGContextSetLineCap( m_cgContext , cap ) ;
75f0a06e 691 CGContextSetLineJoin( m_cgContext , join ) ;
72ba915e
SC
692
693 delete[] userLengths ;
613a24f7 694 }
97071cdb 695
613a24f7 696 if ( fill && stroke )
613a24f7 697 m_mode = kCGPathFillStroke ;
613a24f7
SC
698 }
699}
72ba915e 700
20b69855
SC
701void wxMacCGContext::SetBrush( const wxBrush &brush )
702{
cb4b0966
SC
703 m_brush = brush ;
704 if ( m_cgContext == NULL )
705 return ;
706
20b69855
SC
707 bool fill = brush.GetStyle() != wxTRANSPARENT ;
708 bool stroke = m_pen.GetStyle() != wxTRANSPARENT ;
613a24f7 709
20b69855
SC
710#if 0
711 // we can benchmark performance, should go into a setting later
712 CGContextSetShouldAntialias( m_cgContext , false ) ;
713#endif
72ba915e 714
20b69855
SC
715 if ( fill | stroke )
716 {
20b69855
SC
717 // setup brushes
718 m_mode = kCGPathFill ; // just a default
719
720 if ( fill )
721 {
24cd6f82 722 if ( brush.GetStyle() == wxSOLID )
72ba915e 723 {
24cd6f82
SC
724 RGBColor col = MAC_WXCOLORREF( brush.GetColour().GetPixel() ) ;
725 CGContextSetRGBFillColor( m_cgContext , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
726 }
727 else if ( brush.IsHatch() )
728 {
729 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ) ;
730 CGContextSetFillColorSpace( m_cgContext , patternSpace ) ;
731 wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( brush.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) );
732
733 RGBColor col = MAC_WXCOLORREF( brush.GetColour().GetPixel() ) ;
734 float colorArray[4] = { col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 } ;
735
736 CGContextSetFillPattern( m_cgContext, pattern , colorArray ) ;
737 }
738 else
739 {
740 // now brush is a bitmap
741 float alphaArray[1] = { 1.0 } ;
742 wxBitmap* bmp = brush.GetStipple() ;
743 if ( bmp && bmp->Ok() )
72ba915e 744 {
24cd6f82
SC
745 wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) ) ;
746 CGContextSetFillColorSpace( m_cgContext , patternSpace ) ;
747 wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) );
748 CGContextSetFillPattern( m_cgContext, pattern , alphaArray ) ;
72ba915e
SC
749 }
750 }
75f0a06e 751
20b69855
SC
752 m_mode = kCGPathFill ;
753 }
97071cdb 754
20b69855 755 if ( fill && stroke )
20b69855 756 m_mode = kCGPathFillStroke ;
75f0a06e
DS
757 else if ( stroke )
758 m_mode = kCGPathStroke ;
20b69855
SC
759 }
760}
761
613a24f7
SC
762void AddEllipticArcToPath(CGContextRef c, CGPoint center, float a, float b, float fromDegree , float toDegree )
763{
764 CGContextSaveGState(c);
765 CGContextTranslateCTM(c, center.x, center.y);
766 CGContextScaleCTM(c, a, b);
767 CGContextMoveToPoint(c, 1, 0);
768 CGContextAddArc(c, 0, 0, 1, DegToRad(fromDegree), DegToRad(toDegree), 0);
769 CGContextClosePath(c);
770 CGContextRestoreGState(c);
771}
772
773void AddRoundedRectToPath(CGContextRef c, CGRect rect, float ovalWidth,
774 float ovalHeight)
775{
776 float fw, fh;
777 if (ovalWidth == 0 || ovalHeight == 0)
778 {
779 CGContextAddRect(c, rect);
780 return;
781 }
97071cdb 782
613a24f7
SC
783 CGContextSaveGState(c);
784 CGContextTranslateCTM(c, CGRectGetMinX(rect), CGRectGetMinY(rect));
785 CGContextScaleCTM(c, ovalWidth, ovalHeight);
ec4a2b5d 786
613a24f7
SC
787 fw = CGRectGetWidth(rect) / ovalWidth;
788 fh = CGRectGetHeight(rect) / ovalHeight;
ec4a2b5d
DS
789
790 CGContextMoveToPoint(c, fw, fh / 2);
791 CGContextAddArcToPoint(c, fw, fh, fw / 2, fh, 1);
792 CGContextAddArcToPoint(c, 0, fh, 0, fh / 2, 1);
793 CGContextAddArcToPoint(c, 0, 0, fw / 2, 0, 1);
794 CGContextAddArcToPoint(c, fw, 0, fw, fh / 2, 1);
613a24f7
SC
795 CGContextClosePath(c);
796 CGContextRestoreGState(c);
797}
798
eb1a7cf9
DS
799#pragma mark -
800
613a24f7
SC
801wxDC::wxDC()
802{
eb1a7cf9 803 m_ok = false ;
97071cdb 804 m_colour = true;
613a24f7
SC
805 m_mm_to_pix_x = mm2pt;
806 m_mm_to_pix_y = mm2pt;
24cd6f82 807
613a24f7
SC
808 m_externalDeviceOriginX = 0;
809 m_externalDeviceOriginY = 0;
810 m_logicalScaleX = 1.0;
811 m_logicalScaleY = 1.0;
812 m_userScaleX = 1.0;
813 m_userScaleY = 1.0;
814 m_scaleX = 1.0;
815 m_scaleY = 1.0;
eb1a7cf9 816 m_needComputeScaleX =
97071cdb 817 m_needComputeScaleY = false;
20b69855 818
a63b4755 819 m_macPort = 0 ;
eb1a7cf9
DS
820 m_macLocalOrigin.x =
821 m_macLocalOrigin.y = 0 ;
20b69855 822
613a24f7
SC
823 m_pen = *wxBLACK_PEN;
824 m_font = *wxNORMAL_FONT;
825 m_brush = *wxWHITE_BRUSH;
613a24f7 826
20b69855 827 m_macATSUIStyle = NULL ;
20b69855 828 m_graphicContext = NULL ;
613a24f7
SC
829}
830
97071cdb 831wxDC::~wxDC()
613a24f7 832{
97071cdb 833 if ( m_macATSUIStyle )
613a24f7
SC
834 {
835 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
836 m_macATSUIStyle = NULL ;
837 }
20b69855
SC
838
839 delete m_graphicContext ;
613a24f7
SC
840}
841
842void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
843{
b21d67a0
DS
844 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawBitmap - invalid DC") );
845 wxCHECK_RET( bmp.Ok(), wxT("wxDC(cg)::DoDrawBitmap - invalid bitmap") );
75f0a06e 846
20b69855
SC
847 wxCoord xx = XLOG2DEVMAC(x);
848 wxCoord yy = YLOG2DEVMAC(y);
849 wxCoord w = bmp.GetWidth();
850 wxCoord h = bmp.GetHeight();
851 wxCoord ww = XLOG2DEVREL(w);
852 wxCoord hh = YLOG2DEVREL(h);
853
b60f5ca5 854 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
20b69855
SC
855 CGImageRef image = (CGImageRef)( bmp.CGImageCreate() ) ;
856 HIRect r = CGRectMake( xx , yy , ww , hh ) ;
857 HIViewDrawCGImage( cg , &r , image ) ;
858 CGImageRelease( image ) ;
613a24f7
SC
859}
860
861void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y )
862{
b21d67a0
DS
863 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawIcon - invalid DC") );
864 wxCHECK_RET( icon.Ok(), wxT("wxDC(cg)::DoDrawIcon - invalid icon") );
20b69855
SC
865
866 wxCoord xx = XLOG2DEVMAC(x);
867 wxCoord yy = YLOG2DEVMAC(y);
868 wxCoord w = icon.GetWidth();
869 wxCoord h = icon.GetHeight();
870 wxCoord ww = XLOG2DEVREL(w);
871 wxCoord hh = YLOG2DEVREL(h);
872
b60f5ca5 873 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
20b69855 874 CGRect r = CGRectMake( 00 , 00 , ww , hh ) ;
b21d67a0 875 CGContextSaveGState( cg );
75f0a06e 876 CGContextTranslateCTM( cg, xx , yy + hh );
b21d67a0 877 CGContextScaleCTM( cg, 1, -1 );
20b69855
SC
878 PlotIconRefInContext( cg , &r , kAlignNone , kTransformNone ,
879 NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) ) ;
880 CGContextRestoreGState( cg ) ;
613a24f7
SC
881}
882
883void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
884{
b21d67a0 885 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoSetClippingRegion - invalid DC") );
75f0a06e 886
613a24f7
SC
887 wxCoord xx, yy, ww, hh;
888 xx = XLOG2DEVMAC(x);
889 yy = YLOG2DEVMAC(y);
890 ww = XLOG2DEVREL(width);
891 hh = YLOG2DEVREL(height);
b014adcc 892
b60f5ca5 893 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
b21d67a0 894 CGRect clipRect = CGRectMake( xx , yy , ww, hh ) ;
b014adcc
SC
895 CGContextClipToRect( cgContext , clipRect ) ;
896
20b69855
SC
897// SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ;
898// SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
75f0a06e 899
97071cdb 900 if ( m_clipping )
613a24f7 901 {
b21d67a0
DS
902 m_clipX1 = wxMax( m_clipX1, xx );
903 m_clipY1 = wxMax( m_clipY1, yy );
904 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
905 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
613a24f7
SC
906 }
907 else
908 {
97071cdb 909 m_clipping = true;
b21d67a0 910
613a24f7
SC
911 m_clipX1 = xx;
912 m_clipY1 = yy;
913 m_clipX2 = xx + ww;
914 m_clipY2 = yy + hh;
915 }
75f0a06e 916
b21d67a0 917 // TODO: as soon as we don't reset the context for each operation anymore
613a24f7
SC
918 // we have to update the context as well
919}
920
eb1a7cf9 921void wxDC::DoSetClippingRegionAsRegion( const wxRegion &region )
613a24f7 922{
b21d67a0
DS
923 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoSetClippingRegionAsRegion - invalid DC") );
924
613a24f7
SC
925 if (region.Empty())
926 {
927 DestroyClippingRegion();
928 return;
929 }
97071cdb 930
613a24f7
SC
931 wxCoord x, y, w, h;
932 region.GetBox( x, y, w, h );
933 wxCoord xx, yy, ww, hh;
934 xx = XLOG2DEVMAC(x);
935 yy = YLOG2DEVMAC(y);
936 ww = XLOG2DEVREL(w);
937 hh = YLOG2DEVREL(h);
75f0a06e 938
613a24f7
SC
939 // if we have a scaling that we cannot map onto native regions
940 // we must use the box
941 if ( ww != w || hh != h )
942 {
943 wxDC::DoSetClippingRegion( x, y, w, h );
944 }
945 else
946 {
75f0a06e 947#if 0
613a24f7
SC
948 CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ;
949 if ( xx != x || yy != y )
613a24f7 950 OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ;
b21d67a0 951 SectRgn( (RgnHandle)m_macCurrentClipRgn , (RgnHandle)m_macBoundaryClipRgn , (RgnHandle)m_macCurrentClipRgn ) ;
75f0a06e
DS
952#endif
953
97071cdb 954 if ( m_clipping )
613a24f7 955 {
b21d67a0
DS
956 m_clipX1 = wxMax( m_clipX1, xx );
957 m_clipY1 = wxMax( m_clipY1, yy );
958 m_clipX2 = wxMin( m_clipX2, (xx + ww) );
959 m_clipY2 = wxMin( m_clipY2, (yy + hh) );
613a24f7
SC
960 }
961 else
962 {
97071cdb 963 m_clipping = true;
b21d67a0 964
613a24f7
SC
965 m_clipX1 = xx;
966 m_clipY1 = yy;
967 m_clipX2 = xx + ww;
968 m_clipY2 = yy + hh;
969 }
970 }
971}
972
973void wxDC::DestroyClippingRegion()
974{
20b69855 975// CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
b21d67a0 976
b60f5ca5 977 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
b014adcc 978 CGContextRestoreGState( cgContext );
b21d67a0
DS
979 CGContextSaveGState( cgContext );
980
0e71e845
SC
981 m_graphicContext->SetPen( m_pen ) ;
982 m_graphicContext->SetBrush( m_brush ) ;
b21d67a0 983
97071cdb 984 m_clipping = false;
613a24f7
SC
985}
986
987void wxDC::DoGetSizeMM( int* width, int* height ) const
988{
75f0a06e
DS
989 int w = 0, h = 0;
990
613a24f7 991 GetSize( &w, &h );
75f0a06e
DS
992 if (width)
993 *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) );
994 if (height)
995 *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) );
613a24f7
SC
996}
997
998void wxDC::SetTextForeground( const wxColour &col )
999{
b21d67a0 1000 wxCHECK_RET( Ok(), wxT("wxDC(cg)::SetTextForeground - invalid DC") );
75f0a06e 1001
20b69855
SC
1002 if ( col != m_textForegroundColour )
1003 {
1004 m_textForegroundColour = col;
1005 MacInstallFont() ;
1006 }
613a24f7
SC
1007}
1008
1009void wxDC::SetTextBackground( const wxColour &col )
1010{
b21d67a0 1011 wxCHECK_RET( Ok(), wxT("wxDC(cg)::SetTextBackground - invalid DC") );
75f0a06e 1012
613a24f7 1013 m_textBackgroundColour = col;
613a24f7
SC
1014}
1015
1016void wxDC::SetMapMode( int mode )
1017{
1018 switch (mode)
1019 {
1020 case wxMM_TWIPS:
75f0a06e 1021 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
613a24f7 1022 break;
97071cdb 1023
613a24f7 1024 case wxMM_POINTS:
75f0a06e 1025 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
613a24f7 1026 break;
97071cdb 1027
613a24f7
SC
1028 case wxMM_METRIC:
1029 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
1030 break;
97071cdb 1031
613a24f7 1032 case wxMM_LOMETRIC:
75f0a06e 1033 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
613a24f7 1034 break;
97071cdb 1035
613a24f7 1036 case wxMM_TEXT:
97071cdb 1037 default:
613a24f7
SC
1038 SetLogicalScale( 1.0, 1.0 );
1039 break;
1040 }
97071cdb 1041
613a24f7
SC
1042 if (mode != wxMM_TEXT)
1043 {
97071cdb
DS
1044 m_needComputeScaleX =
1045 m_needComputeScaleY = true;
613a24f7
SC
1046 }
1047}
1048
1049void wxDC::SetUserScale( double x, double y )
1050{
1051 // allow negative ? -> no
1052 m_userScaleX = x;
1053 m_userScaleY = y;
1054 ComputeScaleAndOrigin();
1055}
1056
1057void wxDC::SetLogicalScale( double x, double y )
1058{
1059 // allow negative ?
1060 m_logicalScaleX = x;
1061 m_logicalScaleY = y;
1062 ComputeScaleAndOrigin();
1063}
1064
1065void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
1066{
1067 m_logicalOriginX = x * m_signX; // is this still correct ?
1068 m_logicalOriginY = y * m_signY;
1069 ComputeScaleAndOrigin();
1070}
1071
1072void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1073{
1074 m_externalDeviceOriginX = x;
1075 m_externalDeviceOriginY = y;
1076 ComputeScaleAndOrigin();
1077}
1078
1079void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1080{
1081 m_signX = (xLeftRight ? 1 : -1);
b21d67a0 1082 m_signY = (yBottomUp ? -1 : 1);
613a24f7
SC
1083 ComputeScaleAndOrigin();
1084}
1085
1086wxSize wxDC::GetPPI() const
1087{
1088 return wxSize(72, 72);
1089}
1090
1091int wxDC::GetDepth() const
1092{
20b69855 1093 return 32 ;
613a24f7
SC
1094}
1095
1096void wxDC::ComputeScaleAndOrigin()
1097{
1098 // CMB: copy scale to see if it changes
1099 double origScaleX = m_scaleX;
1100 double origScaleY = m_scaleY;
1101 m_scaleX = m_logicalScaleX * m_userScaleX;
1102 m_scaleY = m_logicalScaleY * m_userScaleY;
24cd6f82
SC
1103 m_deviceOriginX = m_externalDeviceOriginX;
1104 m_deviceOriginY = m_externalDeviceOriginY;
97071cdb 1105
613a24f7
SC
1106 // CMB: if scale has changed call SetPen to recalulate the line width
1107 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
1108 {
1109 // this is a bit artificial, but we need to force wxDC to think
1110 // the pen has changed
75f0a06e 1111 wxPen pen( GetPen() );
97071cdb 1112
613a24f7
SC
1113 m_pen = wxNullPen;
1114 SetPen(pen);
1115 }
1116}
1117
b21d67a0 1118void wxDC::SetPalette( const wxPalette& palette )
613a24f7
SC
1119{
1120}
1121
b21d67a0 1122void wxDC::SetBackgroundMode( int mode )
613a24f7
SC
1123{
1124 m_backgroundMode = mode ;
1125}
1126
b21d67a0 1127void wxDC::SetFont( const wxFont &font )
613a24f7
SC
1128{
1129 m_font = font;
20b69855 1130 MacInstallFont() ;
613a24f7
SC
1131}
1132
b21d67a0 1133void wxDC::SetPen( const wxPen &pen )
613a24f7
SC
1134{
1135 if ( m_pen == pen )
1136 return ;
97071cdb 1137
613a24f7 1138 m_pen = pen;
20b69855
SC
1139 if ( m_graphicContext )
1140 {
24cd6f82
SC
1141 if ( m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT )
1142 {
1143 m_graphicContext->SetPen( m_pen ) ;
1144 }
1145 else
1146 {
1147 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
1148 // eg when using a wxScrollWindow and scrolling around
1149 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1150 int origX = XLOG2DEVMAC(0) ;
1151 int origY = YLOG2DEVMAC(0) ;
b21d67a0 1152 CGContextTranslateCTM( cgContext, origX, origY );
24cd6f82 1153 m_graphicContext->SetPen( m_pen ) ;
b21d67a0 1154 CGContextTranslateCTM( cgContext, -origX, -origY );
24cd6f82 1155 }
20b69855 1156 }
613a24f7
SC
1157}
1158
b21d67a0 1159void wxDC::SetBrush( const wxBrush &brush )
613a24f7
SC
1160{
1161 if (m_brush == brush)
1162 return;
97071cdb 1163
613a24f7 1164 m_brush = brush;
20b69855
SC
1165 if ( m_graphicContext )
1166 {
24cd6f82
SC
1167 if ( brush.GetStyle() == wxSOLID || brush.GetStyle() == wxTRANSPARENT )
1168 {
1169 m_graphicContext->SetBrush( m_brush ) ;
1170 }
1171 else
1172 {
1173 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
1174 // eg when using a wxScrollWindow and scrolling around
1175 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1176 int origX = XLOG2DEVMAC(0) ;
1177 int origY = YLOG2DEVMAC(0) ;
b21d67a0 1178 CGContextTranslateCTM( cgContext, origX, origY );
24cd6f82 1179 m_graphicContext->SetBrush( m_brush ) ;
b21d67a0 1180 CGContextTranslateCTM( cgContext, -origX, -origY );
24cd6f82 1181 }
20b69855 1182 }
613a24f7
SC
1183}
1184
b21d67a0 1185void wxDC::SetBackground( const wxBrush &brush )
613a24f7
SC
1186{
1187 if (m_backgroundBrush == brush)
1188 return;
97071cdb 1189
613a24f7
SC
1190 m_backgroundBrush = brush;
1191 if (!m_backgroundBrush.Ok())
1192 return;
613a24f7
SC
1193}
1194
b21d67a0 1195void wxDC::SetLogicalFunction( int function )
613a24f7
SC
1196{
1197 if (m_logicalFunction == function)
1198 return;
97071cdb 1199
613a24f7 1200 m_logicalFunction = function ;
01b5ab33
SC
1201#if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1202 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1203 if ( m_logicalFunction == wxCOPY )
1204 CGContextSetBlendMode( cgContext, kCGBlendModeNormal ) ;
1205 else if ( m_logicalFunction == wxINVERT )
1206 CGContextSetBlendMode( cgContext, kCGBlendModeExclusion ) ;
1207 else
1208 CGContextSetBlendMode( cgContext, kCGBlendModeNormal ) ;
1209#endif
613a24f7
SC
1210}
1211
1212extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
1213 const wxColour & col, int style);
1214
1215bool wxDC::DoFloodFill(wxCoord x, wxCoord y,
1216 const wxColour& col, int style)
1217{
1218 return wxDoFloodFill(this, x, y, col, style);
1219}
1220
b21d67a0 1221bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
613a24f7 1222{
b21d67a0 1223 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoGetPixel - invalid DC") );
75f0a06e 1224
880f5369
SC
1225 wxMacPortSaver helper((CGrafPtr)m_macPort) ;
1226 RGBColor colour;
75f0a06e
DS
1227
1228 // NB: GetCPixel is a deprecated QD call, and a slow one at that
880f5369
SC
1229 GetCPixel(
1230 XLOG2DEVMAC(x) + m_macLocalOriginInPort.x - m_macLocalOrigin.x,
1231 YLOG2DEVMAC(y) + m_macLocalOriginInPort.y - m_macLocalOrigin.y, &colour );
75f0a06e
DS
1232
1233 // convert from Mac colour to wx
1234 col->Set( colour.red >> 8, colour.green >> 8, colour.blue >> 8 );
97071cdb 1235
880f5369 1236 return true ;
613a24f7
SC
1237}
1238
b21d67a0 1239void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
613a24f7 1240{
b21d67a0
DS
1241 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawLine - invalid DC") );
1242
01b5ab33 1243#if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
08c8555e
SC
1244 if ( m_logicalFunction != wxCOPY )
1245 return ;
01b5ab33 1246#endif
b21d67a0 1247
613a24f7
SC
1248 wxCoord xx1 = XLOG2DEVMAC(x1) ;
1249 wxCoord yy1 = YLOG2DEVMAC(y1) ;
1250 wxCoord xx2 = XLOG2DEVMAC(x2) ;
1251 wxCoord yy2 = YLOG2DEVMAC(y2) ;
1252
20b69855 1253 wxGraphicPath* path = m_graphicContext->CreatePath() ;
b21d67a0 1254 path->MoveToPoint( xx1, yy1 ) ;
20b69855
SC
1255 path->AddLineToPoint( xx2 , yy2 ) ;
1256 path->CloseSubpath() ;
1257 m_graphicContext->StrokePath( path ) ;
1258 delete path ;
613a24f7
SC
1259
1260 CalcBoundingBox(x1, y1);
1261 CalcBoundingBox(x2, y2);
1262}
1263
b21d67a0 1264void wxDC::DoCrossHair( wxCoord x, wxCoord y )
613a24f7 1265{
b21d67a0 1266 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoCrossHair - invalid DC") );
613a24f7 1267
08c8555e
SC
1268 if ( m_logicalFunction != wxCOPY )
1269 return ;
613a24f7 1270
b21d67a0
DS
1271 int w = 0, h = 0;
1272
613a24f7
SC
1273 GetSize( &w, &h );
1274 wxCoord xx = XLOG2DEVMAC(x);
1275 wxCoord yy = YLOG2DEVMAC(y);
1276
20b69855 1277 wxGraphicPath* path = m_graphicContext->CreatePath() ;
b21d67a0 1278 path->MoveToPoint( XLOG2DEVMAC(0), yy ) ;
20b69855
SC
1279 path->AddLineToPoint( XLOG2DEVMAC(w), yy ) ;
1280 path->CloseSubpath() ;
1281 path->MoveToPoint( xx, YLOG2DEVMAC(0) ) ;
1282 path->AddLineToPoint( xx, YLOG2DEVMAC(h) ) ;
1283 path->CloseSubpath() ;
1284 m_graphicContext->StrokePath( path ) ;
1285 delete path ;
613a24f7
SC
1286
1287 CalcBoundingBox(x, y);
b21d67a0 1288 CalcBoundingBox(x + w, y + h);
613a24f7
SC
1289}
1290
b21d67a0 1291void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
613a24f7
SC
1292 wxCoord x2, wxCoord y2,
1293 wxCoord xc, wxCoord yc )
1294{
b21d67a0 1295 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawArc - invalid DC") );
08c8555e
SC
1296
1297 if ( m_logicalFunction != wxCOPY )
1298 return ;
1299
613a24f7
SC
1300 wxCoord xx1 = XLOG2DEVMAC(x1);
1301 wxCoord yy1 = YLOG2DEVMAC(y1);
1302 wxCoord xx2 = XLOG2DEVMAC(x2);
1303 wxCoord yy2 = YLOG2DEVMAC(y2);
1304 wxCoord xxc = XLOG2DEVMAC(xc);
1305 wxCoord yyc = YLOG2DEVMAC(yc);
75f0a06e 1306
613a24f7
SC
1307 double dx = xx1 - xxc;
1308 double dy = yy1 - yyc;
b21d67a0 1309 double radius = sqrt((double)(dx * dx + dy * dy));
75f0a06e 1310 wxCoord rad = (wxCoord)radius;
72ba915e 1311 double sa, ea;
613a24f7
SC
1312 if (xx1 == xx2 && yy1 == yy2)
1313 {
72ba915e
SC
1314 sa = 0.0;
1315 ea = 360.0;
613a24f7
SC
1316 }
1317 else if (radius == 0.0)
1318 {
72ba915e 1319 sa = ea = 0.0;
613a24f7
SC
1320 }
1321 else
1322 {
72ba915e 1323 sa = (xx1 - xxc == 0) ?
613a24f7 1324 (yy1 - yyc < 0) ? 90.0 : -90.0 :
b21d67a0 1325 -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG;
72ba915e 1326 ea = (xx2 - xxc == 0) ?
613a24f7 1327 (yy2 - yyc < 0) ? 90.0 : -90.0 :
b21d67a0 1328 -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG;
613a24f7 1329 }
72ba915e
SC
1330
1331 bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
20b69855
SC
1332 wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ;
1333 CGContextRef ctx = mctx->GetNativeContext() ;
72ba915e
SC
1334 CGContextSaveGState( ctx ) ;
1335 CGContextTranslateCTM( ctx, xxc , yyc );
1336 CGContextScaleCTM( ctx , 1 , -1 ) ;
1337 if ( fill )
1338 CGContextMoveToPoint( ctx , 0 , 0 ) ;
ec4a2b5d 1339 CGContextAddArc( ctx, 0, 0 , rad , DegToRad(sa), DegToRad(ea), 0 );
72ba915e
SC
1340 if ( fill )
1341 CGContextAddLineToPoint( ctx , 0 , 0 ) ;
1342 CGContextRestoreGState( ctx ) ;
20b69855 1343 CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ;
613a24f7
SC
1344}
1345
b21d67a0 1346void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
613a24f7
SC
1347 double sa, double ea )
1348{
b21d67a0 1349 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawEllipticArc - invalid DC") );
20b69855 1350
08c8555e
SC
1351 if ( m_logicalFunction != wxCOPY )
1352 return ;
1353
613a24f7
SC
1354 wxCoord xx = XLOG2DEVMAC(x);
1355 wxCoord yy = YLOG2DEVMAC(y);
1356 wxCoord ww = m_signX * XLOG2DEVREL(w);
1357 wxCoord hh = m_signY * YLOG2DEVREL(h);
75f0a06e 1358
613a24f7 1359 // handle -ve width and/or height
75f0a06e
DS
1360 if (ww < 0)
1361 {
1362 ww = -ww;
1363 xx = xx - ww;
1364 }
1365 if (hh < 0)
1366 {
1367 hh = -hh;
1368 yy = yy - hh;
1369 }
72ba915e
SC
1370
1371 bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
1372
20b69855
SC
1373 wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ;
1374 CGContextRef ctx = mctx->GetNativeContext() ;
72ba915e
SC
1375
1376 CGContextSaveGState( ctx ) ;
ec4a2b5d 1377 CGContextTranslateCTM( ctx, xx + ww / 2, yy + hh / 2 );
72ba915e
SC
1378 CGContextScaleCTM( ctx , 1 * ww / 2 , -1 * hh / 2 ) ;
1379 if ( fill )
1380 CGContextMoveToPoint( ctx , 0 , 0 ) ;
ec4a2b5d 1381 CGContextAddArc( ctx, 0, 0, 1, DegToRad(sa), DegToRad(ea), 0 );
72ba915e
SC
1382 if ( fill )
1383 CGContextAddLineToPoint( ctx , 0 , 0 ) ;
1384 CGContextRestoreGState( ctx ) ;
1385 CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ;
613a24f7
SC
1386}
1387
b21d67a0 1388void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
613a24f7 1389{
b21d67a0 1390 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawPoint - invalid DC") );
75f0a06e 1391
613a24f7
SC
1392 DoDrawLine( x , y , x + 1 , y + 1 ) ;
1393}
1394
b21d67a0 1395void wxDC::DoDrawLines(int n, wxPoint points[],
613a24f7
SC
1396 wxCoord xoffset, wxCoord yoffset)
1397{
b21d67a0 1398 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawLines - invalid DC") );
75f0a06e 1399
01b5ab33 1400#if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
08c8555e
SC
1401 if ( m_logicalFunction != wxCOPY )
1402 return ;
01b5ab33 1403#endif
08c8555e 1404
613a24f7
SC
1405 wxCoord x1, x2 , y1 , y2 ;
1406 x1 = XLOG2DEVMAC(points[0].x + xoffset);
1407 y1 = YLOG2DEVMAC(points[0].y + yoffset);
20b69855
SC
1408 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1409 path->MoveToPoint( x1 , y1 ) ;
613a24f7
SC
1410 for (int i = 1; i < n; i++)
1411 {
1412 x2 = XLOG2DEVMAC(points[i].x + xoffset);
1413 y2 = YLOG2DEVMAC(points[i].y + yoffset);
1414
20b69855 1415 path->AddLineToPoint( x2 , y2 ) ;
613a24f7 1416 }
97071cdb 1417
20b69855
SC
1418 m_graphicContext->StrokePath( path ) ;
1419 delete path ;
613a24f7
SC
1420}
1421
e828c96a
SC
1422#if wxUSE_SPLINES
1423void wxDC::DoDrawSpline(wxList *points)
1424{
b21d67a0
DS
1425 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawSpline - invalid DC") );
1426
e828c96a
SC
1427 if ( m_logicalFunction != wxCOPY )
1428 return ;
1429
1430 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1431
1432 wxList::compatibility_iterator node = points->GetFirst();
1433 if (node == wxList::compatibility_iterator())
1434 // empty list
1435 return;
1436
1437 wxPoint *p = (wxPoint *)node->GetData();
1438
1439 wxCoord x1 = p->x;
1440 wxCoord y1 = p->y;
1441
1442 node = node->GetNext();
1443 p = (wxPoint *)node->GetData();
1444
1445 wxCoord x2 = p->x;
1446 wxCoord y2 = p->y;
1447 wxCoord cx1 = ( x1 + x2 ) / 2;
1448 wxCoord cy1 = ( y1 + y2 ) / 2;
1449
1450 path->MoveToPoint( XLOG2DEVMAC( x1 ) , XLOG2DEVMAC( y1 ) ) ;
1451 path->AddLineToPoint( XLOG2DEVMAC( cx1 ) , XLOG2DEVMAC( cy1 ) ) ;
1452
1453#if !wxUSE_STL
1454 while ((node = node->GetNext()) != NULL)
1455#else
1456 while ((node = node->GetNext()))
1457#endif // !wxUSE_STL
1458 {
1459 p = (wxPoint *)node->GetData();
1460 x1 = x2;
1461 y1 = y2;
1462 x2 = p->x;
1463 y2 = p->y;
1464 wxCoord cx4 = (x1 + x2) / 2;
1465 wxCoord cy4 = (y1 + y2) / 2;
b21d67a0 1466
e828c96a
SC
1467 path->AddQuadCurveToPoint( XLOG2DEVMAC( x1 ) , XLOG2DEVMAC( y1 ) ,
1468 XLOG2DEVMAC( cx4 ) , XLOG2DEVMAC( cy4 ) ) ;
b21d67a0 1469
e828c96a
SC
1470 cx1 = cx4;
1471 cy1 = cy4;
1472 }
1473
1474 path->AddLineToPoint( XLOG2DEVMAC( x2 ) , XLOG2DEVMAC( y2 ) ) ;
1475
1476 m_graphicContext->StrokePath( path ) ;
1477 delete path ;
1478}
1479#endif
1480
b21d67a0 1481void wxDC::DoDrawPolygon( int n, wxPoint points[],
613a24f7
SC
1482 wxCoord xoffset, wxCoord yoffset,
1483 int fillStyle )
1484{
b21d67a0 1485 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawPolygon - invalid DC") );
75f0a06e
DS
1486
1487 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
613a24f7 1488 return ;
08c8555e
SC
1489 if ( m_logicalFunction != wxCOPY )
1490 return ;
1491
75f0a06e 1492 wxCoord x1, x2 , y1 , y2 ;
613a24f7
SC
1493 x2 = x1 = XLOG2DEVMAC(points[0].x + xoffset);
1494 y2 = y1 = YLOG2DEVMAC(points[0].y + yoffset);
1495
20b69855
SC
1496 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1497 path->MoveToPoint( x1 , y1 ) ;
613a24f7
SC
1498 for (int i = 1; i < n; i++)
1499 {
1500 x2 = XLOG2DEVMAC(points[i].x + xoffset);
1501 y2 = YLOG2DEVMAC(points[i].y + yoffset);
1502
20b69855 1503 path->AddLineToPoint( x2 , y2 ) ;
613a24f7 1504 }
97071cdb 1505
613a24f7 1506 if ( x1 != x2 || y1 != y2 )
b21d67a0 1507 path->AddLineToPoint( x1, y1 ) ;
97071cdb 1508
20b69855
SC
1509 path->CloseSubpath() ;
1510 m_graphicContext->DrawPath( path , fillStyle ) ;
75f0a06e 1511
20b69855 1512 delete path ;
613a24f7
SC
1513}
1514
1515void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1516{
b21d67a0 1517 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRectangle - invalid DC") );
08c8555e
SC
1518
1519 if ( m_logicalFunction != wxCOPY )
1520 return ;
1521
613a24f7
SC
1522 wxCoord xx = XLOG2DEVMAC(x);
1523 wxCoord yy = YLOG2DEVMAC(y);
1524 wxCoord ww = m_signX * XLOG2DEVREL(width);
1525 wxCoord hh = m_signY * YLOG2DEVREL(height);
75f0a06e 1526
613a24f7
SC
1527 // CMB: draw nothing if transformed w or h is 0
1528 if (ww == 0 || hh == 0)
1529 return;
97071cdb 1530
613a24f7
SC
1531 // CMB: handle -ve width and/or height
1532 if (ww < 0)
1533 {
1534 ww = -ww;
1535 xx = xx - ww;
1536 }
1537 if (hh < 0)
1538 {
1539 hh = -hh;
1540 yy = yy - hh;
1541 }
97071cdb 1542
20b69855 1543 wxGraphicPath* path = m_graphicContext->CreatePath() ;
24cd6f82 1544 path->AddRectangle( xx , yy , ww , hh ) ;
20b69855
SC
1545 m_graphicContext->DrawPath( path ) ;
1546 delete path ;
613a24f7
SC
1547}
1548
b21d67a0 1549void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
613a24f7
SC
1550 wxCoord width, wxCoord height,
1551 double radius)
1552{
b21d67a0 1553 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRoundedRectangle - invalid DC") );
08c8555e
SC
1554
1555 if ( m_logicalFunction != wxCOPY )
1556 return ;
1557
613a24f7
SC
1558 if (radius < 0.0)
1559 radius = - radius * ((width < height) ? width : height);
1560 wxCoord xx = XLOG2DEVMAC(x);
1561 wxCoord yy = YLOG2DEVMAC(y);
1562 wxCoord ww = m_signX * XLOG2DEVREL(width);
1563 wxCoord hh = m_signY * YLOG2DEVREL(height);
75f0a06e 1564
613a24f7
SC
1565 // CMB: draw nothing if transformed w or h is 0
1566 if (ww == 0 || hh == 0)
1567 return;
97071cdb 1568
613a24f7
SC
1569 // CMB: handle -ve width and/or height
1570 if (ww < 0)
1571 {
1572 ww = -ww;
1573 xx = xx - ww;
1574 }
1575 if (hh < 0)
1576 {
1577 hh = -hh;
1578 yy = yy - hh;
1579 }
97071cdb 1580
20b69855
SC
1581 wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ;
1582 CGContextRef ctx = mctx->GetNativeContext() ;
ec4a2b5d 1583 AddRoundedRectToPath( ctx , CGRectMake( xx , yy , ww , hh ) , 16 ,16 ) ;
20b69855 1584 CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ;
613a24f7
SC
1585}
1586
b21d67a0 1587void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
613a24f7 1588{
b21d67a0 1589 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawEllipse - invalid DC") );
08c8555e
SC
1590
1591 if ( m_logicalFunction != wxCOPY )
1592 return ;
1593
613a24f7
SC
1594 wxCoord xx = XLOG2DEVMAC(x);
1595 wxCoord yy = YLOG2DEVMAC(y);
1596 wxCoord ww = m_signX * XLOG2DEVREL(width);
1597 wxCoord hh = m_signY * YLOG2DEVREL(height);
1598 // CMB: draw nothing if transformed w or h is 0
1599 if (ww == 0 || hh == 0)
1600 return;
97071cdb 1601
613a24f7
SC
1602 // CMB: handle -ve width and/or height
1603 if (ww < 0)
1604 {
1605 ww = -ww;
1606 xx = xx - ww;
1607 }
1608 if (hh < 0)
1609 {
1610 hh = -hh;
1611 yy = yy - hh;
1612 }
20b69855
SC
1613
1614 wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ;
1615 CGContextRef ctx = mctx->GetNativeContext() ;
72ba915e 1616 CGContextSaveGState( ctx ) ;
ec4a2b5d 1617 CGContextTranslateCTM( ctx, xx + ww / 2, yy + hh / 2 );
72ba915e 1618 CGContextScaleCTM( ctx , ww / 2 , hh / 2 ) ;
ec4a2b5d 1619 CGContextAddArc( ctx, 0, 0, 1, 0 , 2 * M_PI , 0 );
72ba915e
SC
1620 CGContextRestoreGState( ctx ) ;
1621 CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ;
613a24f7
SC
1622}
1623
b21d67a0 1624bool wxDC::CanDrawBitmap(void) const
613a24f7
SC
1625{
1626 return true ;
1627}
1628
b21d67a0 1629bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
613a24f7 1630 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
ec4a2b5d 1631 wxCoord xsrcMask, wxCoord ysrcMask )
613a24f7 1632{
b21d67a0
DS
1633 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoBlit - invalid DC") );
1634 wxCHECK_MSG( source->Ok(), false, wxT("wxDC(cg)::DoBlit - invalid source DC") );
75f0a06e 1635
613a24f7 1636 if ( logical_func == wxNO_OP )
97071cdb
DS
1637 return true ;
1638
613a24f7
SC
1639 if (xsrcMask == -1 && ysrcMask == -1)
1640 {
97071cdb
DS
1641 xsrcMask = xsrc;
1642 ysrcMask = ysrc;
613a24f7 1643 }
20b69855 1644
68654a82 1645 wxCoord yysrc = source->YLOG2DEVMAC(ysrc) ;
ec4a2b5d
DS
1646 wxCoord xxsrc = source->XLOG2DEVMAC(xsrc) ;
1647 wxCoord wwsrc = source->XLOG2DEVREL(width) ;
68654a82
SC
1648 wxCoord hhsrc = source->YLOG2DEVREL(height) ;
1649
1650 wxCoord yydest = YLOG2DEVMAC(ydest) ;
1651 wxCoord xxdest = XLOG2DEVMAC(xdest) ;
ec4a2b5d 1652 wxCoord wwdest = XLOG2DEVREL(width) ;
68654a82
SC
1653 wxCoord hhdest = YLOG2DEVREL(height) ;
1654
20b69855 1655 wxMemoryDC* memdc = dynamic_cast<wxMemoryDC*>(source) ;
a8f234d2 1656 if ( memdc && logical_func == wxCOPY )
613a24f7 1657 {
20b69855 1658 wxBitmap blit = memdc->GetSelectedObject() ;
b21d67a0 1659
20b69855
SC
1660 wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") ) ;
1661
20b69855
SC
1662 wxCoord bmpwidth = blit.GetWidth();
1663 wxCoord bmpheight = blit.GetHeight();
1664
68654a82
SC
1665 if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
1666 {
1667 wwsrc = wxMin( wwsrc , bmpwidth - xxsrc ) ;
1668 hhsrc = wxMin( hhsrc , bmpheight - yysrc ) ;
1669 if ( wwsrc > 0 && hhsrc > 0 )
1670 {
1671 if ( xxsrc >= 0 && yysrc >= 0 )
1672 {
1673 wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc ) ;
1674 blit = blit.GetSubBitmap( subrect ) ;
1675 }
1676 else
1677 {
1678 // in this case we'd probably have to adjust the different coordinates, but
1679 // we have to find out proper contract first
1680 blit = wxNullBitmap ;
1681 }
1682 }
1683 else
1684 {
1685 blit = wxNullBitmap ;
1686 }
1687 }
97071cdb 1688
68654a82 1689 if ( blit.Ok() )
613a24f7 1690 {
b60f5ca5 1691 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
68654a82
SC
1692 CGImageRef image = (CGImageRef)( blit.CGImageCreate() ) ;
1693 HIRect r = CGRectMake( xxdest , yydest , wwdest , hhdest ) ;
1694 HIViewDrawCGImage( cg , &r , image ) ;
1695 CGImageRelease( image ) ;
613a24f7 1696 }
20b69855
SC
1697 }
1698 else
1699 {
75f0a06e 1700#if 0
bc8f7aee 1701 CGContextRef cg = (wxMacCGContext*)(source->GetGraphicContext())->GetNativeContext() ;
68654a82 1702 void *data = CGBitmapContextGetData( cg ) ;
75f0a06e
DS
1703#endif
1704
97071cdb 1705 return false ; // wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") ) ;
613a24f7 1706 }
97071cdb
DS
1707
1708 return true;
613a24f7
SC
1709}
1710
b21d67a0 1711void wxDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
613a24f7
SC
1712 double angle)
1713{
b21d67a0
DS
1714 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRotatedText - invalid DC") );
1715 wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoDrawRotatedText - no valid font set") );
613a24f7
SC
1716
1717 if ( str.Length() == 0 )
1718 return ;
08c8555e
SC
1719 if ( m_logicalFunction != wxCOPY )
1720 return ;
1721
613a24f7
SC
1722 OSStatus status = noErr ;
1723 ATSUTextLayout atsuLayout ;
1724 UniCharCount chars = str.Length() ;
1725 UniChar* ubuf = NULL ;
b21d67a0 1726
613a24f7 1727#if SIZEOF_WCHAR_T == 4
75f0a06e 1728 wxMBConvUTF16 converter ;
613a24f7 1729#if wxUSE_UNICODE
75f0a06e
DS
1730 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
1731 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1732 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
613a24f7
SC
1733#else
1734 const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
b21d67a0 1735 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
75f0a06e
DS
1736 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1737 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
613a24f7
SC
1738#endif
1739 chars = unicharlen / 2 ;
1740#else
1741#if wxUSE_UNICODE
1742 ubuf = (UniChar*) str.wc_str() ;
1743#else
1744 wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1745 chars = wxWcslen( wchar.data() ) ;
1746 ubuf = (UniChar*) wchar.data() ;
1747#endif
1748#endif
1749
1750 int drawX = XLOG2DEVMAC(x) ;
1751 int drawY = YLOG2DEVMAC(y) ;
1752
613a24f7
SC
1753 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1754 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1755
1756 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") );
613a24f7 1757
28dd2407
SC
1758 status = ::ATSUSetTransientFontMatching( atsuLayout , true ) ;
1759 wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") );
613a24f7 1760
28dd2407 1761 int iAngle = int( angle );
613a24f7
SC
1762 if ( abs(iAngle) > 0 )
1763 {
1764 Fixed atsuAngle = IntToFixed( iAngle ) ;
1765 ATSUAttributeTag atsuTags[] =
1766 {
1767 kATSULineRotationTag ,
1768 } ;
b21d67a0 1769 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
613a24f7
SC
1770 {
1771 sizeof( Fixed ) ,
1772 } ;
b21d67a0 1773 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
613a24f7
SC
1774 {
1775 &atsuAngle ,
1776 } ;
b21d67a0 1777 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
613a24f7
SC
1778 atsuTags, atsuSizes, atsuValues ) ;
1779 }
75f0a06e 1780
613a24f7 1781 {
b60f5ca5 1782 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
613a24f7
SC
1783 ATSUAttributeTag atsuTags[] =
1784 {
1785 kATSUCGContextTag ,
1786 } ;
b21d67a0 1787 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
613a24f7
SC
1788 {
1789 sizeof( CGContextRef ) ,
1790 } ;
b21d67a0 1791 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
613a24f7
SC
1792 {
1793 &cgContext ,
1794 } ;
b21d67a0 1795 status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag),
613a24f7
SC
1796 atsuTags, atsuSizes, atsuValues ) ;
1797 }
1798
b21d67a0
DS
1799 ATSUTextMeasurement textBefore, textAfter ;
1800 ATSUTextMeasurement ascent, descent ;
613a24f7
SC
1801
1802 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1803 &textBefore , &textAfter, &ascent , &descent );
b21d67a0 1804
613a24f7
SC
1805 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1806
1807 Rect rect ;
ec4a2b5d 1808
613a24f7
SC
1809 if ( m_backgroundMode == wxSOLID )
1810 {
20b69855
SC
1811 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1812 path->MoveToPoint(
613a24f7
SC
1813 drawX ,
1814 drawY ) ;
20b69855 1815 path->AddLineToPoint(
b21d67a0
DS
1816 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) ,
1817 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) ) ;
20b69855 1818 path->AddLineToPoint(
b21d67a0
DS
1819 (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1820 (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ) ;
20b69855 1821 path->AddLineToPoint(
b21d67a0
DS
1822 (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) ,
1823 (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ) ;
613a24f7 1824
20b69855
SC
1825 m_graphicContext->FillPath( path , m_textBackgroundColour ) ;
1826 delete path ;
613a24f7
SC
1827 }
1828
b21d67a0
DS
1829 drawX += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent));
1830 drawY += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent));
20b69855 1831
613a24f7
SC
1832 status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1833 IntToFixed(drawX) , IntToFixed(drawY) , &rect );
1834 wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") );
1835
b60f5ca5
SC
1836 CGContextSaveGState(((wxMacCGContext*)(m_graphicContext))->GetNativeContext());
1837 CGContextTranslateCTM(((wxMacCGContext*)(m_graphicContext))->GetNativeContext(), drawX, drawY);
1838 CGContextScaleCTM(((wxMacCGContext*)(m_graphicContext))->GetNativeContext(), 1, -1);
613a24f7
SC
1839 status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1840 IntToFixed(0) , IntToFixed(0) );
b21d67a0 1841
613a24f7 1842 wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") );
b21d67a0 1843
b60f5ca5 1844 CGContextRestoreGState( ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ) ;
613a24f7
SC
1845
1846 CalcBoundingBox(XDEV2LOG(rect.left), YDEV2LOG(rect.top) );
1847 CalcBoundingBox(XDEV2LOG(rect.right), YDEV2LOG(rect.bottom) );
1848
1849 ::ATSUDisposeTextLayout(atsuLayout);
97071cdb 1850
613a24f7
SC
1851#if SIZEOF_WCHAR_T == 4
1852 free( ubuf ) ;
1853#endif
613a24f7
SC
1854}
1855
b21d67a0 1856void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
613a24f7 1857{
b21d67a0 1858 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawText - invalid DC") );
75f0a06e 1859
613a24f7
SC
1860 DoDrawRotatedText( strtext , x , y , 0.0 ) ;
1861}
1862
b21d67a0 1863bool wxDC::CanGetTextExtent() const
613a24f7 1864{
b21d67a0 1865 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::CanGetTextExtent - invalid DC") );
75f0a06e 1866
613a24f7
SC
1867 return true ;
1868}
1869
b21d67a0 1870void wxDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
613a24f7
SC
1871 wxCoord *descent, wxCoord *externalLeading ,
1872 wxFont *theFont ) const
1873{
b21d67a0 1874 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoGetTextExtent - invalid DC") );
75f0a06e 1875
613a24f7
SC
1876 wxFont formerFont = m_font ;
1877 if ( theFont )
1878 {
b21d67a0 1879 // work around the const-ness
613a24f7 1880 *((wxFont*)(&m_font)) = *theFont ;
20b69855 1881 MacInstallFont() ;
613a24f7
SC
1882 }
1883
613a24f7
SC
1884 if ( str.Length() == 0 )
1885 return ;
1886
b21d67a0 1887 wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") ) ;
20b69855 1888
613a24f7
SC
1889 OSStatus status = noErr ;
1890 ATSUTextLayout atsuLayout ;
1891 UniCharCount chars = str.Length() ;
1892 UniChar* ubuf = NULL ;
b21d67a0 1893
613a24f7 1894#if SIZEOF_WCHAR_T == 4
75f0a06e 1895 wxMBConvUTF16 converter ;
613a24f7 1896#if wxUSE_UNICODE
75f0a06e
DS
1897 size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ;
1898 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1899 converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ;
613a24f7
SC
1900#else
1901 const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
b21d67a0 1902 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
75f0a06e
DS
1903 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1904 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
613a24f7
SC
1905#endif
1906 chars = unicharlen / 2 ;
1907#else
1908#if wxUSE_UNICODE
1909 ubuf = (UniChar*) str.wc_str() ;
1910#else
1911 wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ;
1912 chars = wxWcslen( wchar.data() ) ;
1913 ubuf = (UniChar*) wchar.data() ;
1914#endif
1915#endif
1916
613a24f7
SC
1917 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1918 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1919
1920 wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") );
1921
b21d67a0
DS
1922 ATSUTextMeasurement textBefore, textAfter ;
1923 ATSUTextMeasurement textAscent, textDescent ;
613a24f7
SC
1924
1925 status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd,
1926 &textBefore , &textAfter, &textAscent , &textDescent );
1927
1928 if ( height )
1929 *height = YDEV2LOGREL( FixedToInt(textAscent + textDescent) ) ;
1930 if ( descent )
1931 *descent =YDEV2LOGREL( FixedToInt(textDescent) );
1932 if ( externalLeading )
1933 *externalLeading = 0 ;
1934 if ( width )
1935 *width = XDEV2LOGREL( FixedToInt(textAfter - textBefore) ) ;
1936
1937 ::ATSUDisposeTextLayout(atsuLayout);
b21d67a0 1938
613a24f7
SC
1939#if SIZEOF_WCHAR_T == 4
1940 free( ubuf ) ;
1941#endif
b21d67a0 1942
613a24f7
SC
1943 if ( theFont )
1944 {
1945 // work around the constness
1946 *((wxFont*)(&m_font)) = formerFont ;
20b69855 1947 MacInstallFont() ;
613a24f7
SC
1948 }
1949}
1950
613a24f7
SC
1951bool wxDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
1952{
b21d67a0 1953 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoGetPartialTextExtents - invalid DC") );
613a24f7
SC
1954
1955 widths.Empty();
1956 widths.Add(0, text.Length());
1957
1958 if (text.Length() == 0)
1959 return false;
b21d67a0 1960
764e6694
KO
1961 ATSUTextLayout atsuLayout ;
1962 UniCharCount chars = text.Length() ;
1963 UniChar* ubuf = NULL ;
b21d67a0 1964
764e6694 1965#if SIZEOF_WCHAR_T == 4
d9d488cf 1966 wxMBConvUTF16 converter ;
764e6694
KO
1967#if wxUSE_UNICODE
1968 size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 ) ;
1969 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1970 converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 ) ;
1971#else
1972 const wxWCharBuffer wchar = text.wc_str( wxConvLocal ) ;
ec4a2b5d 1973 size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ;
764e6694
KO
1974 ubuf = (UniChar*) malloc( unicharlen + 2 ) ;
1975 converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ;
1976#endif
1977 chars = unicharlen / 2 ;
1978#else
1979#if wxUSE_UNICODE
1980 ubuf = (UniChar*) text.wc_str() ;
1981#else
1982 wxWCharBuffer wchar = text.wc_str( wxConvLocal ) ;
1983 chars = wxWcslen( wchar.data() ) ;
1984 ubuf = (UniChar*) wchar.data() ;
1985#endif
1986#endif
1987
97071cdb 1988 OSStatus status;
764e6694
KO
1989 status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 ,
1990 &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ;
1991
97071cdb
DS
1992 for ( int pos = 0; pos < (int)chars; pos ++ )
1993 {
1994 unsigned long actualNumberOfBounds = 0;
1995 ATSTrapezoid glyphBounds;
1996
1997 // We get a single bound, since the text should only require one. If it requires more, there is an issue
1998 OSStatus result;
1999 result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1,
2000 kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds );
2001 if (result != noErr || actualNumberOfBounds != 1 )
2002 return false;
2003
2004 widths[pos] = XDEV2LOGREL(FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x ));
2005 //unsigned char uch = s[i];
2006 }
2007
764e6694
KO
2008 ::ATSUDisposeTextLayout(atsuLayout);
2009 return true;
613a24f7
SC
2010}
2011
b21d67a0 2012wxCoord wxDC::GetCharWidth(void) const
613a24f7
SC
2013{
2014 wxCoord width ;
b21d67a0 2015 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL ) ;
75f0a06e 2016
613a24f7
SC
2017 return width ;
2018}
2019
b21d67a0 2020wxCoord wxDC::GetCharHeight(void) const
613a24f7
SC
2021{
2022 wxCoord height ;
b21d67a0 2023 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL ) ;
75f0a06e 2024
613a24f7
SC
2025 return height ;
2026}
2027
b21d67a0 2028void wxDC::Clear(void)
613a24f7 2029{
b21d67a0 2030 wxCHECK_RET( Ok(), wxT("wxDC(cg)::Clear - invalid DC") );
613a24f7 2031
75f0a06e 2032 if (m_backgroundBrush.Ok() && m_backgroundBrush.GetStyle() != wxTRANSPARENT)
ec4a2b5d 2033 {
b014adcc 2034 HIRect rect = CGRectMake( -10000 , -10000 , 20000 , 20000 ) ;
b60f5ca5 2035 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
b21d67a0 2036 switch ( m_backgroundBrush.MacGetBrushKind() )
613a24f7
SC
2037 {
2038 case kwxMacBrushTheme :
2039 {
a63b4755
SC
2040#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
2041 if ( HIThemeSetFill != 0 )
2042 {
72ba915e 2043 HIThemeSetFill( m_backgroundBrush.MacGetTheme(), NULL, cg, kHIThemeOrientationNormal );
a63b4755
SC
2044 CGContextFillRect(cg, rect);
2045
2046 }
2047 else
2048#endif
2049 {
75f0a06e 2050 RGBColor color;
72ba915e
SC
2051 GetThemeBrushAsColor( m_backgroundBrush.MacGetTheme(), 32, true, &color );
2052 CGContextSetRGBFillColor( cg, (float) color.red / 65536,
97071cdb
DS
2053 (float) color.green / 65536, (float) color.blue / 65536, 1 );
2054 CGContextFillRect( cg, rect );
a63b4755 2055 }
72ba915e 2056
a63b4755
SC
2057 // reset to normal value
2058 RGBColor col = MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
72ba915e 2059 CGContextSetRGBFillColor( cg, col.red / 65536.0, col.green / 65536.0, col.blue / 65536.0, 1.0 );
613a24f7 2060 }
97071cdb
DS
2061 break ;
2062
613a24f7
SC
2063 case kwxMacBrushThemeBackground :
2064 {
a63b4755 2065 wxFAIL_MSG( wxT("There shouldn't be theme backgrounds under Quartz") ) ;
75f0a06e 2066
b014adcc 2067#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1688bb38 2068 if ( UMAGetSystemVersion() >= 0x1030 )
b014adcc
SC
2069 {
2070 HIThemeBackgroundDrawInfo drawInfo ;
2071 drawInfo.version = 0 ;
2072 drawInfo.state = kThemeStateActive ;
ec4a2b5d 2073 drawInfo.kind = m_backgroundBrush.MacGetThemeBackground( NULL ) ;
b014adcc 2074 if ( drawInfo.kind == kThemeBackgroundMetal )
ec4a2b5d
DS
2075 {
2076 HIThemeDrawBackground( &rect, &drawInfo, cg, kHIThemeOrientationNormal ) ;
2077 HIThemeApplyBackground( &rect, &drawInfo, cg, kHIThemeOrientationNormal ) ;
2078 }
b014adcc
SC
2079 }
2080#endif
613a24f7 2081 }
97071cdb
DS
2082 break ;
2083
613a24f7 2084 case kwxMacBrushColour :
97071cdb 2085 {
ec4a2b5d
DS
2086 // FIXME: doesn't correctly render stippled brushes !!
2087 // FIXME: should this be replaced by ::SetBrush() ??
2088
97071cdb
DS
2089 RGBColor col = MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel()) ;
2090 CGContextSetRGBFillColor( cg , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
2091 CGContextFillRect(cg, rect);
613a24f7 2092
97071cdb
DS
2093 // reset to normal value
2094 col = MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
2095 CGContextSetRGBFillColor( cg , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
2096 }
2097 break ;
2098
75f0a06e
DS
2099 default :
2100 wxFAIL_MSG( wxT("unknown brush kind") ) ;
97071cdb 2101 break ;
613a24f7
SC
2102 }
2103 }
2104}
2105
2106void wxDC::MacInstallFont() const
2107{
b21d67a0 2108 wxCHECK_RET( Ok(), wxT("wxDC(cg)::MacInstallFont - invalid DC") );
613a24f7 2109
97071cdb 2110 if ( m_macATSUIStyle )
613a24f7 2111 {
20b69855
SC
2112 ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle);
2113 m_macATSUIStyle = NULL ;
613a24f7 2114 }
20b69855 2115
68654a82 2116 if ( m_font.Ok() )
613a24f7 2117 {
ec4a2b5d
DS
2118 OSStatus status ;
2119
68654a82 2120 status = ATSUCreateAndCopyStyle( (ATSUStyle) m_font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle ) ;
b21d67a0 2121
ec4a2b5d 2122 wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") ) ;
20b69855 2123
68654a82
SC
2124 Fixed atsuSize = IntToFixed( int(m_scaleY * m_font.MacGetFontSize()) ) ;
2125 RGBColor atsuColor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel() ) ;
2126 ATSUAttributeTag atsuTags[] =
2127 {
2128 kATSUSizeTag ,
2129 kATSUColorTag ,
2130 } ;
b21d67a0 2131 ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
68654a82
SC
2132 {
2133 sizeof( Fixed ) ,
2134 sizeof( RGBColor ) ,
2135 } ;
b21d67a0 2136 ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] =
68654a82
SC
2137 {
2138 &atsuSize ,
2139 &atsuColor ,
2140 } ;
b21d67a0 2141
ec4a2b5d
DS
2142 status = ::ATSUSetAttributes(
2143 (ATSUStyle)m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) ,
68654a82
SC
2144 atsuTags, atsuSizes, atsuValues);
2145
ec4a2b5d 2146 wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ) ;
68654a82 2147 }
613a24f7
SC
2148}
2149
eb1a7cf9
DS
2150#pragma mark -
2151
613a24f7
SC
2152// ---------------------------------------------------------------------------
2153// coordinates transformations
2154// ---------------------------------------------------------------------------
2155
2156wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2157{
2158 return ((wxDC *)this)->XDEV2LOG(x);
2159}
2160
2161wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2162{
2163 return ((wxDC *)this)->YDEV2LOG(y);
2164}
2165
2166wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2167{
2168 return ((wxDC *)this)->XDEV2LOGREL(x);
2169}
2170
2171wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2172{
2173 return ((wxDC *)this)->YDEV2LOGREL(y);
2174}
2175
2176wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2177{
2178 return ((wxDC *)this)->XLOG2DEV(x);
2179}
2180
2181wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2182{
2183 return ((wxDC *)this)->YLOG2DEV(y);
2184}
2185
2186wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2187{
2188 return ((wxDC *)this)->XLOG2DEVREL(x);
2189}
2190
2191wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2192{
2193 return ((wxDC *)this)->YLOG2DEVREL(y);
2194}
20b69855
SC
2195
2196#endif // wxMAC_USE_CORE_GRAPHICS
2197