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