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