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