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