]> git.saurik.com Git - wxWidgets.git/blob - src/generic/graphicc.cpp
extract wrapper for SystemParametersInfo(SPI_GETNONCLIENTMETRICS) in a header so...
[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 bool SetLogicalFunction( int function );
360
361 virtual void StrokePath( const wxGraphicsPath& p );
362 virtual void FillPath( const wxGraphicsPath& p , int fillStyle = wxWINDING_RULE );
363
364 virtual void Translate( wxDouble dx , wxDouble dy );
365 virtual void Scale( wxDouble xScale , wxDouble yScale );
366 virtual void Rotate( wxDouble angle );
367
368 // concatenates this transform with the current transform of this context
369 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
370
371 // sets the transform of this context
372 virtual void SetTransform( const wxGraphicsMatrix& matrix );
373
374 // gets the matrix of this context
375 virtual wxGraphicsMatrix GetTransform() const;
376
377 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
378 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
379 virtual void PushState();
380 virtual void PopState();
381
382 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
383 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
384 wxDouble *descent, wxDouble *externalLeading ) const;
385 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
386
387 private:
388 void Init(cairo_t *context);
389
390 cairo_t* m_context;
391 };
392
393 //-----------------------------------------------------------------------------
394 // wxCairoPenData implementation
395 //-----------------------------------------------------------------------------
396
397 wxCairoPenData::~wxCairoPenData()
398 {
399 delete[] m_userLengths;
400 }
401
402 void wxCairoPenData::Init()
403 {
404 m_lengths = NULL;
405 m_userLengths = NULL;
406 m_width = 0;
407 m_count = 0;
408 }
409
410 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
411 : wxGraphicsObjectRefData(renderer)
412 {
413 Init();
414 m_pen = pen;
415 m_width = m_pen.GetWidth();
416 if (m_width <= 0.0)
417 m_width = 0.1;
418
419 m_red = m_pen.GetColour().Red()/255.0;
420 m_green = m_pen.GetColour().Green()/255.0;
421 m_blue = m_pen.GetColour().Blue()/255.0;
422 m_alpha = m_pen.GetColour().Alpha()/255.0;
423
424 switch ( m_pen.GetCap() )
425 {
426 case wxCAP_ROUND :
427 m_cap = CAIRO_LINE_CAP_ROUND;
428 break;
429
430 case wxCAP_PROJECTING :
431 m_cap = CAIRO_LINE_CAP_SQUARE;
432 break;
433
434 case wxCAP_BUTT :
435 m_cap = CAIRO_LINE_CAP_BUTT;
436 break;
437
438 default :
439 m_cap = CAIRO_LINE_CAP_BUTT;
440 break;
441 }
442
443 switch ( m_pen.GetJoin() )
444 {
445 case wxJOIN_BEVEL :
446 m_join = CAIRO_LINE_JOIN_BEVEL;
447 break;
448
449 case wxJOIN_MITER :
450 m_join = CAIRO_LINE_JOIN_MITER;
451 break;
452
453 case wxJOIN_ROUND :
454 m_join = CAIRO_LINE_JOIN_ROUND;
455 break;
456
457 default :
458 m_join = CAIRO_LINE_JOIN_MITER;
459 break;
460 }
461
462 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
463 const double dotted[] =
464 {
465 dashUnit , dashUnit + 2.0
466 };
467 static const double short_dashed[] =
468 {
469 9.0 , 6.0
470 };
471 static const double dashed[] =
472 {
473 19.0 , 9.0
474 };
475 static const double dotted_dashed[] =
476 {
477 9.0 , 6.0 , 3.0 , 3.0
478 };
479
480 switch ( m_pen.GetStyle() )
481 {
482 case wxPENSTYLE_SOLID :
483 break;
484
485 case wxPENSTYLE_DOT :
486 m_count = WXSIZEOF(dotted);
487 m_userLengths = new double[ m_count ] ;
488 memcpy( m_userLengths, dotted, sizeof(dotted) );
489 m_lengths = m_userLengths;
490 break;
491
492 case wxPENSTYLE_LONG_DASH :
493 m_lengths = dashed ;
494 m_count = WXSIZEOF(dashed);
495 break;
496
497 case wxPENSTYLE_SHORT_DASH :
498 m_lengths = short_dashed ;
499 m_count = WXSIZEOF(short_dashed);
500 break;
501
502 case wxPENSTYLE_DOT_DASH :
503 m_lengths = dotted_dashed ;
504 m_count = WXSIZEOF(dotted_dashed);
505 break;
506
507 case wxPENSTYLE_USER_DASH :
508 {
509 wxDash *wxdashes ;
510 m_count = m_pen.GetDashes( &wxdashes ) ;
511 if ((wxdashes != NULL) && (m_count > 0))
512 {
513 m_userLengths = new double[m_count] ;
514 for ( int i = 0 ; i < m_count ; ++i )
515 {
516 m_userLengths[i] = wxdashes[i] * dashUnit ;
517
518 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
519 m_userLengths[i] = dashUnit + 2.0 ;
520 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
521 m_userLengths[i] = dashUnit ;
522 }
523 }
524 m_lengths = m_userLengths ;
525 }
526 break;
527 case wxPENSTYLE_STIPPLE :
528 {
529 /*
530 wxBitmap* bmp = pen.GetStipple();
531 if ( bmp && bmp->Ok() )
532 {
533 wxDELETE( m_penImage );
534 wxDELETE( m_penBrush );
535 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
536 m_penBrush = new TextureBrush(m_penImage);
537 m_pen->SetBrush( m_penBrush );
538 }
539 */
540 }
541 break;
542 default :
543 if ( m_pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH
544 && m_pen.GetStyle() <= wxPENSTYLE_LAST_HATCH )
545 {
546 /*
547 wxDELETE( m_penBrush );
548 HatchStyle style = HatchStyleHorizontal;
549 switch( pen.GetStyle() )
550 {
551 case wxPENSTYLE_BDIAGONAL_HATCH :
552 style = HatchStyleBackwardDiagonal;
553 break ;
554 case wxPENSTYLE_CROSSDIAG_HATCH :
555 style = HatchStyleDiagonalCross;
556 break ;
557 case wxPENSTYLE_FDIAGONAL_HATCH :
558 style = HatchStyleForwardDiagonal;
559 break ;
560 case wxPENSTYLE_CROSS_HATCH :
561 style = HatchStyleCross;
562 break ;
563 case wxPENSTYLE_HORIZONTAL_HATCH :
564 style = HatchStyleHorizontal;
565 break ;
566 case wxPENSTYLE_VERTICAL_HATCH :
567 style = HatchStyleVertical;
568 break ;
569
570 }
571 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
572 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
573 m_pen->SetBrush( m_penBrush )
574 */
575 }
576 break;
577 }
578 }
579
580 void wxCairoPenData::Apply( wxGraphicsContext* context )
581 {
582 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
583 cairo_set_line_width(ctext,m_width);
584 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
585 cairo_set_line_cap(ctext,m_cap);
586 cairo_set_line_join(ctext,m_join);
587 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
588 }
589
590 //-----------------------------------------------------------------------------
591 // wxCairoBrushData implementation
592 //-----------------------------------------------------------------------------
593
594 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
595 : wxGraphicsObjectRefData( renderer )
596 {
597 Init();
598 }
599
600 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
601 : wxGraphicsObjectRefData(renderer)
602 {
603 Init();
604
605 m_red = brush.GetColour().Red()/255.0;
606 m_green = brush.GetColour().Green()/255.0;
607 m_blue = brush.GetColour().Blue()/255.0;
608 m_alpha = brush.GetColour().Alpha()/255.0;
609 /*
610 if ( brush.GetStyle() == wxBRUSHSTYLE_SOLID)
611 {
612 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
613 brush.GetColour().Green() , brush.GetColour().Blue() ) );
614 }
615 else if ( brush.IsHatch() )
616 {
617 HatchStyle style = HatchStyleHorizontal;
618 switch( brush.GetStyle() )
619 {
620 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
621 style = HatchStyleBackwardDiagonal;
622 break ;
623 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
624 style = HatchStyleDiagonalCross;
625 break ;
626 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
627 style = HatchStyleForwardDiagonal;
628 break ;
629 case wxBRUSHSTYLE_CROSS_HATCH :
630 style = HatchStyleCross;
631 break ;
632 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
633 style = HatchStyleHorizontal;
634 break ;
635 case wxBRUSHSTYLE_VERTICAL_HATCH :
636 style = HatchStyleVertical;
637 break ;
638
639 }
640 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
641 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
642 }
643 else
644 {
645 wxBitmap* bmp = brush.GetStipple();
646 if ( bmp && bmp->Ok() )
647 {
648 wxDELETE( m_brushImage );
649 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
650 m_brush = new TextureBrush(m_brushImage);
651 }
652 }
653 */
654 }
655
656 wxCairoBrushData::~wxCairoBrushData ()
657 {
658 if (m_brushPattern)
659 cairo_pattern_destroy(m_brushPattern);
660 }
661
662 void wxCairoBrushData::Apply( wxGraphicsContext* context )
663 {
664 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
665 if ( m_brushPattern )
666 {
667 cairo_set_source(ctext,m_brushPattern);
668 }
669 else
670 {
671 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
672 }
673 }
674
675 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
676 const wxColour&c1, const wxColour&c2 )
677 {
678 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
679 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
680 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
681 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
682 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
683 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
684 }
685
686 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
687 const wxColour &oColor, const wxColour &cColor )
688 {
689 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
690 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
691 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
692 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
693 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
694 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
695 }
696
697 void wxCairoBrushData::Init()
698 {
699 m_brushPattern = NULL;
700 }
701
702 //-----------------------------------------------------------------------------
703 // wxCairoFontData implementation
704 //-----------------------------------------------------------------------------
705
706 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
707 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
708 {
709 m_red = col.Red()/255.0;
710 m_green = col.Green()/255.0;
711 m_blue = col.Blue()/255.0;
712 m_alpha = col.Alpha()/255.0;
713 m_size = font.GetPointSize();
714
715 #ifdef __WXMAC__
716 m_font = cairo_atsui_font_face_create_for_atsu_font_id( font.MacGetATSUFontID() );
717 #elif defined(__WXGTK__)
718 m_font = pango_font_description_copy( font.GetNativeFontInfo()->description );
719 #else
720 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
721 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
722 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
723 #endif
724 }
725
726 wxCairoFontData::~wxCairoFontData()
727 {
728 #ifdef __WXMAC__
729 cairo_font_face_destroy( m_font );
730 #elif defined(__WXGTK__)
731 pango_font_description_free( m_font );
732 #else
733 #endif
734 }
735
736 void wxCairoFontData::Apply( wxGraphicsContext* context )
737 {
738 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
739 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
740 #ifdef __WXGTK__
741 // the rest is done using Pango layouts
742 #elif defined(__WXMAC__)
743 cairo_set_font_face(ctext, m_font);
744 cairo_set_font_size(ctext, m_size );
745 #else
746 cairo_select_font_face(ctext, m_fontName, m_slant, m_weights );
747 cairo_set_font_size(ctext, m_size );
748 #endif
749 }
750
751 //-----------------------------------------------------------------------------
752 // wxCairoPathData implementation
753 //-----------------------------------------------------------------------------
754
755 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
756 : wxGraphicsPathData(renderer)
757 {
758 if (pathcontext)
759 {
760 m_pathContext = pathcontext;
761 }
762 else
763 {
764 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
765 m_pathContext = cairo_create(surface);
766 cairo_surface_destroy (surface);
767 }
768 }
769
770 wxCairoPathData::~wxCairoPathData()
771 {
772 cairo_destroy(m_pathContext);
773 }
774
775 wxGraphicsObjectRefData *wxCairoPathData::Clone() const
776 {
777 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
778 cairo_t* pathcontext = cairo_create(surface);
779 cairo_surface_destroy (surface);
780
781 cairo_path_t* path = cairo_copy_path(m_pathContext);
782 cairo_append_path(pathcontext, path);
783 cairo_path_destroy(path);
784 return new wxCairoPathData( GetRenderer() ,pathcontext);
785 }
786
787
788 void* wxCairoPathData::GetNativePath() const
789 {
790 return cairo_copy_path(m_pathContext) ;
791 }
792
793 void wxCairoPathData::UnGetNativePath(void *p) const
794 {
795 cairo_path_destroy((cairo_path_t*)p);
796 }
797
798 //
799 // The Primitives
800 //
801
802 void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
803 {
804 cairo_move_to(m_pathContext,x,y);
805 }
806
807 void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
808 {
809 cairo_line_to(m_pathContext,x,y);
810 }
811
812 void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
813 {
814 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
815 cairo_append_path(m_pathContext, p);
816 UnGetNativePath(p);
817 }
818
819 void wxCairoPathData::CloseSubpath()
820 {
821 cairo_close_path(m_pathContext);
822 }
823
824 void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
825 {
826 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
827 }
828
829 // gets the last point of the current path, (0,0) if not yet set
830 void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
831 {
832 double dx,dy;
833 cairo_get_current_point(m_pathContext,&dx,&dy);
834 if (x)
835 *x = dx;
836 if (y)
837 *y = dy;
838 }
839
840 void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
841 {
842 // as clockwise means positive in our system (y pointing downwards)
843 // TODO make this interpretation dependent of the
844 // real device trans
845 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
846 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
847 else
848 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
849 }
850
851 // transforms each point of this path by the matrix
852 void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
853 {
854 // as we don't have a true path object, we have to apply the inverse
855 // matrix to the context
856 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
857 cairo_matrix_invert( &m );
858 cairo_transform(m_pathContext,&m);
859 }
860
861 // gets the bounding box enclosing all points (possibly including control points)
862 void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
863 {
864 double x1,y1,x2,y2;
865
866 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
867 if ( x2 < x1 )
868 {
869 *x = x2;
870 *w = x1-x2;
871 }
872 else
873 {
874 *x = x1;
875 *w = x2-x1;
876 }
877
878 if( y2 < y1 )
879 {
880 *y = y2;
881 *h = y1-y2;
882 }
883 else
884 {
885 *y = y1;
886 *h = y2-y1;
887 }
888 }
889
890 bool wxCairoPathData::Contains( wxDouble x, wxDouble y, int WXUNUSED(fillStyle) ) const
891 {
892 return cairo_in_stroke( m_pathContext, x, y) != 0;
893 }
894
895 //-----------------------------------------------------------------------------
896 // wxCairoMatrixData implementation
897 //-----------------------------------------------------------------------------
898
899 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
900 : wxGraphicsMatrixData(renderer)
901 {
902 if ( matrix )
903 m_matrix = *matrix;
904 }
905
906 wxCairoMatrixData::~wxCairoMatrixData()
907 {
908 // nothing to do
909 }
910
911 wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
912 {
913 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
914 }
915
916 // concatenates the matrix
917 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
918 {
919 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
920 }
921
922 // sets the matrix to the respective values
923 void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
924 wxDouble tx, wxDouble ty)
925 {
926 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
927 }
928
929 // gets the component valuess of the matrix
930 void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
931 wxDouble* d, wxDouble* tx, wxDouble* ty) const
932 {
933 if (a) *a = m_matrix.xx;
934 if (b) *b = m_matrix.yx;
935 if (c) *c = m_matrix.xy;
936 if (d) *d = m_matrix.yy;
937 if (tx) *tx= m_matrix.x0;
938 if (ty) *ty= m_matrix.y0;
939 }
940
941 // makes this the inverse matrix
942 void wxCairoMatrixData::Invert()
943 {
944 cairo_matrix_invert( &m_matrix );
945 }
946
947 // returns true if the elements of the transformation matrix are equal ?
948 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
949 {
950 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
951 return (
952 m_matrix.xx == tm->xx &&
953 m_matrix.yx == tm->yx &&
954 m_matrix.xy == tm->xy &&
955 m_matrix.yy == tm->yy &&
956 m_matrix.x0 == tm->x0 &&
957 m_matrix.y0 == tm->y0 ) ;
958 }
959
960 // return true if this is the identity matrix
961 bool wxCairoMatrixData::IsIdentity() const
962 {
963 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
964 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
965 }
966
967 //
968 // transformation
969 //
970
971 // add the translation to this matrix
972 void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
973 {
974 cairo_matrix_translate( &m_matrix, dx, dy) ;
975 }
976
977 // add the scale to this matrix
978 void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
979 {
980 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
981 }
982
983 // add the rotation to this matrix (radians)
984 void wxCairoMatrixData::Rotate( wxDouble angle )
985 {
986 cairo_matrix_rotate( &m_matrix, angle) ;
987 }
988
989 //
990 // apply the transforms
991 //
992
993 // applies that matrix to the point
994 void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
995 {
996 double lx = *x, ly = *y ;
997 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
998 *x = lx;
999 *y = ly;
1000 }
1001
1002 // applies the matrix except for translations
1003 void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1004 {
1005 double lx = *dx, ly = *dy ;
1006 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
1007 *dx = lx;
1008 *dy = ly;
1009 }
1010
1011 // returns the native representation
1012 void * wxCairoMatrixData::GetNativeMatrix() const
1013 {
1014 return (void*) &m_matrix;
1015 }
1016
1017 //-----------------------------------------------------------------------------
1018 // wxCairoContext implementation
1019 //-----------------------------------------------------------------------------
1020
1021 class wxCairoOffsetHelper
1022 {
1023 public :
1024 wxCairoOffsetHelper( cairo_t* ctx , bool offset )
1025 {
1026 m_ctx = ctx;
1027 m_offset = offset;
1028 if ( m_offset )
1029 cairo_translate( m_ctx, 0.5, 0.5 );
1030 }
1031 ~wxCairoOffsetHelper( )
1032 {
1033 if ( m_offset )
1034 cairo_translate( m_ctx, -0.5, -0.5 );
1035 }
1036 public :
1037 cairo_t* m_ctx;
1038 bool m_offset;
1039 } ;
1040
1041 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxPrinterDC& dc )
1042 : wxGraphicsContext(renderer)
1043 {
1044 #ifdef __WXGTK20__
1045 const wxDCImpl *impl = dc.GetImpl();
1046 Init( (cairo_t*) impl->GetCairoContext() );
1047
1048 wxPoint org = dc.GetDeviceOrigin();
1049 cairo_translate( m_context, org.x, org.y );
1050
1051 double sx,sy;
1052 dc.GetUserScale( &sx, &sy );
1053 cairo_scale( m_context, sx, sy );
1054
1055 org = dc.GetLogicalOrigin();
1056 cairo_translate( m_context, -org.x, -org.y );
1057 #endif
1058 }
1059
1060 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1061 : wxGraphicsContext(renderer)
1062 {
1063 #ifdef __WXGTK20__
1064 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1065 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1066
1067 #if 0
1068 wxGraphicsMatrix matrix = CreateMatrix();
1069
1070 wxPoint org = dc.GetDeviceOrigin();
1071 matrix.Translate( org.x, org.y );
1072
1073 org = dc.GetLogicalOrigin();
1074 matrix.Translate( -org.x, -org.y );
1075
1076 double sx,sy;
1077 dc.GetUserScale( &sx, &sy );
1078 matrix.Scale( sx, sy );
1079
1080 ConcatTransform( matrix );
1081 #endif
1082 #endif
1083
1084 #ifdef __WXMAC__
1085 int width, height;
1086 dc.GetSize( &width, &height );
1087 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1088 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1089 Init( cairo_create( surface ) );
1090 cairo_surface_destroy( surface );
1091 #endif
1092 }
1093
1094 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxMemoryDC& dc )
1095 : wxGraphicsContext(renderer)
1096 {
1097 #ifdef __WXGTK20__
1098 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
1099 Init( gdk_cairo_create( impldc->GetGDKWindow() ) );
1100
1101 #if 0
1102 wxGraphicsMatrix matrix = CreateMatrix();
1103
1104 wxPoint org = dc.GetDeviceOrigin();
1105 matrix.Translate( org.x, org.y );
1106
1107 org = dc.GetLogicalOrigin();
1108 matrix.Translate( -org.x, -org.y );
1109
1110 double sx,sy;
1111 dc.GetUserScale( &sx, &sy );
1112 matrix.Scale( sx, sy );
1113
1114 ConcatTransform( matrix );
1115 #endif
1116 #endif
1117
1118 #ifdef __WXMAC__
1119 int width, height;
1120 dc.GetSize( &width, &height );
1121 CGContextRef cgcontext = (CGContextRef)dc.GetWindow()->MacGetCGContextRef();
1122 cairo_surface_t* surface = cairo_quartz_surface_create_for_cg_context(cgcontext, width, height);
1123 Init( cairo_create( surface ) );
1124 cairo_surface_destroy( surface );
1125 #endif
1126 }
1127
1128 #ifdef __WXGTK20__
1129 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1130 : wxGraphicsContext(renderer)
1131 {
1132 Init( gdk_cairo_create( drawable ) );
1133 }
1134 #endif
1135
1136 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1137 : wxGraphicsContext(renderer)
1138 {
1139 Init( context );
1140 }
1141
1142 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1143 : wxGraphicsContext(renderer)
1144 {
1145 #ifdef __WXGTK__
1146 // something along these lines (copied from dcclient)
1147
1148 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1149 // code should still be able to create wxClientDCs for them, so we will
1150 // use the parent window here then.
1151 if (window->m_wxwindow == NULL)
1152 {
1153 window = window->GetParent();
1154 }
1155
1156 wxASSERT_MSG( window->m_wxwindow, wxT("wxCairoContext needs a widget") );
1157
1158 Init(gdk_cairo_create(window->GTKGetDrawingWindow()));
1159 #endif
1160 }
1161
1162 wxCairoContext::~wxCairoContext()
1163 {
1164 if ( m_context )
1165 {
1166 PopState();
1167 PopState();
1168 cairo_destroy(m_context);
1169 }
1170 }
1171
1172 void wxCairoContext::Init(cairo_t *context)
1173 {
1174 m_context = context ;
1175 PushState();
1176 PushState();
1177 }
1178
1179
1180 void wxCairoContext::Clip( const wxRegion& region )
1181 {
1182 // Create a path with all the rectangles in the region
1183 wxGraphicsPath path = GetRenderer()->CreatePath();
1184 wxRegionIterator ri(region);
1185 while (ri)
1186 {
1187 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
1188 ++ri;
1189 }
1190
1191 // Put it in the context
1192 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1193 cairo_append_path(m_context, cp);
1194
1195 // clip to that path
1196 cairo_clip(m_context);
1197 path.UnGetNativePath(cp);
1198 }
1199
1200 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1201 {
1202 // Create a path with this rectangle
1203 wxGraphicsPath path = GetRenderer()->CreatePath();
1204 path.AddRectangle(x,y,w,h);
1205
1206 // Put it in the context
1207 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1208 cairo_append_path(m_context, cp);
1209
1210 // clip to that path
1211 cairo_clip(m_context);
1212 path.UnGetNativePath(cp);
1213 }
1214
1215 void wxCairoContext::ResetClip()
1216 {
1217 cairo_reset_clip(m_context);
1218 }
1219
1220
1221 void wxCairoContext::StrokePath( const wxGraphicsPath& path )
1222 {
1223 if ( !m_pen.IsNull() )
1224 {
1225 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1226 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1227 cairo_append_path(m_context,cp);
1228 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1229 cairo_stroke(m_context);
1230 path.UnGetNativePath(cp);
1231 }
1232 }
1233
1234 void wxCairoContext::FillPath( const wxGraphicsPath& path , int fillStyle )
1235 {
1236 if ( !m_brush.IsNull() )
1237 {
1238 wxCairoOffsetHelper helper( m_context, ShouldOffset() ) ;
1239 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1240 cairo_append_path(m_context,cp);
1241 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1242 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1243 cairo_fill(m_context);
1244 path.UnGetNativePath(cp);
1245 }
1246 }
1247
1248 void wxCairoContext::Rotate( wxDouble angle )
1249 {
1250 cairo_rotate(m_context,angle);
1251 }
1252
1253 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1254 {
1255 cairo_translate(m_context,dx,dy);
1256 }
1257
1258 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1259 {
1260 cairo_scale(m_context,xScale,yScale);
1261 }
1262
1263 // concatenates this transform with the current transform of this context
1264 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1265 {
1266 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
1267 }
1268
1269 // sets the transform of this context
1270 void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
1271 {
1272 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
1273 }
1274
1275 // gets the matrix of this context
1276 wxGraphicsMatrix wxCairoContext::GetTransform() const
1277 {
1278 wxGraphicsMatrix matrix = CreateMatrix();
1279 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1280 return matrix;
1281 }
1282
1283
1284
1285 void wxCairoContext::PushState()
1286 {
1287 cairo_save(m_context);
1288 }
1289
1290 void wxCairoContext::PopState()
1291 {
1292 cairo_restore(m_context);
1293 }
1294
1295 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1296 {
1297 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
1298
1299 cairo_surface_t* surface;
1300 int bw = bmp.GetWidth();
1301 int bh = bmp.GetHeight();
1302 wxBitmap bmpSource = bmp; // we need a non-const instance
1303 unsigned char* buffer = new unsigned char[bw*bh*4];
1304 wxUint32* data = (wxUint32*)buffer;
1305
1306 // Create a surface object and copy the bitmap pixel data to it. if the
1307 // image has alpha (or a mask represented as alpha) then we'll use a
1308 // different format and iterator than if it doesn't...
1309 if (bmpSource.HasAlpha() || bmpSource.GetMask())
1310 {
1311 surface = cairo_image_surface_create_for_data(
1312 buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1313 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1314 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1315
1316 wxAlphaPixelData::Iterator p(pixData);
1317 for (int y=0; y<bh; y++)
1318 {
1319 wxAlphaPixelData::Iterator rowStart = p;
1320 for (int x=0; x<bw; x++)
1321 {
1322 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1323 // with alpha in the upper 8 bits, then red, then green, then
1324 // blue. The 32-bit quantities are stored native-endian.
1325 // Pre-multiplied alpha is used.
1326 unsigned char alpha = p.Alpha();
1327 if (alpha == 0)
1328 *data = 0;
1329 else
1330 *data = ( alpha << 24
1331 | (p.Red() * alpha/255) << 16
1332 | (p.Green() * alpha/255) << 8
1333 | (p.Blue() * alpha/255) );
1334 ++data;
1335 ++p;
1336 }
1337 p = rowStart;
1338 p.OffsetY(pixData, 1);
1339 }
1340 }
1341 else // no alpha
1342 {
1343 surface = cairo_image_surface_create_for_data(
1344 buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1345 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1346 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1347
1348 wxNativePixelData::Iterator p(pixData);
1349 for (int y=0; y<bh; y++)
1350 {
1351 wxNativePixelData::Iterator rowStart = p;
1352 for (int x=0; x<bw; x++)
1353 {
1354 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1355 // the upper 8 bits unused. Red, Green, and Blue are stored in
1356 // the remaining 24 bits in that order. The 32-bit quantities
1357 // are stored native-endian.
1358 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1359 ++data;
1360 ++p;
1361 }
1362 p = rowStart;
1363 p.OffsetY(pixData, 1);
1364 }
1365 }
1366
1367
1368 PushState();
1369
1370 // In case we're scaling the image by using a width and height different
1371 // than the bitmap's size create a pattern transformation on the surface and
1372 // draw the transformed pattern.
1373 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
1374 wxDouble scaleX = w / bw;
1375 wxDouble scaleY = h / bh;
1376 cairo_scale(m_context, scaleX, scaleY);
1377
1378 // prepare to draw the image
1379 cairo_translate(m_context, x, y);
1380 cairo_set_source(m_context, pattern);
1381 // use the original size here since the context is scaled already...
1382 cairo_rectangle(m_context, 0, 0, bw, bh);
1383 // fill the rectangle using the pattern
1384 cairo_fill(m_context);
1385
1386 // clean up
1387 cairo_pattern_destroy(pattern);
1388 cairo_surface_destroy(surface);
1389 delete [] buffer;
1390 PopState();
1391 }
1392
1393 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1394 {
1395 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1396 // start using the Cairo backend on other platforms then we may need to
1397 // fiddle with this...
1398 DrawBitmap(icon, x, y, w, h);
1399 }
1400
1401
1402 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1403 {
1404 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::DrawText - no valid font set") );
1405
1406 if ( str.empty())
1407 return;
1408
1409 #ifdef __WXGTK__
1410 const wxCharBuffer data = str.utf8_str();
1411 if ( !data )
1412 return;
1413 size_t datalen = strlen(data);
1414 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1415
1416 PangoLayout *layout = pango_cairo_create_layout (m_context);
1417 pango_layout_set_font_description( layout, ((wxCairoFontData*)m_font.GetRefData())->GetFont());
1418 pango_layout_set_text(layout, data, datalen);
1419 cairo_move_to(m_context, x, y);
1420 pango_cairo_show_layout (m_context, layout);
1421
1422 g_object_unref (layout);
1423 #else
1424 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1425 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1426 // the position we move to by the ascent.
1427 cairo_font_extents_t fe;
1428 cairo_font_extents(m_context, &fe);
1429 cairo_move_to(m_context, x, y+fe.ascent);
1430
1431 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1432 cairo_show_text(m_context,buf);
1433 #endif
1434 }
1435
1436 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1437 wxDouble *descent, wxDouble *externalLeading ) const
1438 {
1439 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
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 ( 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 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1520
1521 if (text.empty())
1522 return;
1523
1524 // TODO
1525 }
1526
1527 void * wxCairoContext::GetNativeContext()
1528 {
1529 return m_context;
1530 }
1531
1532 // Cairo doesn't support bitwise logical function (a.k.a. ROP, raster output
1533 // mode). Cairo supports Porter-Duff compositing operators, but they are quite
1534 // different, although in some cases have similar names.
1535 bool wxCairoContext::SetLogicalFunction( int function )
1536 {
1537 if (m_logicalFunction == function)
1538 return true;
1539
1540 cairo_operator_t op;
1541
1542 switch ( function )
1543 {
1544 case wxCOPY: // (default) src
1545 op = CAIRO_OPERATOR_OVER; // (also default)
1546 break;
1547 case wxOR: // src OR dst
1548 op = CAIRO_OPERATOR_ADD;
1549 break;
1550 case wxNO_OP: // dst
1551 op = CAIRO_OPERATOR_DEST; // ignore the source
1552 break;
1553 case wxCLEAR: // 0
1554 op = CAIRO_OPERATOR_CLEAR;// clear dst
1555 break;
1556
1557 case wxAND: // src AND dst
1558 case wxAND_INVERT: // (NOT src) AND dst
1559 case wxAND_REVERSE:// src AND (NOT dst)
1560 case wxEQUIV: // (NOT src) XOR dst
1561 case wxINVERT: // NOT dst
1562 case wxNAND: // (NOT src) OR (NOT dst)
1563 case wxNOR: // (NOT src) AND (NOT dst)
1564 case wxOR_INVERT: // (NOT src) OR dst
1565 case wxOR_REVERSE: // src OR (NOT dst)
1566 case wxSET: // 1
1567 case wxSRC_INVERT: // NOT src
1568 //wxXOR does _not_ correspond to CAIRO_OPERATOR_XOR
1569 case wxXOR: // src XOR dst
1570 default:
1571 return false;
1572 }
1573
1574 m_logicalFunction = function;
1575 cairo_set_operator(m_context, op);
1576 return true;
1577 }
1578
1579
1580 //-----------------------------------------------------------------------------
1581 // wxCairoRenderer declaration
1582 //-----------------------------------------------------------------------------
1583
1584 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1585 {
1586 public :
1587 wxCairoRenderer() {}
1588
1589 virtual ~wxCairoRenderer() {}
1590
1591 // Context
1592
1593 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1594 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1595 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
1596
1597 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1598
1599 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1600
1601 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1602
1603 virtual wxGraphicsContext * CreateMeasuringContext();
1604
1605 // Path
1606
1607 virtual wxGraphicsPath CreatePath();
1608
1609 // Matrix
1610
1611 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1612 wxDouble tx=0.0, wxDouble ty=0.0);
1613
1614
1615 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1616
1617 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1618
1619 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1620 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1621 const wxColour&c1, const wxColour&c2) ;
1622
1623 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1624 // with radius r and color cColor
1625 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1626 const wxColour &oColor, const wxColour &cColor) ;
1627
1628 // sets the font
1629 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1630
1631 // create a native bitmap representation
1632 #if 0
1633 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap )
1634 {
1635 return wxGraphicsBitmap;
1636 }
1637
1638 // create a subimage from a native image representation
1639 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1640 {
1641 return wxGraphicsBitmap;
1642 }
1643 #endif
1644
1645 private :
1646 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1647 } ;
1648
1649 //-----------------------------------------------------------------------------
1650 // wxCairoRenderer implementation
1651 //-----------------------------------------------------------------------------
1652
1653 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1654
1655 static wxCairoRenderer gs_cairoGraphicsRenderer;
1656 // temporary hack to allow creating a cairo context on any platform
1657 extern wxGraphicsRenderer* gCairoRenderer;
1658 wxGraphicsRenderer* gCairoRenderer = &gs_cairoGraphicsRenderer;
1659
1660 #ifdef __WXGTK__
1661 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1662 {
1663 return &gs_cairoGraphicsRenderer;
1664 }
1665 #endif
1666
1667 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1668 {
1669 return new wxCairoContext(this,dc);
1670 }
1671
1672 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
1673 {
1674 return new wxCairoContext(this,dc);
1675 }
1676
1677 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxPrinterDC& dc)
1678 {
1679 #ifdef __WXGTK20__
1680 const wxDCImpl *impl = dc.GetImpl();
1681 cairo_t* context = (cairo_t*) impl->GetCairoContext();
1682 if (context)
1683 return new wxCairoContext(this,dc);
1684 else
1685 #endif
1686 return NULL;
1687 }
1688
1689 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1690 {
1691 return new wxCairoContext(this,(cairo_t*)context);
1692 }
1693
1694
1695 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1696 {
1697 #ifdef __WXGTK__
1698 return new wxCairoContext(this,(GdkDrawable*)window);
1699 #else
1700 return NULL;
1701 #endif
1702 }
1703
1704 wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
1705 {
1706 return NULL;
1707 // TODO
1708 }
1709
1710 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1711 {
1712 return new wxCairoContext(this, window );
1713 }
1714
1715 // Path
1716
1717 wxGraphicsPath wxCairoRenderer::CreatePath()
1718 {
1719 wxGraphicsPath path;
1720 path.SetRefData( new wxCairoPathData(this) );
1721 return path;
1722 }
1723
1724
1725 // Matrix
1726
1727 wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1728 wxDouble tx, wxDouble ty)
1729
1730 {
1731 wxGraphicsMatrix m;
1732 wxCairoMatrixData* data = new wxCairoMatrixData( this );
1733 data->Set( a,b,c,d,tx,ty ) ;
1734 m.SetRefData(data);
1735 return m;
1736 }
1737
1738 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
1739 {
1740 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1741 return wxNullGraphicsPen;
1742 else
1743 {
1744 wxGraphicsPen p;
1745 p.SetRefData(new wxCairoPenData( this, pen ));
1746 return p;
1747 }
1748 }
1749
1750 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
1751 {
1752 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1753 return wxNullGraphicsBrush;
1754 else
1755 {
1756 wxGraphicsBrush p;
1757 p.SetRefData(new wxCairoBrushData( this, brush ));
1758 return p;
1759 }
1760 }
1761
1762 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1763 wxGraphicsBrush wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1764 const wxColour&c1, const wxColour&c2)
1765 {
1766 wxGraphicsBrush p;
1767 wxCairoBrushData* d = new wxCairoBrushData( this );
1768 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1769 p.SetRefData(d);
1770 return p;
1771 }
1772
1773 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1774 // with radius r and color cColor
1775 wxGraphicsBrush wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1776 const wxColour &oColor, const wxColour &cColor)
1777 {
1778 wxGraphicsBrush p;
1779 wxCairoBrushData* d = new wxCairoBrushData( this );
1780 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1781 p.SetRefData(d);
1782 return p;
1783 }
1784
1785 // sets the font
1786 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1787 {
1788 if ( font.Ok() )
1789 {
1790 wxGraphicsFont p;
1791 p.SetRefData(new wxCairoFontData( this , font, col ));
1792 return p;
1793 }
1794 else
1795 return wxNullGraphicsFont;
1796 }
1797
1798 #endif // wxUSE_GRAPHICS_CONTEXT