1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/graphicc.cpp
3 // Purpose: cairo device context class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #if wxUSE_GRAPHICS_CONTEXT
24 #include "wx/window.h"
27 #include "wx/dialog.h"
29 #include "wx/bitmap.h"
30 #include "wx/dcmemory.h"
33 #include "wx/dcprint.h"
34 #include "wx/module.h"
37 #include "wx/private/graphics.h"
38 #include "wx/rawbmp.h"
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 const double RAD2DEG
= 180.0 / M_PI
;
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 static inline double dmin(double a
, double b
)
58 static inline double dmax(double a
, double b
)
63 static inline double DegToRad(double deg
)
65 return (deg
* M_PI
) / 180.0;
67 static inline double RadToDeg(double deg
)
69 return (deg
* 180.0) / M_PI
;
72 //-----------------------------------------------------------------------------
73 // device context implementation
75 // more and more of the dc functionality should be implemented by calling
76 // the appropricate wxCairoContext, but we will have to do that step by step
77 // also coordinate conversions should be moved to native matrix ops
78 //-----------------------------------------------------------------------------
80 // we always stock two context states, one at entry, to be able to preserve the
81 // state we were called with, the other one after changing to HI Graphics orientation
82 // (this one is used for getting back clippings etc)
84 //-----------------------------------------------------------------------------
85 // wxGraphicsPath implementation
86 //-----------------------------------------------------------------------------
88 // TODO remove this dependency (gdiplus needs the macros)
91 #define max(a,b) (((a) > (b)) ? (a) : (b))
95 #define min(a,b) (((a) < (b)) ? (a) : (b))
101 #include "wx/fontutil.h"
102 #include "wx/gtk/dc.h"
106 #include <cairo-win32.h>
110 #include "wx/mac/private.h"
111 #include <cairo-quartz.h>
112 #include <cairo-atsui.h>
115 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
118 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
121 virtual wxGraphicsObjectRefData
*Clone() const;
124 // These are the path primitives from which everything else can be constructed
127 // begins a new subpath at (x,y)
128 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
130 // adds a straight line from the current point to (x,y)
131 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
133 // adds a cubic Bezier curve from the current point, using two control points and an end point
134 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
137 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
138 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
140 // gets the last point of the current path, (0,0) if not yet set
141 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
144 virtual void AddPath( const wxGraphicsPathData
* path
);
146 // closes the current sub-path
147 virtual void CloseSubpath();
150 // These are convenience functions which - if not available natively will be assembled
151 // using the primitives from above
156 // appends a rectangle as a new closed subpath
157 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
158 // appends an ellipsis as a new closed subpath fitting the passed rectangle
159 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
161 // 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)
162 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
165 // returns the native path
166 virtual void * GetNativePath() const ;
168 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
169 virtual void UnGetNativePath(void *p
) const;
171 // transforms each point of this path by the matrix
172 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
174 // gets the bounding box enclosing all points (possibly including control points)
175 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
177 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxWINDING_RULE
) const;
180 cairo_t
* m_pathContext
;
183 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
186 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
187 virtual ~wxCairoMatrixData() ;
189 virtual wxGraphicsObjectRefData
*Clone() const ;
191 // concatenates the matrix
192 virtual void Concat( const wxGraphicsMatrixData
*t
);
194 // sets the matrix to the respective values
195 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
196 wxDouble tx
=0.0, wxDouble ty
=0.0);
198 // gets the component valuess of the matrix
199 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
200 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
202 // makes this the inverse matrix
203 virtual void Invert();
205 // returns true if the elements of the transformation matrix are equal ?
206 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
208 // return true if this is the identity matrix
209 virtual bool IsIdentity() const;
215 // add the translation to this matrix
216 virtual void Translate( wxDouble dx
, wxDouble dy
);
218 // add the scale to this matrix
219 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
221 // add the rotation to this matrix (radians)
222 virtual void Rotate( wxDouble angle
);
225 // apply the transforms
228 // applies that matrix to the point
229 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
231 // applies the matrix except for translations
232 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
234 // returns the native representation
235 virtual void * GetNativeMatrix() const;
237 cairo_matrix_t m_matrix
;
240 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
243 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
248 virtual void Apply( wxGraphicsContext
* context
);
249 virtual wxDouble
GetWidth() { return m_width
; }
259 cairo_line_cap_t m_cap
;
260 cairo_line_join_t m_join
;
263 const double *m_lengths
;
264 double *m_userLengths
;
269 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
272 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
273 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
274 ~wxCairoBrushData ();
276 virtual void Apply( wxGraphicsContext
* context
);
277 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
278 const wxColour
&c1
, const wxColour
&c2
);
279 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
280 const wxColour
&oColor
, const wxColour
&cColor
);
291 cairo_pattern_t
* m_brushPattern
;
294 class wxCairoFontData
: public wxGraphicsObjectRefData
297 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
300 virtual void Apply( wxGraphicsContext
* context
);
302 const PangoFontDescription
* GetFont() const { return m_font
; }
311 cairo_font_face_t
*m_font
;
312 #elif defined(__WXGTK__)
313 PangoFontDescription
* m_font
;
315 wxCharBuffer m_fontName
;
316 cairo_font_slant_t m_slant
;
317 cairo_font_weight_t m_weight
;
321 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
323 DECLARE_NO_COPY_CLASS(wxCairoContext
)
326 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
327 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
328 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
330 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
332 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
333 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
335 virtual ~wxCairoContext();
337 virtual bool ShouldOffset() const
340 if ( !m_pen
.IsNull() )
342 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
346 return ( penwidth
% 2 ) == 1;
349 virtual void Clip( const wxRegion
®ion
);
351 // clips drawings to the rect
352 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
354 // resets the clipping to original extent
355 virtual void ResetClip();
357 virtual void * GetNativeContext();
359 virtual void StrokePath( const wxGraphicsPath
& p
);
360 virtual void FillPath( const wxGraphicsPath
& p
, int fillStyle
= wxWINDING_RULE
);
362 virtual void Translate( wxDouble dx
, wxDouble dy
);
363 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
364 virtual void Rotate( wxDouble angle
);
366 // concatenates this transform with the current transform of this context
367 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
369 // sets the transform of this context
370 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
372 // gets the matrix of this context
373 virtual wxGraphicsMatrix
GetTransform() const;
375 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
376 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
377 virtual void PushState();
378 virtual void PopState();
380 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
381 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
382 wxDouble
*descent
, wxDouble
*externalLeading
) const;
383 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
386 void Init(cairo_t
*context
);
391 //-----------------------------------------------------------------------------
392 // wxCairoPenData implementation
393 //-----------------------------------------------------------------------------
395 wxCairoPenData::~wxCairoPenData()
397 delete[] m_userLengths
;
400 void wxCairoPenData::Init()
403 m_userLengths
= NULL
;
408 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
409 : wxGraphicsObjectRefData(renderer
)
413 m_width
= m_pen
.GetWidth();
417 m_red
= m_pen
.GetColour().Red()/255.0;
418 m_green
= m_pen
.GetColour().Green()/255.0;
419 m_blue
= m_pen
.GetColour().Blue()/255.0;
420 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
422 switch ( m_pen
.GetCap() )
425 m_cap
= CAIRO_LINE_CAP_ROUND
;
428 case wxCAP_PROJECTING
:
429 m_cap
= CAIRO_LINE_CAP_SQUARE
;
433 m_cap
= CAIRO_LINE_CAP_BUTT
;
437 m_cap
= CAIRO_LINE_CAP_BUTT
;
441 switch ( m_pen
.GetJoin() )
444 m_join
= CAIRO_LINE_JOIN_BEVEL
;
448 m_join
= CAIRO_LINE_JOIN_MITER
;
452 m_join
= CAIRO_LINE_JOIN_ROUND
;
456 m_join
= CAIRO_LINE_JOIN_MITER
;
460 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
461 const double dotted
[] =
463 dashUnit
, dashUnit
+ 2.0
465 static const double short_dashed
[] =
469 static const double dashed
[] =
473 static const double dotted_dashed
[] =
475 9.0 , 6.0 , 3.0 , 3.0
478 switch ( m_pen
.GetStyle() )
484 m_count
= WXSIZEOF(dotted
);
485 m_userLengths
= new double[ m_count
] ;
486 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
487 m_lengths
= m_userLengths
;
492 m_count
= WXSIZEOF(dashed
);
496 m_lengths
= short_dashed
;
497 m_count
= WXSIZEOF(short_dashed
);
501 m_lengths
= dotted_dashed
;
502 m_count
= WXSIZEOF(dotted_dashed
);
508 m_count
= m_pen
.GetDashes( &wxdashes
) ;
509 if ((wxdashes
!= NULL
) && (m_count
> 0))
511 m_userLengths
= new double[m_count
] ;
512 for ( int i
= 0 ; i
< m_count
; ++i
)
514 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
516 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
517 m_userLengths
[i
] = dashUnit
+ 2.0 ;
518 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
519 m_userLengths
[i
] = dashUnit
;
522 m_lengths
= m_userLengths
;
528 wxBitmap* bmp = pen.GetStipple();
529 if ( bmp && bmp->Ok() )
531 wxDELETE( m_penImage );
532 wxDELETE( m_penBrush );
533 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
534 m_penBrush = new TextureBrush(m_penImage);
535 m_pen->SetBrush( m_penBrush );
541 if ( m_pen
.GetStyle() >= wxFIRST_HATCH
&& m_pen
.GetStyle() <= wxLAST_HATCH
)
544 wxDELETE( m_penBrush );
545 HatchStyle style = HatchStyleHorizontal;
546 switch( pen.GetStyle() )
548 case wxBDIAGONAL_HATCH :
549 style = HatchStyleBackwardDiagonal;
551 case wxCROSSDIAG_HATCH :
552 style = HatchStyleDiagonalCross;
554 case wxFDIAGONAL_HATCH :
555 style = HatchStyleForwardDiagonal;
558 style = HatchStyleCross;
560 case wxHORIZONTAL_HATCH :
561 style = HatchStyleHorizontal;
563 case wxVERTICAL_HATCH :
564 style = HatchStyleVertical;
568 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
569 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
570 m_pen->SetBrush( m_penBrush )
577 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
579 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
580 cairo_set_line_width(ctext
,m_width
);
581 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
582 cairo_set_line_cap(ctext
,m_cap
);
583 cairo_set_line_join(ctext
,m_join
);
584 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
587 //-----------------------------------------------------------------------------
588 // wxCairoBrushData implementation
589 //-----------------------------------------------------------------------------
591 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
592 : wxGraphicsObjectRefData( renderer
)
597 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
598 : wxGraphicsObjectRefData(renderer
)
602 m_red
= brush
.GetColour().Red()/255.0;
603 m_green
= brush
.GetColour().Green()/255.0;
604 m_blue
= brush
.GetColour().Blue()/255.0;
605 m_alpha
= brush
.GetColour().Alpha()/255.0;
607 if ( brush.GetStyle() == wxSOLID)
609 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
610 brush.GetColour().Green() , brush.GetColour().Blue() ) );
612 else if ( brush.IsHatch() )
614 HatchStyle style = HatchStyleHorizontal;
615 switch( brush.GetStyle() )
617 case wxBDIAGONAL_HATCH :
618 style = HatchStyleBackwardDiagonal;
620 case wxCROSSDIAG_HATCH :
621 style = HatchStyleDiagonalCross;
623 case wxFDIAGONAL_HATCH :
624 style = HatchStyleForwardDiagonal;
627 style = HatchStyleCross;
629 case wxHORIZONTAL_HATCH :
630 style = HatchStyleHorizontal;
632 case wxVERTICAL_HATCH :
633 style = HatchStyleVertical;
637 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
638 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
642 wxBitmap* bmp = brush.GetStipple();
643 if ( bmp && bmp->Ok() )
645 wxDELETE( m_brushImage );
646 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
647 m_brush = new TextureBrush(m_brushImage);
653 wxCairoBrushData::~wxCairoBrushData ()
656 cairo_pattern_destroy(m_brushPattern
);
659 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
661 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
662 if ( m_brushPattern
)
664 cairo_set_source(ctext
,m_brushPattern
);
668 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
672 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
673 const wxColour
&c1
, const wxColour
&c2
)
675 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
676 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
677 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
678 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
679 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
680 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
683 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
684 const wxColour
&oColor
, const wxColour
&cColor
)
686 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
687 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
688 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
689 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
690 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
691 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
694 void wxCairoBrushData::Init()
696 m_brushPattern
= NULL
;
699 //-----------------------------------------------------------------------------
700 // wxCairoFontData implementation
701 //-----------------------------------------------------------------------------
703 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
704 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
706 m_red
= col
.Red()/255.0;
707 m_green
= col
.Green()/255.0;
708 m_blue
= col
.Blue()/255.0;
709 m_alpha
= col
.Alpha()/255.0;
710 m_size
= font
.GetPointSize();
713 m_font
= cairo_atsui_font_face_create_for_atsu_font_id( font
.MacGetATSUFontID() );
714 #elif defined(__WXGTK__)
715 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
717 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
718 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
719 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
723 wxCairoFontData::~wxCairoFontData()
726 cairo_font_face_destroy( m_font
);
727 #elif defined(__WXGTK__)
728 pango_font_description_free( m_font
);
733 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
735 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
736 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
738 // the rest is done using Pango layouts
739 #elif defined(__WXMAC__)
740 cairo_set_font_face(ctext
, m_font
);
741 cairo_set_font_size(ctext
, m_size
);
743 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
744 cairo_set_font_size(ctext
, m_size
);
748 //-----------------------------------------------------------------------------
749 // wxCairoPathData implementation
750 //-----------------------------------------------------------------------------
752 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
753 : wxGraphicsPathData(renderer
)
757 m_pathContext
= pathcontext
;
761 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
762 m_pathContext
= cairo_create(surface
);
763 cairo_surface_destroy (surface
);
767 wxCairoPathData::~wxCairoPathData()
769 cairo_destroy(m_pathContext
);
772 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
774 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
775 cairo_t
* pathcontext
= cairo_create(surface
);
776 cairo_surface_destroy (surface
);
778 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
779 cairo_append_path(pathcontext
, path
);
780 cairo_path_destroy(path
);
781 return new wxCairoPathData( GetRenderer() ,pathcontext
);
785 void* wxCairoPathData::GetNativePath() const
787 return cairo_copy_path(m_pathContext
) ;
790 void wxCairoPathData::UnGetNativePath(void *p
) const
792 cairo_path_destroy((cairo_path_t
*)p
);
799 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
801 cairo_move_to(m_pathContext
,x
,y
);
804 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
806 cairo_line_to(m_pathContext
,x
,y
);
809 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
811 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
812 cairo_append_path(m_pathContext
, p
);
816 void wxCairoPathData::CloseSubpath()
818 cairo_close_path(m_pathContext
);
821 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
823 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
826 // gets the last point of the current path, (0,0) if not yet set
827 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
830 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
837 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
839 // as clockwise means positive in our system (y pointing downwards)
840 // TODO make this interpretation dependent of the
842 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
843 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
845 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
848 // transforms each point of this path by the matrix
849 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
851 // as we don't have a true path object, we have to apply the inverse
852 // matrix to the context
853 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
854 cairo_matrix_invert( &m
);
855 cairo_transform(m_pathContext
,&m
);
858 // gets the bounding box enclosing all points (possibly including control points)
859 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
863 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
887 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, int WXUNUSED(fillStyle
) ) const
889 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
892 //-----------------------------------------------------------------------------
893 // wxCairoMatrixData implementation
894 //-----------------------------------------------------------------------------
896 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
897 : wxGraphicsMatrixData(renderer
)
903 wxCairoMatrixData::~wxCairoMatrixData()
908 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
910 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
913 // concatenates the matrix
914 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
916 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
919 // sets the matrix to the respective values
920 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
921 wxDouble tx
, wxDouble ty
)
923 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
926 // gets the component valuess of the matrix
927 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
928 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
930 if (a
) *a
= m_matrix
.xx
;
931 if (b
) *b
= m_matrix
.yx
;
932 if (c
) *c
= m_matrix
.xy
;
933 if (d
) *d
= m_matrix
.yy
;
934 if (tx
) *tx
= m_matrix
.x0
;
935 if (ty
) *ty
= m_matrix
.y0
;
938 // makes this the inverse matrix
939 void wxCairoMatrixData::Invert()
941 cairo_matrix_invert( &m_matrix
);
944 // returns true if the elements of the transformation matrix are equal ?
945 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
947 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
949 m_matrix
.xx
== tm
->xx
&&
950 m_matrix
.yx
== tm
->yx
&&
951 m_matrix
.xy
== tm
->xy
&&
952 m_matrix
.yy
== tm
->yy
&&
953 m_matrix
.x0
== tm
->x0
&&
954 m_matrix
.y0
== tm
->y0
) ;
957 // return true if this is the identity matrix
958 bool wxCairoMatrixData::IsIdentity() const
960 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
961 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
968 // add the translation to this matrix
969 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
971 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
974 // add the scale to this matrix
975 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
977 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
980 // add the rotation to this matrix (radians)
981 void wxCairoMatrixData::Rotate( wxDouble angle
)
983 cairo_matrix_rotate( &m_matrix
, angle
) ;
987 // apply the transforms
990 // applies that matrix to the point
991 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
993 double lx
= *x
, ly
= *y
;
994 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
999 // applies the matrix except for translations
1000 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1002 double lx
= *dx
, ly
= *dy
;
1003 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1008 // returns the native representation
1009 void * wxCairoMatrixData::GetNativeMatrix() const
1011 return (void*) &m_matrix
;
1014 //-----------------------------------------------------------------------------
1015 // wxCairoContext implementation
1016 //-----------------------------------------------------------------------------
1018 class wxCairoOffsetHelper
1021 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1026 cairo_translate( m_ctx
, 0.5, 0.5 );
1028 ~wxCairoOffsetHelper( )
1031 cairo_translate( m_ctx
, -0.5, -0.5 );
1038 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1039 : wxGraphicsContext(renderer
)
1042 const wxDCImpl
*impl
= dc
.GetImpl();
1043 Init( (cairo_t
*) impl
->GetCairoContext() );
1045 wxPoint org
= dc
.GetDeviceOrigin();
1046 cairo_translate( m_context
, org
.x
, org
.y
);
1049 dc
.GetUserScale( &sx
, &sy
);
1050 cairo_scale( m_context
, sx
, sy
);
1052 org
= dc
.GetLogicalOrigin();
1053 cairo_translate( m_context
, -org
.x
, -org
.y
);
1057 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1058 : wxGraphicsContext(renderer
)
1061 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1062 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1065 wxGraphicsMatrix matrix
= CreateMatrix();
1067 wxPoint org
= dc
.GetDeviceOrigin();
1068 matrix
.Translate( org
.x
, org
.y
);
1070 org
= dc
.GetLogicalOrigin();
1071 matrix
.Translate( -org
.x
, -org
.y
);
1074 dc
.GetUserScale( &sx
, &sy
);
1075 matrix
.Scale( sx
, sy
);
1077 ConcatTransform( matrix
);
1083 dc
.GetSize( &width
, &height
);
1084 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1085 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1086 Init( cairo_create( surface
) );
1087 cairo_surface_destroy( surface
);
1091 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1092 : wxGraphicsContext(renderer
)
1095 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1096 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1099 wxGraphicsMatrix matrix
= CreateMatrix();
1101 wxPoint org
= dc
.GetDeviceOrigin();
1102 matrix
.Translate( org
.x
, org
.y
);
1104 org
= dc
.GetLogicalOrigin();
1105 matrix
.Translate( -org
.x
, -org
.y
);
1108 dc
.GetUserScale( &sx
, &sy
);
1109 matrix
.Scale( sx
, sy
);
1111 ConcatTransform( matrix
);
1117 dc
.GetSize( &width
, &height
);
1118 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1119 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1120 Init( cairo_create( surface
) );
1121 cairo_surface_destroy( surface
);
1126 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1127 : wxGraphicsContext(renderer
)
1129 Init( gdk_cairo_create( drawable
) );
1133 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1134 : wxGraphicsContext(renderer
)
1139 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1140 : wxGraphicsContext(renderer
)
1143 // something along these lines (copied from dcclient)
1145 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1146 // code should still be able to create wxClientDCs for them, so we will
1147 // use the parent window here then.
1148 if (window
->m_wxwindow
== NULL
)
1150 window
= window
->GetParent();
1153 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1155 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1159 wxCairoContext::~wxCairoContext()
1165 cairo_destroy(m_context
);
1169 void wxCairoContext::Init(cairo_t
*context
)
1171 m_context
= context
;
1177 void wxCairoContext::Clip( const wxRegion
& region
)
1179 // Create a path with all the rectangles in the region
1180 wxGraphicsPath path
= GetRenderer()->CreatePath();
1181 wxRegionIterator
ri(region
);
1184 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1188 // Put it in the context
1189 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1190 cairo_append_path(m_context
, cp
);
1192 // clip to that path
1193 cairo_clip(m_context
);
1194 path
.UnGetNativePath(cp
);
1197 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1199 // Create a path with this rectangle
1200 wxGraphicsPath path
= GetRenderer()->CreatePath();
1201 path
.AddRectangle(x
,y
,w
,h
);
1203 // Put it in the context
1204 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1205 cairo_append_path(m_context
, cp
);
1207 // clip to that path
1208 cairo_clip(m_context
);
1209 path
.UnGetNativePath(cp
);
1212 void wxCairoContext::ResetClip()
1214 cairo_reset_clip(m_context
);
1218 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1220 if ( !m_pen
.IsNull() )
1222 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1223 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1224 cairo_append_path(m_context
,cp
);
1225 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1226 cairo_stroke(m_context
);
1227 path
.UnGetNativePath(cp
);
1231 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, int fillStyle
)
1233 if ( !m_brush
.IsNull() )
1235 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1236 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1237 cairo_append_path(m_context
,cp
);
1238 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1239 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1240 cairo_fill(m_context
);
1241 path
.UnGetNativePath(cp
);
1245 void wxCairoContext::Rotate( wxDouble angle
)
1247 cairo_rotate(m_context
,angle
);
1250 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1252 cairo_translate(m_context
,dx
,dy
);
1255 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1257 cairo_scale(m_context
,xScale
,yScale
);
1260 // concatenates this transform with the current transform of this context
1261 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1263 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1266 // sets the transform of this context
1267 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1269 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1272 // gets the matrix of this context
1273 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1275 wxGraphicsMatrix matrix
= CreateMatrix();
1276 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1282 void wxCairoContext::PushState()
1284 cairo_save(m_context
);
1287 void wxCairoContext::PopState()
1289 cairo_restore(m_context
);
1292 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1294 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1296 cairo_surface_t
* surface
;
1297 int bw
= bmp
.GetWidth();
1298 int bh
= bmp
.GetHeight();
1299 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1300 unsigned char* buffer
= new unsigned char[bw
*bh
*4];
1301 wxUint32
* data
= (wxUint32
*)buffer
;
1303 // Create a surface object and copy the bitmap pixel data to it. if the
1304 // image has alpha (or a mask represented as alpha) then we'll use a
1305 // different format and iterator than if it doesn't...
1306 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1308 surface
= cairo_image_surface_create_for_data(
1309 buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1310 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1311 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1313 wxAlphaPixelData::Iterator
p(pixData
);
1314 for (int y
=0; y
<bh
; y
++)
1316 wxAlphaPixelData::Iterator rowStart
= p
;
1317 for (int x
=0; x
<bw
; x
++)
1319 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1320 // with alpha in the upper 8 bits, then red, then green, then
1321 // blue. The 32-bit quantities are stored native-endian.
1322 // Pre-multiplied alpha is used.
1323 unsigned char alpha
= p
.Alpha();
1327 *data
= ( alpha
<< 24
1328 | (p
.Red() * alpha
/255) << 16
1329 | (p
.Green() * alpha
/255) << 8
1330 | (p
.Blue() * alpha
/255) );
1335 p
.OffsetY(pixData
, 1);
1340 surface
= cairo_image_surface_create_for_data(
1341 buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1342 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1343 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1345 wxNativePixelData::Iterator
p(pixData
);
1346 for (int y
=0; y
<bh
; y
++)
1348 wxNativePixelData::Iterator rowStart
= p
;
1349 for (int x
=0; x
<bw
; x
++)
1351 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1352 // the upper 8 bits unused. Red, Green, and Blue are stored in
1353 // the remaining 24 bits in that order. The 32-bit quantities
1354 // are stored native-endian.
1355 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1360 p
.OffsetY(pixData
, 1);
1367 // In case we're scaling the image by using a width and height different
1368 // than the bitmap's size create a pattern transformation on the surface and
1369 // draw the transformed pattern.
1370 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface(surface
);
1371 wxDouble scaleX
= w
/ bw
;
1372 wxDouble scaleY
= h
/ bh
;
1373 cairo_scale(m_context
, scaleX
, scaleY
);
1375 // prepare to draw the image
1376 cairo_translate(m_context
, x
, y
);
1377 cairo_set_source(m_context
, pattern
);
1378 // use the original size here since the context is scaled already...
1379 cairo_rectangle(m_context
, 0, 0, bw
, bh
);
1380 // fill the rectangle using the pattern
1381 cairo_fill(m_context
);
1384 cairo_pattern_destroy(pattern
);
1385 cairo_surface_destroy(surface
);
1390 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1392 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1393 // start using the Cairo backend on other platforms then we may need to
1394 // fiddle with this...
1395 DrawBitmap(icon
, x
, y
, w
, h
);
1399 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1401 if ( m_font
.IsNull() || str
.empty())
1405 const wxCharBuffer data
= str
.utf8_str();
1408 size_t datalen
= strlen(data
);
1409 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1411 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1412 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1413 pango_layout_set_text(layout
, data
, datalen
);
1414 cairo_move_to(m_context
, x
, y
);
1415 pango_cairo_show_layout (m_context
, layout
);
1417 g_object_unref (layout
);
1419 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1420 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1421 // the position we move to by the ascent.
1422 cairo_font_extents_t fe
;
1423 cairo_font_extents(m_context
, &fe
);
1424 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1426 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1427 cairo_show_text(m_context
,buf
);
1431 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1432 wxDouble
*descent
, wxDouble
*externalLeading
) const
1440 if ( externalLeading
)
1441 *externalLeading
= 0;
1443 if ( m_font
.IsNull() || str
.empty())
1449 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1450 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1451 const wxCharBuffer data
= str
.utf8_str();
1456 pango_layout_set_text( layout
, data
, strlen(data
) );
1457 pango_layout_get_pixel_size (layout
, &w
, &h
);
1464 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1465 int baseline
= pango_layout_iter_get_baseline(iter
);
1466 pango_layout_iter_free(iter
);
1467 *descent
= h
- PANGO_PIXELS(baseline
);
1469 g_object_unref (layout
);
1471 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1475 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1476 cairo_text_extents_t te
;
1477 cairo_text_extents(m_context
, buf
, &te
);
1481 if (height
|| descent
|| externalLeading
)
1483 cairo_font_extents_t fe
;
1484 cairo_font_extents(m_context
, &fe
);
1486 // some backends have negative descents
1488 if ( fe
.descent
< 0 )
1489 fe
.descent
= -fe
.descent
;
1491 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1493 // some backends are broken re height ... (eg currently ATSUI)
1494 fe
.height
= fe
.ascent
+ fe
.descent
;
1498 *height
= fe
.height
;
1500 *descent
= fe
.descent
;
1501 if ( externalLeading
)
1502 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1507 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1510 widths
.Add(0, text
.length());
1518 void * wxCairoContext::GetNativeContext()
1523 //-----------------------------------------------------------------------------
1524 // wxCairoRenderer declaration
1525 //-----------------------------------------------------------------------------
1527 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1530 wxCairoRenderer() {}
1532 virtual ~wxCairoRenderer() {}
1536 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1537 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1538 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1540 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1542 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1544 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1546 virtual wxGraphicsContext
* CreateMeasuringContext();
1550 virtual wxGraphicsPath
CreatePath();
1554 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1555 wxDouble tx
=0.0, wxDouble ty
=0.0);
1558 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1560 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1562 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1563 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1564 const wxColour
&c1
, const wxColour
&c2
) ;
1566 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1567 // with radius r and color cColor
1568 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1569 const wxColour
&oColor
, const wxColour
&cColor
) ;
1572 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1574 // create a native bitmap representation
1576 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
)
1578 return wxGraphicsBitmap
;
1581 // create a subimage from a native image representation
1582 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1584 return wxGraphicsBitmap
;
1589 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1592 //-----------------------------------------------------------------------------
1593 // wxCairoRenderer implementation
1594 //-----------------------------------------------------------------------------
1596 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1598 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1599 // temporary hack to allow creating a cairo context on any platform
1600 extern wxGraphicsRenderer
* gCairoRenderer
;
1601 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1604 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1606 return &gs_cairoGraphicsRenderer
;
1610 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1612 return new wxCairoContext(this,dc
);
1615 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1617 return new wxCairoContext(this,dc
);
1620 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1623 const wxDCImpl
*impl
= dc
.GetImpl();
1624 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1626 return new wxCairoContext(this,dc
);
1632 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1634 return new wxCairoContext(this,(cairo_t
*)context
);
1638 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1641 return new wxCairoContext(this,(GdkDrawable
*)window
);
1647 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1653 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1655 return new wxCairoContext(this, window
);
1660 wxGraphicsPath
wxCairoRenderer::CreatePath()
1662 wxGraphicsPath path
;
1663 path
.SetRefData( new wxCairoPathData(this) );
1670 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1671 wxDouble tx
, wxDouble ty
)
1675 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1676 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1681 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1683 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1684 return wxNullGraphicsPen
;
1688 p
.SetRefData(new wxCairoPenData( this, pen
));
1693 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1695 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1696 return wxNullGraphicsBrush
;
1700 p
.SetRefData(new wxCairoBrushData( this, brush
));
1705 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1706 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1707 const wxColour
&c1
, const wxColour
&c2
)
1710 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1711 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1716 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1717 // with radius r and color cColor
1718 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1719 const wxColour
&oColor
, const wxColour
&cColor
)
1722 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1723 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1729 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1734 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1738 return wxNullGraphicsFont
;
1741 #endif // wxUSE_GRAPHICS_CONTEXT