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