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