]> git.saurik.com Git - wxWidgets.git/blame - src/generic/graphicc.cpp
Fixed wxUSE_STD_DEFAULT==0 compilation.
[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
6c99dbd5
VZ
24#include "wx/cairo.h"
25
184fc6c8 26#ifndef WX_PRECOMP
7f0d0111
VZ
27 #include "wx/bitmap.h"
28 #include "wx/icon.h"
7f0d0111
VZ
29 #include "wx/dcclient.h"
30 #include "wx/dcmemory.h"
31 #include "wx/dcprint.h"
081d8d96
PC
32 #ifdef __WXGTK__
33 #include "wx/window.h"
34 #endif
184fc6c8
SC
35#endif
36
2f94ab40 37#include "wx/private/graphics.h"
d9485f89 38#include "wx/rawbmp.h"
50de831a 39#include "wx/vector.h"
184fc6c8 40
184fc6c8
SC
41using namespace std;
42
184fc6c8
SC
43//-----------------------------------------------------------------------------
44// device context implementation
45//
46// more and more of the dc functionality should be implemented by calling
47// the appropricate wxCairoContext, but we will have to do that step by step
48// also coordinate conversions should be moved to native matrix ops
49//-----------------------------------------------------------------------------
50
51// we always stock two context states, one at entry, to be able to preserve the
52// state we were called with, the other one after changing to HI Graphics orientation
53// (this one is used for getting back clippings etc)
54
55//-----------------------------------------------------------------------------
56// wxGraphicsPath implementation
57//-----------------------------------------------------------------------------
58
59// TODO remove this dependency (gdiplus needs the macros)
60
61#ifndef max
62#define max(a,b) (((a) > (b)) ? (a) : (b))
63#endif
64
65#ifndef min
66#define min(a,b) (((a) < (b)) ? (a) : (b))
67#endif
68
69#include <cairo.h>
c0e69d72
KO
70#ifdef __WXMSW__
71#include <cairo-win32.h>
72#endif
73
00bd8e72 74#ifdef __WXGTK__
184fc6c8 75#include <gtk/gtk.h>
84f67b11 76#include "wx/fontutil.h"
888dde65 77#include "wx/gtk/dc.h"
00bd8e72 78#endif
184fc6c8 79
e7f9f819
SC
80#ifdef __WXMSW__
81#include <cairo-win32.h>
82#endif
83
84#ifdef __WXMAC__
33ddeaba 85#include "wx/osx/private.h"
e7f9f819
SC
86#include <cairo-quartz.h>
87#include <cairo-atsui.h>
88#endif
89
0db8a70e 90class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData
184fc6c8 91{
184fc6c8 92public :
0db8a70e
RD
93 wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL);
94 ~wxCairoPathData();
184fc6c8 95
0db8a70e 96 virtual wxGraphicsObjectRefData *Clone() const;
184fc6c8
SC
97
98 //
99 // These are the path primitives from which everything else can be constructed
100 //
101
102 // begins a new subpath at (x,y)
103 virtual void MoveToPoint( wxDouble x, wxDouble y );
104
105 // adds a straight line from the current point to (x,y)
106 virtual void AddLineToPoint( wxDouble x, wxDouble y );
107
108 // adds a cubic Bezier curve from the current point, using two control points and an end point
109 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
110
111
112 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
113 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
114
115 // gets the last point of the current path, (0,0) if not yet set
0db8a70e 116 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
184fc6c8 117
00bd8e72 118 // adds another path
0db8a70e 119 virtual void AddPath( const wxGraphicsPathData* path );
00bd8e72 120
184fc6c8
SC
121 // closes the current sub-path
122 virtual void CloseSubpath();
123
124 //
125 // These are convenience functions which - if not available natively will be assembled
126 // using the primitives from above
127 //
128
129 /*
130
49ad157e 131 // appends a rectangle as a new closed subpath
184fc6c8
SC
132 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
133 // appends an ellipsis as a new closed subpath fitting the passed rectangle
134 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
135
136 // 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)
137 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
138 */
139
cd5adaa6
RD
140 // returns the native path
141 virtual void * GetNativePath() const ;
142
143 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
0db8a70e 144 virtual void UnGetNativePath(void *p) const;
f540e5bd 145
00bd8e72 146 // transforms each point of this path by the matrix
0db8a70e 147 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
00bd8e72
SC
148
149 // gets the bounding box enclosing all points (possibly including control points)
0db8a70e 150 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
00bd8e72 151
94a007ec 152 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxWINDING_RULE) const;
00bd8e72 153
184fc6c8
SC
154private :
155 cairo_t* m_pathContext;
156};
157
0db8a70e 158class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData
184fc6c8 159{
00bd8e72 160public :
0db8a70e
RD
161 wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
162 virtual ~wxCairoMatrixData() ;
184fc6c8 163
0db8a70e 164 virtual wxGraphicsObjectRefData *Clone() const ;
184fc6c8 165
00bd8e72 166 // concatenates the matrix
0db8a70e 167 virtual void Concat( const wxGraphicsMatrixData *t );
184fc6c8 168
00bd8e72 169 // sets the matrix to the respective values
49ad157e 170 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
00bd8e72 171 wxDouble tx=0.0, wxDouble ty=0.0);
184fc6c8 172
248802d0
RD
173 // gets the component valuess of the matrix
174 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
175 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
49ad157e 176
00bd8e72
SC
177 // makes this the inverse matrix
178 virtual void Invert();
184fc6c8 179
00bd8e72 180 // returns true if the elements of the transformation matrix are equal ?
0db8a70e 181 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
184fc6c8 182
00bd8e72 183 // return true if this is the identity matrix
0db8a70e 184 virtual bool IsIdentity() const;
184fc6c8 185
00bd8e72
SC
186 //
187 // transformation
188 //
184fc6c8 189
00bd8e72
SC
190 // add the translation to this matrix
191 virtual void Translate( wxDouble dx , wxDouble dy );
192
193 // add the scale to this matrix
194 virtual void Scale( wxDouble xScale , wxDouble yScale );
195
196 // add the rotation to this matrix (radians)
49ad157e 197 virtual void Rotate( wxDouble angle );
00bd8e72
SC
198
199 //
200 // apply the transforms
201 //
202
203 // applies that matrix to the point
0db8a70e 204 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
00bd8e72
SC
205
206 // applies the matrix except for translations
0db8a70e 207 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
00bd8e72
SC
208
209 // returns the native representation
210 virtual void * GetNativeMatrix() const;
211private:
212 cairo_matrix_t m_matrix ;
00bd8e72
SC
213} ;
214
87752530 215class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData
184fc6c8 216{
00bd8e72 217public:
87752530
SC
218 wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
219 ~wxCairoPenData();
00bd8e72
SC
220
221 void Init();
222
223 virtual void Apply( wxGraphicsContext* context );
224 virtual wxDouble GetWidth() { return m_width; }
225
226private :
227 double m_width;
49ad157e 228
00bd8e72
SC
229 double m_red;
230 double m_green;
231 double m_blue;
232 double m_alpha;
49ad157e 233
00bd8e72
SC
234 cairo_line_cap_t m_cap;
235 cairo_line_join_t m_join;
236
237 int m_count;
238 const double *m_lengths;
239 double *m_userLengths;
184fc6c8 240
00bd8e72 241 wxPen m_pen;
50de831a
PC
242
243 wxDECLARE_NO_COPY_CLASS(wxCairoPenData);
00bd8e72
SC
244};
245
87752530 246class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData
184fc6c8 247{
00bd8e72 248public:
87752530
SC
249 wxCairoBrushData( wxGraphicsRenderer* renderer );
250 wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
251 ~wxCairoBrushData ();
00bd8e72
SC
252
253 virtual void Apply( wxGraphicsContext* context );
4ee4c7b9
VZ
254
255 void CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
256 wxDouble x2, wxDouble y2,
257 const wxGraphicsGradientStops& stops);
258 void CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
259 wxDouble xc, wxDouble yc, wxDouble radius,
260 const wxGraphicsGradientStops& stops);
00bd8e72
SC
261
262protected:
263 virtual void Init();
264
4ee4c7b9
VZ
265 // common part of Create{Linear,Radial}GradientBrush()
266 void AddGradientStops(const wxGraphicsGradientStops& stops);
267
00bd8e72
SC
268private :
269 double m_red;
270 double m_green;
271 double m_blue;
272 double m_alpha;
273
274 cairo_pattern_t* m_brushPattern;
00bd8e72
SC
275};
276
87752530 277class wxCairoFontData : public wxGraphicsObjectRefData
184fc6c8 278{
00bd8e72 279public:
87752530
SC
280 wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
281 ~wxCairoFontData();
00bd8e72
SC
282
283 virtual void Apply( wxGraphicsContext* context );
84f67b11
SC
284#ifdef __WXGTK__
285 const PangoFontDescription* GetFont() const { return m_font; }
0d19936d 286 bool GetUnderlined() const { return m_underlined; }
84f67b11 287#endif
00bd8e72 288private :
00bd8e72 289 double m_size;
0d19936d 290 bool m_underlined;
00bd8e72
SC
291 double m_red;
292 double m_green;
293 double m_blue;
294 double m_alpha;
84f67b11 295#ifdef __WXMAC__
e7f9f819 296 cairo_font_face_t *m_font;
84f67b11
SC
297#elif defined(__WXGTK__)
298 PangoFontDescription* m_font;
299#else
300 wxCharBuffer m_fontName;
301 cairo_font_slant_t m_slant;
302 cairo_font_weight_t m_weight;
303#endif
c0e69d72
KO
304#ifdef __WXMSW__
305 wxCairoContext( wxGraphicsRenderer* renderer, HDC context );
306#endif
00bd8e72 307};
184fc6c8 308
2fc9c1ea
KO
309class wxCairoBitmapData : public wxGraphicsObjectRefData
310{
311public:
312 wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp );
2986eb86 313 wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap );
2fc9c1ea
KO
314 ~wxCairoBitmapData();
315
316 virtual cairo_surface_t* GetCairoSurface() { return m_surface; }
317 virtual cairo_pattern_t* GetCairoPattern() { return m_pattern; }
318 virtual wxSize GetSize() { return wxSize(m_width, m_height); }
319private :
320 cairo_surface_t* m_surface;
321 cairo_pattern_t* m_pattern;
322 int m_width;
323 int m_height;
324 unsigned char* m_buffer;
325};
326
00bd8e72 327class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
184fc6c8 328{
184fc6c8 329public:
00bd8e72 330 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
888dde65 331 wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc );
c9008abe 332 wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc );
00bd8e72
SC
333#ifdef __WXGTK__
334 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
335#endif
336 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
337 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
184fc6c8
SC
338 wxCairoContext();
339 virtual ~wxCairoContext();
340
e7f9f819 341 virtual bool ShouldOffset() const
49ad157e 342 {
136a1de9
SC
343 if ( !m_enableOffset )
344 return false;
345
e7f9f819
SC
346 int penwidth = 0 ;
347 if ( !m_pen.IsNull() )
348 {
349 penwidth = (int)((wxCairoPenData*)m_pen.GetRefData())->GetWidth();
350 if ( penwidth == 0 )
351 penwidth = 1;
352 }
353 return ( penwidth % 2 ) == 1;
354 }
355
184fc6c8 356 virtual void Clip( const wxRegion &region );
c0e69d72
KO
357#ifdef __WXMSW__
358 cairo_surface_t* m_mswSurface;
359#endif
539e2795
SC
360
361 // clips drawings to the rect
362 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
539e2795 363
cd5adaa6
RD
364 // resets the clipping to original extent
365 virtual void ResetClip();
366
367 virtual void * GetNativeContext();
368
bf02a7f9 369 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
49ad157e 370
bf02a7f9
SC
371 virtual bool SetCompositionMode(wxCompositionMode op);
372
373 virtual void BeginLayer(wxDouble opacity);
374
375 virtual void EndLayer();
03647350 376
0db8a70e 377 virtual void StrokePath( const wxGraphicsPath& p );
94a007ec 378 virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxWINDING_RULE );
184fc6c8 379
184fc6c8
SC
380 virtual void Translate( wxDouble dx , wxDouble dy );
381 virtual void Scale( wxDouble xScale , wxDouble yScale );
382 virtual void Rotate( wxDouble angle );
383
00bd8e72 384 // concatenates this transform with the current transform of this context
0db8a70e 385 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
00bd8e72
SC
386
387 // sets the transform of this context
0db8a70e 388 virtual void SetTransform( const wxGraphicsMatrix& matrix );
00bd8e72
SC
389
390 // gets the matrix of this context
0db8a70e 391 virtual wxGraphicsMatrix GetTransform() const;
00bd8e72 392
e77cba1a 393 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
184fc6c8
SC
394 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
395 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
396 virtual void PushState();
397 virtual void PopState();
398
184fc6c8
SC
399 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
400 wxDouble *descent, wxDouble *externalLeading ) const;
401 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
402
50de831a
PC
403protected:
404 virtual void DoDrawText( const wxString &str, wxDouble x, wxDouble y );
405
184fc6c8 406private:
e7f9f819 407 void Init(cairo_t *context);
49ad157e 408
184fc6c8 409 cairo_t* m_context;
03647350 410
bf02a7f9 411 wxVector<float> m_layerOpacities;
0b7dce54 412
c0c133e1 413 wxDECLARE_NO_COPY_CLASS(wxCairoContext);
184fc6c8
SC
414};
415
184fc6c8 416//-----------------------------------------------------------------------------
87752530 417// wxCairoPenData implementation
184fc6c8
SC
418//-----------------------------------------------------------------------------
419
87752530 420wxCairoPenData::~wxCairoPenData()
539e2795 421{
00bd8e72 422 delete[] m_userLengths;
539e2795 423}
cd5adaa6 424
87752530 425void wxCairoPenData::Init()
539e2795 426{
00bd8e72
SC
427 m_lengths = NULL;
428 m_userLengths = NULL;
429 m_width = 0;
430 m_count = 0;
539e2795
SC
431}
432
87752530
SC
433wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
434: wxGraphicsObjectRefData(renderer)
49ad157e 435{
00bd8e72 436 Init();
7f8bd9fc
RD
437 m_pen = pen;
438 m_width = m_pen.GetWidth();
00bd8e72
SC
439 if (m_width <= 0.0)
440 m_width = 0.1;
539e2795 441
00bd8e72 442 m_red = m_pen.GetColour().Red()/255.0;
49ad157e 443 m_green = m_pen.GetColour().Green()/255.0;
00bd8e72
SC
444 m_blue = m_pen.GetColour().Blue()/255.0;
445 m_alpha = m_pen.GetColour().Alpha()/255.0;
184fc6c8 446
184fc6c8
SC
447 switch ( m_pen.GetCap() )
448 {
449 case wxCAP_ROUND :
00bd8e72 450 m_cap = CAIRO_LINE_CAP_ROUND;
184fc6c8
SC
451 break;
452
453 case wxCAP_PROJECTING :
00bd8e72 454 m_cap = CAIRO_LINE_CAP_SQUARE;
184fc6c8
SC
455 break;
456
457 case wxCAP_BUTT :
00bd8e72 458 m_cap = CAIRO_LINE_CAP_BUTT;
184fc6c8
SC
459 break;
460
461 default :
00bd8e72 462 m_cap = CAIRO_LINE_CAP_BUTT;
184fc6c8
SC
463 break;
464 }
184fc6c8 465
184fc6c8
SC
466 switch ( m_pen.GetJoin() )
467 {
468 case wxJOIN_BEVEL :
00bd8e72 469 m_join = CAIRO_LINE_JOIN_BEVEL;
184fc6c8
SC
470 break;
471
472 case wxJOIN_MITER :
00bd8e72 473 m_join = CAIRO_LINE_JOIN_MITER;
184fc6c8
SC
474 break;
475
476 case wxJOIN_ROUND :
00bd8e72 477 m_join = CAIRO_LINE_JOIN_ROUND;
184fc6c8
SC
478 break;
479
480 default :
00bd8e72 481 m_join = CAIRO_LINE_JOIN_MITER;
184fc6c8
SC
482 break;
483 }
184fc6c8 484
00bd8e72 485 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
184fc6c8 486 const double dotted[] =
00bd8e72
SC
487 {
488 dashUnit , dashUnit + 2.0
489 };
490 static const double short_dashed[] =
491 {
492 9.0 , 6.0
493 };
494 static const double dashed[] =
495 {
496 19.0 , 9.0
497 };
498 static const double dotted_dashed[] =
499 {
500 9.0 , 6.0 , 3.0 , 3.0
501 };
184fc6c8
SC
502
503 switch ( m_pen.GetStyle() )
504 {
fb040094 505 case wxPENSTYLE_SOLID :
184fc6c8
SC
506 break;
507
fb040094 508 case wxPENSTYLE_DOT :
00bd8e72
SC
509 m_count = WXSIZEOF(dotted);
510 m_userLengths = new double[ m_count ] ;
511 memcpy( m_userLengths, dotted, sizeof(dotted) );
512 m_lengths = m_userLengths;
184fc6c8
SC
513 break;
514
fb040094 515 case wxPENSTYLE_LONG_DASH :
84f67b11 516 m_lengths = dashed ;
00bd8e72 517 m_count = WXSIZEOF(dashed);
184fc6c8
SC
518 break;
519
fb040094 520 case wxPENSTYLE_SHORT_DASH :
84f67b11 521 m_lengths = short_dashed ;
00bd8e72 522 m_count = WXSIZEOF(short_dashed);
184fc6c8
SC
523 break;
524
fb040094 525 case wxPENSTYLE_DOT_DASH :
84f67b11 526 m_lengths = dotted_dashed ;
00bd8e72 527 m_count = WXSIZEOF(dotted_dashed);
184fc6c8
SC
528 break;
529
fb040094 530 case wxPENSTYLE_USER_DASH :
184fc6c8
SC
531 {
532 wxDash *wxdashes ;
00bd8e72
SC
533 m_count = m_pen.GetDashes( &wxdashes ) ;
534 if ((wxdashes != NULL) && (m_count > 0))
184fc6c8 535 {
00bd8e72
SC
536 m_userLengths = new double[m_count] ;
537 for ( int i = 0 ; i < m_count ; ++i )
184fc6c8 538 {
00bd8e72 539 m_userLengths[i] = wxdashes[i] * dashUnit ;
184fc6c8 540
00bd8e72
SC
541 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
542 m_userLengths[i] = dashUnit + 2.0 ;
543 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
544 m_userLengths[i] = dashUnit ;
184fc6c8
SC
545 }
546 }
00bd8e72 547 m_lengths = m_userLengths ;
184fc6c8
SC
548 }
549 break;
fb040094 550 case wxPENSTYLE_STIPPLE :
184fc6c8
SC
551 {
552 /*
00bd8e72 553 wxBitmap* bmp = pen.GetStipple();
a1b806b9 554 if ( bmp && bmp->IsOk() )
00bd8e72
SC
555 {
556 wxDELETE( m_penImage );
557 wxDELETE( m_penBrush );
558 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
559 m_penBrush = new TextureBrush(m_penImage);
560 m_pen->SetBrush( m_penBrush );
561 }
184fc6c8
SC
562 */
563 }
564 break;
565 default :
89efaf2b 566 if ( m_pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH
fb040094 567 && m_pen.GetStyle() <= wxPENSTYLE_LAST_HATCH )
184fc6c8
SC
568 {
569 /*
00bd8e72
SC
570 wxDELETE( m_penBrush );
571 HatchStyle style = HatchStyleHorizontal;
572 switch( pen.GetStyle() )
573 {
fb040094 574 case wxPENSTYLE_BDIAGONAL_HATCH :
00bd8e72
SC
575 style = HatchStyleBackwardDiagonal;
576 break ;
fb040094 577 case wxPENSTYLE_CROSSDIAG_HATCH :
00bd8e72
SC
578 style = HatchStyleDiagonalCross;
579 break ;
fb040094 580 case wxPENSTYLE_FDIAGONAL_HATCH :
00bd8e72
SC
581 style = HatchStyleForwardDiagonal;
582 break ;
fb040094 583 case wxPENSTYLE_CROSS_HATCH :
00bd8e72
SC
584 style = HatchStyleCross;
585 break ;
fb040094 586 case wxPENSTYLE_HORIZONTAL_HATCH :
00bd8e72
SC
587 style = HatchStyleHorizontal;
588 break ;
fb040094 589 case wxPENSTYLE_VERTICAL_HATCH :
00bd8e72
SC
590 style = HatchStyleVertical;
591 break ;
592
593 }
594 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
595 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
596 m_pen->SetBrush( m_penBrush )
184fc6c8
SC
597 */
598 }
599 break;
600 }
00bd8e72 601}
184fc6c8 602
87752530 603void wxCairoPenData::Apply( wxGraphicsContext* context )
00bd8e72
SC
604{
605 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
606 cairo_set_line_width(ctext,m_width);
607 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
608 cairo_set_line_cap(ctext,m_cap);
609 cairo_set_line_join(ctext,m_join);
610 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
184fc6c8
SC
611}
612
00bd8e72 613//-----------------------------------------------------------------------------
87752530 614// wxCairoBrushData implementation
00bd8e72
SC
615//-----------------------------------------------------------------------------
616
7f8bd9fc
RD
617wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
618 : wxGraphicsObjectRefData( renderer )
00bd8e72
SC
619{
620 Init();
621}
184fc6c8 622
87752530 623wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
7f8bd9fc 624 : wxGraphicsObjectRefData(renderer)
00bd8e72 625{
7f8bd9fc 626 Init();
49ad157e 627
00bd8e72 628 m_red = brush.GetColour().Red()/255.0;
49ad157e 629 m_green = brush.GetColour().Green()/255.0;
00bd8e72
SC
630 m_blue = brush.GetColour().Blue()/255.0;
631 m_alpha = brush.GetColour().Alpha()/255.0;
632 /*
fb040094 633 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
00bd8e72
SC
634 {
635 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
636 brush.GetColour().Green() , brush.GetColour().Blue() ) );
637 }
638 else if ( brush.IsHatch() )
639 {
640 HatchStyle style = HatchStyleHorizontal;
641 switch( brush.GetStyle() )
642 {
fb040094 643 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
00bd8e72
SC
644 style = HatchStyleBackwardDiagonal;
645 break ;
fb040094 646 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
00bd8e72
SC
647 style = HatchStyleDiagonalCross;
648 break ;
fb040094 649 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
00bd8e72
SC
650 style = HatchStyleForwardDiagonal;
651 break ;
fb040094 652 case wxBRUSHSTYLE_CROSS_HATCH :
00bd8e72
SC
653 style = HatchStyleCross;
654 break ;
fb040094 655 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
00bd8e72
SC
656 style = HatchStyleHorizontal;
657 break ;
fb040094 658 case wxBRUSHSTYLE_VERTICAL_HATCH :
00bd8e72
SC
659 style = HatchStyleVertical;
660 break ;
661
662 }
663 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
664 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
665 }
49ad157e 666 else
00bd8e72
SC
667 {
668 wxBitmap* bmp = brush.GetStipple();
a1b806b9 669 if ( bmp && bmp->IsOk() )
00bd8e72
SC
670 {
671 wxDELETE( m_brushImage );
672 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
673 m_brush = new TextureBrush(m_brushImage);
674 }
184fc6c8 675 }
00bd8e72 676 */
184fc6c8
SC
677}
678
87752530 679wxCairoBrushData::~wxCairoBrushData ()
184fc6c8 680{
00bd8e72
SC
681 if (m_brushPattern)
682 cairo_pattern_destroy(m_brushPattern);
184fc6c8
SC
683}
684
87752530 685void wxCairoBrushData::Apply( wxGraphicsContext* context )
184fc6c8 686{
00bd8e72
SC
687 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
688 if ( m_brushPattern )
689 {
690 cairo_set_source(ctext,m_brushPattern);
691 }
692 else
693 {
694 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
695 }
184fc6c8
SC
696}
697
4ee4c7b9
VZ
698void wxCairoBrushData::AddGradientStops(const wxGraphicsGradientStops& stops)
699{
700 // loop over all the stops, they include the beginning and ending ones
701 const unsigned numStops = stops.GetCount();
702 for ( unsigned n = 0; n < numStops; n++ )
703 {
704 const wxGraphicsGradientStop stop = stops.Item(n);
705
706 const wxColour col = stop.GetColour();
707
708 cairo_pattern_add_color_stop_rgba
709 (
710 m_brushPattern,
711 stop.GetPosition(),
712 col.Red()/255.0,
713 col.Green()/255.0,
714 col.Blue()/255.0,
715 col.Alpha()/255.0
716 );
717 }
718
719 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS,
720 wxT("Couldn't create cairo pattern"));
721}
722
723void
724wxCairoBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
725 wxDouble x2, wxDouble y2,
726 const wxGraphicsGradientStops& stops)
184fc6c8 727{
00bd8e72 728 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
4ee4c7b9
VZ
729
730 AddGradientStops(stops);
184fc6c8
SC
731}
732
4ee4c7b9
VZ
733void
734wxCairoBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
735 wxDouble xc, wxDouble yc,
736 wxDouble radius,
737 const wxGraphicsGradientStops& stops)
184fc6c8 738{
00bd8e72 739 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
4ee4c7b9
VZ
740
741 AddGradientStops(stops);
184fc6c8
SC
742}
743
87752530 744void wxCairoBrushData::Init()
184fc6c8 745{
00bd8e72 746 m_brushPattern = NULL;
184fc6c8
SC
747}
748
00bd8e72 749//-----------------------------------------------------------------------------
87752530 750// wxCairoFontData implementation
00bd8e72
SC
751//-----------------------------------------------------------------------------
752
49ad157e 753wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
87752530 754 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
184fc6c8 755{
00bd8e72 756 m_red = col.Red()/255.0;
49ad157e 757 m_green = col.Green()/255.0;
00bd8e72
SC
758 m_blue = col.Blue()/255.0;
759 m_alpha = col.Alpha()/255.0;
00bd8e72 760 m_size = font.GetPointSize();
0d19936d 761 m_underlined = font.GetUnderlined();
84f67b11
SC
762
763#ifdef __WXMAC__
aa6208d9 764 m_font = cairo_quartz_font_face_create_for_cgfont( font.OSXGetCGFont() );
84f67b11
SC
765#elif defined(__WXGTK__)
766 m_font = pango_font_description_copy( font.GetNativeFontInfo()->description );
767#else
00bd8e72
SC
768 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
769 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
770 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
e7f9f819 771#endif
184fc6c8
SC
772}
773
87752530 774wxCairoFontData::~wxCairoFontData()
184fc6c8 775{
84f67b11 776#ifdef __WXMAC__
e7f9f819 777 cairo_font_face_destroy( m_font );
84f67b11
SC
778#elif defined(__WXGTK__)
779 pango_font_description_free( m_font );
780#else
781#endif
00bd8e72 782}
184fc6c8 783
87752530 784void wxCairoFontData::Apply( wxGraphicsContext* context )
00bd8e72
SC
785{
786 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
787 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
84f67b11
SC
788#ifdef __WXGTK__
789 // the rest is done using Pango layouts
790#elif defined(__WXMAC__)
791 cairo_set_font_face(ctext, m_font);
3e700936 792 cairo_set_font_size(ctext, m_size );
84f67b11 793#else
711a4812 794 cairo_select_font_face(ctext, m_fontName, m_slant, m_weight );
84f67b11 795 cairo_set_font_size(ctext, m_size );
e7f9f819 796#endif
00bd8e72
SC
797}
798
799//-----------------------------------------------------------------------------
0db8a70e 800// wxCairoPathData implementation
00bd8e72
SC
801//-----------------------------------------------------------------------------
802
0db8a70e
RD
803wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
804 : wxGraphicsPathData(renderer)
184fc6c8 805{
00bd8e72 806 if (pathcontext)
184fc6c8 807 {
00bd8e72
SC
808 m_pathContext = pathcontext;
809 }
810 else
811 {
812 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
813 m_pathContext = cairo_create(surface);
814 cairo_surface_destroy (surface);
184fc6c8 815 }
00bd8e72 816}
184fc6c8 817
0db8a70e 818wxCairoPathData::~wxCairoPathData()
00bd8e72
SC
819{
820 cairo_destroy(m_pathContext);
184fc6c8
SC
821}
822
0db8a70e 823wxGraphicsObjectRefData *wxCairoPathData::Clone() const
184fc6c8 824{
00bd8e72
SC
825 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
826 cairo_t* pathcontext = cairo_create(surface);
827 cairo_surface_destroy (surface);
828
829 cairo_path_t* path = cairo_copy_path(m_pathContext);
830 cairo_append_path(pathcontext, path);
831 cairo_path_destroy(path);
0db8a70e 832 return new wxCairoPathData( GetRenderer() ,pathcontext);
00bd8e72
SC
833}
834
835
0db8a70e 836void* wxCairoPathData::GetNativePath() const
00bd8e72
SC
837{
838 return cairo_copy_path(m_pathContext) ;
839}
840
0db8a70e 841void wxCairoPathData::UnGetNativePath(void *p) const
00bd8e72
SC
842{
843 cairo_path_destroy((cairo_path_t*)p);
844}
845
846//
847// The Primitives
848//
849
0db8a70e 850void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
00bd8e72
SC
851{
852 cairo_move_to(m_pathContext,x,y);
853}
854
0db8a70e 855void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
00bd8e72
SC
856{
857 cairo_line_to(m_pathContext,x,y);
858}
859
0db8a70e 860void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
6b06903d 861{
d9485f89
RD
862 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
863 cairo_append_path(m_pathContext, p);
864 UnGetNativePath(p);
6b06903d
RD
865}
866
0db8a70e 867void wxCairoPathData::CloseSubpath()
00bd8e72
SC
868{
869 cairo_close_path(m_pathContext);
870}
871
0db8a70e 872void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
00bd8e72
SC
873{
874 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
875}
876
877// gets the last point of the current path, (0,0) if not yet set
0db8a70e 878void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
00bd8e72
SC
879{
880 double dx,dy;
881 cairo_get_current_point(m_pathContext,&dx,&dy);
0db8a70e
RD
882 if (x)
883 *x = dx;
884 if (y)
885 *y = dy;
00bd8e72
SC
886}
887
0db8a70e 888void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
00bd8e72 889{
49ad157e 890 // as clockwise means positive in our system (y pointing downwards)
00bd8e72
SC
891 // TODO make this interpretation dependent of the
892 // real device trans
893 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
894 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
895 else
896 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
897}
898
899// transforms each point of this path by the matrix
49ad157e 900void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
00bd8e72
SC
901{
902 // as we don't have a true path object, we have to apply the inverse
903 // matrix to the context
904 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
905 cairo_matrix_invert( &m );
906 cairo_transform(m_pathContext,&m);
49ad157e 907}
00bd8e72
SC
908
909// gets the bounding box enclosing all points (possibly including control points)
0db8a70e 910void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
00bd8e72
SC
911{
912 double x1,y1,x2,y2;
913
914 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
915 if ( x2 < x1 )
184fc6c8 916 {
00bd8e72
SC
917 *x = x2;
918 *w = x1-x2;
919 }
920 else
921 {
922 *x = x1;
923 *w = x2-x1;
184fc6c8 924 }
49ad157e 925
00bd8e72
SC
926 if( y2 < y1 )
927 {
928 *y = y2;
929 *h = y1-y2;
930 }
931 else
932 {
933 *y = y1;
934 *h = y2-y1;
935 }
936}
937
f2b905d7 938bool wxCairoPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
00bd8e72 939{
f2b905d7
SC
940 cairo_set_fill_rule(m_pathContext,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
941 return cairo_in_fill( m_pathContext, x, y) != 0;
00bd8e72
SC
942}
943
944//-----------------------------------------------------------------------------
0db8a70e 945// wxCairoMatrixData implementation
00bd8e72
SC
946//-----------------------------------------------------------------------------
947
0db8a70e
RD
948wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
949 : wxGraphicsMatrixData(renderer)
00bd8e72
SC
950{
951 if ( matrix )
952 m_matrix = *matrix;
953}
954
49ad157e 955wxCairoMatrixData::~wxCairoMatrixData()
00bd8e72
SC
956{
957 // nothing to do
958}
959
49ad157e 960wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
00bd8e72 961{
0db8a70e 962 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
00bd8e72
SC
963}
964
965// concatenates the matrix
49ad157e 966void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
00bd8e72 967{
49ad157e 968 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
00bd8e72
SC
969}
970
00bd8e72 971// sets the matrix to the respective values
49ad157e
VZ
972void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
973 wxDouble tx, wxDouble ty)
00bd8e72
SC
974{
975 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
976}
977
248802d0
RD
978// gets the component valuess of the matrix
979void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
980 wxDouble* d, wxDouble* tx, wxDouble* ty) const
981{
982 if (a) *a = m_matrix.xx;
983 if (b) *b = m_matrix.yx;
984 if (c) *c = m_matrix.xy;
985 if (d) *d = m_matrix.yy;
986 if (tx) *tx= m_matrix.x0;
987 if (ty) *ty= m_matrix.y0;
988}
989
00bd8e72 990// makes this the inverse matrix
49ad157e 991void wxCairoMatrixData::Invert()
00bd8e72
SC
992{
993 cairo_matrix_invert( &m_matrix );
994}
995
996// returns true if the elements of the transformation matrix are equal ?
49ad157e 997bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
00bd8e72
SC
998{
999 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
49ad157e
VZ
1000 return (
1001 m_matrix.xx == tm->xx &&
1002 m_matrix.yx == tm->yx &&
1003 m_matrix.xy == tm->xy &&
1004 m_matrix.yy == tm->yy &&
1005 m_matrix.x0 == tm->x0 &&
00bd8e72
SC
1006 m_matrix.y0 == tm->y0 ) ;
1007}
1008
1009// return true if this is the identity matrix
0db8a70e 1010bool wxCairoMatrixData::IsIdentity() const
00bd8e72
SC
1011{
1012 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
1013 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
1014}
1015
1016//
1017// transformation
1018//
1019
1020// add the translation to this matrix
0db8a70e 1021void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
00bd8e72
SC
1022{
1023 cairo_matrix_translate( &m_matrix, dx, dy) ;
1024}
1025
1026// add the scale to this matrix
0db8a70e 1027void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
00bd8e72
SC
1028{
1029 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
1030}
1031
1032// add the rotation to this matrix (radians)
49ad157e 1033void wxCairoMatrixData::Rotate( wxDouble angle )
00bd8e72
SC
1034{
1035 cairo_matrix_rotate( &m_matrix, angle) ;
49ad157e 1036}
00bd8e72
SC
1037
1038//
1039// apply the transforms
1040//
1041
1042// applies that matrix to the point
0db8a70e 1043void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
00bd8e72
SC
1044{
1045 double lx = *x, ly = *y ;
1046 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
1047 *x = lx;
1048 *y = ly;
1049}
1050
1051// applies the matrix except for translations
0db8a70e 1052void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
00bd8e72
SC
1053{
1054 double lx = *dx, ly = *dy ;
1055 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
1056 *dx = lx;
1057 *dy = ly;
1058}
1059
1060// returns the native representation
0db8a70e 1061void * wxCairoMatrixData::GetNativeMatrix() const
00bd8e72
SC
1062{
1063 return (void*) &m_matrix;
1064}
1065
2fc9c1ea
KO
1066// wxCairoBitmap implementation
1067//-----------------------------------------------------------------------------
1068
2986eb86
SC
1069wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, cairo_surface_t* bitmap ) :
1070 wxGraphicsObjectRefData( renderer )
1071{
1072 m_surface = bitmap;
1073 m_pattern = cairo_pattern_create_for_surface(m_surface);
1074}
1075
2fc9c1ea
KO
1076wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ) : wxGraphicsObjectRefData( renderer )
1077{
1078 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1079
711a4812 1080#ifdef wxHAS_RAW_BITMAP
125e7c11
KO
1081 int bw = m_width = bmp.GetWidth();
1082 int bh = m_height = bmp.GetHeight();
2fc9c1ea
KO
1083 wxBitmap bmpSource = bmp; // we need a non-const instance
1084 m_buffer = new unsigned char[bw*bh*4];
3c10f6db 1085 wxUint32* data = (wxUint32*)m_buffer;
2fc9c1ea
KO
1086
1087 // Create a surface object and copy the bitmap pixel data to it. if the
1088 // image has alpha (or a mask represented as alpha) then we'll use a
1089 // different format and iterator than if it doesn't...
1090 if (bmpSource.HasAlpha() || bmpSource.GetMask())
1091 {
1092 m_surface = cairo_image_surface_create_for_data(
1093 m_buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1094 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1095 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1096
1097 wxAlphaPixelData::Iterator p(pixData);
1098 for (int y=0; y<bh; y++)
1099 {
1100 wxAlphaPixelData::Iterator rowStart = p;
1101 for (int x=0; x<bw; x++)
1102 {
1103 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1104 // with alpha in the upper 8 bits, then red, then green, then
1105 // blue. The 32-bit quantities are stored native-endian.
1106 // Pre-multiplied alpha is used.
1107 unsigned char alpha = p.Alpha();
1108 if (alpha == 0)
1109 *data = 0;
1110 else
1111 *data = ( alpha << 24
1112 | (p.Red() * alpha/255) << 16
1113 | (p.Green() * alpha/255) << 8
1114 | (p.Blue() * alpha/255) );
1115 ++data;
1116 ++p;
1117 }
1118 p = rowStart;
1119 p.OffsetY(pixData, 1);
1120 }
1121 }
1122 else // no alpha
1123 {
1124 m_surface = cairo_image_surface_create_for_data(
1125 m_buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1126 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1127 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1128
1129 wxNativePixelData::Iterator p(pixData);
1130 for (int y=0; y<bh; y++)
1131 {
1132 wxNativePixelData::Iterator rowStart = p;
1133 for (int x=0; x<bw; x++)
1134 {
1135 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1136 // the upper 8 bits unused. Red, Green, and Blue are stored in
1137 // the remaining 24 bits in that order. The 32-bit quantities
1138 // are stored native-endian.
1139 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1140 ++data;
1141 ++p;
1142 }
1143 p = rowStart;
1144 p.OffsetY(pixData, 1);
1145 }
1146 }
66f917a7 1147 m_pattern = cairo_pattern_create_for_surface(m_surface);
711a4812 1148#endif // wxHAS_RAW_BITMAP
2fc9c1ea
KO
1149}
1150
1151wxCairoBitmapData::~wxCairoBitmapData()
1152{
66f917a7
KO
1153 if (m_pattern)
1154 cairo_pattern_destroy(m_pattern);
4ee4c7b9 1155
66f917a7
KO
1156 if (m_surface)
1157 cairo_surface_destroy(m_surface);
4ee4c7b9 1158
2fc9c1ea
KO
1159 delete [] m_buffer;
1160}
1161
1162
1163
00bd8e72
SC
1164//-----------------------------------------------------------------------------
1165// wxCairoContext implementation
1166//-----------------------------------------------------------------------------
1167
e7f9f819
SC
1168class wxCairoOffsetHelper
1169{
1170public :
1171 wxCairoOffsetHelper( cairo_t* ctx , bool offset )
1172 {
1173 m_ctx = ctx;
1174 m_offset = offset;
1175 if ( m_offset )
1176 cairo_translate( m_ctx, 0.5, 0.5 );
1177 }
1178 ~wxCairoOffsetHelper( )
1179 {
1180 if ( m_offset )
1181 cairo_translate( m_ctx, -0.5, -0.5 );
1182 }
1183public :
1184 cairo_t* m_ctx;
1185 bool m_offset;
1186} ;
1187
c9008abe
RR
1188wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc )
1189: wxGraphicsContext(renderer)
1190{
1191#ifdef __WXGTK20__
1192 const wxDCImpl *impl = dc.GetImpl();
1193 Init( (cairo_t*) impl->GetCairoContext() );
1194
b129eaa3
VZ
1195 wxSize sz = dc.GetSize();
1196 m_width = sz.x;
1197 m_height = sz.y;
1198
c9008abe 1199 wxPoint org = dc.GetDeviceOrigin();
eaeb9985 1200 cairo_translate( m_context, org.x, org.y );
49ad157e 1201
c9008abe
RR
1202 double sx,sy;
1203 dc.GetUserScale( &sx, &sy );
eaeb9985 1204 cairo_scale( m_context, sx, sy );
c9008abe 1205
eaeb9985
RR
1206 org = dc.GetLogicalOrigin();
1207 cairo_translate( m_context, -org.x, -org.y );
c9008abe
RR
1208#endif
1209}
1210
00bd8e72
SC
1211wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1212: wxGraphicsContext(renderer)
1213{
b129eaa3
VZ
1214 int width, height;
1215 dc.GetSize( &width, &height );
1216 m_width = width;
1217 m_height = height;
1218
136a1de9
SC
1219 m_enableOffset = true;
1220
c9008abe 1221#ifdef __WXGTK20__
888dde65 1222 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
64a226f7 1223 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
49ad157e
VZ
1224
1225#if 0
c9008abe 1226 wxGraphicsMatrix matrix = CreateMatrix();
49ad157e 1227
c9008abe
RR
1228 wxPoint org = dc.GetDeviceOrigin();
1229 matrix.Translate( org.x, org.y );
49ad157e 1230
c9008abe
RR
1231 org = dc.GetLogicalOrigin();
1232 matrix.Translate( -org.x, -org.y );
1233
1234 double sx,sy;
1235 dc.GetUserScale( &sx, &sy );
1236 matrix.Scale( sx, sy );
1237
1238 ConcatTransform( matrix );
1239#endif
e7f9f819 1240#endif
c9008abe 1241
888dde65 1242#ifdef __WXMAC__
888dde65
RR
1243 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1244 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1245 Init( cairo_create( surface ) );
1246 cairo_surface_destroy( surface );
1247#endif
1248}
1249
1250wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc )
1251: wxGraphicsContext(renderer)
1252{
b129eaa3
VZ
1253 int width, height;
1254 dc.GetSize( &width, &height );
1255 m_width = width;
1256 m_height = height;
1257
136a1de9
SC
1258 m_enableOffset = true;
1259
c9008abe 1260#ifdef __WXGTK20__
888dde65
RR
1261 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1262 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
49ad157e
VZ
1263
1264#if 0
c9008abe 1265 wxGraphicsMatrix matrix = CreateMatrix();
49ad157e 1266
c9008abe
RR
1267 wxPoint org = dc.GetDeviceOrigin();
1268 matrix.Translate( org.x, org.y );
49ad157e 1269
c9008abe
RR
1270 org = dc.GetLogicalOrigin();
1271 matrix.Translate( -org.x, -org.y );
1272
1273 double sx,sy;
1274 dc.GetUserScale( &sx, &sy );
1275 matrix.Scale( sx, sy );
1276
1277 ConcatTransform( matrix );
64a226f7 1278#endif
c9008abe
RR
1279#endif
1280
e7f9f819 1281#ifdef __WXMAC__
e7f9f819
SC
1282 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1283 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1284 Init( cairo_create( surface ) );
1285 cairo_surface_destroy( surface );
00bd8e72 1286#endif
00bd8e72
SC
1287}
1288
c9008abe 1289#ifdef __WXGTK20__
00bd8e72
SC
1290wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1291: wxGraphicsContext(renderer)
1292{
e7f9f819 1293 Init( gdk_cairo_create( drawable ) );
b129eaa3
VZ
1294
1295 int width, height;
1296 gdk_drawable_get_size( drawable, &width, &height );
1297 m_width = width;
1298 m_height = height;
00bd8e72
SC
1299}
1300#endif
1301
c0e69d72
KO
1302#ifdef __WXMSW__
1303wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, HDC handle )
1304: wxGraphicsContext(renderer)
1305{
1306 m_mswSurface = cairo_win32_surface_create(handle);
b129eaa3
VZ
1307 Init( cairo_create(m_mswSurface) );
1308 m_width =
1309 m_height = 0;
c0e69d72
KO
1310}
1311#endif
1312
1313
00bd8e72
SC
1314wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1315: wxGraphicsContext(renderer)
1316{
e7f9f819 1317 Init( context );
b129eaa3
VZ
1318 m_width =
1319 m_height = 0;
184fc6c8
SC
1320}
1321
00bd8e72
SC
1322wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1323: wxGraphicsContext(renderer)
184fc6c8 1324{
136a1de9 1325 m_enableOffset = true;
00bd8e72
SC
1326#ifdef __WXGTK__
1327 // something along these lines (copied from dcclient)
1328
00bd8e72
SC
1329 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1330 // code should still be able to create wxClientDCs for them, so we will
1331 // use the parent window here then.
82a234fb 1332 if (window->m_wxwindow == NULL)
184fc6c8 1333 {
00bd8e72 1334 window = window->GetParent();
184fc6c8 1335 }
00bd8e72 1336
82a234fb 1337 wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
00bd8e72 1338
82a234fb 1339 Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
b129eaa3
VZ
1340
1341 wxSize sz = window->GetSize();
1342 m_width = sz.x;
1343 m_height = sz.y;
00bd8e72 1344#endif
00bd8e72
SC
1345}
1346
1347wxCairoContext::~wxCairoContext()
1348{
00bd8e72
SC
1349 if ( m_context )
1350 {
1351 PopState();
c0e69d72
KO
1352#ifdef __WXMSW__
1353 m_mswSurface = cairo_win32_surface_create((HDC)window->GetHandle());
1354 m_context = cairo_create(m_mswSurface);
1355#endif
00bd8e72
SC
1356 PopState();
1357 cairo_destroy(m_context);
1358 }
c0e69d72
KO
1359#ifdef __WXMSW__
1360 if ( m_mswSurface )
1361 cairo_surface_destroy(m_mswSurface);
1362#endif
00bd8e72
SC
1363}
1364
e7f9f819
SC
1365void wxCairoContext::Init(cairo_t *context)
1366{
1367 m_context = context ;
1368 PushState();
1369 PushState();
1370}
1371
00bd8e72 1372
d9485f89 1373void wxCairoContext::Clip( const wxRegion& region )
00bd8e72 1374{
d9485f89
RD
1375 // Create a path with all the rectangles in the region
1376 wxGraphicsPath path = GetRenderer()->CreatePath();
1377 wxRegionIterator ri(region);
1378 while (ri)
1379 {
1380 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
82a234fb 1381 ++ri;
d9485f89 1382 }
49ad157e 1383
d9485f89
RD
1384 // Put it in the context
1385 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1386 cairo_append_path(m_context, cp);
1387
1388 // clip to that path
1389 cairo_clip(m_context);
49ad157e 1390 path.UnGetNativePath(cp);
00bd8e72
SC
1391}
1392
1393void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1394{
d9485f89
RD
1395 // Create a path with this rectangle
1396 wxGraphicsPath path = GetRenderer()->CreatePath();
1397 path.AddRectangle(x,y,w,h);
1398
1399 // Put it in the context
1400 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1401 cairo_append_path(m_context, cp);
1402
1403 // clip to that path
71696da0 1404 cairo_clip(m_context);
49ad157e 1405 path.UnGetNativePath(cp);
00bd8e72
SC
1406}
1407
1408void wxCairoContext::ResetClip()
1409{
d9485f89 1410 cairo_reset_clip(m_context);
00bd8e72
SC
1411}
1412
1413
0db8a70e 1414void wxCairoContext::StrokePath( const wxGraphicsPath& path )
00bd8e72 1415{
87752530 1416 if ( !m_pen.IsNull() )
49ad157e
VZ
1417 {
1418 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
0db8a70e 1419 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
00bd8e72 1420 cairo_append_path(m_context,cp);
87752530 1421 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
00bd8e72 1422 cairo_stroke(m_context);
0db8a70e 1423 path.UnGetNativePath(cp);
00bd8e72
SC
1424 }
1425}
1426
94a007ec 1427void wxCairoContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
00bd8e72 1428{
87752530 1429 if ( !m_brush.IsNull() )
00bd8e72 1430 {
e7f9f819 1431 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
0db8a70e 1432 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
00bd8e72 1433 cairo_append_path(m_context,cp);
87752530 1434 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
00bd8e72
SC
1435 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1436 cairo_fill(m_context);
0db8a70e 1437 path.UnGetNativePath(cp);
00bd8e72
SC
1438 }
1439}
1440
1441void wxCairoContext::Rotate( wxDouble angle )
1442{
1443 cairo_rotate(m_context,angle);
1444}
1445
1446void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1447{
1448 cairo_translate(m_context,dx,dy);
1449}
1450
1451void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1452{
1453 cairo_scale(m_context,xScale,yScale);
1454}
1455
1456// concatenates this transform with the current transform of this context
0db8a70e 1457void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
00bd8e72 1458{
0db8a70e 1459 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
00bd8e72
SC
1460}
1461
1462// sets the transform of this context
0db8a70e 1463void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
00bd8e72 1464{
0db8a70e 1465 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
00bd8e72
SC
1466}
1467
1468// gets the matrix of this context
0db8a70e 1469wxGraphicsMatrix wxCairoContext::GetTransform() const
00bd8e72 1470{
0db8a70e
RD
1471 wxGraphicsMatrix matrix = CreateMatrix();
1472 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1473 return matrix;
00bd8e72
SC
1474}
1475
1476
1477
1478void wxCairoContext::PushState()
1479{
1480 cairo_save(m_context);
1481}
1482
1483void wxCairoContext::PopState()
1484{
1485 cairo_restore(m_context);
1486}
184fc6c8
SC
1487
1488void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1489{
2fc9c1ea 1490 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
3c10f6db 1491 DrawBitmap(bitmap, x, y, w, h);
d9485f89 1492
2fc9c1ea 1493}
49ad157e 1494
2fc9c1ea
KO
1495void wxCairoContext::DrawBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1496{
d9485f89 1497 PushState();
49ad157e 1498
d9485f89
RD
1499 // In case we're scaling the image by using a width and height different
1500 // than the bitmap's size create a pattern transformation on the surface and
1501 // draw the transformed pattern.
2fc9c1ea
KO
1502 wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData());
1503 cairo_pattern_t* pattern = data->GetCairoPattern();
1504 wxSize size = data->GetSize();
1505
1506 wxDouble scaleX = w / size.GetWidth();
1507 wxDouble scaleY = h / size.GetHeight();
d9485f89
RD
1508
1509 // prepare to draw the image
1510 cairo_translate(m_context, x, y);
ce6b1014 1511 cairo_scale(m_context, scaleX, scaleY);
d9485f89
RD
1512 cairo_set_source(m_context, pattern);
1513 // use the original size here since the context is scaled already...
2fc9c1ea 1514 cairo_rectangle(m_context, 0, 0, size.GetWidth(), size.GetHeight());
d9485f89
RD
1515 // fill the rectangle using the pattern
1516 cairo_fill(m_context);
1517
d9485f89 1518 PopState();
184fc6c8
SC
1519}
1520
1521void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1522{
d9485f89
RD
1523 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1524 // start using the Cairo backend on other platforms then we may need to
1525 // fiddle with this...
1526 DrawBitmap(icon, x, y, w, h);
184fc6c8
SC
1527}
1528
1529
0b7dce54 1530void wxCairoContext::DoDrawText(const wxString& str, wxDouble x, wxDouble y)
184fc6c8 1531{
0b7dce54
VZ
1532 wxCHECK_RET( !m_font.IsNull(),
1533 wxT("wxCairoContext::DrawText - no valid font set") );
1011fbeb
SC
1534
1535 if ( str.empty())
d9485f89 1536 return;
84f67b11 1537
10c508ef 1538 const wxCharBuffer data = str.utf8_str();
e7f9f819
SC
1539 if ( !data )
1540 return;
0b7dce54 1541
84f67b11
SC
1542 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1543
0b7dce54
VZ
1544#ifdef __WXGTK__
1545 size_t datalen = strlen(data);
1546
e7f9f819 1547 PangoLayout *layout = pango_cairo_create_layout (m_context);
0d19936d
FM
1548 wxCairoFontData* font_data = (wxCairoFontData*) m_font.GetRefData();
1549 pango_layout_set_font_description( layout, font_data->GetFont());
84f67b11 1550 pango_layout_set_text(layout, data, datalen);
03647350
VZ
1551
1552 if (font_data->GetUnderlined())
0d19936d
FM
1553 {
1554 PangoAttrList *attrs = pango_attr_list_new();
1555 PangoAttribute *attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
1556 pango_attr_list_insert(attrs, attr);
1557 pango_layout_set_attributes(layout, attrs);
1558 pango_attr_list_unref(attrs);
1559 }
1560
e7f9f819
SC
1561 cairo_move_to(m_context, x, y);
1562 pango_cairo_show_layout (m_context, layout);
1563
1564 g_object_unref (layout);
1565#else
d9485f89
RD
1566 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1567 // the position we move to by the ascent.
1568 cairo_font_extents_t fe;
1569 cairo_font_extents(m_context, &fe);
1570 cairo_move_to(m_context, x, y+fe.ascent);
49ad157e 1571
0b7dce54 1572 cairo_show_text(m_context, data);
e7f9f819 1573#endif
184fc6c8
SC
1574}
1575
1576void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1577 wxDouble *descent, wxDouble *externalLeading ) const
1578{
1011fbeb
SC
1579 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1580
e7f9f819
SC
1581 if ( width )
1582 *width = 0;
1583 if ( height )
1584 *height = 0;
1585 if ( descent )
1586 *descent = 0;
1587 if ( externalLeading )
1588 *externalLeading = 0;
1589
1011fbeb 1590 if ( str.empty())
d9485f89
RD
1591 return;
1592
e7f9f819
SC
1593#ifdef __WXGTK__
1594 int w, h;
49ad157e 1595
e7f9f819 1596 PangoLayout *layout = pango_cairo_create_layout (m_context);
84f67b11 1597 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
10c508ef 1598 const wxCharBuffer data = str.utf8_str();
84f67b11 1599 if ( !data )
e7f9f819
SC
1600 {
1601 return;
1602 }
84f67b11 1603 pango_layout_set_text( layout, data, strlen(data) );
e7f9f819
SC
1604 pango_layout_get_pixel_size (layout, &w, &h);
1605 if ( width )
1606 *width = w;
1607 if ( height )
1608 *height = h;
1609 if (descent)
1610 {
1611 PangoLayoutIter *iter = pango_layout_get_iter(layout);
1612 int baseline = pango_layout_iter_get_baseline(iter);
1613 pango_layout_iter_free(iter);
1614 *descent = h - PANGO_PIXELS(baseline);
1615 }
1616 g_object_unref (layout);
1617#else
d9485f89
RD
1618 ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
1619
1620 if (width)
1621 {
1622 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1623 cairo_text_extents_t te;
1624 cairo_text_extents(m_context, buf, &te);
1625 *width = te.width;
1626 }
1627
1628 if (height || descent || externalLeading)
1629 {
1630 cairo_font_extents_t fe;
1631 cairo_font_extents(m_context, &fe);
49ad157e 1632
3e700936 1633 // some backends have negative descents
49ad157e 1634
3e700936
SC
1635 if ( fe.descent < 0 )
1636 fe.descent = -fe.descent;
49ad157e 1637
3e700936
SC
1638 if ( fe.height < (fe.ascent + fe.descent ) )
1639 {
1640 // some backends are broken re height ... (eg currently ATSUI)
1641 fe.height = fe.ascent + fe.descent;
1642 }
49ad157e 1643
d9485f89
RD
1644 if (height)
1645 *height = fe.height;
1646 if ( descent )
1647 *descent = fe.descent;
1648 if ( externalLeading )
1649 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
1650 }
e7f9f819 1651#endif
184fc6c8
SC
1652}
1653
1654void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1655{
1656 widths.Empty();
1657 widths.Add(0, text.length());
1658
1011fbeb
SC
1659 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1660
184fc6c8
SC
1661 if (text.empty())
1662 return;
00bd8e72
SC
1663
1664 // TODO
184fc6c8
SC
1665}
1666
49ad157e 1667void * wxCairoContext::GetNativeContext()
184fc6c8 1668{
00bd8e72
SC
1669 return m_context;
1670}
184fc6c8 1671
bf02a7f9 1672bool wxCairoContext::SetAntialiasMode(wxAntialiasMode antialias)
49ad157e 1673{
bf02a7f9 1674 if (m_antialias == antialias)
49ad157e
VZ
1675 return true;
1676
bf02a7f9 1677 m_antialias = antialias;
03647350 1678
bf02a7f9
SC
1679 cairo_antialias_t antialiasMode;
1680 switch (antialias)
1681 {
1682 case wxANTIALIAS_DEFAULT:
1683 antialiasMode = CAIRO_ANTIALIAS_DEFAULT;
1684 break;
1685 case wxANTIALIAS_NONE:
1686 antialiasMode = CAIRO_ANTIALIAS_NONE;
1687 break;
1688 default:
1689 return false;
1690 }
1691 cairo_set_antialias(m_context, antialiasMode);
1692 return true;
1693}
49ad157e 1694
bf02a7f9
SC
1695bool wxCairoContext::SetCompositionMode(wxCompositionMode op)
1696{
1697 if ( m_composition == op )
1698 return true;
03647350 1699
bf02a7f9
SC
1700 m_composition = op;
1701 cairo_operator_t cop;
1702 switch (op)
49ad157e 1703 {
50de831a 1704 case wxCOMPOSITION_CLEAR:
bf02a7f9 1705 cop = CAIRO_OPERATOR_CLEAR;
49ad157e 1706 break;
bf02a7f9
SC
1707 case wxCOMPOSITION_SOURCE:
1708 cop = CAIRO_OPERATOR_SOURCE;
49ad157e 1709 break;
bf02a7f9
SC
1710 case wxCOMPOSITION_OVER:
1711 cop = CAIRO_OPERATOR_OVER;
49ad157e 1712 break;
bf02a7f9
SC
1713 case wxCOMPOSITION_IN:
1714 cop = CAIRO_OPERATOR_IN;
1715 break;
1716 case wxCOMPOSITION_OUT:
1717 cop = CAIRO_OPERATOR_OUT;
1718 break;
1719 case wxCOMPOSITION_ATOP:
1720 cop = CAIRO_OPERATOR_ATOP;
1721 break;
1722 case wxCOMPOSITION_DEST:
1723 cop = CAIRO_OPERATOR_DEST;
1724 break;
1725 case wxCOMPOSITION_DEST_OVER:
1726 cop = CAIRO_OPERATOR_DEST_OVER;
1727 break;
1728 case wxCOMPOSITION_DEST_IN:
1729 cop = CAIRO_OPERATOR_DEST_IN;
1730 break;
1731 case wxCOMPOSITION_DEST_OUT:
1732 cop = CAIRO_OPERATOR_DEST_OUT;
1733 break;
1734 case wxCOMPOSITION_DEST_ATOP:
1735 cop = CAIRO_OPERATOR_DEST_ATOP;
1736 break;
1737 case wxCOMPOSITION_XOR:
1738 cop = CAIRO_OPERATOR_XOR;
1739 break;
1740 case wxCOMPOSITION_ADD:
1741 cop = CAIRO_OPERATOR_ADD;
49ad157e 1742 break;
49ad157e
VZ
1743 default:
1744 return false;
1745 }
bf02a7f9 1746 cairo_set_operator(m_context, cop);
49ad157e
VZ
1747 return true;
1748}
1749
bf02a7f9
SC
1750void wxCairoContext::BeginLayer(wxDouble opacity)
1751{
1752 m_layerOpacities.push_back(opacity);
1753 cairo_push_group(m_context);
1754}
49ad157e 1755
bf02a7f9
SC
1756void wxCairoContext::EndLayer()
1757{
1758 float opacity = m_layerOpacities.back();
1759 m_layerOpacities.pop_back();
1760 cairo_pop_group_to_source(m_context);
1761 cairo_paint_with_alpha(m_context,opacity);
1762}
03647350 1763
00bd8e72
SC
1764//-----------------------------------------------------------------------------
1765// wxCairoRenderer declaration
1766//-----------------------------------------------------------------------------
1767
1768class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1769{
1770public :
1771 wxCairoRenderer() {}
1772
1773 virtual ~wxCairoRenderer() {}
1774
1775 // Context
1776
1777 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
773ccc31 1778 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
0b822969 1779 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
773ccc31 1780
00bd8e72
SC
1781 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1782
1783 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1784
1785 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1786
091ef146
SC
1787 virtual wxGraphicsContext * CreateMeasuringContext();
1788
00bd8e72
SC
1789 // Path
1790
0db8a70e 1791 virtual wxGraphicsPath CreatePath();
00bd8e72
SC
1792
1793 // Matrix
1794
49ad157e 1795 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
00bd8e72
SC
1796 wxDouble tx=0.0, wxDouble ty=0.0);
1797
1798
87752530 1799 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
00bd8e72 1800
87752530 1801 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
00bd8e72 1802
4ee4c7b9
VZ
1803 virtual wxGraphicsBrush
1804 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
1805 wxDouble x2, wxDouble y2,
1806 const wxGraphicsGradientStops& stops);
00bd8e72 1807
4ee4c7b9
VZ
1808 virtual wxGraphicsBrush
1809 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
1810 wxDouble xc, wxDouble yc,
1811 wxDouble radius,
1812 const wxGraphicsGradientStops& stops);
00bd8e72
SC
1813
1814 // sets the font
87752530 1815 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
00bd8e72 1816
d974a494 1817 // create a native bitmap representation
2fc9c1ea 1818 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
49ad157e 1819
2986eb86
SC
1820 // create a graphics bitmap from a native bitmap
1821 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
1822
d974a494 1823 // create a subimage from a native image representation
2fc9c1ea 1824 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
d974a494 1825
00bd8e72
SC
1826private :
1827 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1828} ;
1829
1830//-----------------------------------------------------------------------------
1831// wxCairoRenderer implementation
1832//-----------------------------------------------------------------------------
1833
1834IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1835
1836static wxCairoRenderer gs_cairoGraphicsRenderer;
3e700936
SC
1837// temporary hack to allow creating a cairo context on any platform
1838extern wxGraphicsRenderer* gCairoRenderer;
1839wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer;
00bd8e72 1840
00bd8e72 1841wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
539e2795 1842{
00bd8e72
SC
1843 return new wxCairoContext(this,dc);
1844}
1845
773ccc31
SC
1846wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
1847{
888dde65 1848 return new wxCairoContext(this,dc);
773ccc31 1849}
773ccc31 1850
0b822969
RR
1851wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc)
1852{
c9008abe 1853#ifdef __WXGTK20__
0b822969
RR
1854 const wxDCImpl *impl = dc.GetImpl();
1855 cairo_t* context = (cairo_t*) impl->GetCairoContext();
1856 if (context)
c9008abe 1857 return new wxCairoContext(this,dc);
0b822969 1858 else
c9008abe 1859#endif
0b822969
RR
1860 return NULL;
1861}
1862
00bd8e72
SC
1863wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1864{
a8f9fe13 1865#ifdef __WXMSW__
c0e69d72 1866 return new wxCairoContext(this,(HDC)context);
711a4812 1867#else
00bd8e72 1868 return new wxCairoContext(this,(cairo_t*)context);
c0e69d72 1869#endif
00bd8e72
SC
1870}
1871
1872
1873wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1874{
1875#ifdef __WXGTK__
ed1b38a8 1876 return new wxCairoContext(this,(GdkDrawable*)window);
00bd8e72
SC
1877#else
1878 return NULL;
1879#endif
1880}
1881
091ef146
SC
1882wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
1883{
5e8d27fe
KO
1884#ifdef __WXGTK__
1885 return CreateContextFromNativeWindow(gdk_get_default_root_window());
1886#endif
091ef146
SC
1887 return NULL;
1888 // TODO
1889}
1890
00bd8e72
SC
1891wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1892{
1893 return new wxCairoContext(this, window );
1894}
1895
1896// Path
1897
0db8a70e 1898wxGraphicsPath wxCairoRenderer::CreatePath()
00bd8e72 1899{
0db8a70e
RD
1900 wxGraphicsPath path;
1901 path.SetRefData( new wxCairoPathData(this) );
1902 return path;
539e2795
SC
1903}
1904
00bd8e72
SC
1905
1906// Matrix
1907
49ad157e 1908wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
0db8a70e 1909 wxDouble tx, wxDouble ty)
00bd8e72 1910
184fc6c8 1911{
0db8a70e
RD
1912 wxGraphicsMatrix m;
1913 wxCairoMatrixData* data = new wxCairoMatrixData( this );
1914 data->Set( a,b,c,d,tx,ty ) ;
1915 m.SetRefData(data);
00bd8e72 1916 return m;
7ba86d93
RD
1917}
1918
49ad157e 1919wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
539e2795 1920{
a1b806b9 1921 if ( !pen.IsOk() || pen.GetStyle() == wxPENSTYLE_TRANSPARENT )
87752530 1922 return wxNullGraphicsPen;
00bd8e72 1923 else
87752530
SC
1924 {
1925 wxGraphicsPen p;
1926 p.SetRefData(new wxCairoPenData( this, pen ));
1927 return p;
1928 }
539e2795
SC
1929}
1930
49ad157e 1931wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
539e2795 1932{
a1b806b9 1933 if ( !brush.IsOk() || brush.GetStyle() == wxBRUSHSTYLE_TRANSPARENT )
87752530 1934 return wxNullGraphicsBrush;
00bd8e72 1935 else
87752530
SC
1936 {
1937 wxGraphicsBrush p;
1938 p.SetRefData(new wxCairoBrushData( this, brush ));
1939 return p;
1940 }
00bd8e72
SC
1941}
1942
4ee4c7b9
VZ
1943wxGraphicsBrush
1944wxCairoRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
1945 wxDouble x2, wxDouble y2,
1946 const wxGraphicsGradientStops& stops)
00bd8e72 1947{
87752530
SC
1948 wxGraphicsBrush p;
1949 wxCairoBrushData* d = new wxCairoBrushData( this );
4ee4c7b9 1950 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
87752530
SC
1951 p.SetRefData(d);
1952 return p;
00bd8e72
SC
1953}
1954
4ee4c7b9
VZ
1955wxGraphicsBrush
1956wxCairoRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
1957 wxDouble xc, wxDouble yc, wxDouble r,
1958 const wxGraphicsGradientStops& stops)
00bd8e72 1959{
87752530
SC
1960 wxGraphicsBrush p;
1961 wxCairoBrushData* d = new wxCairoBrushData( this );
4ee4c7b9 1962 d->CreateRadialGradientBrush(xo, yo, xc, yc, r, stops);
87752530
SC
1963 p.SetRefData(d);
1964 return p;
00bd8e72
SC
1965}
1966
1967// sets the font
49ad157e 1968wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
00bd8e72 1969{
a1b806b9 1970 if ( font.IsOk() )
49ad157e 1971 {
87752530
SC
1972 wxGraphicsFont p;
1973 p.SetRefData(new wxCairoFontData( this , font, col ));
1974 return p;
1975 }
00bd8e72 1976 else
87752530 1977 return wxNullGraphicsFont;
539e2795
SC
1978}
1979
f5008769 1980wxGraphicsBitmap wxCairoRenderer::CreateBitmap( const wxBitmap& bmp )
2fc9c1ea 1981{
a1b806b9 1982 if ( bmp.IsOk() )
2fc9c1ea
KO
1983 {
1984 wxGraphicsBitmap p;
1985 p.SetRefData(new wxCairoBitmapData( this , bmp ));
1986 return p;
1987 }
1988 else
1989 return wxNullGraphicsBitmap;
1990}
1991
2986eb86
SC
1992wxGraphicsBitmap wxCairoRenderer::CreateBitmapFromNativeBitmap( void* bitmap )
1993{
1994 if ( bitmap != NULL )
1995 {
1996 wxGraphicsBitmap p;
1997 p.SetRefData(new wxCairoBitmapData( this , (cairo_surface_t*) bitmap ));
1998 return p;
1999 }
2000 else
2001 return wxNullGraphicsBitmap;
2002}
2003
84e0e526
VZ
2004wxGraphicsBitmap
2005wxCairoRenderer::CreateSubBitmap(const wxGraphicsBitmap& WXUNUSED(bitmap),
2006 wxDouble WXUNUSED(x),
2007 wxDouble WXUNUSED(y),
2008 wxDouble WXUNUSED(w),
2009 wxDouble WXUNUSED(h))
2fc9c1ea
KO
2010{
2011 wxFAIL_MSG("wxCairoRenderer::CreateSubBitmap is not implemented.");
2012 return wxNullGraphicsBitmap;
2013}
2014
c0e69d72
KO
2015wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
2016{
c0e69d72 2017 return &gs_cairoGraphicsRenderer;
ef40c54c
VZ
2018}
2019
2020#else // !wxUSE_CAIRO
2021
2022wxGraphicsRenderer* wxGraphicsRenderer::GetCairoRenderer()
2023{
c0e69d72 2024 return NULL;
c0e69d72 2025}
9a67941f 2026
ef40c54c
VZ
2027#endif // wxUSE_CAIRO/!wxUSE_CAIRO
2028
2029// MSW and OS X have their own native default renderers, but the other ports
2030// use Cairo by default
2031#if !(defined(__WXMSW__) || defined(__WXOSX__))
2032wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
2033{
2034 return GetCairoRenderer();
2035}
2036#endif // !(__WXMSW__ || __WXOSX__)
2037
9a67941f 2038#endif // wxUSE_GRAPHICS_CONTEXT