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