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