]>
Commit | Line | Data |
---|---|---|
50581042 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/mac/carbon/dccg.cpp | |
3 | // Purpose: wxDC class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
f43426c1 | 14 | #if wxUSE_GRAPHICS_CONTEXT && wxMAC_USE_CORE_GRAPHICS |
50581042 | 15 | |
83b96a06 PC |
16 | #include "wx/graphics.h" |
17 | ||
50581042 | 18 | #ifndef WX_PRECOMP |
cc5de8fe | 19 | #include "wx/dcclient.h" |
50581042 | 20 | #include "wx/log.h" |
50581042 | 21 | #include "wx/region.h" |
50581042 SC |
22 | #endif |
23 | ||
24 | #include "wx/mac/uma.h" | |
25 | ||
50581042 SC |
26 | #ifdef __MSL__ |
27 | #if __MSL__ >= 0x6000 | |
28 | #include "math.h" | |
29 | // in case our functions were defined outside std, we make it known all the same | |
30 | namespace std { } | |
31 | using namespace std; | |
32 | #endif | |
33 | #endif | |
34 | ||
35 | #include "wx/mac/private.h" | |
36 | ||
37 | #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4 | |
38 | typedef float CGFloat; | |
39 | #endif | |
40 | ||
eec960fa SC |
41 | //----------------------------------------------------------------------------- |
42 | // constants | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | #if !defined( __DARWIN__ ) || defined(__MWERKS__) | |
46 | #ifndef M_PI | |
47 | const double M_PI = 3.14159265358979; | |
48 | #endif | |
49 | #endif | |
50 | ||
51 | static const double RAD2DEG = 180.0 / M_PI; | |
52 | ||
50581042 SC |
53 | // |
54 | // Graphics Path | |
55 | // | |
56 | ||
57 | class WXDLLEXPORT wxMacCoreGraphicsPath : public wxGraphicsPath | |
58 | { | |
50581042 SC |
59 | public : |
60 | wxMacCoreGraphicsPath(); | |
61 | ~wxMacCoreGraphicsPath(); | |
62 | ||
63 | // begins a new subpath at (x,y) | |
64 | virtual void MoveToPoint( wxDouble x, wxDouble y ); | |
65 | ||
66 | // adds a straight line from the current point to (x,y) | |
67 | virtual void AddLineToPoint( wxDouble x, wxDouble y ); | |
68 | ||
69 | // adds a cubic Bezier curve from the current point, using two control points and an end point | |
70 | virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ); | |
71 | ||
72 | // closes the current sub-path | |
73 | virtual void CloseSubpath(); | |
74 | ||
75 | // gets the last point of the current path, (0,0) if not yet set | |
76 | virtual void GetCurrentPoint( wxDouble& x, wxDouble&y); | |
77 | ||
78 | // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle | |
79 | virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ); | |
80 | ||
81 | // | |
82 | // These are convenience functions which - if not available natively will be assembled | |
83 | // using the primitives from above | |
84 | // | |
85 | ||
86 | // adds a quadratic Bezier curve from the current point, using a control point and an end point | |
87 | virtual void AddQuadCurveToPoint( wxDouble cx, wxDouble cy, wxDouble x, wxDouble y ); | |
88 | ||
89 | // appends a rectangle as a new closed subpath | |
90 | virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
91 | ||
92 | // appends an ellipsis as a new closed subpath fitting the passed rectangle | |
93 | virtual void AddCircle( wxDouble x, wxDouble y, wxDouble r ); | |
94 | ||
95 | // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1) | |
96 | virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ); | |
97 | ||
eec960fa SC |
98 | // returns the native path |
99 | virtual void * GetNativePath() const { return m_path; } | |
100 | ||
101 | // give the native path returned by GetNativePath() back (there might be some deallocations necessary) | |
102 | virtual void UnGetNativePath(void *p) {} | |
103 | ||
104 | DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsPath) | |
105 | DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsPath) | |
50581042 SC |
106 | private : |
107 | CGMutablePathRef m_path; | |
108 | }; | |
109 | ||
eec960fa SC |
110 | //----------------------------------------------------------------------------- |
111 | // wxGraphicsPath implementation | |
112 | //----------------------------------------------------------------------------- | |
113 | ||
114 | IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsPath, wxGraphicsPath) | |
115 | ||
50581042 SC |
116 | wxMacCoreGraphicsPath::wxMacCoreGraphicsPath() |
117 | { | |
118 | m_path = CGPathCreateMutable(); | |
119 | } | |
120 | ||
121 | wxMacCoreGraphicsPath::~wxMacCoreGraphicsPath() | |
122 | { | |
123 | CGPathRelease( m_path ); | |
124 | } | |
125 | ||
126 | // opens (starts) a new subpath | |
127 | void wxMacCoreGraphicsPath::MoveToPoint( wxDouble x1 , wxDouble y1 ) | |
128 | { | |
129 | CGPathMoveToPoint( m_path , NULL , x1 , y1 ); | |
130 | } | |
131 | ||
132 | void wxMacCoreGraphicsPath::AddLineToPoint( wxDouble x1 , wxDouble y1 ) | |
133 | { | |
134 | CGPathAddLineToPoint( m_path , NULL , x1 , y1 ); | |
135 | } | |
136 | ||
137 | void wxMacCoreGraphicsPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y ) | |
138 | { | |
139 | CGPathAddCurveToPoint( m_path , NULL , cx1 , cy1 , cx2, cy2, x , y ); | |
140 | } | |
141 | ||
142 | void wxMacCoreGraphicsPath::AddQuadCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble x, wxDouble y ) | |
143 | { | |
144 | CGPathAddQuadCurveToPoint( m_path , NULL , cx1 , cy1 , x , y ); | |
145 | } | |
146 | ||
147 | void wxMacCoreGraphicsPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
148 | { | |
149 | CGRect cgRect = { { x , y } , { w , h } }; | |
150 | CGPathAddRect( m_path , NULL , cgRect ); | |
151 | } | |
152 | ||
153 | void wxMacCoreGraphicsPath::AddCircle( wxDouble x, wxDouble y , wxDouble r ) | |
154 | { | |
155 | CGPathAddArc( m_path , NULL , x , y , r , 0.0 , 2 * M_PI , true ); | |
156 | } | |
157 | ||
158 | // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle | |
159 | void wxMacCoreGraphicsPath::AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) | |
160 | { | |
a798c64c SC |
161 | // inverse direction as we the 'normal' state is a y axis pointing down, ie mirrored to the standard core graphics setup |
162 | CGPathAddArc( m_path, NULL , x, y, r, startAngle, endAngle, !clockwise); | |
50581042 SC |
163 | } |
164 | ||
165 | void wxMacCoreGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) | |
166 | { | |
167 | CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r); | |
168 | } | |
169 | ||
170 | // closes the current subpath | |
171 | void wxMacCoreGraphicsPath::CloseSubpath() | |
172 | { | |
173 | CGPathCloseSubpath( m_path ); | |
174 | } | |
175 | ||
50581042 SC |
176 | // gets the last point of the current path, (0,0) if not yet set |
177 | void wxMacCoreGraphicsPath::GetCurrentPoint( wxDouble& x, wxDouble&y) | |
178 | { | |
179 | CGPoint p = CGPathGetCurrentPoint( m_path ); | |
180 | x = p.x; | |
181 | y = p.y; | |
182 | } | |
183 | ||
184 | // | |
185 | // Graphics Context | |
186 | // | |
187 | ||
188 | class WXDLLEXPORT wxMacCoreGraphicsContext : public wxGraphicsContext | |
189 | { | |
50581042 | 190 | public: |
50581042 SC |
191 | wxMacCoreGraphicsContext( CGContextRef cgcontext ); |
192 | ||
1056ddcf SC |
193 | wxMacCoreGraphicsContext( WindowRef window ); |
194 | ||
eec960fa SC |
195 | wxMacCoreGraphicsContext( wxWindow* window ); |
196 | ||
50581042 SC |
197 | wxMacCoreGraphicsContext(); |
198 | ||
199 | ~wxMacCoreGraphicsContext(); | |
200 | ||
1056ddcf | 201 | void Init(); |
50581042 SC |
202 | |
203 | // creates a path instance that corresponds to the type of graphics context, ie GDIPlus, cairo, CoreGraphics ... | |
204 | virtual wxGraphicsPath * CreatePath(); | |
205 | ||
206 | // push the current state of the context, ie the transformation matrix on a stack | |
207 | virtual void PushState(); | |
208 | ||
209 | // pops a stored state from the stack | |
210 | virtual void PopState(); | |
211 | ||
212 | // clips drawings to the region | |
213 | virtual void Clip( const wxRegion ®ion ); | |
214 | ||
1056ddcf SC |
215 | // clips drawings to the rect |
216 | virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
217 | ||
218 | // resets the clipping to original extent | |
219 | virtual void ResetClip(); | |
220 | ||
221 | virtual void * GetNativeContext(); | |
222 | ||
50581042 SC |
223 | // |
224 | // transformation | |
225 | // | |
226 | ||
227 | // translate | |
228 | virtual void Translate( wxDouble dx , wxDouble dy ); | |
229 | ||
230 | // scale | |
231 | virtual void Scale( wxDouble xScale , wxDouble yScale ); | |
232 | ||
233 | // rotate (radians) | |
234 | virtual void Rotate( wxDouble angle ); | |
235 | ||
236 | // | |
237 | // setting the paint | |
238 | // | |
239 | ||
240 | // sets the pan | |
241 | virtual void SetPen( const wxPen &pen ); | |
242 | ||
243 | // sets the brush for filling | |
244 | virtual void SetBrush( const wxBrush &brush ); | |
245 | ||
246 | // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2 | |
247 | virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, | |
248 | const wxColour&c1, const wxColour&c2); | |
249 | ||
250 | // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc) | |
251 | // with radius r and color cColor | |
252 | virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, | |
253 | const wxColour &oColor, const wxColour &cColor); | |
254 | ||
255 | // sets the font | |
256 | virtual void SetFont( const wxFont &font ); | |
257 | ||
258 | // sets the text color | |
259 | virtual void SetTextColor( const wxColour &col ); | |
260 | ||
261 | // strokes along a path with the current pen | |
262 | virtual void StrokePath( const wxGraphicsPath *path ); | |
263 | ||
264 | // fills a path with the current brush | |
265 | virtual void FillPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE ); | |
266 | ||
267 | // draws a path by first filling and then stroking | |
268 | virtual void DrawPath( const wxGraphicsPath *path, int fillStyle = wxWINDING_RULE ); | |
269 | ||
270 | // | |
271 | // text | |
272 | // | |
273 | ||
274 | virtual void DrawText( const wxString &str, wxDouble x, wxDouble y ); | |
275 | ||
276 | virtual void DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle ); | |
277 | ||
278 | virtual void GetTextExtent( const wxString &text, wxDouble *width, wxDouble *height, | |
279 | wxDouble *descent, wxDouble *externalLeading ) const; | |
280 | ||
281 | virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const; | |
282 | ||
283 | // | |
284 | // image support | |
285 | // | |
286 | ||
287 | virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
288 | ||
289 | virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ); | |
290 | ||
50581042 SC |
291 | void SetNativeContext( CGContextRef cg ); |
292 | CGPathDrawingMode GetDrawingMode() const { return m_mode; } | |
de3cb39f RD |
293 | |
294 | ||
295 | virtual bool ShouldOffset() const | |
296 | { | |
297 | int penwidth = m_pen.GetWidth(); | |
298 | if ( penwidth == 0 ) | |
299 | penwidth = 1; | |
300 | if ( m_pen.GetStyle() == wxTRANSPARENT ) | |
301 | penwidth = 0; | |
302 | return ( penwidth % 2 ) == 1; | |
303 | } | |
304 | ||
50581042 | 305 | |
eec960fa SC |
306 | DECLARE_NO_COPY_CLASS(wxMacCoreGraphicsContext) |
307 | DECLARE_DYNAMIC_CLASS(wxMacCoreGraphicsContext) | |
308 | ||
50581042 | 309 | private: |
eec960fa SC |
310 | void EnsureIsValid(); |
311 | ||
50581042 | 312 | CGContextRef m_cgContext; |
1056ddcf | 313 | WindowRef m_windowRef; |
eec960fa SC |
314 | int m_originX; |
315 | int m_originY; | |
316 | wxMacCFRefHolder<HIShapeRef> m_clipRgn; | |
1056ddcf | 317 | bool m_releaseContext; |
50581042 SC |
318 | CGPathDrawingMode m_mode; |
319 | ATSUStyle m_macATSUIStyle; | |
320 | wxPen m_pen; | |
321 | wxBrush m_brush; | |
1056ddcf | 322 | wxFont m_font; |
50581042 SC |
323 | wxColor m_textForegroundColor; |
324 | }; | |
325 | ||
50581042 SC |
326 | //----------------------------------------------------------------------------- |
327 | // device context implementation | |
328 | // | |
329 | // more and more of the dc functionality should be implemented by calling | |
330 | // the appropricate wxMacCoreGraphicsContext, but we will have to do that step by step | |
331 | // also coordinate conversions should be moved to native matrix ops | |
332 | //----------------------------------------------------------------------------- | |
333 | ||
334 | // we always stock two context states, one at entry, to be able to preserve the | |
335 | // state we were called with, the other one after changing to HI Graphics orientation | |
336 | // (this one is used for getting back clippings etc) | |
337 | ||
50581042 SC |
338 | //----------------------------------------------------------------------------- |
339 | // wxGraphicsContext implementation | |
340 | //----------------------------------------------------------------------------- | |
341 | ||
eec960fa SC |
342 | IMPLEMENT_DYNAMIC_CLASS(wxMacCoreGraphicsContext, wxGraphicsContext) |
343 | ||
1056ddcf | 344 | void wxMacCoreGraphicsContext::Init() |
50581042 | 345 | { |
50581042 SC |
346 | m_cgContext = NULL; |
347 | m_mode = kCGPathFill; | |
348 | m_macATSUIStyle = NULL; | |
1056ddcf | 349 | m_releaseContext = false; |
eec960fa | 350 | m_clipRgn.Set(HIShapeCreateEmpty()); |
50581042 SC |
351 | } |
352 | ||
353 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( CGContextRef cgcontext ) | |
354 | { | |
1056ddcf SC |
355 | Init(); |
356 | m_cgContext = cgcontext; | |
50581042 SC |
357 | CGContextSaveGState( m_cgContext ); |
358 | CGContextSaveGState( m_cgContext ); | |
359 | } | |
360 | ||
1056ddcf SC |
361 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( WindowRef window ) |
362 | { | |
363 | Init(); | |
364 | m_windowRef = window; | |
eec960fa SC |
365 | } |
366 | ||
367 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext( wxWindow* window ) | |
368 | { | |
369 | Init(); | |
370 | m_windowRef = (WindowRef) window->MacGetTopLevelWindowRef(); | |
371 | m_originX = m_originY = 0; | |
372 | window->MacWindowToRootWindow( &m_originX , &m_originY ); | |
1056ddcf SC |
373 | } |
374 | ||
50581042 SC |
375 | wxMacCoreGraphicsContext::wxMacCoreGraphicsContext() |
376 | { | |
1056ddcf | 377 | Init(); |
50581042 SC |
378 | } |
379 | ||
380 | wxMacCoreGraphicsContext::~wxMacCoreGraphicsContext() | |
381 | { | |
382 | if ( m_cgContext ) | |
383 | { | |
eec960fa | 384 | // TODO : when is this necessary - should we add a Flush() method ? CGContextSynchronize( m_cgContext ); |
50581042 SC |
385 | CGContextRestoreGState( m_cgContext ); |
386 | CGContextRestoreGState( m_cgContext ); | |
387 | } | |
388 | ||
1056ddcf SC |
389 | if ( m_releaseContext ) |
390 | QDEndCGContext( GetWindowPort( m_windowRef ) , &m_cgContext); | |
50581042 SC |
391 | } |
392 | ||
eec960fa SC |
393 | void wxMacCoreGraphicsContext::EnsureIsValid() |
394 | { | |
395 | if ( !m_cgContext ) | |
396 | { | |
397 | OSStatus status = QDBeginCGContext( GetWindowPort( m_windowRef ) , &m_cgContext ); | |
398 | wxASSERT_MSG( status == noErr , wxT("Cannot nest wxDCs on the same window") ); | |
399 | Rect bounds; | |
400 | GetWindowBounds( m_windowRef, kWindowContentRgn, &bounds ); | |
401 | CGContextSaveGState( m_cgContext ); | |
402 | CGContextTranslateCTM( m_cgContext , 0 , bounds.bottom - bounds.top ); | |
403 | CGContextScaleCTM( m_cgContext , 1 , -1 ); | |
404 | CGContextTranslateCTM( m_cgContext, m_originX, m_originY ); | |
405 | CGContextSaveGState( m_cgContext ); | |
406 | m_releaseContext = true; | |
407 | if ( !HIShapeIsEmpty(m_clipRgn) ) | |
408 | { | |
409 | HIShapeReplacePathInCGContext( m_clipRgn, m_cgContext ); | |
410 | CGContextClip( m_cgContext ); | |
411 | } | |
412 | } | |
413 | } | |
414 | ||
415 | ||
50581042 SC |
416 | void wxMacCoreGraphicsContext::Clip( const wxRegion ®ion ) |
417 | { | |
eec960fa SC |
418 | if( m_cgContext ) |
419 | { | |
420 | HIShapeRef shape = HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() ); | |
421 | HIShapeReplacePathInCGContext( shape, m_cgContext ); | |
422 | CGContextClip( m_cgContext ); | |
423 | CFRelease( shape ); | |
424 | } | |
425 | else | |
426 | { | |
427 | m_clipRgn.Set(HIShapeCreateWithQDRgn( (RgnHandle) region.GetWXHRGN() )); | |
428 | } | |
1056ddcf SC |
429 | } |
430 | ||
431 | // clips drawings to the rect | |
432 | void wxMacCoreGraphicsContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
433 | { | |
eec960fa SC |
434 | HIRect r = CGRectMake( x , y , w , h ); |
435 | if ( m_cgContext ) | |
436 | { | |
437 | CGContextClipToRect( m_cgContext, r ); | |
438 | } | |
439 | else | |
440 | { | |
441 | m_clipRgn.Set(HIShapeCreateWithRect(&r)); | |
442 | } | |
1056ddcf SC |
443 | } |
444 | ||
445 | // resets the clipping to original extent | |
446 | void wxMacCoreGraphicsContext::ResetClip() | |
447 | { | |
eec960fa SC |
448 | if ( m_cgContext ) |
449 | { | |
450 | CGContextRestoreGState( m_cgContext ); | |
451 | CGContextSaveGState( m_cgContext ); | |
452 | } | |
453 | else | |
454 | { | |
455 | m_clipRgn.Set(HIShapeCreateEmpty()); | |
456 | } | |
50581042 SC |
457 | } |
458 | ||
eec960fa | 459 | void wxMacCoreGraphicsContext::StrokePath( const wxGraphicsPath *path ) |
50581042 | 460 | { |
de3cb39f RD |
461 | EnsureIsValid(); |
462 | ||
463 | bool offset = ShouldOffset(); | |
1056ddcf SC |
464 | |
465 | if ( offset ) | |
466 | CGContextTranslateCTM( m_cgContext, 0.5, 0.5 ); | |
467 | ||
eec960fa | 468 | CGContextAddPath( m_cgContext , (CGPathRef) path->GetNativePath() ); |
50581042 | 469 | CGContextStrokePath( m_cgContext ); |
1056ddcf SC |
470 | |
471 | if ( offset ) | |
472 | CGContextTranslateCTM( m_cgContext, -0.5, -0.5 ); | |
50581042 SC |
473 | } |
474 | ||
eec960fa | 475 | void wxMacCoreGraphicsContext::DrawPath( const wxGraphicsPath *path , int fillStyle ) |
50581042 | 476 | { |
eec960fa SC |
477 | EnsureIsValid(); |
478 | ||
50581042 SC |
479 | CGPathDrawingMode mode = m_mode; |
480 | ||
481 | if ( fillStyle == wxODDEVEN_RULE ) | |
482 | { | |
483 | if ( mode == kCGPathFill ) | |
484 | mode = kCGPathEOFill; | |
485 | else if ( mode == kCGPathFillStroke ) | |
486 | mode = kCGPathEOFillStroke; | |
487 | } | |
de3cb39f RD |
488 | |
489 | bool offset = ShouldOffset(); | |
1056ddcf SC |
490 | |
491 | if ( offset ) | |
492 | CGContextTranslateCTM( m_cgContext, 0.5, 0.5 ); | |
493 | ||
eec960fa | 494 | CGContextAddPath( m_cgContext , (CGPathRef) path->GetNativePath() ); |
50581042 | 495 | CGContextDrawPath( m_cgContext , mode ); |
1056ddcf SC |
496 | |
497 | if ( offset ) | |
498 | CGContextTranslateCTM( m_cgContext, -0.5, -0.5 ); | |
50581042 SC |
499 | } |
500 | ||
eec960fa | 501 | void wxMacCoreGraphicsContext::FillPath( const wxGraphicsPath *path , int fillStyle ) |
50581042 | 502 | { |
eec960fa SC |
503 | EnsureIsValid(); |
504 | ||
505 | CGContextAddPath( m_cgContext , (CGPathRef) path->GetNativePath() ); | |
50581042 SC |
506 | if ( fillStyle == wxODDEVEN_RULE ) |
507 | CGContextEOFillPath( m_cgContext ); | |
508 | else | |
509 | CGContextFillPath( m_cgContext ); | |
510 | } | |
511 | ||
512 | wxGraphicsPath* wxMacCoreGraphicsContext::CreatePath() | |
513 | { | |
50581042 SC |
514 | return new wxMacCoreGraphicsPath(); |
515 | } | |
516 | ||
50581042 SC |
517 | void wxMacCoreGraphicsContext::SetNativeContext( CGContextRef cg ) |
518 | { | |
519 | // we allow either setting or clearing but not replacing | |
520 | wxASSERT( m_cgContext == NULL || cg == NULL ); | |
521 | ||
522 | if ( cg ) | |
523 | CGContextSaveGState( cg ); | |
524 | m_cgContext = cg; | |
525 | } | |
526 | ||
527 | void wxMacCoreGraphicsContext::Translate( wxDouble dx , wxDouble dy ) | |
528 | { | |
eec960fa SC |
529 | EnsureIsValid(); |
530 | ||
50581042 SC |
531 | CGContextTranslateCTM( m_cgContext, dx, dy ); |
532 | } | |
533 | ||
534 | void wxMacCoreGraphicsContext::Scale( wxDouble xScale , wxDouble yScale ) | |
535 | { | |
eec960fa SC |
536 | EnsureIsValid(); |
537 | ||
50581042 SC |
538 | CGContextScaleCTM( m_cgContext , xScale , yScale ); |
539 | } | |
540 | ||
541 | void wxMacCoreGraphicsContext::Rotate( wxDouble angle ) | |
542 | { | |
eec960fa SC |
543 | EnsureIsValid(); |
544 | ||
50581042 SC |
545 | CGContextRotateCTM( m_cgContext , angle ); |
546 | } | |
547 | ||
548 | void wxMacCoreGraphicsContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
549 | { | |
eec960fa SC |
550 | EnsureIsValid(); |
551 | ||
50581042 SC |
552 | CGImageRef image = (CGImageRef)( bmp.CGImageCreate() ); |
553 | HIRect r = CGRectMake( x , y , w , h ); | |
554 | HIViewDrawCGImage( m_cgContext , &r , image ); | |
555 | CGImageRelease( image ); | |
556 | } | |
557 | ||
558 | void wxMacCoreGraphicsContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h ) | |
559 | { | |
eec960fa SC |
560 | EnsureIsValid(); |
561 | ||
50581042 SC |
562 | CGRect r = CGRectMake( 00 , 00 , w , h ); |
563 | CGContextSaveGState( m_cgContext ); | |
564 | CGContextTranslateCTM( m_cgContext, x , y + h ); | |
565 | CGContextScaleCTM( m_cgContext, 1, -1 ); | |
566 | PlotIconRefInContext( m_cgContext , &r , kAlignNone , kTransformNone , | |
567 | NULL , kPlotIconRefNormalFlags , MAC_WXHICON( icon.GetHICON() ) ); | |
568 | CGContextRestoreGState( m_cgContext ); | |
569 | } | |
570 | ||
571 | void wxMacCoreGraphicsContext::PushState() | |
572 | { | |
eec960fa SC |
573 | EnsureIsValid(); |
574 | ||
50581042 SC |
575 | CGContextSaveGState( m_cgContext ); |
576 | } | |
577 | ||
578 | void wxMacCoreGraphicsContext::PopState() | |
579 | { | |
eec960fa SC |
580 | EnsureIsValid(); |
581 | ||
50581042 SC |
582 | CGContextRestoreGState( m_cgContext ); |
583 | } | |
584 | ||
585 | void wxMacCoreGraphicsContext::SetTextColor( const wxColour &col ) | |
586 | { | |
587 | m_textForegroundColor = col; | |
1056ddcf SC |
588 | // to recreate the native font after color change |
589 | SetFont( m_font ); | |
50581042 SC |
590 | } |
591 | ||
592 | #pragma mark - | |
593 | #pragma mark wxMacCoreGraphicsPattern, ImagePattern, HatchPattern classes | |
594 | ||
595 | // CGPattern wrapper class: always allocate on heap, never call destructor | |
596 | ||
597 | class wxMacCoreGraphicsPattern | |
598 | { | |
599 | public : | |
600 | wxMacCoreGraphicsPattern() {} | |
601 | ||
602 | // is guaranteed to be called only with a non-Null CGContextRef | |
603 | virtual void Render( CGContextRef ctxRef ) = 0; | |
604 | ||
605 | operator CGPatternRef() const { return m_patternRef; } | |
606 | ||
607 | protected : | |
608 | virtual ~wxMacCoreGraphicsPattern() | |
609 | { | |
610 | // as this is called only when the m_patternRef is been released; | |
611 | // don't release it again | |
612 | } | |
613 | ||
614 | static void _Render( void *info, CGContextRef ctxRef ) | |
615 | { | |
616 | wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info; | |
617 | if ( self && ctxRef ) | |
618 | self->Render( ctxRef ); | |
619 | } | |
620 | ||
621 | static void _Dispose( void *info ) | |
622 | { | |
623 | wxMacCoreGraphicsPattern* self = (wxMacCoreGraphicsPattern*) info; | |
624 | delete self; | |
625 | } | |
626 | ||
627 | CGPatternRef m_patternRef; | |
628 | ||
629 | static const CGPatternCallbacks ms_Callbacks; | |
630 | }; | |
631 | ||
632 | const CGPatternCallbacks wxMacCoreGraphicsPattern::ms_Callbacks = { 0, &wxMacCoreGraphicsPattern::_Render, &wxMacCoreGraphicsPattern::_Dispose }; | |
633 | ||
634 | class ImagePattern : public wxMacCoreGraphicsPattern | |
635 | { | |
636 | public : | |
83b96a06 | 637 | ImagePattern( const wxBitmap* bmp , const CGAffineTransform& transform ) |
50581042 SC |
638 | { |
639 | wxASSERT( bmp && bmp->Ok() ); | |
640 | ||
641 | Init( (CGImageRef) bmp->CGImageCreate() , transform ); | |
642 | } | |
643 | ||
644 | // ImagePattern takes ownership of CGImageRef passed in | |
83b96a06 | 645 | ImagePattern( CGImageRef image , const CGAffineTransform& transform ) |
50581042 SC |
646 | { |
647 | if ( image ) | |
648 | CFRetain( image ); | |
649 | ||
650 | Init( image , transform ); | |
651 | } | |
652 | ||
653 | virtual void Render( CGContextRef ctxRef ) | |
654 | { | |
655 | if (m_image != NULL) | |
656 | HIViewDrawCGImage( ctxRef, &m_imageBounds, m_image ); | |
657 | } | |
658 | ||
659 | protected : | |
83b96a06 | 660 | void Init( CGImageRef image, const CGAffineTransform& transform ) |
50581042 SC |
661 | { |
662 | m_image = image; | |
663 | if ( m_image ) | |
664 | { | |
665 | m_imageBounds = CGRectMake( 0.0, 0.0, (CGFloat)CGImageGetWidth( m_image ), (CGFloat)CGImageGetHeight( m_image ) ); | |
666 | m_patternRef = CGPatternCreate( | |
667 | this , m_imageBounds, transform , | |
668 | m_imageBounds.size.width, m_imageBounds.size.height, | |
669 | kCGPatternTilingNoDistortion, true , &wxMacCoreGraphicsPattern::ms_Callbacks ); | |
670 | } | |
671 | } | |
672 | ||
673 | virtual ~ImagePattern() | |
674 | { | |
675 | if ( m_image ) | |
676 | CGImageRelease( m_image ); | |
677 | } | |
678 | ||
679 | CGImageRef m_image; | |
680 | CGRect m_imageBounds; | |
681 | }; | |
682 | ||
683 | class HatchPattern : public wxMacCoreGraphicsPattern | |
684 | { | |
685 | public : | |
83b96a06 | 686 | HatchPattern( int hatchstyle, const CGAffineTransform& transform ) |
50581042 SC |
687 | { |
688 | m_hatch = hatchstyle; | |
689 | m_imageBounds = CGRectMake( 0.0, 0.0, 8.0 , 8.0 ); | |
690 | m_patternRef = CGPatternCreate( | |
691 | this , m_imageBounds, transform , | |
692 | m_imageBounds.size.width, m_imageBounds.size.height, | |
693 | kCGPatternTilingNoDistortion, false , &wxMacCoreGraphicsPattern::ms_Callbacks ); | |
694 | } | |
695 | ||
696 | void StrokeLineSegments( CGContextRef ctxRef , const CGPoint pts[] , size_t count ) | |
697 | { | |
698 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 | |
699 | if ( UMAGetSystemVersion() >= 0x1040 ) | |
700 | { | |
701 | CGContextStrokeLineSegments( ctxRef , pts , count ); | |
702 | } | |
703 | else | |
704 | #endif | |
705 | { | |
706 | CGContextBeginPath( ctxRef ); | |
707 | for (size_t i = 0; i < count; i += 2) | |
708 | { | |
709 | CGContextMoveToPoint(ctxRef, pts[i].x, pts[i].y); | |
710 | CGContextAddLineToPoint(ctxRef, pts[i+1].x, pts[i+1].y); | |
711 | } | |
712 | CGContextStrokePath(ctxRef); | |
713 | } | |
714 | } | |
715 | ||
716 | virtual void Render( CGContextRef ctxRef ) | |
717 | { | |
718 | switch ( m_hatch ) | |
719 | { | |
720 | case wxBDIAGONAL_HATCH : | |
721 | { | |
722 | CGPoint pts[] = | |
723 | { | |
724 | { 8.0 , 0.0 } , { 0.0 , 8.0 } | |
725 | }; | |
726 | StrokeLineSegments( ctxRef , pts , 2 ); | |
727 | } | |
728 | break; | |
729 | ||
730 | case wxCROSSDIAG_HATCH : | |
731 | { | |
732 | CGPoint pts[] = | |
733 | { | |
734 | { 0.0 , 0.0 } , { 8.0 , 8.0 } , | |
735 | { 8.0 , 0.0 } , { 0.0 , 8.0 } | |
736 | }; | |
737 | StrokeLineSegments( ctxRef , pts , 4 ); | |
738 | } | |
739 | break; | |
740 | ||
741 | case wxFDIAGONAL_HATCH : | |
742 | { | |
743 | CGPoint pts[] = | |
744 | { | |
745 | { 0.0 , 0.0 } , { 8.0 , 8.0 } | |
746 | }; | |
747 | StrokeLineSegments( ctxRef , pts , 2 ); | |
748 | } | |
749 | break; | |
750 | ||
751 | case wxCROSS_HATCH : | |
752 | { | |
753 | CGPoint pts[] = | |
754 | { | |
755 | { 0.0 , 4.0 } , { 8.0 , 4.0 } , | |
756 | { 4.0 , 0.0 } , { 4.0 , 8.0 } , | |
757 | }; | |
758 | StrokeLineSegments( ctxRef , pts , 4 ); | |
759 | } | |
760 | break; | |
761 | ||
762 | case wxHORIZONTAL_HATCH : | |
763 | { | |
764 | CGPoint pts[] = | |
765 | { | |
766 | { 0.0 , 4.0 } , { 8.0 , 4.0 } , | |
767 | }; | |
768 | StrokeLineSegments( ctxRef , pts , 2 ); | |
769 | } | |
770 | break; | |
771 | ||
772 | case wxVERTICAL_HATCH : | |
773 | { | |
774 | CGPoint pts[] = | |
775 | { | |
776 | { 4.0 , 0.0 } , { 4.0 , 8.0 } , | |
777 | }; | |
778 | StrokeLineSegments( ctxRef , pts , 2 ); | |
779 | } | |
780 | break; | |
781 | ||
782 | default: | |
783 | break; | |
784 | } | |
785 | } | |
786 | ||
787 | protected : | |
788 | virtual ~HatchPattern() {} | |
789 | ||
790 | CGRect m_imageBounds; | |
791 | int m_hatch; | |
792 | }; | |
793 | ||
794 | #pragma mark - | |
795 | ||
796 | void wxMacCoreGraphicsContext::SetPen( const wxPen &pen ) | |
797 | { | |
798 | m_pen = pen; | |
799 | if ( m_cgContext == NULL ) | |
800 | return; | |
801 | ||
802 | bool fill = m_brush.GetStyle() != wxTRANSPARENT; | |
803 | bool stroke = pen.GetStyle() != wxTRANSPARENT; | |
804 | ||
805 | #if 0 | |
806 | // we can benchmark performance; should go into a setting eventually | |
807 | CGContextSetShouldAntialias( m_cgContext , false ); | |
808 | #endif | |
809 | ||
83b96a06 | 810 | if ( fill || stroke ) |
50581042 SC |
811 | { |
812 | // set up brushes | |
813 | m_mode = kCGPathFill; // just a default | |
814 | ||
815 | if ( stroke ) | |
816 | { | |
817 | CGContextSetRGBStrokeColor( m_cgContext , pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 , | |
818 | pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 ); | |
819 | ||
820 | // TODO: * m_dc->m_scaleX | |
821 | CGFloat penWidth = pen.GetWidth(); | |
822 | if (penWidth <= 0.0) | |
823 | penWidth = 0.1; | |
824 | CGContextSetLineWidth( m_cgContext , penWidth ); | |
825 | ||
826 | CGLineCap cap; | |
827 | switch ( pen.GetCap() ) | |
828 | { | |
829 | case wxCAP_ROUND : | |
830 | cap = kCGLineCapRound; | |
831 | break; | |
832 | ||
833 | case wxCAP_PROJECTING : | |
834 | cap = kCGLineCapSquare; | |
835 | break; | |
836 | ||
837 | case wxCAP_BUTT : | |
838 | cap = kCGLineCapButt; | |
839 | break; | |
840 | ||
841 | default : | |
842 | cap = kCGLineCapButt; | |
843 | break; | |
844 | } | |
845 | ||
846 | CGLineJoin join; | |
847 | switch ( pen.GetJoin() ) | |
848 | { | |
849 | case wxJOIN_BEVEL : | |
850 | join = kCGLineJoinBevel; | |
851 | break; | |
852 | ||
853 | case wxJOIN_MITER : | |
854 | join = kCGLineJoinMiter; | |
855 | break; | |
856 | ||
857 | case wxJOIN_ROUND : | |
858 | join = kCGLineJoinRound; | |
859 | break; | |
860 | ||
861 | default : | |
862 | join = kCGLineJoinMiter; | |
863 | break; | |
864 | } | |
865 | ||
866 | m_mode = kCGPathStroke; | |
867 | int count = 0; | |
868 | ||
869 | const CGFloat *lengths = NULL; | |
870 | CGFloat *userLengths = NULL; | |
871 | ||
872 | const CGFloat dashUnit = penWidth < 1.0 ? 1.0 : penWidth; | |
873 | ||
874 | const CGFloat dotted[] = { dashUnit , dashUnit + 2.0 }; | |
875 | const CGFloat short_dashed[] = { 9.0 , 6.0 }; | |
876 | const CGFloat dashed[] = { 19.0 , 9.0 }; | |
877 | const CGFloat dotted_dashed[] = { 9.0 , 6.0 , 3.0 , 3.0 }; | |
878 | ||
879 | switch ( pen.GetStyle() ) | |
880 | { | |
881 | case wxSOLID : | |
882 | break; | |
883 | ||
884 | case wxDOT : | |
885 | lengths = dotted; | |
886 | count = WXSIZEOF(dotted); | |
887 | break; | |
888 | ||
889 | case wxLONG_DASH : | |
890 | lengths = dashed; | |
891 | count = WXSIZEOF(dashed); | |
892 | break; | |
893 | ||
894 | case wxSHORT_DASH : | |
895 | lengths = short_dashed; | |
896 | count = WXSIZEOF(short_dashed); | |
897 | break; | |
898 | ||
899 | case wxDOT_DASH : | |
900 | lengths = dotted_dashed; | |
901 | count = WXSIZEOF(dotted_dashed); | |
902 | break; | |
903 | ||
904 | case wxUSER_DASH : | |
905 | wxDash *dashes; | |
906 | count = pen.GetDashes( &dashes ); | |
907 | if ((dashes != NULL) && (count > 0)) | |
908 | { | |
909 | userLengths = new CGFloat[count]; | |
910 | for ( int i = 0; i < count; ++i ) | |
911 | { | |
912 | userLengths[i] = dashes[i] * dashUnit; | |
913 | ||
914 | if ( i % 2 == 1 && userLengths[i] < dashUnit + 2.0 ) | |
915 | userLengths[i] = dashUnit + 2.0; | |
916 | else if ( i % 2 == 0 && userLengths[i] < dashUnit ) | |
917 | userLengths[i] = dashUnit; | |
918 | } | |
919 | } | |
920 | lengths = userLengths; | |
921 | break; | |
922 | ||
923 | case wxSTIPPLE : | |
924 | { | |
925 | CGFloat alphaArray[1] = { 1.0 }; | |
926 | wxBitmap* bmp = pen.GetStipple(); | |
927 | if ( bmp && bmp->Ok() ) | |
928 | { | |
929 | wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) ); | |
930 | CGContextSetStrokeColorSpace( m_cgContext , patternSpace ); | |
931 | wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) ); | |
932 | CGContextSetStrokePattern( m_cgContext, pattern , alphaArray ); | |
933 | } | |
934 | } | |
935 | break; | |
936 | ||
937 | default : | |
938 | { | |
939 | wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ); | |
940 | CGContextSetStrokeColorSpace( m_cgContext , patternSpace ); | |
941 | wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( pen.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) ); | |
942 | ||
943 | CGFloat colorArray[4] = { pen.GetColour().Red() / 255.0 , pen.GetColour().Green() / 255.0 , | |
944 | pen.GetColour().Blue() / 255.0 , pen.GetColour().Alpha() / 255.0 }; | |
945 | ||
946 | CGContextSetStrokePattern( m_cgContext, pattern , colorArray ); | |
947 | } | |
948 | break; | |
949 | } | |
950 | ||
951 | if ((lengths != NULL) && (count > 0)) | |
952 | { | |
953 | CGContextSetLineDash( m_cgContext , 0 , lengths , count ); | |
954 | // force the line cap, otherwise we get artifacts (overlaps) and just solid lines | |
955 | cap = kCGLineCapButt; | |
956 | } | |
957 | else | |
958 | { | |
959 | CGContextSetLineDash( m_cgContext , 0 , NULL , 0 ); | |
960 | } | |
961 | ||
962 | CGContextSetLineCap( m_cgContext , cap ); | |
963 | CGContextSetLineJoin( m_cgContext , join ); | |
964 | ||
965 | delete[] userLengths; | |
966 | } | |
967 | ||
968 | if ( fill && stroke ) | |
969 | m_mode = kCGPathFillStroke; | |
970 | } | |
971 | } | |
972 | ||
973 | void wxMacCoreGraphicsContext::SetBrush( const wxBrush &brush ) | |
974 | { | |
975 | m_brush = brush; | |
976 | if ( m_cgContext == NULL ) | |
977 | return; | |
978 | ||
979 | bool fill = brush.GetStyle() != wxTRANSPARENT; | |
980 | bool stroke = m_pen.GetStyle() != wxTRANSPARENT; | |
981 | ||
982 | #if 0 | |
983 | // we can benchmark performance, should go into a setting later | |
984 | CGContextSetShouldAntialias( m_cgContext , false ); | |
985 | #endif | |
986 | ||
83b96a06 | 987 | if ( fill || stroke ) |
50581042 SC |
988 | { |
989 | // setup brushes | |
990 | m_mode = kCGPathFill; // just a default | |
991 | ||
992 | if ( fill ) | |
993 | { | |
994 | if ( brush.GetStyle() == wxSOLID ) | |
995 | { | |
996 | CGContextSetRGBFillColor( m_cgContext , brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 , | |
997 | brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 ); | |
998 | } | |
999 | else if ( brush.IsHatch() ) | |
1000 | { | |
1001 | wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( wxMacGetGenericRGBColorSpace() ) ); | |
1002 | CGContextSetFillColorSpace( m_cgContext , patternSpace ); | |
1003 | wxMacCFRefHolder<CGPatternRef> pattern( *( new HatchPattern( brush.GetStyle() , CGContextGetCTM( m_cgContext ) ) ) ); | |
1004 | ||
1005 | CGFloat colorArray[4] = { brush.GetColour().Red() / 255.0 , brush.GetColour().Green() / 255.0 , | |
1006 | brush.GetColour().Blue() / 255.0 , brush.GetColour().Alpha() / 255.0 }; | |
1007 | ||
1008 | CGContextSetFillPattern( m_cgContext, pattern , colorArray ); | |
1009 | } | |
1010 | else | |
1011 | { | |
1012 | // now brush is a bitmap | |
1013 | CGFloat alphaArray[1] = { 1.0 }; | |
1014 | wxBitmap* bmp = brush.GetStipple(); | |
1015 | if ( bmp && bmp->Ok() ) | |
1016 | { | |
1017 | wxMacCFRefHolder<CGColorSpaceRef> patternSpace( CGColorSpaceCreatePattern( NULL ) ); | |
1018 | CGContextSetFillColorSpace( m_cgContext , patternSpace ); | |
1019 | wxMacCFRefHolder<CGPatternRef> pattern( *( new ImagePattern( bmp , CGContextGetCTM( m_cgContext ) ) ) ); | |
1020 | CGContextSetFillPattern( m_cgContext, pattern , alphaArray ); | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | m_mode = kCGPathFill; | |
1025 | } | |
1026 | ||
1027 | if ( fill && stroke ) | |
1028 | m_mode = kCGPathFillStroke; | |
1029 | else if ( stroke ) | |
1030 | m_mode = kCGPathStroke; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2 | |
1035 | void wxMacCoreGraphicsContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, | |
1036 | const wxColour&c1, const wxColour&c2) | |
1037 | { | |
1038 | } | |
1039 | ||
1040 | // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc) | |
1041 | // with radius r and color cColor | |
1042 | void wxMacCoreGraphicsContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius, | |
1043 | const wxColour &oColor, const wxColour &cColor) | |
1044 | { | |
1045 | } | |
1046 | ||
1047 | ||
1048 | void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y ) | |
1049 | { | |
1050 | DrawText(str, x, y, 0.0); | |
1051 | } | |
1052 | ||
38af4365 | 1053 | void wxMacCoreGraphicsContext::DrawText( const wxString &str, wxDouble x, wxDouble y, wxDouble angle ) |
50581042 | 1054 | { |
eec960fa SC |
1055 | EnsureIsValid(); |
1056 | ||
50581042 SC |
1057 | OSStatus status = noErr; |
1058 | ATSUTextLayout atsuLayout; | |
1059 | UniCharCount chars = str.length(); | |
1060 | UniChar* ubuf = NULL; | |
1061 | ||
1062 | #if SIZEOF_WCHAR_T == 4 | |
1063 | wxMBConvUTF16 converter; | |
1064 | #if wxUSE_UNICODE | |
1065 | size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ); | |
1066 | ubuf = (UniChar*) malloc( unicharlen + 2 ); | |
1067 | converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ); | |
1068 | #else | |
1069 | const wxWCharBuffer wchar = str.wc_str( wxConvLocal ); | |
1070 | size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ); | |
1071 | ubuf = (UniChar*) malloc( unicharlen + 2 ); | |
1072 | converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ); | |
1073 | #endif | |
1074 | chars = unicharlen / 2; | |
1075 | #else | |
1076 | #if wxUSE_UNICODE | |
1077 | ubuf = (UniChar*) str.wc_str(); | |
1078 | #else | |
1079 | wxWCharBuffer wchar = str.wc_str( wxConvLocal ); | |
1080 | chars = wxWcslen( wchar.data() ); | |
1081 | ubuf = (UniChar*) wchar.data(); | |
1082 | #endif | |
1083 | #endif | |
1084 | ||
1085 | status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 , | |
1086 | &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ); | |
1087 | ||
1088 | wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the rotated text") ); | |
1089 | ||
1090 | status = ::ATSUSetTransientFontMatching( atsuLayout , true ); | |
1091 | wxASSERT_MSG( status == noErr , wxT("couldn't setup transient font matching") ); | |
1092 | ||
1093 | int iAngle = int( angle * RAD2DEG ); | |
1094 | if ( abs(iAngle) > 0 ) | |
1095 | { | |
1096 | Fixed atsuAngle = IntToFixed( iAngle ); | |
1097 | ATSUAttributeTag atsuTags[] = | |
1098 | { | |
1099 | kATSULineRotationTag , | |
1100 | }; | |
1101 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
1102 | { | |
1103 | sizeof( Fixed ) , | |
1104 | }; | |
1105 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
1106 | { | |
1107 | &atsuAngle , | |
1108 | }; | |
1109 | status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag), | |
1110 | atsuTags, atsuSizes, atsuValues ); | |
1111 | } | |
1112 | ||
1113 | { | |
1114 | ATSUAttributeTag atsuTags[] = | |
1115 | { | |
1116 | kATSUCGContextTag , | |
1117 | }; | |
1118 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
1119 | { | |
1120 | sizeof( CGContextRef ) , | |
1121 | }; | |
1122 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
1123 | { | |
1124 | &m_cgContext , | |
1125 | }; | |
1126 | status = ::ATSUSetLayoutControls(atsuLayout , sizeof(atsuTags) / sizeof(ATSUAttributeTag), | |
1127 | atsuTags, atsuSizes, atsuValues ); | |
1128 | } | |
1129 | ||
1130 | ATSUTextMeasurement textBefore, textAfter; | |
1131 | ATSUTextMeasurement ascent, descent; | |
1132 | ||
1133 | status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
1134 | &textBefore , &textAfter, &ascent , &descent ); | |
1135 | ||
1136 | wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") ); | |
1137 | ||
1138 | Rect rect; | |
1139 | /* | |
1140 | // TODO | |
1141 | if ( m_backgroundMode == wxSOLID ) | |
1142 | { | |
1143 | wxGraphicsPath* path = m_graphicContext->CreatePath(); | |
1144 | path->MoveToPoint( drawX , drawY ); | |
1145 | path->AddLineToPoint( | |
1146 | (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent)) , | |
1147 | (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent)) ); | |
1148 | path->AddLineToPoint( | |
1149 | (int) (drawX + sin(angle / RAD2DEG) * FixedToInt(ascent + descent ) + cos(angle / RAD2DEG) * FixedToInt(textAfter)) , | |
1150 | (int) (drawY + cos(angle / RAD2DEG) * FixedToInt(ascent + descent) - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ); | |
1151 | path->AddLineToPoint( | |
1152 | (int) (drawX + cos(angle / RAD2DEG) * FixedToInt(textAfter)) , | |
1153 | (int) (drawY - sin(angle / RAD2DEG) * FixedToInt(textAfter)) ); | |
1154 | ||
1155 | m_graphicContext->FillPath( path , m_textBackgroundColour ); | |
1156 | delete path; | |
1157 | } | |
1158 | */ | |
1159 | x += (int)(sin(angle / RAD2DEG) * FixedToInt(ascent)); | |
1160 | y += (int)(cos(angle / RAD2DEG) * FixedToInt(ascent)); | |
1161 | ||
1162 | status = ::ATSUMeasureTextImage( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
1163 | IntToFixed(x) , IntToFixed(y) , &rect ); | |
1164 | wxASSERT_MSG( status == noErr , wxT("couldn't measure the rotated text") ); | |
1165 | ||
1166 | CGContextSaveGState(m_cgContext); | |
1167 | CGContextTranslateCTM(m_cgContext, x, y); | |
1168 | CGContextScaleCTM(m_cgContext, 1, -1); | |
1169 | status = ::ATSUDrawText( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
1170 | IntToFixed(0) , IntToFixed(0) ); | |
1171 | ||
1172 | wxASSERT_MSG( status == noErr , wxT("couldn't draw the rotated text") ); | |
1173 | ||
1174 | CGContextRestoreGState(m_cgContext); | |
1175 | ||
1176 | ::ATSUDisposeTextLayout(atsuLayout); | |
1177 | ||
1178 | #if SIZEOF_WCHAR_T == 4 | |
1179 | free( ubuf ); | |
1180 | #endif | |
1181 | } | |
1182 | ||
1183 | void wxMacCoreGraphicsContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height, | |
1184 | wxDouble *descent, wxDouble *externalLeading ) const | |
1185 | { | |
1186 | wxCHECK_RET( m_macATSUIStyle != NULL, wxT("wxDC(cg)::DoGetTextExtent - no valid font set") ); | |
1187 | ||
1188 | OSStatus status = noErr; | |
1189 | ||
1190 | ATSUTextLayout atsuLayout; | |
1191 | UniCharCount chars = str.length(); | |
1192 | UniChar* ubuf = NULL; | |
1193 | ||
1194 | #if SIZEOF_WCHAR_T == 4 | |
1195 | wxMBConvUTF16 converter; | |
1196 | #if wxUSE_UNICODE | |
1197 | size_t unicharlen = converter.WC2MB( NULL , str.wc_str() , 0 ); | |
1198 | ubuf = (UniChar*) malloc( unicharlen + 2 ); | |
1199 | converter.WC2MB( (char*) ubuf , str.wc_str(), unicharlen + 2 ); | |
1200 | #else | |
1201 | const wxWCharBuffer wchar = str.wc_str( wxConvLocal ); | |
1202 | size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ); | |
1203 | ubuf = (UniChar*) malloc( unicharlen + 2 ); | |
1204 | converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ); | |
1205 | #endif | |
1206 | chars = unicharlen / 2; | |
1207 | #else | |
1208 | #if wxUSE_UNICODE | |
1209 | ubuf = (UniChar*) str.wc_str(); | |
1210 | #else | |
1211 | wxWCharBuffer wchar = str.wc_str( wxConvLocal ); | |
1212 | chars = wxWcslen( wchar.data() ); | |
1213 | ubuf = (UniChar*) wchar.data(); | |
1214 | #endif | |
1215 | #endif | |
1216 | ||
1217 | status = ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 , | |
1218 | &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ); | |
1219 | ||
1220 | wxASSERT_MSG( status == noErr , wxT("couldn't create the layout of the text") ); | |
1221 | ||
1222 | ATSUTextMeasurement textBefore, textAfter; | |
1223 | ATSUTextMeasurement textAscent, textDescent; | |
1224 | ||
1225 | status = ::ATSUGetUnjustifiedBounds( atsuLayout, kATSUFromTextBeginning, kATSUToTextEnd, | |
1226 | &textBefore , &textAfter, &textAscent , &textDescent ); | |
1227 | ||
1228 | if ( height ) | |
1229 | *height = FixedToInt(textAscent + textDescent); | |
1230 | if ( descent ) | |
1231 | *descent = FixedToInt(textDescent); | |
1232 | if ( externalLeading ) | |
1233 | *externalLeading = 0; | |
1234 | if ( width ) | |
1235 | *width = FixedToInt(textAfter - textBefore); | |
1236 | ||
1237 | ::ATSUDisposeTextLayout(atsuLayout); | |
1238 | } | |
1239 | ||
1240 | void wxMacCoreGraphicsContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const | |
1241 | { | |
1242 | widths.Empty(); | |
1243 | widths.Add(0, text.length()); | |
1244 | ||
1245 | if (text.empty()) | |
1246 | return; | |
1247 | ||
1248 | ATSUTextLayout atsuLayout; | |
1249 | UniCharCount chars = text.length(); | |
1250 | UniChar* ubuf = NULL; | |
1251 | ||
1252 | #if SIZEOF_WCHAR_T == 4 | |
1253 | wxMBConvUTF16 converter; | |
1254 | #if wxUSE_UNICODE | |
1255 | size_t unicharlen = converter.WC2MB( NULL , text.wc_str() , 0 ); | |
1256 | ubuf = (UniChar*) malloc( unicharlen + 2 ); | |
1257 | converter.WC2MB( (char*) ubuf , text.wc_str(), unicharlen + 2 ); | |
1258 | #else | |
1259 | const wxWCharBuffer wchar = text.wc_str( wxConvLocal ); | |
1260 | size_t unicharlen = converter.WC2MB( NULL , wchar.data() , 0 ); | |
1261 | ubuf = (UniChar*) malloc( unicharlen + 2 ); | |
1262 | converter.WC2MB( (char*) ubuf , wchar.data() , unicharlen + 2 ); | |
1263 | #endif | |
1264 | chars = unicharlen / 2; | |
1265 | #else | |
1266 | #if wxUSE_UNICODE | |
1267 | ubuf = (UniChar*) text.wc_str(); | |
1268 | #else | |
1269 | wxWCharBuffer wchar = text.wc_str( wxConvLocal ); | |
1270 | chars = wxWcslen( wchar.data() ); | |
1271 | ubuf = (UniChar*) wchar.data(); | |
1272 | #endif | |
1273 | #endif | |
1274 | ||
83b96a06 | 1275 | ::ATSUCreateTextLayoutWithTextPtr( (UniCharArrayPtr) ubuf , 0 , chars , chars , 1 , |
50581042 SC |
1276 | &chars , (ATSUStyle*) &m_macATSUIStyle , &atsuLayout ); |
1277 | ||
1278 | for ( int pos = 0; pos < (int)chars; pos ++ ) | |
1279 | { | |
1280 | unsigned long actualNumberOfBounds = 0; | |
1281 | ATSTrapezoid glyphBounds; | |
1282 | ||
1283 | // We get a single bound, since the text should only require one. If it requires more, there is an issue | |
1284 | OSStatus result; | |
1285 | result = ATSUGetGlyphBounds( atsuLayout, 0, 0, kATSUFromTextBeginning, pos + 1, | |
1286 | kATSUseDeviceOrigins, 1, &glyphBounds, &actualNumberOfBounds ); | |
1287 | if (result != noErr || actualNumberOfBounds != 1 ) | |
1288 | return; | |
1289 | ||
1290 | widths[pos] = FixedToInt( glyphBounds.upperRight.x - glyphBounds.upperLeft.x ); | |
1291 | //unsigned char uch = s[i]; | |
1292 | } | |
1293 | ||
1294 | ::ATSUDisposeTextLayout(atsuLayout); | |
1295 | } | |
1296 | ||
1297 | void wxMacCoreGraphicsContext::SetFont( const wxFont &font ) | |
1298 | { | |
1299 | if ( m_macATSUIStyle ) | |
1300 | { | |
1301 | ::ATSUDisposeStyle((ATSUStyle)m_macATSUIStyle); | |
1302 | m_macATSUIStyle = NULL; | |
1303 | } | |
1304 | ||
1305 | if ( font.Ok() ) | |
1306 | { | |
1056ddcf | 1307 | m_font = font ; |
50581042 SC |
1308 | OSStatus status; |
1309 | ||
1310 | status = ATSUCreateAndCopyStyle( (ATSUStyle) font.MacGetATSUStyle() , (ATSUStyle*) &m_macATSUIStyle ); | |
1311 | ||
1312 | wxASSERT_MSG( status == noErr, wxT("couldn't create ATSU style") ); | |
1313 | ||
1314 | // we need the scale here ... | |
1315 | ||
1316 | Fixed atsuSize = IntToFixed( int( /*m_scaleY*/ 1 * font.MacGetFontSize()) ); | |
1317 | RGBColor atsuColor = MAC_WXCOLORREF( m_textForegroundColor.GetPixel() ); | |
1318 | ATSUAttributeTag atsuTags[] = | |
1319 | { | |
1320 | kATSUSizeTag , | |
1321 | kATSUColorTag , | |
1322 | }; | |
1323 | ByteCount atsuSizes[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
1324 | { | |
1325 | sizeof( Fixed ) , | |
1326 | sizeof( RGBColor ) , | |
1327 | }; | |
1328 | ATSUAttributeValuePtr atsuValues[sizeof(atsuTags) / sizeof(ATSUAttributeTag)] = | |
1329 | { | |
1330 | &atsuSize , | |
1331 | &atsuColor , | |
1332 | }; | |
1333 | ||
1334 | status = ::ATSUSetAttributes( | |
1335 | (ATSUStyle)m_macATSUIStyle, sizeof(atsuTags) / sizeof(ATSUAttributeTag) , | |
1336 | atsuTags, atsuSizes, atsuValues); | |
1337 | ||
1338 | wxASSERT_MSG( status == noErr , wxT("couldn't modify ATSU style") ); | |
1339 | } | |
1340 | } | |
1341 | ||
1056ddcf SC |
1342 | void * wxMacCoreGraphicsContext::GetNativeContext() |
1343 | { | |
1344 | return m_cgContext; | |
1345 | } | |
1346 | ||
50581042 SC |
1347 | wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC &dc ) |
1348 | { | |
1349 | return new wxMacCoreGraphicsContext((CGContextRef)dc.GetWindow()->MacGetCGContextRef() ); | |
1350 | } | |
1351 | ||
1056ddcf SC |
1352 | wxGraphicsContext* wxGraphicsContext::Create( wxWindow * window ) |
1353 | { | |
eec960fa | 1354 | return new wxMacCoreGraphicsContext( window ); |
1056ddcf SC |
1355 | } |
1356 | ||
1357 | wxGraphicsContext* wxGraphicsContext::CreateFromNative( void * context ) | |
1358 | { | |
1359 | return new wxMacCoreGraphicsContext((CGContextRef)context); | |
1360 | } | |
1361 | ||
513b47e9 SC |
1362 | wxGraphicsContext* wxGraphicsContext::CreateFromNativeWindow( void * window ) |
1363 | { | |
1364 | return new wxMacCoreGraphicsContext((WindowRef)window); | |
1365 | } | |
1366 | ||
50581042 | 1367 | #endif // wxMAC_USE_CORE_GRAPHICS |