]> git.saurik.com Git - wxWidgets.git/blob - src/generic/graphicc.cpp
VC6 compilation fix: using access declaration can only be used with immediate base...
[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/osx/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 wxPoint org = dc.GetDeviceOrigin();
1046 cairo_translate( m_context, org.x, org.y );
1047
1048 double sx,sy;
1049 dc.GetUserScale( &sx, &sy );
1050 cairo_scale( m_context, sx, sy );
1051
1052 org = dc.GetLogicalOrigin();
1053 cairo_translate( m_context, -org.x, -org.y );
1054 #endif
1055 }
1056
1057 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1058 : wxGraphicsContext(renderer)
1059 {
1060 #ifdef __WXGTK20__
1061 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1062 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1063
1064 #if 0
1065 wxGraphicsMatrix matrix = CreateMatrix();
1066
1067 wxPoint org = dc.GetDeviceOrigin();
1068 matrix.Translate( org.x, org.y );
1069
1070 org = dc.GetLogicalOrigin();
1071 matrix.Translate( -org.x, -org.y );
1072
1073 double sx,sy;
1074 dc.GetUserScale( &sx, &sy );
1075 matrix.Scale( sx, sy );
1076
1077 ConcatTransform( matrix );
1078 #endif
1079 #endif
1080
1081 #ifdef __WXMAC__
1082 int width, height;
1083 dc.GetSize( &width, &height );
1084 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1085 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1086 Init( cairo_create( surface ) );
1087 cairo_surface_destroy( surface );
1088 #endif
1089 }
1090
1091 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc )
1092 : wxGraphicsContext(renderer)
1093 {
1094 #ifdef __WXGTK20__
1095 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1096 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1097
1098 #if 0
1099 wxGraphicsMatrix matrix = CreateMatrix();
1100
1101 wxPoint org = dc.GetDeviceOrigin();
1102 matrix.Translate( org.x, org.y );
1103
1104 org = dc.GetLogicalOrigin();
1105 matrix.Translate( -org.x, -org.y );
1106
1107 double sx,sy;
1108 dc.GetUserScale( &sx, &sy );
1109 matrix.Scale( sx, sy );
1110
1111 ConcatTransform( matrix );
1112 #endif
1113 #endif
1114
1115 #ifdef __WXMAC__
1116 int width, height;
1117 dc.GetSize( &width, &height );
1118 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1119 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1120 Init( cairo_create( surface ) );
1121 cairo_surface_destroy( surface );
1122 #endif
1123 }
1124
1125 #ifdef __WXGTK20__
1126 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1127 : wxGraphicsContext(renderer)
1128 {
1129 Init( gdk_cairo_create( drawable ) );
1130 }
1131 #endif
1132
1133 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1134 : wxGraphicsContext(renderer)
1135 {
1136 Init( context );
1137 }
1138
1139 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1140 : wxGraphicsContext(renderer)
1141 {
1142 #ifdef __WXGTK__
1143 // something along these lines (copied from dcclient)
1144
1145 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1146 // code should still be able to create wxClientDCs for them, so we will
1147 // use the parent window here then.
1148 if (window->m_wxwindow == NULL)
1149 {
1150 window = window->GetParent();
1151 }
1152
1153 wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
1154
1155 Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
1156 #endif
1157 }
1158
1159 wxCairoContext::~wxCairoContext()
1160 {
1161 if ( m_context )
1162 {
1163 PopState();
1164 PopState();
1165 cairo_destroy(m_context);
1166 }
1167 }
1168
1169 void wxCairoContext::Init(cairo_t *context)
1170 {
1171 m_context = context ;
1172 PushState();
1173 PushState();
1174 }
1175
1176
1177 void wxCairoContext::Clip( const wxRegion& region )
1178 {
1179 // Create a path with all the rectangles in the region
1180 wxGraphicsPath path = GetRenderer()->CreatePath();
1181 wxRegionIterator ri(region);
1182 while (ri)
1183 {
1184 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
1185 ++ri;
1186 }
1187
1188 // Put it in the context
1189 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1190 cairo_append_path(m_context, cp);
1191
1192 // clip to that path
1193 cairo_clip(m_context);
1194 path.UnGetNativePath(cp);
1195 }
1196
1197 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1198 {
1199 // Create a path with this rectangle
1200 wxGraphicsPath path = GetRenderer()->CreatePath();
1201 path.AddRectangle(x,y,w,h);
1202
1203 // Put it in the context
1204 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1205 cairo_append_path(m_context, cp);
1206
1207 // clip to that path
1208 cairo_clip(m_context);
1209 path.UnGetNativePath(cp);
1210 }
1211
1212 void wxCairoContext::ResetClip()
1213 {
1214 cairo_reset_clip(m_context);
1215 }
1216
1217
1218 void wxCairoContext::StrokePath( const wxGraphicsPath& path )
1219 {
1220 if ( !m_pen.IsNull() )
1221 {
1222 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1223 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1224 cairo_append_path(m_context,cp);
1225 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1226 cairo_stroke(m_context);
1227 path.UnGetNativePath(cp);
1228 }
1229 }
1230
1231 void wxCairoContext::FillPath( const wxGraphicsPath& path , int fillStyle )
1232 {
1233 if ( !m_brush.IsNull() )
1234 {
1235 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1236 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1237 cairo_append_path(m_context,cp);
1238 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1239 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1240 cairo_fill(m_context);
1241 path.UnGetNativePath(cp);
1242 }
1243 }
1244
1245 void wxCairoContext::Rotate( wxDouble angle )
1246 {
1247 cairo_rotate(m_context,angle);
1248 }
1249
1250 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1251 {
1252 cairo_translate(m_context,dx,dy);
1253 }
1254
1255 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1256 {
1257 cairo_scale(m_context,xScale,yScale);
1258 }
1259
1260 // concatenates this transform with the current transform of this context
1261 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1262 {
1263 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
1264 }
1265
1266 // sets the transform of this context
1267 void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
1268 {
1269 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
1270 }
1271
1272 // gets the matrix of this context
1273 wxGraphicsMatrix wxCairoContext::GetTransform() const
1274 {
1275 wxGraphicsMatrix matrix = CreateMatrix();
1276 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1277 return matrix;
1278 }
1279
1280
1281
1282 void wxCairoContext::PushState()
1283 {
1284 cairo_save(m_context);
1285 }
1286
1287 void wxCairoContext::PopState()
1288 {
1289 cairo_restore(m_context);
1290 }
1291
1292 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1293 {
1294 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1295
1296 cairo_surface_t* surface;
1297 int bw = bmp.GetWidth();
1298 int bh = bmp.GetHeight();
1299 wxBitmap bmpSource = bmp; // we need a non-const instance
1300 unsigned char* buffer = new unsigned char[bw*bh*4];
1301 wxUint32* data = (wxUint32*)buffer;
1302
1303 // Create a surface object and copy the bitmap pixel data to it. if the
1304 // image has alpha (or a mask represented as alpha) then we'll use a
1305 // different format and iterator than if it doesn't...
1306 if (bmpSource.HasAlpha() || bmpSource.GetMask())
1307 {
1308 surface = cairo_image_surface_create_for_data(
1309 buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1310 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1311 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1312
1313 wxAlphaPixelData::Iterator p(pixData);
1314 for (int y=0; y<bh; y++)
1315 {
1316 wxAlphaPixelData::Iterator rowStart = p;
1317 for (int x=0; x<bw; x++)
1318 {
1319 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1320 // with alpha in the upper 8 bits, then red, then green, then
1321 // blue. The 32-bit quantities are stored native-endian.
1322 // Pre-multiplied alpha is used.
1323 unsigned char alpha = p.Alpha();
1324 if (alpha == 0)
1325 *data = 0;
1326 else
1327 *data = ( alpha << 24
1328 | (p.Red() * alpha/255) << 16
1329 | (p.Green() * alpha/255) << 8
1330 | (p.Blue() * alpha/255) );
1331 ++data;
1332 ++p;
1333 }
1334 p = rowStart;
1335 p.OffsetY(pixData, 1);
1336 }
1337 }
1338 else // no alpha
1339 {
1340 surface = cairo_image_surface_create_for_data(
1341 buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1342 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1343 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1344
1345 wxNativePixelData::Iterator p(pixData);
1346 for (int y=0; y<bh; y++)
1347 {
1348 wxNativePixelData::Iterator rowStart = p;
1349 for (int x=0; x<bw; x++)
1350 {
1351 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1352 // the upper 8 bits unused. Red, Green, and Blue are stored in
1353 // the remaining 24 bits in that order. The 32-bit quantities
1354 // are stored native-endian.
1355 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1356 ++data;
1357 ++p;
1358 }
1359 p = rowStart;
1360 p.OffsetY(pixData, 1);
1361 }
1362 }
1363
1364
1365 PushState();
1366
1367 // In case we're scaling the image by using a width and height different
1368 // than the bitmap's size create a pattern transformation on the surface and
1369 // draw the transformed pattern.
1370 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
1371 wxDouble scaleX = w / bw;
1372 wxDouble scaleY = h / bh;
1373 cairo_scale(m_context, scaleX, scaleY);
1374
1375 // prepare to draw the image
1376 cairo_translate(m_context, x, y);
1377 cairo_set_source(m_context, pattern);
1378 // use the original size here since the context is scaled already...
1379 cairo_rectangle(m_context, 0, 0, bw, bh);
1380 // fill the rectangle using the pattern
1381 cairo_fill(m_context);
1382
1383 // clean up
1384 cairo_pattern_destroy(pattern);
1385 cairo_surface_destroy(surface);
1386 delete [] buffer;
1387 PopState();
1388 }
1389
1390 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1391 {
1392 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1393 // start using the Cairo backend on other platforms then we may need to
1394 // fiddle with this...
1395 DrawBitmap(icon, x, y, w, h);
1396 }
1397
1398
1399 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1400 {
1401 if ( m_font.IsNull() || str.empty())
1402 return;
1403
1404 #ifdef __WXGTK__
1405 const wxCharBuffer data = str.utf8_str();
1406 if ( !data )
1407 return;
1408 size_t datalen = strlen(data);
1409 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1410
1411 PangoLayout *layout = pango_cairo_create_layout (m_context);
1412 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1413 pango_layout_set_text(layout, data, datalen);
1414 cairo_move_to(m_context, x, y);
1415 pango_cairo_show_layout (m_context, layout);
1416
1417 g_object_unref (layout);
1418 #else
1419 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1420 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1421 // the position we move to by the ascent.
1422 cairo_font_extents_t fe;
1423 cairo_font_extents(m_context, &fe);
1424 cairo_move_to(m_context, x, y+fe.ascent);
1425
1426 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1427 cairo_show_text(m_context,buf);
1428 #endif
1429 }
1430
1431 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1432 wxDouble *descent, wxDouble *externalLeading ) const
1433 {
1434 if ( width )
1435 *width = 0;
1436 if ( height )
1437 *height = 0;
1438 if ( descent )
1439 *descent = 0;
1440 if ( externalLeading )
1441 *externalLeading = 0;
1442
1443 if ( m_font.IsNull() || str.empty())
1444 return;
1445
1446 #ifdef __WXGTK__
1447 int w, h;
1448
1449 PangoLayout *layout = pango_cairo_create_layout (m_context);
1450 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1451 const wxCharBuffer data = str.utf8_str();
1452 if ( !data )
1453 {
1454 return;
1455 }
1456 pango_layout_set_text( layout, data, strlen(data) );
1457 pango_layout_get_pixel_size (layout, &w, &h);
1458 if ( width )
1459 *width = w;
1460 if ( height )
1461 *height = h;
1462 if (descent)
1463 {
1464 PangoLayoutIter *iter = pango_layout_get_iter(layout);
1465 int baseline = pango_layout_iter_get_baseline(iter);
1466 pango_layout_iter_free(iter);
1467 *descent = h - PANGO_PIXELS(baseline);
1468 }
1469 g_object_unref (layout);
1470 #else
1471 ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
1472
1473 if (width)
1474 {
1475 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1476 cairo_text_extents_t te;
1477 cairo_text_extents(m_context, buf, &te);
1478 *width = te.width;
1479 }
1480
1481 if (height || descent || externalLeading)
1482 {
1483 cairo_font_extents_t fe;
1484 cairo_font_extents(m_context, &fe);
1485
1486 // some backends have negative descents
1487
1488 if ( fe.descent < 0 )
1489 fe.descent = -fe.descent;
1490
1491 if ( fe.height < (fe.ascent + fe.descent ) )
1492 {
1493 // some backends are broken re height ... (eg currently ATSUI)
1494 fe.height = fe.ascent + fe.descent;
1495 }
1496
1497 if (height)
1498 *height = fe.height;
1499 if ( descent )
1500 *descent = fe.descent;
1501 if ( externalLeading )
1502 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
1503 }
1504 #endif
1505 }
1506
1507 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1508 {
1509 widths.Empty();
1510 widths.Add(0, text.length());
1511
1512 if (text.empty())
1513 return;
1514
1515 // TODO
1516 }
1517
1518 void * wxCairoContext::GetNativeContext()
1519 {
1520 return m_context;
1521 }
1522
1523 //-----------------------------------------------------------------------------
1524 // wxCairoRenderer declaration
1525 //-----------------------------------------------------------------------------
1526
1527 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1528 {
1529 public :
1530 wxCairoRenderer() {}
1531
1532 virtual ~wxCairoRenderer() {}
1533
1534 // Context
1535
1536 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1537 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1538 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
1539
1540 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1541
1542 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1543
1544 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1545
1546 virtual wxGraphicsContext * CreateMeasuringContext();
1547
1548 // Path
1549
1550 virtual wxGraphicsPath CreatePath();
1551
1552 // Matrix
1553
1554 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1555 wxDouble tx=0.0, wxDouble ty=0.0);
1556
1557
1558 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1559
1560 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1561
1562 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1563 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1564 const wxColour&c1, const wxColour&c2) ;
1565
1566 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1567 // with radius r and color cColor
1568 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1569 const wxColour &oColor, const wxColour &cColor) ;
1570
1571 // sets the font
1572 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1573
1574 // create a native bitmap representation
1575 #if 0
1576 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap )
1577 {
1578 return wxGraphicsBitmap;
1579 }
1580
1581 // create a subimage from a native image representation
1582 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1583 {
1584 return wxGraphicsBitmap;
1585 }
1586 #endif
1587
1588 private :
1589 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1590 } ;
1591
1592 //-----------------------------------------------------------------------------
1593 // wxCairoRenderer implementation
1594 //-----------------------------------------------------------------------------
1595
1596 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1597
1598 static wxCairoRenderer gs_cairoGraphicsRenderer;
1599 // temporary hack to allow creating a cairo context on any platform
1600 extern wxGraphicsRenderer* gCairoRenderer;
1601 wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer;
1602
1603 #ifdef __WXGTK__
1604 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1605 {
1606 return &gs_cairoGraphicsRenderer;
1607 }
1608 #endif
1609
1610 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1611 {
1612 return new wxCairoContext(this,dc);
1613 }
1614
1615 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
1616 {
1617 return new wxCairoContext(this,dc);
1618 }
1619
1620 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc)
1621 {
1622 #ifdef __WXGTK20__
1623 const wxDCImpl *impl = dc.GetImpl();
1624 cairo_t* context = (cairo_t*) impl->GetCairoContext();
1625 if (context)
1626 return new wxCairoContext(this,dc);
1627 else
1628 #endif
1629 return NULL;
1630 }
1631
1632 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1633 {
1634 return new wxCairoContext(this,(cairo_t*)context);
1635 }
1636
1637
1638 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1639 {
1640 #ifdef __WXGTK__
1641 return new wxCairoContext(this,(GdkDrawable*)window);
1642 #else
1643 return NULL;
1644 #endif
1645 }
1646
1647 wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
1648 {
1649 return NULL;
1650 // TODO
1651 }
1652
1653 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1654 {
1655 return new wxCairoContext(this, window );
1656 }
1657
1658 // Path
1659
1660 wxGraphicsPath wxCairoRenderer::CreatePath()
1661 {
1662 wxGraphicsPath path;
1663 path.SetRefData( new wxCairoPathData(this) );
1664 return path;
1665 }
1666
1667
1668 // Matrix
1669
1670 wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1671 wxDouble tx, wxDouble ty)
1672
1673 {
1674 wxGraphicsMatrix m;
1675 wxCairoMatrixData* data = new wxCairoMatrixData( this );
1676 data->Set( a,b,c,d,tx,ty ) ;
1677 m.SetRefData(data);
1678 return m;
1679 }
1680
1681 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
1682 {
1683 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1684 return wxNullGraphicsPen;
1685 else
1686 {
1687 wxGraphicsPen p;
1688 p.SetRefData(new wxCairoPenData( this, pen ));
1689 return p;
1690 }
1691 }
1692
1693 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
1694 {
1695 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1696 return wxNullGraphicsBrush;
1697 else
1698 {
1699 wxGraphicsBrush p;
1700 p.SetRefData(new wxCairoBrushData( this, brush ));
1701 return p;
1702 }
1703 }
1704
1705 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1706 wxGraphicsBrush wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1707 const wxColour&c1, const wxColour&c2)
1708 {
1709 wxGraphicsBrush p;
1710 wxCairoBrushData* d = new wxCairoBrushData( this );
1711 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1712 p.SetRefData(d);
1713 return p;
1714 }
1715
1716 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1717 // with radius r and color cColor
1718 wxGraphicsBrush wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1719 const wxColour &oColor, const wxColour &cColor)
1720 {
1721 wxGraphicsBrush p;
1722 wxCairoBrushData* d = new wxCairoBrushData( this );
1723 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1724 p.SetRefData(d);
1725 return p;
1726 }
1727
1728 // sets the font
1729 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1730 {
1731 if ( font.Ok() )
1732 {
1733 wxGraphicsFont p;
1734 p.SetRefData(new wxCairoFontData( this , font, col ));
1735 return p;
1736 }
1737 else
1738 return wxNullGraphicsFont;
1739 }
1740
1741 #endif // wxUSE_GRAPHICS_CONTEXT