]> git.saurik.com Git - wxWidgets.git/blob - src/generic/graphicc.cpp
8e6be335cb632bf99e8eedfe47eb797c070e832b
[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 #include "wx/dc.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/image.h"
22 #include "wx/window.h"
23 #include "wx/dc.h"
24 #include "wx/utils.h"
25 #include "wx/dialog.h"
26 #include "wx/app.h"
27 #include "wx/bitmap.h"
28 #include "wx/dcmemory.h"
29 #include "wx/log.h"
30 #include "wx/icon.h"
31 #include "wx/dcprint.h"
32 #include "wx/module.h"
33 #endif
34
35 #ifdef __WXGTK__
36 #include "wx/gtk/win_gtk.h"
37 #endif
38
39 #include "wx/graphics.h"
40
41 #if wxUSE_GRAPHICS_CONTEXT
42
43 #include <vector>
44
45 using namespace std;
46
47 //-----------------------------------------------------------------------------
48 // constants
49 //-----------------------------------------------------------------------------
50
51 const double RAD2DEG = 180.0 / M_PI;
52
53 //-----------------------------------------------------------------------------
54 // Local functions
55 //-----------------------------------------------------------------------------
56
57 static inline double dmin(double a, double b)
58 {
59 return a < b ? a : b;
60 }
61 static inline double dmax(double a, double b)
62 {
63 return a > b ? a : b;
64 }
65
66 static inline double DegToRad(double deg)
67 {
68 return (deg * M_PI) / 180.0;
69 }
70 static inline double RadToDeg(double deg)
71 {
72 return (deg * 180.0) / M_PI;
73 }
74
75 //-----------------------------------------------------------------------------
76 // device context implementation
77 //
78 // more and more of the dc functionality should be implemented by calling
79 // the appropricate wxCairoContext, but we will have to do that step by step
80 // also coordinate conversions should be moved to native matrix ops
81 //-----------------------------------------------------------------------------
82
83 // we always stock two context states, one at entry, to be able to preserve the
84 // state we were called with, the other one after changing to HI Graphics orientation
85 // (this one is used for getting back clippings etc)
86
87 //-----------------------------------------------------------------------------
88 // wxGraphicsPath implementation
89 //-----------------------------------------------------------------------------
90
91 // TODO remove this dependency (gdiplus needs the macros)
92
93 #ifndef max
94 #define max(a,b) (((a) > (b)) ? (a) : (b))
95 #endif
96
97 #ifndef min
98 #define min(a,b) (((a) < (b)) ? (a) : (b))
99 #endif
100
101 #include <cairo.h>
102 #ifdef __WXGTK__
103 #include <gtk/gtk.h>
104 #endif
105
106 class WXDLLIMPEXP_CORE wxCairoPath : public wxGraphicsPath
107 {
108 public :
109 wxCairoPath();
110 wxCairoPath(wxGraphicsRenderer* renderer, cairo_t* pathcontext = NULL);
111 ~wxCairoPath();
112
113 virtual wxGraphicsPath *Clone() const;
114
115 //
116 // These are the path primitives from which everything else can be constructed
117 //
118
119 // begins a new subpath at (x,y)
120 virtual void MoveToPoint( wxDouble x, wxDouble y );
121
122 // adds a straight line from the current point to (x,y)
123 virtual void AddLineToPoint( wxDouble x, wxDouble y );
124
125 // adds a cubic Bezier curve from the current point, using two control points and an end point
126 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
127
128
129 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
130 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
131
132 // gets the last point of the current path, (0,0) if not yet set
133 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) ;
134
135 // adds another path
136 virtual void AddPath( const wxGraphicsPath* path );
137
138 // closes the current sub-path
139 virtual void CloseSubpath();
140
141 //
142 // These are convenience functions which - if not available natively will be assembled
143 // using the primitives from above
144 //
145
146 /*
147
148 // appends a rectangle as a new closed subpath
149 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
150 // appends an ellipsis as a new closed subpath fitting the passed rectangle
151 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
152
153 // 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)
154 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
155 */
156
157 // returns the native path
158 virtual void * GetNativePath() const ;
159
160 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
161 virtual void UnGetNativePath(void *p) ;
162
163 // transforms each point of this path by the matrix
164 virtual void Transform( wxGraphicsMatrix* matrix ) ;
165
166 // gets the bounding box enclosing all points (possibly including control points)
167 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) ;
168
169 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxWINDING_RULE) ;
170
171 private :
172 cairo_t* m_pathContext;
173 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoPath)
174 };
175
176 class WXDLLIMPEXP_CORE wxCairoMatrix : public wxGraphicsMatrix
177 {
178 public :
179 wxCairoMatrix() ;
180
181 wxCairoMatrix(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
182 virtual ~wxCairoMatrix() ;
183
184 virtual wxGraphicsMatrix *Clone() const ;
185
186 // concatenates the matrix
187 virtual void Concat( const wxGraphicsMatrix *t );
188
189 // copies the passed in matrix
190 virtual void Copy( const wxGraphicsMatrix *t );
191
192 // sets the matrix to the respective values
193 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
194 wxDouble tx=0.0, wxDouble ty=0.0);
195
196 // makes this the inverse matrix
197 virtual void Invert();
198
199 // returns true if the elements of the transformation matrix are equal ?
200 virtual bool IsEqual( const wxGraphicsMatrix* t) const ;
201
202 // return true if this is the identity matrix
203 virtual bool IsIdentity();
204
205 //
206 // transformation
207 //
208
209 // add the translation to this matrix
210 virtual void Translate( wxDouble dx , wxDouble dy );
211
212 // add the scale to this matrix
213 virtual void Scale( wxDouble xScale , wxDouble yScale );
214
215 // add the rotation to this matrix (radians)
216 virtual void Rotate( wxDouble angle );
217
218 //
219 // apply the transforms
220 //
221
222 // applies that matrix to the point
223 virtual void TransformPoint( wxDouble *x, wxDouble *y );
224
225 // applies the matrix except for translations
226 virtual void TransformDistance( wxDouble *dx, wxDouble *dy );
227
228 // returns the native representation
229 virtual void * GetNativeMatrix() const;
230 private:
231 cairo_matrix_t m_matrix ;
232
233 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoMatrix)
234 } ;
235
236 class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData
237 {
238 public:
239 wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
240 ~wxCairoPenData();
241
242 void Init();
243
244 virtual void Apply( wxGraphicsContext* context );
245 virtual wxDouble GetWidth() { return m_width; }
246
247 private :
248 double m_width;
249
250 double m_red;
251 double m_green;
252 double m_blue;
253 double m_alpha;
254
255 cairo_line_cap_t m_cap;
256 cairo_line_join_t m_join;
257
258 int m_count;
259 const double *m_lengths;
260 double *m_userLengths;
261
262 wxPen m_pen;
263 };
264
265 class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData
266 {
267 public:
268 wxCairoBrushData( wxGraphicsRenderer* renderer );
269 wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
270 ~wxCairoBrushData ();
271
272 virtual void Apply( wxGraphicsContext* context );
273 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
274 const wxColour&c1, const wxColour&c2 );
275 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
276 const wxColour &oColor, const wxColour &cColor );
277
278 protected:
279 virtual void Init();
280
281 private :
282 double m_red;
283 double m_green;
284 double m_blue;
285 double m_alpha;
286
287 cairo_pattern_t* m_brushPattern;
288 };
289
290 class wxCairoFontData : public wxGraphicsObjectRefData
291 {
292 public:
293 wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
294 ~wxCairoFontData();
295
296 virtual void Apply( wxGraphicsContext* context );
297 private :
298 wxCharBuffer m_fontName;
299 double m_size;
300 cairo_font_slant_t m_slant;
301 cairo_font_weight_t m_weight;
302 double m_red;
303 double m_green;
304 double m_blue;
305 double m_alpha;
306 };
307
308 class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
309 {
310 DECLARE_NO_COPY_CLASS(wxCairoContext)
311
312 public:
313 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
314 #ifdef __WXGTK__
315 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
316 #endif
317 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
318 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
319 wxCairoContext();
320 virtual ~wxCairoContext();
321
322 virtual void Clip( const wxRegion &region );
323
324 // clips drawings to the rect
325 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
326
327 // resets the clipping to original extent
328 virtual void ResetClip();
329
330 virtual void * GetNativeContext();
331
332 virtual void StrokePath( const wxGraphicsPath *p );
333 virtual void FillPath( const wxGraphicsPath *p , int fillStyle = wxWINDING_RULE );
334
335 virtual void Translate( wxDouble dx , wxDouble dy );
336 virtual void Scale( wxDouble xScale , wxDouble yScale );
337 virtual void Rotate( wxDouble angle );
338
339 // concatenates this transform with the current transform of this context
340 virtual void ConcatTransform( const wxGraphicsMatrix* matrix );
341
342 // sets the transform of this context
343 virtual void SetTransform( const wxGraphicsMatrix* matrix );
344
345 // gets the matrix of this context
346 virtual void GetTransform( wxGraphicsMatrix* matrix );
347
348 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
349 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
350 virtual void PushState();
351 virtual void PopState();
352
353 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
354 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
355 wxDouble *descent, wxDouble *externalLeading ) const;
356 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
357
358 private:
359 cairo_t* m_context;
360 };
361
362 //-----------------------------------------------------------------------------
363 // wxCairoPenData implementation
364 //-----------------------------------------------------------------------------
365
366 wxCairoPenData::~wxCairoPenData()
367 {
368 delete[] m_userLengths;
369 }
370
371 void wxCairoPenData::Init()
372 {
373 m_lengths = NULL;
374 m_userLengths = NULL;
375 m_width = 0;
376 m_count = 0;
377 }
378
379 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
380 : wxGraphicsObjectRefData(renderer)
381 {
382 Init();
383 m_width = pen.GetWidth();
384 if (m_width <= 0.0)
385 m_width = 0.1;
386
387 m_red = m_pen.GetColour().Red()/255.0;
388 m_green = m_pen.GetColour().Green()/255.0;
389 m_blue = m_pen.GetColour().Blue()/255.0;
390 m_alpha = m_pen.GetColour().Alpha()/255.0;
391
392 switch ( m_pen.GetCap() )
393 {
394 case wxCAP_ROUND :
395 m_cap = CAIRO_LINE_CAP_ROUND;
396 break;
397
398 case wxCAP_PROJECTING :
399 m_cap = CAIRO_LINE_CAP_SQUARE;
400 break;
401
402 case wxCAP_BUTT :
403 m_cap = CAIRO_LINE_CAP_BUTT;
404 break;
405
406 default :
407 m_cap = CAIRO_LINE_CAP_BUTT;
408 break;
409 }
410
411 switch ( m_pen.GetJoin() )
412 {
413 case wxJOIN_BEVEL :
414 m_join = CAIRO_LINE_JOIN_BEVEL;
415 break;
416
417 case wxJOIN_MITER :
418 m_join = CAIRO_LINE_JOIN_MITER;
419 break;
420
421 case wxJOIN_ROUND :
422 m_join = CAIRO_LINE_JOIN_ROUND;
423 break;
424
425 default :
426 m_join = CAIRO_LINE_JOIN_MITER;
427 break;
428 }
429
430 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
431 const double dotted[] =
432 {
433 dashUnit , dashUnit + 2.0
434 };
435 static const double short_dashed[] =
436 {
437 9.0 , 6.0
438 };
439 static const double dashed[] =
440 {
441 19.0 , 9.0
442 };
443 static const double dotted_dashed[] =
444 {
445 9.0 , 6.0 , 3.0 , 3.0
446 };
447
448 switch ( m_pen.GetStyle() )
449 {
450 case wxSOLID :
451 break;
452
453 case wxDOT :
454 m_count = WXSIZEOF(dotted);
455 m_userLengths = new double[ m_count ] ;
456 memcpy( m_userLengths, dotted, sizeof(dotted) );
457 m_lengths = m_userLengths;
458 break;
459
460 case wxLONG_DASH :
461 m_lengths = dotted ;
462 m_count = WXSIZEOF(dashed);
463 break;
464
465 case wxSHORT_DASH :
466 m_lengths = dotted ;
467 m_count = WXSIZEOF(short_dashed);
468 break;
469
470 case wxDOT_DASH :
471 m_lengths = dotted ;
472 m_count = WXSIZEOF(dotted_dashed);
473 break;
474
475 case wxUSER_DASH :
476 {
477 wxDash *wxdashes ;
478 m_count = m_pen.GetDashes( &wxdashes ) ;
479 if ((wxdashes != NULL) && (m_count > 0))
480 {
481 m_userLengths = new double[m_count] ;
482 for ( int i = 0 ; i < m_count ; ++i )
483 {
484 m_userLengths[i] = wxdashes[i] * dashUnit ;
485
486 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
487 m_userLengths[i] = dashUnit + 2.0 ;
488 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
489 m_userLengths[i] = dashUnit ;
490 }
491 }
492 m_lengths = m_userLengths ;
493 }
494 break;
495 case wxSTIPPLE :
496 {
497 /*
498 wxBitmap* bmp = pen.GetStipple();
499 if ( bmp && bmp->Ok() )
500 {
501 wxDELETE( m_penImage );
502 wxDELETE( m_penBrush );
503 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
504 m_penBrush = new TextureBrush(m_penImage);
505 m_pen->SetBrush( m_penBrush );
506 }
507 */
508 }
509 break;
510 default :
511 if ( m_pen.GetStyle() >= wxFIRST_HATCH && m_pen.GetStyle() <= wxLAST_HATCH )
512 {
513 /*
514 wxDELETE( m_penBrush );
515 HatchStyle style = HatchStyleHorizontal;
516 switch( pen.GetStyle() )
517 {
518 case wxBDIAGONAL_HATCH :
519 style = HatchStyleBackwardDiagonal;
520 break ;
521 case wxCROSSDIAG_HATCH :
522 style = HatchStyleDiagonalCross;
523 break ;
524 case wxFDIAGONAL_HATCH :
525 style = HatchStyleForwardDiagonal;
526 break ;
527 case wxCROSS_HATCH :
528 style = HatchStyleCross;
529 break ;
530 case wxHORIZONTAL_HATCH :
531 style = HatchStyleHorizontal;
532 break ;
533 case wxVERTICAL_HATCH :
534 style = HatchStyleVertical;
535 break ;
536
537 }
538 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
539 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
540 m_pen->SetBrush( m_penBrush )
541 */
542 }
543 break;
544 }
545 }
546
547 void wxCairoPenData::Apply( wxGraphicsContext* context )
548 {
549 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
550 cairo_set_line_width(ctext,m_width);
551 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
552 cairo_set_line_cap(ctext,m_cap);
553 cairo_set_line_join(ctext,m_join);
554 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
555 }
556
557 //-----------------------------------------------------------------------------
558 // wxCairoBrushData implementation
559 //-----------------------------------------------------------------------------
560
561 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer ) : wxGraphicsObjectRefData( renderer )
562 {
563 Init();
564 }
565
566 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
567 : wxGraphicsObjectRefData(renderer)
568 {
569 m_red = brush.GetColour().Red()/255.0;
570 m_green = brush.GetColour().Green()/255.0;
571 m_blue = brush.GetColour().Blue()/255.0;
572 m_alpha = brush.GetColour().Alpha()/255.0;
573 /*
574 if ( brush.GetStyle() == wxSOLID)
575 {
576 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
577 brush.GetColour().Green() , brush.GetColour().Blue() ) );
578 }
579 else if ( brush.IsHatch() )
580 {
581 HatchStyle style = HatchStyleHorizontal;
582 switch( brush.GetStyle() )
583 {
584 case wxBDIAGONAL_HATCH :
585 style = HatchStyleBackwardDiagonal;
586 break ;
587 case wxCROSSDIAG_HATCH :
588 style = HatchStyleDiagonalCross;
589 break ;
590 case wxFDIAGONAL_HATCH :
591 style = HatchStyleForwardDiagonal;
592 break ;
593 case wxCROSS_HATCH :
594 style = HatchStyleCross;
595 break ;
596 case wxHORIZONTAL_HATCH :
597 style = HatchStyleHorizontal;
598 break ;
599 case wxVERTICAL_HATCH :
600 style = HatchStyleVertical;
601 break ;
602
603 }
604 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
605 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
606 }
607 else
608 {
609 wxBitmap* bmp = brush.GetStipple();
610 if ( bmp && bmp->Ok() )
611 {
612 wxDELETE( m_brushImage );
613 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
614 m_brush = new TextureBrush(m_brushImage);
615 }
616 }
617 */
618 }
619
620 wxCairoBrushData::~wxCairoBrushData ()
621 {
622 if (m_brushPattern)
623 cairo_pattern_destroy(m_brushPattern);
624 }
625
626 void wxCairoBrushData::Apply( wxGraphicsContext* context )
627 {
628 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
629 if ( m_brushPattern )
630 {
631 cairo_set_source(ctext,m_brushPattern);
632 }
633 else
634 {
635 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
636 }
637 }
638
639 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
640 const wxColour&c1, const wxColour&c2 )
641 {
642 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
643 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
644 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
645 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
646 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
647 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
648 }
649
650 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
651 const wxColour &oColor, const wxColour &cColor )
652 {
653 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
654 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
655 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
656 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
657 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
658 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
659 }
660
661 void wxCairoBrushData::Init()
662 {
663 m_brushPattern = NULL;
664 }
665
666 //-----------------------------------------------------------------------------
667 // wxCairoFontData implementation
668 //-----------------------------------------------------------------------------
669
670 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
671 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
672 {
673 m_red = col.Red()/255.0;
674 m_green = col.Green()/255.0;
675 m_blue = col.Blue()/255.0;
676 m_alpha = col.Alpha()/255.0;
677
678 m_size = font.GetPointSize();
679 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
680 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
681 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
682 }
683
684 wxCairoFontData::~wxCairoFontData()
685 {
686 }
687
688 void wxCairoFontData::Apply( wxGraphicsContext* context )
689 {
690 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
691 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
692 cairo_select_font_face(ctext,m_fontName,m_slant,m_weight);
693 cairo_set_font_size(ctext,m_size);
694 // TODO UNDERLINE
695 // TODO FIX SIZE
696 }
697
698 //-----------------------------------------------------------------------------
699 // wxCairoPath implementation
700 //-----------------------------------------------------------------------------
701
702 IMPLEMENT_DYNAMIC_CLASS(wxCairoPath,wxGraphicsPath)
703
704 wxCairoPath::wxCairoPath() : wxGraphicsPath(NULL)
705 {
706 wxLogDebug(wxT("Illegal Constructor called"));
707 }
708
709 wxCairoPath::wxCairoPath( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
710 : wxGraphicsPath(renderer)
711 {
712 if (pathcontext)
713 {
714 m_pathContext = pathcontext;
715 }
716 else
717 {
718 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
719 m_pathContext = cairo_create(surface);
720 cairo_surface_destroy (surface);
721 }
722 }
723
724 wxCairoPath::~wxCairoPath()
725 {
726 cairo_destroy(m_pathContext);
727 }
728
729 wxGraphicsPath *wxCairoPath::Clone() const
730 {
731 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
732 cairo_t* pathcontext = cairo_create(surface);
733 cairo_surface_destroy (surface);
734
735 cairo_path_t* path = cairo_copy_path(m_pathContext);
736 cairo_append_path(pathcontext, path);
737 cairo_path_destroy(path);
738 return new wxCairoPath( GetRenderer() ,pathcontext);
739 }
740
741
742 void* wxCairoPath::GetNativePath() const
743 {
744 return cairo_copy_path(m_pathContext) ;
745 }
746
747 void wxCairoPath::UnGetNativePath(void *p)
748 {
749 cairo_path_destroy((cairo_path_t*)p);
750 }
751
752 //
753 // The Primitives
754 //
755
756 void wxCairoPath::MoveToPoint( wxDouble x , wxDouble y )
757 {
758 cairo_move_to(m_pathContext,x,y);
759 }
760
761 void wxCairoPath::AddLineToPoint( wxDouble x , wxDouble y )
762 {
763 cairo_line_to(m_pathContext,x,y);
764 }
765
766 void wxCairoPath::AddPath( const wxGraphicsPath* path )
767 {
768 // TODO
769 }
770
771 void wxCairoPath::CloseSubpath()
772 {
773 cairo_close_path(m_pathContext);
774 }
775
776 void wxCairoPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
777 {
778 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
779 }
780
781 // gets the last point of the current path, (0,0) if not yet set
782 void wxCairoPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
783 {
784 double dx,dy;
785 cairo_get_current_point(m_pathContext,&dx,&dy);
786 x = dx;
787 y = dy;
788 }
789
790 void wxCairoPath::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
791 {
792 // as clockwise means positive in our system (y pointing downwards)
793 // TODO make this interpretation dependent of the
794 // real device trans
795 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
796 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
797 else
798 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
799 }
800
801 // transforms each point of this path by the matrix
802 void wxCairoPath::Transform( wxGraphicsMatrix* matrix )
803 {
804 // as we don't have a true path object, we have to apply the inverse
805 // matrix to the context
806 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
807 cairo_matrix_invert( &m );
808 cairo_transform(m_pathContext,&m);
809 }
810
811 // gets the bounding box enclosing all points (possibly including control points)
812 void wxCairoPath::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h)
813 {
814 double x1,y1,x2,y2;
815
816 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
817 if ( x2 < x1 )
818 {
819 *x = x2;
820 *w = x1-x2;
821 }
822 else
823 {
824 *x = x1;
825 *w = x2-x1;
826 }
827
828 if( y2 < y1 )
829 {
830 *y = y2;
831 *h = y1-y2;
832 }
833 else
834 {
835 *y = y1;
836 *h = y2-y1;
837 }
838 }
839
840 bool wxCairoPath::Contains( wxDouble x, wxDouble y, int fillStyle )
841 {
842 return cairo_in_stroke( m_pathContext, x, y) != NULL;
843 }
844
845 //-----------------------------------------------------------------------------
846 // wxCairoMatrix implementation
847 //-----------------------------------------------------------------------------
848
849 IMPLEMENT_DYNAMIC_CLASS(wxCairoMatrix,wxGraphicsMatrix)
850
851 wxCairoMatrix::wxCairoMatrix() : wxGraphicsMatrix(NULL)
852 {
853 wxLogDebug(wxT("Illegal Constructor called"));
854 }
855
856 wxCairoMatrix::wxCairoMatrix(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
857 : wxGraphicsMatrix(renderer)
858 {
859 if ( matrix )
860 m_matrix = *matrix;
861 }
862
863 wxCairoMatrix::~wxCairoMatrix()
864 {
865 // nothing to do
866 }
867
868 wxGraphicsMatrix *wxCairoMatrix::Clone() const
869 {
870 return new wxCairoMatrix(GetRenderer(),&m_matrix);
871 }
872
873 // concatenates the matrix
874 void wxCairoMatrix::Concat( const wxGraphicsMatrix *t )
875 {
876 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
877 }
878
879 // copies the passed in matrix
880 void wxCairoMatrix::Copy( const wxGraphicsMatrix *t )
881 {
882 m_matrix = *((cairo_matrix_t*) t->GetNativeMatrix());
883 }
884
885 // sets the matrix to the respective values
886 void wxCairoMatrix::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
887 wxDouble tx, wxDouble ty)
888 {
889 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
890 }
891
892 // makes this the inverse matrix
893 void wxCairoMatrix::Invert()
894 {
895 cairo_matrix_invert( &m_matrix );
896 }
897
898 // returns true if the elements of the transformation matrix are equal ?
899 bool wxCairoMatrix::IsEqual( const wxGraphicsMatrix* t) const
900 {
901 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
902 return (
903 m_matrix.xx == tm->xx &&
904 m_matrix.yx == tm->yx &&
905 m_matrix.xy == tm->xy &&
906 m_matrix.yy == tm->yy &&
907 m_matrix.x0 == tm->x0 &&
908 m_matrix.y0 == tm->y0 ) ;
909 }
910
911 // return true if this is the identity matrix
912 bool wxCairoMatrix::IsIdentity()
913 {
914 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
915 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
916 }
917
918 //
919 // transformation
920 //
921
922 // add the translation to this matrix
923 void wxCairoMatrix::Translate( wxDouble dx , wxDouble dy )
924 {
925 cairo_matrix_translate( &m_matrix, dx, dy) ;
926 }
927
928 // add the scale to this matrix
929 void wxCairoMatrix::Scale( wxDouble xScale , wxDouble yScale )
930 {
931 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
932 }
933
934 // add the rotation to this matrix (radians)
935 void wxCairoMatrix::Rotate( wxDouble angle )
936 {
937 cairo_matrix_rotate( &m_matrix, angle) ;
938 }
939
940 //
941 // apply the transforms
942 //
943
944 // applies that matrix to the point
945 void wxCairoMatrix::TransformPoint( wxDouble *x, wxDouble *y )
946 {
947 double lx = *x, ly = *y ;
948 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
949 *x = lx;
950 *y = ly;
951 }
952
953 // applies the matrix except for translations
954 void wxCairoMatrix::TransformDistance( wxDouble *dx, wxDouble *dy )
955 {
956 double lx = *dx, ly = *dy ;
957 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
958 *dx = lx;
959 *dy = ly;
960 }
961
962 // returns the native representation
963 void * wxCairoMatrix::GetNativeMatrix() const
964 {
965 return (void*) &m_matrix;
966 }
967
968 //-----------------------------------------------------------------------------
969 // wxCairoContext implementation
970 //-----------------------------------------------------------------------------
971
972 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
973 : wxGraphicsContext(renderer)
974 {
975 #ifdef __WXGTK__
976 m_context = gdk_cairo_create( dc.m_window ) ;
977 #endif
978 PushState();
979 PushState();
980 }
981
982 #ifdef __WXGTK__
983 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
984 : wxGraphicsContext(renderer)
985 {
986 m_context = gdk_cairo_create( drawable ) ;
987 PushState();
988 PushState();
989 }
990 #endif
991
992 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
993 : wxGraphicsContext(renderer)
994 {
995 m_context = context ;
996 PushState();
997 PushState();
998 }
999
1000 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1001 : wxGraphicsContext(renderer)
1002 {
1003 #ifdef __WXGTK__
1004 // something along these lines (copied from dcclient)
1005
1006 GtkWidget *widget = window->m_wxwindow;
1007
1008 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1009 // code should still be able to create wxClientDCs for them, so we will
1010 // use the parent window here then.
1011 if ( !widget )
1012 {
1013 window = window->GetParent();
1014 widget = window->m_wxwindow;
1015 }
1016
1017 wxASSERT_MSG( widget, wxT("wxCairoContext needs a widget") );
1018
1019 GtkPizza *pizza = GTK_PIZZA( widget );
1020 GdkDrawable* drawable = pizza->bin_window;
1021 m_context = gdk_cairo_create( drawable ) ;
1022 #endif
1023 PushState();
1024 PushState();
1025 }
1026
1027 wxCairoContext::~wxCairoContext()
1028 {
1029 if ( m_context )
1030 {
1031 PopState();
1032 PopState();
1033 cairo_destroy(m_context);
1034 }
1035 }
1036
1037
1038 void wxCairoContext::Clip( const wxRegion & WXUNUSED(region) )
1039 {
1040 // TODO
1041 }
1042
1043 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1044 {
1045 // TODO
1046 }
1047
1048 void wxCairoContext::ResetClip()
1049 {
1050 // TODO
1051 }
1052
1053
1054 void wxCairoContext::StrokePath( const wxGraphicsPath *path )
1055 {
1056 if ( !m_pen.IsNull() )
1057 {
1058 cairo_path_t* cp = (cairo_path_t*) path->GetNativePath() ;
1059 cairo_append_path(m_context,cp);
1060 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1061 cairo_stroke(m_context);
1062 wxConstCast(path, wxGraphicsPath)->UnGetNativePath(cp);
1063 }
1064 }
1065
1066 void wxCairoContext::FillPath( const wxGraphicsPath *path , int fillStyle )
1067 {
1068 if ( !m_brush.IsNull() )
1069 {
1070 cairo_path_t* cp = (cairo_path_t*) path->GetNativePath() ;
1071 cairo_append_path(m_context,cp);
1072 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1073 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1074 cairo_fill(m_context);
1075 wxConstCast(path, wxGraphicsPath)->UnGetNativePath(cp);
1076 }
1077 }
1078
1079 void wxCairoContext::Rotate( wxDouble angle )
1080 {
1081 cairo_rotate(m_context,angle);
1082 }
1083
1084 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1085 {
1086 cairo_translate(m_context,dx,dy);
1087 }
1088
1089 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1090 {
1091 cairo_scale(m_context,xScale,yScale);
1092 }
1093
1094 // concatenates this transform with the current transform of this context
1095 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix* matrix )
1096 {
1097 cairo_transform(m_context,(const cairo_matrix_t *) matrix->GetNativeMatrix());
1098 }
1099
1100 // sets the transform of this context
1101 void wxCairoContext::SetTransform( const wxGraphicsMatrix* matrix )
1102 {
1103 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix->GetNativeMatrix());
1104 }
1105
1106 // gets the matrix of this context
1107 void wxCairoContext::GetTransform( wxGraphicsMatrix* matrix )
1108 {
1109 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix->GetNativeMatrix());
1110 }
1111
1112
1113
1114 void wxCairoContext::PushState()
1115 {
1116 cairo_save(m_context);
1117 }
1118
1119 void wxCairoContext::PopState()
1120 {
1121 cairo_restore(m_context);
1122 }
1123
1124 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1125 {
1126 /*
1127 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
1128 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1129 delete image ;
1130 */
1131 }
1132
1133 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1134 {
1135 /*
1136 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
1137 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1138 delete image ;
1139 */
1140 }
1141
1142
1143 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1144 {
1145 if ( m_font.IsNull() || str.IsEmpty())
1146 return ;
1147 cairo_move_to(m_context,x,y);
1148 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1149 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1150 cairo_show_text(m_context,buf);
1151 }
1152
1153 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1154 wxDouble *descent, wxDouble *externalLeading ) const
1155 {
1156 // TODO
1157 }
1158
1159 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1160 {
1161 widths.Empty();
1162 widths.Add(0, text.length());
1163
1164 if (text.empty())
1165 return;
1166
1167 // TODO
1168 }
1169
1170 void * wxCairoContext::GetNativeContext()
1171 {
1172 return m_context;
1173 }
1174
1175 //-----------------------------------------------------------------------------
1176 // wxCairoRenderer declaration
1177 //-----------------------------------------------------------------------------
1178
1179 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1180 {
1181 public :
1182 wxCairoRenderer() {}
1183
1184 virtual ~wxCairoRenderer() {}
1185
1186 // Context
1187
1188 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1189
1190 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1191
1192 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1193
1194 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1195
1196 // Path
1197
1198 virtual wxGraphicsPath * CreatePath();
1199
1200 // Matrix
1201
1202 virtual wxGraphicsMatrix * CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1203 wxDouble tx=0.0, wxDouble ty=0.0);
1204
1205
1206 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1207
1208 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1209
1210 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1211 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1212 const wxColour&c1, const wxColour&c2) ;
1213
1214 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1215 // with radius r and color cColor
1216 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1217 const wxColour &oColor, const wxColour &cColor) ;
1218
1219 // sets the font
1220 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1221
1222 private :
1223 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1224 } ;
1225
1226 //-----------------------------------------------------------------------------
1227 // wxCairoRenderer implementation
1228 //-----------------------------------------------------------------------------
1229
1230 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1231
1232 static wxCairoRenderer gs_cairoGraphicsRenderer;
1233
1234 #ifdef __WXGTK__
1235 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1236 {
1237 return &gs_cairoGraphicsRenderer;
1238 }
1239 #endif
1240
1241 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1242 {
1243 return new wxCairoContext(this,dc);
1244 }
1245
1246 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1247 {
1248 return new wxCairoContext(this,(cairo_t*)context);
1249 }
1250
1251
1252 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1253 {
1254 #ifdef __WXGTK__
1255 return new wxCairoContext(this,(GdkDrawable*)window);
1256 #else
1257 return NULL;
1258 #endif
1259 }
1260
1261 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1262 {
1263 return new wxCairoContext(this, window );
1264 }
1265
1266 // Path
1267
1268 wxGraphicsPath * wxCairoRenderer::CreatePath()
1269 {
1270 return new wxCairoPath( this );
1271 }
1272
1273
1274 // Matrix
1275
1276 wxGraphicsMatrix * wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1277 wxDouble tx, wxDouble ty)
1278
1279 {
1280 wxCairoMatrix* m = new wxCairoMatrix( this );
1281 m->Set( a,b,c,d,tx,ty ) ;
1282 return m;
1283 }
1284
1285 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
1286 {
1287 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1288 return wxNullGraphicsPen;
1289 else
1290 {
1291 wxGraphicsPen p;
1292 p.SetRefData(new wxCairoPenData( this, pen ));
1293 return p;
1294 }
1295 }
1296
1297 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
1298 {
1299 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1300 return wxNullGraphicsBrush;
1301 else
1302 {
1303 wxGraphicsBrush p;
1304 p.SetRefData(new wxCairoBrushData( this, brush ));
1305 return p;
1306 }
1307 }
1308
1309 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1310 wxGraphicsBrush wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1311 const wxColour&c1, const wxColour&c2)
1312 {
1313 wxGraphicsBrush p;
1314 wxCairoBrushData* d = new wxCairoBrushData( this );
1315 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1316 p.SetRefData(d);
1317 return p;
1318 }
1319
1320 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1321 // with radius r and color cColor
1322 wxGraphicsBrush wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1323 const wxColour &oColor, const wxColour &cColor)
1324 {
1325 wxGraphicsBrush p;
1326 wxCairoBrushData* d = new wxCairoBrushData( this );
1327 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1328 p.SetRefData(d);
1329 return p;
1330 }
1331
1332 // sets the font
1333 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1334 {
1335 if ( font.Ok() )
1336 {
1337 wxGraphicsFont p;
1338 p.SetRefData(new wxCairoFontData( this , font, col ));
1339 return p;
1340 }
1341 else
1342 return wxNullGraphicsFont;
1343 }
1344
1345 #endif // wxUSE_GRAPHICS_CONTEXT