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