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