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