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 wxCairoPath
: public wxGraphicsPath
110 wxCairoPath(wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
= NULL
);
113 virtual wxGraphicsPath
*Clone() const;
116 // These are the path primitives from which everything else can be constructed
119 // begins a new subpath at (x,y)
120 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
122 // adds a straight line from the current point to (x,y)
123 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
125 // adds a cubic Bezier curve from the current point, using two control points and an end point
126 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
129 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
130 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
132 // gets the last point of the current path, (0,0) if not yet set
133 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) ;
136 virtual void AddPath( const wxGraphicsPath
* path
);
138 // closes the current sub-path
139 virtual void CloseSubpath();
142 // These are convenience functions which - if not available natively will be assembled
143 // using the primitives from above
148 // appends a rectangle as a new closed subpath
149 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
150 // appends an ellipsis as a new closed subpath fitting the passed rectangle
151 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
153 // 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)
154 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
157 // returns the native path
158 virtual void * GetNativePath() const ;
160 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
161 virtual void UnGetNativePath(void *p
) ;
163 // transforms each point of this path by the matrix
164 virtual void Transform( wxGraphicsMatrix
* matrix
) ;
166 // gets the bounding box enclosing all points (possibly including control points)
167 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) ;
169 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxWINDING_RULE
) ;
172 cairo_t
* m_pathContext
;
173 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoPath
)
176 class WXDLLIMPEXP_CORE wxCairoMatrix
: public wxGraphicsMatrix
181 wxCairoMatrix(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
= NULL
) ;
182 virtual ~wxCairoMatrix() ;
184 virtual wxGraphicsMatrix
*Clone() const ;
186 // concatenates the matrix
187 virtual void Concat( const wxGraphicsMatrix
*t
);
189 // copies the passed in matrix
190 virtual void Copy( const wxGraphicsMatrix
*t
);
192 // sets the matrix to the respective values
193 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
194 wxDouble tx
=0.0, wxDouble ty
=0.0);
196 // makes this the inverse matrix
197 virtual void Invert();
199 // returns true if the elements of the transformation matrix are equal ?
200 virtual bool IsEqual( const wxGraphicsMatrix
* t
) const ;
202 // return true if this is the identity matrix
203 virtual bool IsIdentity();
209 // add the translation to this matrix
210 virtual void Translate( wxDouble dx
, wxDouble dy
);
212 // add the scale to this matrix
213 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
215 // add the rotation to this matrix (radians)
216 virtual void Rotate( wxDouble angle
);
219 // apply the transforms
222 // applies that matrix to the point
223 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
);
225 // applies the matrix except for translations
226 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
);
228 // returns the native representation
229 virtual void * GetNativeMatrix() const;
231 cairo_matrix_t m_matrix
;
233 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoMatrix
)
236 class WXDLLIMPEXP_CORE wxCairoPenData
: public wxGraphicsObjectRefData
239 wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
244 virtual void Apply( wxGraphicsContext
* context
);
245 virtual wxDouble
GetWidth() { return m_width
; }
255 cairo_line_cap_t m_cap
;
256 cairo_line_join_t m_join
;
259 const double *m_lengths
;
260 double *m_userLengths
;
265 class WXDLLIMPEXP_CORE wxCairoBrushData
: public wxGraphicsObjectRefData
268 wxCairoBrushData( wxGraphicsRenderer
* renderer
);
269 wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
270 ~wxCairoBrushData ();
272 virtual void Apply( wxGraphicsContext
* context
);
273 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
274 const wxColour
&c1
, const wxColour
&c2
);
275 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
276 const wxColour
&oColor
, const wxColour
&cColor
);
287 cairo_pattern_t
* m_brushPattern
;
290 class wxCairoFontData
: public wxGraphicsObjectRefData
293 wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
296 virtual void Apply( wxGraphicsContext
* context
);
298 wxCharBuffer m_fontName
;
300 cairo_font_slant_t m_slant
;
301 cairo_font_weight_t m_weight
;
308 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
310 DECLARE_NO_COPY_CLASS(wxCairoContext
)
313 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
315 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
317 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
318 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
320 virtual ~wxCairoContext();
322 virtual void Clip( const wxRegion
®ion
);
324 // clips drawings to the rect
325 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
327 // resets the clipping to original extent
328 virtual void ResetClip();
330 virtual void * GetNativeContext();
332 virtual void StrokePath( const wxGraphicsPath
*p
);
333 virtual void FillPath( const wxGraphicsPath
*p
, int fillStyle
= wxWINDING_RULE
);
335 virtual void Translate( wxDouble dx
, wxDouble dy
);
336 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
337 virtual void Rotate( wxDouble angle
);
339 // concatenates this transform with the current transform of this context
340 virtual void ConcatTransform( const wxGraphicsMatrix
* matrix
);
342 // sets the transform of this context
343 virtual void SetTransform( const wxGraphicsMatrix
* matrix
);
345 // gets the matrix of this context
346 virtual void GetTransform( wxGraphicsMatrix
* matrix
);
348 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
349 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
350 virtual void PushState();
351 virtual void PopState();
353 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
354 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
355 wxDouble
*descent
, wxDouble
*externalLeading
) const;
356 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
362 //-----------------------------------------------------------------------------
363 // wxCairoPenData implementation
364 //-----------------------------------------------------------------------------
366 wxCairoPenData::~wxCairoPenData()
368 delete[] m_userLengths
;
371 void wxCairoPenData::Init()
374 m_userLengths
= NULL
;
379 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
380 : wxGraphicsObjectRefData(renderer
)
384 m_width
= m_pen
.GetWidth();
388 m_red
= m_pen
.GetColour().Red()/255.0;
389 m_green
= m_pen
.GetColour().Green()/255.0;
390 m_blue
= m_pen
.GetColour().Blue()/255.0;
391 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
393 switch ( m_pen
.GetCap() )
396 m_cap
= CAIRO_LINE_CAP_ROUND
;
399 case wxCAP_PROJECTING
:
400 m_cap
= CAIRO_LINE_CAP_SQUARE
;
404 m_cap
= CAIRO_LINE_CAP_BUTT
;
408 m_cap
= CAIRO_LINE_CAP_BUTT
;
412 switch ( m_pen
.GetJoin() )
415 m_join
= CAIRO_LINE_JOIN_BEVEL
;
419 m_join
= CAIRO_LINE_JOIN_MITER
;
423 m_join
= CAIRO_LINE_JOIN_ROUND
;
427 m_join
= CAIRO_LINE_JOIN_MITER
;
431 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
432 const double dotted
[] =
434 dashUnit
, dashUnit
+ 2.0
436 static const double short_dashed
[] =
440 static const double dashed
[] =
444 static const double dotted_dashed
[] =
446 9.0 , 6.0 , 3.0 , 3.0
449 switch ( m_pen
.GetStyle() )
455 m_count
= WXSIZEOF(dotted
);
456 m_userLengths
= new double[ m_count
] ;
457 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
458 m_lengths
= m_userLengths
;
463 m_count
= WXSIZEOF(dashed
);
468 m_count
= WXSIZEOF(short_dashed
);
473 m_count
= WXSIZEOF(dotted_dashed
);
479 m_count
= m_pen
.GetDashes( &wxdashes
) ;
480 if ((wxdashes
!= NULL
) && (m_count
> 0))
482 m_userLengths
= new double[m_count
] ;
483 for ( int i
= 0 ; i
< m_count
; ++i
)
485 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
487 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
488 m_userLengths
[i
] = dashUnit
+ 2.0 ;
489 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
490 m_userLengths
[i
] = dashUnit
;
493 m_lengths
= m_userLengths
;
499 wxBitmap* bmp = pen.GetStipple();
500 if ( bmp && bmp->Ok() )
502 wxDELETE( m_penImage );
503 wxDELETE( m_penBrush );
504 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
505 m_penBrush = new TextureBrush(m_penImage);
506 m_pen->SetBrush( m_penBrush );
512 if ( m_pen
.GetStyle() >= wxFIRST_HATCH
&& m_pen
.GetStyle() <= wxLAST_HATCH
)
515 wxDELETE( m_penBrush );
516 HatchStyle style = HatchStyleHorizontal;
517 switch( pen.GetStyle() )
519 case wxBDIAGONAL_HATCH :
520 style = HatchStyleBackwardDiagonal;
522 case wxCROSSDIAG_HATCH :
523 style = HatchStyleDiagonalCross;
525 case wxFDIAGONAL_HATCH :
526 style = HatchStyleForwardDiagonal;
529 style = HatchStyleCross;
531 case wxHORIZONTAL_HATCH :
532 style = HatchStyleHorizontal;
534 case wxVERTICAL_HATCH :
535 style = HatchStyleVertical;
539 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
540 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
541 m_pen->SetBrush( m_penBrush )
548 void wxCairoPenData::Apply( wxGraphicsContext
* context
)
550 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
551 cairo_set_line_width(ctext
,m_width
);
552 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
553 cairo_set_line_cap(ctext
,m_cap
);
554 cairo_set_line_join(ctext
,m_join
);
555 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
558 //-----------------------------------------------------------------------------
559 // wxCairoBrushData implementation
560 //-----------------------------------------------------------------------------
562 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
)
563 : wxGraphicsObjectRefData( renderer
)
568 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
569 : wxGraphicsObjectRefData(renderer
)
573 m_red
= brush
.GetColour().Red()/255.0;
574 m_green
= brush
.GetColour().Green()/255.0;
575 m_blue
= brush
.GetColour().Blue()/255.0;
576 m_alpha
= brush
.GetColour().Alpha()/255.0;
578 if ( brush.GetStyle() == wxSOLID)
580 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
581 brush.GetColour().Green() , brush.GetColour().Blue() ) );
583 else if ( brush.IsHatch() )
585 HatchStyle style = HatchStyleHorizontal;
586 switch( brush.GetStyle() )
588 case wxBDIAGONAL_HATCH :
589 style = HatchStyleBackwardDiagonal;
591 case wxCROSSDIAG_HATCH :
592 style = HatchStyleDiagonalCross;
594 case wxFDIAGONAL_HATCH :
595 style = HatchStyleForwardDiagonal;
598 style = HatchStyleCross;
600 case wxHORIZONTAL_HATCH :
601 style = HatchStyleHorizontal;
603 case wxVERTICAL_HATCH :
604 style = HatchStyleVertical;
608 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
609 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
613 wxBitmap* bmp = brush.GetStipple();
614 if ( bmp && bmp->Ok() )
616 wxDELETE( m_brushImage );
617 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
618 m_brush = new TextureBrush(m_brushImage);
624 wxCairoBrushData::~wxCairoBrushData ()
627 cairo_pattern_destroy(m_brushPattern
);
630 void wxCairoBrushData::Apply( wxGraphicsContext
* context
)
632 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
633 if ( m_brushPattern
)
635 cairo_set_source(ctext
,m_brushPattern
);
639 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
643 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
644 const wxColour
&c1
, const wxColour
&c2
)
646 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
647 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
648 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
649 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
650 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
651 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
654 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
655 const wxColour
&oColor
, const wxColour
&cColor
)
657 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
658 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
659 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
660 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
661 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
662 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
665 void wxCairoBrushData::Init()
667 m_brushPattern
= NULL
;
670 //-----------------------------------------------------------------------------
671 // wxCairoFontData implementation
672 //-----------------------------------------------------------------------------
674 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
675 const wxColour
& col
) : wxGraphicsObjectRefData(renderer
)
677 m_red
= col
.Red()/255.0;
678 m_green
= col
.Green()/255.0;
679 m_blue
= col
.Blue()/255.0;
680 m_alpha
= col
.Alpha()/255.0;
682 m_size
= font
.GetPointSize();
683 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
684 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
685 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
688 wxCairoFontData::~wxCairoFontData()
692 void wxCairoFontData::Apply( wxGraphicsContext
* context
)
694 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
695 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
696 cairo_select_font_face(ctext
,m_fontName
,m_slant
,m_weight
);
697 cairo_set_font_size(ctext
,m_size
);
702 //-----------------------------------------------------------------------------
703 // wxCairoPath implementation
704 //-----------------------------------------------------------------------------
706 IMPLEMENT_DYNAMIC_CLASS(wxCairoPath
,wxGraphicsPath
)
708 wxCairoPath::wxCairoPath() : wxGraphicsPath(NULL
)
710 wxLogDebug(wxT("Illegal Constructor called"));
713 wxCairoPath::wxCairoPath( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
714 : wxGraphicsPath(renderer
)
718 m_pathContext
= pathcontext
;
722 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
723 m_pathContext
= cairo_create(surface
);
724 cairo_surface_destroy (surface
);
728 wxCairoPath::~wxCairoPath()
730 cairo_destroy(m_pathContext
);
733 wxGraphicsPath
*wxCairoPath::Clone() const
735 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
736 cairo_t
* pathcontext
= cairo_create(surface
);
737 cairo_surface_destroy (surface
);
739 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
740 cairo_append_path(pathcontext
, path
);
741 cairo_path_destroy(path
);
742 return new wxCairoPath( GetRenderer() ,pathcontext
);
746 void* wxCairoPath::GetNativePath() const
748 return cairo_copy_path(m_pathContext
) ;
751 void wxCairoPath::UnGetNativePath(void *p
)
753 cairo_path_destroy((cairo_path_t
*)p
);
760 void wxCairoPath::MoveToPoint( wxDouble x
, wxDouble y
)
762 cairo_move_to(m_pathContext
,x
,y
);
765 void wxCairoPath::AddLineToPoint( wxDouble x
, wxDouble y
)
767 cairo_line_to(m_pathContext
,x
,y
);
770 void wxCairoPath::AddPath( const wxGraphicsPath
* path
)
775 void wxCairoPath::CloseSubpath()
777 cairo_close_path(m_pathContext
);
780 void wxCairoPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
782 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
785 // gets the last point of the current path, (0,0) if not yet set
786 void wxCairoPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
789 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
794 void wxCairoPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
796 // as clockwise means positive in our system (y pointing downwards)
797 // TODO make this interpretation dependent of the
799 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
800 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
802 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
805 // transforms each point of this path by the matrix
806 void wxCairoPath::Transform( wxGraphicsMatrix
* matrix
)
808 // as we don't have a true path object, we have to apply the inverse
809 // matrix to the context
810 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
811 cairo_matrix_invert( &m
);
812 cairo_transform(m_pathContext
,&m
);
815 // gets the bounding box enclosing all points (possibly including control points)
816 void wxCairoPath::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
)
820 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
844 bool wxCairoPath::Contains( wxDouble x
, wxDouble y
, int fillStyle
)
846 return cairo_in_stroke( m_pathContext
, x
, y
) != NULL
;
849 //-----------------------------------------------------------------------------
850 // wxCairoMatrix implementation
851 //-----------------------------------------------------------------------------
853 IMPLEMENT_DYNAMIC_CLASS(wxCairoMatrix
,wxGraphicsMatrix
)
855 wxCairoMatrix::wxCairoMatrix() : wxGraphicsMatrix(NULL
)
857 wxLogDebug(wxT("Illegal Constructor called"));
860 wxCairoMatrix::wxCairoMatrix(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
861 : wxGraphicsMatrix(renderer
)
867 wxCairoMatrix::~wxCairoMatrix()
872 wxGraphicsMatrix
*wxCairoMatrix::Clone() const
874 return new wxCairoMatrix(GetRenderer(),&m_matrix
);
877 // concatenates the matrix
878 void wxCairoMatrix::Concat( const wxGraphicsMatrix
*t
)
880 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
883 // copies the passed in matrix
884 void wxCairoMatrix::Copy( const wxGraphicsMatrix
*t
)
886 m_matrix
= *((cairo_matrix_t
*) t
->GetNativeMatrix());
889 // sets the matrix to the respective values
890 void wxCairoMatrix::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
891 wxDouble tx
, wxDouble ty
)
893 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
896 // makes this the inverse matrix
897 void wxCairoMatrix::Invert()
899 cairo_matrix_invert( &m_matrix
);
902 // returns true if the elements of the transformation matrix are equal ?
903 bool wxCairoMatrix::IsEqual( const wxGraphicsMatrix
* t
) const
905 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
907 m_matrix
.xx
== tm
->xx
&&
908 m_matrix
.yx
== tm
->yx
&&
909 m_matrix
.xy
== tm
->xy
&&
910 m_matrix
.yy
== tm
->yy
&&
911 m_matrix
.x0
== tm
->x0
&&
912 m_matrix
.y0
== tm
->y0
) ;
915 // return true if this is the identity matrix
916 bool wxCairoMatrix::IsIdentity()
918 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
919 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
926 // add the translation to this matrix
927 void wxCairoMatrix::Translate( wxDouble dx
, wxDouble dy
)
929 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
932 // add the scale to this matrix
933 void wxCairoMatrix::Scale( wxDouble xScale
, wxDouble yScale
)
935 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
938 // add the rotation to this matrix (radians)
939 void wxCairoMatrix::Rotate( wxDouble angle
)
941 cairo_matrix_rotate( &m_matrix
, angle
) ;
945 // apply the transforms
948 // applies that matrix to the point
949 void wxCairoMatrix::TransformPoint( wxDouble
*x
, wxDouble
*y
)
951 double lx
= *x
, ly
= *y
;
952 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
957 // applies the matrix except for translations
958 void wxCairoMatrix::TransformDistance( wxDouble
*dx
, wxDouble
*dy
)
960 double lx
= *dx
, ly
= *dy
;
961 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
966 // returns the native representation
967 void * wxCairoMatrix::GetNativeMatrix() const
969 return (void*) &m_matrix
;
972 //-----------------------------------------------------------------------------
973 // wxCairoContext implementation
974 //-----------------------------------------------------------------------------
976 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
977 : wxGraphicsContext(renderer
)
980 m_context
= gdk_cairo_create( dc
.m_window
) ;
987 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
988 : wxGraphicsContext(renderer
)
990 m_context
= gdk_cairo_create( drawable
) ;
996 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
997 : wxGraphicsContext(renderer
)
999 m_context
= context
;
1004 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1005 : wxGraphicsContext(renderer
)
1008 // something along these lines (copied from dcclient)
1010 GtkWidget
*widget
= window
->m_wxwindow
;
1012 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1013 // code should still be able to create wxClientDCs for them, so we will
1014 // use the parent window here then.
1017 window
= window
->GetParent();
1018 widget
= window
->m_wxwindow
;
1021 wxASSERT_MSG( widget
, wxT("wxCairoContext needs a widget") );
1023 GtkPizza
*pizza
= GTK_PIZZA( widget
);
1024 GdkDrawable
* drawable
= pizza
->bin_window
;
1025 m_context
= gdk_cairo_create( drawable
) ;
1031 wxCairoContext::~wxCairoContext()
1037 cairo_destroy(m_context
);
1042 void wxCairoContext::Clip( const wxRegion
& WXUNUSED(region
) )
1047 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1052 void wxCairoContext::ResetClip()
1058 void wxCairoContext::StrokePath( const wxGraphicsPath
*path
)
1060 if ( !m_pen
.IsNull() )
1062 cairo_path_t
* cp
= (cairo_path_t
*) path
->GetNativePath() ;
1063 cairo_append_path(m_context
,cp
);
1064 ((wxCairoPenData
*)m_pen
.GetRefData())->Apply(this);
1065 cairo_stroke(m_context
);
1066 wxConstCast(path
, wxGraphicsPath
)->UnGetNativePath(cp
);
1070 void wxCairoContext::FillPath( const wxGraphicsPath
*path
, int fillStyle
)
1072 if ( !m_brush
.IsNull() )
1074 cairo_path_t
* cp
= (cairo_path_t
*) path
->GetNativePath() ;
1075 cairo_append_path(m_context
,cp
);
1076 ((wxCairoBrushData
*)m_brush
.GetRefData())->Apply(this);
1077 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1078 cairo_fill(m_context
);
1079 wxConstCast(path
, wxGraphicsPath
)->UnGetNativePath(cp
);
1083 void wxCairoContext::Rotate( wxDouble angle
)
1085 cairo_rotate(m_context
,angle
);
1088 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1090 cairo_translate(m_context
,dx
,dy
);
1093 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1095 cairo_scale(m_context
,xScale
,yScale
);
1098 // concatenates this transform with the current transform of this context
1099 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
* matrix
)
1101 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
->GetNativeMatrix());
1104 // sets the transform of this context
1105 void wxCairoContext::SetTransform( const wxGraphicsMatrix
* matrix
)
1107 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
->GetNativeMatrix());
1110 // gets the matrix of this context
1111 void wxCairoContext::GetTransform( wxGraphicsMatrix
* matrix
)
1113 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
->GetNativeMatrix());
1118 void wxCairoContext::PushState()
1120 cairo_save(m_context
);
1123 void wxCairoContext::PopState()
1125 cairo_restore(m_context
);
1128 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1131 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
1132 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1137 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1140 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
1141 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1147 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1149 if ( m_font
.IsNull() || str
.IsEmpty())
1151 cairo_move_to(m_context
,x
,y
);
1152 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1153 ((wxCairoFontData
*)m_font
.GetRefData())->Apply(this);
1154 cairo_show_text(m_context
,buf
);
1157 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1158 wxDouble
*descent
, wxDouble
*externalLeading
) const
1163 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1166 widths
.Add(0, text
.length());
1174 void * wxCairoContext::GetNativeContext()
1179 //-----------------------------------------------------------------------------
1180 // wxCairoRenderer declaration
1181 //-----------------------------------------------------------------------------
1183 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1186 wxCairoRenderer() {}
1188 virtual ~wxCairoRenderer() {}
1192 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1194 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1196 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1198 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1202 virtual wxGraphicsPath
* CreatePath();
1206 virtual wxGraphicsMatrix
* CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1207 wxDouble tx
=0.0, wxDouble ty
=0.0);
1210 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1212 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1214 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1215 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1216 const wxColour
&c1
, const wxColour
&c2
) ;
1218 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1219 // with radius r and color cColor
1220 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1221 const wxColour
&oColor
, const wxColour
&cColor
) ;
1224 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1227 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1230 //-----------------------------------------------------------------------------
1231 // wxCairoRenderer implementation
1232 //-----------------------------------------------------------------------------
1234 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1236 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1239 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1241 return &gs_cairoGraphicsRenderer
;
1245 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1247 return new wxCairoContext(this,dc
);
1250 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1252 return new wxCairoContext(this,(cairo_t
*)context
);
1256 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1259 return new wxCairoContext(this,(GdkDrawable
*)window
);
1265 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1267 return new wxCairoContext(this, window
);
1272 wxGraphicsPath
* wxCairoRenderer::CreatePath()
1274 return new wxCairoPath( this );
1280 wxGraphicsMatrix
* wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1281 wxDouble tx
, wxDouble ty
)
1284 wxCairoMatrix
* m
= new wxCairoMatrix( this );
1285 m
->Set( a
,b
,c
,d
,tx
,ty
) ;
1289 wxGraphicsPen
wxCairoRenderer::CreatePen(const wxPen
& pen
)
1291 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1292 return wxNullGraphicsPen
;
1296 p
.SetRefData(new wxCairoPenData( this, pen
));
1301 wxGraphicsBrush
wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1303 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1304 return wxNullGraphicsBrush
;
1308 p
.SetRefData(new wxCairoBrushData( this, brush
));
1313 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1314 wxGraphicsBrush
wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1315 const wxColour
&c1
, const wxColour
&c2
)
1318 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1319 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1324 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1325 // with radius r and color cColor
1326 wxGraphicsBrush
wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1327 const wxColour
&oColor
, const wxColour
&cColor
)
1330 wxCairoBrushData
* d
= new wxCairoBrushData( this );
1331 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1337 wxGraphicsFont
wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1342 p
.SetRefData(new wxCairoFontData( this , font
, col
));
1346 return wxNullGraphicsFont
;
1349 #endif // wxUSE_GRAPHICS_CONTEXT