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