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() )
482 case wxPENSTYLE_SOLID
:
485 case wxPENSTYLE_DOT
:
486 m_count
= WXSIZEOF(dotted
);
487 m_userLengths
= new double[ m_count
] ;
488 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
489 m_lengths
= m_userLengths
;
492 case wxPENSTYLE_LONG_DASH
:
494 m_count
= WXSIZEOF(dashed
);
497 case wxPENSTYLE_SHORT_DASH
:
498 m_lengths
= short_dashed
;
499 m_count
= WXSIZEOF(short_dashed
);
502 case wxPENSTYLE_DOT_DASH
:
503 m_lengths
= dotted_dashed
;
504 m_count
= WXSIZEOF(dotted_dashed
);
507 case wxPENSTYLE_USER_DASH
:
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
;
527 case wxPENSTYLE_STIPPLE
:
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() >= wxPENSTYLE_FIRST_HATCH
544 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
547 wxDELETE( m_penBrush );
548 HatchStyle style = HatchStyleHorizontal;
549 switch( pen.GetStyle() )
551 case wxPENSTYLE_BDIAGONAL_HATCH :
552 style = HatchStyleBackwardDiagonal;
554 case wxPENSTYLE_CROSSDIAG_HATCH :
555 style = HatchStyleDiagonalCross;
557 case wxPENSTYLE_FDIAGONAL_HATCH :
558 style = HatchStyleForwardDiagonal;
560 case wxPENSTYLE_CROSS_HATCH :
561 style = HatchStyleCross;
563 case wxPENSTYLE_HORIZONTAL_HATCH :
564 style = HatchStyleHorizontal;
566 case wxPENSTYLE_VERTICAL_HATCH :
567 style = HatchStyleVertical;
571 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
572 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
573 m_pen->SetBrush( m_penBrush )
580 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
582 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
583 cairo_set_line_width(ctext
,m_width
);
584 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
585 cairo_set_line_cap(ctext
,m_cap
);
586 cairo_set_line_join(ctext
,m_join
);
587 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
590 //-----------------------------------------------------------------------------
591 // wxCairoBrushData implementation
592 //-----------------------------------------------------------------------------
594 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
595 : wxGraphicsObjectRefData( renderer
)
600 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
601 : wxGraphicsObjectRefData(renderer
)
605 m_red
= brush
.GetColour().Red()/255.0;
606 m_green
= brush
.GetColour().Green()/255.0;
607 m_blue
= brush
.GetColour().Blue()/255.0;
608 m_alpha
= brush
.GetColour().Alpha()/255.0;
610 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
612 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
613 brush.GetColour().Green() , brush.GetColour().Blue() ) );
615 else if ( brush.IsHatch() )
617 HatchStyle style = HatchStyleHorizontal;
618 switch( brush.GetStyle() )
620 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
621 style = HatchStyleBackwardDiagonal;
623 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
624 style = HatchStyleDiagonalCross;
626 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
627 style = HatchStyleForwardDiagonal;
629 case wxBRUSHSTYLE_CROSS_HATCH :
630 style = HatchStyleCross;
632 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
633 style = HatchStyleHorizontal;
635 case wxBRUSHSTYLE_VERTICAL_HATCH :
636 style = HatchStyleVertical;
640 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
641 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
645 wxBitmap* bmp = brush.GetStipple();
646 if ( bmp && bmp->Ok() )
648 wxDELETE( m_brushImage );
649 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
650 m_brush = new TextureBrush(m_brushImage);
656 wxCairoBrushData::~wxCairoBrushData ()
659 cairo_pattern_destroy(m_brushPattern
);
662 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
664 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
665 if ( m_brushPattern
)
667 cairo_set_source(ctext
,m_brushPattern
);
671 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
675 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
676 const wxColour
&c1
, const wxColour
&c2
)
678 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
679 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
680 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
681 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
682 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
683 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
686 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
687 const wxColour
&oColor
, const wxColour
&cColor
)
689 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
690 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
691 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
692 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
693 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
694 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
697 void wxCairoBrushData::Init()
699 m_brushPattern
= NULL
;
702 //-----------------------------------------------------------------------------
703 // wxCairoFontData implementation
704 //-----------------------------------------------------------------------------
706 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
707 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
709 m_red
= col
.Red()/255.0;
710 m_green
= col
.Green()/255.0;
711 m_blue
= col
.Blue()/255.0;
712 m_alpha
= col
.Alpha()/255.0;
713 m_size
= font
.GetPointSize();
716 m_font
= cairo_atsui_font_face_create_for_atsu_font_id( font
.MacGetATSUFontID() );
717 #elif defined(__WXGTK__)
718 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
720 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
721 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
722 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
726 wxCairoFontData::~wxCairoFontData()
729 cairo_font_face_destroy( m_font
);
730 #elif defined(__WXGTK__)
731 pango_font_description_free( m_font
);
736 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
738 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
739 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
741 // the rest is done using Pango layouts
742 #elif defined(__WXMAC__)
743 cairo_set_font_face(ctext
, m_font
);
744 cairo_set_font_size(ctext
, m_size
);
746 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
747 cairo_set_font_size(ctext
, m_size
);
751 //-----------------------------------------------------------------------------
752 // wxCairoPathData implementation
753 //-----------------------------------------------------------------------------
755 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
756 : wxGraphicsPathData(renderer
)
760 m_pathContext
= pathcontext
;
764 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
765 m_pathContext
= cairo_create(surface
);
766 cairo_surface_destroy (surface
);
770 wxCairoPathData::~wxCairoPathData()
772 cairo_destroy(m_pathContext
);
775 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
777 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
778 cairo_t
* pathcontext
= cairo_create(surface
);
779 cairo_surface_destroy (surface
);
781 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
782 cairo_append_path(pathcontext
, path
);
783 cairo_path_destroy(path
);
784 return new wxCairoPathData( GetRenderer() ,pathcontext
);
788 void* wxCairoPathData::GetNativePath() const
790 return cairo_copy_path(m_pathContext
) ;
793 void wxCairoPathData::UnGetNativePath(void *p
) const
795 cairo_path_destroy((cairo_path_t
*)p
);
802 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
804 cairo_move_to(m_pathContext
,x
,y
);
807 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
809 cairo_line_to(m_pathContext
,x
,y
);
812 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
814 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
815 cairo_append_path(m_pathContext
, p
);
819 void wxCairoPathData::CloseSubpath()
821 cairo_close_path(m_pathContext
);
824 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
826 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
829 // gets the last point of the current path, (0,0) if not yet set
830 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
833 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
840 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
842 // as clockwise means positive in our system (y pointing downwards)
843 // TODO make this interpretation dependent of the
845 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
846 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
848 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
851 // transforms each point of this path by the matrix
852 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
854 // as we don't have a true path object, we have to apply the inverse
855 // matrix to the context
856 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
857 cairo_matrix_invert( &m
);
858 cairo_transform(m_pathContext
,&m
);
861 // gets the bounding box enclosing all points (possibly including control points)
862 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
866 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
890 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, int WXUNUSED(fillStyle
) ) const
892 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
895 //-----------------------------------------------------------------------------
896 // wxCairoMatrixData implementation
897 //-----------------------------------------------------------------------------
899 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
900 : wxGraphicsMatrixData(renderer
)
906 wxCairoMatrixData::~wxCairoMatrixData()
911 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
913 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
916 // concatenates the matrix
917 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
919 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
922 // sets the matrix to the respective values
923 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
924 wxDouble tx
, wxDouble ty
)
926 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
929 // gets the component valuess of the matrix
930 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
931 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
933 if (a
) *a
= m_matrix
.xx
;
934 if (b
) *b
= m_matrix
.yx
;
935 if (c
) *c
= m_matrix
.xy
;
936 if (d
) *d
= m_matrix
.yy
;
937 if (tx
) *tx
= m_matrix
.x0
;
938 if (ty
) *ty
= m_matrix
.y0
;
941 // makes this the inverse matrix
942 void wxCairoMatrixData::Invert()
944 cairo_matrix_invert( &m_matrix
);
947 // returns true if the elements of the transformation matrix are equal ?
948 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
950 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
952 m_matrix
.xx
== tm
->xx
&&
953 m_matrix
.yx
== tm
->yx
&&
954 m_matrix
.xy
== tm
->xy
&&
955 m_matrix
.yy
== tm
->yy
&&
956 m_matrix
.x0
== tm
->x0
&&
957 m_matrix
.y0
== tm
->y0
) ;
960 // return true if this is the identity matrix
961 bool wxCairoMatrixData::IsIdentity() const
963 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
964 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
971 // add the translation to this matrix
972 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
974 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
977 // add the scale to this matrix
978 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
980 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
983 // add the rotation to this matrix (radians)
984 void wxCairoMatrixData::Rotate( wxDouble angle
)
986 cairo_matrix_rotate( &m_matrix
, angle
) ;
990 // apply the transforms
993 // applies that matrix to the point
994 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
996 double lx
= *x
, ly
= *y
;
997 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
1002 // applies the matrix except for translations
1003 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1005 double lx
= *dx
, ly
= *dy
;
1006 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1011 // returns the native representation
1012 void * wxCairoMatrixData::GetNativeMatrix() const
1014 return (void*) &m_matrix
;
1017 //-----------------------------------------------------------------------------
1018 // wxCairoContext implementation
1019 //-----------------------------------------------------------------------------
1021 class wxCairoOffsetHelper
1024 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1029 cairo_translate( m_ctx
, 0.5, 0.5 );
1031 ~wxCairoOffsetHelper( )
1034 cairo_translate( m_ctx
, -0.5, -0.5 );
1041 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1042 : wxGraphicsContext(renderer
)
1045 const wxDCImpl
*impl
= dc
.GetImpl();
1046 Init( (cairo_t
*) impl
->GetCairoContext() );
1048 wxPoint org
= dc
.GetDeviceOrigin();
1049 cairo_translate( m_context
, org
.x
, org
.y
);
1052 dc
.GetUserScale( &sx
, &sy
);
1053 cairo_scale( m_context
, sx
, sy
);
1055 org
= dc
.GetLogicalOrigin();
1056 cairo_translate( m_context
, -org
.x
, -org
.y
);
1060 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1061 : wxGraphicsContext(renderer
)
1064 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1065 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1068 wxGraphicsMatrix matrix
= CreateMatrix();
1070 wxPoint org
= dc
.GetDeviceOrigin();
1071 matrix
.Translate( org
.x
, org
.y
);
1073 org
= dc
.GetLogicalOrigin();
1074 matrix
.Translate( -org
.x
, -org
.y
);
1077 dc
.GetUserScale( &sx
, &sy
);
1078 matrix
.Scale( sx
, sy
);
1080 ConcatTransform( matrix
);
1086 dc
.GetSize( &width
, &height
);
1087 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1088 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1089 Init( cairo_create( surface
) );
1090 cairo_surface_destroy( surface
);
1094 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1095 : wxGraphicsContext(renderer
)
1098 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1099 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1102 wxGraphicsMatrix matrix
= CreateMatrix();
1104 wxPoint org
= dc
.GetDeviceOrigin();
1105 matrix
.Translate( org
.x
, org
.y
);
1107 org
= dc
.GetLogicalOrigin();
1108 matrix
.Translate( -org
.x
, -org
.y
);
1111 dc
.GetUserScale( &sx
, &sy
);
1112 matrix
.Scale( sx
, sy
);
1114 ConcatTransform( matrix
);
1120 dc
.GetSize( &width
, &height
);
1121 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1122 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1123 Init( cairo_create( surface
) );
1124 cairo_surface_destroy( surface
);
1129 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1130 : wxGraphicsContext(renderer
)
1132 Init( gdk_cairo_create( drawable
) );
1136 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1137 : wxGraphicsContext(renderer
)
1142 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1143 : wxGraphicsContext(renderer
)
1146 // something along these lines (copied from dcclient)
1148 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1149 // code should still be able to create wxClientDCs for them, so we will
1150 // use the parent window here then.
1151 if (window
->m_wxwindow
== NULL
)
1153 window
= window
->GetParent();
1156 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1158 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1162 wxCairoContext::~wxCairoContext()
1168 cairo_destroy(m_context
);
1172 void wxCairoContext::Init(cairo_t
*context
)
1174 m_context
= context
;
1180 void wxCairoContext::Clip( const wxRegion
& region
)
1182 // Create a path with all the rectangles in the region
1183 wxGraphicsPath path
= GetRenderer()->CreatePath();
1184 wxRegionIterator
ri(region
);
1187 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1191 // Put it in the context
1192 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1193 cairo_append_path(m_context
, cp
);
1195 // clip to that path
1196 cairo_clip(m_context
);
1197 path
.UnGetNativePath(cp
);
1200 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1202 // Create a path with this rectangle
1203 wxGraphicsPath path
= GetRenderer()->CreatePath();
1204 path
.AddRectangle(x
,y
,w
,h
);
1206 // Put it in the context
1207 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1208 cairo_append_path(m_context
, cp
);
1210 // clip to that path
1211 cairo_clip(m_context
);
1212 path
.UnGetNativePath(cp
);
1215 void wxCairoContext::ResetClip()
1217 cairo_reset_clip(m_context
);
1221 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1223 if ( !m_pen
.IsNull() )
1225 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1226 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1227 cairo_append_path(m_context
,cp
);
1228 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1229 cairo_stroke(m_context
);
1230 path
.UnGetNativePath(cp
);
1234 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, int fillStyle
)
1236 if ( !m_brush
.IsNull() )
1238 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1239 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1240 cairo_append_path(m_context
,cp
);
1241 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1242 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1243 cairo_fill(m_context
);
1244 path
.UnGetNativePath(cp
);
1248 void wxCairoContext::Rotate( wxDouble angle
)
1250 cairo_rotate(m_context
,angle
);
1253 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1255 cairo_translate(m_context
,dx
,dy
);
1258 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1260 cairo_scale(m_context
,xScale
,yScale
);
1263 // concatenates this transform with the current transform of this context
1264 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1266 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1269 // sets the transform of this context
1270 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1272 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1275 // gets the matrix of this context
1276 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1278 wxGraphicsMatrix matrix
= CreateMatrix();
1279 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1285 void wxCairoContext::PushState()
1287 cairo_save(m_context
);
1290 void wxCairoContext::PopState()
1292 cairo_restore(m_context
);
1295 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1297 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1299 cairo_surface_t
* surface
;
1300 int bw
= bmp
.GetWidth();
1301 int bh
= bmp
.GetHeight();
1302 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1303 unsigned char* buffer
= new unsigned char[bw
*bh
*4];
1304 wxUint32
* data
= (wxUint32
*)buffer
;
1306 // Create a surface object and copy the bitmap pixel data to it. if the
1307 // image has alpha (or a mask represented as alpha) then we'll use a
1308 // different format and iterator than if it doesn't...
1309 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1311 surface
= cairo_image_surface_create_for_data(
1312 buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1313 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1314 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1316 wxAlphaPixelData::Iterator
p(pixData
);
1317 for (int y
=0; y
<bh
; y
++)
1319 wxAlphaPixelData::Iterator rowStart
= p
;
1320 for (int x
=0; x
<bw
; x
++)
1322 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1323 // with alpha in the upper 8 bits, then red, then green, then
1324 // blue. The 32-bit quantities are stored native-endian.
1325 // Pre-multiplied alpha is used.
1326 unsigned char alpha
= p
.Alpha();
1330 *data
= ( alpha
<< 24
1331 | (p
.Red() * alpha
/255) << 16
1332 | (p
.Green() * alpha
/255) << 8
1333 | (p
.Blue() * alpha
/255) );
1338 p
.OffsetY(pixData
, 1);
1343 surface
= cairo_image_surface_create_for_data(
1344 buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1345 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1346 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1348 wxNativePixelData::Iterator
p(pixData
);
1349 for (int y
=0; y
<bh
; y
++)
1351 wxNativePixelData::Iterator rowStart
= p
;
1352 for (int x
=0; x
<bw
; x
++)
1354 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1355 // the upper 8 bits unused. Red, Green, and Blue are stored in
1356 // the remaining 24 bits in that order. The 32-bit quantities
1357 // are stored native-endian.
1358 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1363 p
.OffsetY(pixData
, 1);
1370 // In case we're scaling the image by using a width and height different
1371 // than the bitmap's size create a pattern transformation on the surface and
1372 // draw the transformed pattern.
1373 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface(surface
);
1374 wxDouble scaleX
= w
/ bw
;
1375 wxDouble scaleY
= h
/ bh
;
1376 cairo_scale(m_context
, scaleX
, scaleY
);
1378 // prepare to draw the image
1379 cairo_translate(m_context
, x
, y
);
1380 cairo_set_source(m_context
, pattern
);
1381 // use the original size here since the context is scaled already...
1382 cairo_rectangle(m_context
, 0, 0, bw
, bh
);
1383 // fill the rectangle using the pattern
1384 cairo_fill(m_context
);
1387 cairo_pattern_destroy(pattern
);
1388 cairo_surface_destroy(surface
);
1393 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1395 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1396 // start using the Cairo backend on other platforms then we may need to
1397 // fiddle with this...
1398 DrawBitmap(icon
, x
, y
, w
, h
);
1402 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1404 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::DrawText - no valid font set") );
1410 const wxCharBuffer data
= str
.utf8_str();
1413 size_t datalen
= strlen(data
);
1414 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1416 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1417 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1418 pango_layout_set_text(layout
, data
, datalen
);
1419 cairo_move_to(m_context
, x
, y
);
1420 pango_cairo_show_layout (m_context
, layout
);
1422 g_object_unref (layout
);
1424 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1425 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1426 // the position we move to by the ascent.
1427 cairo_font_extents_t fe
;
1428 cairo_font_extents(m_context
, &fe
);
1429 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1431 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1432 cairo_show_text(m_context
,buf
);
1436 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1437 wxDouble
*descent
, wxDouble
*externalLeading
) const
1439 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1447 if ( externalLeading
)
1448 *externalLeading
= 0;
1456 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1457 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1458 const wxCharBuffer data
= str
.utf8_str();
1463 pango_layout_set_text( layout
, data
, strlen(data
) );
1464 pango_layout_get_pixel_size (layout
, &w
, &h
);
1471 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1472 int baseline
= pango_layout_iter_get_baseline(iter
);
1473 pango_layout_iter_free(iter
);
1474 *descent
= h
- PANGO_PIXELS(baseline
);
1476 g_object_unref (layout
);
1478 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1482 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1483 cairo_text_extents_t te
;
1484 cairo_text_extents(m_context
, buf
, &te
);
1488 if (height
|| descent
|| externalLeading
)
1490 cairo_font_extents_t fe
;
1491 cairo_font_extents(m_context
, &fe
);
1493 // some backends have negative descents
1495 if ( fe
.descent
< 0 )
1496 fe
.descent
= -fe
.descent
;
1498 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1500 // some backends are broken re height ... (eg currently ATSUI)
1501 fe
.height
= fe
.ascent
+ fe
.descent
;
1505 *height
= fe
.height
;
1507 *descent
= fe
.descent
;
1508 if ( externalLeading
)
1509 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1514 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1517 widths
.Add(0, text
.length());
1519 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1527 void * wxCairoContext::GetNativeContext()
1532 // Cairo doesn't support bitwise logical function (a.k.a. ROP, raster output
1533 // mode). Cairo supports Porter-Duff compositing operators, but they are quite
1534 // different, although in some cases have similar names.
1535 bool wxCairoContext::SetLogicalFunction( int function
)
1537 if (m_logicalFunction
== function
)
1540 cairo_operator_t op
;
1544 case wxCOPY
: // (default) src
1545 op
= CAIRO_OPERATOR_OVER
; // (also default)
1547 case wxOR
: // src OR dst
1548 op
= CAIRO_OPERATOR_ADD
;
1550 case wxNO_OP
: // dst
1551 op
= CAIRO_OPERATOR_DEST
; // ignore the source
1554 op
= CAIRO_OPERATOR_CLEAR
;// clear dst
1557 case wxAND
: // src AND dst
1558 case wxAND_INVERT
: // (NOT src) AND dst
1559 case wxAND_REVERSE
:// src AND (NOT dst)
1560 case wxEQUIV
: // (NOT src) XOR dst
1561 case wxINVERT
: // NOT dst
1562 case wxNAND
: // (NOT src) OR (NOT dst)
1563 case wxNOR
: // (NOT src) AND (NOT dst)
1564 case wxOR_INVERT
: // (NOT src) OR dst
1565 case wxOR_REVERSE
: // src OR (NOT dst)
1567 case wxSRC_INVERT
: // NOT src
1568 //wxXOR does _not_ correspond to CAIRO_OPERATOR_XOR
1569 case wxXOR
: // src XOR dst
1574 m_logicalFunction
= function
;
1575 cairo_set_operator(m_context
, op
);
1580 //-----------------------------------------------------------------------------
1581 // wxCairoRenderer declaration
1582 //-----------------------------------------------------------------------------
1584 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1587 wxCairoRenderer() {}
1589 virtual ~wxCairoRenderer() {}
1593 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1594 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1595 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1597 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1599 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1601 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1603 virtual wxGraphicsContext
* CreateMeasuringContext();
1607 virtual wxGraphicsPath
CreatePath();
1611 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1612 wxDouble tx
=0.0, wxDouble ty
=0.0);
1615 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1617 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1619 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1620 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1621 const wxColour
&c1
, const wxColour
&c2
) ;
1623 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1624 // with radius r and color cColor
1625 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1626 const wxColour
&oColor
, const wxColour
&cColor
) ;
1629 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1631 // create a native bitmap representation
1633 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
)
1635 return wxGraphicsBitmap
;
1638 // create a subimage from a native image representation
1639 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1641 return wxGraphicsBitmap
;
1646 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1649 //-----------------------------------------------------------------------------
1650 // wxCairoRenderer implementation
1651 //-----------------------------------------------------------------------------
1653 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1655 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1656 // temporary hack to allow creating a cairo context on any platform
1657 extern wxGraphicsRenderer
* gCairoRenderer
;
1658 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1661 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1663 return &gs_cairoGraphicsRenderer
;
1667 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1669 return new wxCairoContext(this,dc
);
1672 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1674 return new wxCairoContext(this,dc
);
1677 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1680 const wxDCImpl
*impl
= dc
.GetImpl();
1681 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1683 return new wxCairoContext(this,dc
);
1689 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1691 return new wxCairoContext(this,(cairo_t
*)context
);
1695 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1698 return new wxCairoContext(this,(GdkDrawable
*)window
);
1704 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1710 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1712 return new wxCairoContext(this, window
);
1717 wxGraphicsPath
wxCairoRenderer::CreatePath()
1719 wxGraphicsPath path
;
1720 path
.SetRefData( new wxCairoPathData(this) );
1727 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1728 wxDouble tx
, wxDouble ty
)
1732 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1733 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1738 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1740 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1741 return wxNullGraphicsPen
;
1745 p
.SetRefData(new wxCairoPenData( this, pen
));
1750 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1752 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1753 return wxNullGraphicsBrush
;
1757 p
.SetRefData(new wxCairoBrushData( this, brush
));
1762 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1763 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1764 const wxColour
&c1
, const wxColour
&c2
)
1767 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1768 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1773 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1774 // with radius r and color cColor
1775 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1776 const wxColour
&oColor
, const wxColour
&cColor
)
1779 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1780 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1786 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1791 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1795 return wxNullGraphicsFont
;
1798 #endif // wxUSE_GRAPHICS_CONTEXT