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