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