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