]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dccg.cpp
adding the 0.5 offset for odd pen widths
[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 }
1354 }
1355
1356 void wxDC::SetTextBackground( const wxColour &col )
1357 {
1358 wxCHECK_RET( Ok(), wxT("wxDC(cg)::SetTextBackground - invalid DC") );
1359
1360 m_textBackgroundColour = col;
1361 }
1362
1363 void wxDC::SetMapMode( int mode )
1364 {
1365 switch (mode)
1366 {
1367 case wxMM_TWIPS:
1368 SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
1369 break;
1370
1371 case wxMM_POINTS:
1372 SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
1373 break;
1374
1375 case wxMM_METRIC:
1376 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
1377 break;
1378
1379 case wxMM_LOMETRIC:
1380 SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
1381 break;
1382
1383 case wxMM_TEXT:
1384 default:
1385 SetLogicalScale( 1.0, 1.0 );
1386 break;
1387 }
1388
1389 if (mode != wxMM_TEXT)
1390 {
1391 m_needComputeScaleX =
1392 m_needComputeScaleY = true;
1393 }
1394 }
1395
1396 void wxDC::SetUserScale( double x, double y )
1397 {
1398 // allow negative ? -> no
1399 m_userScaleX = x;
1400 m_userScaleY = y;
1401 ComputeScaleAndOrigin();
1402 }
1403
1404 void wxDC::SetLogicalScale( double x, double y )
1405 {
1406 // allow negative ?
1407 m_logicalScaleX = x;
1408 m_logicalScaleY = y;
1409 ComputeScaleAndOrigin();
1410 }
1411
1412 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
1413 {
1414 m_logicalOriginX = x * m_signX; // is this still correct ?
1415 m_logicalOriginY = y * m_signY;
1416 ComputeScaleAndOrigin();
1417 }
1418
1419 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
1420 {
1421 m_externalDeviceOriginX = x;
1422 m_externalDeviceOriginY = y;
1423 ComputeScaleAndOrigin();
1424 }
1425
1426 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1427 {
1428 m_signX = (xLeftRight ? 1 : -1);
1429 m_signY = (yBottomUp ? -1 : 1);
1430 ComputeScaleAndOrigin();
1431 }
1432
1433 wxSize wxDC::GetPPI() const
1434 {
1435 return wxSize(72, 72);
1436 }
1437
1438 int wxDC::GetDepth() const
1439 {
1440 return 32;
1441 }
1442
1443 void wxDC::ComputeScaleAndOrigin()
1444 {
1445 // CMB: copy scale to see if it changes
1446 double origScaleX = m_scaleX;
1447 double origScaleY = m_scaleY;
1448 m_scaleX = m_logicalScaleX * m_userScaleX;
1449 m_scaleY = m_logicalScaleY * m_userScaleY;
1450 m_deviceOriginX = m_externalDeviceOriginX;
1451 m_deviceOriginY = m_externalDeviceOriginY;
1452
1453 // CMB: if scale has changed call SetPen to recalulate the line width
1454 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
1455 {
1456 // this is a bit artificial, but we need to force wxDC to think
1457 // the pen has changed
1458 wxPen pen( GetPen() );
1459
1460 m_pen = wxNullPen;
1461 SetPen( pen );
1462 }
1463 }
1464
1465 void wxDC::SetPalette( const wxPalette& palette )
1466 {
1467 }
1468
1469 void wxDC::SetBackgroundMode( int mode )
1470 {
1471 m_backgroundMode = mode ;
1472 }
1473
1474 void wxDC::SetFont( const wxFont &font )
1475 {
1476 m_font = font;
1477 if ( m_graphicContext )
1478 m_graphicContext->SetFont( font ) ;
1479 }
1480
1481 void wxDC::SetPen( const wxPen &pen )
1482 {
1483 if ( m_pen == pen )
1484 return ;
1485
1486 m_pen = pen;
1487 if ( m_graphicContext )
1488 {
1489 if ( m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT )
1490 {
1491 m_graphicContext->SetPen( m_pen ) ;
1492 }
1493 else
1494 {
1495 // we have to compensate for moved device origins etc. otherwise patterned pens are standing still
1496 // eg when using a wxScrollWindow and scrolling around
1497 int origX = XLOG2DEVMAC( 0 ) ;
1498 int origY = YLOG2DEVMAC( 0 ) ;
1499 m_graphicContext->Translate( origX , origY ) ;
1500 m_graphicContext->SetPen( m_pen ) ;
1501 m_graphicContext->Translate( -origX , -origY ) ;
1502 }
1503 }
1504 }
1505
1506 void wxDC::SetBrush( const wxBrush &brush )
1507 {
1508 if (m_brush == brush)
1509 return;
1510
1511 m_brush = brush;
1512 if ( m_graphicContext )
1513 {
1514 if ( brush.GetStyle() == wxSOLID || brush.GetStyle() == wxTRANSPARENT )
1515 {
1516 m_graphicContext->SetBrush( m_brush ) ;
1517 }
1518 else
1519 {
1520 // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still
1521 // eg when using a wxScrollWindow and scrolling around
1522 int origX = XLOG2DEVMAC(0) ;
1523 int origY = YLOG2DEVMAC(0) ;
1524 m_graphicContext->Translate( origX , origY ) ;
1525 m_graphicContext->SetBrush( m_brush ) ;
1526 m_graphicContext->Translate( -origX , -origY ) ;
1527 }
1528 }
1529 }
1530
1531 void wxDC::SetBackground( const wxBrush &brush )
1532 {
1533 if (m_backgroundBrush == brush)
1534 return;
1535
1536 m_backgroundBrush = brush;
1537 if (!m_backgroundBrush.Ok())
1538 return;
1539 }
1540
1541 void wxDC::SetLogicalFunction( int function )
1542 {
1543 if (m_logicalFunction == function)
1544 return;
1545
1546 m_logicalFunction = function ;
1547 #if wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1548 CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
1549 if ( m_logicalFunction == wxCOPY )
1550 CGContextSetBlendMode( cgContext, kCGBlendModeNormal ) ;
1551 else if ( m_logicalFunction == wxINVERT )
1552 CGContextSetBlendMode( cgContext, kCGBlendModeExclusion ) ;
1553 else
1554 CGContextSetBlendMode( cgContext, kCGBlendModeNormal ) ;
1555 #endif
1556 }
1557
1558 extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y,
1559 const wxColour & col, int style);
1560
1561 bool wxDC::DoFloodFill(wxCoord x, wxCoord y,
1562 const wxColour& col, int style)
1563 {
1564 return wxDoFloodFill(this, x, y, col, style);
1565 }
1566
1567 bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const
1568 {
1569 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoGetPixel - invalid DC") );
1570
1571 wxMacPortSaver helper((CGrafPtr)m_macPort) ;
1572 RGBColor colour;
1573
1574 // NB: GetCPixel is a deprecated QD call, and a slow one at that
1575 GetCPixel(
1576 XLOG2DEVMAC(x) + m_macLocalOriginInPort.x - m_macLocalOrigin.x,
1577 YLOG2DEVMAC(y) + m_macLocalOriginInPort.y - m_macLocalOrigin.y, &colour );
1578
1579 // convert from Mac colour to wx
1580 col->Set( colour.red >> 8, colour.green >> 8, colour.blue >> 8 );
1581
1582 return true ;
1583 }
1584
1585 void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
1586 {
1587 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawLine - invalid DC") );
1588
1589 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1590 if ( m_logicalFunction != wxCOPY )
1591 return ;
1592 #endif
1593
1594 wxCoord xx1 = XLOG2DEVMAC(x1) ;
1595 wxCoord yy1 = YLOG2DEVMAC(y1) ;
1596 wxCoord xx2 = XLOG2DEVMAC(x2) ;
1597 wxCoord yy2 = YLOG2DEVMAC(y2) ;
1598
1599 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1600 path->MoveToPoint( xx1, yy1 ) ;
1601 path->AddLineToPoint( xx2 , yy2 ) ;
1602 path->CloseSubpath() ;
1603 m_graphicContext->StrokePath( path ) ;
1604 delete path ;
1605
1606 CalcBoundingBox(x1, y1);
1607 CalcBoundingBox(x2, y2);
1608 }
1609
1610 void wxDC::DoCrossHair( wxCoord x, wxCoord y )
1611 {
1612 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoCrossHair - invalid DC") );
1613
1614 if ( m_logicalFunction != wxCOPY )
1615 return ;
1616
1617 int w = 0, h = 0;
1618
1619 GetSize( &w, &h );
1620 wxCoord xx = XLOG2DEVMAC(x);
1621 wxCoord yy = YLOG2DEVMAC(y);
1622
1623 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1624 path->MoveToPoint( XLOG2DEVMAC(0), yy ) ;
1625 path->AddLineToPoint( XLOG2DEVMAC(w), yy ) ;
1626 path->CloseSubpath() ;
1627 path->MoveToPoint( xx, YLOG2DEVMAC(0) ) ;
1628 path->AddLineToPoint( xx, YLOG2DEVMAC(h) ) ;
1629 path->CloseSubpath() ;
1630 m_graphicContext->StrokePath( path ) ;
1631 delete path ;
1632
1633 CalcBoundingBox(x, y);
1634 CalcBoundingBox(x + w, y + h);
1635 }
1636
1637 void wxDC::DoDrawArc( wxCoord x1, wxCoord y1,
1638 wxCoord x2, wxCoord y2,
1639 wxCoord xc, wxCoord yc )
1640 {
1641 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawArc - invalid DC") );
1642
1643 if ( m_logicalFunction != wxCOPY )
1644 return ;
1645
1646 wxCoord xx1 = XLOG2DEVMAC(x1);
1647 wxCoord yy1 = YLOG2DEVMAC(y1);
1648 wxCoord xx2 = XLOG2DEVMAC(x2);
1649 wxCoord yy2 = YLOG2DEVMAC(y2);
1650 wxCoord xxc = XLOG2DEVMAC(xc);
1651 wxCoord yyc = YLOG2DEVMAC(yc);
1652
1653 double dx = xx1 - xxc;
1654 double dy = yy1 - yyc;
1655 double radius = sqrt((double)(dx * dx + dy * dy));
1656 wxCoord rad = (wxCoord)radius;
1657 double sa, ea;
1658 if (xx1 == xx2 && yy1 == yy2)
1659 {
1660 sa = 0.0;
1661 ea = 360.0;
1662 }
1663 else if (radius == 0.0)
1664 {
1665 sa = ea = 0.0;
1666 }
1667 else
1668 {
1669 sa = (xx1 - xxc == 0) ?
1670 (yy1 - yyc < 0) ? 90.0 : -90.0 :
1671 -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG;
1672 ea = (xx2 - xxc == 0) ?
1673 (yy2 - yyc < 0) ? 90.0 : -90.0 :
1674 -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG;
1675 }
1676
1677 bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
1678
1679 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1680 m_graphicContext->PushState() ;
1681 m_graphicContext->Translate( xxc, yyc ) ;
1682 m_graphicContext->Scale( 1, -1 ) ;
1683 if ( fill )
1684 path->MoveToPoint( 0, 0 ) ;
1685 path->AddArc( 0, 0, rad , DegToRad(sa) , DegToRad(ea), false ) ;
1686 if ( fill )
1687 path->AddLineToPoint( 0, 0 ) ;
1688 m_graphicContext->DrawPath( path ) ;
1689 m_graphicContext->PopState() ;
1690 delete path ;
1691 }
1692
1693 void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h,
1694 double sa, double ea )
1695 {
1696 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawEllipticArc - invalid DC") );
1697
1698 if ( m_logicalFunction != wxCOPY )
1699 return ;
1700
1701 wxCoord xx = XLOG2DEVMAC(x);
1702 wxCoord yy = YLOG2DEVMAC(y);
1703 wxCoord ww = m_signX * XLOG2DEVREL(w);
1704 wxCoord hh = m_signY * YLOG2DEVREL(h);
1705
1706 // handle -ve width and/or height
1707 if (ww < 0)
1708 {
1709 ww = -ww;
1710 xx = xx - ww;
1711 }
1712 if (hh < 0)
1713 {
1714 hh = -hh;
1715 yy = yy - hh;
1716 }
1717
1718 bool fill = m_brush.GetStyle() != wxTRANSPARENT ;
1719
1720 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1721 m_graphicContext->PushState() ;
1722 m_graphicContext->Translate( xx + ww / 2, yy + hh / 2 ) ;
1723 m_graphicContext->Scale( 1 * ww / 2 , -1 * hh / 2 ) ;
1724 if ( fill )
1725 path->MoveToPoint( 0, 0 ) ;
1726 path->AddArc( 0, 0, 1 , DegToRad(sa) , DegToRad(ea), false ) ;
1727 if ( fill )
1728 path->AddLineToPoint( 0, 0 ) ;
1729 m_graphicContext->DrawPath( path ) ;
1730 m_graphicContext->PopState() ;
1731 delete path ;
1732 }
1733
1734 void wxDC::DoDrawPoint( wxCoord x, wxCoord y )
1735 {
1736 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawPoint - invalid DC") );
1737
1738 DoDrawLine( x , y , x + 1 , y + 1 ) ;
1739 }
1740
1741 void wxDC::DoDrawLines(int n, wxPoint points[],
1742 wxCoord xoffset, wxCoord yoffset)
1743 {
1744 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawLines - invalid DC") );
1745
1746 #if !wxMAC_USE_CORE_GRAPHICS_BLEND_MODES
1747 if ( m_logicalFunction != wxCOPY )
1748 return ;
1749 #endif
1750
1751 wxCoord x1, x2 , y1 , y2 ;
1752 x1 = XLOG2DEVMAC(points[0].x + xoffset);
1753 y1 = YLOG2DEVMAC(points[0].y + yoffset);
1754 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1755 path->MoveToPoint( x1 , y1 ) ;
1756 for (int i = 1; i < n; i++)
1757 {
1758 x2 = XLOG2DEVMAC(points[i].x + xoffset);
1759 y2 = YLOG2DEVMAC(points[i].y + yoffset);
1760
1761 path->AddLineToPoint( x2 , y2 ) ;
1762 }
1763
1764 m_graphicContext->StrokePath( path ) ;
1765 delete path ;
1766 }
1767
1768 #if wxUSE_SPLINES
1769 void wxDC::DoDrawSpline(wxList *points)
1770 {
1771 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawSpline - invalid DC") );
1772
1773 if ( m_logicalFunction != wxCOPY )
1774 return ;
1775
1776 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1777
1778 wxList::compatibility_iterator node = points->GetFirst();
1779 if (node == wxList::compatibility_iterator())
1780 // empty list
1781 return;
1782
1783 wxPoint *p = (wxPoint *)node->GetData();
1784
1785 wxCoord x1 = p->x;
1786 wxCoord y1 = p->y;
1787
1788 node = node->GetNext();
1789 p = (wxPoint *)node->GetData();
1790
1791 wxCoord x2 = p->x;
1792 wxCoord y2 = p->y;
1793 wxCoord cx1 = ( x1 + x2 ) / 2;
1794 wxCoord cy1 = ( y1 + y2 ) / 2;
1795
1796 path->MoveToPoint( XLOG2DEVMAC( x1 ) , YLOG2DEVMAC( y1 ) ) ;
1797 path->AddLineToPoint( XLOG2DEVMAC( cx1 ) , YLOG2DEVMAC( cy1 ) ) ;
1798
1799 #if !wxUSE_STL
1800 while ((node = node->GetNext()) != NULL)
1801 #else
1802 while ((node = node->GetNext()))
1803 #endif // !wxUSE_STL
1804 {
1805 p = (wxPoint *)node->GetData();
1806 x1 = x2;
1807 y1 = y2;
1808 x2 = p->x;
1809 y2 = p->y;
1810 wxCoord cx4 = (x1 + x2) / 2;
1811 wxCoord cy4 = (y1 + y2) / 2;
1812
1813 path->AddQuadCurveToPoint(
1814 XLOG2DEVMAC( x1 ) , YLOG2DEVMAC( y1 ) ,
1815 XLOG2DEVMAC( cx4 ) , YLOG2DEVMAC( cy4 ) ) ;
1816
1817 cx1 = cx4;
1818 cy1 = cy4;
1819 }
1820
1821 path->AddLineToPoint( XLOG2DEVMAC( x2 ) , YLOG2DEVMAC( y2 ) ) ;
1822
1823 m_graphicContext->StrokePath( path ) ;
1824 delete path ;
1825 }
1826 #endif
1827
1828 void wxDC::DoDrawPolygon( int n, wxPoint points[],
1829 wxCoord xoffset, wxCoord yoffset,
1830 int fillStyle )
1831 {
1832 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawPolygon - invalid DC") );
1833
1834 if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) )
1835 return ;
1836 if ( m_logicalFunction != wxCOPY )
1837 return ;
1838
1839 wxCoord x1, x2 , y1 , y2 ;
1840 x2 = x1 = XLOG2DEVMAC(points[0].x + xoffset);
1841 y2 = y1 = YLOG2DEVMAC(points[0].y + yoffset);
1842
1843 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1844 path->MoveToPoint( x1 , y1 ) ;
1845 for (int i = 1; i < n; i++)
1846 {
1847 x2 = XLOG2DEVMAC(points[i].x + xoffset);
1848 y2 = YLOG2DEVMAC(points[i].y + yoffset);
1849
1850 path->AddLineToPoint( x2 , y2 ) ;
1851 }
1852
1853 if ( x1 != x2 || y1 != y2 )
1854 path->AddLineToPoint( x1, y1 ) ;
1855
1856 path->CloseSubpath() ;
1857 m_graphicContext->DrawPath( path , fillStyle ) ;
1858
1859 delete path ;
1860 }
1861
1862 void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1863 {
1864 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRectangle - invalid DC") );
1865
1866 if ( m_logicalFunction != wxCOPY )
1867 return ;
1868
1869 wxCoord xx = XLOG2DEVMAC(x);
1870 wxCoord yy = YLOG2DEVMAC(y);
1871 wxCoord ww = m_signX * XLOG2DEVREL(width);
1872 wxCoord hh = m_signY * YLOG2DEVREL(height);
1873
1874 // CMB: draw nothing if transformed w or h is 0
1875 if (ww == 0 || hh == 0)
1876 return;
1877
1878 // CMB: handle -ve width and/or height
1879 if (ww < 0)
1880 {
1881 ww = -ww;
1882 xx = xx - ww;
1883 }
1884 if (hh < 0)
1885 {
1886 hh = -hh;
1887 yy = yy - hh;
1888 }
1889
1890 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1891 path->AddRectangle( xx , yy , ww , hh ) ;
1892 m_graphicContext->DrawPath( path ) ;
1893 delete path ;
1894 }
1895
1896 void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y,
1897 wxCoord width, wxCoord height,
1898 double radius)
1899 {
1900 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRoundedRectangle - invalid DC") );
1901
1902 if ( m_logicalFunction != wxCOPY )
1903 return ;
1904
1905 if (radius < 0.0)
1906 radius = - radius * ((width < height) ? width : height);
1907 wxCoord xx = XLOG2DEVMAC(x);
1908 wxCoord yy = YLOG2DEVMAC(y);
1909 wxCoord ww = m_signX * XLOG2DEVREL(width);
1910 wxCoord hh = m_signY * YLOG2DEVREL(height);
1911
1912 // CMB: draw nothing if transformed w or h is 0
1913 if (ww == 0 || hh == 0)
1914 return;
1915
1916 // CMB: handle -ve width and/or height
1917 if (ww < 0)
1918 {
1919 ww = -ww;
1920 xx = xx - ww;
1921 }
1922 if (hh < 0)
1923 {
1924 hh = -hh;
1925 yy = yy - hh;
1926 }
1927
1928 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1929 if ( radius == 0)
1930 {
1931 path->AddRectangle( xx , yy , ww , hh ) ;
1932 m_graphicContext->DrawPath( path ) ;
1933 }
1934 else
1935 {
1936 m_graphicContext->PushState() ;
1937 m_graphicContext->Translate( xx , yy ) ;
1938 m_graphicContext->Scale( radius , radius ) ;
1939 double fw = ww / radius ;
1940 double fh = hh / radius;
1941 path->MoveToPoint(fw, fh / 2);
1942 path->AddArcToPoint(fw, fh, fw / 2, fh, 1);
1943 path->AddArcToPoint(0, fh, 0, fh / 2, 1);
1944 path->AddArcToPoint(0, 0, fw / 2, 0, 1);
1945 path->AddArcToPoint(fw, 0, fw, fh / 2, 1);
1946 path->CloseSubpath();
1947 m_graphicContext->DrawPath( path ) ;
1948 m_graphicContext->PopState() ;
1949 }
1950 delete path ;
1951 }
1952
1953 void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
1954 {
1955 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawEllipse - invalid DC") );
1956
1957 if ( m_logicalFunction != wxCOPY )
1958 return ;
1959
1960 wxCoord xx = XLOG2DEVMAC(x);
1961 wxCoord yy = YLOG2DEVMAC(y);
1962 wxCoord ww = m_signX * XLOG2DEVREL(width);
1963 wxCoord hh = m_signY * YLOG2DEVREL(height);
1964
1965 // CMB: draw nothing if transformed w or h is 0
1966 if (ww == 0 || hh == 0)
1967 return;
1968
1969 // CMB: handle -ve width and/or height
1970 if (ww < 0)
1971 {
1972 ww = -ww;
1973 xx = xx - ww;
1974 }
1975 if (hh < 0)
1976 {
1977 hh = -hh;
1978 yy = yy - hh;
1979 }
1980
1981 wxGraphicPath* path = m_graphicContext->CreatePath() ;
1982 m_graphicContext->PushState() ;
1983 m_graphicContext->Translate(xx + ww / 2, yy + hh / 2);
1984 m_graphicContext->Scale(ww / 2 , hh / 2);
1985 path->AddArc( 0, 0, 1, 0 , 2 * M_PI , false ) ;
1986 m_graphicContext->DrawPath( path ) ;
1987 m_graphicContext->PopState() ;
1988 delete path ;
1989 }
1990
1991 bool wxDC::CanDrawBitmap() const
1992 {
1993 return true ;
1994 }
1995
1996 bool wxDC::DoBlit(
1997 wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
1998 wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask,
1999 wxCoord xsrcMask, wxCoord ysrcMask )
2000 {
2001 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoBlit - invalid DC") );
2002 wxCHECK_MSG( source->Ok(), false, wxT("wxDC(cg)::DoBlit - invalid source DC") );
2003
2004 if ( logical_func == wxNO_OP )
2005 return true ;
2006
2007 if (xsrcMask == -1 && ysrcMask == -1)
2008 {
2009 xsrcMask = xsrc;
2010 ysrcMask = ysrc;
2011 }
2012
2013 wxCoord yysrc = source->YLOG2DEVMAC(ysrc) ;
2014 wxCoord xxsrc = source->XLOG2DEVMAC(xsrc) ;
2015 wxCoord wwsrc = source->XLOG2DEVREL(width) ;
2016 wxCoord hhsrc = source->YLOG2DEVREL(height) ;
2017
2018 wxCoord yydest = YLOG2DEVMAC(ydest) ;
2019 wxCoord xxdest = XLOG2DEVMAC(xdest) ;
2020 wxCoord wwdest = XLOG2DEVREL(width) ;
2021 wxCoord hhdest = YLOG2DEVREL(height) ;
2022
2023 wxMemoryDC* memdc = dynamic_cast<wxMemoryDC*>(source) ;
2024 if ( memdc && logical_func == wxCOPY )
2025 {
2026 wxBitmap blit = memdc->GetSelectedObject() ;
2027
2028 wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") ) ;
2029
2030 wxCoord bmpwidth = blit.GetWidth();
2031 wxCoord bmpheight = blit.GetHeight();
2032
2033 if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc )
2034 {
2035 wwsrc = wxMin( wwsrc , bmpwidth - xxsrc ) ;
2036 hhsrc = wxMin( hhsrc , bmpheight - yysrc ) ;
2037 if ( wwsrc > 0 && hhsrc > 0 )
2038 {
2039 if ( xxsrc >= 0 && yysrc >= 0 )
2040 {
2041 wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc ) ;
2042 // TODO we perhaps could add a DrawSubBitmap call to dc for performance reasons
2043 blit = blit.GetSubBitmap( subrect ) ;
2044 }
2045 else
2046 {
2047 // in this case we'd probably have to adjust the different coordinates, but
2048 // we have to find out proper contract first
2049 blit = wxNullBitmap ;
2050 }
2051 }
2052 else
2053 {
2054 blit = wxNullBitmap ;
2055 }
2056 }
2057
2058 if ( blit.Ok() )
2059 {
2060 m_graphicContext->DrawBitmap( blit, xxdest , yydest , wwdest , hhdest ) ;
2061 }
2062 }
2063 else
2064 {
2065 wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts, and only with wxCOPY logical operation.") ) ;
2066 return false ;
2067 }
2068
2069 return true;
2070 }
2071
2072 void wxDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y,
2073 double angle)
2074 {
2075 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRotatedText - invalid DC") );
2076 // wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoDrawRotatedText - no valid font set") );
2077
2078 if ( str.length() == 0 )
2079 return ;
2080 if ( m_logicalFunction != wxCOPY )
2081 return ;
2082
2083 int drawX = XLOG2DEVMAC(x) ;
2084 int drawY = YLOG2DEVMAC(y) ;
2085
2086 m_graphicContext->DrawText( str, drawX ,drawY , angle ) ;
2087 }
2088
2089 void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y)
2090 {
2091 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawText - invalid DC") );
2092
2093 DoDrawRotatedText( strtext , x , y , 0.0 ) ;
2094 }
2095
2096 bool wxDC::CanGetTextExtent() const
2097 {
2098 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::CanGetTextExtent - invalid DC") );
2099
2100 return true ;
2101 }
2102
2103 void wxDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height,
2104 wxCoord *descent, wxCoord *externalLeading ,
2105 wxFont *theFont ) const
2106 {
2107 wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoGetTextExtent - invalid DC") );
2108
2109 wxFont formerFont = m_font ;
2110 if ( theFont )
2111 {
2112 m_graphicContext->SetFont( *theFont ) ;
2113 }
2114
2115 wxCoord h , d , e , w ;
2116
2117 m_graphicContext->GetTextExtent( str, &w, &h, &d, &e ) ;
2118
2119 if ( height )
2120 *height = YDEV2LOGREL( h ) ;
2121 if ( descent )
2122 *descent =YDEV2LOGREL( d);
2123 if ( externalLeading )
2124 *externalLeading = YDEV2LOGREL( e);
2125 if ( width )
2126 *width = XDEV2LOGREL( w ) ;
2127
2128 if ( theFont )
2129 {
2130 m_graphicContext->SetFont( m_font ) ;
2131 }
2132 }
2133
2134 bool wxDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
2135 {
2136 wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoGetPartialTextExtents - invalid DC") );
2137
2138 m_graphicContext->GetPartialTextExtents( text, widths ) ;
2139 for ( size_t i = 0 ; i < widths.GetCount() ; ++i )
2140 widths[i] = XDEV2LOGREL( widths[i] );
2141
2142 return true;
2143 }
2144
2145 wxCoord wxDC::GetCharWidth(void) const
2146 {
2147 wxCoord width ;
2148 DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL ) ;
2149
2150 return width ;
2151 }
2152
2153 wxCoord wxDC::GetCharHeight(void) const
2154 {
2155 wxCoord height ;
2156 DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL ) ;
2157
2158 return height ;
2159 }
2160
2161 void wxDC::Clear(void)
2162 {
2163 wxCHECK_RET( Ok(), wxT("wxDC(cg)::Clear - invalid DC") );
2164
2165 if (m_backgroundBrush.Ok() && m_backgroundBrush.GetStyle() != wxTRANSPARENT)
2166 {
2167 HIRect rect = CGRectMake( -10000 , -10000 , 20000 , 20000 ) ;
2168 CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ;
2169 switch ( m_backgroundBrush.MacGetBrushKind() )
2170 {
2171 case kwxMacBrushTheme :
2172 {
2173 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
2174 if ( HIThemeSetFill != 0 )
2175 {
2176 HIThemeSetFill( m_backgroundBrush.MacGetTheme(), NULL, cg, kHIThemeOrientationNormal );
2177 CGContextFillRect(cg, rect);
2178
2179 }
2180 else
2181 #endif
2182 {
2183 RGBColor color;
2184 GetThemeBrushAsColor( m_backgroundBrush.MacGetTheme(), 32, true, &color );
2185 CGContextSetRGBFillColor( cg, (CGFloat) color.red / 65536,
2186 (CGFloat) color.green / 65536, (CGFloat) color.blue / 65536, 1 );
2187 CGContextFillRect( cg, rect );
2188 }
2189
2190 // reset to normal value
2191 RGBColor col = MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
2192 CGContextSetRGBFillColor( cg, col.red / 65536.0, col.green / 65536.0, col.blue / 65536.0, 1.0 );
2193 }
2194 break ;
2195
2196 case kwxMacBrushThemeBackground :
2197 {
2198 wxFAIL_MSG( wxT("There shouldn't be theme backgrounds under Quartz") ) ;
2199
2200 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2201 if ( UMAGetSystemVersion() >= 0x1030 )
2202 {
2203 HIThemeBackgroundDrawInfo drawInfo ;
2204 drawInfo.version = 0 ;
2205 drawInfo.state = kThemeStateActive ;
2206 drawInfo.kind = m_backgroundBrush.MacGetThemeBackground( NULL ) ;
2207 if ( drawInfo.kind == kThemeBackgroundMetal )
2208 {
2209 HIThemeDrawBackground( &rect, &drawInfo, cg, kHIThemeOrientationNormal ) ;
2210 HIThemeApplyBackground( &rect, &drawInfo, cg, kHIThemeOrientationNormal ) ;
2211 }
2212 }
2213 #endif
2214 }
2215 break ;
2216
2217 case kwxMacBrushColour :
2218 {
2219 // FIXME: doesn't correctly render stippled brushes !!
2220 // FIXME: should this be replaced by ::SetBrush() ??
2221
2222 RGBColor col = MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel()) ;
2223 CGContextSetRGBFillColor( cg , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
2224 CGContextFillRect(cg, rect);
2225
2226 // reset to normal value
2227 col = MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ;
2228 CGContextSetRGBFillColor( cg , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ;
2229 }
2230 break ;
2231
2232 default :
2233 wxFAIL_MSG( wxT("unknown brush kind") ) ;
2234 break ;
2235 }
2236 }
2237 }
2238
2239 #pragma mark -
2240
2241 // ---------------------------------------------------------------------------
2242 // coordinates transformations
2243 // ---------------------------------------------------------------------------
2244
2245 wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const
2246 {
2247 return ((wxDC *)this)->XDEV2LOG(x);
2248 }
2249
2250 wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const
2251 {
2252 return ((wxDC *)this)->YDEV2LOG(y);
2253 }
2254
2255 wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const
2256 {
2257 return ((wxDC *)this)->XDEV2LOGREL(x);
2258 }
2259
2260 wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const
2261 {
2262 return ((wxDC *)this)->YDEV2LOGREL(y);
2263 }
2264
2265 wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const
2266 {
2267 return ((wxDC *)this)->XLOG2DEV(x);
2268 }
2269
2270 wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const
2271 {
2272 return ((wxDC *)this)->YLOG2DEV(y);
2273 }
2274
2275 wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const
2276 {
2277 return ((wxDC *)this)->XLOG2DEVREL(x);
2278 }
2279
2280 wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const
2281 {
2282 return ((wxDC *)this)->YLOG2DEVREL(y);
2283 }
2284
2285 #endif // wxMAC_USE_CORE_GRAPHICS