adapting to new pizza, fixing dashes, pango layout
[wxWidgets.git] / src / generic / graphicc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2006-10-03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dc.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/image.h"
22 #include "wx/window.h"
23 #include "wx/dc.h"
24 #include "wx/utils.h"
25 #include "wx/dialog.h"
26 #include "wx/app.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
29 #include "wx/log.h"
30 #include "wx/icon.h"
31 #include "wx/dcprint.h"
32 #include "wx/module.h"
33 #endif
34
35 #ifdef __WXGTK__
36 #include <gtk/gtk.h>
37 #endif
38
39 #include "wx/graphics.h"
40 #include "wx/rawbmp.h"
41
42 #if wxUSE_GRAPHICS_CONTEXT
43
44 #include <vector>
45
46 using namespace std;
47
48 //-----------------------------------------------------------------------------
49 // constants
50 //-----------------------------------------------------------------------------
51
52 const double RAD2DEG = 180.0 / M_PI;
53
54 //-----------------------------------------------------------------------------
55 // Local functions
56 //-----------------------------------------------------------------------------
57
58 static inline double dmin(double a, double b)
59 {
60 return a < b ? a : b;
61 }
62 static inline double dmax(double a, double b)
63 {
64 return a > b ? a : b;
65 }
66
67 static inline double DegToRad(double deg)
68 {
69 return (deg * M_PI) / 180.0;
70 }
71 static inline double RadToDeg(double deg)
72 {
73 return (deg * 180.0) / M_PI;
74 }
75
76 //-----------------------------------------------------------------------------
77 // device context implementation
78 //
79 // more and more of the dc functionality should be implemented by calling
80 // the appropricate wxCairoContext, but we will have to do that step by step
81 // also coordinate conversions should be moved to native matrix ops
82 //-----------------------------------------------------------------------------
83
84 // we always stock two context states, one at entry, to be able to preserve the
85 // state we were called with, the other one after changing to HI Graphics orientation
86 // (this one is used for getting back clippings etc)
87
88 //-----------------------------------------------------------------------------
89 // wxGraphicsPath implementation
90 //-----------------------------------------------------------------------------
91
92 // TODO remove this dependency (gdiplus needs the macros)
93
94 #ifndef max
95 #define max(a,b) (((a) > (b)) ? (a) : (b))
96 #endif
97
98 #ifndef min
99 #define min(a,b) (((a) < (b)) ? (a) : (b))
100 #endif
101
102 #include <cairo.h>
103 #ifdef __WXGTK__
104 #include "wx/gtk/win_gtk.h"
105 #include <gtk/gtk.h>
106 #include "wx/fontutil.h"
107 #endif
108
109 #ifdef __WXMSW__
110 #include <cairo-win32.h>
111 #endif
112
113 #ifdef __WXMAC__
114 #include "wx/mac/private.h"
115 #include <cairo-quartz.h>
116 #include <cairo-atsui.h>
117 #endif
118
119 class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData
120 {
121 public :
122 wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL);
123 ~wxCairoPathData();
124
125 virtual wxGraphicsObjectRefData *Clone() const;
126
127 //
128 // These are the path primitives from which everything else can be constructed
129 //
130
131 // begins a new subpath at (x,y)
132 virtual void MoveToPoint( wxDouble x, wxDouble y );
133
134 // adds a straight line from the current point to (x,y)
135 virtual void AddLineToPoint( wxDouble x, wxDouble y );
136
137 // adds a cubic Bezier curve from the current point, using two control points and an end point
138 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
139
140
141 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
142 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
143
144 // gets the last point of the current path, (0,0) if not yet set
145 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
146
147 // adds another path
148 virtual void AddPath( const wxGraphicsPathData* path );
149
150 // closes the current sub-path
151 virtual void CloseSubpath();
152
153 //
154 // These are convenience functions which - if not available natively will be assembled
155 // using the primitives from above
156 //
157
158 /*
159
160 // appends a rectangle as a new closed subpath
161 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
162 // appends an ellipsis as a new closed subpath fitting the passed rectangle
163 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
164
165 // 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)
166 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
167 */
168
169 // returns the native path
170 virtual void * GetNativePath() const ;
171
172 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
173 virtual void UnGetNativePath(void *p) const;
174
175 // transforms each point of this path by the matrix
176 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
177
178 // gets the bounding box enclosing all points (possibly including control points)
179 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
180
181 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxWINDING_RULE) const;
182
183 private :
184 cairo_t* m_pathContext;
185 };
186
187 class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData
188 {
189 public :
190 wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
191 virtual ~wxCairoMatrixData() ;
192
193 virtual wxGraphicsObjectRefData *Clone() const ;
194
195 // concatenates the matrix
196 virtual void Concat( const wxGraphicsMatrixData *t );
197
198 // sets the matrix to the respective values
199 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
200 wxDouble tx=0.0, wxDouble ty=0.0);
201
202 // gets the component valuess of the matrix
203 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
204 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
205
206 // makes this the inverse matrix
207 virtual void Invert();
208
209 // returns true if the elements of the transformation matrix are equal ?
210 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
211
212 // return true if this is the identity matrix
213 virtual bool IsIdentity() const;
214
215 //
216 // transformation
217 //
218
219 // add the translation to this matrix
220 virtual void Translate( wxDouble dx , wxDouble dy );
221
222 // add the scale to this matrix
223 virtual void Scale( wxDouble xScale , wxDouble yScale );
224
225 // add the rotation to this matrix (radians)
226 virtual void Rotate( wxDouble angle );
227
228 //
229 // apply the transforms
230 //
231
232 // applies that matrix to the point
233 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
234
235 // applies the matrix except for translations
236 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
237
238 // returns the native representation
239 virtual void * GetNativeMatrix() const;
240 private:
241 cairo_matrix_t m_matrix ;
242 } ;
243
244 class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData
245 {
246 public:
247 wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
248 ~wxCairoPenData();
249
250 void Init();
251
252 virtual void Apply( wxGraphicsContext* context );
253 virtual wxDouble GetWidth() { return m_width; }
254
255 private :
256 double m_width;
257
258 double m_red;
259 double m_green;
260 double m_blue;
261 double m_alpha;
262
263 cairo_line_cap_t m_cap;
264 cairo_line_join_t m_join;
265
266 int m_count;
267 const double *m_lengths;
268 double *m_userLengths;
269
270 wxPen m_pen;
271 };
272
273 class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData
274 {
275 public:
276 wxCairoBrushData( wxGraphicsRenderer* renderer );
277 wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
278 ~wxCairoBrushData ();
279
280 virtual void Apply( wxGraphicsContext* context );
281 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
282 const wxColour&c1, const wxColour&c2 );
283 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
284 const wxColour &oColor, const wxColour &cColor );
285
286 protected:
287 virtual void Init();
288
289 private :
290 double m_red;
291 double m_green;
292 double m_blue;
293 double m_alpha;
294
295 cairo_pattern_t* m_brushPattern;
296 };
297
298 class wxCairoFontData : public wxGraphicsObjectRefData
299 {
300 public:
301 wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
302 ~wxCairoFontData();
303
304 virtual void Apply( wxGraphicsContext* context );
305 #ifdef __WXGTK__
306 const PangoFontDescription* GetFont() const { return m_font; }
307 #endif
308 private :
309 double m_size;
310 double m_red;
311 double m_green;
312 double m_blue;
313 double m_alpha;
314 #ifdef __WXMAC__
315 cairo_font_face_t *m_font;
316 #elif defined(__WXGTK__)
317 PangoFontDescription* m_font;
318 #else
319 wxCharBuffer m_fontName;
320 cairo_font_slant_t m_slant;
321 cairo_font_weight_t m_weight;
322 #endif
323 };
324
325 class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
326 {
327 DECLARE_NO_COPY_CLASS(wxCairoContext)
328
329 public:
330 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
331 #ifdef __WXGTK__
332 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
333 #endif
334 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
335 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
336 wxCairoContext();
337 virtual ~wxCairoContext();
338
339 virtual bool ShouldOffset() const
340 {
341 int penwidth = 0 ;
342 if ( !m_pen.IsNull() )
343 {
344 penwidth = (int)((wxCairoPenData*)m_pen.GetRefData())->GetWidth();
345 if ( penwidth == 0 )
346 penwidth = 1;
347 }
348 return ( penwidth % 2 ) == 1;
349 }
350
351 virtual void Clip( const wxRegion &region );
352
353 // clips drawings to the rect
354 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
355
356 // resets the clipping to original extent
357 virtual void ResetClip();
358
359 virtual void * GetNativeContext();
360
361 virtual void StrokePath( const wxGraphicsPath& p );
362 virtual void FillPath( const wxGraphicsPath& p , int fillStyle = wxWINDING_RULE );
363
364 virtual void Translate( wxDouble dx , wxDouble dy );
365 virtual void Scale( wxDouble xScale , wxDouble yScale );
366 virtual void Rotate( wxDouble angle );
367
368 // concatenates this transform with the current transform of this context
369 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
370
371 // sets the transform of this context
372 virtual void SetTransform( const wxGraphicsMatrix& matrix );
373
374 // gets the matrix of this context
375 virtual wxGraphicsMatrix GetTransform() const;
376
377 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
378 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
379 virtual void PushState();
380 virtual void PopState();
381
382 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
383 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
384 wxDouble *descent, wxDouble *externalLeading ) const;
385 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
386
387 private:
388 void Init(cairo_t *context);
389
390 cairo_t* m_context;
391 };
392
393 //-----------------------------------------------------------------------------
394 // wxCairoPenData implementation
395 //-----------------------------------------------------------------------------
396
397 wxCairoPenData::~wxCairoPenData()
398 {
399 delete[] m_userLengths;
400 }
401
402 void wxCairoPenData::Init()
403 {
404 m_lengths = NULL;
405 m_userLengths = NULL;
406 m_width = 0;
407 m_count = 0;
408 }
409
410 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
411 : wxGraphicsObjectRefData(renderer)
412 {
413 Init();
414 m_pen = pen;
415 m_width = m_pen.GetWidth();
416 if (m_width <= 0.0)
417 m_width = 0.1;
418
419 m_red = m_pen.GetColour().Red()/255.0;
420 m_green = m_pen.GetColour().Green()/255.0;
421 m_blue = m_pen.GetColour().Blue()/255.0;
422 m_alpha = m_pen.GetColour().Alpha()/255.0;
423
424 switch ( m_pen.GetCap() )
425 {
426 case wxCAP_ROUND :
427 m_cap = CAIRO_LINE_CAP_ROUND;
428 break;
429
430 case wxCAP_PROJECTING :
431 m_cap = CAIRO_LINE_CAP_SQUARE;
432 break;
433
434 case wxCAP_BUTT :
435 m_cap = CAIRO_LINE_CAP_BUTT;
436 break;
437
438 default :
439 m_cap = CAIRO_LINE_CAP_BUTT;
440 break;
441 }
442
443 switch ( m_pen.GetJoin() )
444 {
445 case wxJOIN_BEVEL :
446 m_join = CAIRO_LINE_JOIN_BEVEL;
447 break;
448
449 case wxJOIN_MITER :
450 m_join = CAIRO_LINE_JOIN_MITER;
451 break;
452
453 case wxJOIN_ROUND :
454 m_join = CAIRO_LINE_JOIN_ROUND;
455 break;
456
457 default :
458 m_join = CAIRO_LINE_JOIN_MITER;
459 break;
460 }
461
462 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
463 const double dotted[] =
464 {
465 dashUnit , dashUnit + 2.0
466 };
467 static const double short_dashed[] =
468 {
469 9.0 , 6.0
470 };
471 static const double dashed[] =
472 {
473 19.0 , 9.0
474 };
475 static const double dotted_dashed[] =
476 {
477 9.0 , 6.0 , 3.0 , 3.0
478 };
479
480 switch ( m_pen.GetStyle() )
481 {
482 case wxSOLID :
483 break;
484
485 case wxDOT :
486 m_count = WXSIZEOF(dotted);
487 m_userLengths = new double[ m_count ] ;
488 memcpy( m_userLengths, dotted, sizeof(dotted) );
489 m_lengths = m_userLengths;
490 break;
491
492 case wxLONG_DASH :
493 m_lengths = dashed ;
494 m_count = WXSIZEOF(dashed);
495 break;
496
497 case wxSHORT_DASH :
498 m_lengths = short_dashed ;
499 m_count = WXSIZEOF(short_dashed);
500 break;
501
502 case wxDOT_DASH :
503 m_lengths = dotted_dashed ;
504 m_count = WXSIZEOF(dotted_dashed);
505 break;
506
507 case wxUSER_DASH :
508 {
509 wxDash *wxdashes ;
510 m_count = m_pen.GetDashes( &wxdashes ) ;
511 if ((wxdashes != NULL) && (m_count > 0))
512 {
513 m_userLengths = new double[m_count] ;
514 for ( int i = 0 ; i < m_count ; ++i )
515 {
516 m_userLengths[i] = wxdashes[i] * dashUnit ;
517
518 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
519 m_userLengths[i] = dashUnit + 2.0 ;
520 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
521 m_userLengths[i] = dashUnit ;
522 }
523 }
524 m_lengths = m_userLengths ;
525 }
526 break;
527 case wxSTIPPLE :
528 {
529 /*
530 wxBitmap* bmp = pen.GetStipple();
531 if ( bmp && bmp->Ok() )
532 {
533 wxDELETE( m_penImage );
534 wxDELETE( m_penBrush );
535 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
536 m_penBrush = new TextureBrush(m_penImage);
537 m_pen->SetBrush( m_penBrush );
538 }
539 */
540 }
541 break;
542 default :
543 if ( m_pen.GetStyle() >= wxFIRST_HATCH && m_pen.GetStyle() <= wxLAST_HATCH )
544 {
545 /*
546 wxDELETE( m_penBrush );
547 HatchStyle style = HatchStyleHorizontal;
548 switch( pen.GetStyle() )
549 {
550 case wxBDIAGONAL_HATCH :
551 style = HatchStyleBackwardDiagonal;
552 break ;
553 case wxCROSSDIAG_HATCH :
554 style = HatchStyleDiagonalCross;
555 break ;
556 case wxFDIAGONAL_HATCH :
557 style = HatchStyleForwardDiagonal;
558 break ;
559 case wxCROSS_HATCH :
560 style = HatchStyleCross;
561 break ;
562 case wxHORIZONTAL_HATCH :
563 style = HatchStyleHorizontal;
564 break ;
565 case wxVERTICAL_HATCH :
566 style = HatchStyleVertical;
567 break ;
568
569 }
570 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
571 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
572 m_pen->SetBrush( m_penBrush )
573 */
574 }
575 break;
576 }
577 }
578
579 void wxCairoPenData::Apply( wxGraphicsContext* context )
580 {
581 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
582 cairo_set_line_width(ctext,m_width);
583 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
584 cairo_set_line_cap(ctext,m_cap);
585 cairo_set_line_join(ctext,m_join);
586 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
587 }
588
589 //-----------------------------------------------------------------------------
590 // wxCairoBrushData implementation
591 //-----------------------------------------------------------------------------
592
593 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
594 : wxGraphicsObjectRefData( renderer )
595 {
596 Init();
597 }
598
599 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
600 : wxGraphicsObjectRefData(renderer)
601 {
602 Init();
603
604 m_red = brush.GetColour().Red()/255.0;
605 m_green = brush.GetColour().Green()/255.0;
606 m_blue = brush.GetColour().Blue()/255.0;
607 m_alpha = brush.GetColour().Alpha()/255.0;
608 /*
609 if ( brush.GetStyle() == wxSOLID)
610 {
611 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
612 brush.GetColour().Green() , brush.GetColour().Blue() ) );
613 }
614 else if ( brush.IsHatch() )
615 {
616 HatchStyle style = HatchStyleHorizontal;
617 switch( brush.GetStyle() )
618 {
619 case wxBDIAGONAL_HATCH :
620 style = HatchStyleBackwardDiagonal;
621 break ;
622 case wxCROSSDIAG_HATCH :
623 style = HatchStyleDiagonalCross;
624 break ;
625 case wxFDIAGONAL_HATCH :
626 style = HatchStyleForwardDiagonal;
627 break ;
628 case wxCROSS_HATCH :
629 style = HatchStyleCross;
630 break ;
631 case wxHORIZONTAL_HATCH :
632 style = HatchStyleHorizontal;
633 break ;
634 case wxVERTICAL_HATCH :
635 style = HatchStyleVertical;
636 break ;
637
638 }
639 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
640 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
641 }
642 else
643 {
644 wxBitmap* bmp = brush.GetStipple();
645 if ( bmp && bmp->Ok() )
646 {
647 wxDELETE( m_brushImage );
648 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
649 m_brush = new TextureBrush(m_brushImage);
650 }
651 }
652 */
653 }
654
655 wxCairoBrushData::~wxCairoBrushData ()
656 {
657 if (m_brushPattern)
658 cairo_pattern_destroy(m_brushPattern);
659 }
660
661 void wxCairoBrushData::Apply( wxGraphicsContext* context )
662 {
663 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
664 if ( m_brushPattern )
665 {
666 cairo_set_source(ctext,m_brushPattern);
667 }
668 else
669 {
670 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
671 }
672 }
673
674 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
675 const wxColour&c1, const wxColour&c2 )
676 {
677 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
678 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
679 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
680 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
681 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
682 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
683 }
684
685 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
686 const wxColour &oColor, const wxColour &cColor )
687 {
688 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
689 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
690 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
691 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
692 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
693 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
694 }
695
696 void wxCairoBrushData::Init()
697 {
698 m_brushPattern = NULL;
699 }
700
701 //-----------------------------------------------------------------------------
702 // wxCairoFontData implementation
703 //-----------------------------------------------------------------------------
704
705 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
706 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
707 {
708 m_red = col.Red()/255.0;
709 m_green = col.Green()/255.0;
710 m_blue = col.Blue()/255.0;
711 m_alpha = col.Alpha()/255.0;
712 m_size = font.GetPointSize();
713
714 #ifdef __WXMAC__
715 m_font = cairo_atsui_font_face_create_for_atsu_font_id( font.MacGetATSUFontID() );
716 #elif defined(__WXGTK__)
717 m_font = pango_font_description_copy( font.GetNativeFontInfo()->description );
718 #else
719 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
720 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
721 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
722 #endif
723 }
724
725 wxCairoFontData::~wxCairoFontData()
726 {
727 #ifdef __WXMAC__
728 cairo_font_face_destroy( m_font );
729 #elif defined(__WXGTK__)
730 pango_font_description_free( m_font );
731 #else
732 #endif
733 }
734
735 void wxCairoFontData::Apply( wxGraphicsContext* context )
736 {
737 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
738 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
739 #ifdef __WXGTK__
740 // the rest is done using Pango layouts
741 #elif defined(__WXMAC__)
742 cairo_set_font_face(ctext, m_font);
743 #else
744 cairo_select_font_face(ctext, m_fontName, m_slant, m_weights );
745 cairo_set_font_size(ctext, m_size );
746 #endif
747 }
748
749 //-----------------------------------------------------------------------------
750 // wxCairoPathData implementation
751 //-----------------------------------------------------------------------------
752
753 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
754 : wxGraphicsPathData(renderer)
755 {
756 if (pathcontext)
757 {
758 m_pathContext = pathcontext;
759 }
760 else
761 {
762 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
763 m_pathContext = cairo_create(surface);
764 cairo_surface_destroy (surface);
765 }
766 }
767
768 wxCairoPathData::~wxCairoPathData()
769 {
770 cairo_destroy(m_pathContext);
771 }
772
773 wxGraphicsObjectRefData *wxCairoPathData::Clone() const
774 {
775 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
776 cairo_t* pathcontext = cairo_create(surface);
777 cairo_surface_destroy (surface);
778
779 cairo_path_t* path = cairo_copy_path(m_pathContext);
780 cairo_append_path(pathcontext, path);
781 cairo_path_destroy(path);
782 return new wxCairoPathData( GetRenderer() ,pathcontext);
783 }
784
785
786 void* wxCairoPathData::GetNativePath() const
787 {
788 return cairo_copy_path(m_pathContext) ;
789 }
790
791 void wxCairoPathData::UnGetNativePath(void *p) const
792 {
793 cairo_path_destroy((cairo_path_t*)p);
794 }
795
796 //
797 // The Primitives
798 //
799
800 void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
801 {
802 cairo_move_to(m_pathContext,x,y);
803 }
804
805 void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
806 {
807 cairo_line_to(m_pathContext,x,y);
808 }
809
810 void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
811 {
812 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
813 cairo_append_path(m_pathContext, p);
814 UnGetNativePath(p);
815 }
816
817 void wxCairoPathData::CloseSubpath()
818 {
819 cairo_close_path(m_pathContext);
820 }
821
822 void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
823 {
824 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
825 }
826
827 // gets the last point of the current path, (0,0) if not yet set
828 void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
829 {
830 double dx,dy;
831 cairo_get_current_point(m_pathContext,&dx,&dy);
832 if (x)
833 *x = dx;
834 if (y)
835 *y = dy;
836 }
837
838 void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
839 {
840 // as clockwise means positive in our system (y pointing downwards)
841 // TODO make this interpretation dependent of the
842 // real device trans
843 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
844 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
845 else
846 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
847 }
848
849 // transforms each point of this path by the matrix
850 void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
851 {
852 // as we don't have a true path object, we have to apply the inverse
853 // matrix to the context
854 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
855 cairo_matrix_invert( &m );
856 cairo_transform(m_pathContext,&m);
857 }
858
859 // gets the bounding box enclosing all points (possibly including control points)
860 void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
861 {
862 double x1,y1,x2,y2;
863
864 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
865 if ( x2 < x1 )
866 {
867 *x = x2;
868 *w = x1-x2;
869 }
870 else
871 {
872 *x = x1;
873 *w = x2-x1;
874 }
875
876 if( y2 < y1 )
877 {
878 *y = y2;
879 *h = y1-y2;
880 }
881 else
882 {
883 *y = y1;
884 *h = y2-y1;
885 }
886 }
887
888 bool wxCairoPathData::Contains( wxDouble x, wxDouble y, int WXUNUSED(fillStyle) ) const
889 {
890 return cairo_in_stroke( m_pathContext, x, y) != 0;
891 }
892
893 //-----------------------------------------------------------------------------
894 // wxCairoMatrixData implementation
895 //-----------------------------------------------------------------------------
896
897 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
898 : wxGraphicsMatrixData(renderer)
899 {
900 if ( matrix )
901 m_matrix = *matrix;
902 }
903
904 wxCairoMatrixData::~wxCairoMatrixData()
905 {
906 // nothing to do
907 }
908
909 wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
910 {
911 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
912 }
913
914 // concatenates the matrix
915 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
916 {
917 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
918 }
919
920 // sets the matrix to the respective values
921 void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
922 wxDouble tx, wxDouble ty)
923 {
924 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
925 }
926
927 // gets the component valuess of the matrix
928 void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
929 wxDouble* d, wxDouble* tx, wxDouble* ty) const
930 {
931 if (a) *a = m_matrix.xx;
932 if (b) *b = m_matrix.yx;
933 if (c) *c = m_matrix.xy;
934 if (d) *d = m_matrix.yy;
935 if (tx) *tx= m_matrix.x0;
936 if (ty) *ty= m_matrix.y0;
937 }
938
939 // makes this the inverse matrix
940 void wxCairoMatrixData::Invert()
941 {
942 cairo_matrix_invert( &m_matrix );
943 }
944
945 // returns true if the elements of the transformation matrix are equal ?
946 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
947 {
948 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
949 return (
950 m_matrix.xx == tm->xx &&
951 m_matrix.yx == tm->yx &&
952 m_matrix.xy == tm->xy &&
953 m_matrix.yy == tm->yy &&
954 m_matrix.x0 == tm->x0 &&
955 m_matrix.y0 == tm->y0 ) ;
956 }
957
958 // return true if this is the identity matrix
959 bool wxCairoMatrixData::IsIdentity() const
960 {
961 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
962 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
963 }
964
965 //
966 // transformation
967 //
968
969 // add the translation to this matrix
970 void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
971 {
972 cairo_matrix_translate( &m_matrix, dx, dy) ;
973 }
974
975 // add the scale to this matrix
976 void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
977 {
978 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
979 }
980
981 // add the rotation to this matrix (radians)
982 void wxCairoMatrixData::Rotate( wxDouble angle )
983 {
984 cairo_matrix_rotate( &m_matrix, angle) ;
985 }
986
987 //
988 // apply the transforms
989 //
990
991 // applies that matrix to the point
992 void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
993 {
994 double lx = *x, ly = *y ;
995 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
996 *x = lx;
997 *y = ly;
998 }
999
1000 // applies the matrix except for translations
1001 void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1002 {
1003 double lx = *dx, ly = *dy ;
1004 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
1005 *dx = lx;
1006 *dy = ly;
1007 }
1008
1009 // returns the native representation
1010 void * wxCairoMatrixData::GetNativeMatrix() const
1011 {
1012 return (void*) &m_matrix;
1013 }
1014
1015 //-----------------------------------------------------------------------------
1016 // wxCairoContext implementation
1017 //-----------------------------------------------------------------------------
1018
1019 class wxCairoOffsetHelper
1020 {
1021 public :
1022 wxCairoOffsetHelper( cairo_t* ctx , bool offset )
1023 {
1024 m_ctx = ctx;
1025 m_offset = offset;
1026 if ( m_offset )
1027 cairo_translate( m_ctx, 0.5, 0.5 );
1028 }
1029 ~wxCairoOffsetHelper( )
1030 {
1031 if ( m_offset )
1032 cairo_translate( m_ctx, -0.5, -0.5 );
1033 }
1034 public :
1035 cairo_t* m_ctx;
1036 bool m_offset;
1037 } ;
1038
1039 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1040 : wxGraphicsContext(renderer)
1041 {
1042 #ifdef __WXGTK__
1043 Init( gdk_cairo_create( dc.m_window ) );
1044 #endif
1045 #ifdef __WXMAC__
1046 int width, height;
1047 dc.GetSize( &width, &height );
1048 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1049 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1050 Init( cairo_create( surface ) );
1051 cairo_surface_destroy( surface );
1052 #endif
1053 }
1054
1055 #ifdef __WXGTK__
1056 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1057 : wxGraphicsContext(renderer)
1058 {
1059 Init( gdk_cairo_create( drawable ) );
1060 }
1061 #endif
1062
1063 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1064 : wxGraphicsContext(renderer)
1065 {
1066 Init( context );
1067 }
1068
1069 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1070 : wxGraphicsContext(renderer)
1071 {
1072 #ifdef __WXGTK__
1073 // something along these lines (copied from dcclient)
1074
1075 GtkWidget *widget = window->m_wxwindow;
1076
1077 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1078 // code should still be able to create wxClientDCs for them, so we will
1079 // use the parent window here then.
1080 if ( !widget )
1081 {
1082 window = window->GetParent();
1083 widget = window->m_wxwindow;
1084 }
1085
1086 wxASSERT_MSG( widget, wxT("wxCairoContext needs a widget") );
1087
1088 wxPizza *pizza = WX_PIZZA( widget );
1089 GdkDrawable* drawable = pizza->m_backing_window;
1090 Init( gdk_cairo_create( drawable ) ) ;
1091 #endif
1092 }
1093
1094 wxCairoContext::~wxCairoContext()
1095 {
1096 if ( m_context )
1097 {
1098 PopState();
1099 PopState();
1100 cairo_destroy(m_context);
1101 }
1102 }
1103
1104 void wxCairoContext::Init(cairo_t *context)
1105 {
1106 m_context = context ;
1107 PushState();
1108 PushState();
1109 }
1110
1111
1112 void wxCairoContext::Clip( const wxRegion& region )
1113 {
1114 // Create a path with all the rectangles in the region
1115 wxGraphicsPath path = GetRenderer()->CreatePath();
1116 wxRegionIterator ri(region);
1117 while (ri)
1118 {
1119 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
1120 ri++;
1121 }
1122
1123 // Put it in the context
1124 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1125 cairo_append_path(m_context, cp);
1126
1127 // clip to that path
1128 cairo_clip(m_context);
1129 path.UnGetNativePath(cp);
1130 }
1131
1132 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1133 {
1134 // Create a path with this rectangle
1135 wxGraphicsPath path = GetRenderer()->CreatePath();
1136 path.AddRectangle(x,y,w,h);
1137
1138 // Put it in the context
1139 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1140 cairo_append_path(m_context, cp);
1141
1142 // clip to that path
1143 // cairo_clip(m_context);
1144 path.UnGetNativePath(cp);
1145 }
1146
1147 void wxCairoContext::ResetClip()
1148 {
1149 cairo_reset_clip(m_context);
1150 }
1151
1152
1153 void wxCairoContext::StrokePath( const wxGraphicsPath& path )
1154 {
1155 if ( !m_pen.IsNull() )
1156 {
1157 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1158 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1159 cairo_append_path(m_context,cp);
1160 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1161 cairo_stroke(m_context);
1162 path.UnGetNativePath(cp);
1163 }
1164 }
1165
1166 void wxCairoContext::FillPath( const wxGraphicsPath& path , int fillStyle )
1167 {
1168 if ( !m_brush.IsNull() )
1169 {
1170 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1171 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1172 cairo_append_path(m_context,cp);
1173 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1174 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1175 cairo_fill(m_context);
1176 path.UnGetNativePath(cp);
1177 }
1178 }
1179
1180 void wxCairoContext::Rotate( wxDouble angle )
1181 {
1182 cairo_rotate(m_context,angle);
1183 }
1184
1185 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1186 {
1187 cairo_translate(m_context,dx,dy);
1188 }
1189
1190 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1191 {
1192 cairo_scale(m_context,xScale,yScale);
1193 }
1194
1195 // concatenates this transform with the current transform of this context
1196 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1197 {
1198 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
1199 }
1200
1201 // sets the transform of this context
1202 void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
1203 {
1204 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
1205 }
1206
1207 // gets the matrix of this context
1208 wxGraphicsMatrix wxCairoContext::GetTransform() const
1209 {
1210 wxGraphicsMatrix matrix = CreateMatrix();
1211 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1212 return matrix;
1213 }
1214
1215
1216
1217 void wxCairoContext::PushState()
1218 {
1219 cairo_save(m_context);
1220 }
1221
1222 void wxCairoContext::PopState()
1223 {
1224 cairo_restore(m_context);
1225 }
1226
1227 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1228 {
1229 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1230
1231 cairo_surface_t* surface;
1232 int bw = bmp.GetWidth();
1233 int bh = bmp.GetHeight();
1234 wxBitmap bmpSource = bmp; // we need a non-const instance
1235 unsigned char* buffer = new unsigned char[bw*bh*4];
1236 wxUint32* data = (wxUint32*)buffer;
1237
1238 // Create a surface object and copy the bitmap pixel data to it. if the
1239 // image has alpha (or a mask represented as alpha) then we'll use a
1240 // different format and iterator than if it doesn't...
1241 if (bmpSource.HasAlpha() || bmpSource.GetMask())
1242 {
1243 surface = cairo_image_surface_create_for_data(
1244 buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1245 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1246 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1247
1248 wxAlphaPixelData::Iterator p(pixData);
1249 for (int y=0; y<bh; y++)
1250 {
1251 wxAlphaPixelData::Iterator rowStart = p;
1252 for (int x=0; x<bw; x++)
1253 {
1254 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1255 // with alpha in the upper 8 bits, then red, then green, then
1256 // blue. The 32-bit quantities are stored native-endian.
1257 // Pre-multiplied alpha is used.
1258 unsigned char alpha = p.Alpha();
1259 if (alpha == 0)
1260 *data = 0;
1261 else
1262 *data = ( alpha << 24
1263 | (p.Red() * alpha/255) << 16
1264 | (p.Green() * alpha/255) << 8
1265 | (p.Blue() * alpha/255) );
1266 ++data;
1267 ++p;
1268 }
1269 p = rowStart;
1270 p.OffsetY(pixData, 1);
1271 }
1272 }
1273 else // no alpha
1274 {
1275 surface = cairo_image_surface_create_for_data(
1276 buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1277 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1278 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1279
1280 wxNativePixelData::Iterator p(pixData);
1281 for (int y=0; y<bh; y++)
1282 {
1283 wxNativePixelData::Iterator rowStart = p;
1284 for (int x=0; x<bw; x++)
1285 {
1286 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1287 // the upper 8 bits unused. Red, Green, and Blue are stored in
1288 // the remaining 24 bits in that order. The 32-bit quantities
1289 // are stored native-endian.
1290 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1291 ++data;
1292 ++p;
1293 }
1294 p = rowStart;
1295 p.OffsetY(pixData, 1);
1296 }
1297 }
1298
1299
1300 PushState();
1301
1302 // In case we're scaling the image by using a width and height different
1303 // than the bitmap's size create a pattern transformation on the surface and
1304 // draw the transformed pattern.
1305 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
1306 wxDouble scaleX = w / bw;
1307 wxDouble scaleY = h / bh;
1308 cairo_scale(m_context, scaleX, scaleY);
1309
1310 // prepare to draw the image
1311 cairo_translate(m_context, x, y);
1312 cairo_set_source(m_context, pattern);
1313 // use the original size here since the context is scaled already...
1314 cairo_rectangle(m_context, 0, 0, bw, bh);
1315 // fill the rectangle using the pattern
1316 cairo_fill(m_context);
1317
1318 // clean up
1319 cairo_pattern_destroy(pattern);
1320 cairo_surface_destroy(surface);
1321 delete [] buffer;
1322 PopState();
1323 }
1324
1325 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1326 {
1327 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1328 // start using the Cairo backend on other platforms then we may need to
1329 // fiddle with this...
1330 DrawBitmap(icon, x, y, w, h);
1331 }
1332
1333
1334 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1335 {
1336 if ( m_font.IsNull() || str.empty())
1337 return;
1338
1339 #ifdef __WXGTK__
1340 const wxCharBuffer data = wxConvUTF8.cWC2MB( str );
1341 if ( !data )
1342 return;
1343 size_t datalen = strlen(data);
1344 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1345
1346 PangoLayout *layout = pango_cairo_create_layout (m_context);
1347 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1348 pango_layout_set_text(layout, data, datalen);
1349 cairo_move_to(m_context, x, y);
1350 pango_cairo_show_layout (m_context, layout);
1351
1352 g_object_unref (layout);
1353 #else
1354 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1355 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1356 // the position we move to by the ascent.
1357 cairo_font_extents_t fe;
1358 cairo_font_extents(m_context, &fe);
1359 cairo_move_to(m_context, x, y+fe.ascent);
1360
1361 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1362 cairo_show_text(m_context,buf);
1363 #endif
1364 }
1365
1366 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1367 wxDouble *descent, wxDouble *externalLeading ) const
1368 {
1369 if ( width )
1370 *width = 0;
1371 if ( height )
1372 *height = 0;
1373 if ( descent )
1374 *descent = 0;
1375 if ( externalLeading )
1376 *externalLeading = 0;
1377
1378 if ( m_font.IsNull() || str.empty())
1379 return;
1380
1381 #ifdef __WXGTK__
1382 int w, h;
1383
1384 PangoLayout *layout = pango_cairo_create_layout (m_context);
1385 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1386 const wxCharBuffer data = wxConvUTF8.cWC2MB( str );
1387 if ( !data )
1388 {
1389 return;
1390 }
1391 pango_layout_set_text( layout, data, strlen(data) );
1392 pango_layout_get_pixel_size (layout, &w, &h);
1393 if ( width )
1394 *width = w;
1395 if ( height )
1396 *height = h;
1397 if (descent)
1398 {
1399 PangoLayoutIter *iter = pango_layout_get_iter(layout);
1400 int baseline = pango_layout_iter_get_baseline(iter);
1401 pango_layout_iter_free(iter);
1402 *descent = h - PANGO_PIXELS(baseline);
1403 }
1404 g_object_unref (layout);
1405 #else
1406 ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
1407
1408 if (width)
1409 {
1410 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1411 cairo_text_extents_t te;
1412 cairo_text_extents(m_context, buf, &te);
1413 *width = te.width;
1414 }
1415
1416 if (height || descent || externalLeading)
1417 {
1418 cairo_font_extents_t fe;
1419 cairo_font_extents(m_context, &fe);
1420
1421 if (height)
1422 *height = fe.height;
1423 if ( descent )
1424 *descent = fe.descent;
1425 if ( externalLeading )
1426 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
1427 }
1428 #endif
1429 }
1430
1431 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1432 {
1433 widths.Empty();
1434 widths.Add(0, text.length());
1435
1436 if (text.empty())
1437 return;
1438
1439 // TODO
1440 }
1441
1442 void * wxCairoContext::GetNativeContext()
1443 {
1444 return m_context;
1445 }
1446
1447 //-----------------------------------------------------------------------------
1448 // wxCairoRenderer declaration
1449 //-----------------------------------------------------------------------------
1450
1451 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1452 {
1453 public :
1454 wxCairoRenderer() {}
1455
1456 virtual ~wxCairoRenderer() {}
1457
1458 // Context
1459
1460 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1461
1462 #ifdef __WXMSW__
1463 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1464 #endif
1465
1466 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1467
1468 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1469
1470 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1471
1472 virtual wxGraphicsContext * CreateMeasuringContext();
1473
1474 // Path
1475
1476 virtual wxGraphicsPath CreatePath();
1477
1478 // Matrix
1479
1480 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1481 wxDouble tx=0.0, wxDouble ty=0.0);
1482
1483
1484 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1485
1486 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1487
1488 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1489 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1490 const wxColour&c1, const wxColour&c2) ;
1491
1492 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1493 // with radius r and color cColor
1494 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1495 const wxColour &oColor, const wxColour &cColor) ;
1496
1497 // sets the font
1498 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1499
1500 private :
1501 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1502 } ;
1503
1504 //-----------------------------------------------------------------------------
1505 // wxCairoRenderer implementation
1506 //-----------------------------------------------------------------------------
1507
1508 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1509
1510 static wxCairoRenderer gs_cairoGraphicsRenderer;
1511
1512 #ifdef __WXGTK__
1513 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1514 {
1515 return &gs_cairoGraphicsRenderer;
1516 }
1517 #endif
1518
1519 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1520 {
1521 return new wxCairoContext(this,dc);
1522 }
1523
1524 #ifdef __WXMSW__
1525 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
1526 {
1527 return NULL;
1528 }
1529 #endif
1530
1531 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1532 {
1533 return new wxCairoContext(this,(cairo_t*)context);
1534 }
1535
1536
1537 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1538 {
1539 #ifdef __WXGTK__
1540 return new wxCairoContext(this,(GdkDrawable*)window);
1541 #else
1542 return NULL;
1543 #endif
1544 }
1545
1546 wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
1547 {
1548 return NULL;
1549 // TODO
1550 }
1551
1552 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1553 {
1554 return new wxCairoContext(this, window );
1555 }
1556
1557 // Path
1558
1559 wxGraphicsPath wxCairoRenderer::CreatePath()
1560 {
1561 wxGraphicsPath path;
1562 path.SetRefData( new wxCairoPathData(this) );
1563 return path;
1564 }
1565
1566
1567 // Matrix
1568
1569 wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1570 wxDouble tx, wxDouble ty)
1571
1572 {
1573 wxGraphicsMatrix m;
1574 wxCairoMatrixData* data = new wxCairoMatrixData( this );
1575 data->Set( a,b,c,d,tx,ty ) ;
1576 m.SetRefData(data);
1577 return m;
1578 }
1579
1580 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
1581 {
1582 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1583 return wxNullGraphicsPen;
1584 else
1585 {
1586 wxGraphicsPen p;
1587 p.SetRefData(new wxCairoPenData( this, pen ));
1588 return p;
1589 }
1590 }
1591
1592 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
1593 {
1594 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1595 return wxNullGraphicsBrush;
1596 else
1597 {
1598 wxGraphicsBrush p;
1599 p.SetRefData(new wxCairoBrushData( this, brush ));
1600 return p;
1601 }
1602 }
1603
1604 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1605 wxGraphicsBrush wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1606 const wxColour&c1, const wxColour&c2)
1607 {
1608 wxGraphicsBrush p;
1609 wxCairoBrushData* d = new wxCairoBrushData( this );
1610 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1611 p.SetRefData(d);
1612 return p;
1613 }
1614
1615 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1616 // with radius r and color cColor
1617 wxGraphicsBrush wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1618 const wxColour &oColor, const wxColour &cColor)
1619 {
1620 wxGraphicsBrush p;
1621 wxCairoBrushData* d = new wxCairoBrushData( this );
1622 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1623 p.SetRefData(d);
1624 return p;
1625 }
1626
1627 // sets the font
1628 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1629 {
1630 if ( font.Ok() )
1631 {
1632 wxGraphicsFont p;
1633 p.SetRefData(new wxCairoFontData( this , font, col ));
1634 return p;
1635 }
1636 else
1637 return wxNullGraphicsFont;
1638 }
1639
1640 #endif // wxUSE_GRAPHICS_CONTEXT