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
21 #include "wx/bitmap.h"
25 #include "wx/dcclient.h"
26 #include "wx/dcmemory.h"
27 #include "wx/dcprint.h"
30 #include "wx/private/graphics.h"
31 #include "wx/rawbmp.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 const double RAD2DEG
= 180.0 / M_PI
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static inline double dmin(double a
, double b
)
49 static inline double dmax(double a
, double b
)
54 static inline double DegToRad(double deg
)
56 return (deg
* M_PI
) / 180.0;
58 static inline double RadToDeg(double deg
)
60 return (deg
* 180.0) / M_PI
;
63 //-----------------------------------------------------------------------------
64 // device context implementation
66 // more and more of the dc functionality should be implemented by calling
67 // the appropricate wxCairoContext, but we will have to do that step by step
68 // also coordinate conversions should be moved to native matrix ops
69 //-----------------------------------------------------------------------------
71 // we always stock two context states, one at entry, to be able to preserve the
72 // state we were called with, the other one after changing to HI Graphics orientation
73 // (this one is used for getting back clippings etc)
75 //-----------------------------------------------------------------------------
76 // wxGraphicsPath implementation
77 //-----------------------------------------------------------------------------
79 // TODO remove this dependency (gdiplus needs the macros)
82 #define max(a,b) (((a) > (b)) ? (a) : (b))
86 #define min(a,b) (((a) < (b)) ? (a) : (b))
92 #include "wx/fontutil.h"
93 #include "wx/gtk/dc.h"
97 #include <cairo-win32.h>
101 #include "wx/osx/private.h"
102 #include <cairo-quartz.h>
103 #include <cairo-atsui.h>
106 class WXDLLIMPEXP_CORE wxCairoPathData
: public wxGraphicsPathData
109 wxCairoPathData(wxGraphicsRenderer
* renderer
, cairo_t
* path
= NULL
);
112 virtual wxGraphicsObjectRefData
*Clone() const;
115 // These are the path primitives from which everything else can be constructed
118 // begins a new subpath at (x,y)
119 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
121 // adds a straight line from the current point to (x,y)
122 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
124 // adds a cubic Bezier curve from the current point, using two control points and an end point
125 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
128 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
129 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
131 // gets the last point of the current path, (0,0) if not yet set
132 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
135 virtual void AddPath( const wxGraphicsPathData
* path
);
137 // closes the current sub-path
138 virtual void CloseSubpath();
141 // These are convenience functions which - if not available natively will be assembled
142 // using the primitives from above
147 // appends a rectangle as a new closed subpath
148 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
149 // appends an ellipsis as a new closed subpath fitting the passed rectangle
150 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
152 // 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)
153 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
156 // returns the native path
157 virtual void * GetNativePath() const ;
159 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
160 virtual void UnGetNativePath(void *p
) const;
162 // transforms each point of this path by the matrix
163 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
165 // gets the bounding box enclosing all points (possibly including control points)
166 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
168 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxWINDING_RULE
) const;
171 cairo_t
* m_pathContext
;
174 class WXDLLIMPEXP_CORE wxCairoMatrixData
: public wxGraphicsMatrixData
177 wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
178 virtual ~wxCairoMatrixData() ;
180 virtual wxGraphicsObjectRefData
*Clone() const ;
182 // concatenates the matrix
183 virtual void Concat( const wxGraphicsMatrixData
*t
);
185 // sets the matrix to the respective values
186 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
187 wxDouble tx
=0.0, wxDouble ty
=0.0);
189 // gets the component valuess of the matrix
190 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
191 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
193 // makes this the inverse matrix
194 virtual void Invert();
196 // returns true if the elements of the transformation matrix are equal ?
197 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
199 // return true if this is the identity matrix
200 virtual bool IsIdentity() const;
206 // add the translation to this matrix
207 virtual void Translate( wxDouble dx
, wxDouble dy
);
209 // add the scale to this matrix
210 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
212 // add the rotation to this matrix (radians)
213 virtual void Rotate( wxDouble angle
);
216 // apply the transforms
219 // applies that matrix to the point
220 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
222 // applies the matrix except for translations
223 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
225 // returns the native representation
226 virtual void * GetNativeMatrix() const;
228 cairo_matrix_t m_matrix
;
231 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
234 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
239 virtual void Apply( wxGraphicsContext
* context
);
240 virtual wxDouble
GetWidth() { return m_width
; }
250 cairo_line_cap_t m_cap
;
251 cairo_line_join_t m_join
;
254 const double *m_lengths
;
255 double *m_userLengths
;
260 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
263 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
264 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
265 ~wxCairoBrushData ();
267 virtual void Apply( wxGraphicsContext
* context
);
268 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
269 const wxColour
&c1
, const wxColour
&c2
);
270 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
271 const wxColour
&oColor
, const wxColour
&cColor
);
282 cairo_pattern_t
* m_brushPattern
;
285 class wxCairoFontData
: public wxGraphicsObjectRefData
288 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
291 virtual void Apply( wxGraphicsContext
* context
);
293 const PangoFontDescription
* GetFont() const { return m_font
; }
302 cairo_font_face_t
*m_font
;
303 #elif defined(__WXGTK__)
304 PangoFontDescription
* m_font
;
306 wxCharBuffer m_fontName
;
307 cairo_font_slant_t m_slant
;
308 cairo_font_weight_t m_weight
;
312 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
314 DECLARE_NO_COPY_CLASS(wxCairoContext
)
317 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
318 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
);
319 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
);
321 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
323 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
324 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
326 virtual ~wxCairoContext();
328 virtual bool ShouldOffset() const
331 if ( !m_pen
.IsNull() )
333 penwidth
= (int)((wxCairoPenData
*)m_pen
.GetRefData())->GetWidth();
337 return ( penwidth
% 2 ) == 1;
340 virtual void Clip( const wxRegion
®ion
);
342 // clips drawings to the rect
343 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
345 // resets the clipping to original extent
346 virtual void ResetClip();
348 virtual void * GetNativeContext();
350 virtual bool SetLogicalFunction( int function
);
352 virtual void StrokePath( const wxGraphicsPath
& p
);
353 virtual void FillPath( const wxGraphicsPath
& p
, int fillStyle
= wxWINDING_RULE
);
355 virtual void Translate( wxDouble dx
, wxDouble dy
);
356 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
357 virtual void Rotate( wxDouble angle
);
359 // concatenates this transform with the current transform of this context
360 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
362 // sets the transform of this context
363 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
365 // gets the matrix of this context
366 virtual wxGraphicsMatrix
GetTransform() const;
368 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
369 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
370 virtual void PushState();
371 virtual void PopState();
373 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
374 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
375 wxDouble
*descent
, wxDouble
*externalLeading
) const;
376 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
379 void Init(cairo_t
*context
);
384 //-----------------------------------------------------------------------------
385 // wxCairoPenData implementation
386 //-----------------------------------------------------------------------------
388 wxCairoPenData::~wxCairoPenData()
390 delete[] m_userLengths
;
393 void wxCairoPenData::Init()
396 m_userLengths
= NULL
;
401 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
402 : wxGraphicsObjectRefData(renderer
)
406 m_width
= m_pen
.GetWidth();
410 m_red
= m_pen
.GetColour().Red()/255.0;
411 m_green
= m_pen
.GetColour().Green()/255.0;
412 m_blue
= m_pen
.GetColour().Blue()/255.0;
413 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
415 switch ( m_pen
.GetCap() )
418 m_cap
= CAIRO_LINE_CAP_ROUND
;
421 case wxCAP_PROJECTING
:
422 m_cap
= CAIRO_LINE_CAP_SQUARE
;
426 m_cap
= CAIRO_LINE_CAP_BUTT
;
430 m_cap
= CAIRO_LINE_CAP_BUTT
;
434 switch ( m_pen
.GetJoin() )
437 m_join
= CAIRO_LINE_JOIN_BEVEL
;
441 m_join
= CAIRO_LINE_JOIN_MITER
;
445 m_join
= CAIRO_LINE_JOIN_ROUND
;
449 m_join
= CAIRO_LINE_JOIN_MITER
;
453 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
454 const double dotted
[] =
456 dashUnit
, dashUnit
+ 2.0
458 static const double short_dashed
[] =
462 static const double dashed
[] =
466 static const double dotted_dashed
[] =
468 9.0 , 6.0 , 3.0 , 3.0
471 switch ( m_pen
.GetStyle() )
473 case wxPENSTYLE_SOLID
:
476 case wxPENSTYLE_DOT
:
477 m_count
= WXSIZEOF(dotted
);
478 m_userLengths
= new double[ m_count
] ;
479 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
480 m_lengths
= m_userLengths
;
483 case wxPENSTYLE_LONG_DASH
:
485 m_count
= WXSIZEOF(dashed
);
488 case wxPENSTYLE_SHORT_DASH
:
489 m_lengths
= short_dashed
;
490 m_count
= WXSIZEOF(short_dashed
);
493 case wxPENSTYLE_DOT_DASH
:
494 m_lengths
= dotted_dashed
;
495 m_count
= WXSIZEOF(dotted_dashed
);
498 case wxPENSTYLE_USER_DASH
:
501 m_count
= m_pen
.GetDashes( &wxdashes
) ;
502 if ((wxdashes
!= NULL
) && (m_count
> 0))
504 m_userLengths
= new double[m_count
] ;
505 for ( int i
= 0 ; i
< m_count
; ++i
)
507 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
509 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
510 m_userLengths
[i
] = dashUnit
+ 2.0 ;
511 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
512 m_userLengths
[i
] = dashUnit
;
515 m_lengths
= m_userLengths
;
518 case wxPENSTYLE_STIPPLE
:
521 wxBitmap* bmp = pen.GetStipple();
522 if ( bmp && bmp->Ok() )
524 wxDELETE( m_penImage );
525 wxDELETE( m_penBrush );
526 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
527 m_penBrush = new TextureBrush(m_penImage);
528 m_pen->SetBrush( m_penBrush );
534 if ( m_pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
535 && m_pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
538 wxDELETE( m_penBrush );
539 HatchStyle style = HatchStyleHorizontal;
540 switch( pen.GetStyle() )
542 case wxPENSTYLE_BDIAGONAL_HATCH :
543 style = HatchStyleBackwardDiagonal;
545 case wxPENSTYLE_CROSSDIAG_HATCH :
546 style = HatchStyleDiagonalCross;
548 case wxPENSTYLE_FDIAGONAL_HATCH :
549 style = HatchStyleForwardDiagonal;
551 case wxPENSTYLE_CROSS_HATCH :
552 style = HatchStyleCross;
554 case wxPENSTYLE_HORIZONTAL_HATCH :
555 style = HatchStyleHorizontal;
557 case wxPENSTYLE_VERTICAL_HATCH :
558 style = HatchStyleVertical;
562 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
563 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
564 m_pen->SetBrush( m_penBrush )
571 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
573 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
574 cairo_set_line_width(ctext
,m_width
);
575 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
576 cairo_set_line_cap(ctext
,m_cap
);
577 cairo_set_line_join(ctext
,m_join
);
578 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
581 //-----------------------------------------------------------------------------
582 // wxCairoBrushData implementation
583 //-----------------------------------------------------------------------------
585 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
586 : wxGraphicsObjectRefData( renderer
)
591 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
592 : wxGraphicsObjectRefData(renderer
)
596 m_red
= brush
.GetColour().Red()/255.0;
597 m_green
= brush
.GetColour().Green()/255.0;
598 m_blue
= brush
.GetColour().Blue()/255.0;
599 m_alpha
= brush
.GetColour().Alpha()/255.0;
601 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
603 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
604 brush.GetColour().Green() , brush.GetColour().Blue() ) );
606 else if ( brush.IsHatch() )
608 HatchStyle style = HatchStyleHorizontal;
609 switch( brush.GetStyle() )
611 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
612 style = HatchStyleBackwardDiagonal;
614 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
615 style = HatchStyleDiagonalCross;
617 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
618 style = HatchStyleForwardDiagonal;
620 case wxBRUSHSTYLE_CROSS_HATCH :
621 style = HatchStyleCross;
623 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
624 style = HatchStyleHorizontal;
626 case wxBRUSHSTYLE_VERTICAL_HATCH :
627 style = HatchStyleVertical;
631 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
632 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
636 wxBitmap* bmp = brush.GetStipple();
637 if ( bmp && bmp->Ok() )
639 wxDELETE( m_brushImage );
640 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
641 m_brush = new TextureBrush(m_brushImage);
647 wxCairoBrushData::~wxCairoBrushData ()
650 cairo_pattern_destroy(m_brushPattern
);
653 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
655 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
656 if ( m_brushPattern
)
658 cairo_set_source(ctext
,m_brushPattern
);
662 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
666 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
667 const wxColour
&c1
, const wxColour
&c2
)
669 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
670 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
671 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
672 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
673 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
674 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
677 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
678 const wxColour
&oColor
, const wxColour
&cColor
)
680 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
681 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
682 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
683 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
684 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
685 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
688 void wxCairoBrushData::Init()
690 m_brushPattern
= NULL
;
693 //-----------------------------------------------------------------------------
694 // wxCairoFontData implementation
695 //-----------------------------------------------------------------------------
697 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
698 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
700 m_red
= col
.Red()/255.0;
701 m_green
= col
.Green()/255.0;
702 m_blue
= col
.Blue()/255.0;
703 m_alpha
= col
.Alpha()/255.0;
704 m_size
= font
.GetPointSize();
707 m_font
= cairo_atsui_font_face_create_for_atsu_font_id( font
.MacGetATSUFontID() );
708 #elif defined(__WXGTK__)
709 m_font
= pango_font_description_copy( font
.GetNativeFontInfo()->description
);
711 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
712 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
713 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
717 wxCairoFontData::~wxCairoFontData()
720 cairo_font_face_destroy( m_font
);
721 #elif defined(__WXGTK__)
722 pango_font_description_free( m_font
);
727 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
729 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
730 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
732 // the rest is done using Pango layouts
733 #elif defined(__WXMAC__)
734 cairo_set_font_face(ctext
, m_font
);
735 cairo_set_font_size(ctext
, m_size
);
737 cairo_select_font_face(ctext
, m_fontName
, m_slant
, m_weights
);
738 cairo_set_font_size(ctext
, m_size
);
742 //-----------------------------------------------------------------------------
743 // wxCairoPathData implementation
744 //-----------------------------------------------------------------------------
746 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
747 : wxGraphicsPathData(renderer
)
751 m_pathContext
= pathcontext
;
755 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
756 m_pathContext
= cairo_create(surface
);
757 cairo_surface_destroy (surface
);
761 wxCairoPathData::~wxCairoPathData()
763 cairo_destroy(m_pathContext
);
766 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
768 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
769 cairo_t
* pathcontext
= cairo_create(surface
);
770 cairo_surface_destroy (surface
);
772 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
773 cairo_append_path(pathcontext
, path
);
774 cairo_path_destroy(path
);
775 return new wxCairoPathData( GetRenderer() ,pathcontext
);
779 void* wxCairoPathData::GetNativePath() const
781 return cairo_copy_path(m_pathContext
) ;
784 void wxCairoPathData::UnGetNativePath(void *p
) const
786 cairo_path_destroy((cairo_path_t
*)p
);
793 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
795 cairo_move_to(m_pathContext
,x
,y
);
798 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
800 cairo_line_to(m_pathContext
,x
,y
);
803 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
805 cairo_path_t
* p
= (cairo_path_t
*)path
->GetNativePath();
806 cairo_append_path(m_pathContext
, p
);
810 void wxCairoPathData::CloseSubpath()
812 cairo_close_path(m_pathContext
);
815 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
817 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
820 // gets the last point of the current path, (0,0) if not yet set
821 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
824 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
831 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
833 // as clockwise means positive in our system (y pointing downwards)
834 // TODO make this interpretation dependent of the
836 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
837 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
839 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
842 // transforms each point of this path by the matrix
843 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
845 // as we don't have a true path object, we have to apply the inverse
846 // matrix to the context
847 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
848 cairo_matrix_invert( &m
);
849 cairo_transform(m_pathContext
,&m
);
852 // gets the bounding box enclosing all points (possibly including control points)
853 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
857 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
881 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, int WXUNUSED(fillStyle
) ) const
883 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
886 //-----------------------------------------------------------------------------
887 // wxCairoMatrixData implementation
888 //-----------------------------------------------------------------------------
890 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
891 : wxGraphicsMatrixData(renderer
)
897 wxCairoMatrixData::~wxCairoMatrixData()
902 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
904 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
907 // concatenates the matrix
908 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
910 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
913 // sets the matrix to the respective values
914 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
915 wxDouble tx
, wxDouble ty
)
917 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
920 // gets the component valuess of the matrix
921 void wxCairoMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
922 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
924 if (a
) *a
= m_matrix
.xx
;
925 if (b
) *b
= m_matrix
.yx
;
926 if (c
) *c
= m_matrix
.xy
;
927 if (d
) *d
= m_matrix
.yy
;
928 if (tx
) *tx
= m_matrix
.x0
;
929 if (ty
) *ty
= m_matrix
.y0
;
932 // makes this the inverse matrix
933 void wxCairoMatrixData::Invert()
935 cairo_matrix_invert( &m_matrix
);
938 // returns true if the elements of the transformation matrix are equal ?
939 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
941 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
943 m_matrix
.xx
== tm
->xx
&&
944 m_matrix
.yx
== tm
->yx
&&
945 m_matrix
.xy
== tm
->xy
&&
946 m_matrix
.yy
== tm
->yy
&&
947 m_matrix
.x0
== tm
->x0
&&
948 m_matrix
.y0
== tm
->y0
) ;
951 // return true if this is the identity matrix
952 bool wxCairoMatrixData::IsIdentity() const
954 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
955 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
962 // add the translation to this matrix
963 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
965 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
968 // add the scale to this matrix
969 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
971 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
974 // add the rotation to this matrix (radians)
975 void wxCairoMatrixData::Rotate( wxDouble angle
)
977 cairo_matrix_rotate( &m_matrix
, angle
) ;
981 // apply the transforms
984 // applies that matrix to the point
985 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
987 double lx
= *x
, ly
= *y
;
988 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
993 // applies the matrix except for translations
994 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
996 double lx
= *dx
, ly
= *dy
;
997 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
1002 // returns the native representation
1003 void * wxCairoMatrixData::GetNativeMatrix() const
1005 return (void*) &m_matrix
;
1008 //-----------------------------------------------------------------------------
1009 // wxCairoContext implementation
1010 //-----------------------------------------------------------------------------
1012 class wxCairoOffsetHelper
1015 wxCairoOffsetHelper( cairo_t
* ctx
, bool offset
)
1020 cairo_translate( m_ctx
, 0.5, 0.5 );
1022 ~wxCairoOffsetHelper( )
1025 cairo_translate( m_ctx
, -0.5, -0.5 );
1032 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxPrinterDC
& dc
)
1033 : wxGraphicsContext(renderer
)
1036 const wxDCImpl
*impl
= dc
.GetImpl();
1037 Init( (cairo_t
*) impl
->GetCairoContext() );
1039 wxPoint org
= dc
.GetDeviceOrigin();
1040 cairo_translate( m_context
, org
.x
, org
.y
);
1043 dc
.GetUserScale( &sx
, &sy
);
1044 cairo_scale( m_context
, sx
, sy
);
1046 org
= dc
.GetLogicalOrigin();
1047 cairo_translate( m_context
, -org
.x
, -org
.y
);
1051 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
1052 : wxGraphicsContext(renderer
)
1055 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1056 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1059 wxGraphicsMatrix matrix
= CreateMatrix();
1061 wxPoint org
= dc
.GetDeviceOrigin();
1062 matrix
.Translate( org
.x
, org
.y
);
1064 org
= dc
.GetLogicalOrigin();
1065 matrix
.Translate( -org
.x
, -org
.y
);
1068 dc
.GetUserScale( &sx
, &sy
);
1069 matrix
.Scale( sx
, sy
);
1071 ConcatTransform( matrix
);
1077 dc
.GetSize( &width
, &height
);
1078 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1079 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1080 Init( cairo_create( surface
) );
1081 cairo_surface_destroy( surface
);
1085 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxMemoryDC
& dc
)
1086 : wxGraphicsContext(renderer
)
1089 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
1090 Init( gdk_cairo_create( impldc
->GetGDKWindow() ) );
1093 wxGraphicsMatrix matrix
= CreateMatrix();
1095 wxPoint org
= dc
.GetDeviceOrigin();
1096 matrix
.Translate( org
.x
, org
.y
);
1098 org
= dc
.GetLogicalOrigin();
1099 matrix
.Translate( -org
.x
, -org
.y
);
1102 dc
.GetUserScale( &sx
, &sy
);
1103 matrix
.Scale( sx
, sy
);
1105 ConcatTransform( matrix
);
1111 dc
.GetSize( &width
, &height
);
1112 CGContextRef cgcontext
= (CGContextRef
)dc
.GetWindow()->MacGetCGContextRef();
1113 cairo_surface_t
* surface
= cairo_quartz_surface_create_for_cg_context(cgcontext
, width
, height
);
1114 Init( cairo_create( surface
) );
1115 cairo_surface_destroy( surface
);
1120 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1121 : wxGraphicsContext(renderer
)
1123 Init( gdk_cairo_create( drawable
) );
1127 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1128 : wxGraphicsContext(renderer
)
1133 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1134 : wxGraphicsContext(renderer
)
1137 // something along these lines (copied from dcclient)
1139 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1140 // code should still be able to create wxClientDCs for them, so we will
1141 // use the parent window here then.
1142 if (window
->m_wxwindow
== NULL
)
1144 window
= window
->GetParent();
1147 wxASSERT_MSG( window
->m_wxwindow
, wxT("wxCairoContext needs a widget") );
1149 Init(gdk_cairo_create(window
->GTKGetDrawingWindow()));
1153 wxCairoContext::~wxCairoContext()
1159 cairo_destroy(m_context
);
1163 void wxCairoContext::Init(cairo_t
*context
)
1165 m_context
= context
;
1171 void wxCairoContext::Clip( const wxRegion
& region
)
1173 // Create a path with all the rectangles in the region
1174 wxGraphicsPath path
= GetRenderer()->CreatePath();
1175 wxRegionIterator
ri(region
);
1178 path
.AddRectangle(ri
.GetX(), ri
.GetY(), ri
.GetW(), ri
.GetH());
1182 // Put it in the context
1183 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1184 cairo_append_path(m_context
, cp
);
1186 // clip to that path
1187 cairo_clip(m_context
);
1188 path
.UnGetNativePath(cp
);
1191 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1193 // Create a path with this rectangle
1194 wxGraphicsPath path
= GetRenderer()->CreatePath();
1195 path
.AddRectangle(x
,y
,w
,h
);
1197 // Put it in the context
1198 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1199 cairo_append_path(m_context
, cp
);
1201 // clip to that path
1202 cairo_clip(m_context
);
1203 path
.UnGetNativePath(cp
);
1206 void wxCairoContext::ResetClip()
1208 cairo_reset_clip(m_context
);
1212 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1214 if ( !m_pen
.IsNull() )
1216 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1217 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1218 cairo_append_path(m_context
,cp
);
1219 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1220 cairo_stroke(m_context
);
1221 path
.UnGetNativePath(cp
);
1225 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, int fillStyle
)
1227 if ( !m_brush
.IsNull() )
1229 wxCairoOffsetHelper
helper( m_context
, ShouldOffset() ) ;
1230 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1231 cairo_append_path(m_context
,cp
);
1232 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1233 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1234 cairo_fill(m_context
);
1235 path
.UnGetNativePath(cp
);
1239 void wxCairoContext::Rotate( wxDouble angle
)
1241 cairo_rotate(m_context
,angle
);
1244 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1246 cairo_translate(m_context
,dx
,dy
);
1249 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1251 cairo_scale(m_context
,xScale
,yScale
);
1254 // concatenates this transform with the current transform of this context
1255 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1257 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1260 // sets the transform of this context
1261 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1263 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1266 // gets the matrix of this context
1267 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1269 wxGraphicsMatrix matrix
= CreateMatrix();
1270 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1276 void wxCairoContext::PushState()
1278 cairo_save(m_context
);
1281 void wxCairoContext::PopState()
1283 cairo_restore(m_context
);
1286 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1288 wxCHECK_RET( bmp
.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1290 cairo_surface_t
* surface
;
1291 int bw
= bmp
.GetWidth();
1292 int bh
= bmp
.GetHeight();
1293 wxBitmap bmpSource
= bmp
; // we need a non-const instance
1294 unsigned char* buffer
= new unsigned char[bw
*bh
*4];
1295 wxUint32
* data
= (wxUint32
*)buffer
;
1297 // Create a surface object and copy the bitmap pixel data to it. if the
1298 // image has alpha (or a mask represented as alpha) then we'll use a
1299 // different format and iterator than if it doesn't...
1300 if (bmpSource
.HasAlpha() || bmpSource
.GetMask())
1302 surface
= cairo_image_surface_create_for_data(
1303 buffer
, CAIRO_FORMAT_ARGB32
, bw
, bh
, bw
*4);
1304 wxAlphaPixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1305 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1307 wxAlphaPixelData::Iterator
p(pixData
);
1308 for (int y
=0; y
<bh
; y
++)
1310 wxAlphaPixelData::Iterator rowStart
= p
;
1311 for (int x
=0; x
<bw
; x
++)
1313 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1314 // with alpha in the upper 8 bits, then red, then green, then
1315 // blue. The 32-bit quantities are stored native-endian.
1316 // Pre-multiplied alpha is used.
1317 unsigned char alpha
= p
.Alpha();
1321 *data
= ( alpha
<< 24
1322 | (p
.Red() * alpha
/255) << 16
1323 | (p
.Green() * alpha
/255) << 8
1324 | (p
.Blue() * alpha
/255) );
1329 p
.OffsetY(pixData
, 1);
1334 surface
= cairo_image_surface_create_for_data(
1335 buffer
, CAIRO_FORMAT_RGB24
, bw
, bh
, bw
*4);
1336 wxNativePixelData
pixData(bmpSource
, wxPoint(0,0), wxSize(bw
, bh
));
1337 wxCHECK_RET( pixData
, wxT("Failed to gain raw access to bitmap data."));
1339 wxNativePixelData::Iterator
p(pixData
);
1340 for (int y
=0; y
<bh
; y
++)
1342 wxNativePixelData::Iterator rowStart
= p
;
1343 for (int x
=0; x
<bw
; x
++)
1345 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1346 // the upper 8 bits unused. Red, Green, and Blue are stored in
1347 // the remaining 24 bits in that order. The 32-bit quantities
1348 // are stored native-endian.
1349 *data
= ( p
.Red() << 16 | p
.Green() << 8 | p
.Blue() );
1354 p
.OffsetY(pixData
, 1);
1361 // In case we're scaling the image by using a width and height different
1362 // than the bitmap's size create a pattern transformation on the surface and
1363 // draw the transformed pattern.
1364 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface(surface
);
1365 wxDouble scaleX
= w
/ bw
;
1366 wxDouble scaleY
= h
/ bh
;
1367 cairo_scale(m_context
, scaleX
, scaleY
);
1369 // prepare to draw the image
1370 cairo_translate(m_context
, x
, y
);
1371 cairo_set_source(m_context
, pattern
);
1372 // use the original size here since the context is scaled already...
1373 cairo_rectangle(m_context
, 0, 0, bw
, bh
);
1374 // fill the rectangle using the pattern
1375 cairo_fill(m_context
);
1378 cairo_pattern_destroy(pattern
);
1379 cairo_surface_destroy(surface
);
1384 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1386 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1387 // start using the Cairo backend on other platforms then we may need to
1388 // fiddle with this...
1389 DrawBitmap(icon
, x
, y
, w
, h
);
1393 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1395 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::DrawText - no valid font set") );
1401 const wxCharBuffer data
= str
.utf8_str();
1404 size_t datalen
= strlen(data
);
1405 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1407 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1408 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1409 pango_layout_set_text(layout
, data
, datalen
);
1410 cairo_move_to(m_context
, x
, y
);
1411 pango_cairo_show_layout (m_context
, layout
);
1413 g_object_unref (layout
);
1415 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1416 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1417 // the position we move to by the ascent.
1418 cairo_font_extents_t fe
;
1419 cairo_font_extents(m_context
, &fe
);
1420 cairo_move_to(m_context
, x
, y
+fe
.ascent
);
1422 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1423 cairo_show_text(m_context
,buf
);
1427 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1428 wxDouble
*descent
, wxDouble
*externalLeading
) const
1430 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1438 if ( externalLeading
)
1439 *externalLeading
= 0;
1447 PangoLayout
*layout
= pango_cairo_create_layout (m_context
);
1448 pango_layout_set_font_description( layout
, ((wxCairoFontData
*)m_font
.GetRefData())->GetFont());
1449 const wxCharBuffer data
= str
.utf8_str();
1454 pango_layout_set_text( layout
, data
, strlen(data
) );
1455 pango_layout_get_pixel_size (layout
, &w
, &h
);
1462 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
1463 int baseline
= pango_layout_iter_get_baseline(iter
);
1464 pango_layout_iter_free(iter
);
1465 *descent
= h
- PANGO_PIXELS(baseline
);
1467 g_object_unref (layout
);
1469 ((wxCairoFontData
*)m_font
.GetRefData())->Apply((wxCairoContext
*)this);
1473 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1474 cairo_text_extents_t te
;
1475 cairo_text_extents(m_context
, buf
, &te
);
1479 if (height
|| descent
|| externalLeading
)
1481 cairo_font_extents_t fe
;
1482 cairo_font_extents(m_context
, &fe
);
1484 // some backends have negative descents
1486 if ( fe
.descent
< 0 )
1487 fe
.descent
= -fe
.descent
;
1489 if ( fe
.height
< (fe
.ascent
+ fe
.descent
) )
1491 // some backends are broken re height ... (eg currently ATSUI)
1492 fe
.height
= fe
.ascent
+ fe
.descent
;
1496 *height
= fe
.height
;
1498 *descent
= fe
.descent
;
1499 if ( externalLeading
)
1500 *externalLeading
= wxMax(0, fe
.height
- (fe
.ascent
+ fe
.descent
));
1505 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1508 widths
.Add(0, text
.length());
1510 wxCHECK_RET( !m_font
.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1518 void * wxCairoContext::GetNativeContext()
1523 // Cairo doesn't support bitwise logical function (a.k.a. ROP, raster output
1524 // mode). Cairo supports Porter-Duff compositing operators, but they are quite
1525 // different, although in some cases have similar names.
1526 bool wxCairoContext::SetLogicalFunction( int function
)
1528 if (m_logicalFunction
== function
)
1531 cairo_operator_t op
;
1535 case wxCOPY
: // (default) src
1536 op
= CAIRO_OPERATOR_OVER
; // (also default)
1538 case wxOR
: // src OR dst
1539 op
= CAIRO_OPERATOR_ADD
;
1541 case wxNO_OP
: // dst
1542 op
= CAIRO_OPERATOR_DEST
; // ignore the source
1545 op
= CAIRO_OPERATOR_CLEAR
;// clear dst
1548 case wxAND
: // src AND dst
1549 case wxAND_INVERT
: // (NOT src) AND dst
1550 case wxAND_REVERSE
:// src AND (NOT dst)
1551 case wxEQUIV
: // (NOT src) XOR dst
1552 case wxINVERT
: // NOT dst
1553 case wxNAND
: // (NOT src) OR (NOT dst)
1554 case wxNOR
: // (NOT src) AND (NOT dst)
1555 case wxOR_INVERT
: // (NOT src) OR dst
1556 case wxOR_REVERSE
: // src OR (NOT dst)
1558 case wxSRC_INVERT
: // NOT src
1559 //wxXOR does _not_ correspond to CAIRO_OPERATOR_XOR
1560 case wxXOR
: // src XOR dst
1565 m_logicalFunction
= function
;
1566 cairo_set_operator(m_context
, op
);
1571 //-----------------------------------------------------------------------------
1572 // wxCairoRenderer declaration
1573 //-----------------------------------------------------------------------------
1575 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1578 wxCairoRenderer() {}
1580 virtual ~wxCairoRenderer() {}
1584 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1585 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1586 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1588 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1590 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1592 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1594 virtual wxGraphicsContext
* CreateMeasuringContext();
1598 virtual wxGraphicsPath
CreatePath();
1602 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1603 wxDouble tx
=0.0, wxDouble ty
=0.0);
1606 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1608 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1610 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1611 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1612 const wxColour
&c1
, const wxColour
&c2
) ;
1614 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1615 // with radius r and color cColor
1616 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1617 const wxColour
&oColor
, const wxColour
&cColor
) ;
1620 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1622 // create a native bitmap representation
1624 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
)
1626 return wxGraphicsBitmap
;
1629 // create a subimage from a native image representation
1630 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1632 return wxGraphicsBitmap
;
1637 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1640 //-----------------------------------------------------------------------------
1641 // wxCairoRenderer implementation
1642 //-----------------------------------------------------------------------------
1644 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1646 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1647 // temporary hack to allow creating a cairo context on any platform
1648 extern wxGraphicsRenderer
* gCairoRenderer
;
1649 wxGraphicsRenderer
* gCairoRenderer
= &gs_cairoGraphicsRenderer
;
1652 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1654 return &gs_cairoGraphicsRenderer
;
1658 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1660 return new wxCairoContext(this,dc
);
1663 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1665 return new wxCairoContext(this,dc
);
1668 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxPrinterDC
& dc
)
1671 const wxDCImpl
*impl
= dc
.GetImpl();
1672 cairo_t
* context
= (cairo_t
*) impl
->GetCairoContext();
1674 return new wxCairoContext(this,dc
);
1680 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1682 return new wxCairoContext(this,(cairo_t
*)context
);
1686 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1689 return new wxCairoContext(this,(GdkDrawable
*)window
);
1695 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1701 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1703 return new wxCairoContext(this, window
);
1708 wxGraphicsPath
wxCairoRenderer::CreatePath()
1710 wxGraphicsPath path
;
1711 path
.SetRefData( new wxCairoPathData(this) );
1718 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1719 wxDouble tx
, wxDouble ty
)
1723 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1724 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1729 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1731 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1732 return wxNullGraphicsPen
;
1736 p
.SetRefData(new wxCairoPenData( this, pen
));
1741 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1743 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1744 return wxNullGraphicsBrush
;
1748 p
.SetRefData(new wxCairoBrushData( this, brush
));
1753 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1754 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1755 const wxColour
&c1
, const wxColour
&c2
)
1758 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1759 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1764 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1765 // with radius r and color cColor
1766 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1767 const wxColour
&oColor
, const wxColour
&cColor
)
1770 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1771 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1777 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1782 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1786 return wxNullGraphicsFont
;
1789 #endif // wxUSE_GRAPHICS_CONTEXT