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