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"
22 #include "wx/window.h"
25 #include "wx/dialog.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
31 #include "wx/dcprint.h"
32 #include "wx/module.h"
36 #include "wx/gtk/win_gtk.h"
39 #include "wx/graphics.h"
41 #if wxUSE_GRAPHICS_CONTEXT
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 const double RAD2DEG
= 180.0 / M_PI
;
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 static inline double dmin(double a
, double b
)
61 static inline double dmax(double a
, double b
)
66 static inline double DegToRad(double deg
)
68 return (deg
* M_PI
) / 180.0;
70 static inline double RadToDeg(double deg
)
72 return (deg
* 180.0) / M_PI
;
75 //-----------------------------------------------------------------------------
76 // device context implementation
78 // more and more of the dc functionality should be implemented by calling
79 // the appropricate wxCairoContext, but we will have to do that step by step
80 // also coordinate conversions should be moved to native matrix ops
81 //-----------------------------------------------------------------------------
83 // we always stock two context states, one at entry, to be able to preserve the
84 // state we were called with, the other one after changing to HI Graphics orientation
85 // (this one is used for getting back clippings etc)
87 //-----------------------------------------------------------------------------
88 // wxGraphicsPath implementation
89 //-----------------------------------------------------------------------------
91 // TODO remove this dependency (gdiplus needs the macros)
94 #define max(a,b) (((a) > (b)) ? (a) : (b))
98 #define min(a,b) (((a) < (b)) ? (a) : (b))
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 // makes this the inverse matrix
190 virtual void Invert();
192 // returns true if the elements of the transformation matrix are equal ?
193 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
195 // return true if this is the identity matrix
196 virtual bool IsIdentity() const;
202 // add the translation to this matrix
203 virtual void Translate( wxDouble dx
, wxDouble dy
);
205 // add the scale to this matrix
206 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
208 // add the rotation to this matrix (radians)
209 virtual void Rotate( wxDouble angle
);
212 // apply the transforms
215 // applies that matrix to the point
216 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
218 // applies the matrix except for translations
219 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
221 // returns the native representation
222 virtual void * GetNativeMatrix() const;
224 cairo_matrix_t m_matrix
;
227 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
230 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
235 virtual void Apply( wxGraphicsContext
* context
);
236 virtual wxDouble
GetWidth() { return m_width
; }
246 cairo_line_cap_t m_cap
;
247 cairo_line_join_t m_join
;
250 const double *m_lengths
;
251 double *m_userLengths
;
256 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
259 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
260 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
261 ~wxCairoBrushData ();
263 virtual void Apply( wxGraphicsContext
* context
);
264 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
265 const wxColour
&c1
, const wxColour
&c2
);
266 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
267 const wxColour
&oColor
, const wxColour
&cColor
);
278 cairo_pattern_t
* m_brushPattern
;
281 class wxCairoFontData
: public wxGraphicsObjectRefData
284 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
287 virtual void Apply( wxGraphicsContext
* context
);
289 wxCharBuffer m_fontName
;
291 cairo_font_slant_t m_slant
;
292 cairo_font_weight_t m_weight
;
299 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
301 DECLARE_NO_COPY_CLASS(wxCairoContext
)
304 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
306 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
308 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
309 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
311 virtual ~wxCairoContext();
313 virtual void Clip( const wxRegion
®ion
);
315 // clips drawings to the rect
316 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
318 // resets the clipping to original extent
319 virtual void ResetClip();
321 virtual void * GetNativeContext();
323 virtual void StrokePath( const wxGraphicsPath
& p
);
324 virtual void FillPath( const wxGraphicsPath
& p
, int fillStyle
= wxWINDING_RULE
);
326 virtual void Translate( wxDouble dx
, wxDouble dy
);
327 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
328 virtual void Rotate( wxDouble angle
);
330 // concatenates this transform with the current transform of this context
331 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
333 // sets the transform of this context
334 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
336 // gets the matrix of this context
337 virtual wxGraphicsMatrix
GetTransform() const;
339 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
340 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
341 virtual void PushState();
342 virtual void PopState();
344 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
345 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
346 wxDouble
*descent
, wxDouble
*externalLeading
) const;
347 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
353 //-----------------------------------------------------------------------------
354 // wxCairoPenData implementation
355 //-----------------------------------------------------------------------------
357 wxCairoPenData::~wxCairoPenData()
359 delete[] m_userLengths
;
362 void wxCairoPenData::Init()
365 m_userLengths
= NULL
;
370 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
371 : wxGraphicsObjectRefData(renderer
)
375 m_width
= m_pen
.GetWidth();
379 m_red
= m_pen
.GetColour().Red()/255.0;
380 m_green
= m_pen
.GetColour().Green()/255.0;
381 m_blue
= m_pen
.GetColour().Blue()/255.0;
382 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
384 switch ( m_pen
.GetCap() )
387 m_cap
= CAIRO_LINE_CAP_ROUND
;
390 case wxCAP_PROJECTING
:
391 m_cap
= CAIRO_LINE_CAP_SQUARE
;
395 m_cap
= CAIRO_LINE_CAP_BUTT
;
399 m_cap
= CAIRO_LINE_CAP_BUTT
;
403 switch ( m_pen
.GetJoin() )
406 m_join
= CAIRO_LINE_JOIN_BEVEL
;
410 m_join
= CAIRO_LINE_JOIN_MITER
;
414 m_join
= CAIRO_LINE_JOIN_ROUND
;
418 m_join
= CAIRO_LINE_JOIN_MITER
;
422 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
423 const double dotted
[] =
425 dashUnit
, dashUnit
+ 2.0
427 static const double short_dashed
[] =
431 static const double dashed
[] =
435 static const double dotted_dashed
[] =
437 9.0 , 6.0 , 3.0 , 3.0
440 switch ( m_pen
.GetStyle() )
446 m_count
= WXSIZEOF(dotted
);
447 m_userLengths
= new double[ m_count
] ;
448 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
449 m_lengths
= m_userLengths
;
454 m_count
= WXSIZEOF(dashed
);
459 m_count
= WXSIZEOF(short_dashed
);
464 m_count
= WXSIZEOF(dotted_dashed
);
470 m_count
= m_pen
.GetDashes( &wxdashes
) ;
471 if ((wxdashes
!= NULL
) && (m_count
> 0))
473 m_userLengths
= new double[m_count
] ;
474 for ( int i
= 0 ; i
< m_count
; ++i
)
476 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
478 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
479 m_userLengths
[i
] = dashUnit
+ 2.0 ;
480 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
481 m_userLengths
[i
] = dashUnit
;
484 m_lengths
= m_userLengths
;
490 wxBitmap* bmp = pen.GetStipple();
491 if ( bmp && bmp->Ok() )
493 wxDELETE( m_penImage );
494 wxDELETE( m_penBrush );
495 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
496 m_penBrush = new TextureBrush(m_penImage);
497 m_pen->SetBrush( m_penBrush );
503 if ( m_pen
.GetStyle() >= wxFIRST_HATCH
&& m_pen
.GetStyle() <= wxLAST_HATCH
)
506 wxDELETE( m_penBrush );
507 HatchStyle style = HatchStyleHorizontal;
508 switch( pen.GetStyle() )
510 case wxBDIAGONAL_HATCH :
511 style = HatchStyleBackwardDiagonal;
513 case wxCROSSDIAG_HATCH :
514 style = HatchStyleDiagonalCross;
516 case wxFDIAGONAL_HATCH :
517 style = HatchStyleForwardDiagonal;
520 style = HatchStyleCross;
522 case wxHORIZONTAL_HATCH :
523 style = HatchStyleHorizontal;
525 case wxVERTICAL_HATCH :
526 style = HatchStyleVertical;
530 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
531 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
532 m_pen->SetBrush( m_penBrush )
539 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
541 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
542 cairo_set_line_width(ctext
,m_width
);
543 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
544 cairo_set_line_cap(ctext
,m_cap
);
545 cairo_set_line_join(ctext
,m_join
);
546 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
549 //-----------------------------------------------------------------------------
550 // wxCairoBrushData implementation
551 //-----------------------------------------------------------------------------
553 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
554 : wxGraphicsObjectRefData( renderer
)
559 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
560 : wxGraphicsObjectRefData(renderer
)
564 m_red
= brush
.GetColour().Red()/255.0;
565 m_green
= brush
.GetColour().Green()/255.0;
566 m_blue
= brush
.GetColour().Blue()/255.0;
567 m_alpha
= brush
.GetColour().Alpha()/255.0;
569 if ( brush.GetStyle() == wxSOLID)
571 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
572 brush.GetColour().Green() , brush.GetColour().Blue() ) );
574 else if ( brush.IsHatch() )
576 HatchStyle style = HatchStyleHorizontal;
577 switch( brush.GetStyle() )
579 case wxBDIAGONAL_HATCH :
580 style = HatchStyleBackwardDiagonal;
582 case wxCROSSDIAG_HATCH :
583 style = HatchStyleDiagonalCross;
585 case wxFDIAGONAL_HATCH :
586 style = HatchStyleForwardDiagonal;
589 style = HatchStyleCross;
591 case wxHORIZONTAL_HATCH :
592 style = HatchStyleHorizontal;
594 case wxVERTICAL_HATCH :
595 style = HatchStyleVertical;
599 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
600 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
604 wxBitmap* bmp = brush.GetStipple();
605 if ( bmp && bmp->Ok() )
607 wxDELETE( m_brushImage );
608 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
609 m_brush = new TextureBrush(m_brushImage);
615 wxCairoBrushData::~wxCairoBrushData ()
618 cairo_pattern_destroy(m_brushPattern
);
621 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
623 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
624 if ( m_brushPattern
)
626 cairo_set_source(ctext
,m_brushPattern
);
630 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
634 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
635 const wxColour
&c1
, const wxColour
&c2
)
637 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
638 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
639 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
640 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
641 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
642 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
645 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
646 const wxColour
&oColor
, const wxColour
&cColor
)
648 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
649 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
650 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
651 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
652 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
653 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
656 void wxCairoBrushData::Init()
658 m_brushPattern
= NULL
;
661 //-----------------------------------------------------------------------------
662 // wxCairoFontData implementation
663 //-----------------------------------------------------------------------------
665 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
666 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
668 m_red
= col
.Red()/255.0;
669 m_green
= col
.Green()/255.0;
670 m_blue
= col
.Blue()/255.0;
671 m_alpha
= col
.Alpha()/255.0;
673 m_size
= font
.GetPointSize();
674 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
675 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
676 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
679 wxCairoFontData::~wxCairoFontData()
683 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
685 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
686 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
687 cairo_select_font_face(ctext
,m_fontName
,m_slant
,m_weight
);
688 cairo_set_font_size(ctext
,m_size
);
693 //-----------------------------------------------------------------------------
694 // wxCairoPathData implementation
695 //-----------------------------------------------------------------------------
697 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
698 : wxGraphicsPathData(renderer
)
702 m_pathContext
= pathcontext
;
706 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
707 m_pathContext
= cairo_create(surface
);
708 cairo_surface_destroy (surface
);
712 wxCairoPathData::~wxCairoPathData()
714 cairo_destroy(m_pathContext
);
717 wxGraphicsObjectRefData
*wxCairoPathData::Clone() const
719 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
720 cairo_t
* pathcontext
= cairo_create(surface
);
721 cairo_surface_destroy (surface
);
723 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
724 cairo_append_path(pathcontext
, path
);
725 cairo_path_destroy(path
);
726 return new wxCairoPathData( GetRenderer() ,pathcontext
);
730 void* wxCairoPathData::GetNativePath() const
732 return cairo_copy_path(m_pathContext
) ;
735 void wxCairoPathData::UnGetNativePath(void *p
) const
737 cairo_path_destroy((cairo_path_t
*)p
);
744 void wxCairoPathData::MoveToPoint( wxDouble x
, wxDouble y
)
746 cairo_move_to(m_pathContext
,x
,y
);
749 void wxCairoPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
751 cairo_line_to(m_pathContext
,x
,y
);
754 void wxCairoPathData::AddPath( const wxGraphicsPathData
* path
)
759 void wxCairoPathData::CloseSubpath()
761 cairo_close_path(m_pathContext
);
764 void wxCairoPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
766 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
769 // gets the last point of the current path, (0,0) if not yet set
770 void wxCairoPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
773 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
780 void wxCairoPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
782 // as clockwise means positive in our system (y pointing downwards)
783 // TODO make this interpretation dependent of the
785 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
786 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
788 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
791 // transforms each point of this path by the matrix
792 void wxCairoPathData::Transform( const wxGraphicsMatrixData
* matrix
)
794 // as we don't have a true path object, we have to apply the inverse
795 // matrix to the context
796 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
797 cairo_matrix_invert( &m
);
798 cairo_transform(m_pathContext
,&m
);
801 // gets the bounding box enclosing all points (possibly including control points)
802 void wxCairoPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
806 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
830 bool wxCairoPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
832 return cairo_in_stroke( m_pathContext
, x
, y
) != 0;
835 //-----------------------------------------------------------------------------
836 // wxCairoMatrixData implementation
837 //-----------------------------------------------------------------------------
839 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
840 : wxGraphicsMatrixData(renderer
)
846 wxCairoMatrixData::~wxCairoMatrixData()
851 wxGraphicsObjectRefData
*wxCairoMatrixData::Clone() const
853 return new wxCairoMatrixData(GetRenderer(),&m_matrix
);
856 // concatenates the matrix
857 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData
*t
)
859 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
862 // sets the matrix to the respective values
863 void wxCairoMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
864 wxDouble tx
, wxDouble ty
)
866 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
869 // makes this the inverse matrix
870 void wxCairoMatrixData::Invert()
872 cairo_matrix_invert( &m_matrix
);
875 // returns true if the elements of the transformation matrix are equal ?
876 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
878 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
880 m_matrix
.xx
== tm
->xx
&&
881 m_matrix
.yx
== tm
->yx
&&
882 m_matrix
.xy
== tm
->xy
&&
883 m_matrix
.yy
== tm
->yy
&&
884 m_matrix
.x0
== tm
->x0
&&
885 m_matrix
.y0
== tm
->y0
) ;
888 // return true if this is the identity matrix
889 bool wxCairoMatrixData::IsIdentity() const
891 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
892 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
899 // add the translation to this matrix
900 void wxCairoMatrixData::Translate( wxDouble dx
, wxDouble dy
)
902 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
905 // add the scale to this matrix
906 void wxCairoMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
908 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
911 // add the rotation to this matrix (radians)
912 void wxCairoMatrixData::Rotate( wxDouble angle
)
914 cairo_matrix_rotate( &m_matrix
, angle
) ;
918 // apply the transforms
921 // applies that matrix to the point
922 void wxCairoMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
924 double lx
= *x
, ly
= *y
;
925 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
930 // applies the matrix except for translations
931 void wxCairoMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
933 double lx
= *dx
, ly
= *dy
;
934 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
939 // returns the native representation
940 void * wxCairoMatrixData::GetNativeMatrix() const
942 return (void*) &m_matrix
;
945 //-----------------------------------------------------------------------------
946 // wxCairoContext implementation
947 //-----------------------------------------------------------------------------
949 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
950 : wxGraphicsContext(renderer
)
953 m_context
= gdk_cairo_create( dc
.m_window
) ;
960 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
961 : wxGraphicsContext(renderer
)
963 m_context
= gdk_cairo_create( drawable
) ;
969 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
970 : wxGraphicsContext(renderer
)
972 m_context
= context
;
977 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
978 : wxGraphicsContext(renderer
)
981 // something along these lines (copied from dcclient)
983 GtkWidget
*widget
= window
->m_wxwindow
;
985 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
986 // code should still be able to create wxClientDCs for them, so we will
987 // use the parent window here then.
990 window
= window
->GetParent();
991 widget
= window
->m_wxwindow
;
994 wxASSERT_MSG( widget
, wxT("wxCairoContext needs a widget") );
996 GtkPizza
*pizza
= GTK_PIZZA( widget
);
997 GdkDrawable
* drawable
= pizza
->bin_window
;
998 m_context
= gdk_cairo_create( drawable
) ;
1004 wxCairoContext::~wxCairoContext()
1010 cairo_destroy(m_context
);
1015 void wxCairoContext::Clip( const wxRegion
& WXUNUSED(region
) )
1020 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1025 void wxCairoContext::ResetClip()
1031 void wxCairoContext::StrokePath( const wxGraphicsPath
& path
)
1033 if ( !m_pen
.IsNull() )
1035 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1036 cairo_append_path(m_context
,cp
);
1037 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1038 cairo_stroke(m_context
);
1039 path
.UnGetNativePath(cp
);
1043 void wxCairoContext::FillPath( const wxGraphicsPath
& path
, int fillStyle
)
1045 if ( !m_brush
.IsNull() )
1047 cairo_path_t
* cp
= (cairo_path_t
*) path
.GetNativePath() ;
1048 cairo_append_path(m_context
,cp
);
1049 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1050 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1051 cairo_fill(m_context
);
1052 path
.UnGetNativePath(cp
);
1056 void wxCairoContext::Rotate( wxDouble angle
)
1058 cairo_rotate(m_context
,angle
);
1061 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1063 cairo_translate(m_context
,dx
,dy
);
1066 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1068 cairo_scale(m_context
,xScale
,yScale
);
1071 // concatenates this transform with the current transform of this context
1072 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1074 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1077 // sets the transform of this context
1078 void wxCairoContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1080 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
.GetNativeMatrix());
1083 // gets the matrix of this context
1084 wxGraphicsMatrix
wxCairoContext::GetTransform() const
1086 wxGraphicsMatrix matrix
= CreateMatrix();
1087 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
.GetNativeMatrix());
1093 void wxCairoContext::PushState()
1095 cairo_save(m_context
);
1098 void wxCairoContext::PopState()
1100 cairo_restore(m_context
);
1103 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1106 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
1107 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1112 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1115 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
1116 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1122 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1124 if ( m_font
.IsNull() || str
.IsEmpty())
1126 cairo_move_to(m_context
,x
,y
);
1127 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1128 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1129 cairo_show_text(m_context
,buf
);
1132 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1133 wxDouble
*descent
, wxDouble
*externalLeading
) const
1138 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1141 widths
.Add(0, text
.length());
1149 void * wxCairoContext::GetNativeContext()
1154 //-----------------------------------------------------------------------------
1155 // wxCairoRenderer declaration
1156 //-----------------------------------------------------------------------------
1158 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1161 wxCairoRenderer() {}
1163 virtual ~wxCairoRenderer() {}
1167 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1170 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1173 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1175 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1177 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1179 virtual wxGraphicsContext
* CreateMeasuringContext();
1183 virtual wxGraphicsPath
CreatePath();
1187 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1188 wxDouble tx
=0.0, wxDouble ty
=0.0);
1191 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1193 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1195 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1196 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1197 const wxColour
&c1
, const wxColour
&c2
) ;
1199 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1200 // with radius r and color cColor
1201 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1202 const wxColour
&oColor
, const wxColour
&cColor
) ;
1205 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1208 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1211 //-----------------------------------------------------------------------------
1212 // wxCairoRenderer implementation
1213 //-----------------------------------------------------------------------------
1215 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1217 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1220 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1222 return &gs_cairoGraphicsRenderer
;
1226 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1228 return new wxCairoContext(this,dc
);
1232 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxMemoryDC
& dc
)
1238 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1240 return new wxCairoContext(this,(cairo_t
*)context
);
1244 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1247 return new wxCairoContext(this,(GdkDrawable
*)window
);
1253 wxGraphicsContext
* wxCairoRenderer::CreateMeasuringContext()
1259 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1261 return new wxCairoContext(this, window
);
1266 wxGraphicsPath
wxCairoRenderer::CreatePath()
1268 wxGraphicsPath path
;
1269 path
.SetRefData( new wxCairoPathData(this) );
1276 wxGraphicsMatrix
wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1277 wxDouble tx
, wxDouble ty
)
1281 wxCairoMatrixData
* data
= new wxCairoMatrixData( this );
1282 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1287 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1289 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1290 return wxNullGraphicsPen
;
1294 p
.SetRefData(new wxCairoPenData( this, pen
));
1299 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1301 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1302 return wxNullGraphicsBrush
;
1306 p
.SetRefData(new wxCairoBrushData( this, brush
));
1311 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1312 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1313 const wxColour
&c1
, const wxColour
&c2
)
1316 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1317 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1322 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1323 // with radius r and color cColor
1324 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1325 const wxColour
&oColor
, const wxColour
&cColor
)
1328 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1329 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1335 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1340 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1344 return wxNullGraphicsFont
;
1347 #endif // wxUSE_GRAPHICS_CONTEXT