]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/graphicc.cpp
Implement support for markup labels for wxGTK wxButton.
[wxWidgets.git] / src / generic / graphicc.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/graphicc.cpp
3// Purpose: cairo device context class
4// Author: Stefan Csomor
5// Modified by:
6// Created: 2006-10-03
7// RCS-ID: $Id$
8// Copyright: (c) 2006 Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18#if wxUSE_GRAPHICS_CONTEXT
19
20#include "wx/graphics.h"
21
22#if wxUSE_CAIRO
23
24#include "wx/cairo.h"
25
26#ifndef WX_PRECOMP
27 #include "wx/bitmap.h"
28 #include "wx/icon.h"
29 #include "wx/dcclient.h"
30 #include "wx/dcmemory.h"
31 #include "wx/dcprint.h"
32 #ifdef __WXGTK__
33 #include "wx/window.h"
34 #endif
35#endif
36
37#include "wx/private/graphics.h"
38#include "wx/rawbmp.h"
39#include "wx/vector.h"
40
41using namespace std;
42
43//-----------------------------------------------------------------------------
44// device context implementation
45//
46// more and more of the dc functionality should be implemented by calling
47// the appropricate wxCairoContext, but we will have to do that step by step
48// also coordinate conversions should be moved to native matrix ops
49//-----------------------------------------------------------------------------
50
51// we always stock two context states, one at entry, to be able to preserve the
52// state we were called with, the other one after changing to HI Graphics orientation
53// (this one is used for getting back clippings etc)
54
55//-----------------------------------------------------------------------------
56// wxGraphicsPath implementation
57//-----------------------------------------------------------------------------
58
59// TODO remove this dependency (gdiplus needs the macros)
60
61#ifndef max
62#define max(a,b) (((a) > (b)) ? (a) : (b))
63#endif
64
65#ifndef min
66#define min(a,b) (((a) < (b)) ? (a) : (b))
67#endif
68
69#include <cairo.h>
70#ifdef __WXMSW__
71#include <cairo-win32.h>
72#endif
73
74#ifdef __WXGTK__
75#include <gtk/gtk.h>
76#include "wx/fontutil.h"
77#include "wx/gtk/dc.h"
78#endif
79
80#ifdef __WXMSW__
81#include <cairo-win32.h>
82#endif
83
84#ifdef __WXMAC__
85#include "wx/osx/private.h"
86#include <cairo-quartz.h>
87#include <cairo-atsui.h>
88#endif
89
90class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData
91{
92public :
93 wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL);
94 ~wxCairoPathData();
95
96 virtual wxGraphicsObjectRefData *Clone() const;
97
98 //
99 // These are the path primitives from which everything else can be constructed
100 //
101
102 // begins a new subpath at (x,y)
103 virtual void MoveToPoint( wxDouble x, wxDouble y );
104
105 // adds a straight line from the current point to (x,y)
106 virtual void AddLineToPoint( wxDouble x, wxDouble y );
107
108 // adds a cubic Bezier curve from the current point, using two control points and an end point
109 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
110
111
112 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
113 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
114
115 // gets the last point of the current path, (0,0) if not yet set
116 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
117
118 // adds another path
119 virtual void AddPath( const wxGraphicsPathData* path );
120
121 // closes the current sub-path
122 virtual void CloseSubpath();
123
124 //
125 // These are convenience functions which - if not available natively will be assembled
126 // using the primitives from above
127 //
128
129 /*
130
131 // appends a rectangle as a new closed subpath
132 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
133 // appends an ellipsis as a new closed subpath fitting the passed rectangle
134 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
135
136 // 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)
137 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
138 */
139
140 // returns the native path
141 virtual void * GetNativePath() const ;
142
143 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
144 virtual void UnGetNativePath(void *p) const;
145
146 // transforms each point of this path by the matrix
147 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
148
149 // gets the bounding box enclosing all points (possibly including control points)
150 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
151
152 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxWINDING_RULE) const;
153
154private :
155 cairo_t* m_pathContext;
156};
157
158class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData
159{
160public :
161 wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
162 virtual ~wxCairoMatrixData() ;
163
164 virtual wxGraphicsObjectRefData *Clone() const ;
165
166 // concatenates the matrix
167 virtual void Concat( const wxGraphicsMatrixData *t );
168
169 // sets the matrix to the respective values
170 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
171 wxDouble tx=0.0, wxDouble ty=0.0);
172
173 // gets the component valuess of the matrix
174 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
175 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
176
177 // makes this the inverse matrix
178 virtual void Invert();
179
180 // returns true if the elements of the transformation matrix are equal ?
181 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
182
183 // return true if this is the identity matrix
184 virtual bool IsIdentity() const;
185
186 //
187 // transformation
188 //
189
190 // add the translation to this matrix
191 virtual void Translate( wxDouble dx , wxDouble dy );
192
193 // add the scale to this matrix
194 virtual void Scale( wxDouble xScale , wxDouble yScale );
195
196 // add the rotation to this matrix (radians)
197 virtual void Rotate( wxDouble angle );
198
199 //
200 // apply the transforms
201 //
202
203 // applies that matrix to the point
204 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
205
206 // applies the matrix except for translations
207 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
208
209 // returns the native representation
210 virtual void * GetNativeMatrix() const;
211private:
212 cairo_matrix_t m_matrix ;
213} ;
214
215class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData
216{
217public:
218 wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
219 ~wxCairoPenData();
220
221 void Init();
222
223 virtual void Apply( wxGraphicsContext* context );
224 virtual wxDouble GetWidth() { return m_width; }
225
226private :
227 double m_width;
228
229 double m_red;
230 double m_green;
231 double m_blue;
232 double m_alpha;
233
234 cairo_line_cap_t m_cap;
235 cairo_line_join_t m_join;
236
237 int m_count;
238 const double *m_lengths;
239 double *m_userLengths;
240
241 wxPen m_pen;
242
243 wxDECLARE_NO_COPY_CLASS(wxCairoPenData);
244};
245
246class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData
247{
248public:
249 wxCairoBrushData( wxGraphicsRenderer* renderer );
250 wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
251 ~wxCairoBrushData ();
252
253 virtual void Apply( wxGraphicsContext* context );
254
255 void CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
256 wxDouble x2, wxDouble y2,
257 const wxGraphicsGradientStops& stops);
258 void CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
259 wxDouble xc, wxDouble yc, wxDouble radius,
260 const wxGraphicsGradientStops& stops);
261
262protected:
263 virtual void Init();
264
265 // common part of Create{Linear,Radial}GradientBrush()
266 void AddGradientStops(const wxGraphicsGradientStops& stops);
267
268private :
269 double m_red;
270 double m_green;
271 double m_blue;
272 double m_alpha;
273
274 cairo_pattern_t* m_brushPattern;
275};
276
277class wxCairoFontData : public wxGraphicsObjectRefData
278{
279public:
280 wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
281 ~wxCairoFontData();
282
283 virtual void Apply( wxGraphicsContext* context );
284#ifdef __WXGTK__
285 const PangoFontDescription* GetFont() const { return m_font; }
286 bool GetUnderlined() const { return m_underlined; }
287#endif
288private :
289 double m_size;
290 bool m_underlined;
291 double m_red;
292 double m_green;
293 double m_blue;
294 double m_alpha;
295#ifdef __WXMAC__
296 cairo_font_face_t *m_font;
297#elif defined(__WXGTK__)
298 PangoFontDescription* m_font;
299#else
300 wxCharBuffer m_fontName;
301 cairo_font_slant_t m_slant;
302 cairo_font_weight_t m_weight;
303#endif
304#ifdef __WXMSW__
305 wxCairoContext( wxGraphicsRenderer* renderer, HDC context );
306#endif
307};
308
309class wxCairoBitmapData : public wxGraphicsObjectRefData
310{
311public:
312 wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp );
313 wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap );
314 ~wxCairoBitmapData();
315
316 virtual cairo_surface_t* GetCairoSurface() { return m_surface; }
317 virtual cairo_pattern_t* GetCairoPattern() { return m_pattern; }
318 virtual wxSize GetSize() { return wxSize(m_width, m_height); }
319private :
320 cairo_surface_t* m_surface;
321 cairo_pattern_t* m_pattern;
322 int m_width;
323 int m_height;
324 unsigned char* m_buffer;
325};
326
327class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
328{
329public:
330 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
331 wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc );
332 wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc );
333#ifdef __WXGTK__
334 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
335#endif
336 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
337 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
338 wxCairoContext();
339 virtual ~wxCairoContext();
340
341 virtual bool ShouldOffset() const
342 {
343 int penwidth = 0 ;
344 if ( !m_pen.IsNull() )
345 {
346 penwidth = (int)((wxCairoPenData*)m_pen.GetRefData())->GetWidth();
347 if ( penwidth == 0 )
348 penwidth = 1;
349 }
350 return ( penwidth % 2 ) == 1;
351 }
352
353 virtual void Clip( const wxRegion &region );
354#ifdef __WXMSW__
355 cairo_surface_t* m_mswSurface;
356#endif
357
358 // clips drawings to the rect
359 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
360
361 // resets the clipping to original extent
362 virtual void ResetClip();
363
364 virtual void * GetNativeContext();
365
366 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
367
368 virtual bool SetCompositionMode(wxCompositionMode op);
369
370 virtual void BeginLayer(wxDouble opacity);
371
372 virtual void EndLayer();
373
374 virtual void StrokePath( const wxGraphicsPath& p );
375 virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxWINDING_RULE );
376
377 virtual void Translate( wxDouble dx , wxDouble dy );
378 virtual void Scale( wxDouble xScale , wxDouble yScale );
379 virtual void Rotate( wxDouble angle );
380
381 // concatenates this transform with the current transform of this context
382 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
383
384 // sets the transform of this context
385 virtual void SetTransform( const wxGraphicsMatrix& matrix );
386
387 // gets the matrix of this context
388 virtual wxGraphicsMatrix GetTransform() const;
389
390 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
391 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
392 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
393 virtual void PushState();
394 virtual void PopState();
395
396 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
397 wxDouble *descent, wxDouble *externalLeading ) const;
398 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
399
400protected:
401 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
402
403private:
404 void Init(cairo_t *context);
405
406 cairo_t* m_context;
407
408 wxVector<float> m_layerOpacities;
409
410 wxDECLARE_NO_COPY_CLASS(wxCairoContext);
411};
412
413//-----------------------------------------------------------------------------
414// wxCairoPenData implementation
415//-----------------------------------------------------------------------------
416
417wxCairoPenData::~wxCairoPenData()
418{
419 delete[] m_userLengths;
420}
421
422void wxCairoPenData::Init()
423{
424 m_lengths = NULL;
425 m_userLengths = NULL;
426 m_width = 0;
427 m_count = 0;
428}
429
430wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
431: wxGraphicsObjectRefData(renderer)
432{
433 Init();
434 m_pen = pen;
435 m_width = m_pen.GetWidth();
436 if (m_width <= 0.0)
437 m_width = 0.1;
438
439 m_red = m_pen.GetColour().Red()/255.0;
440 m_green = m_pen.GetColour().Green()/255.0;
441 m_blue = m_pen.GetColour().Blue()/255.0;
442 m_alpha = m_pen.GetColour().Alpha()/255.0;
443
444 switch ( m_pen.GetCap() )
445 {
446 case wxCAP_ROUND :
447 m_cap = CAIRO_LINE_CAP_ROUND;
448 break;
449
450 case wxCAP_PROJECTING :
451 m_cap = CAIRO_LINE_CAP_SQUARE;
452 break;
453
454 case wxCAP_BUTT :
455 m_cap = CAIRO_LINE_CAP_BUTT;
456 break;
457
458 default :
459 m_cap = CAIRO_LINE_CAP_BUTT;
460 break;
461 }
462
463 switch ( m_pen.GetJoin() )
464 {
465 case wxJOIN_BEVEL :
466 m_join = CAIRO_LINE_JOIN_BEVEL;
467 break;
468
469 case wxJOIN_MITER :
470 m_join = CAIRO_LINE_JOIN_MITER;
471 break;
472
473 case wxJOIN_ROUND :
474 m_join = CAIRO_LINE_JOIN_ROUND;
475 break;
476
477 default :
478 m_join = CAIRO_LINE_JOIN_MITER;
479 break;
480 }
481
482 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
483 const double dotted[] =
484 {
485 dashUnit , dashUnit + 2.0
486 };
487 static const double short_dashed[] =
488 {
489 9.0 , 6.0
490 };
491 static const double dashed[] =
492 {
493 19.0 , 9.0
494 };
495 static const double dotted_dashed[] =
496 {
497 9.0 , 6.0 , 3.0 , 3.0
498 };
499
500 switch ( m_pen.GetStyle() )
501 {
502 case wxPENSTYLE_SOLID :
503 break;
504
505 case wxPENSTYLE_DOT :
506 m_count = WXSIZEOF(dotted);
507 m_userLengths = new double[ m_count ] ;
508 memcpy( m_userLengths, dotted, sizeof(dotted) );
509 m_lengths = m_userLengths;
510 break;
511
512 case wxPENSTYLE_LONG_DASH :
513 m_lengths = dashed ;
514 m_count = WXSIZEOF(dashed);
515 break;
516
517 case wxPENSTYLE_SHORT_DASH :
518 m_lengths = short_dashed ;
519 m_count = WXSIZEOF(short_dashed);
520 break;
521
522 case wxPENSTYLE_DOT_DASH :
523 m_lengths = dotted_dashed ;
524 m_count = WXSIZEOF(dotted_dashed);
525 break;
526
527 case wxPENSTYLE_USER_DASH :
528 {
529 wxDash *wxdashes ;
530 m_count = m_pen.GetDashes( &wxdashes ) ;
531 if ((wxdashes != NULL) && (m_count > 0))
532 {
533 m_userLengths = new double[m_count] ;
534 for ( int i = 0 ; i < m_count ; ++i )
535 {
536 m_userLengths[i] = wxdashes[i] * dashUnit ;
537
538 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
539 m_userLengths[i] = dashUnit + 2.0 ;
540 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
541 m_userLengths[i] = dashUnit ;
542 }
543 }
544 m_lengths = m_userLengths ;
545 }
546 break;
547 case wxPENSTYLE_STIPPLE :
548 {
549 /*
550 wxBitmap* bmp = pen.GetStipple();
551 if ( bmp && bmp->Ok() )
552 {
553 wxDELETE( m_penImage );
554 wxDELETE( m_penBrush );
555 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
556 m_penBrush = new TextureBrush(m_penImage);
557 m_pen->SetBrush( m_penBrush );
558 }
559 */
560 }
561 break;
562 default :
563 if ( m_pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH
564 && m_pen.GetStyle() <= wxPENSTYLE_LAST_HATCH )
565 {
566 /*
567 wxDELETE( m_penBrush );
568 HatchStyle style = HatchStyleHorizontal;
569 switch( pen.GetStyle() )
570 {
571 case wxPENSTYLE_BDIAGONAL_HATCH :
572 style = HatchStyleBackwardDiagonal;
573 break ;
574 case wxPENSTYLE_CROSSDIAG_HATCH :
575 style = HatchStyleDiagonalCross;
576 break ;
577 case wxPENSTYLE_FDIAGONAL_HATCH :
578 style = HatchStyleForwardDiagonal;
579 break ;
580 case wxPENSTYLE_CROSS_HATCH :
581 style = HatchStyleCross;
582 break ;
583 case wxPENSTYLE_HORIZONTAL_HATCH :
584 style = HatchStyleHorizontal;
585 break ;
586 case wxPENSTYLE_VERTICAL_HATCH :
587 style = HatchStyleVertical;
588 break ;
589
590 }
591 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
592 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
593 m_pen->SetBrush( m_penBrush )
594 */
595 }
596 break;
597 }
598}
599
600void wxCairoPenData::Apply( wxGraphicsContext* context )
601{
602 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
603 cairo_set_line_width(ctext,m_width);
604 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
605 cairo_set_line_cap(ctext,m_cap);
606 cairo_set_line_join(ctext,m_join);
607 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
608}
609
610//-----------------------------------------------------------------------------
611// wxCairoBrushData implementation
612//-----------------------------------------------------------------------------
613
614wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
615 : wxGraphicsObjectRefData( renderer )
616{
617 Init();
618}
619
620wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
621 : wxGraphicsObjectRefData(renderer)
622{
623 Init();
624
625 m_red = brush.GetColour().Red()/255.0;
626 m_green = brush.GetColour().Green()/255.0;
627 m_blue = brush.GetColour().Blue()/255.0;
628 m_alpha = brush.GetColour().Alpha()/255.0;
629 /*
630 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
631 {
632 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
633 brush.GetColour().Green() , brush.GetColour().Blue() ) );
634 }
635 else if ( brush.IsHatch() )
636 {
637 HatchStyle style = HatchStyleHorizontal;
638 switch( brush.GetStyle() )
639 {
640 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
641 style = HatchStyleBackwardDiagonal;
642 break ;
643 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
644 style = HatchStyleDiagonalCross;
645 break ;
646 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
647 style = HatchStyleForwardDiagonal;
648 break ;
649 case wxBRUSHSTYLE_CROSS_HATCH :
650 style = HatchStyleCross;
651 break ;
652 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
653 style = HatchStyleHorizontal;
654 break ;
655 case wxBRUSHSTYLE_VERTICAL_HATCH :
656 style = HatchStyleVertical;
657 break ;
658
659 }
660 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
661 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
662 }
663 else
664 {
665 wxBitmap* bmp = brush.GetStipple();
666 if ( bmp && bmp->Ok() )
667 {
668 wxDELETE( m_brushImage );
669 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
670 m_brush = new TextureBrush(m_brushImage);
671 }
672 }
673 */
674}
675
676wxCairoBrushData::~wxCairoBrushData ()
677{
678 if (m_brushPattern)
679 cairo_pattern_destroy(m_brushPattern);
680}
681
682void wxCairoBrushData::Apply( wxGraphicsContext* context )
683{
684 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
685 if ( m_brushPattern )
686 {
687 cairo_set_source(ctext,m_brushPattern);
688 }
689 else
690 {
691 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
692 }
693}
694
695void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops& stops)
696{
697 // loop over all the stops, they include the beginning and ending ones
698 const unsigned numStops = stops.GetCount();
699 for ( unsigned n = 0; n < numStops; n++ )
700 {
701 const wxGraphicsGradientStop stop = stops.Item(n);
702
703 const wxColour col = stop.GetColour();
704
705 cairo_pattern_add_color_stop_rgba
706 (
707 m_brushPattern,
708 stop.GetPosition(),
709 col.Red()/255.0,
710 col.Green()/255.0,
711 col.Blue()/255.0,
712 col.Alpha()/255.0
713 );
714 }
715
716 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS,
717 wxT("Couldn't create cairo pattern"));
718}
719
720void
721wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
722 wxDouble x2, wxDouble y2,
723 const wxGraphicsGradientStops& stops)
724{
725 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
726
727 AddGradientStops(stops);
728}
729
730void
731wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
732 wxDouble xc, wxDouble yc,
733 wxDouble radius,
734 const wxGraphicsGradientStops& stops)
735{
736 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
737
738 AddGradientStops(stops);
739}
740
741void wxCairoBrushData::Init()
742{
743 m_brushPattern = NULL;
744}
745
746//-----------------------------------------------------------------------------
747// wxCairoFontData implementation
748//-----------------------------------------------------------------------------
749
750wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
751 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
752{
753 m_red = col.Red()/255.0;
754 m_green = col.Green()/255.0;
755 m_blue = col.Blue()/255.0;
756 m_alpha = col.Alpha()/255.0;
757 m_size = font.GetPointSize();
758 m_underlined = font.GetUnderlined();
759
760#ifdef __WXMAC__
761 m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() );
762#elif defined(__WXGTK__)
763 m_font = pango_font_description_copy( font.GetNativeFontInfo()->description );
764#else
765 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
766 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
767 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
768#endif
769}
770
771wxCairoFontData::~wxCairoFontData()
772{
773#ifdef __WXMAC__
774 cairo_font_face_destroy( m_font );
775#elif defined(__WXGTK__)
776 pango_font_description_free( m_font );
777#else
778#endif
779}
780
781void wxCairoFontData::Apply( wxGraphicsContext* context )
782{
783 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
784 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
785#ifdef __WXGTK__
786 // the rest is done using Pango layouts
787#elif defined(__WXMAC__)
788 cairo_set_font_face(ctext, m_font);
789 cairo_set_font_size(ctext, m_size );
790#else
791 cairo_select_font_face(ctext, m_fontName, m_slant, m_weight );
792 cairo_set_font_size(ctext, m_size );
793#endif
794}
795
796//-----------------------------------------------------------------------------
797// wxCairoPathData implementation
798//-----------------------------------------------------------------------------
799
800wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
801 : wxGraphicsPathData(renderer)
802{
803 if (pathcontext)
804 {
805 m_pathContext = pathcontext;
806 }
807 else
808 {
809 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
810 m_pathContext = cairo_create(surface);
811 cairo_surface_destroy (surface);
812 }
813}
814
815wxCairoPathData::~wxCairoPathData()
816{
817 cairo_destroy(m_pathContext);
818}
819
820wxGraphicsObjectRefData *wxCairoPathData::Clone() const
821{
822 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
823 cairo_t* pathcontext = cairo_create(surface);
824 cairo_surface_destroy (surface);
825
826 cairo_path_t* path = cairo_copy_path(m_pathContext);
827 cairo_append_path(pathcontext, path);
828 cairo_path_destroy(path);
829 return new wxCairoPathData( GetRenderer() ,pathcontext);
830}
831
832
833void* wxCairoPathData::GetNativePath() const
834{
835 return cairo_copy_path(m_pathContext) ;
836}
837
838void wxCairoPathData::UnGetNativePath(void *p) const
839{
840 cairo_path_destroy((cairo_path_t*)p);
841}
842
843//
844// The Primitives
845//
846
847void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
848{
849 cairo_move_to(m_pathContext,x,y);
850}
851
852void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
853{
854 cairo_line_to(m_pathContext,x,y);
855}
856
857void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
858{
859 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
860 cairo_append_path(m_pathContext, p);
861 UnGetNativePath(p);
862}
863
864void wxCairoPathData::CloseSubpath()
865{
866 cairo_close_path(m_pathContext);
867}
868
869void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
870{
871 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
872}
873
874// gets the last point of the current path, (0,0) if not yet set
875void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
876{
877 double dx,dy;
878 cairo_get_current_point(m_pathContext,&dx,&dy);
879 if (x)
880 *x = dx;
881 if (y)
882 *y = dy;
883}
884
885void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
886{
887 // as clockwise means positive in our system (y pointing downwards)
888 // TODO make this interpretation dependent of the
889 // real device trans
890 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
891 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
892 else
893 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
894}
895
896// transforms each point of this path by the matrix
897void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
898{
899 // as we don't have a true path object, we have to apply the inverse
900 // matrix to the context
901 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
902 cairo_matrix_invert( &m );
903 cairo_transform(m_pathContext,&m);
904}
905
906// gets the bounding box enclosing all points (possibly including control points)
907void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
908{
909 double x1,y1,x2,y2;
910
911 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
912 if ( x2 < x1 )
913 {
914 *x = x2;
915 *w = x1-x2;
916 }
917 else
918 {
919 *x = x1;
920 *w = x2-x1;
921 }
922
923 if( y2 < y1 )
924 {
925 *y = y2;
926 *h = y1-y2;
927 }
928 else
929 {
930 *y = y1;
931 *h = y2-y1;
932 }
933}
934
935bool wxCairoPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
936{
937 cairo_set_fill_rule(m_pathContext,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
938 return cairo_in_fill( m_pathContext, x, y) != 0;
939}
940
941//-----------------------------------------------------------------------------
942// wxCairoMatrixData implementation
943//-----------------------------------------------------------------------------
944
945wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
946 : wxGraphicsMatrixData(renderer)
947{
948 if ( matrix )
949 m_matrix = *matrix;
950}
951
952wxCairoMatrixData::~wxCairoMatrixData()
953{
954 // nothing to do
955}
956
957wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
958{
959 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
960}
961
962// concatenates the matrix
963void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
964{
965 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
966}
967
968// sets the matrix to the respective values
969void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
970 wxDouble tx, wxDouble ty)
971{
972 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
973}
974
975// gets the component valuess of the matrix
976void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
977 wxDouble* d, wxDouble* tx, wxDouble* ty) const
978{
979 if (a) *a = m_matrix.xx;
980 if (b) *b = m_matrix.yx;
981 if (c) *c = m_matrix.xy;
982 if (d) *d = m_matrix.yy;
983 if (tx) *tx= m_matrix.x0;
984 if (ty) *ty= m_matrix.y0;
985}
986
987// makes this the inverse matrix
988void wxCairoMatrixData::Invert()
989{
990 cairo_matrix_invert( &m_matrix );
991}
992
993// returns true if the elements of the transformation matrix are equal ?
994bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
995{
996 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
997 return (
998 m_matrix.xx == tm->xx &&
999 m_matrix.yx == tm->yx &&
1000 m_matrix.xy == tm->xy &&
1001 m_matrix.yy == tm->yy &&
1002 m_matrix.x0 == tm->x0 &&
1003 m_matrix.y0 == tm->y0 ) ;
1004}
1005
1006// return true if this is the identity matrix
1007bool wxCairoMatrixData::IsIdentity() const
1008{
1009 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
1010 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
1011}
1012
1013//
1014// transformation
1015//
1016
1017// add the translation to this matrix
1018void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
1019{
1020 cairo_matrix_translate( &m_matrix, dx, dy) ;
1021}
1022
1023// add the scale to this matrix
1024void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
1025{
1026 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
1027}
1028
1029// add the rotation to this matrix (radians)
1030void wxCairoMatrixData::Rotate( wxDouble angle )
1031{
1032 cairo_matrix_rotate( &m_matrix, angle) ;
1033}
1034
1035//
1036// apply the transforms
1037//
1038
1039// applies that matrix to the point
1040void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
1041{
1042 double lx = *x, ly = *y ;
1043 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
1044 *x = lx;
1045 *y = ly;
1046}
1047
1048// applies the matrix except for translations
1049void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1050{
1051 double lx = *dx, ly = *dy ;
1052 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
1053 *dx = lx;
1054 *dy = ly;
1055}
1056
1057// returns the native representation
1058void * wxCairoMatrixData::GetNativeMatrix() const
1059{
1060 return (void*) &m_matrix;
1061}
1062
1063// wxCairoBitmap implementation
1064//-----------------------------------------------------------------------------
1065
1066wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ) :
1067 wxGraphicsObjectRefData( renderer )
1068{
1069 m_surface = bitmap;
1070 m_pattern = cairo_pattern_create_for_surface(m_surface);
1071}
1072
1073wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ) : wxGraphicsObjectRefData( renderer )
1074{
1075 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1076
1077#ifdef wxHAS_RAW_BITMAP
1078 int bw = m_width = bmp.GetWidth();
1079 int bh = m_height = bmp.GetHeight();
1080 wxBitmap bmpSource = bmp; // we need a non-const instance
1081 m_buffer = new unsigned char[bw*bh*4];
1082 wxUint32* data = (wxUint32*)m_buffer;
1083
1084 // Create a surface object and copy the bitmap pixel data to it. if the
1085 // image has alpha (or a mask represented as alpha) then we'll use a
1086 // different format and iterator than if it doesn't...
1087 if (bmpSource.HasAlpha() || bmpSource.GetMask())
1088 {
1089 m_surface = cairo_image_surface_create_for_data(
1090 m_buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1091 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1092 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1093
1094 wxAlphaPixelData::Iterator p(pixData);
1095 for (int y=0; y<bh; y++)
1096 {
1097 wxAlphaPixelData::Iterator rowStart = p;
1098 for (int x=0; x<bw; x++)
1099 {
1100 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1101 // with alpha in the upper 8 bits, then red, then green, then
1102 // blue. The 32-bit quantities are stored native-endian.
1103 // Pre-multiplied alpha is used.
1104 unsigned char alpha = p.Alpha();
1105 if (alpha == 0)
1106 *data = 0;
1107 else
1108 *data = ( alpha << 24
1109 | (p.Red() * alpha/255) << 16
1110 | (p.Green() * alpha/255) << 8
1111 | (p.Blue() * alpha/255) );
1112 ++data;
1113 ++p;
1114 }
1115 p = rowStart;
1116 p.OffsetY(pixData, 1);
1117 }
1118 }
1119 else // no alpha
1120 {
1121 m_surface = cairo_image_surface_create_for_data(
1122 m_buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1123 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1124 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1125
1126 wxNativePixelData::Iterator p(pixData);
1127 for (int y=0; y<bh; y++)
1128 {
1129 wxNativePixelData::Iterator rowStart = p;
1130 for (int x=0; x<bw; x++)
1131 {
1132 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1133 // the upper 8 bits unused. Red, Green, and Blue are stored in
1134 // the remaining 24 bits in that order. The 32-bit quantities
1135 // are stored native-endian.
1136 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1137 ++data;
1138 ++p;
1139 }
1140 p = rowStart;
1141 p.OffsetY(pixData, 1);
1142 }
1143 }
1144 m_pattern = cairo_pattern_create_for_surface(m_surface);
1145#endif // wxHAS_RAW_BITMAP
1146}
1147
1148wxCairoBitmapData::~wxCairoBitmapData()
1149{
1150 if (m_pattern)
1151 cairo_pattern_destroy(m_pattern);
1152
1153 if (m_surface)
1154 cairo_surface_destroy(m_surface);
1155
1156 delete [] m_buffer;
1157}
1158
1159
1160
1161//-----------------------------------------------------------------------------
1162// wxCairoContext implementation
1163//-----------------------------------------------------------------------------
1164
1165class wxCairoOffsetHelper
1166{
1167public :
1168 wxCairoOffsetHelper( cairo_t* ctx , bool offset )
1169 {
1170 m_ctx = ctx;
1171 m_offset = offset;
1172 if ( m_offset )
1173 cairo_translate( m_ctx, 0.5, 0.5 );
1174 }
1175 ~wxCairoOffsetHelper( )
1176 {
1177 if ( m_offset )
1178 cairo_translate( m_ctx, -0.5, -0.5 );
1179 }
1180public :
1181 cairo_t* m_ctx;
1182 bool m_offset;
1183} ;
1184
1185wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc )
1186: wxGraphicsContext(renderer)
1187{
1188#ifdef __WXGTK20__
1189 const wxDCImpl *impl = dc.GetImpl();
1190 Init( (cairo_t*) impl->GetCairoContext() );
1191
1192 wxPoint org = dc.GetDeviceOrigin();
1193 cairo_translate( m_context, org.x, org.y );
1194
1195 double sx,sy;
1196 dc.GetUserScale( &sx, &sy );
1197 cairo_scale( m_context, sx, sy );
1198
1199 org = dc.GetLogicalOrigin();
1200 cairo_translate( m_context, -org.x, -org.y );
1201#endif
1202}
1203
1204wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1205: wxGraphicsContext(renderer)
1206{
1207#ifdef __WXGTK20__
1208 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1209 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1210
1211#if 0
1212 wxGraphicsMatrix matrix = CreateMatrix();
1213
1214 wxPoint org = dc.GetDeviceOrigin();
1215 matrix.Translate( org.x, org.y );
1216
1217 org = dc.GetLogicalOrigin();
1218 matrix.Translate( -org.x, -org.y );
1219
1220 double sx,sy;
1221 dc.GetUserScale( &sx, &sy );
1222 matrix.Scale( sx, sy );
1223
1224 ConcatTransform( matrix );
1225#endif
1226#endif
1227
1228#ifdef __WXMAC__
1229 int width, height;
1230 dc.GetSize( &width, &height );
1231 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1232 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1233 Init( cairo_create( surface ) );
1234 cairo_surface_destroy( surface );
1235#endif
1236}
1237
1238wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc )
1239: wxGraphicsContext(renderer)
1240{
1241#ifdef __WXGTK20__
1242 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1243 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1244
1245#if 0
1246 wxGraphicsMatrix matrix = CreateMatrix();
1247
1248 wxPoint org = dc.GetDeviceOrigin();
1249 matrix.Translate( org.x, org.y );
1250
1251 org = dc.GetLogicalOrigin();
1252 matrix.Translate( -org.x, -org.y );
1253
1254 double sx,sy;
1255 dc.GetUserScale( &sx, &sy );
1256 matrix.Scale( sx, sy );
1257
1258 ConcatTransform( matrix );
1259#endif
1260#endif
1261
1262#ifdef __WXMAC__
1263 int width, height;
1264 dc.GetSize( &width, &height );
1265 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1266 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1267 Init( cairo_create( surface ) );
1268 cairo_surface_destroy( surface );
1269#endif
1270}
1271
1272#ifdef __WXGTK20__
1273wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1274: wxGraphicsContext(renderer)
1275{
1276 Init( gdk_cairo_create( drawable ) );
1277}
1278#endif
1279
1280#ifdef __WXMSW__
1281wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, HDC handle )
1282: wxGraphicsContext(renderer)
1283{
1284 m_mswSurface = cairo_win32_surface_create(handle);
1285 m_context = cairo_create(m_mswSurface);
1286 PushState();
1287 PushState();
1288}
1289#endif
1290
1291
1292wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1293: wxGraphicsContext(renderer)
1294{
1295 Init( context );
1296}
1297
1298wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1299: wxGraphicsContext(renderer)
1300{
1301#ifdef __WXGTK__
1302 // something along these lines (copied from dcclient)
1303
1304 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1305 // code should still be able to create wxClientDCs for them, so we will
1306 // use the parent window here then.
1307 if (window->m_wxwindow == NULL)
1308 {
1309 window = window->GetParent();
1310 }
1311
1312 wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
1313
1314 Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
1315#endif
1316}
1317
1318wxCairoContext::~wxCairoContext()
1319{
1320 if ( m_context )
1321 {
1322 PopState();
1323#ifdef __WXMSW__
1324 m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle());
1325 m_context = cairo_create(m_mswSurface);
1326#endif
1327 PopState();
1328 cairo_destroy(m_context);
1329 }
1330#ifdef __WXMSW__
1331 if ( m_mswSurface )
1332 cairo_surface_destroy(m_mswSurface);
1333#endif
1334}
1335
1336void wxCairoContext::Init(cairo_t *context)
1337{
1338 m_context = context ;
1339 PushState();
1340 PushState();
1341}
1342
1343
1344void wxCairoContext::Clip( const wxRegion& region )
1345{
1346 // Create a path with all the rectangles in the region
1347 wxGraphicsPath path = GetRenderer()->CreatePath();
1348 wxRegionIterator ri(region);
1349 while (ri)
1350 {
1351 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
1352 ++ri;
1353 }
1354
1355 // Put it in the context
1356 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1357 cairo_append_path(m_context, cp);
1358
1359 // clip to that path
1360 cairo_clip(m_context);
1361 path.UnGetNativePath(cp);
1362}
1363
1364void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1365{
1366 // Create a path with this rectangle
1367 wxGraphicsPath path = GetRenderer()->CreatePath();
1368 path.AddRectangle(x,y,w,h);
1369
1370 // Put it in the context
1371 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1372 cairo_append_path(m_context, cp);
1373
1374 // clip to that path
1375 cairo_clip(m_context);
1376 path.UnGetNativePath(cp);
1377}
1378
1379void wxCairoContext::ResetClip()
1380{
1381 cairo_reset_clip(m_context);
1382}
1383
1384
1385void wxCairoContext::StrokePath( const wxGraphicsPath& path )
1386{
1387 if ( !m_pen.IsNull() )
1388 {
1389 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1390 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1391 cairo_append_path(m_context,cp);
1392 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1393 cairo_stroke(m_context);
1394 path.UnGetNativePath(cp);
1395 }
1396}
1397
1398void wxCairoContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
1399{
1400 if ( !m_brush.IsNull() )
1401 {
1402 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1403 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1404 cairo_append_path(m_context,cp);
1405 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1406 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1407 cairo_fill(m_context);
1408 path.UnGetNativePath(cp);
1409 }
1410}
1411
1412void wxCairoContext::Rotate( wxDouble angle )
1413{
1414 cairo_rotate(m_context,angle);
1415}
1416
1417void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1418{
1419 cairo_translate(m_context,dx,dy);
1420}
1421
1422void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1423{
1424 cairo_scale(m_context,xScale,yScale);
1425}
1426
1427// concatenates this transform with the current transform of this context
1428void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1429{
1430 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
1431}
1432
1433// sets the transform of this context
1434void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
1435{
1436 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
1437}
1438
1439// gets the matrix of this context
1440wxGraphicsMatrix wxCairoContext::GetTransform() const
1441{
1442 wxGraphicsMatrix matrix = CreateMatrix();
1443 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1444 return matrix;
1445}
1446
1447
1448
1449void wxCairoContext::PushState()
1450{
1451 cairo_save(m_context);
1452}
1453
1454void wxCairoContext::PopState()
1455{
1456 cairo_restore(m_context);
1457}
1458
1459void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1460{
1461 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1462 DrawBitmap(bitmap, x, y, w, h);
1463
1464}
1465
1466void wxCairoContext::DrawBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1467{
1468 PushState();
1469
1470 // In case we're scaling the image by using a width and height different
1471 // than the bitmap's size create a pattern transformation on the surface and
1472 // draw the transformed pattern.
1473 wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData());
1474 cairo_pattern_t* pattern = data->GetCairoPattern();
1475 wxSize size = data->GetSize();
1476
1477 wxDouble scaleX = w / size.GetWidth();
1478 wxDouble scaleY = h / size.GetHeight();
1479
1480 // prepare to draw the image
1481 cairo_translate(m_context, x, y);
1482 cairo_scale(m_context, scaleX, scaleY);
1483 cairo_set_source(m_context, pattern);
1484 // use the original size here since the context is scaled already...
1485 cairo_rectangle(m_context, 0, 0, size.GetWidth(), size.GetHeight());
1486 // fill the rectangle using the pattern
1487 cairo_fill(m_context);
1488
1489 PopState();
1490}
1491
1492void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1493{
1494 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1495 // start using the Cairo backend on other platforms then we may need to
1496 // fiddle with this...
1497 DrawBitmap(icon, x, y, w, h);
1498}
1499
1500
1501void wxCairoContext::DoDrawText(const wxString& str, wxDouble x, wxDouble y)
1502{
1503 wxCHECK_RET( !m_font.IsNull(),
1504 wxT("wxCairoContext::DrawText - no valid font set") );
1505
1506 if ( str.empty())
1507 return;
1508
1509 const wxCharBuffer data = str.utf8_str();
1510 if ( !data )
1511 return;
1512
1513 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1514
1515#ifdef __WXGTK__
1516 size_t datalen = strlen(data);
1517
1518 PangoLayout *layout = pango_cairo_create_layout (m_context);
1519 wxCairoFontData* font_data = (wxCairoFontData*) m_font.GetRefData();
1520 pango_layout_set_font_description( layout, font_data->GetFont());
1521 pango_layout_set_text(layout, data, datalen);
1522
1523 if (font_data->GetUnderlined())
1524 {
1525 PangoAttrList *attrs = pango_attr_list_new();
1526 PangoAttribute *attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1527 pango_attr_list_insert(attrs, attr);
1528 pango_layout_set_attributes(layout, attrs);
1529 pango_attr_list_unref(attrs);
1530 }
1531
1532 cairo_move_to(m_context, x, y);
1533 pango_cairo_show_layout (m_context, layout);
1534
1535 g_object_unref (layout);
1536#else
1537 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1538 // the position we move to by the ascent.
1539 cairo_font_extents_t fe;
1540 cairo_font_extents(m_context, &fe);
1541 cairo_move_to(m_context, x, y+fe.ascent);
1542
1543 cairo_show_text(m_context, data);
1544#endif
1545}
1546
1547void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1548 wxDouble *descent, wxDouble *externalLeading ) const
1549{
1550 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1551
1552 if ( width )
1553 *width = 0;
1554 if ( height )
1555 *height = 0;
1556 if ( descent )
1557 *descent = 0;
1558 if ( externalLeading )
1559 *externalLeading = 0;
1560
1561 if ( str.empty())
1562 return;
1563
1564#ifdef __WXGTK__
1565 int w, h;
1566
1567 PangoLayout *layout = pango_cairo_create_layout (m_context);
1568 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1569 const wxCharBuffer data = str.utf8_str();
1570 if ( !data )
1571 {
1572 return;
1573 }
1574 pango_layout_set_text( layout, data, strlen(data) );
1575 pango_layout_get_pixel_size (layout, &w, &h);
1576 if ( width )
1577 *width = w;
1578 if ( height )
1579 *height = h;
1580 if (descent)
1581 {
1582 PangoLayoutIter *iter = pango_layout_get_iter(layout);
1583 int baseline = pango_layout_iter_get_baseline(iter);
1584 pango_layout_iter_free(iter);
1585 *descent = h - PANGO_PIXELS(baseline);
1586 }
1587 g_object_unref (layout);
1588#else
1589 ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
1590
1591 if (width)
1592 {
1593 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1594 cairo_text_extents_t te;
1595 cairo_text_extents(m_context, buf, &te);
1596 *width = te.width;
1597 }
1598
1599 if (height || descent || externalLeading)
1600 {
1601 cairo_font_extents_t fe;
1602 cairo_font_extents(m_context, &fe);
1603
1604 // some backends have negative descents
1605
1606 if ( fe.descent < 0 )
1607 fe.descent = -fe.descent;
1608
1609 if ( fe.height < (fe.ascent + fe.descent ) )
1610 {
1611 // some backends are broken re height ... (eg currently ATSUI)
1612 fe.height = fe.ascent + fe.descent;
1613 }
1614
1615 if (height)
1616 *height = fe.height;
1617 if ( descent )
1618 *descent = fe.descent;
1619 if ( externalLeading )
1620 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
1621 }
1622#endif
1623}
1624
1625void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1626{
1627 widths.Empty();
1628 widths.Add(0, text.length());
1629
1630 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1631
1632 if (text.empty())
1633 return;
1634
1635 // TODO
1636}
1637
1638void * wxCairoContext::GetNativeContext()
1639{
1640 return m_context;
1641}
1642
1643bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias)
1644{
1645 if (m_antialias == antialias)
1646 return true;
1647
1648 m_antialias = antialias;
1649
1650 cairo_antialias_t antialiasMode;
1651 switch (antialias)
1652 {
1653 case wxANTIALIAS_DEFAULT:
1654 antialiasMode = CAIRO_ANTIALIAS_DEFAULT;
1655 break;
1656 case wxANTIALIAS_NONE:
1657 antialiasMode = CAIRO_ANTIALIAS_NONE;
1658 break;
1659 default:
1660 return false;
1661 }
1662 cairo_set_antialias(m_context, antialiasMode);
1663 return true;
1664}
1665
1666bool wxCairoContext::SetCompositionMode(wxCompositionMode op)
1667{
1668 if ( m_composition == op )
1669 return true;
1670
1671 m_composition = op;
1672 cairo_operator_t cop;
1673 switch (op)
1674 {
1675 case wxCOMPOSITION_CLEAR:
1676 cop = CAIRO_OPERATOR_CLEAR;
1677 break;
1678 case wxCOMPOSITION_SOURCE:
1679 cop = CAIRO_OPERATOR_SOURCE;
1680 break;
1681 case wxCOMPOSITION_OVER:
1682 cop = CAIRO_OPERATOR_OVER;
1683 break;
1684 case wxCOMPOSITION_IN:
1685 cop = CAIRO_OPERATOR_IN;
1686 break;
1687 case wxCOMPOSITION_OUT:
1688 cop = CAIRO_OPERATOR_OUT;
1689 break;
1690 case wxCOMPOSITION_ATOP:
1691 cop = CAIRO_OPERATOR_ATOP;
1692 break;
1693 case wxCOMPOSITION_DEST:
1694 cop = CAIRO_OPERATOR_DEST;
1695 break;
1696 case wxCOMPOSITION_DEST_OVER:
1697 cop = CAIRO_OPERATOR_DEST_OVER;
1698 break;
1699 case wxCOMPOSITION_DEST_IN:
1700 cop = CAIRO_OPERATOR_DEST_IN;
1701 break;
1702 case wxCOMPOSITION_DEST_OUT:
1703 cop = CAIRO_OPERATOR_DEST_OUT;
1704 break;
1705 case wxCOMPOSITION_DEST_ATOP:
1706 cop = CAIRO_OPERATOR_DEST_ATOP;
1707 break;
1708 case wxCOMPOSITION_XOR:
1709 cop = CAIRO_OPERATOR_XOR;
1710 break;
1711 case wxCOMPOSITION_ADD:
1712 cop = CAIRO_OPERATOR_ADD;
1713 break;
1714 default:
1715 return false;
1716 }
1717 cairo_set_operator(m_context, cop);
1718 return true;
1719}
1720
1721void wxCairoContext::BeginLayer(wxDouble opacity)
1722{
1723 m_layerOpacities.push_back(opacity);
1724 cairo_push_group(m_context);
1725}
1726
1727void wxCairoContext::EndLayer()
1728{
1729 float opacity = m_layerOpacities.back();
1730 m_layerOpacities.pop_back();
1731 cairo_pop_group_to_source(m_context);
1732 cairo_paint_with_alpha(m_context,opacity);
1733}
1734
1735//-----------------------------------------------------------------------------
1736// wxCairoRenderer declaration
1737//-----------------------------------------------------------------------------
1738
1739class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1740{
1741public :
1742 wxCairoRenderer() {}
1743
1744 virtual ~wxCairoRenderer() {}
1745
1746 // Context
1747
1748 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1749 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1750 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
1751
1752 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1753
1754 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1755
1756 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1757
1758 virtual wxGraphicsContext * CreateMeasuringContext();
1759
1760 // Path
1761
1762 virtual wxGraphicsPath CreatePath();
1763
1764 // Matrix
1765
1766 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1767 wxDouble tx=0.0, wxDouble ty=0.0);
1768
1769
1770 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1771
1772 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1773
1774 virtual wxGraphicsBrush
1775 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
1776 wxDouble x2, wxDouble y2,
1777 const wxGraphicsGradientStops& stops);
1778
1779 virtual wxGraphicsBrush
1780 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
1781 wxDouble xc, wxDouble yc,
1782 wxDouble radius,
1783 const wxGraphicsGradientStops& stops);
1784
1785 // sets the font
1786 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1787
1788 // create a native bitmap representation
1789 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
1790
1791 // create a graphics bitmap from a native bitmap
1792 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
1793
1794 // create a subimage from a native image representation
1795 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1796
1797private :
1798 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1799} ;
1800
1801//-----------------------------------------------------------------------------
1802// wxCairoRenderer implementation
1803//-----------------------------------------------------------------------------
1804
1805IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1806
1807static wxCairoRenderer gs_cairoGraphicsRenderer;
1808// temporary hack to allow creating a cairo context on any platform
1809extern wxGraphicsRenderer* gCairoRenderer;
1810wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer;
1811
1812wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1813{
1814 return new wxCairoContext(this,dc);
1815}
1816
1817wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
1818{
1819 return new wxCairoContext(this,dc);
1820}
1821
1822wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc)
1823{
1824#ifdef __WXGTK20__
1825 const wxDCImpl *impl = dc.GetImpl();
1826 cairo_t* context = (cairo_t*) impl->GetCairoContext();
1827 if (context)
1828 return new wxCairoContext(this,dc);
1829 else
1830#endif
1831 return NULL;
1832}
1833
1834wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1835{
1836#ifdef __WXMSW__
1837 return new wxCairoContext(this,(HDC)context);
1838#else
1839 return new wxCairoContext(this,(cairo_t*)context);
1840#endif
1841}
1842
1843
1844wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1845{
1846#ifdef __WXGTK__
1847 return new wxCairoContext(this,(GdkDrawable*)window);
1848#else
1849 return NULL;
1850#endif
1851}
1852
1853wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
1854{
1855#ifdef __WXGTK__
1856 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1857#endif
1858 return NULL;
1859 // TODO
1860}
1861
1862wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1863{
1864 return new wxCairoContext(this, window );
1865}
1866
1867// Path
1868
1869wxGraphicsPath wxCairoRenderer::CreatePath()
1870{
1871 wxGraphicsPath path;
1872 path.SetRefData( new wxCairoPathData(this) );
1873 return path;
1874}
1875
1876
1877// Matrix
1878
1879wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1880 wxDouble tx, wxDouble ty)
1881
1882{
1883 wxGraphicsMatrix m;
1884 wxCairoMatrixData* data = new wxCairoMatrixData( this );
1885 data->Set( a,b,c,d,tx,ty ) ;
1886 m.SetRefData(data);
1887 return m;
1888}
1889
1890wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
1891{
1892 if ( !pen.Ok() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT )
1893 return wxNullGraphicsPen;
1894 else
1895 {
1896 wxGraphicsPen p;
1897 p.SetRefData(new wxCairoPenData( this, pen ));
1898 return p;
1899 }
1900}
1901
1902wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
1903{
1904 if ( !brush.Ok() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT )
1905 return wxNullGraphicsBrush;
1906 else
1907 {
1908 wxGraphicsBrush p;
1909 p.SetRefData(new wxCairoBrushData( this, brush ));
1910 return p;
1911 }
1912}
1913
1914wxGraphicsBrush
1915wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
1916 wxDouble x2, wxDouble y2,
1917 const wxGraphicsGradientStops& stops)
1918{
1919 wxGraphicsBrush p;
1920 wxCairoBrushData* d = new wxCairoBrushData( this );
1921 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
1922 p.SetRefData(d);
1923 return p;
1924}
1925
1926wxGraphicsBrush
1927wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
1928 wxDouble xc, wxDouble yc, wxDouble r,
1929 const wxGraphicsGradientStops& stops)
1930{
1931 wxGraphicsBrush p;
1932 wxCairoBrushData* d = new wxCairoBrushData( this );
1933 d->CreateRadialGradientBrush(xo, yo, xc, yc, r, stops);
1934 p.SetRefData(d);
1935 return p;
1936}
1937
1938// sets the font
1939wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1940{
1941 if ( font.Ok() )
1942 {
1943 wxGraphicsFont p;
1944 p.SetRefData(new wxCairoFontData( this , font, col ));
1945 return p;
1946 }
1947 else
1948 return wxNullGraphicsFont;
1949}
1950
1951wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp )
1952{
1953 if ( bmp.Ok() )
1954 {
1955 wxGraphicsBitmap p;
1956 p.SetRefData(new wxCairoBitmapData( this , bmp ));
1957 return p;
1958 }
1959 else
1960 return wxNullGraphicsBitmap;
1961}
1962
1963wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
1964{
1965 if ( bitmap != NULL )
1966 {
1967 wxGraphicsBitmap p;
1968 p.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t*) bitmap ));
1969 return p;
1970 }
1971 else
1972 return wxNullGraphicsBitmap;
1973}
1974
1975wxGraphicsBitmap
1976wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap& WXUNUSED(bitmap),
1977 wxDouble WXUNUSED(x),
1978 wxDouble WXUNUSED(y),
1979 wxDouble WXUNUSED(w),
1980 wxDouble WXUNUSED(h))
1981{
1982 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
1983 return wxNullGraphicsBitmap;
1984}
1985
1986wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
1987{
1988 return &gs_cairoGraphicsRenderer;
1989}
1990
1991#else // !wxUSE_CAIRO
1992
1993wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
1994{
1995 return NULL;
1996}
1997
1998#endif // wxUSE_CAIRO/!wxUSE_CAIRO
1999
2000// MSW and OS X have their own native default renderers, but the other ports
2001// use Cairo by default
2002#if !(defined(__WXMSW__) || defined(__WXOSX__))
2003wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2004{
2005 return GetCairoRenderer();
2006}
2007#endif // !(__WXMSW__ || __WXOSX__)
2008
2009#endif // wxUSE_GRAPHICS_CONTEXT