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