Ensure that the pen and bush are properly initialized
[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_pen = pen;
384 m_width = m_pen.GetWidth();
385 if (m_width <= 0.0)
386 m_width = 0.1;
387
388 m_red = m_pen.GetColour().Red()/255.0;
389 m_green = m_pen.GetColour().Green()/255.0;
390 m_blue = m_pen.GetColour().Blue()/255.0;
391 m_alpha = m_pen.GetColour().Alpha()/255.0;
392
393 switch ( m_pen.GetCap() )
394 {
395 case wxCAP_ROUND :
396 m_cap = CAIRO_LINE_CAP_ROUND;
397 break;
398
399 case wxCAP_PROJECTING :
400 m_cap = CAIRO_LINE_CAP_SQUARE;
401 break;
402
403 case wxCAP_BUTT :
404 m_cap = CAIRO_LINE_CAP_BUTT;
405 break;
406
407 default :
408 m_cap = CAIRO_LINE_CAP_BUTT;
409 break;
410 }
411
412 switch ( m_pen.GetJoin() )
413 {
414 case wxJOIN_BEVEL :
415 m_join = CAIRO_LINE_JOIN_BEVEL;
416 break;
417
418 case wxJOIN_MITER :
419 m_join = CAIRO_LINE_JOIN_MITER;
420 break;
421
422 case wxJOIN_ROUND :
423 m_join = CAIRO_LINE_JOIN_ROUND;
424 break;
425
426 default :
427 m_join = CAIRO_LINE_JOIN_MITER;
428 break;
429 }
430
431 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
432 const double dotted[] =
433 {
434 dashUnit , dashUnit + 2.0
435 };
436 static const double short_dashed[] =
437 {
438 9.0 , 6.0
439 };
440 static const double dashed[] =
441 {
442 19.0 , 9.0
443 };
444 static const double dotted_dashed[] =
445 {
446 9.0 , 6.0 , 3.0 , 3.0
447 };
448
449 switch ( m_pen.GetStyle() )
450 {
451 case wxSOLID :
452 break;
453
454 case wxDOT :
455 m_count = WXSIZEOF(dotted);
456 m_userLengths = new double[ m_count ] ;
457 memcpy( m_userLengths, dotted, sizeof(dotted) );
458 m_lengths = m_userLengths;
459 break;
460
461 case wxLONG_DASH :
462 m_lengths = dotted ;
463 m_count = WXSIZEOF(dashed);
464 break;
465
466 case wxSHORT_DASH :
467 m_lengths = dotted ;
468 m_count = WXSIZEOF(short_dashed);
469 break;
470
471 case wxDOT_DASH :
472 m_lengths = dotted ;
473 m_count = WXSIZEOF(dotted_dashed);
474 break;
475
476 case wxUSER_DASH :
477 {
478 wxDash *wxdashes ;
479 m_count = m_pen.GetDashes( &wxdashes ) ;
480 if ((wxdashes != NULL) && (m_count > 0))
481 {
482 m_userLengths = new double[m_count] ;
483 for ( int i = 0 ; i < m_count ; ++i )
484 {
485 m_userLengths[i] = wxdashes[i] * dashUnit ;
486
487 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
488 m_userLengths[i] = dashUnit + 2.0 ;
489 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
490 m_userLengths[i] = dashUnit ;
491 }
492 }
493 m_lengths = m_userLengths ;
494 }
495 break;
496 case wxSTIPPLE :
497 {
498 /*
499 wxBitmap* bmp = pen.GetStipple();
500 if ( bmp && bmp->Ok() )
501 {
502 wxDELETE( m_penImage );
503 wxDELETE( m_penBrush );
504 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
505 m_penBrush = new TextureBrush(m_penImage);
506 m_pen->SetBrush( m_penBrush );
507 }
508 */
509 }
510 break;
511 default :
512 if ( m_pen.GetStyle() >= wxFIRST_HATCH && m_pen.GetStyle() <= wxLAST_HATCH )
513 {
514 /*
515 wxDELETE( m_penBrush );
516 HatchStyle style = HatchStyleHorizontal;
517 switch( pen.GetStyle() )
518 {
519 case wxBDIAGONAL_HATCH :
520 style = HatchStyleBackwardDiagonal;
521 break ;
522 case wxCROSSDIAG_HATCH :
523 style = HatchStyleDiagonalCross;
524 break ;
525 case wxFDIAGONAL_HATCH :
526 style = HatchStyleForwardDiagonal;
527 break ;
528 case wxCROSS_HATCH :
529 style = HatchStyleCross;
530 break ;
531 case wxHORIZONTAL_HATCH :
532 style = HatchStyleHorizontal;
533 break ;
534 case wxVERTICAL_HATCH :
535 style = HatchStyleVertical;
536 break ;
537
538 }
539 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
540 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
541 m_pen->SetBrush( m_penBrush )
542 */
543 }
544 break;
545 }
546 }
547
548 void wxCairoPenData::Apply( wxGraphicsContext* context )
549 {
550 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
551 cairo_set_line_width(ctext,m_width);
552 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
553 cairo_set_line_cap(ctext,m_cap);
554 cairo_set_line_join(ctext,m_join);
555 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
556 }
557
558 //-----------------------------------------------------------------------------
559 // wxCairoBrushData implementation
560 //-----------------------------------------------------------------------------
561
562 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
563 : wxGraphicsObjectRefData( renderer )
564 {
565 Init();
566 }
567
568 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
569 : wxGraphicsObjectRefData(renderer)
570 {
571 Init();
572
573 m_red = brush.GetColour().Red()/255.0;
574 m_green = brush.GetColour().Green()/255.0;
575 m_blue = brush.GetColour().Blue()/255.0;
576 m_alpha = brush.GetColour().Alpha()/255.0;
577 /*
578 if ( brush.GetStyle() == wxSOLID)
579 {
580 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
581 brush.GetColour().Green() , brush.GetColour().Blue() ) );
582 }
583 else if ( brush.IsHatch() )
584 {
585 HatchStyle style = HatchStyleHorizontal;
586 switch( brush.GetStyle() )
587 {
588 case wxBDIAGONAL_HATCH :
589 style = HatchStyleBackwardDiagonal;
590 break ;
591 case wxCROSSDIAG_HATCH :
592 style = HatchStyleDiagonalCross;
593 break ;
594 case wxFDIAGONAL_HATCH :
595 style = HatchStyleForwardDiagonal;
596 break ;
597 case wxCROSS_HATCH :
598 style = HatchStyleCross;
599 break ;
600 case wxHORIZONTAL_HATCH :
601 style = HatchStyleHorizontal;
602 break ;
603 case wxVERTICAL_HATCH :
604 style = HatchStyleVertical;
605 break ;
606
607 }
608 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
609 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
610 }
611 else
612 {
613 wxBitmap* bmp = brush.GetStipple();
614 if ( bmp && bmp->Ok() )
615 {
616 wxDELETE( m_brushImage );
617 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
618 m_brush = new TextureBrush(m_brushImage);
619 }
620 }
621 */
622 }
623
624 wxCairoBrushData::~wxCairoBrushData ()
625 {
626 if (m_brushPattern)
627 cairo_pattern_destroy(m_brushPattern);
628 }
629
630 void wxCairoBrushData::Apply( wxGraphicsContext* context )
631 {
632 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
633 if ( m_brushPattern )
634 {
635 cairo_set_source(ctext,m_brushPattern);
636 }
637 else
638 {
639 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
640 }
641 }
642
643 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
644 const wxColour&c1, const wxColour&c2 )
645 {
646 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
647 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
648 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
649 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
650 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
651 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
652 }
653
654 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
655 const wxColour &oColor, const wxColour &cColor )
656 {
657 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
658 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
659 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
660 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
661 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
662 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
663 }
664
665 void wxCairoBrushData::Init()
666 {
667 m_brushPattern = NULL;
668 }
669
670 //-----------------------------------------------------------------------------
671 // wxCairoFontData implementation
672 //-----------------------------------------------------------------------------
673
674 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
675 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
676 {
677 m_red = col.Red()/255.0;
678 m_green = col.Green()/255.0;
679 m_blue = col.Blue()/255.0;
680 m_alpha = col.Alpha()/255.0;
681
682 m_size = font.GetPointSize();
683 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
684 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
685 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
686 }
687
688 wxCairoFontData::~wxCairoFontData()
689 {
690 }
691
692 void wxCairoFontData::Apply( wxGraphicsContext* context )
693 {
694 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
695 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
696 cairo_select_font_face(ctext,m_fontName,m_slant,m_weight);
697 cairo_set_font_size(ctext,m_size);
698 // TODO UNDERLINE
699 // TODO FIX SIZE
700 }
701
702 //-----------------------------------------------------------------------------
703 // wxCairoPath implementation
704 //-----------------------------------------------------------------------------
705
706 IMPLEMENT_DYNAMIC_CLASS(wxCairoPath,wxGraphicsPath)
707
708 wxCairoPath::wxCairoPath() : wxGraphicsPath(NULL)
709 {
710 wxLogDebug(wxT("Illegal Constructor called"));
711 }
712
713 wxCairoPath::wxCairoPath( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
714 : wxGraphicsPath(renderer)
715 {
716 if (pathcontext)
717 {
718 m_pathContext = pathcontext;
719 }
720 else
721 {
722 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
723 m_pathContext = cairo_create(surface);
724 cairo_surface_destroy (surface);
725 }
726 }
727
728 wxCairoPath::~wxCairoPath()
729 {
730 cairo_destroy(m_pathContext);
731 }
732
733 wxGraphicsPath *wxCairoPath::Clone() const
734 {
735 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
736 cairo_t* pathcontext = cairo_create(surface);
737 cairo_surface_destroy (surface);
738
739 cairo_path_t* path = cairo_copy_path(m_pathContext);
740 cairo_append_path(pathcontext, path);
741 cairo_path_destroy(path);
742 return new wxCairoPath( GetRenderer() ,pathcontext);
743 }
744
745
746 void* wxCairoPath::GetNativePath() const
747 {
748 return cairo_copy_path(m_pathContext) ;
749 }
750
751 void wxCairoPath::UnGetNativePath(void *p)
752 {
753 cairo_path_destroy((cairo_path_t*)p);
754 }
755
756 //
757 // The Primitives
758 //
759
760 void wxCairoPath::MoveToPoint( wxDouble x , wxDouble y )
761 {
762 cairo_move_to(m_pathContext,x,y);
763 }
764
765 void wxCairoPath::AddLineToPoint( wxDouble x , wxDouble y )
766 {
767 cairo_line_to(m_pathContext,x,y);
768 }
769
770 void wxCairoPath::AddPath( const wxGraphicsPath* path )
771 {
772 // TODO
773 }
774
775 void wxCairoPath::CloseSubpath()
776 {
777 cairo_close_path(m_pathContext);
778 }
779
780 void wxCairoPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
781 {
782 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
783 }
784
785 // gets the last point of the current path, (0,0) if not yet set
786 void wxCairoPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
787 {
788 double dx,dy;
789 cairo_get_current_point(m_pathContext,&dx,&dy);
790 x = dx;
791 y = dy;
792 }
793
794 void wxCairoPath::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
795 {
796 // as clockwise means positive in our system (y pointing downwards)
797 // TODO make this interpretation dependent of the
798 // real device trans
799 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
800 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
801 else
802 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
803 }
804
805 // transforms each point of this path by the matrix
806 void wxCairoPath::Transform( wxGraphicsMatrix* matrix )
807 {
808 // as we don't have a true path object, we have to apply the inverse
809 // matrix to the context
810 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
811 cairo_matrix_invert( &m );
812 cairo_transform(m_pathContext,&m);
813 }
814
815 // gets the bounding box enclosing all points (possibly including control points)
816 void wxCairoPath::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h)
817 {
818 double x1,y1,x2,y2;
819
820 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
821 if ( x2 < x1 )
822 {
823 *x = x2;
824 *w = x1-x2;
825 }
826 else
827 {
828 *x = x1;
829 *w = x2-x1;
830 }
831
832 if( y2 < y1 )
833 {
834 *y = y2;
835 *h = y1-y2;
836 }
837 else
838 {
839 *y = y1;
840 *h = y2-y1;
841 }
842 }
843
844 bool wxCairoPath::Contains( wxDouble x, wxDouble y, int fillStyle )
845 {
846 return cairo_in_stroke( m_pathContext, x, y) != NULL;
847 }
848
849 //-----------------------------------------------------------------------------
850 // wxCairoMatrix implementation
851 //-----------------------------------------------------------------------------
852
853 IMPLEMENT_DYNAMIC_CLASS(wxCairoMatrix,wxGraphicsMatrix)
854
855 wxCairoMatrix::wxCairoMatrix() : wxGraphicsMatrix(NULL)
856 {
857 wxLogDebug(wxT("Illegal Constructor called"));
858 }
859
860 wxCairoMatrix::wxCairoMatrix(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
861 : wxGraphicsMatrix(renderer)
862 {
863 if ( matrix )
864 m_matrix = *matrix;
865 }
866
867 wxCairoMatrix::~wxCairoMatrix()
868 {
869 // nothing to do
870 }
871
872 wxGraphicsMatrix *wxCairoMatrix::Clone() const
873 {
874 return new wxCairoMatrix(GetRenderer(),&m_matrix);
875 }
876
877 // concatenates the matrix
878 void wxCairoMatrix::Concat( const wxGraphicsMatrix *t )
879 {
880 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
881 }
882
883 // copies the passed in matrix
884 void wxCairoMatrix::Copy( const wxGraphicsMatrix *t )
885 {
886 m_matrix = *((cairo_matrix_t*) t->GetNativeMatrix());
887 }
888
889 // sets the matrix to the respective values
890 void wxCairoMatrix::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
891 wxDouble tx, wxDouble ty)
892 {
893 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
894 }
895
896 // makes this the inverse matrix
897 void wxCairoMatrix::Invert()
898 {
899 cairo_matrix_invert( &m_matrix );
900 }
901
902 // returns true if the elements of the transformation matrix are equal ?
903 bool wxCairoMatrix::IsEqual( const wxGraphicsMatrix* t) const
904 {
905 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
906 return (
907 m_matrix.xx == tm->xx &&
908 m_matrix.yx == tm->yx &&
909 m_matrix.xy == tm->xy &&
910 m_matrix.yy == tm->yy &&
911 m_matrix.x0 == tm->x0 &&
912 m_matrix.y0 == tm->y0 ) ;
913 }
914
915 // return true if this is the identity matrix
916 bool wxCairoMatrix::IsIdentity()
917 {
918 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
919 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
920 }
921
922 //
923 // transformation
924 //
925
926 // add the translation to this matrix
927 void wxCairoMatrix::Translate( wxDouble dx , wxDouble dy )
928 {
929 cairo_matrix_translate( &m_matrix, dx, dy) ;
930 }
931
932 // add the scale to this matrix
933 void wxCairoMatrix::Scale( wxDouble xScale , wxDouble yScale )
934 {
935 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
936 }
937
938 // add the rotation to this matrix (radians)
939 void wxCairoMatrix::Rotate( wxDouble angle )
940 {
941 cairo_matrix_rotate( &m_matrix, angle) ;
942 }
943
944 //
945 // apply the transforms
946 //
947
948 // applies that matrix to the point
949 void wxCairoMatrix::TransformPoint( wxDouble *x, wxDouble *y )
950 {
951 double lx = *x, ly = *y ;
952 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
953 *x = lx;
954 *y = ly;
955 }
956
957 // applies the matrix except for translations
958 void wxCairoMatrix::TransformDistance( wxDouble *dx, wxDouble *dy )
959 {
960 double lx = *dx, ly = *dy ;
961 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
962 *dx = lx;
963 *dy = ly;
964 }
965
966 // returns the native representation
967 void * wxCairoMatrix::GetNativeMatrix() const
968 {
969 return (void*) &m_matrix;
970 }
971
972 //-----------------------------------------------------------------------------
973 // wxCairoContext implementation
974 //-----------------------------------------------------------------------------
975
976 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
977 : wxGraphicsContext(renderer)
978 {
979 #ifdef __WXGTK__
980 m_context = gdk_cairo_create( dc.m_window ) ;
981 #endif
982 PushState();
983 PushState();
984 }
985
986 #ifdef __WXGTK__
987 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
988 : wxGraphicsContext(renderer)
989 {
990 m_context = gdk_cairo_create( drawable ) ;
991 PushState();
992 PushState();
993 }
994 #endif
995
996 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
997 : wxGraphicsContext(renderer)
998 {
999 m_context = context ;
1000 PushState();
1001 PushState();
1002 }
1003
1004 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1005 : wxGraphicsContext(renderer)
1006 {
1007 #ifdef __WXGTK__
1008 // something along these lines (copied from dcclient)
1009
1010 GtkWidget *widget = window->m_wxwindow;
1011
1012 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1013 // code should still be able to create wxClientDCs for them, so we will
1014 // use the parent window here then.
1015 if ( !widget )
1016 {
1017 window = window->GetParent();
1018 widget = window->m_wxwindow;
1019 }
1020
1021 wxASSERT_MSG( widget, wxT("wxCairoContext needs a widget") );
1022
1023 GtkPizza *pizza = GTK_PIZZA( widget );
1024 GdkDrawable* drawable = pizza->bin_window;
1025 m_context = gdk_cairo_create( drawable ) ;
1026 #endif
1027 PushState();
1028 PushState();
1029 }
1030
1031 wxCairoContext::~wxCairoContext()
1032 {
1033 if ( m_context )
1034 {
1035 PopState();
1036 PopState();
1037 cairo_destroy(m_context);
1038 }
1039 }
1040
1041
1042 void wxCairoContext::Clip( const wxRegion & WXUNUSED(region) )
1043 {
1044 // TODO
1045 }
1046
1047 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1048 {
1049 // TODO
1050 }
1051
1052 void wxCairoContext::ResetClip()
1053 {
1054 // TODO
1055 }
1056
1057
1058 void wxCairoContext::StrokePath( const wxGraphicsPath *path )
1059 {
1060 if ( !m_pen.IsNull() )
1061 {
1062 cairo_path_t* cp = (cairo_path_t*) path->GetNativePath() ;
1063 cairo_append_path(m_context,cp);
1064 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1065 cairo_stroke(m_context);
1066 wxConstCast(path, wxGraphicsPath)->UnGetNativePath(cp);
1067 }
1068 }
1069
1070 void wxCairoContext::FillPath( const wxGraphicsPath *path , int fillStyle )
1071 {
1072 if ( !m_brush.IsNull() )
1073 {
1074 cairo_path_t* cp = (cairo_path_t*) path->GetNativePath() ;
1075 cairo_append_path(m_context,cp);
1076 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1077 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1078 cairo_fill(m_context);
1079 wxConstCast(path, wxGraphicsPath)->UnGetNativePath(cp);
1080 }
1081 }
1082
1083 void wxCairoContext::Rotate( wxDouble angle )
1084 {
1085 cairo_rotate(m_context,angle);
1086 }
1087
1088 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1089 {
1090 cairo_translate(m_context,dx,dy);
1091 }
1092
1093 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1094 {
1095 cairo_scale(m_context,xScale,yScale);
1096 }
1097
1098 // concatenates this transform with the current transform of this context
1099 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix* matrix )
1100 {
1101 cairo_transform(m_context,(const cairo_matrix_t *) matrix->GetNativeMatrix());
1102 }
1103
1104 // sets the transform of this context
1105 void wxCairoContext::SetTransform( const wxGraphicsMatrix* matrix )
1106 {
1107 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix->GetNativeMatrix());
1108 }
1109
1110 // gets the matrix of this context
1111 void wxCairoContext::GetTransform( wxGraphicsMatrix* matrix )
1112 {
1113 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix->GetNativeMatrix());
1114 }
1115
1116
1117
1118 void wxCairoContext::PushState()
1119 {
1120 cairo_save(m_context);
1121 }
1122
1123 void wxCairoContext::PopState()
1124 {
1125 cairo_restore(m_context);
1126 }
1127
1128 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1129 {
1130 /*
1131 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
1132 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1133 delete image ;
1134 */
1135 }
1136
1137 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1138 {
1139 /*
1140 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
1141 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1142 delete image ;
1143 */
1144 }
1145
1146
1147 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1148 {
1149 if ( m_font.IsNull() || str.IsEmpty())
1150 return ;
1151 cairo_move_to(m_context,x,y);
1152 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1153 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1154 cairo_show_text(m_context,buf);
1155 }
1156
1157 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1158 wxDouble *descent, wxDouble *externalLeading ) const
1159 {
1160 // TODO
1161 }
1162
1163 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1164 {
1165 widths.Empty();
1166 widths.Add(0, text.length());
1167
1168 if (text.empty())
1169 return;
1170
1171 // TODO
1172 }
1173
1174 void * wxCairoContext::GetNativeContext()
1175 {
1176 return m_context;
1177 }
1178
1179 //-----------------------------------------------------------------------------
1180 // wxCairoRenderer declaration
1181 //-----------------------------------------------------------------------------
1182
1183 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1184 {
1185 public :
1186 wxCairoRenderer() {}
1187
1188 virtual ~wxCairoRenderer() {}
1189
1190 // Context
1191
1192 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1193
1194 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1195
1196 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1197
1198 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1199
1200 // Path
1201
1202 virtual wxGraphicsPath * CreatePath();
1203
1204 // Matrix
1205
1206 virtual wxGraphicsMatrix * CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1207 wxDouble tx=0.0, wxDouble ty=0.0);
1208
1209
1210 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1211
1212 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1213
1214 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1215 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1216 const wxColour&c1, const wxColour&c2) ;
1217
1218 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1219 // with radius r and color cColor
1220 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1221 const wxColour &oColor, const wxColour &cColor) ;
1222
1223 // sets the font
1224 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1225
1226 private :
1227 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1228 } ;
1229
1230 //-----------------------------------------------------------------------------
1231 // wxCairoRenderer implementation
1232 //-----------------------------------------------------------------------------
1233
1234 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1235
1236 static wxCairoRenderer gs_cairoGraphicsRenderer;
1237
1238 #ifdef __WXGTK__
1239 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1240 {
1241 return &gs_cairoGraphicsRenderer;
1242 }
1243 #endif
1244
1245 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1246 {
1247 return new wxCairoContext(this,dc);
1248 }
1249
1250 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1251 {
1252 return new wxCairoContext(this,(cairo_t*)context);
1253 }
1254
1255
1256 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1257 {
1258 #ifdef __WXGTK__
1259 return new wxCairoContext(this,(GdkDrawable*)window);
1260 #else
1261 return NULL;
1262 #endif
1263 }
1264
1265 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1266 {
1267 return new wxCairoContext(this, window );
1268 }
1269
1270 // Path
1271
1272 wxGraphicsPath * wxCairoRenderer::CreatePath()
1273 {
1274 return new wxCairoPath( this );
1275 }
1276
1277
1278 // Matrix
1279
1280 wxGraphicsMatrix * wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1281 wxDouble tx, wxDouble ty)
1282
1283 {
1284 wxCairoMatrix* m = new wxCairoMatrix( this );
1285 m->Set( a,b,c,d,tx,ty ) ;
1286 return m;
1287 }
1288
1289 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
1290 {
1291 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1292 return wxNullGraphicsPen;
1293 else
1294 {
1295 wxGraphicsPen p;
1296 p.SetRefData(new wxCairoPenData( this, pen ));
1297 return p;
1298 }
1299 }
1300
1301 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
1302 {
1303 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1304 return wxNullGraphicsBrush;
1305 else
1306 {
1307 wxGraphicsBrush p;
1308 p.SetRefData(new wxCairoBrushData( this, brush ));
1309 return p;
1310 }
1311 }
1312
1313 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1314 wxGraphicsBrush wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1315 const wxColour&c1, const wxColour&c2)
1316 {
1317 wxGraphicsBrush p;
1318 wxCairoBrushData* d = new wxCairoBrushData( this );
1319 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1320 p.SetRefData(d);
1321 return p;
1322 }
1323
1324 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1325 // with radius r and color cColor
1326 wxGraphicsBrush wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1327 const wxColour &oColor, const wxColour &cColor)
1328 {
1329 wxGraphicsBrush p;
1330 wxCairoBrushData* d = new wxCairoBrushData( this );
1331 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1332 p.SetRefData(d);
1333 return p;
1334 }
1335
1336 // sets the font
1337 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1338 {
1339 if ( font.Ok() )
1340 {
1341 wxGraphicsFont p;
1342 p.SetRefData(new wxCairoFontData( this , font, col ));
1343 return p;
1344 }
1345 else
1346 return wxNullGraphicsFont;
1347 }
1348
1349 #endif // wxUSE_GRAPHICS_CONTEXT