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