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