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