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 wxCairoPen
: public wxGraphicsPen
240 wxCairoPen( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
245 virtual void Apply( wxGraphicsContext
* context
);
246 virtual wxDouble
GetWidth() { return m_width
; }
256 cairo_line_cap_t m_cap
;
257 cairo_line_join_t m_join
;
260 const double *m_lengths
;
261 double *m_userLengths
;
264 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoPen
)
267 class WXDLLIMPEXP_CORE wxCairoBrush
: public wxGraphicsBrush
271 wxCairoBrush( wxGraphicsRenderer
* renderer
);
272 wxCairoBrush( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
275 virtual void Apply( wxGraphicsContext
* context
);
276 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
277 const wxColour
&c1
, const wxColour
&c2
);
278 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
279 const wxColour
&oColor
, const wxColour
&cColor
);
290 cairo_pattern_t
* m_brushPattern
;
292 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoBrush
)
295 class wxCairoFont
: public wxGraphicsFont
299 wxCairoFont( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
302 virtual void Apply( wxGraphicsContext
* context
);
304 wxCharBuffer m_fontName
;
306 cairo_font_slant_t m_slant
;
307 cairo_font_weight_t m_weight
;
312 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoFont
)
315 class WXDLLIMPEXP_CORE wxCairoContext
: public wxGraphicsContext
317 DECLARE_NO_COPY_CLASS(wxCairoContext
)
320 wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
);
322 wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
);
324 wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
);
325 wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
);
327 virtual ~wxCairoContext();
329 virtual void Clip( const wxRegion
®ion
);
331 // clips drawings to the rect
332 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
334 // resets the clipping to original extent
335 virtual void ResetClip();
337 virtual void * GetNativeContext();
339 virtual void StrokePath( const wxGraphicsPath
*p
);
340 virtual void FillPath( const wxGraphicsPath
*p
, int fillStyle
= wxWINDING_RULE
);
342 virtual void Translate( wxDouble dx
, wxDouble dy
);
343 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
344 virtual void Rotate( wxDouble angle
);
346 // concatenates this transform with the current transform of this context
347 virtual void ConcatTransform( const wxGraphicsMatrix
* matrix
);
349 // sets the transform of this context
350 virtual void SetTransform( const wxGraphicsMatrix
* matrix
);
352 // gets the matrix of this context
353 virtual void GetTransform( wxGraphicsMatrix
* matrix
);
355 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
356 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
357 virtual void PushState();
358 virtual void PopState();
360 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
361 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
362 wxDouble
*descent
, wxDouble
*externalLeading
) const;
363 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
369 //-----------------------------------------------------------------------------
370 // wxCairoPen implementation
371 //-----------------------------------------------------------------------------
373 IMPLEMENT_DYNAMIC_CLASS(wxCairoPen
,wxGraphicsPen
)
375 wxCairoPen::wxCairoPen() : wxGraphicsPen(NULL
)
377 wxLogDebug(wxT("Illegal Constructor called"));
380 wxCairoPen::~wxCairoPen()
382 delete[] m_userLengths
;
385 void wxCairoPen::Init()
388 m_userLengths
= NULL
;
393 wxCairoPen::wxCairoPen( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
394 : wxGraphicsPen(renderer
)
397 m_width
= pen
.GetWidth();
401 m_red
= m_pen
.GetColour().Red()/255.0;
402 m_green
= m_pen
.GetColour().Green()/255.0;
403 m_blue
= m_pen
.GetColour().Blue()/255.0;
404 m_alpha
= m_pen
.GetColour().Alpha()/255.0;
406 switch ( m_pen
.GetCap() )
409 m_cap
= CAIRO_LINE_CAP_ROUND
;
412 case wxCAP_PROJECTING
:
413 m_cap
= CAIRO_LINE_CAP_SQUARE
;
417 m_cap
= CAIRO_LINE_CAP_BUTT
;
421 m_cap
= CAIRO_LINE_CAP_BUTT
;
425 switch ( m_pen
.GetJoin() )
428 m_join
= CAIRO_LINE_JOIN_BEVEL
;
432 m_join
= CAIRO_LINE_JOIN_MITER
;
436 m_join
= CAIRO_LINE_JOIN_ROUND
;
440 m_join
= CAIRO_LINE_JOIN_MITER
;
444 const double dashUnit
= m_width
< 1.0 ? 1.0 : m_width
;
445 const double dotted
[] =
447 dashUnit
, dashUnit
+ 2.0
449 static const double short_dashed
[] =
453 static const double dashed
[] =
457 static const double dotted_dashed
[] =
459 9.0 , 6.0 , 3.0 , 3.0
462 switch ( m_pen
.GetStyle() )
468 m_count
= WXSIZEOF(dotted
);
469 m_userLengths
= new double[ m_count
] ;
470 memcpy( m_userLengths
, dotted
, sizeof(dotted
) );
471 m_lengths
= m_userLengths
;
476 m_count
= WXSIZEOF(dashed
);
481 m_count
= WXSIZEOF(short_dashed
);
486 m_count
= WXSIZEOF(dotted_dashed
);
492 m_count
= m_pen
.GetDashes( &wxdashes
) ;
493 if ((wxdashes
!= NULL
) && (m_count
> 0))
495 m_userLengths
= new double[m_count
] ;
496 for ( int i
= 0 ; i
< m_count
; ++i
)
498 m_userLengths
[i
] = wxdashes
[i
] * dashUnit
;
500 if ( i
% 2 == 1 && m_userLengths
[i
] < dashUnit
+ 2.0 )
501 m_userLengths
[i
] = dashUnit
+ 2.0 ;
502 else if ( i
% 2 == 0 && m_userLengths
[i
] < dashUnit
)
503 m_userLengths
[i
] = dashUnit
;
506 m_lengths
= m_userLengths
;
512 wxBitmap* bmp = pen.GetStipple();
513 if ( bmp && bmp->Ok() )
515 wxDELETE( m_penImage );
516 wxDELETE( m_penBrush );
517 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
518 m_penBrush = new TextureBrush(m_penImage);
519 m_pen->SetBrush( m_penBrush );
525 if ( m_pen
.GetStyle() >= wxFIRST_HATCH
&& m_pen
.GetStyle() <= wxLAST_HATCH
)
528 wxDELETE( m_penBrush );
529 HatchStyle style = HatchStyleHorizontal;
530 switch( pen.GetStyle() )
532 case wxBDIAGONAL_HATCH :
533 style = HatchStyleBackwardDiagonal;
535 case wxCROSSDIAG_HATCH :
536 style = HatchStyleDiagonalCross;
538 case wxFDIAGONAL_HATCH :
539 style = HatchStyleForwardDiagonal;
542 style = HatchStyleCross;
544 case wxHORIZONTAL_HATCH :
545 style = HatchStyleHorizontal;
547 case wxVERTICAL_HATCH :
548 style = HatchStyleVertical;
552 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
553 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
554 m_pen->SetBrush( m_penBrush )
561 void wxCairoPen::Apply( wxGraphicsContext
* context
)
563 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
564 cairo_set_line_width(ctext
,m_width
);
565 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
566 cairo_set_line_cap(ctext
,m_cap
);
567 cairo_set_line_join(ctext
,m_join
);
568 cairo_set_dash(ctext
,(double*)m_lengths
,m_count
,0.0);
571 //-----------------------------------------------------------------------------
572 // wxCairoBrush implementation
573 //-----------------------------------------------------------------------------
575 IMPLEMENT_DYNAMIC_CLASS(wxCairoBrush
,wxGraphicsBrush
)
577 wxCairoBrush::wxCairoBrush() : wxGraphicsBrush( NULL
)
579 wxLogDebug(wxT("Illegal Constructor called"));
582 wxCairoBrush::wxCairoBrush( wxGraphicsRenderer
* renderer
) : wxGraphicsBrush( renderer
)
587 wxCairoBrush::wxCairoBrush( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
588 : wxGraphicsBrush(renderer
)
590 m_red
= brush
.GetColour().Red()/255.0;
591 m_green
= brush
.GetColour().Green()/255.0;
592 m_blue
= brush
.GetColour().Blue()/255.0;
593 m_alpha
= brush
.GetColour().Alpha()/255.0;
595 if ( brush.GetStyle() == wxSOLID)
597 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
598 brush.GetColour().Green() , brush.GetColour().Blue() ) );
600 else if ( brush.IsHatch() )
602 HatchStyle style = HatchStyleHorizontal;
603 switch( brush.GetStyle() )
605 case wxBDIAGONAL_HATCH :
606 style = HatchStyleBackwardDiagonal;
608 case wxCROSSDIAG_HATCH :
609 style = HatchStyleDiagonalCross;
611 case wxFDIAGONAL_HATCH :
612 style = HatchStyleForwardDiagonal;
615 style = HatchStyleCross;
617 case wxHORIZONTAL_HATCH :
618 style = HatchStyleHorizontal;
620 case wxVERTICAL_HATCH :
621 style = HatchStyleVertical;
625 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
626 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
630 wxBitmap* bmp = brush.GetStipple();
631 if ( bmp && bmp->Ok() )
633 wxDELETE( m_brushImage );
634 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
635 m_brush = new TextureBrush(m_brushImage);
641 wxCairoBrush::~wxCairoBrush ()
644 cairo_pattern_destroy(m_brushPattern
);
647 void wxCairoBrush::Apply( wxGraphicsContext
* context
)
649 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
650 if ( m_brushPattern
)
652 cairo_set_source(ctext
,m_brushPattern
);
656 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
660 void wxCairoBrush::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
661 const wxColour
&c1
, const wxColour
&c2
)
663 m_brushPattern
= cairo_pattern_create_linear(x1
,y1
,x2
,y2
);
664 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,c1
.Red()/255.0,
665 c1
.Green()/255.0, c1
.Blue()/255.0,c1
.Alpha()/255.0);
666 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,c2
.Red()/255.0,
667 c2
.Green()/255.0, c2
.Blue()/255.0,c2
.Alpha()/255.0);
668 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
671 void wxCairoBrush::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
672 const wxColour
&oColor
, const wxColour
&cColor
)
674 m_brushPattern
= cairo_pattern_create_radial(xo
,yo
,0.0,xc
,yc
,radius
);
675 cairo_pattern_add_color_stop_rgba(m_brushPattern
,0.0,oColor
.Red()/255.0,
676 oColor
.Green()/255.0, oColor
.Blue()/255.0,oColor
.Alpha()/255.0);
677 cairo_pattern_add_color_stop_rgba(m_brushPattern
,1.0,cColor
.Red()/255.0,
678 cColor
.Green()/255.0, cColor
.Blue()/255.0,cColor
.Alpha()/255.0);
679 wxASSERT_MSG(cairo_pattern_status(m_brushPattern
) == CAIRO_STATUS_SUCCESS
, wxT("Couldn't create cairo pattern"));
682 void wxCairoBrush::Init()
684 m_brushPattern
= NULL
;
687 //-----------------------------------------------------------------------------
688 // wxCairoFont implementation
689 //-----------------------------------------------------------------------------
691 wxCairoFont::wxCairoFont() : wxGraphicsFont(NULL
)
693 wxLogDebug(wxT("Illegal Constructor called"));
696 wxCairoFont::wxCairoFont( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
697 const wxColour
& col
) : wxGraphicsFont(renderer
)
699 m_red
= col
.Red()/255.0;
700 m_green
= col
.Green()/255.0;
701 m_blue
= col
.Blue()/255.0;
702 m_alpha
= col
.Alpha()/255.0;
704 m_size
= font
.GetPointSize();
705 m_fontName
= font
.GetFaceName().mb_str(wxConvUTF8
);
706 m_slant
= font
.GetStyle() == wxFONTSTYLE_ITALIC
? CAIRO_FONT_SLANT_ITALIC
:CAIRO_FONT_SLANT_NORMAL
;
707 m_weight
= font
.GetWeight() == wxFONTWEIGHT_BOLD
? CAIRO_FONT_WEIGHT_BOLD
:CAIRO_FONT_WEIGHT_NORMAL
;
710 wxCairoFont::~wxCairoFont()
714 void wxCairoFont::Apply( wxGraphicsContext
* context
)
716 cairo_t
* ctext
= (cairo_t
*) context
->GetNativeContext();
717 cairo_set_source_rgba(ctext
,m_red
,m_green
, m_blue
,m_alpha
);
718 cairo_select_font_face(ctext
,m_fontName
,m_slant
,m_weight
);
719 cairo_set_font_size(ctext
,m_size
);
724 //-----------------------------------------------------------------------------
725 // wxCairoPath implementation
726 //-----------------------------------------------------------------------------
728 wxCairoPath::wxCairoPath() : wxGraphicsPath(NULL
)
730 wxLogDebug(wxT("Illegal Constructor called"));
733 wxCairoPath::wxCairoPath( wxGraphicsRenderer
* renderer
, cairo_t
* pathcontext
)
734 : wxGraphicsPath(renderer
)
738 m_pathContext
= pathcontext
;
742 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
743 m_pathContext
= cairo_create(surface
);
744 cairo_surface_destroy (surface
);
748 wxCairoPath::~wxCairoPath()
750 cairo_destroy(m_pathContext
);
753 wxGraphicsPath
*wxCairoPath::Clone() const
755 cairo_surface_t
* surface
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
,1,1);
756 cairo_t
* pathcontext
= cairo_create(surface
);
757 cairo_surface_destroy (surface
);
759 cairo_path_t
* path
= cairo_copy_path(m_pathContext
);
760 cairo_append_path(pathcontext
, path
);
761 cairo_path_destroy(path
);
762 return new wxCairoPath( GetRenderer() ,pathcontext
);
766 void* wxCairoPath::GetNativePath() const
768 return cairo_copy_path(m_pathContext
) ;
771 void wxCairoPath::UnGetNativePath(void *p
)
773 cairo_path_destroy((cairo_path_t
*)p
);
780 void wxCairoPath::MoveToPoint( wxDouble x
, wxDouble y
)
782 cairo_move_to(m_pathContext
,x
,y
);
785 void wxCairoPath::AddLineToPoint( wxDouble x
, wxDouble y
)
787 cairo_line_to(m_pathContext
,x
,y
);
790 void wxCairoPath::CloseSubpath()
792 cairo_close_path(m_pathContext
);
795 void wxCairoPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
797 cairo_curve_to(m_pathContext
,cx1
,cy1
,cx2
,cy2
,x
,y
);
800 // gets the last point of the current path, (0,0) if not yet set
801 void wxCairoPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
804 cairo_get_current_point(m_pathContext
,&dx
,&dy
);
809 void wxCairoPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
811 // as clockwise means positive in our system (y pointing downwards)
812 // TODO make this interpretation dependent of the
814 if ( clockwise
||(endAngle
-startAngle
)>=2*M_PI
)
815 cairo_arc(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
817 cairo_arc_negative(m_pathContext
,x
,y
,r
,startAngle
,endAngle
);
820 // transforms each point of this path by the matrix
821 void wxCairoPath::Transform( wxGraphicsMatrix
* matrix
)
823 // as we don't have a true path object, we have to apply the inverse
824 // matrix to the context
825 cairo_matrix_t m
= *((cairo_matrix_t
*) matrix
->GetNativeMatrix());
826 cairo_matrix_invert( &m
);
827 cairo_transform(m_pathContext
,&m
);
830 // gets the bounding box enclosing all points (possibly including control points)
831 void wxCairoPath::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
)
835 cairo_stroke_extents( m_pathContext
, &x1
, &y1
, &x2
, &y2
);
859 bool wxCairoPath::Contains( wxDouble x
, wxDouble y
, int fillStyle
)
861 return cairo_in_stroke( m_pathContext
, x
, y
) != NULL
;
864 //-----------------------------------------------------------------------------
865 // wxCairoMatrix implementation
866 //-----------------------------------------------------------------------------
868 IMPLEMENT_DYNAMIC_CLASS(wxCairoMatrix
,wxGraphicsMatrix
)
870 wxCairoMatrix::wxCairoMatrix() : wxGraphicsMatrix(NULL
)
872 wxLogDebug(wxT("Illegal Constructor called"));
875 wxCairoMatrix::wxCairoMatrix(wxGraphicsRenderer
* renderer
, const cairo_matrix_t
* matrix
)
876 : wxGraphicsMatrix(renderer
)
882 wxCairoMatrix::~wxCairoMatrix()
887 wxGraphicsMatrix
*wxCairoMatrix::Clone() const
889 return new wxCairoMatrix(GetRenderer(),&m_matrix
);
892 // concatenates the matrix
893 void wxCairoMatrix::Concat( const wxGraphicsMatrix
*t
)
895 cairo_matrix_multiply( &m_matrix
, &m_matrix
, (cairo_matrix_t
*) t
->GetNativeMatrix());
898 // copies the passed in matrix
899 void wxCairoMatrix::Copy( const wxGraphicsMatrix
*t
)
901 m_matrix
= *((cairo_matrix_t
*) t
->GetNativeMatrix());
904 // sets the matrix to the respective values
905 void wxCairoMatrix::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
906 wxDouble tx
, wxDouble ty
)
908 cairo_matrix_init( &m_matrix
, a
, b
, c
, d
, tx
, ty
);
911 // makes this the inverse matrix
912 void wxCairoMatrix::Invert()
914 cairo_matrix_invert( &m_matrix
);
917 // returns true if the elements of the transformation matrix are equal ?
918 bool wxCairoMatrix::IsEqual( const wxGraphicsMatrix
* t
) const
920 const cairo_matrix_t
* tm
= (cairo_matrix_t
*) t
->GetNativeMatrix();
922 m_matrix
.xx
== tm
->xx
&&
923 m_matrix
.yx
== tm
->yx
&&
924 m_matrix
.xy
== tm
->xy
&&
925 m_matrix
.yy
== tm
->yy
&&
926 m_matrix
.x0
== tm
->x0
&&
927 m_matrix
.y0
== tm
->y0
) ;
930 // return true if this is the identity matrix
931 bool wxCairoMatrix::IsIdentity()
933 return ( m_matrix
.xx
== 1 && m_matrix
.yy
== 1 &&
934 m_matrix
.yx
== 0 && m_matrix
.xy
== 0 && m_matrix
.x0
== 0 && m_matrix
.y0
== 0);
941 // add the translation to this matrix
942 void wxCairoMatrix::Translate( wxDouble dx
, wxDouble dy
)
944 cairo_matrix_translate( &m_matrix
, dx
, dy
) ;
947 // add the scale to this matrix
948 void wxCairoMatrix::Scale( wxDouble xScale
, wxDouble yScale
)
950 cairo_matrix_scale( &m_matrix
, xScale
, yScale
) ;
953 // add the rotation to this matrix (radians)
954 void wxCairoMatrix::Rotate( wxDouble angle
)
956 cairo_matrix_rotate( &m_matrix
, angle
) ;
960 // apply the transforms
963 // applies that matrix to the point
964 void wxCairoMatrix::TransformPoint( wxDouble
*x
, wxDouble
*y
)
966 double lx
= *x
, ly
= *y
;
967 cairo_matrix_transform_point( &m_matrix
, &lx
, &ly
);
972 // applies the matrix except for translations
973 void wxCairoMatrix::TransformDistance( wxDouble
*dx
, wxDouble
*dy
)
975 double lx
= *dx
, ly
= *dy
;
976 cairo_matrix_transform_distance( &m_matrix
, &lx
, &ly
);
981 // returns the native representation
982 void * wxCairoMatrix::GetNativeMatrix() const
984 return (void*) &m_matrix
;
987 //-----------------------------------------------------------------------------
988 // wxCairoContext implementation
989 //-----------------------------------------------------------------------------
991 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, const wxWindowDC
& dc
)
992 : wxGraphicsContext(renderer
)
995 m_context
= gdk_cairo_create( dc
.m_window
) ;
1002 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, GdkDrawable
*drawable
)
1003 : wxGraphicsContext(renderer
)
1005 m_context
= gdk_cairo_create( drawable
) ;
1011 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, cairo_t
*context
)
1012 : wxGraphicsContext(renderer
)
1014 m_context
= context
;
1019 wxCairoContext::wxCairoContext( wxGraphicsRenderer
* renderer
, wxWindow
*window
)
1020 : wxGraphicsContext(renderer
)
1023 // something along these lines (copied from dcclient)
1025 GtkWidget
*widget
= window
->m_wxwindow
;
1027 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1028 // code should still be able to create wxClientDCs for them, so we will
1029 // use the parent window here then.
1032 window
= window
->GetParent();
1033 widget
= window
->m_wxwindow
;
1036 wxASSERT_MSG( widget
, wxT("wxCairoContext needs a widget") );
1038 GtkPizza
*pizza
= GTK_PIZZA( widget
);
1039 GdkDrawable
* drawable
= pizza
->bin_window
;
1040 m_context
= gdk_cairo_create( drawable
) ;
1046 wxCairoContext::~wxCairoContext()
1056 cairo_destroy(m_context
);
1061 void wxCairoContext::Clip( const wxRegion
& WXUNUSED(region
) )
1066 void wxCairoContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1071 void wxCairoContext::ResetClip()
1077 void wxCairoContext::StrokePath( const wxGraphicsPath
*path
)
1081 cairo_path_t
* cp
= (cairo_path_t
*) path
->GetNativePath() ;
1082 cairo_append_path(m_context
,cp
);
1084 cairo_stroke(m_context
);
1085 wxConstCast(path
, wxGraphicsPath
)->UnGetNativePath(cp
);
1089 void wxCairoContext::FillPath( const wxGraphicsPath
*path
, int fillStyle
)
1093 cairo_path_t
* cp
= (cairo_path_t
*) path
->GetNativePath() ;
1094 cairo_append_path(m_context
,cp
);
1095 m_brush
->Apply(this);
1096 cairo_set_fill_rule(m_context
,fillStyle
==wxODDEVEN_RULE
? CAIRO_FILL_RULE_EVEN_ODD
: CAIRO_FILL_RULE_WINDING
);
1097 cairo_fill(m_context
);
1098 wxConstCast(path
, wxGraphicsPath
)->UnGetNativePath(cp
);
1102 void wxCairoContext::Rotate( wxDouble angle
)
1104 cairo_rotate(m_context
,angle
);
1107 void wxCairoContext::Translate( wxDouble dx
, wxDouble dy
)
1109 cairo_translate(m_context
,dx
,dy
);
1112 void wxCairoContext::Scale( wxDouble xScale
, wxDouble yScale
)
1114 cairo_scale(m_context
,xScale
,yScale
);
1117 // concatenates this transform with the current transform of this context
1118 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix
* matrix
)
1120 cairo_transform(m_context
,(const cairo_matrix_t
*) matrix
->GetNativeMatrix());
1123 // sets the transform of this context
1124 void wxCairoContext::SetTransform( const wxGraphicsMatrix
* matrix
)
1126 cairo_set_matrix(m_context
,(const cairo_matrix_t
*) matrix
->GetNativeMatrix());
1129 // gets the matrix of this context
1130 void wxCairoContext::GetTransform( wxGraphicsMatrix
* matrix
)
1132 cairo_get_matrix(m_context
,(cairo_matrix_t
*) matrix
->GetNativeMatrix());
1137 void wxCairoContext::PushState()
1139 cairo_save(m_context
);
1142 void wxCairoContext::PopState()
1144 cairo_restore(m_context
);
1147 void wxCairoContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1150 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
1151 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1156 void wxCairoContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1159 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
1160 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1166 void wxCairoContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1168 if ( m_font
== NULL
|| str
.IsEmpty())
1170 cairo_move_to(m_context
,x
,y
);
1171 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
1172 m_font
->Apply(this);
1173 cairo_show_text(m_context
,buf
);
1176 void wxCairoContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1177 wxDouble
*descent
, wxDouble
*externalLeading
) const
1182 void wxCairoContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1185 widths
.Add(0, text
.length());
1193 void * wxCairoContext::GetNativeContext()
1198 //-----------------------------------------------------------------------------
1199 // wxCairoRenderer declaration
1200 //-----------------------------------------------------------------------------
1202 class WXDLLIMPEXP_CORE wxCairoRenderer
: public wxGraphicsRenderer
1205 wxCairoRenderer() {}
1207 virtual ~wxCairoRenderer() {}
1211 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1213 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1215 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1217 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1221 virtual wxGraphicsPath
* CreatePath();
1225 virtual wxGraphicsMatrix
* CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1226 wxDouble tx
=0.0, wxDouble ty
=0.0);
1229 virtual wxGraphicsPen
* CreatePen(const wxPen
& pen
) ;
1231 virtual wxGraphicsBrush
* CreateBrush(const wxBrush
& brush
) ;
1233 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1234 virtual wxGraphicsBrush
* CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1235 const wxColour
&c1
, const wxColour
&c2
) ;
1237 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1238 // with radius r and color cColor
1239 virtual wxGraphicsBrush
* CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1240 const wxColour
&oColor
, const wxColour
&cColor
) ;
1243 virtual wxGraphicsFont
* CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1246 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer
)
1249 //-----------------------------------------------------------------------------
1250 // wxCairoRenderer implementation
1251 //-----------------------------------------------------------------------------
1253 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer
,wxGraphicsRenderer
)
1255 static wxCairoRenderer gs_cairoGraphicsRenderer
;
1258 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1260 return &gs_cairoGraphicsRenderer
;
1264 wxGraphicsContext
* wxCairoRenderer::CreateContext( const wxWindowDC
& dc
)
1266 return new wxCairoContext(this,dc
);
1269 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeContext( void * context
)
1271 return new wxCairoContext(this,(cairo_t
*)context
);
1275 wxGraphicsContext
* wxCairoRenderer::CreateContextFromNativeWindow( void * window
)
1278 return new wxCairoContext(this,(GdkDrawable
*)window
);
1284 wxGraphicsContext
* wxCairoRenderer::CreateContext( wxWindow
* window
)
1286 return new wxCairoContext(this, window
);
1291 wxGraphicsPath
* wxCairoRenderer::CreatePath()
1293 return new wxCairoPath( this );
1299 wxGraphicsMatrix
* wxCairoRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1300 wxDouble tx
, wxDouble ty
)
1303 wxCairoMatrix
* m
= new wxCairoMatrix( this );
1304 m
->Set( a
,b
,c
,d
,tx
,ty
) ;
1308 wxGraphicsPen
* wxCairoRenderer::CreatePen(const wxPen
& pen
)
1310 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1313 return new wxCairoPen( this, pen
);
1316 wxGraphicsBrush
* wxCairoRenderer::CreateBrush(const wxBrush
& brush
)
1318 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1321 return new wxCairoBrush( this, brush
);
1324 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1325 wxGraphicsBrush
* wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1326 const wxColour
&c1
, const wxColour
&c2
)
1328 wxCairoBrush
* brush
= new wxCairoBrush(this);
1329 brush
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1333 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1334 // with radius r and color cColor
1335 wxGraphicsBrush
* wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1336 const wxColour
&oColor
, const wxColour
&cColor
)
1338 wxCairoBrush
* brush
= new wxCairoBrush(this);
1339 brush
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1344 wxGraphicsFont
* wxCairoRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1347 return new wxCairoFont( this , font
, col
);
1352 #endif // wxUSE_GRAPHICS_CONTEXT