]>
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 | |
eb1a7cf9 | 57 | // TODO: update |
b21d67a0 DS |
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; | |
eb1a7cf9 | 74 | win->MacWindowToRootWindow( &x, &y ) ; |
bcbbfab8 SC |
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 | 597 | |
ec4a2b5d | 598 | const float dotted[] = { dashUnit , dashUnit + 2.0 }; |
24cd6f82 SC |
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] ; | |
ec4a2b5d | 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); | |
ec4a2b5d | 782 | |
613a24f7 SC |
783 | fw = CGRectGetWidth(rect) / ovalWidth; |
784 | fh = CGRectGetHeight(rect) / ovalHeight; | |
ec4a2b5d DS |
785 | |
786 | CGContextMoveToPoint(c, fw, fh / 2); | |
787 | CGContextAddArcToPoint(c, fw, fh, fw / 2, fh, 1); | |
788 | CGContextAddArcToPoint(c, 0, fh, 0, fh / 2, 1); | |
789 | CGContextAddArcToPoint(c, 0, 0, fw / 2, 0, 1); | |
790 | CGContextAddArcToPoint(c, fw, 0, fw, fh / 2, 1); | |
613a24f7 SC |
791 | CGContextClosePath(c); |
792 | CGContextRestoreGState(c); | |
793 | } | |
794 | ||
eb1a7cf9 DS |
795 | #pragma mark - |
796 | ||
613a24f7 SC |
797 | wxDC::wxDC() |
798 | { | |
eb1a7cf9 | 799 | m_ok = false ; |
97071cdb | 800 | m_colour = true; |
613a24f7 SC |
801 | m_mm_to_pix_x = mm2pt; |
802 | m_mm_to_pix_y = mm2pt; | |
24cd6f82 | 803 | |
613a24f7 SC |
804 | m_externalDeviceOriginX = 0; |
805 | m_externalDeviceOriginY = 0; | |
806 | m_logicalScaleX = 1.0; | |
807 | m_logicalScaleY = 1.0; | |
808 | m_userScaleX = 1.0; | |
809 | m_userScaleY = 1.0; | |
810 | m_scaleX = 1.0; | |
811 | m_scaleY = 1.0; | |
eb1a7cf9 | 812 | m_needComputeScaleX = |
97071cdb | 813 | m_needComputeScaleY = false; |
20b69855 | 814 | |
a63b4755 | 815 | m_macPort = 0 ; |
eb1a7cf9 DS |
816 | m_macLocalOrigin.x = |
817 | m_macLocalOrigin.y = 0 ; | |
20b69855 | 818 | |
613a24f7 SC |
819 | m_pen = *wxBLACK_PEN; |
820 | m_font = *wxNORMAL_FONT; | |
821 | m_brush = *wxWHITE_BRUSH; | |
613a24f7 | 822 | |
20b69855 | 823 | m_macATSUIStyle = NULL ; |
20b69855 | 824 | m_graphicContext = NULL ; |
613a24f7 SC |
825 | } |
826 | ||
97071cdb | 827 | wxDC::~wxDC() |
613a24f7 | 828 | { |
97071cdb | 829 | if ( m_macATSUIStyle ) |
613a24f7 SC |
830 | { |
831 | ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle); | |
832 | m_macATSUIStyle = NULL ; | |
833 | } | |
20b69855 SC |
834 | |
835 | delete m_graphicContext ; | |
613a24f7 SC |
836 | } |
837 | ||
838 | void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask ) | |
839 | { | |
b21d67a0 DS |
840 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawBitmap - invalid DC") ); |
841 | wxCHECK_RET( bmp.Ok(), wxT("wxDC(cg)::DoDrawBitmap - invalid bitmap") ); | |
75f0a06e | 842 | |
20b69855 SC |
843 | wxCoord xx = XLOG2DEVMAC(x); |
844 | wxCoord yy = YLOG2DEVMAC(y); | |
845 | wxCoord w = bmp.GetWidth(); | |
846 | wxCoord h = bmp.GetHeight(); | |
847 | wxCoord ww = XLOG2DEVREL(w); | |
848 | wxCoord hh = YLOG2DEVREL(h); | |
849 | ||
b60f5ca5 | 850 | CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; |
20b69855 SC |
851 | CGImageRef image = (CGImageRef)( bmp.CGImageCreate() ) ; |
852 | HIRect r = CGRectMake( xx , yy , ww , hh ) ; | |
853 | HIViewDrawCGImage( cg , &r , image ) ; | |
854 | CGImageRelease( image ) ; | |
613a24f7 SC |
855 | } |
856 | ||
857 | void wxDC::DoDrawIcon( const wxIcon &icon, wxCoord x, wxCoord y ) | |
858 | { | |
b21d67a0 DS |
859 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawIcon - invalid DC") ); |
860 | wxCHECK_RET( icon.Ok(), wxT("wxDC(cg)::DoDrawIcon - invalid icon") ); | |
20b69855 SC |
861 | |
862 | wxCoord xx = XLOG2DEVMAC(x); | |
863 | wxCoord yy = YLOG2DEVMAC(y); | |
864 | wxCoord w = icon.GetWidth(); | |
865 | wxCoord h = icon.GetHeight(); | |
866 | wxCoord ww = XLOG2DEVREL(w); | |
867 | wxCoord hh = YLOG2DEVREL(h); | |
868 | ||
b60f5ca5 | 869 | CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; |
20b69855 | 870 | CGRect r = CGRectMake( 00 , 00 , ww , hh ) ; |
b21d67a0 | 871 | CGContextSaveGState( cg ); |
75f0a06e | 872 | CGContextTranslateCTM( cg, xx , yy + hh ); |
b21d67a0 | 873 | CGContextScaleCTM( cg, 1, -1 ); |
20b69855 SC |
874 | PlotIconRefInContext( cg , &r , kAlignNone , kTransformNone , |
875 | NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) ) ; | |
876 | CGContextRestoreGState( cg ) ; | |
613a24f7 SC |
877 | } |
878 | ||
879 | void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height ) | |
880 | { | |
b21d67a0 | 881 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoSetClippingRegion - invalid DC") ); |
75f0a06e | 882 | |
613a24f7 SC |
883 | wxCoord xx, yy, ww, hh; |
884 | xx = XLOG2DEVMAC(x); | |
885 | yy = YLOG2DEVMAC(y); | |
886 | ww = XLOG2DEVREL(width); | |
887 | hh = YLOG2DEVREL(height); | |
b014adcc | 888 | |
b60f5ca5 | 889 | CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; |
b21d67a0 | 890 | CGRect clipRect = CGRectMake( xx , yy , ww, hh ) ; |
b014adcc SC |
891 | CGContextClipToRect( cgContext , clipRect ) ; |
892 | ||
20b69855 SC |
893 | // SetRectRgn( (RgnHandle) m_macCurrentClipRgn , xx , yy , xx + ww , yy + hh ) ; |
894 | // SectRgn( (RgnHandle) m_macCurrentClipRgn , (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ; | |
75f0a06e | 895 | |
97071cdb | 896 | if ( m_clipping ) |
613a24f7 | 897 | { |
b21d67a0 DS |
898 | m_clipX1 = wxMax( m_clipX1, xx ); |
899 | m_clipY1 = wxMax( m_clipY1, yy ); | |
900 | m_clipX2 = wxMin( m_clipX2, (xx + ww) ); | |
901 | m_clipY2 = wxMin( m_clipY2, (yy + hh) ); | |
613a24f7 SC |
902 | } |
903 | else | |
904 | { | |
97071cdb | 905 | m_clipping = true; |
b21d67a0 | 906 | |
613a24f7 SC |
907 | m_clipX1 = xx; |
908 | m_clipY1 = yy; | |
909 | m_clipX2 = xx + ww; | |
910 | m_clipY2 = yy + hh; | |
911 | } | |
75f0a06e | 912 | |
b21d67a0 | 913 | // TODO: as soon as we don't reset the context for each operation anymore |
613a24f7 SC |
914 | // we have to update the context as well |
915 | } | |
916 | ||
eb1a7cf9 | 917 | void wxDC::DoSetClippingRegionAsRegion( const wxRegion ®ion ) |
613a24f7 | 918 | { |
b21d67a0 DS |
919 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoSetClippingRegionAsRegion - invalid DC") ); |
920 | ||
613a24f7 SC |
921 | if (region.Empty()) |
922 | { | |
923 | DestroyClippingRegion(); | |
924 | return; | |
925 | } | |
97071cdb | 926 | |
613a24f7 SC |
927 | wxCoord x, y, w, h; |
928 | region.GetBox( x, y, w, h ); | |
929 | wxCoord xx, yy, ww, hh; | |
930 | xx = XLOG2DEVMAC(x); | |
931 | yy = YLOG2DEVMAC(y); | |
932 | ww = XLOG2DEVREL(w); | |
933 | hh = YLOG2DEVREL(h); | |
75f0a06e | 934 | |
613a24f7 SC |
935 | // if we have a scaling that we cannot map onto native regions |
936 | // we must use the box | |
937 | if ( ww != w || hh != h ) | |
938 | { | |
939 | wxDC::DoSetClippingRegion( x, y, w, h ); | |
940 | } | |
941 | else | |
942 | { | |
75f0a06e | 943 | #if 0 |
613a24f7 SC |
944 | CopyRgn( (RgnHandle) region.GetWXHRGN() , (RgnHandle) m_macCurrentClipRgn ) ; |
945 | if ( xx != x || yy != y ) | |
613a24f7 | 946 | OffsetRgn( (RgnHandle) m_macCurrentClipRgn , xx - x , yy - y ) ; |
b21d67a0 | 947 | SectRgn( (RgnHandle)m_macCurrentClipRgn , (RgnHandle)m_macBoundaryClipRgn , (RgnHandle)m_macCurrentClipRgn ) ; |
75f0a06e DS |
948 | #endif |
949 | ||
97071cdb | 950 | if ( m_clipping ) |
613a24f7 | 951 | { |
b21d67a0 DS |
952 | m_clipX1 = wxMax( m_clipX1, xx ); |
953 | m_clipY1 = wxMax( m_clipY1, yy ); | |
954 | m_clipX2 = wxMin( m_clipX2, (xx + ww) ); | |
955 | m_clipY2 = wxMin( m_clipY2, (yy + hh) ); | |
613a24f7 SC |
956 | } |
957 | else | |
958 | { | |
97071cdb | 959 | m_clipping = true; |
b21d67a0 | 960 | |
613a24f7 SC |
961 | m_clipX1 = xx; |
962 | m_clipY1 = yy; | |
963 | m_clipX2 = xx + ww; | |
964 | m_clipY2 = yy + hh; | |
965 | } | |
966 | } | |
967 | } | |
968 | ||
969 | void wxDC::DestroyClippingRegion() | |
970 | { | |
20b69855 | 971 | // CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ; |
b21d67a0 | 972 | |
b60f5ca5 | 973 | CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; |
b014adcc | 974 | CGContextRestoreGState( cgContext ); |
b21d67a0 DS |
975 | CGContextSaveGState( cgContext ); |
976 | ||
0e71e845 SC |
977 | m_graphicContext->SetPen( m_pen ) ; |
978 | m_graphicContext->SetBrush( m_brush ) ; | |
b21d67a0 | 979 | |
97071cdb | 980 | m_clipping = false; |
613a24f7 SC |
981 | } |
982 | ||
983 | void wxDC::DoGetSizeMM( int* width, int* height ) const | |
984 | { | |
75f0a06e DS |
985 | int w = 0, h = 0; |
986 | ||
613a24f7 | 987 | GetSize( &w, &h ); |
75f0a06e DS |
988 | if (width) |
989 | *width = long( double(w) / (m_scaleX * m_mm_to_pix_x) ); | |
990 | if (height) | |
991 | *height = long( double(h) / (m_scaleY * m_mm_to_pix_y) ); | |
613a24f7 SC |
992 | } |
993 | ||
994 | void wxDC::SetTextForeground( const wxColour &col ) | |
995 | { | |
b21d67a0 | 996 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::SetTextForeground - invalid DC") ); |
75f0a06e | 997 | |
20b69855 SC |
998 | if ( col != m_textForegroundColour ) |
999 | { | |
1000 | m_textForegroundColour = col; | |
1001 | MacInstallFont() ; | |
1002 | } | |
613a24f7 SC |
1003 | } |
1004 | ||
1005 | void wxDC::SetTextBackground( const wxColour &col ) | |
1006 | { | |
b21d67a0 | 1007 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::SetTextBackground - invalid DC") ); |
75f0a06e | 1008 | |
613a24f7 | 1009 | m_textBackgroundColour = col; |
613a24f7 SC |
1010 | } |
1011 | ||
1012 | void wxDC::SetMapMode( int mode ) | |
1013 | { | |
1014 | switch (mode) | |
1015 | { | |
1016 | case wxMM_TWIPS: | |
75f0a06e | 1017 | SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y ); |
613a24f7 | 1018 | break; |
97071cdb | 1019 | |
613a24f7 | 1020 | case wxMM_POINTS: |
75f0a06e | 1021 | SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y ); |
613a24f7 | 1022 | break; |
97071cdb | 1023 | |
613a24f7 SC |
1024 | case wxMM_METRIC: |
1025 | SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y ); | |
1026 | break; | |
97071cdb | 1027 | |
613a24f7 | 1028 | case wxMM_LOMETRIC: |
75f0a06e | 1029 | SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 ); |
613a24f7 | 1030 | break; |
97071cdb | 1031 | |
613a24f7 | 1032 | case wxMM_TEXT: |
97071cdb | 1033 | default: |
613a24f7 SC |
1034 | SetLogicalScale( 1.0, 1.0 ); |
1035 | break; | |
1036 | } | |
97071cdb | 1037 | |
613a24f7 SC |
1038 | if (mode != wxMM_TEXT) |
1039 | { | |
97071cdb DS |
1040 | m_needComputeScaleX = |
1041 | m_needComputeScaleY = true; | |
613a24f7 SC |
1042 | } |
1043 | } | |
1044 | ||
1045 | void wxDC::SetUserScale( double x, double y ) | |
1046 | { | |
1047 | // allow negative ? -> no | |
1048 | m_userScaleX = x; | |
1049 | m_userScaleY = y; | |
1050 | ComputeScaleAndOrigin(); | |
1051 | } | |
1052 | ||
1053 | void wxDC::SetLogicalScale( double x, double y ) | |
1054 | { | |
1055 | // allow negative ? | |
1056 | m_logicalScaleX = x; | |
1057 | m_logicalScaleY = y; | |
1058 | ComputeScaleAndOrigin(); | |
1059 | } | |
1060 | ||
1061 | void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y ) | |
1062 | { | |
1063 | m_logicalOriginX = x * m_signX; // is this still correct ? | |
1064 | m_logicalOriginY = y * m_signY; | |
1065 | ComputeScaleAndOrigin(); | |
1066 | } | |
1067 | ||
1068 | void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y ) | |
1069 | { | |
1070 | m_externalDeviceOriginX = x; | |
1071 | m_externalDeviceOriginY = y; | |
1072 | ComputeScaleAndOrigin(); | |
1073 | } | |
1074 | ||
1075 | void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) | |
1076 | { | |
1077 | m_signX = (xLeftRight ? 1 : -1); | |
b21d67a0 | 1078 | m_signY = (yBottomUp ? -1 : 1); |
613a24f7 SC |
1079 | ComputeScaleAndOrigin(); |
1080 | } | |
1081 | ||
1082 | wxSize wxDC::GetPPI() const | |
1083 | { | |
1084 | return wxSize(72, 72); | |
1085 | } | |
1086 | ||
1087 | int wxDC::GetDepth() const | |
1088 | { | |
20b69855 | 1089 | return 32 ; |
613a24f7 SC |
1090 | } |
1091 | ||
1092 | void wxDC::ComputeScaleAndOrigin() | |
1093 | { | |
1094 | // CMB: copy scale to see if it changes | |
1095 | double origScaleX = m_scaleX; | |
1096 | double origScaleY = m_scaleY; | |
1097 | m_scaleX = m_logicalScaleX * m_userScaleX; | |
1098 | m_scaleY = m_logicalScaleY * m_userScaleY; | |
24cd6f82 SC |
1099 | m_deviceOriginX = m_externalDeviceOriginX; |
1100 | m_deviceOriginY = m_externalDeviceOriginY; | |
97071cdb | 1101 | |
613a24f7 SC |
1102 | // CMB: if scale has changed call SetPen to recalulate the line width |
1103 | if (m_scaleX != origScaleX || m_scaleY != origScaleY) | |
1104 | { | |
1105 | // this is a bit artificial, but we need to force wxDC to think | |
1106 | // the pen has changed | |
75f0a06e | 1107 | wxPen pen( GetPen() ); |
97071cdb | 1108 | |
613a24f7 SC |
1109 | m_pen = wxNullPen; |
1110 | SetPen(pen); | |
1111 | } | |
1112 | } | |
1113 | ||
b21d67a0 | 1114 | void wxDC::SetPalette( const wxPalette& palette ) |
613a24f7 SC |
1115 | { |
1116 | } | |
1117 | ||
b21d67a0 | 1118 | void wxDC::SetBackgroundMode( int mode ) |
613a24f7 SC |
1119 | { |
1120 | m_backgroundMode = mode ; | |
1121 | } | |
1122 | ||
b21d67a0 | 1123 | void wxDC::SetFont( const wxFont &font ) |
613a24f7 SC |
1124 | { |
1125 | m_font = font; | |
20b69855 | 1126 | MacInstallFont() ; |
613a24f7 SC |
1127 | } |
1128 | ||
b21d67a0 | 1129 | void wxDC::SetPen( const wxPen &pen ) |
613a24f7 SC |
1130 | { |
1131 | if ( m_pen == pen ) | |
1132 | return ; | |
97071cdb | 1133 | |
613a24f7 | 1134 | m_pen = pen; |
20b69855 SC |
1135 | if ( m_graphicContext ) |
1136 | { | |
24cd6f82 SC |
1137 | if ( m_pen.GetStyle() == wxSOLID || m_pen.GetStyle() == wxTRANSPARENT ) |
1138 | { | |
1139 | m_graphicContext->SetPen( m_pen ) ; | |
1140 | } | |
1141 | else | |
1142 | { | |
1143 | // we have to compensate for moved device origins etc. otherwise patterned pens are standing still | |
1144 | // eg when using a wxScrollWindow and scrolling around | |
1145 | CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; | |
1146 | int origX = XLOG2DEVMAC(0) ; | |
1147 | int origY = YLOG2DEVMAC(0) ; | |
b21d67a0 | 1148 | CGContextTranslateCTM( cgContext, origX, origY ); |
24cd6f82 | 1149 | m_graphicContext->SetPen( m_pen ) ; |
b21d67a0 | 1150 | CGContextTranslateCTM( cgContext, -origX, -origY ); |
24cd6f82 | 1151 | } |
20b69855 | 1152 | } |
613a24f7 SC |
1153 | } |
1154 | ||
b21d67a0 | 1155 | void wxDC::SetBrush( const wxBrush &brush ) |
613a24f7 SC |
1156 | { |
1157 | if (m_brush == brush) | |
1158 | return; | |
97071cdb | 1159 | |
613a24f7 | 1160 | m_brush = brush; |
20b69855 SC |
1161 | if ( m_graphicContext ) |
1162 | { | |
24cd6f82 SC |
1163 | if ( brush.GetStyle() == wxSOLID || brush.GetStyle() == wxTRANSPARENT ) |
1164 | { | |
1165 | m_graphicContext->SetBrush( m_brush ) ; | |
1166 | } | |
1167 | else | |
1168 | { | |
1169 | // we have to compensate for moved device origins etc. otherwise patterned brushes are standing still | |
1170 | // eg when using a wxScrollWindow and scrolling around | |
1171 | CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; | |
1172 | int origX = XLOG2DEVMAC(0) ; | |
1173 | int origY = YLOG2DEVMAC(0) ; | |
b21d67a0 | 1174 | CGContextTranslateCTM( cgContext, origX, origY ); |
24cd6f82 | 1175 | m_graphicContext->SetBrush( m_brush ) ; |
b21d67a0 | 1176 | CGContextTranslateCTM( cgContext, -origX, -origY ); |
24cd6f82 | 1177 | } |
20b69855 | 1178 | } |
613a24f7 SC |
1179 | } |
1180 | ||
b21d67a0 | 1181 | void wxDC::SetBackground( const wxBrush &brush ) |
613a24f7 SC |
1182 | { |
1183 | if (m_backgroundBrush == brush) | |
1184 | return; | |
97071cdb | 1185 | |
613a24f7 SC |
1186 | m_backgroundBrush = brush; |
1187 | if (!m_backgroundBrush.Ok()) | |
1188 | return; | |
613a24f7 SC |
1189 | } |
1190 | ||
b21d67a0 | 1191 | void wxDC::SetLogicalFunction( int function ) |
613a24f7 SC |
1192 | { |
1193 | if (m_logicalFunction == function) | |
1194 | return; | |
97071cdb | 1195 | |
613a24f7 | 1196 | m_logicalFunction = function ; |
613a24f7 SC |
1197 | } |
1198 | ||
1199 | extern bool wxDoFloodFill(wxDC *dc, wxCoord x, wxCoord y, | |
1200 | const wxColour & col, int style); | |
1201 | ||
1202 | bool wxDC::DoFloodFill(wxCoord x, wxCoord y, | |
1203 | const wxColour& col, int style) | |
1204 | { | |
1205 | return wxDoFloodFill(this, x, y, col, style); | |
1206 | } | |
1207 | ||
b21d67a0 | 1208 | bool wxDC::DoGetPixel( wxCoord x, wxCoord y, wxColour *col ) const |
613a24f7 | 1209 | { |
b21d67a0 | 1210 | wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoGetPixel - invalid DC") ); |
75f0a06e | 1211 | |
880f5369 SC |
1212 | wxMacPortSaver helper((CGrafPtr)m_macPort) ; |
1213 | RGBColor colour; | |
75f0a06e DS |
1214 | |
1215 | // NB: GetCPixel is a deprecated QD call, and a slow one at that | |
880f5369 SC |
1216 | GetCPixel( |
1217 | XLOG2DEVMAC(x) + m_macLocalOriginInPort.x - m_macLocalOrigin.x, | |
1218 | YLOG2DEVMAC(y) + m_macLocalOriginInPort.y - m_macLocalOrigin.y, &colour ); | |
75f0a06e DS |
1219 | |
1220 | // convert from Mac colour to wx | |
1221 | col->Set( colour.red >> 8, colour.green >> 8, colour.blue >> 8 ); | |
97071cdb | 1222 | |
880f5369 | 1223 | return true ; |
613a24f7 SC |
1224 | } |
1225 | ||
b21d67a0 | 1226 | void wxDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 ) |
613a24f7 | 1227 | { |
b21d67a0 DS |
1228 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawLine - invalid DC") ); |
1229 | ||
08c8555e SC |
1230 | if ( m_logicalFunction != wxCOPY ) |
1231 | return ; | |
b21d67a0 | 1232 | |
613a24f7 SC |
1233 | wxCoord xx1 = XLOG2DEVMAC(x1) ; |
1234 | wxCoord yy1 = YLOG2DEVMAC(y1) ; | |
1235 | wxCoord xx2 = XLOG2DEVMAC(x2) ; | |
1236 | wxCoord yy2 = YLOG2DEVMAC(y2) ; | |
1237 | ||
20b69855 | 1238 | wxGraphicPath* path = m_graphicContext->CreatePath() ; |
b21d67a0 | 1239 | path->MoveToPoint( xx1, yy1 ) ; |
20b69855 SC |
1240 | path->AddLineToPoint( xx2 , yy2 ) ; |
1241 | path->CloseSubpath() ; | |
1242 | m_graphicContext->StrokePath( path ) ; | |
1243 | delete path ; | |
613a24f7 SC |
1244 | |
1245 | CalcBoundingBox(x1, y1); | |
1246 | CalcBoundingBox(x2, y2); | |
1247 | } | |
1248 | ||
b21d67a0 | 1249 | void wxDC::DoCrossHair( wxCoord x, wxCoord y ) |
613a24f7 | 1250 | { |
b21d67a0 | 1251 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoCrossHair - invalid DC") ); |
613a24f7 | 1252 | |
08c8555e SC |
1253 | if ( m_logicalFunction != wxCOPY ) |
1254 | return ; | |
613a24f7 | 1255 | |
b21d67a0 DS |
1256 | int w = 0, h = 0; |
1257 | ||
613a24f7 SC |
1258 | GetSize( &w, &h ); |
1259 | wxCoord xx = XLOG2DEVMAC(x); | |
1260 | wxCoord yy = YLOG2DEVMAC(y); | |
1261 | ||
20b69855 | 1262 | wxGraphicPath* path = m_graphicContext->CreatePath() ; |
b21d67a0 | 1263 | path->MoveToPoint( XLOG2DEVMAC(0), yy ) ; |
20b69855 SC |
1264 | path->AddLineToPoint( XLOG2DEVMAC(w), yy ) ; |
1265 | path->CloseSubpath() ; | |
1266 | path->MoveToPoint( xx, YLOG2DEVMAC(0) ) ; | |
1267 | path->AddLineToPoint( xx, YLOG2DEVMAC(h) ) ; | |
1268 | path->CloseSubpath() ; | |
1269 | m_graphicContext->StrokePath( path ) ; | |
1270 | delete path ; | |
613a24f7 SC |
1271 | |
1272 | CalcBoundingBox(x, y); | |
b21d67a0 | 1273 | CalcBoundingBox(x + w, y + h); |
613a24f7 SC |
1274 | } |
1275 | ||
b21d67a0 | 1276 | void wxDC::DoDrawArc( wxCoord x1, wxCoord y1, |
613a24f7 SC |
1277 | wxCoord x2, wxCoord y2, |
1278 | wxCoord xc, wxCoord yc ) | |
1279 | { | |
b21d67a0 | 1280 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawArc - invalid DC") ); |
08c8555e SC |
1281 | |
1282 | if ( m_logicalFunction != wxCOPY ) | |
1283 | return ; | |
1284 | ||
613a24f7 SC |
1285 | wxCoord xx1 = XLOG2DEVMAC(x1); |
1286 | wxCoord yy1 = YLOG2DEVMAC(y1); | |
1287 | wxCoord xx2 = XLOG2DEVMAC(x2); | |
1288 | wxCoord yy2 = YLOG2DEVMAC(y2); | |
1289 | wxCoord xxc = XLOG2DEVMAC(xc); | |
1290 | wxCoord yyc = YLOG2DEVMAC(yc); | |
75f0a06e | 1291 | |
613a24f7 SC |
1292 | double dx = xx1 - xxc; |
1293 | double dy = yy1 - yyc; | |
b21d67a0 | 1294 | double radius = sqrt((double)(dx * dx + dy * dy)); |
75f0a06e | 1295 | wxCoord rad = (wxCoord)radius; |
72ba915e | 1296 | double sa, ea; |
613a24f7 SC |
1297 | if (xx1 == xx2 && yy1 == yy2) |
1298 | { | |
72ba915e SC |
1299 | sa = 0.0; |
1300 | ea = 360.0; | |
613a24f7 SC |
1301 | } |
1302 | else if (radius == 0.0) | |
1303 | { | |
72ba915e | 1304 | sa = ea = 0.0; |
613a24f7 SC |
1305 | } |
1306 | else | |
1307 | { | |
72ba915e | 1308 | sa = (xx1 - xxc == 0) ? |
613a24f7 | 1309 | (yy1 - yyc < 0) ? 90.0 : -90.0 : |
b21d67a0 | 1310 | -atan2(double(yy1 - yyc), double(xx1 - xxc)) * RAD2DEG; |
72ba915e | 1311 | ea = (xx2 - xxc == 0) ? |
613a24f7 | 1312 | (yy2 - yyc < 0) ? 90.0 : -90.0 : |
b21d67a0 | 1313 | -atan2(double(yy2 - yyc), double(xx2 - xxc)) * RAD2DEG; |
613a24f7 | 1314 | } |
72ba915e SC |
1315 | |
1316 | bool fill = m_brush.GetStyle() != wxTRANSPARENT ; | |
20b69855 SC |
1317 | wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ; |
1318 | CGContextRef ctx = mctx->GetNativeContext() ; | |
72ba915e SC |
1319 | CGContextSaveGState( ctx ) ; |
1320 | CGContextTranslateCTM( ctx, xxc , yyc ); | |
1321 | CGContextScaleCTM( ctx , 1 , -1 ) ; | |
1322 | if ( fill ) | |
1323 | CGContextMoveToPoint( ctx , 0 , 0 ) ; | |
ec4a2b5d | 1324 | CGContextAddArc( ctx, 0, 0 , rad , DegToRad(sa), DegToRad(ea), 0 ); |
72ba915e SC |
1325 | if ( fill ) |
1326 | CGContextAddLineToPoint( ctx , 0 , 0 ) ; | |
1327 | CGContextRestoreGState( ctx ) ; | |
20b69855 | 1328 | CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ; |
613a24f7 SC |
1329 | } |
1330 | ||
b21d67a0 | 1331 | void wxDC::DoDrawEllipticArc( wxCoord x, wxCoord y, wxCoord w, wxCoord h, |
613a24f7 SC |
1332 | double sa, double ea ) |
1333 | { | |
b21d67a0 | 1334 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawEllipticArc - invalid DC") ); |
20b69855 | 1335 | |
08c8555e SC |
1336 | if ( m_logicalFunction != wxCOPY ) |
1337 | return ; | |
1338 | ||
613a24f7 SC |
1339 | wxCoord xx = XLOG2DEVMAC(x); |
1340 | wxCoord yy = YLOG2DEVMAC(y); | |
1341 | wxCoord ww = m_signX * XLOG2DEVREL(w); | |
1342 | wxCoord hh = m_signY * YLOG2DEVREL(h); | |
75f0a06e | 1343 | |
613a24f7 | 1344 | // handle -ve width and/or height |
75f0a06e DS |
1345 | if (ww < 0) |
1346 | { | |
1347 | ww = -ww; | |
1348 | xx = xx - ww; | |
1349 | } | |
1350 | if (hh < 0) | |
1351 | { | |
1352 | hh = -hh; | |
1353 | yy = yy - hh; | |
1354 | } | |
72ba915e SC |
1355 | |
1356 | bool fill = m_brush.GetStyle() != wxTRANSPARENT ; | |
1357 | ||
20b69855 SC |
1358 | wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ; |
1359 | CGContextRef ctx = mctx->GetNativeContext() ; | |
72ba915e SC |
1360 | |
1361 | CGContextSaveGState( ctx ) ; | |
ec4a2b5d | 1362 | CGContextTranslateCTM( ctx, xx + ww / 2, yy + hh / 2 ); |
72ba915e SC |
1363 | CGContextScaleCTM( ctx , 1 * ww / 2 , -1 * hh / 2 ) ; |
1364 | if ( fill ) | |
1365 | CGContextMoveToPoint( ctx , 0 , 0 ) ; | |
ec4a2b5d | 1366 | CGContextAddArc( ctx, 0, 0, 1, DegToRad(sa), DegToRad(ea), 0 ); |
72ba915e SC |
1367 | if ( fill ) |
1368 | CGContextAddLineToPoint( ctx , 0 , 0 ) ; | |
1369 | CGContextRestoreGState( ctx ) ; | |
1370 | CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ; | |
613a24f7 SC |
1371 | } |
1372 | ||
b21d67a0 | 1373 | void wxDC::DoDrawPoint( wxCoord x, wxCoord y ) |
613a24f7 | 1374 | { |
b21d67a0 | 1375 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawPoint - invalid DC") ); |
75f0a06e | 1376 | |
613a24f7 SC |
1377 | DoDrawLine( x , y , x + 1 , y + 1 ) ; |
1378 | } | |
1379 | ||
b21d67a0 | 1380 | void wxDC::DoDrawLines(int n, wxPoint points[], |
613a24f7 SC |
1381 | wxCoord xoffset, wxCoord yoffset) |
1382 | { | |
b21d67a0 | 1383 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawLines - invalid DC") ); |
75f0a06e | 1384 | |
08c8555e SC |
1385 | if ( m_logicalFunction != wxCOPY ) |
1386 | return ; | |
1387 | ||
613a24f7 SC |
1388 | wxCoord x1, x2 , y1 , y2 ; |
1389 | x1 = XLOG2DEVMAC(points[0].x + xoffset); | |
1390 | y1 = YLOG2DEVMAC(points[0].y + yoffset); | |
20b69855 SC |
1391 | wxGraphicPath* path = m_graphicContext->CreatePath() ; |
1392 | path->MoveToPoint( x1 , y1 ) ; | |
613a24f7 SC |
1393 | for (int i = 1; i < n; i++) |
1394 | { | |
1395 | x2 = XLOG2DEVMAC(points[i].x + xoffset); | |
1396 | y2 = YLOG2DEVMAC(points[i].y + yoffset); | |
1397 | ||
20b69855 | 1398 | path->AddLineToPoint( x2 , y2 ) ; |
613a24f7 | 1399 | } |
97071cdb | 1400 | |
20b69855 SC |
1401 | m_graphicContext->StrokePath( path ) ; |
1402 | delete path ; | |
613a24f7 SC |
1403 | } |
1404 | ||
e828c96a SC |
1405 | #if wxUSE_SPLINES |
1406 | void wxDC::DoDrawSpline(wxList *points) | |
1407 | { | |
b21d67a0 DS |
1408 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawSpline - invalid DC") ); |
1409 | ||
e828c96a SC |
1410 | if ( m_logicalFunction != wxCOPY ) |
1411 | return ; | |
1412 | ||
1413 | wxGraphicPath* path = m_graphicContext->CreatePath() ; | |
1414 | ||
1415 | wxList::compatibility_iterator node = points->GetFirst(); | |
1416 | if (node == wxList::compatibility_iterator()) | |
1417 | // empty list | |
1418 | return; | |
1419 | ||
1420 | wxPoint *p = (wxPoint *)node->GetData(); | |
1421 | ||
1422 | wxCoord x1 = p->x; | |
1423 | wxCoord y1 = p->y; | |
1424 | ||
1425 | node = node->GetNext(); | |
1426 | p = (wxPoint *)node->GetData(); | |
1427 | ||
1428 | wxCoord x2 = p->x; | |
1429 | wxCoord y2 = p->y; | |
1430 | wxCoord cx1 = ( x1 + x2 ) / 2; | |
1431 | wxCoord cy1 = ( y1 + y2 ) / 2; | |
1432 | ||
1433 | path->MoveToPoint( XLOG2DEVMAC( x1 ) , XLOG2DEVMAC( y1 ) ) ; | |
1434 | path->AddLineToPoint( XLOG2DEVMAC( cx1 ) , XLOG2DEVMAC( cy1 ) ) ; | |
1435 | ||
1436 | #if !wxUSE_STL | |
1437 | while ((node = node->GetNext()) != NULL) | |
1438 | #else | |
1439 | while ((node = node->GetNext())) | |
1440 | #endif // !wxUSE_STL | |
1441 | { | |
1442 | p = (wxPoint *)node->GetData(); | |
1443 | x1 = x2; | |
1444 | y1 = y2; | |
1445 | x2 = p->x; | |
1446 | y2 = p->y; | |
1447 | wxCoord cx4 = (x1 + x2) / 2; | |
1448 | wxCoord cy4 = (y1 + y2) / 2; | |
b21d67a0 | 1449 | |
e828c96a SC |
1450 | path->AddQuadCurveToPoint( XLOG2DEVMAC( x1 ) , XLOG2DEVMAC( y1 ) , |
1451 | XLOG2DEVMAC( cx4 ) , XLOG2DEVMAC( cy4 ) ) ; | |
b21d67a0 | 1452 | |
e828c96a SC |
1453 | cx1 = cx4; |
1454 | cy1 = cy4; | |
1455 | } | |
1456 | ||
1457 | path->AddLineToPoint( XLOG2DEVMAC( x2 ) , XLOG2DEVMAC( y2 ) ) ; | |
1458 | ||
1459 | m_graphicContext->StrokePath( path ) ; | |
1460 | delete path ; | |
1461 | } | |
1462 | #endif | |
1463 | ||
b21d67a0 | 1464 | void wxDC::DoDrawPolygon( int n, wxPoint points[], |
613a24f7 SC |
1465 | wxCoord xoffset, wxCoord yoffset, |
1466 | int fillStyle ) | |
1467 | { | |
b21d67a0 | 1468 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawPolygon - invalid DC") ); |
75f0a06e DS |
1469 | |
1470 | if ( n <= 0 || (m_brush.GetStyle() == wxTRANSPARENT && m_pen.GetStyle() == wxTRANSPARENT ) ) | |
613a24f7 | 1471 | return ; |
08c8555e SC |
1472 | if ( m_logicalFunction != wxCOPY ) |
1473 | return ; | |
1474 | ||
75f0a06e | 1475 | wxCoord x1, x2 , y1 , y2 ; |
613a24f7 SC |
1476 | x2 = x1 = XLOG2DEVMAC(points[0].x + xoffset); |
1477 | y2 = y1 = YLOG2DEVMAC(points[0].y + yoffset); | |
1478 | ||
20b69855 SC |
1479 | wxGraphicPath* path = m_graphicContext->CreatePath() ; |
1480 | path->MoveToPoint( x1 , y1 ) ; | |
613a24f7 SC |
1481 | for (int i = 1; i < n; i++) |
1482 | { | |
1483 | x2 = XLOG2DEVMAC(points[i].x + xoffset); | |
1484 | y2 = YLOG2DEVMAC(points[i].y + yoffset); | |
1485 | ||
20b69855 | 1486 | path->AddLineToPoint( x2 , y2 ) ; |
613a24f7 | 1487 | } |
97071cdb | 1488 | |
613a24f7 | 1489 | if ( x1 != x2 || y1 != y2 ) |
b21d67a0 | 1490 | path->AddLineToPoint( x1, y1 ) ; |
97071cdb | 1491 | |
20b69855 SC |
1492 | path->CloseSubpath() ; |
1493 | m_graphicContext->DrawPath( path , fillStyle ) ; | |
75f0a06e | 1494 | |
20b69855 | 1495 | delete path ; |
613a24f7 SC |
1496 | } |
1497 | ||
1498 | void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) | |
1499 | { | |
b21d67a0 | 1500 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRectangle - invalid DC") ); |
08c8555e SC |
1501 | |
1502 | if ( m_logicalFunction != wxCOPY ) | |
1503 | return ; | |
1504 | ||
613a24f7 SC |
1505 | wxCoord xx = XLOG2DEVMAC(x); |
1506 | wxCoord yy = YLOG2DEVMAC(y); | |
1507 | wxCoord ww = m_signX * XLOG2DEVREL(width); | |
1508 | wxCoord hh = m_signY * YLOG2DEVREL(height); | |
75f0a06e | 1509 | |
613a24f7 SC |
1510 | // CMB: draw nothing if transformed w or h is 0 |
1511 | if (ww == 0 || hh == 0) | |
1512 | return; | |
97071cdb | 1513 | |
613a24f7 SC |
1514 | // CMB: handle -ve width and/or height |
1515 | if (ww < 0) | |
1516 | { | |
1517 | ww = -ww; | |
1518 | xx = xx - ww; | |
1519 | } | |
1520 | if (hh < 0) | |
1521 | { | |
1522 | hh = -hh; | |
1523 | yy = yy - hh; | |
1524 | } | |
97071cdb | 1525 | |
20b69855 | 1526 | wxGraphicPath* path = m_graphicContext->CreatePath() ; |
24cd6f82 | 1527 | path->AddRectangle( xx , yy , ww , hh ) ; |
20b69855 SC |
1528 | m_graphicContext->DrawPath( path ) ; |
1529 | delete path ; | |
613a24f7 SC |
1530 | } |
1531 | ||
b21d67a0 | 1532 | void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, |
613a24f7 SC |
1533 | wxCoord width, wxCoord height, |
1534 | double radius) | |
1535 | { | |
b21d67a0 | 1536 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRoundedRectangle - invalid DC") ); |
08c8555e SC |
1537 | |
1538 | if ( m_logicalFunction != wxCOPY ) | |
1539 | return ; | |
1540 | ||
613a24f7 SC |
1541 | if (radius < 0.0) |
1542 | radius = - radius * ((width < height) ? width : height); | |
1543 | wxCoord xx = XLOG2DEVMAC(x); | |
1544 | wxCoord yy = YLOG2DEVMAC(y); | |
1545 | wxCoord ww = m_signX * XLOG2DEVREL(width); | |
1546 | wxCoord hh = m_signY * YLOG2DEVREL(height); | |
75f0a06e | 1547 | |
613a24f7 SC |
1548 | // CMB: draw nothing if transformed w or h is 0 |
1549 | if (ww == 0 || hh == 0) | |
1550 | return; | |
97071cdb | 1551 | |
613a24f7 SC |
1552 | // CMB: handle -ve width and/or height |
1553 | if (ww < 0) | |
1554 | { | |
1555 | ww = -ww; | |
1556 | xx = xx - ww; | |
1557 | } | |
1558 | if (hh < 0) | |
1559 | { | |
1560 | hh = -hh; | |
1561 | yy = yy - hh; | |
1562 | } | |
97071cdb | 1563 | |
20b69855 SC |
1564 | wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ; |
1565 | CGContextRef ctx = mctx->GetNativeContext() ; | |
ec4a2b5d | 1566 | AddRoundedRectToPath( ctx , CGRectMake( xx , yy , ww , hh ) , 16 ,16 ) ; |
20b69855 | 1567 | CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ; |
613a24f7 SC |
1568 | } |
1569 | ||
b21d67a0 | 1570 | void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height) |
613a24f7 | 1571 | { |
b21d67a0 | 1572 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawEllipse - invalid DC") ); |
08c8555e SC |
1573 | |
1574 | if ( m_logicalFunction != wxCOPY ) | |
1575 | return ; | |
1576 | ||
613a24f7 SC |
1577 | wxCoord xx = XLOG2DEVMAC(x); |
1578 | wxCoord yy = YLOG2DEVMAC(y); | |
1579 | wxCoord ww = m_signX * XLOG2DEVREL(width); | |
1580 | wxCoord hh = m_signY * YLOG2DEVREL(height); | |
1581 | // CMB: draw nothing if transformed w or h is 0 | |
1582 | if (ww == 0 || hh == 0) | |
1583 | return; | |
97071cdb | 1584 | |
613a24f7 SC |
1585 | // CMB: handle -ve width and/or height |
1586 | if (ww < 0) | |
1587 | { | |
1588 | ww = -ww; | |
1589 | xx = xx - ww; | |
1590 | } | |
1591 | if (hh < 0) | |
1592 | { | |
1593 | hh = -hh; | |
1594 | yy = yy - hh; | |
1595 | } | |
20b69855 SC |
1596 | |
1597 | wxMacCGContext* mctx = ((wxMacCGContext*) m_graphicContext) ; | |
1598 | CGContextRef ctx = mctx->GetNativeContext() ; | |
72ba915e | 1599 | CGContextSaveGState( ctx ) ; |
ec4a2b5d | 1600 | CGContextTranslateCTM( ctx, xx + ww / 2, yy + hh / 2 ); |
72ba915e | 1601 | CGContextScaleCTM( ctx , ww / 2 , hh / 2 ) ; |
ec4a2b5d | 1602 | CGContextAddArc( ctx, 0, 0, 1, 0 , 2 * M_PI , 0 ); |
72ba915e SC |
1603 | CGContextRestoreGState( ctx ) ; |
1604 | CGContextDrawPath( ctx , mctx->GetDrawingMode() ) ; | |
613a24f7 SC |
1605 | } |
1606 | ||
b21d67a0 | 1607 | bool wxDC::CanDrawBitmap(void) const |
613a24f7 SC |
1608 | { |
1609 | return true ; | |
1610 | } | |
1611 | ||
b21d67a0 | 1612 | bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, |
613a24f7 | 1613 | wxDC *source, wxCoord xsrc, wxCoord ysrc, int logical_func , bool useMask, |
ec4a2b5d | 1614 | wxCoord xsrcMask, wxCoord ysrcMask ) |
613a24f7 | 1615 | { |
b21d67a0 DS |
1616 | wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoBlit - invalid DC") ); |
1617 | wxCHECK_MSG( source->Ok(), false, wxT("wxDC(cg)::DoBlit - invalid source DC") ); | |
75f0a06e | 1618 | |
613a24f7 | 1619 | if ( logical_func == wxNO_OP ) |
97071cdb DS |
1620 | return true ; |
1621 | ||
613a24f7 SC |
1622 | if (xsrcMask == -1 && ysrcMask == -1) |
1623 | { | |
97071cdb DS |
1624 | xsrcMask = xsrc; |
1625 | ysrcMask = ysrc; | |
613a24f7 | 1626 | } |
20b69855 | 1627 | |
68654a82 | 1628 | wxCoord yysrc = source->YLOG2DEVMAC(ysrc) ; |
ec4a2b5d DS |
1629 | wxCoord xxsrc = source->XLOG2DEVMAC(xsrc) ; |
1630 | wxCoord wwsrc = source->XLOG2DEVREL(width) ; | |
68654a82 SC |
1631 | wxCoord hhsrc = source->YLOG2DEVREL(height) ; |
1632 | ||
1633 | wxCoord yydest = YLOG2DEVMAC(ydest) ; | |
1634 | wxCoord xxdest = XLOG2DEVMAC(xdest) ; | |
ec4a2b5d | 1635 | wxCoord wwdest = XLOG2DEVREL(width) ; |
68654a82 SC |
1636 | wxCoord hhdest = YLOG2DEVREL(height) ; |
1637 | ||
20b69855 | 1638 | wxMemoryDC* memdc = dynamic_cast<wxMemoryDC*>(source) ; |
a8f234d2 | 1639 | if ( memdc && logical_func == wxCOPY ) |
613a24f7 | 1640 | { |
20b69855 | 1641 | wxBitmap blit = memdc->GetSelectedObject() ; |
b21d67a0 | 1642 | |
20b69855 SC |
1643 | wxASSERT_MSG( blit.Ok() , wxT("Invalid bitmap for blitting") ) ; |
1644 | ||
20b69855 SC |
1645 | wxCoord bmpwidth = blit.GetWidth(); |
1646 | wxCoord bmpheight = blit.GetHeight(); | |
1647 | ||
68654a82 SC |
1648 | if ( xxsrc != 0 || yysrc != 0 || bmpwidth != wwsrc || bmpheight != hhsrc ) |
1649 | { | |
1650 | wwsrc = wxMin( wwsrc , bmpwidth - xxsrc ) ; | |
1651 | hhsrc = wxMin( hhsrc , bmpheight - yysrc ) ; | |
1652 | if ( wwsrc > 0 && hhsrc > 0 ) | |
1653 | { | |
1654 | if ( xxsrc >= 0 && yysrc >= 0 ) | |
1655 | { | |
1656 | wxRect subrect( xxsrc, yysrc, wwsrc , hhsrc ) ; | |
1657 | blit = blit.GetSubBitmap( subrect ) ; | |
1658 | } | |
1659 | else | |
1660 | { | |
1661 | // in this case we'd probably have to adjust the different coordinates, but | |
1662 | // we have to find out proper contract first | |
1663 | blit = wxNullBitmap ; | |
1664 | } | |
1665 | } | |
1666 | else | |
1667 | { | |
1668 | blit = wxNullBitmap ; | |
1669 | } | |
1670 | } | |
97071cdb | 1671 | |
68654a82 | 1672 | if ( blit.Ok() ) |
613a24f7 | 1673 | { |
b60f5ca5 | 1674 | CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; |
68654a82 SC |
1675 | CGImageRef image = (CGImageRef)( blit.CGImageCreate() ) ; |
1676 | HIRect r = CGRectMake( xxdest , yydest , wwdest , hhdest ) ; | |
1677 | HIViewDrawCGImage( cg , &r , image ) ; | |
1678 | CGImageRelease( image ) ; | |
613a24f7 | 1679 | } |
20b69855 SC |
1680 | } |
1681 | else | |
1682 | { | |
75f0a06e | 1683 | #if 0 |
bc8f7aee | 1684 | CGContextRef cg = (wxMacCGContext*)(source->GetGraphicContext())->GetNativeContext() ; |
68654a82 | 1685 | void *data = CGBitmapContextGetData( cg ) ; |
75f0a06e DS |
1686 | #endif |
1687 | ||
97071cdb | 1688 | return false ; // wxFAIL_MSG( wxT("Blitting is only supported from bitmap contexts") ) ; |
613a24f7 | 1689 | } |
97071cdb DS |
1690 | |
1691 | return true; | |
613a24f7 SC |
1692 | } |
1693 | ||
b21d67a0 | 1694 | void wxDC::DoDrawRotatedText(const wxString& str, wxCoord x, wxCoord y, |
613a24f7 SC |
1695 | double angle) |
1696 | { | |
b21d67a0 DS |
1697 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawRotatedText - invalid DC") ); |
1698 | wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoDrawRotatedText - no valid font set") ); | |
613a24f7 SC |
1699 | |
1700 | if ( str.Length() == 0 ) | |
1701 | return ; | |
08c8555e SC |
1702 | if ( m_logicalFunction != wxCOPY ) |
1703 | return ; | |
1704 | ||
613a24f7 SC |
1705 | OSStatus status = noErr ; |
1706 | ATSUTextLayout atsuLayout ; | |
1707 | UniCharCount chars = str.Length() ; | |
1708 | UniChar* ubuf = NULL ; | |
b21d67a0 | 1709 | |
613a24f7 | 1710 | #if SIZEOF_WCHAR_T == 4 |
75f0a06e | 1711 | wxMBConvUTF16 converter ; |
613a24f7 | 1712 | #if wxUSE_UNICODE |
75f0a06e DS |
1713 | size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ; |
1714 | ubuf = (UniChar*) malloc( unicharlen + 2 ) ; | |
1715 | converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ; | |
613a24f7 SC |
1716 | #else |
1717 | const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ; | |
b21d67a0 | 1718 | size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ; |
75f0a06e DS |
1719 | ubuf = (UniChar*) malloc( unicharlen + 2 ) ; |
1720 | converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ; | |
613a24f7 SC |
1721 | #endif |
1722 | chars = unicharlen / 2 ; | |
1723 | #else | |
1724 | #if wxUSE_UNICODE | |
1725 | ubuf = (UniChar*) str.wc_str() ; | |
1726 | #else | |
1727 | wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ; | |
1728 | chars = wxWcslen( wchar.data() ) ; | |
1729 | ubuf = (UniChar*) wchar.data() ; | |
1730 | #endif | |
1731 | #endif | |
1732 | ||
1733 | int drawX = XLOG2DEVMAC(x) ; | |
1734 | int drawY = YLOG2DEVMAC(y) ; | |
1735 | ||
613a24f7 SC |
1736 | status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 , |
1737 | &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ; | |
1738 | ||
1739 | wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") ); | |
613a24f7 | 1740 | |
28dd2407 SC |
1741 | status = ::ATSUSetTransientFontMatching( atsuLayout , true ) ; |
1742 | wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") ); | |
613a24f7 | 1743 | |
28dd2407 | 1744 | int iAngle = int( angle ); |
613a24f7 SC |
1745 | if ( abs(iAngle) > 0 ) |
1746 | { | |
1747 | Fixed atsuAngle = IntToFixed( iAngle ) ; | |
1748 | ATSUAttributeTag atsuTags[] = | |
1749 | { | |
1750 | kATSULineRotationTag , | |
1751 | } ; | |
b21d67a0 | 1752 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = |
613a24f7 SC |
1753 | { |
1754 | sizeof( Fixed ) , | |
1755 | } ; | |
b21d67a0 | 1756 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = |
613a24f7 SC |
1757 | { |
1758 | &atsuAngle , | |
1759 | } ; | |
b21d67a0 | 1760 | status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag), |
613a24f7 SC |
1761 | atsuTags, atsuSizes, atsuValues ) ; |
1762 | } | |
75f0a06e | 1763 | |
613a24f7 | 1764 | { |
b60f5ca5 | 1765 | CGContextRef cgContext = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; |
613a24f7 SC |
1766 | ATSUAttributeTag atsuTags[] = |
1767 | { | |
1768 | kATSUCGContextTag , | |
1769 | } ; | |
b21d67a0 | 1770 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = |
613a24f7 SC |
1771 | { |
1772 | sizeof( CGContextRef ) , | |
1773 | } ; | |
b21d67a0 | 1774 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = |
613a24f7 SC |
1775 | { |
1776 | &cgContext , | |
1777 | } ; | |
b21d67a0 | 1778 | status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag), |
613a24f7 SC |
1779 | atsuTags, atsuSizes, atsuValues ) ; |
1780 | } | |
1781 | ||
b21d67a0 DS |
1782 | ATSUTextMeasurement textBefore, textAfter ; |
1783 | ATSUTextMeasurement ascent, descent ; | |
613a24f7 SC |
1784 | |
1785 | status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
1786 | &textBefore , &textAfter, &ascent , &descent ); | |
b21d67a0 | 1787 | |
613a24f7 SC |
1788 | wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") ); |
1789 | ||
1790 | Rect rect ; | |
ec4a2b5d | 1791 | |
613a24f7 SC |
1792 | if ( m_backgroundMode == wxSOLID ) |
1793 | { | |
20b69855 SC |
1794 | wxGraphicPath* path = m_graphicContext->CreatePath() ; |
1795 | path->MoveToPoint( | |
613a24f7 SC |
1796 | drawX , |
1797 | drawY ) ; | |
20b69855 | 1798 | path->AddLineToPoint( |
b21d67a0 DS |
1799 | (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) , |
1800 | (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) ) ; | |
20b69855 | 1801 | path->AddLineToPoint( |
b21d67a0 DS |
1802 | (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) , |
1803 | (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ) ; | |
20b69855 | 1804 | path->AddLineToPoint( |
b21d67a0 DS |
1805 | (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) , |
1806 | (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ) ; | |
613a24f7 | 1807 | |
20b69855 SC |
1808 | m_graphicContext->FillPath( path , m_textBackgroundColour ) ; |
1809 | delete path ; | |
613a24f7 SC |
1810 | } |
1811 | ||
b21d67a0 DS |
1812 | drawX += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent)); |
1813 | drawY += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent)); | |
20b69855 | 1814 | |
613a24f7 SC |
1815 | status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, |
1816 | IntToFixed(drawX) , IntToFixed(drawY) , &rect ); | |
1817 | wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") ); | |
1818 | ||
b60f5ca5 SC |
1819 | CGContextSaveGState(((wxMacCGContext*)(m_graphicContext))->GetNativeContext()); |
1820 | CGContextTranslateCTM(((wxMacCGContext*)(m_graphicContext))->GetNativeContext(), drawX, drawY); | |
1821 | CGContextScaleCTM(((wxMacCGContext*)(m_graphicContext))->GetNativeContext(), 1, -1); | |
613a24f7 SC |
1822 | status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, |
1823 | IntToFixed(0) , IntToFixed(0) ); | |
b21d67a0 | 1824 | |
613a24f7 | 1825 | wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") ); |
b21d67a0 | 1826 | |
b60f5ca5 | 1827 | CGContextRestoreGState( ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ) ; |
613a24f7 SC |
1828 | |
1829 | CalcBoundingBox(XDEV2LOG(rect.left), YDEV2LOG(rect.top) ); | |
1830 | CalcBoundingBox(XDEV2LOG(rect.right), YDEV2LOG(rect.bottom) ); | |
1831 | ||
1832 | ::ATSUDisposeTextLayout(atsuLayout); | |
97071cdb | 1833 | |
613a24f7 SC |
1834 | #if SIZEOF_WCHAR_T == 4 |
1835 | free( ubuf ) ; | |
1836 | #endif | |
613a24f7 SC |
1837 | } |
1838 | ||
b21d67a0 | 1839 | void wxDC::DoDrawText(const wxString& strtext, wxCoord x, wxCoord y) |
613a24f7 | 1840 | { |
b21d67a0 | 1841 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoDrawText - invalid DC") ); |
75f0a06e | 1842 | |
613a24f7 SC |
1843 | DoDrawRotatedText( strtext , x , y , 0.0 ) ; |
1844 | } | |
1845 | ||
b21d67a0 | 1846 | bool wxDC::CanGetTextExtent() const |
613a24f7 | 1847 | { |
b21d67a0 | 1848 | wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::CanGetTextExtent - invalid DC") ); |
75f0a06e | 1849 | |
613a24f7 SC |
1850 | return true ; |
1851 | } | |
1852 | ||
b21d67a0 | 1853 | void wxDC::DoGetTextExtent( const wxString &str, wxCoord *width, wxCoord *height, |
613a24f7 SC |
1854 | wxCoord *descent, wxCoord *externalLeading , |
1855 | wxFont *theFont ) const | |
1856 | { | |
b21d67a0 | 1857 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::DoGetTextExtent - invalid DC") ); |
75f0a06e | 1858 | |
613a24f7 SC |
1859 | wxFont formerFont = m_font ; |
1860 | if ( theFont ) | |
1861 | { | |
b21d67a0 | 1862 | // work around the const-ness |
613a24f7 | 1863 | *((wxFont*)(&m_font)) = *theFont ; |
20b69855 | 1864 | MacInstallFont() ; |
613a24f7 SC |
1865 | } |
1866 | ||
613a24f7 SC |
1867 | if ( str.Length() == 0 ) |
1868 | return ; | |
1869 | ||
b21d67a0 | 1870 | wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") ) ; |
20b69855 | 1871 | |
613a24f7 SC |
1872 | OSStatus status = noErr ; |
1873 | ATSUTextLayout atsuLayout ; | |
1874 | UniCharCount chars = str.Length() ; | |
1875 | UniChar* ubuf = NULL ; | |
b21d67a0 | 1876 | |
613a24f7 | 1877 | #if SIZEOF_WCHAR_T == 4 |
75f0a06e | 1878 | wxMBConvUTF16 converter ; |
613a24f7 | 1879 | #if wxUSE_UNICODE |
75f0a06e DS |
1880 | size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ) ; |
1881 | ubuf = (UniChar*) malloc( unicharlen + 2 ) ; | |
1882 | converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ) ; | |
613a24f7 SC |
1883 | #else |
1884 | const wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ; | |
b21d67a0 | 1885 | size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ; |
75f0a06e DS |
1886 | ubuf = (UniChar*) malloc( unicharlen + 2 ) ; |
1887 | converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ; | |
613a24f7 SC |
1888 | #endif |
1889 | chars = unicharlen / 2 ; | |
1890 | #else | |
1891 | #if wxUSE_UNICODE | |
1892 | ubuf = (UniChar*) str.wc_str() ; | |
1893 | #else | |
1894 | wxWCharBuffer wchar = str.wc_str( wxConvLocal ) ; | |
1895 | chars = wxWcslen( wchar.data() ) ; | |
1896 | ubuf = (UniChar*) wchar.data() ; | |
1897 | #endif | |
1898 | #endif | |
1899 | ||
613a24f7 SC |
1900 | status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 , |
1901 | &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ; | |
1902 | ||
1903 | wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") ); | |
1904 | ||
b21d67a0 DS |
1905 | ATSUTextMeasurement textBefore, textAfter ; |
1906 | ATSUTextMeasurement textAscent, textDescent ; | |
613a24f7 SC |
1907 | |
1908 | status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
1909 | &textBefore , &textAfter, &textAscent , &textDescent ); | |
1910 | ||
1911 | if ( height ) | |
1912 | *height = YDEV2LOGREL( FixedToInt(textAscent + textDescent) ) ; | |
1913 | if ( descent ) | |
1914 | *descent =YDEV2LOGREL( FixedToInt(textDescent) ); | |
1915 | if ( externalLeading ) | |
1916 | *externalLeading = 0 ; | |
1917 | if ( width ) | |
1918 | *width = XDEV2LOGREL( FixedToInt(textAfter - textBefore) ) ; | |
1919 | ||
1920 | ::ATSUDisposeTextLayout(atsuLayout); | |
b21d67a0 | 1921 | |
613a24f7 SC |
1922 | #if SIZEOF_WCHAR_T == 4 |
1923 | free( ubuf ) ; | |
1924 | #endif | |
b21d67a0 | 1925 | |
613a24f7 SC |
1926 | if ( theFont ) |
1927 | { | |
1928 | // work around the constness | |
1929 | *((wxFont*)(&m_font)) = formerFont ; | |
20b69855 | 1930 | MacInstallFont() ; |
613a24f7 SC |
1931 | } |
1932 | } | |
1933 | ||
613a24f7 SC |
1934 | bool wxDC::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const |
1935 | { | |
b21d67a0 | 1936 | wxCHECK_MSG( Ok(), false, wxT("wxDC(cg)::DoGetPartialTextExtents - invalid DC") ); |
613a24f7 SC |
1937 | |
1938 | widths.Empty(); | |
1939 | widths.Add(0, text.Length()); | |
1940 | ||
1941 | if (text.Length() == 0) | |
1942 | return false; | |
b21d67a0 | 1943 | |
764e6694 KO |
1944 | ATSUTextLayout atsuLayout ; |
1945 | UniCharCount chars = text.Length() ; | |
1946 | UniChar* ubuf = NULL ; | |
b21d67a0 | 1947 | |
764e6694 | 1948 | #if SIZEOF_WCHAR_T == 4 |
d9d488cf | 1949 | wxMBConvUTF16 converter ; |
764e6694 KO |
1950 | #if wxUSE_UNICODE |
1951 | size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 ) ; | |
1952 | ubuf = (UniChar*) malloc( unicharlen + 2 ) ; | |
1953 | converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 ) ; | |
1954 | #else | |
1955 | const wxWCharBuffer wchar = text.wc_str( wxConvLocal ) ; | |
ec4a2b5d | 1956 | size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ) ; |
764e6694 KO |
1957 | ubuf = (UniChar*) malloc( unicharlen + 2 ) ; |
1958 | converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ) ; | |
1959 | #endif | |
1960 | chars = unicharlen / 2 ; | |
1961 | #else | |
1962 | #if wxUSE_UNICODE | |
1963 | ubuf = (UniChar*) text.wc_str() ; | |
1964 | #else | |
1965 | wxWCharBuffer wchar = text.wc_str( wxConvLocal ) ; | |
1966 | chars = wxWcslen( wchar.data() ) ; | |
1967 | ubuf = (UniChar*) wchar.data() ; | |
1968 | #endif | |
1969 | #endif | |
1970 | ||
97071cdb | 1971 | OSStatus status; |
764e6694 KO |
1972 | status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 , |
1973 | &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ) ; | |
1974 | ||
97071cdb DS |
1975 | for ( int pos = 0; pos < (int)chars; pos ++ ) |
1976 | { | |
1977 | unsigned long actualNumberOfBounds = 0; | |
1978 | ATSTrapezoid glyphBounds; | |
1979 | ||
1980 | // We get a single bound, since the text should only require one. If it requires more, there is an issue | |
1981 | OSStatus result; | |
1982 | result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1, | |
1983 | kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds ); | |
1984 | if (result != noErr || actualNumberOfBounds != 1 ) | |
1985 | return false; | |
1986 | ||
1987 | widths[pos] = XDEV2LOGREL(FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x )); | |
1988 | //unsigned char uch = s[i]; | |
1989 | } | |
1990 | ||
764e6694 KO |
1991 | ::ATSUDisposeTextLayout(atsuLayout); |
1992 | return true; | |
613a24f7 SC |
1993 | } |
1994 | ||
b21d67a0 | 1995 | wxCoord wxDC::GetCharWidth(void) const |
613a24f7 SC |
1996 | { |
1997 | wxCoord width ; | |
b21d67a0 | 1998 | DoGetTextExtent( wxT("g") , &width , NULL , NULL , NULL , NULL ) ; |
75f0a06e | 1999 | |
613a24f7 SC |
2000 | return width ; |
2001 | } | |
2002 | ||
b21d67a0 | 2003 | wxCoord wxDC::GetCharHeight(void) const |
613a24f7 SC |
2004 | { |
2005 | wxCoord height ; | |
b21d67a0 | 2006 | DoGetTextExtent( wxT("g") , NULL , &height , NULL , NULL , NULL ) ; |
75f0a06e | 2007 | |
613a24f7 SC |
2008 | return height ; |
2009 | } | |
2010 | ||
b21d67a0 | 2011 | void wxDC::Clear(void) |
613a24f7 | 2012 | { |
b21d67a0 | 2013 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::Clear - invalid DC") ); |
613a24f7 | 2014 | |
75f0a06e | 2015 | if (m_backgroundBrush.Ok() && m_backgroundBrush.GetStyle() != wxTRANSPARENT) |
ec4a2b5d | 2016 | { |
b014adcc | 2017 | HIRect rect = CGRectMake( -10000 , -10000 , 20000 , 20000 ) ; |
b60f5ca5 | 2018 | CGContextRef cg = ((wxMacCGContext*)(m_graphicContext))->GetNativeContext() ; |
b21d67a0 | 2019 | switch ( m_backgroundBrush.MacGetBrushKind() ) |
613a24f7 SC |
2020 | { |
2021 | case kwxMacBrushTheme : | |
2022 | { | |
a63b4755 SC |
2023 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 |
2024 | if ( HIThemeSetFill != 0 ) | |
2025 | { | |
72ba915e | 2026 | HIThemeSetFill( m_backgroundBrush.MacGetTheme(), NULL, cg, kHIThemeOrientationNormal ); |
a63b4755 SC |
2027 | CGContextFillRect(cg, rect); |
2028 | ||
2029 | } | |
2030 | else | |
2031 | #endif | |
2032 | { | |
75f0a06e | 2033 | RGBColor color; |
72ba915e SC |
2034 | GetThemeBrushAsColor( m_backgroundBrush.MacGetTheme(), 32, true, &color ); |
2035 | CGContextSetRGBFillColor( cg, (float) color.red / 65536, | |
97071cdb DS |
2036 | (float) color.green / 65536, (float) color.blue / 65536, 1 ); |
2037 | CGContextFillRect( cg, rect ); | |
a63b4755 | 2038 | } |
72ba915e | 2039 | |
a63b4755 SC |
2040 | // reset to normal value |
2041 | RGBColor col = MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ; | |
72ba915e | 2042 | CGContextSetRGBFillColor( cg, col.red / 65536.0, col.green / 65536.0, col.blue / 65536.0, 1.0 ); |
613a24f7 | 2043 | } |
97071cdb DS |
2044 | break ; |
2045 | ||
613a24f7 SC |
2046 | case kwxMacBrushThemeBackground : |
2047 | { | |
a63b4755 | 2048 | wxFAIL_MSG( wxT("There shouldn't be theme backgrounds under Quartz") ) ; |
75f0a06e | 2049 | |
b014adcc | 2050 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 |
1688bb38 | 2051 | if ( UMAGetSystemVersion() >= 0x1030 ) |
b014adcc SC |
2052 | { |
2053 | HIThemeBackgroundDrawInfo drawInfo ; | |
2054 | drawInfo.version = 0 ; | |
2055 | drawInfo.state = kThemeStateActive ; | |
ec4a2b5d | 2056 | drawInfo.kind = m_backgroundBrush.MacGetThemeBackground( NULL ) ; |
b014adcc | 2057 | if ( drawInfo.kind == kThemeBackgroundMetal ) |
ec4a2b5d DS |
2058 | { |
2059 | HIThemeDrawBackground( &rect, &drawInfo, cg, kHIThemeOrientationNormal ) ; | |
2060 | HIThemeApplyBackground( &rect, &drawInfo, cg, kHIThemeOrientationNormal ) ; | |
2061 | } | |
b014adcc SC |
2062 | } |
2063 | #endif | |
613a24f7 | 2064 | } |
97071cdb DS |
2065 | break ; |
2066 | ||
613a24f7 | 2067 | case kwxMacBrushColour : |
97071cdb | 2068 | { |
ec4a2b5d DS |
2069 | // FIXME: doesn't correctly render stippled brushes !! |
2070 | // FIXME: should this be replaced by ::SetBrush() ?? | |
2071 | ||
97071cdb DS |
2072 | RGBColor col = MAC_WXCOLORREF( m_backgroundBrush.GetColour().GetPixel()) ; |
2073 | CGContextSetRGBFillColor( cg , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ; | |
2074 | CGContextFillRect(cg, rect); | |
613a24f7 | 2075 | |
97071cdb DS |
2076 | // reset to normal value |
2077 | col = MAC_WXCOLORREF( GetBrush().GetColour().GetPixel() ) ; | |
2078 | CGContextSetRGBFillColor( cg , col.red / 65536.0 , col.green / 65536.0 , col.blue / 65536.0 , 1.0 ) ; | |
2079 | } | |
2080 | break ; | |
2081 | ||
75f0a06e DS |
2082 | default : |
2083 | wxFAIL_MSG( wxT("unknown brush kind") ) ; | |
97071cdb | 2084 | break ; |
613a24f7 SC |
2085 | } |
2086 | } | |
2087 | } | |
2088 | ||
2089 | void wxDC::MacInstallFont() const | |
2090 | { | |
b21d67a0 | 2091 | wxCHECK_RET( Ok(), wxT("wxDC(cg)::MacInstallFont - invalid DC") ); |
613a24f7 | 2092 | |
97071cdb | 2093 | if ( m_macATSUIStyle ) |
613a24f7 | 2094 | { |
20b69855 SC |
2095 | ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle); |
2096 | m_macATSUIStyle = NULL ; | |
613a24f7 | 2097 | } |
20b69855 | 2098 | |
68654a82 | 2099 | if ( m_font.Ok() ) |
613a24f7 | 2100 | { |
ec4a2b5d DS |
2101 | OSStatus status ; |
2102 | ||
68654a82 | 2103 | status = ATSUCreateAndCopyStyle( (ATSUStyle) m_font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle ) ; |
b21d67a0 | 2104 | |
ec4a2b5d | 2105 | wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") ) ; |
20b69855 | 2106 | |
68654a82 SC |
2107 | Fixed atsuSize = IntToFixed( int(m_scaleY * m_font.MacGetFontSize()) ) ; |
2108 | RGBColor atsuColor = MAC_WXCOLORREF( m_textForegroundColour.GetPixel() ) ; | |
2109 | ATSUAttributeTag atsuTags[] = | |
2110 | { | |
2111 | kATSUSizeTag , | |
2112 | kATSUColorTag , | |
2113 | } ; | |
b21d67a0 | 2114 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = |
68654a82 SC |
2115 | { |
2116 | sizeof( Fixed ) , | |
2117 | sizeof( RGBColor ) , | |
2118 | } ; | |
b21d67a0 | 2119 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = |
68654a82 SC |
2120 | { |
2121 | &atsuSize , | |
2122 | &atsuColor , | |
2123 | } ; | |
b21d67a0 | 2124 | |
ec4a2b5d DS |
2125 | status = ::ATSUSetAttributes( |
2126 | (ATSUStyle)m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) , | |
68654a82 SC |
2127 | atsuTags, atsuSizes, atsuValues); |
2128 | ||
ec4a2b5d | 2129 | wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ) ; |
68654a82 | 2130 | } |
613a24f7 SC |
2131 | } |
2132 | ||
eb1a7cf9 DS |
2133 | #pragma mark - |
2134 | ||
613a24f7 SC |
2135 | // --------------------------------------------------------------------------- |
2136 | // coordinates transformations | |
2137 | // --------------------------------------------------------------------------- | |
2138 | ||
2139 | wxCoord wxDCBase::DeviceToLogicalX(wxCoord x) const | |
2140 | { | |
2141 | return ((wxDC *)this)->XDEV2LOG(x); | |
2142 | } | |
2143 | ||
2144 | wxCoord wxDCBase::DeviceToLogicalY(wxCoord y) const | |
2145 | { | |
2146 | return ((wxDC *)this)->YDEV2LOG(y); | |
2147 | } | |
2148 | ||
2149 | wxCoord wxDCBase::DeviceToLogicalXRel(wxCoord x) const | |
2150 | { | |
2151 | return ((wxDC *)this)->XDEV2LOGREL(x); | |
2152 | } | |
2153 | ||
2154 | wxCoord wxDCBase::DeviceToLogicalYRel(wxCoord y) const | |
2155 | { | |
2156 | return ((wxDC *)this)->YDEV2LOGREL(y); | |
2157 | } | |
2158 | ||
2159 | wxCoord wxDCBase::LogicalToDeviceX(wxCoord x) const | |
2160 | { | |
2161 | return ((wxDC *)this)->XLOG2DEV(x); | |
2162 | } | |
2163 | ||
2164 | wxCoord wxDCBase::LogicalToDeviceY(wxCoord y) const | |
2165 | { | |
2166 | return ((wxDC *)this)->YLOG2DEV(y); | |
2167 | } | |
2168 | ||
2169 | wxCoord wxDCBase::LogicalToDeviceXRel(wxCoord x) const | |
2170 | { | |
2171 | return ((wxDC *)this)->XLOG2DEVREL(x); | |
2172 | } | |
2173 | ||
2174 | wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const | |
2175 | { | |
2176 | return ((wxDC *)this)->YLOG2DEVREL(y); | |
2177 | } | |
20b69855 SC |
2178 | |
2179 | #endif // wxMAC_USE_CORE_GRAPHICS | |
2180 |