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/osx/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 bool SetLogicalFunction( int function
);
361 virtual void StrokePath( const wxGraphicsPath
& p
);
362 virtual void FillPath( const wxGraphicsPath
& p
, int fillStyle
= wxWINDING_RULE
);
364 virtual void Translate( wxDouble dx
, wxDouble dy
);
365 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
366 virtual void Rotate( wxDouble angle
);
368 // concatenates this transform with the current transform of this context
369 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
371 // sets the transform of this context
372 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
374 // gets the matrix of this context
375 virtual wxGraphicsMatrix
GetTransform() const;
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();
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;
388 void Init(cairo_t
*context
);
393 //-----------------------------------------------------------------------------
394 // wxCairoPenData implementation
395 //-----------------------------------------------------------------------------
397 wxCairoPenData::~wxCairoPenData()
399 delete[] m_userLengths
;
402 void wxCairoPenData::Init()
405 m_userLengths
= NULL
;
410 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
411 : wxGraphicsObjectRefData(renderer
)
415 m_width
= m_pen
.GetWidth();
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;
424 switch ( m_pen
.GetCap() )
427 m_cap
= CAIRO_LINE_CAP_ROUND
;
430 case wxCAP_PROJECTING
:
431 m_cap
= CAIRO_LINE_CAP_SQUARE
;
435 m_cap
= CAIRO_LINE_CAP_BUTT
;
439 m_cap
= CAIRO_LINE_CAP_BUTT
;
443 switch ( m_pen
.GetJoin() )
446 m_join
= CAIRO_LINE_JOIN_BEVEL
;
450 m_join
= CAIRO_LINE_JOIN_MITER
;
454 m_join
= CAIRO_LINE_JOIN_ROUND
;
458 m_join
= CAIRO_LINE_JOIN_MITER
;
462 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
463 const double dotted
[] =
465 dashUnit
, dashUnit
+ 2.0
467 static const double short_dashed
[] =
471 static const double dashed
[] =
475 static const double dotted_dashed
[] =
477 9.0 , 6.0 , 3.0 , 3.0
480 switch ( m_pen
.GetStyle() )
486 m_count
= WXSIZEOF(dotted
);
487 m_userLengths
= new double[ m_count
] ;
488 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
489 m_lengths
= m_userLengths
;
494 m_count
= WXSIZEOF(dashed
);
498 m_lengths
= short_dashed
;
499 m_count
= WXSIZEOF(short_dashed
);
503 m_lengths
= dotted_dashed
;
504 m_count
= WXSIZEOF(dotted_dashed
);
510 m_count
= m_pen
.GetDashes( &wxdashes
) ;
511 if ((wxdashes
!= NULL
) && (m_count
> 0))
513 m_userLengths
= new double[m_count
] ;
514 for ( int i
= 0 ; i
< m_count
; ++i
)
516 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
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
;
524 m_lengths
= m_userLengths
;
530 wxBitmap* bmp = pen.GetStipple();
531 if ( bmp && bmp->Ok() )
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 );
543 if ( m_pen
.GetStyle() >= wxFIRST_HATCH
&& m_pen
.GetStyle() <= wxLAST_HATCH
)
546 wxDELETE( m_penBrush );
547 HatchStyle style = HatchStyleHorizontal;
548 switch( pen.GetStyle() )
550 case wxBDIAGONAL_HATCH :
551 style = HatchStyleBackwardDiagonal;
553 case wxCROSSDIAG_HATCH :
554 style = HatchStyleDiagonalCross;
556 case wxFDIAGONAL_HATCH :
557 style = HatchStyleForwardDiagonal;
560 style = HatchStyleCross;
562 case wxHORIZONTAL_HATCH :
563 style = HatchStyleHorizontal;
565 case wxVERTICAL_HATCH :
566 style = HatchStyleVertical;
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 )
579 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
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);
589 //-----------------------------------------------------------------------------
590 // wxCairoBrushData implementation
591 //-----------------------------------------------------------------------------
593 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
594 : wxGraphicsObjectRefData( renderer
)
599 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
600 : wxGraphicsObjectRefData(renderer
)
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;
609 if ( brush.GetStyle() == wxSOLID)
611 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
612 brush.GetColour().Green() , brush.GetColour().Blue() ) );
614 else if ( brush.IsHatch() )
616 HatchStyle style = HatchStyleHorizontal;
617 switch( brush.GetStyle() )
619 case wxBDIAGONAL_HATCH :
620 style = HatchStyleBackwardDiagonal;
622 case wxCROSSDIAG_HATCH :
623 style = HatchStyleDiagonalCross;
625 case wxFDIAGONAL_HATCH :
626 style = HatchStyleForwardDiagonal;
629 style = HatchStyleCross;
631 case wxHORIZONTAL_HATCH :
632 style = HatchStyleHorizontal;
634 case wxVERTICAL_HATCH :
635 style = HatchStyleVertical;
639 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
640 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
644 wxBitmap* bmp = brush.GetStipple();
645 if ( bmp && bmp->Ok() )
647 wxDELETE( m_brushImage );
648 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
649 m_brush = new TextureBrush(m_brushImage);
655 wxCairoBrushData::~wxCairoBrushData ()
658 cairo_pattern_destroy(m_brushPattern
);
661 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
663 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
664 if ( m_brushPattern
)
666 cairo_set_source(ctext
,m_brushPattern
);
670 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
674 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
675 const wxColour
&c1
, const wxColour
&c2
)
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"));
685 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
686 const wxColour
&oColor
, const wxColour
&cColor
)
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"));
696 void wxCairoBrushData::Init()
698 m_brushPattern
= NULL
;
701 //-----------------------------------------------------------------------------
702 // wxCairoFontData implementation
703 //-----------------------------------------------------------------------------
705 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
706 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
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();
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
);
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
;
725 wxCairoFontData::~wxCairoFontData()
728 cairo_font_face_destroy( m_font
);
729 #elif defined(__WXGTK__)
730 pango_font_description_free( m_font
);
735 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
737 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
738 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
740 // the rest is done using Pango layouts
741 #elif defined(__WXMAC__)
742 cairo_set_font_face(ctext
, m_font
);
743 cairo_set_font_size(ctext
, m_size
);
745 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
746 cairo_set_font_size(ctext
, m_size
);
750 //-----------------------------------------------------------------------------
751 // wxCairoPathData implementation
752 //-----------------------------------------------------------------------------
754 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
755 : wxGraphicsPathData(renderer
)
759 m_pathContext
= pathcontext
;
763 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
764 m_pathContext
= cairo_create(surface
);
765 cairo_surface_destroy (surface
);
769 wxCairoPathData::~wxCairoPathData()
771 cairo_destroy(m_pathContext
);
774 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
776 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
777 cairo_t
* pathcontext
= cairo_create(surface
);
778 cairo_surface_destroy (surface
);
780 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
781 cairo_append_path(pathcontext
, path
);
782 cairo_path_destroy(path
);
783 return new wxCairoPathData( GetRenderer() ,pathcontext
);
787 void* wxCairoPathData::GetNativePath() const
789 return cairo_copy_path(m_pathContext
) ;
792 void wxCairoPathData::UnGetNativePath(void *p
) const
794 cairo_path_destroy((cairo_path_t
*)p
);
801 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
803 cairo_move_to(m_pathContext
,x
,y
);
806 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
808 cairo_line_to(m_pathContext
,x
,y
);
811 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
813 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
814 cairo_append_path(m_pathContext
, p
);
818 void wxCairoPathData::CloseSubpath()
820 cairo_close_path(m_pathContext
);
823 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
825 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
828 // gets the last point of the current path, (0,0) if not yet set
829 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
832 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
839 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
841 // as clockwise means positive in our system (y pointing downwards)
842 // TODO make this interpretation dependent of the
844 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
845 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
847 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
850 // transforms each point of this path by the matrix
851 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
853 // as we don't have a true path object, we have to apply the inverse
854 // matrix to the context
855 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
856 cairo_matrix_invert( &m
);
857 cairo_transform(m_pathContext
,&m
);
860 // gets the bounding box enclosing all points (possibly including control points)
861 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
865 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
889 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, int WXUNUSED(fillStyle
) ) const
891 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
894 //-----------------------------------------------------------------------------
895 // wxCairoMatrixData implementation
896 //-----------------------------------------------------------------------------
898 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
899 : wxGraphicsMatrixData(renderer
)
905 wxCairoMatrixData::~wxCairoMatrixData()
910 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
912 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
915 // concatenates the matrix
916 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
918 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
921 // sets the matrix to the respective values
922 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
923 wxDouble tx
, wxDouble ty
)
925 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
928 // gets the component valuess of the matrix
929 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
930 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
932 if (a
) *a
= m_matrix
.xx
;
933 if (b
) *b
= m_matrix
.yx
;
934 if (c
) *c
= m_matrix
.xy
;
935 if (d
) *d
= m_matrix
.yy
;
936 if (tx
) *tx
= m_matrix
.x0
;
937 if (ty
) *ty
= m_matrix
.y0
;
940 // makes this the inverse matrix
941 void wxCairoMatrixData::Invert()
943 cairo_matrix_invert( &m_matrix
);
946 // returns true if the elements of the transformation matrix are equal ?
947 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
949 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
951 m_matrix
.xx
== tm
->xx
&&
952 m_matrix
.yx
== tm
->yx
&&
953 m_matrix
.xy
== tm
->xy
&&
954 m_matrix
.yy
== tm
->yy
&&
955 m_matrix
.x0
== tm
->x0
&&
956 m_matrix
.y0
== tm
->y0
) ;
959 // return true if this is the identity matrix
960 bool wxCairoMatrixData::IsIdentity() const
962 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
963 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
970 // add the translation to this matrix
971 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
973 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
976 // add the scale to this matrix
977 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
979 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
982 // add the rotation to this matrix (radians)
983 void wxCairoMatrixData::Rotate( wxDouble angle
)
985 cairo_matrix_rotate( &m_matrix
, angle
) ;
989 // apply the transforms
992 // applies that matrix to the point
993 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
995 double lx
= *x
, ly
= *y
;
996 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1001 // applies the matrix except for translations
1002 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1004 double lx
= *dx
, ly
= *dy
;
1005 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1010 // returns the native representation
1011 void * wxCairoMatrixData::GetNativeMatrix() const
1013 return (void*) &m_matrix
;
1016 //-----------------------------------------------------------------------------
1017 // wxCairoContext implementation
1018 //-----------------------------------------------------------------------------
1020 class wxCairoOffsetHelper
1023 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1028 cairo_translate( m_ctx
, 0.5, 0.5 );
1030 ~wxCairoOffsetHelper( )
1033 cairo_translate( m_ctx
, -0.5, -0.5 );
1040 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1041 : wxGraphicsContext(renderer
)
1044 const wxDCImpl
*impl
= dc
.GetImpl();
1045 Init( (cairo_t
*) impl
->GetCairoContext() );
1047 wxPoint org
= dc
.GetDeviceOrigin();
1048 cairo_translate( m_context
, org
.x
, org
.y
);
1051 dc
.GetUserScale( &sx
, &sy
);
1052 cairo_scale( m_context
, sx
, sy
);
1054 org
= dc
.GetLogicalOrigin();
1055 cairo_translate( m_context
, -org
.x
, -org
.y
);
1059 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1060 : wxGraphicsContext(renderer
)
1063 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1064 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1067 wxGraphicsMatrix matrix
= CreateMatrix();
1069 wxPoint org
= dc
.GetDeviceOrigin();
1070 matrix
.Translate( org
.x
, org
.y
);
1072 org
= dc
.GetLogicalOrigin();
1073 matrix
.Translate( -org
.x
, -org
.y
);
1076 dc
.GetUserScale( &sx
, &sy
);
1077 matrix
.Scale( sx
, sy
);
1079 ConcatTransform( matrix
);
1085 dc
.GetSize( &width
, &height
);
1086 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1087 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1088 Init( cairo_create( surface
) );
1089 cairo_surface_destroy( surface
);
1093 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1094 : wxGraphicsContext(renderer
)
1097 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1098 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1101 wxGraphicsMatrix matrix
= CreateMatrix();
1103 wxPoint org
= dc
.GetDeviceOrigin();
1104 matrix
.Translate( org
.x
, org
.y
);
1106 org
= dc
.GetLogicalOrigin();
1107 matrix
.Translate( -org
.x
, -org
.y
);
1110 dc
.GetUserScale( &sx
, &sy
);
1111 matrix
.Scale( sx
, sy
);
1113 ConcatTransform( matrix
);
1119 dc
.GetSize( &width
, &height
);
1120 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1121 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1122 Init( cairo_create( surface
) );
1123 cairo_surface_destroy( surface
);
1128 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1129 : wxGraphicsContext(renderer
)
1131 Init( gdk_cairo_create( drawable
) );
1135 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1136 : wxGraphicsContext(renderer
)
1141 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1142 : wxGraphicsContext(renderer
)
1145 // something along these lines (copied from dcclient)
1147 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1148 // code should still be able to create wxClientDCs for them, so we will
1149 // use the parent window here then.
1150 if (window
->m_wxwindow
== NULL
)
1152 window
= window
->GetParent();
1155 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1157 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1161 wxCairoContext::~wxCairoContext()
1167 cairo_destroy(m_context
);
1171 void wxCairoContext::Init(cairo_t
*context
)
1173 m_context
= context
;
1179 void wxCairoContext::Clip( const wxRegion
& region
)
1181 // Create a path with all the rectangles in the region
1182 wxGraphicsPath path
= GetRenderer()->CreatePath();
1183 wxRegionIterator
ri(region
);
1186 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1190 // Put it in the context
1191 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1192 cairo_append_path(m_context
, cp
);
1194 // clip to that path
1195 cairo_clip(m_context
);
1196 path
.UnGetNativePath(cp
);
1199 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1201 // Create a path with this rectangle
1202 wxGraphicsPath path
= GetRenderer()->CreatePath();
1203 path
.AddRectangle(x
,y
,w
,h
);
1205 // Put it in the context
1206 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1207 cairo_append_path(m_context
, cp
);
1209 // clip to that path
1210 cairo_clip(m_context
);
1211 path
.UnGetNativePath(cp
);
1214 void wxCairoContext::ResetClip()
1216 cairo_reset_clip(m_context
);
1220 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1222 if ( !m_pen
.IsNull() )
1224 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1225 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1226 cairo_append_path(m_context
,cp
);
1227 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1228 cairo_stroke(m_context
);
1229 path
.UnGetNativePath(cp
);
1233 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, int fillStyle
)
1235 if ( !m_brush
.IsNull() )
1237 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1238 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1239 cairo_append_path(m_context
,cp
);
1240 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1241 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1242 cairo_fill(m_context
);
1243 path
.UnGetNativePath(cp
);
1247 void wxCairoContext::Rotate( wxDouble angle
)
1249 cairo_rotate(m_context
,angle
);
1252 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1254 cairo_translate(m_context
,dx
,dy
);
1257 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1259 cairo_scale(m_context
,xScale
,yScale
);
1262 // concatenates this transform with the current transform of this context
1263 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1265 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1268 // sets the transform of this context
1269 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1271 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1274 // gets the matrix of this context
1275 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1277 wxGraphicsMatrix matrix
= CreateMatrix();
1278 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1284 void wxCairoContext::PushState()
1286 cairo_save(m_context
);
1289 void wxCairoContext::PopState()
1291 cairo_restore(m_context
);
1294 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1296 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1298 cairo_surface_t
* surface
;
1299 int bw
= bmp
.GetWidth();
1300 int bh
= bmp
.GetHeight();
1301 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1302 unsigned char* buffer
= new unsigned char[bw
*bh
*4];
1303 wxUint32
* data
= (wxUint32
*)buffer
;
1305 // Create a surface object and copy the bitmap pixel data to it. if the
1306 // image has alpha (or a mask represented as alpha) then we'll use a
1307 // different format and iterator than if it doesn't...
1308 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1310 surface
= cairo_image_surface_create_for_data(
1311 buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1312 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1313 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1315 wxAlphaPixelData::Iterator
p(pixData
);
1316 for (int y
=0; y
<bh
; y
++)
1318 wxAlphaPixelData::Iterator rowStart
= p
;
1319 for (int x
=0; x
<bw
; x
++)
1321 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1322 // with alpha in the upper 8 bits, then red, then green, then
1323 // blue. The 32-bit quantities are stored native-endian.
1324 // Pre-multiplied alpha is used.
1325 unsigned char alpha
= p
.Alpha();
1329 *data
= ( alpha
<< 24
1330 | (p
.Red() * alpha
/255) << 16
1331 | (p
.Green() * alpha
/255) << 8
1332 | (p
.Blue() * alpha
/255) );
1337 p
.OffsetY(pixData
, 1);
1342 surface
= cairo_image_surface_create_for_data(
1343 buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1344 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1345 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1347 wxNativePixelData::Iterator
p(pixData
);
1348 for (int y
=0; y
<bh
; y
++)
1350 wxNativePixelData::Iterator rowStart
= p
;
1351 for (int x
=0; x
<bw
; x
++)
1353 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1354 // the upper 8 bits unused. Red, Green, and Blue are stored in
1355 // the remaining 24 bits in that order. The 32-bit quantities
1356 // are stored native-endian.
1357 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1362 p
.OffsetY(pixData
, 1);
1369 // In case we're scaling the image by using a width and height different
1370 // than the bitmap's size create a pattern transformation on the surface and
1371 // draw the transformed pattern.
1372 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface(surface
);
1373 wxDouble scaleX
= w
/ bw
;
1374 wxDouble scaleY
= h
/ bh
;
1375 cairo_scale(m_context
, scaleX
, scaleY
);
1377 // prepare to draw the image
1378 cairo_translate(m_context
, x
, y
);
1379 cairo_set_source(m_context
, pattern
);
1380 // use the original size here since the context is scaled already...
1381 cairo_rectangle(m_context
, 0, 0, bw
, bh
);
1382 // fill the rectangle using the pattern
1383 cairo_fill(m_context
);
1386 cairo_pattern_destroy(pattern
);
1387 cairo_surface_destroy(surface
);
1392 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1394 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1395 // start using the Cairo backend on other platforms then we may need to
1396 // fiddle with this...
1397 DrawBitmap(icon
, x
, y
, w
, h
);
1401 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1403 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::DrawText - no valid font set") );
1409 const wxCharBuffer data
= str
.utf8_str();
1412 size_t datalen
= strlen(data
);
1413 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1415 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1416 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1417 pango_layout_set_text(layout
, data
, datalen
);
1418 cairo_move_to(m_context
, x
, y
);
1419 pango_cairo_show_layout (m_context
, layout
);
1421 g_object_unref (layout
);
1423 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1424 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1425 // the position we move to by the ascent.
1426 cairo_font_extents_t fe
;
1427 cairo_font_extents(m_context
, &fe
);
1428 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1430 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1431 cairo_show_text(m_context
,buf
);
1435 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1436 wxDouble
*descent
, wxDouble
*externalLeading
) const
1438 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1446 if ( externalLeading
)
1447 *externalLeading
= 0;
1455 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1456 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1457 const wxCharBuffer data
= str
.utf8_str();
1462 pango_layout_set_text( layout
, data
, strlen(data
) );
1463 pango_layout_get_pixel_size (layout
, &w
, &h
);
1470 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1471 int baseline
= pango_layout_iter_get_baseline(iter
);
1472 pango_layout_iter_free(iter
);
1473 *descent
= h
- PANGO_PIXELS(baseline
);
1475 g_object_unref (layout
);
1477 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1481 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1482 cairo_text_extents_t te
;
1483 cairo_text_extents(m_context
, buf
, &te
);
1487 if (height
|| descent
|| externalLeading
)
1489 cairo_font_extents_t fe
;
1490 cairo_font_extents(m_context
, &fe
);
1492 // some backends have negative descents
1494 if ( fe
.descent
< 0 )
1495 fe
.descent
= -fe
.descent
;
1497 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1499 // some backends are broken re height ... (eg currently ATSUI)
1500 fe
.height
= fe
.ascent
+ fe
.descent
;
1504 *height
= fe
.height
;
1506 *descent
= fe
.descent
;
1507 if ( externalLeading
)
1508 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1513 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1516 widths
.Add(0, text
.length());
1518 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1526 void * wxCairoContext::GetNativeContext()
1531 // Cairo doesn't support bitwise logical function (a.k.a. ROP, raster output
1532 // mode). Cairo supports Porter-Duff compositing operators, but they are quite
1533 // different, although in some cases have similar names.
1534 bool wxCairoContext::SetLogicalFunction( int function
)
1536 if (m_logicalFunction
== function
)
1539 cairo_operator_t op
;
1543 case wxCOPY
: // (default) src
1544 op
= CAIRO_OPERATOR_OVER
; // (also default)
1546 case wxOR
: // src OR dst
1547 op
= CAIRO_OPERATOR_ADD
;
1549 case wxNO_OP
: // dst
1550 op
= CAIRO_OPERATOR_DEST
; // ignore the source
1553 op
= CAIRO_OPERATOR_CLEAR
;// clear dst
1556 case wxAND
: // src AND dst
1557 case wxAND_INVERT
: // (NOT src) AND dst
1558 case wxAND_REVERSE
:// src AND (NOT dst)
1559 case wxEQUIV
: // (NOT src) XOR dst
1560 case wxINVERT
: // NOT dst
1561 case wxNAND
: // (NOT src) OR (NOT dst)
1562 case wxNOR
: // (NOT src) AND (NOT dst)
1563 case wxOR_INVERT
: // (NOT src) OR dst
1564 case wxOR_REVERSE
: // src OR (NOT dst)
1566 case wxSRC_INVERT
: // NOT src
1567 //wxXOR does _not_ correspond to CAIRO_OPERATOR_XOR
1568 case wxXOR
: // src XOR dst
1573 m_logicalFunction
= function
;
1574 cairo_set_operator(m_context
, op
);
1579 //-----------------------------------------------------------------------------
1580 // wxCairoRenderer declaration
1581 //-----------------------------------------------------------------------------
1583 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1586 wxCairoRenderer() {}
1588 virtual ~wxCairoRenderer() {}
1592 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1593 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1594 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1596 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1598 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1600 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1602 virtual wxGraphicsContext
* CreateMeasuringContext();
1606 virtual wxGraphicsPath
CreatePath();
1610 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1611 wxDouble tx
=0.0, wxDouble ty
=0.0);
1614 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1616 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1618 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1619 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1620 const wxColour
&c1
, const wxColour
&c2
) ;
1622 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1623 // with radius r and color cColor
1624 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1625 const wxColour
&oColor
, const wxColour
&cColor
) ;
1628 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1630 // create a native bitmap representation
1632 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
)
1634 return wxGraphicsBitmap
;
1637 // create a subimage from a native image representation
1638 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1640 return wxGraphicsBitmap
;
1645 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1648 //-----------------------------------------------------------------------------
1649 // wxCairoRenderer implementation
1650 //-----------------------------------------------------------------------------
1652 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1654 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1655 // temporary hack to allow creating a cairo context on any platform
1656 extern wxGraphicsRenderer
* gCairoRenderer
;
1657 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1660 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1662 return &gs_cairoGraphicsRenderer
;
1666 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1668 return new wxCairoContext(this,dc
);
1671 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1673 return new wxCairoContext(this,dc
);
1676 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1679 const wxDCImpl
*impl
= dc
.GetImpl();
1680 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1682 return new wxCairoContext(this,dc
);
1688 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1690 return new wxCairoContext(this,(cairo_t
*)context
);
1694 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1697 return new wxCairoContext(this,(GdkDrawable
*)window
);
1703 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1709 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1711 return new wxCairoContext(this, window
);
1716 wxGraphicsPath
wxCairoRenderer::CreatePath()
1718 wxGraphicsPath path
;
1719 path
.SetRefData( new wxCairoPathData(this) );
1726 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1727 wxDouble tx
, wxDouble ty
)
1731 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1732 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1737 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1739 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1740 return wxNullGraphicsPen
;
1744 p
.SetRefData(new wxCairoPenData( this, pen
));
1749 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1751 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1752 return wxNullGraphicsBrush
;
1756 p
.SetRefData(new wxCairoBrushData( this, brush
));
1761 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1762 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1763 const wxColour
&c1
, const wxColour
&c2
)
1766 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1767 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1772 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1773 // with radius r and color cColor
1774 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1775 const wxColour
&oColor
, const wxColour
&cColor
)
1778 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1779 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1785 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1790 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1794 return wxNullGraphicsFont
;
1797 #endif // wxUSE_GRAPHICS_CONTEXT