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