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