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