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