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