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