/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/generic/graphicc.cpp
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/generic/graphicc.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download)
Tue Sep 7 03:29:01 2010 UTC (9 years, 11 months ago) by william
File size: 46311 byte(s)
branching from upstream revision (http://pcsx2.googlecode.com/svn/trunk
): r3113 to
https://svn.netsolutions.dnsalias.com/websvn/ps2/pcsx2/pcsx2_0.9.7/branch/r3113_0.9.7_beta
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: graphicc.cpp 56887 2008-11-22 04:40:08Z KO $
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 #include "wx/rawbmp.h"
41
42 #if wxUSE_GRAPHICS_CONTEXT
43
44 #include <vector>
45
46 using namespace std;
47
48 //-----------------------------------------------------------------------------
49 // constants
50 //-----------------------------------------------------------------------------
51
52 const double RAD2DEG = 180.0 / M_PI;
53
54 //-----------------------------------------------------------------------------
55 // Local functions
56 //-----------------------------------------------------------------------------
57
58 static inline double dmin(double a, double b)
59 {
60 return a < b ? a : b;
61 }
62 static inline double dmax(double a, double b)
63 {
64 return a > b ? a : b;
65 }
66
67 static inline double DegToRad(double deg)
68 {
69 return (deg * M_PI) / 180.0;
70 }
71 static inline double RadToDeg(double deg)
72 {
73 return (deg * 180.0) / M_PI;
74 }
75
76 //-----------------------------------------------------------------------------
77 // device context implementation
78 //
79 // more and more of the dc functionality should be implemented by calling
80 // the appropricate wxCairoContext, but we will have to do that step by step
81 // also coordinate conversions should be moved to native matrix ops
82 //-----------------------------------------------------------------------------
83
84 // we always stock two context states, one at entry, to be able to preserve the
85 // state we were called with, the other one after changing to HI Graphics orientation
86 // (this one is used for getting back clippings etc)
87
88 //-----------------------------------------------------------------------------
89 // wxGraphicsPath implementation
90 //-----------------------------------------------------------------------------
91
92 // TODO remove this dependency (gdiplus needs the macros)
93
94 #ifndef max
95 #define max(a,b) (((a) > (b)) ? (a) : (b))
96 #endif
97
98 #ifndef min
99 #define min(a,b) (((a) < (b)) ? (a) : (b))
100 #endif
101
102 #include <cairo.h>
103 #ifdef __WXGTK__
104 #include <gtk/gtk.h>
105 #endif
106
107 class WXDLLIMPEXP_CORE wxCairoPathData : public wxGraphicsPathData
108 {
109 public :
110 wxCairoPathData(wxGraphicsRenderer* renderer, cairo_t* path = NULL);
111 ~wxCairoPathData();
112
113 virtual wxGraphicsObjectRefData *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) const;
134
135 // adds another path
136 virtual void AddPath( const wxGraphicsPathData* 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) const;
162
163 // transforms each point of this path by the matrix
164 virtual void Transform( const wxGraphicsMatrixData* 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) const;
168
169 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxWINDING_RULE) const;
170
171 private :
172 cairo_t* m_pathContext;
173 };
174
175 class WXDLLIMPEXP_CORE wxCairoMatrixData : public wxGraphicsMatrixData
176 {
177 public :
178 wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix = NULL ) ;
179 virtual ~wxCairoMatrixData() ;
180
181 virtual wxGraphicsObjectRefData *Clone() const ;
182
183 // concatenates the matrix
184 virtual void Concat( const wxGraphicsMatrixData *t );
185
186 // sets the matrix to the respective values
187 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
188 wxDouble tx=0.0, wxDouble ty=0.0);
189
190 // gets the component valuess of the matrix
191 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
192 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
193
194 // makes this the inverse matrix
195 virtual void Invert();
196
197 // returns true if the elements of the transformation matrix are equal ?
198 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
199
200 // return true if this is the identity matrix
201 virtual bool IsIdentity() const;
202
203 //
204 // transformation
205 //
206
207 // add the translation to this matrix
208 virtual void Translate( wxDouble dx , wxDouble dy );
209
210 // add the scale to this matrix
211 virtual void Scale( wxDouble xScale , wxDouble yScale );
212
213 // add the rotation to this matrix (radians)
214 virtual void Rotate( wxDouble angle );
215
216 //
217 // apply the transforms
218 //
219
220 // applies that matrix to the point
221 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
222
223 // applies the matrix except for translations
224 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
225
226 // returns the native representation
227 virtual void * GetNativeMatrix() const;
228 private:
229 cairo_matrix_t m_matrix ;
230 } ;
231
232 class WXDLLIMPEXP_CORE wxCairoPenData : public wxGraphicsObjectRefData
233 {
234 public:
235 wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
236 ~wxCairoPenData();
237
238 void Init();
239
240 virtual void Apply( wxGraphicsContext* context );
241 virtual wxDouble GetWidth() { return m_width; }
242
243 private :
244 double m_width;
245
246 double m_red;
247 double m_green;
248 double m_blue;
249 double m_alpha;
250
251 cairo_line_cap_t m_cap;
252 cairo_line_join_t m_join;
253
254 int m_count;
255 const double *m_lengths;
256 double *m_userLengths;
257
258 wxPen m_pen;
259 };
260
261 class WXDLLIMPEXP_CORE wxCairoBrushData : public wxGraphicsObjectRefData
262 {
263 public:
264 wxCairoBrushData( wxGraphicsRenderer* renderer );
265 wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
266 ~wxCairoBrushData ();
267
268 virtual void Apply( wxGraphicsContext* context );
269 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
270 const wxColour&c1, const wxColour&c2 );
271 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
272 const wxColour &oColor, const wxColour &cColor );
273
274 protected:
275 virtual void Init();
276
277 private :
278 double m_red;
279 double m_green;
280 double m_blue;
281 double m_alpha;
282
283 cairo_pattern_t* m_brushPattern;
284 };
285
286 class wxCairoFontData : public wxGraphicsObjectRefData
287 {
288 public:
289 wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
290 ~wxCairoFontData();
291
292 virtual void Apply( wxGraphicsContext* context );
293 private :
294 wxCharBuffer m_fontName;
295 double m_size;
296 cairo_font_slant_t m_slant;
297 cairo_font_weight_t m_weight;
298 double m_red;
299 double m_green;
300 double m_blue;
301 double m_alpha;
302 };
303
304 class wxCairoBitmapData : public wxGraphicsObjectRefData
305 {
306 public:
307 wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp );
308 ~wxCairoBitmapData();
309
310 virtual cairo_surface_t* GetCairoSurface() { return m_surface; }
311 virtual cairo_pattern_t* GetCairoPattern() { return m_pattern; }
312 virtual wxSize GetSize() { return wxSize(m_width, m_height); }
313 private :
314 cairo_surface_t* m_surface;
315 cairo_pattern_t* m_pattern;
316 int m_width;
317 int m_height;
318 unsigned char* m_buffer;
319 };
320
321 class WXDLLIMPEXP_CORE wxCairoContext : public wxGraphicsContext
322 {
323 DECLARE_NO_COPY_CLASS(wxCairoContext)
324
325 public:
326 wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc );
327 #ifdef __WXGTK__
328 wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable );
329 #endif
330 wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context );
331 wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window);
332 wxCairoContext();
333 virtual ~wxCairoContext();
334
335 virtual void Clip( const wxRegion &region );
336
337 // clips drawings to the rect
338 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
339
340 // resets the clipping to original extent
341 virtual void ResetClip();
342
343 virtual void * GetNativeContext();
344
345 virtual void StrokePath( const wxGraphicsPath& p );
346 virtual void FillPath( const wxGraphicsPath& p , int fillStyle = wxWINDING_RULE );
347
348 virtual void Translate( wxDouble dx , wxDouble dy );
349 virtual void Scale( wxDouble xScale , wxDouble yScale );
350 virtual void Rotate( wxDouble angle );
351
352 // concatenates this transform with the current transform of this context
353 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
354
355 // sets the transform of this context
356 virtual void SetTransform( const wxGraphicsMatrix& matrix );
357
358 // gets the matrix of this context
359 virtual wxGraphicsMatrix GetTransform() const;
360
361 void DrawGraphicsBitmapInternal( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
362 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
363 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
364 virtual void PushState();
365 virtual void PopState();
366
367 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
368 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
369 wxDouble *descent, wxDouble *externalLeading ) const;
370 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
371
372 private:
373 cairo_t* m_context;
374 };
375
376 //-----------------------------------------------------------------------------
377 // wxCairoPenData implementation
378 //-----------------------------------------------------------------------------
379
380 wxCairoPenData::~wxCairoPenData()
381 {
382 delete[] m_userLengths;
383 }
384
385 void wxCairoPenData::Init()
386 {
387 m_lengths = NULL;
388 m_userLengths = NULL;
389 m_width = 0;
390 m_count = 0;
391 }
392
393 wxCairoPenData::wxCairoPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
394 : wxGraphicsObjectRefData(renderer)
395 {
396 Init();
397 m_pen = pen;
398 m_width = m_pen.GetWidth();
399 if (m_width <= 0.0)
400 m_width = 0.1;
401
402 m_red = m_pen.GetColour().Red()/255.0;
403 m_green = m_pen.GetColour().Green()/255.0;
404 m_blue = m_pen.GetColour().Blue()/255.0;
405 m_alpha = m_pen.GetColour().Alpha()/255.0;
406
407 switch ( m_pen.GetCap() )
408 {
409 case wxCAP_ROUND :
410 m_cap = CAIRO_LINE_CAP_ROUND;
411 break;
412
413 case wxCAP_PROJECTING :
414 m_cap = CAIRO_LINE_CAP_SQUARE;
415 break;
416
417 case wxCAP_BUTT :
418 m_cap = CAIRO_LINE_CAP_BUTT;
419 break;
420
421 default :
422 m_cap = CAIRO_LINE_CAP_BUTT;
423 break;
424 }
425
426 switch ( m_pen.GetJoin() )
427 {
428 case wxJOIN_BEVEL :
429 m_join = CAIRO_LINE_JOIN_BEVEL;
430 break;
431
432 case wxJOIN_MITER :
433 m_join = CAIRO_LINE_JOIN_MITER;
434 break;
435
436 case wxJOIN_ROUND :
437 m_join = CAIRO_LINE_JOIN_ROUND;
438 break;
439
440 default :
441 m_join = CAIRO_LINE_JOIN_MITER;
442 break;
443 }
444
445 const double dashUnit = m_width < 1.0 ? 1.0 : m_width;
446 const double dotted[] =
447 {
448 dashUnit , dashUnit + 2.0
449 };
450 static const double short_dashed[] =
451 {
452 9.0 , 6.0
453 };
454 static const double dashed[] =
455 {
456 19.0 , 9.0
457 };
458 static const double dotted_dashed[] =
459 {
460 9.0 , 6.0 , 3.0 , 3.0
461 };
462
463 switch ( m_pen.GetStyle() )
464 {
465 case wxSOLID :
466 break;
467
468 case wxDOT :
469 m_count = WXSIZEOF(dotted);
470 m_userLengths = new double[ m_count ] ;
471 memcpy( m_userLengths, dotted, sizeof(dotted) );
472 m_lengths = m_userLengths;
473 break;
474
475 case wxLONG_DASH :
476 m_lengths = dotted ;
477 m_count = WXSIZEOF(dashed);
478 break;
479
480 case wxSHORT_DASH :
481 m_lengths = dotted ;
482 m_count = WXSIZEOF(short_dashed);
483 break;
484
485 case wxDOT_DASH :
486 m_lengths = dotted ;
487 m_count = WXSIZEOF(dotted_dashed);
488 break;
489
490 case wxUSER_DASH :
491 {
492 wxDash *wxdashes ;
493 m_count = m_pen.GetDashes( &wxdashes ) ;
494 if ((wxdashes != NULL) && (m_count > 0))
495 {
496 m_userLengths = new double[m_count] ;
497 for ( int i = 0 ; i < m_count ; ++i )
498 {
499 m_userLengths[i] = wxdashes[i] * dashUnit ;
500
501 if ( i % 2 == 1 && m_userLengths[i] < dashUnit + 2.0 )
502 m_userLengths[i] = dashUnit + 2.0 ;
503 else if ( i % 2 == 0 && m_userLengths[i] < dashUnit )
504 m_userLengths[i] = dashUnit ;
505 }
506 }
507 m_lengths = m_userLengths ;
508 }
509 break;
510 case wxSTIPPLE :
511 {
512 /*
513 wxBitmap* bmp = pen.GetStipple();
514 if ( bmp && bmp->Ok() )
515 {
516 wxDELETE( m_penImage );
517 wxDELETE( m_penBrush );
518 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
519 m_penBrush = new TextureBrush(m_penImage);
520 m_pen->SetBrush( m_penBrush );
521 }
522 */
523 }
524 break;
525 default :
526 if ( m_pen.GetStyle() >= wxFIRST_HATCH && m_pen.GetStyle() <= wxLAST_HATCH )
527 {
528 /*
529 wxDELETE( m_penBrush );
530 HatchStyle style = HatchStyleHorizontal;
531 switch( pen.GetStyle() )
532 {
533 case wxBDIAGONAL_HATCH :
534 style = HatchStyleBackwardDiagonal;
535 break ;
536 case wxCROSSDIAG_HATCH :
537 style = HatchStyleDiagonalCross;
538 break ;
539 case wxFDIAGONAL_HATCH :
540 style = HatchStyleForwardDiagonal;
541 break ;
542 case wxCROSS_HATCH :
543 style = HatchStyleCross;
544 break ;
545 case wxHORIZONTAL_HATCH :
546 style = HatchStyleHorizontal;
547 break ;
548 case wxVERTICAL_HATCH :
549 style = HatchStyleVertical;
550 break ;
551
552 }
553 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
554 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
555 m_pen->SetBrush( m_penBrush )
556 */
557 }
558 break;
559 }
560 }
561
562 void wxCairoPenData::Apply( wxGraphicsContext* context )
563 {
564 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
565 cairo_set_line_width(ctext,m_width);
566 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
567 cairo_set_line_cap(ctext,m_cap);
568 cairo_set_line_join(ctext,m_join);
569 cairo_set_dash(ctext,(double*)m_lengths,m_count,0.0);
570 }
571
572 //-----------------------------------------------------------------------------
573 // wxCairoBrushData implementation
574 //-----------------------------------------------------------------------------
575
576 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer )
577 : wxGraphicsObjectRefData( renderer )
578 {
579 Init();
580 }
581
582 wxCairoBrushData::wxCairoBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush )
583 : wxGraphicsObjectRefData(renderer)
584 {
585 Init();
586
587 m_red = brush.GetColour().Red()/255.0;
588 m_green = brush.GetColour().Green()/255.0;
589 m_blue = brush.GetColour().Blue()/255.0;
590 m_alpha = brush.GetColour().Alpha()/255.0;
591 /*
592 if ( brush.GetStyle() == wxSOLID)
593 {
594 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
595 brush.GetColour().Green() , brush.GetColour().Blue() ) );
596 }
597 else if ( brush.IsHatch() )
598 {
599 HatchStyle style = HatchStyleHorizontal;
600 switch( brush.GetStyle() )
601 {
602 case wxBDIAGONAL_HATCH :
603 style = HatchStyleBackwardDiagonal;
604 break ;
605 case wxCROSSDIAG_HATCH :
606 style = HatchStyleDiagonalCross;
607 break ;
608 case wxFDIAGONAL_HATCH :
609 style = HatchStyleForwardDiagonal;
610 break ;
611 case wxCROSS_HATCH :
612 style = HatchStyleCross;
613 break ;
614 case wxHORIZONTAL_HATCH :
615 style = HatchStyleHorizontal;
616 break ;
617 case wxVERTICAL_HATCH :
618 style = HatchStyleVertical;
619 break ;
620
621 }
622 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
623 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
624 }
625 else
626 {
627 wxBitmap* bmp = brush.GetStipple();
628 if ( bmp && bmp->Ok() )
629 {
630 wxDELETE( m_brushImage );
631 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
632 m_brush = new TextureBrush(m_brushImage);
633 }
634 }
635 */
636 }
637
638 wxCairoBrushData::~wxCairoBrushData ()
639 {
640 if (m_brushPattern)
641 cairo_pattern_destroy(m_brushPattern);
642 }
643
644 void wxCairoBrushData::Apply( wxGraphicsContext* context )
645 {
646 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
647 if ( m_brushPattern )
648 {
649 cairo_set_source(ctext,m_brushPattern);
650 }
651 else
652 {
653 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
654 }
655 }
656
657 void wxCairoBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
658 const wxColour&c1, const wxColour&c2 )
659 {
660 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
661 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
662 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
663 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
664 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
665 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
666 }
667
668 void wxCairoBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
669 const wxColour &oColor, const wxColour &cColor )
670 {
671 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
672 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
673 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
674 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
675 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
676 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
677 }
678
679 void wxCairoBrushData::Init()
680 {
681 m_brushPattern = NULL;
682 }
683
684 //-----------------------------------------------------------------------------
685 // wxCairoFontData implementation
686 //-----------------------------------------------------------------------------
687
688 wxCairoFontData::wxCairoFontData( wxGraphicsRenderer* renderer, const wxFont &font,
689 const wxColour& col ) : wxGraphicsObjectRefData(renderer)
690 {
691 m_red = col.Red()/255.0;
692 m_green = col.Green()/255.0;
693 m_blue = col.Blue()/255.0;
694 m_alpha = col.Alpha()/255.0;
695
696 m_size = font.GetPointSize();
697 m_fontName = font.GetFaceName().mb_str(wxConvUTF8);
698 m_slant = font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL;
699 m_weight = font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL;
700 }
701
702 wxCairoFontData::~wxCairoFontData()
703 {
704 }
705
706 void wxCairoFontData::Apply( wxGraphicsContext* context )
707 {
708 cairo_t * ctext = (cairo_t*) context->GetNativeContext();
709 cairo_set_source_rgba(ctext,m_red,m_green, m_blue,m_alpha);
710 cairo_select_font_face(ctext,m_fontName,m_slant,m_weight);
711 cairo_set_font_size(ctext,m_size);
712 // TODO UNDERLINE
713 // TODO FIX SIZE
714 }
715
716 //-----------------------------------------------------------------------------
717 // wxCairoPathData implementation
718 //-----------------------------------------------------------------------------
719
720 wxCairoPathData::wxCairoPathData( wxGraphicsRenderer* renderer, cairo_t* pathcontext)
721 : wxGraphicsPathData(renderer)
722 {
723 if (pathcontext)
724 {
725 m_pathContext = pathcontext;
726 }
727 else
728 {
729 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
730 m_pathContext = cairo_create(surface);
731 cairo_surface_destroy (surface);
732 }
733 }
734
735 wxCairoPathData::~wxCairoPathData()
736 {
737 cairo_destroy(m_pathContext);
738 }
739
740 wxGraphicsObjectRefData *wxCairoPathData::Clone() const
741 {
742 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
743 cairo_t* pathcontext = cairo_create(surface);
744 cairo_surface_destroy (surface);
745
746 cairo_path_t* path = cairo_copy_path(m_pathContext);
747 cairo_append_path(pathcontext, path);
748 cairo_path_destroy(path);
749 return new wxCairoPathData( GetRenderer() ,pathcontext);
750 }
751
752
753 void* wxCairoPathData::GetNativePath() const
754 {
755 return cairo_copy_path(m_pathContext) ;
756 }
757
758 void wxCairoPathData::UnGetNativePath(void *p) const
759 {
760 cairo_path_destroy((cairo_path_t*)p);
761 }
762
763 //
764 // The Primitives
765 //
766
767 void wxCairoPathData::MoveToPoint( wxDouble x , wxDouble y )
768 {
769 cairo_move_to(m_pathContext,x,y);
770 }
771
772 void wxCairoPathData::AddLineToPoint( wxDouble x , wxDouble y )
773 {
774 cairo_line_to(m_pathContext,x,y);
775 }
776
777 void wxCairoPathData::AddPath( const wxGraphicsPathData* path )
778 {
779 cairo_path_t* p = (cairo_path_t*)path->GetNativePath();
780 cairo_append_path(m_pathContext, p);
781 UnGetNativePath(p);
782 }
783
784 void wxCairoPathData::CloseSubpath()
785 {
786 cairo_close_path(m_pathContext);
787 }
788
789 void wxCairoPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
790 {
791 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
792 }
793
794 // gets the last point of the current path, (0,0) if not yet set
795 void wxCairoPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
796 {
797 double dx,dy;
798 cairo_get_current_point(m_pathContext,&dx,&dy);
799 if (x)
800 *x = dx;
801 if (y)
802 *y = dy;
803 }
804
805 void wxCairoPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
806 {
807 // as clockwise means positive in our system (y pointing downwards)
808 // TODO make this interpretation dependent of the
809 // real device trans
810 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
811 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
812 else
813 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
814 }
815
816 // transforms each point of this path by the matrix
817 void wxCairoPathData::Transform( const wxGraphicsMatrixData* matrix )
818 {
819 // as we don't have a true path object, we have to apply the inverse
820 // matrix to the context
821 cairo_matrix_t m = *((cairo_matrix_t*) matrix->GetNativeMatrix());
822 cairo_matrix_invert( &m );
823 cairo_transform(m_pathContext,&m);
824 }
825
826 // gets the bounding box enclosing all points (possibly including control points)
827 void wxCairoPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
828 {
829 double x1,y1,x2,y2;
830
831 cairo_stroke_extents( m_pathContext, &x1, &y1, &x2, &y2 );
832 if ( x2 < x1 )
833 {
834 *x = x2;
835 *w = x1-x2;
836 }
837 else
838 {
839 *x = x1;
840 *w = x2-x1;
841 }
842
843 if( y2 < y1 )
844 {
845 *y = y2;
846 *h = y1-y2;
847 }
848 else
849 {
850 *y = y1;
851 *h = y2-y1;
852 }
853 }
854
855 bool wxCairoPathData::Contains( wxDouble x, wxDouble y, int fillStyle ) const
856 {
857 return cairo_in_stroke( m_pathContext, x, y) != 0;
858 }
859
860 //-----------------------------------------------------------------------------
861 // wxCairoMatrixData implementation
862 //-----------------------------------------------------------------------------
863
864 wxCairoMatrixData::wxCairoMatrixData(wxGraphicsRenderer* renderer, const cairo_matrix_t* matrix )
865 : wxGraphicsMatrixData(renderer)
866 {
867 if ( matrix )
868 m_matrix = *matrix;
869 }
870
871 wxCairoMatrixData::~wxCairoMatrixData()
872 {
873 // nothing to do
874 }
875
876 wxGraphicsObjectRefData *wxCairoMatrixData::Clone() const
877 {
878 return new wxCairoMatrixData(GetRenderer(),&m_matrix);
879 }
880
881 // concatenates the matrix
882 void wxCairoMatrixData::Concat( const wxGraphicsMatrixData *t )
883 {
884 cairo_matrix_multiply( &m_matrix, &m_matrix, (cairo_matrix_t*) t->GetNativeMatrix());
885 }
886
887 // sets the matrix to the respective values
888 void wxCairoMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
889 wxDouble tx, wxDouble ty)
890 {
891 cairo_matrix_init( &m_matrix, a, b, c, d, tx, ty);
892 }
893
894 // gets the component valuess of the matrix
895 void wxCairoMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
896 wxDouble* d, wxDouble* tx, wxDouble* ty) const
897 {
898 if (a) *a = m_matrix.xx;
899 if (b) *b = m_matrix.yx;
900 if (c) *c = m_matrix.xy;
901 if (d) *d = m_matrix.yy;
902 if (tx) *tx= m_matrix.x0;
903 if (ty) *ty= m_matrix.y0;
904 }
905
906 // makes this the inverse matrix
907 void wxCairoMatrixData::Invert()
908 {
909 cairo_matrix_invert( &m_matrix );
910 }
911
912 // returns true if the elements of the transformation matrix are equal ?
913 bool wxCairoMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
914 {
915 const cairo_matrix_t* tm = (cairo_matrix_t*) t->GetNativeMatrix();
916 return (
917 m_matrix.xx == tm->xx &&
918 m_matrix.yx == tm->yx &&
919 m_matrix.xy == tm->xy &&
920 m_matrix.yy == tm->yy &&
921 m_matrix.x0 == tm->x0 &&
922 m_matrix.y0 == tm->y0 ) ;
923 }
924
925 // return true if this is the identity matrix
926 bool wxCairoMatrixData::IsIdentity() const
927 {
928 return ( m_matrix.xx == 1 && m_matrix.yy == 1 &&
929 m_matrix.yx == 0 && m_matrix.xy == 0 && m_matrix.x0 == 0 && m_matrix.y0 == 0);
930 }
931
932 //
933 // transformation
934 //
935
936 // add the translation to this matrix
937 void wxCairoMatrixData::Translate( wxDouble dx , wxDouble dy )
938 {
939 cairo_matrix_translate( &m_matrix, dx, dy) ;
940 }
941
942 // add the scale to this matrix
943 void wxCairoMatrixData::Scale( wxDouble xScale , wxDouble yScale )
944 {
945 cairo_matrix_scale( &m_matrix, xScale, yScale) ;
946 }
947
948 // add the rotation to this matrix (radians)
949 void wxCairoMatrixData::Rotate( wxDouble angle )
950 {
951 cairo_matrix_rotate( &m_matrix, angle) ;
952 }
953
954 //
955 // apply the transforms
956 //
957
958 // applies that matrix to the point
959 void wxCairoMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
960 {
961 double lx = *x, ly = *y ;
962 cairo_matrix_transform_point( &m_matrix, &lx, &ly);
963 *x = lx;
964 *y = ly;
965 }
966
967 // applies the matrix except for translations
968 void wxCairoMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
969 {
970 double lx = *dx, ly = *dy ;
971 cairo_matrix_transform_distance( &m_matrix, &lx, &ly);
972 *dx = lx;
973 *dy = ly;
974 }
975
976 // returns the native representation
977 void * wxCairoMatrixData::GetNativeMatrix() const
978 {
979 return (void*) &m_matrix;
980 }
981
982 //-----------------------------------------------------------------------------
983 // wxCairoBitmap implementation
984 //-----------------------------------------------------------------------------
985
986 wxCairoBitmapData::wxCairoBitmapData( wxGraphicsRenderer* renderer, const wxBitmap& bmp ) : wxGraphicsObjectRefData( renderer )
987 {
988 wxCHECK_RET( bmp.IsOk(), wxT("Invalid bitmap in wxCairoContext::DrawBitmap"));
989
990 int bw = m_width = bmp.GetWidth();
991 int bh = m_height = bmp.GetHeight();
992 wxBitmap bmpSource = bmp; // we need a non-const instance
993 m_buffer = new unsigned char[bw*bh*4];
994 wxUint32* data = (wxUint32*)m_buffer;
995
996 // Create a surface object and copy the bitmap pixel data to it. if the
997 // image has alpha (or a mask represented as alpha) then we'll use a
998 // different format and iterator than if it doesn't...
999 if (bmpSource.HasAlpha() || bmpSource.GetMask())
1000 {
1001 m_surface = cairo_image_surface_create_for_data(
1002 m_buffer, CAIRO_FORMAT_ARGB32, bw, bh, bw*4);
1003 wxAlphaPixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1004 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1005
1006 wxAlphaPixelData::Iterator p(pixData);
1007 for (int y=0; y<bh; y++)
1008 {
1009 wxAlphaPixelData::Iterator rowStart = p;
1010 for (int x=0; x<bw; x++)
1011 {
1012 // Each pixel in CAIRO_FORMAT_ARGB32 is a 32-bit quantity,
1013 // with alpha in the upper 8 bits, then red, then green, then
1014 // blue. The 32-bit quantities are stored native-endian.
1015 // Pre-multiplied alpha is used.
1016 unsigned char alpha = p.Alpha();
1017 if (alpha == 0)
1018 *data = 0;
1019 else
1020 *data = ( alpha << 24
1021 | (p.Red() * alpha/255) << 16
1022 | (p.Green() * alpha/255) << 8
1023 | (p.Blue() * alpha/255) );
1024 ++data;
1025 ++p;
1026 }
1027 p = rowStart;
1028 p.OffsetY(pixData, 1);
1029 }
1030 }
1031 else // no alpha
1032 {
1033 m_surface = cairo_image_surface_create_for_data(
1034 m_buffer, CAIRO_FORMAT_RGB24, bw, bh, bw*4);
1035 wxNativePixelData pixData(bmpSource, wxPoint(0,0), wxSize(bw, bh));
1036 wxCHECK_RET( pixData, wxT("Failed to gain raw access to bitmap data."));
1037
1038 wxNativePixelData::Iterator p(pixData);
1039 for (int y=0; y<bh; y++)
1040 {
1041 wxNativePixelData::Iterator rowStart = p;
1042 for (int x=0; x<bw; x++)
1043 {
1044 // Each pixel in CAIRO_FORMAT_RGB24 is a 32-bit quantity, with
1045 // the upper 8 bits unused. Red, Green, and Blue are stored in
1046 // the remaining 24 bits in that order. The 32-bit quantities
1047 // are stored native-endian.
1048 *data = ( p.Red() << 16 | p.Green() << 8 | p.Blue() );
1049 ++data;
1050 ++p;
1051 }
1052 p = rowStart;
1053 p.OffsetY(pixData, 1);
1054 }
1055 }
1056 m_pattern = cairo_pattern_create_for_surface(m_surface);
1057 }
1058
1059 wxCairoBitmapData::~wxCairoBitmapData()
1060 {
1061 cairo_pattern_destroy(m_pattern);
1062 cairo_surface_destroy(m_surface);
1063 delete [] m_buffer;
1064 }
1065
1066 //-----------------------------------------------------------------------------
1067 // wxCairoContext implementation
1068 //-----------------------------------------------------------------------------
1069
1070 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, const wxWindowDC& dc )
1071 : wxGraphicsContext(renderer)
1072 {
1073 #ifdef __WXGTK__
1074 m_context = gdk_cairo_create( dc.m_window ) ;
1075 #endif
1076 PushState();
1077 PushState();
1078 }
1079
1080 #ifdef __WXGTK__
1081 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, GdkDrawable *drawable )
1082 : wxGraphicsContext(renderer)
1083 {
1084 m_context = gdk_cairo_create( drawable ) ;
1085 PushState();
1086 PushState();
1087 }
1088 #endif
1089
1090 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, cairo_t *context )
1091 : wxGraphicsContext(renderer)
1092 {
1093 m_context = context ;
1094 PushState();
1095 PushState();
1096 }
1097
1098 wxCairoContext::wxCairoContext( wxGraphicsRenderer* renderer, wxWindow *window)
1099 : wxGraphicsContext(renderer)
1100 {
1101 #ifdef __WXGTK__
1102 // something along these lines (copied from dcclient)
1103
1104 GtkWidget *widget = window->m_wxwindow;
1105
1106 // Some controls don't have m_wxwindow - like wxStaticBox, but the user
1107 // code should still be able to create wxClientDCs for them, so we will
1108 // use the parent window here then.
1109 if ( !widget )
1110 {
1111 window = window->GetParent();
1112 widget = window->m_wxwindow;
1113 }
1114
1115 wxASSERT_MSG( widget, wxT("wxCairoContext needs a widget") );
1116
1117 GtkPizza *pizza = GTK_PIZZA( widget );
1118 GdkDrawable* drawable = pizza->bin_window;
1119 m_context = gdk_cairo_create( drawable ) ;
1120 #endif
1121 PushState();
1122 PushState();
1123 }
1124
1125 wxCairoContext::~wxCairoContext()
1126 {
1127 if ( m_context )
1128 {
1129 PopState();
1130 PopState();
1131 cairo_destroy(m_context);
1132 }
1133 }
1134
1135
1136 void wxCairoContext::Clip( const wxRegion& region )
1137 {
1138 // Create a path with all the rectangles in the region
1139 wxGraphicsPath path = GetRenderer()->CreatePath();
1140 wxRegionIterator ri(region);
1141 while (ri)
1142 {
1143 path.AddRectangle(ri.GetX(), ri.GetY(), ri.GetW(), ri.GetH());
1144 ri++;
1145 }
1146
1147 // Put it in the context
1148 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1149 cairo_append_path(m_context, cp);
1150
1151 // clip to that path
1152 cairo_clip(m_context);
1153 path.UnGetNativePath(cp);
1154 }
1155
1156 void wxCairoContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1157 {
1158 // Create a path with this rectangle
1159 wxGraphicsPath path = GetRenderer()->CreatePath();
1160 path.AddRectangle(x,y,w,h);
1161
1162 // Put it in the context
1163 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1164 cairo_append_path(m_context, cp);
1165
1166 // clip to that path
1167 cairo_clip(m_context);
1168 path.UnGetNativePath(cp);
1169 }
1170
1171 void wxCairoContext::ResetClip()
1172 {
1173 cairo_reset_clip(m_context);
1174 }
1175
1176
1177 void wxCairoContext::StrokePath( const wxGraphicsPath& path )
1178 {
1179 if ( !m_pen.IsNull() )
1180 {
1181 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1182 cairo_append_path(m_context,cp);
1183 ((wxCairoPenData*)m_pen.GetRefData())->Apply(this);
1184 cairo_stroke(m_context);
1185 path.UnGetNativePath(cp);
1186 }
1187 }
1188
1189 void wxCairoContext::FillPath( const wxGraphicsPath& path , int fillStyle )
1190 {
1191 if ( !m_brush.IsNull() )
1192 {
1193 cairo_path_t* cp = (cairo_path_t*) path.GetNativePath() ;
1194 cairo_append_path(m_context,cp);
1195 ((wxCairoBrushData*)m_brush.GetRefData())->Apply(this);
1196 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
1197 cairo_fill(m_context);
1198 path.UnGetNativePath(cp);
1199 }
1200 }
1201
1202 void wxCairoContext::Rotate( wxDouble angle )
1203 {
1204 cairo_rotate(m_context,angle);
1205 }
1206
1207 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
1208 {
1209 cairo_translate(m_context,dx,dy);
1210 }
1211
1212 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
1213 {
1214 cairo_scale(m_context,xScale,yScale);
1215 }
1216
1217 // concatenates this transform with the current transform of this context
1218 void wxCairoContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1219 {
1220 cairo_transform(m_context,(const cairo_matrix_t *) matrix.GetNativeMatrix());
1221 }
1222
1223 // sets the transform of this context
1224 void wxCairoContext::SetTransform( const wxGraphicsMatrix& matrix )
1225 {
1226 cairo_set_matrix(m_context,(const cairo_matrix_t*) matrix.GetNativeMatrix());
1227 }
1228
1229 // gets the matrix of this context
1230 wxGraphicsMatrix wxCairoContext::GetTransform() const
1231 {
1232 wxGraphicsMatrix matrix = CreateMatrix();
1233 cairo_get_matrix(m_context,(cairo_matrix_t*) matrix.GetNativeMatrix());
1234 return matrix;
1235 }
1236
1237
1238
1239 void wxCairoContext::PushState()
1240 {
1241 cairo_save(m_context);
1242 }
1243
1244 void wxCairoContext::PopState()
1245 {
1246 cairo_restore(m_context);
1247 }
1248
1249 void wxGraphicsContext::DrawGraphicsBitmap(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h)
1250 {
1251 static_cast<wxCairoContext*>(this)->DrawGraphicsBitmapInternal(bmp, x, y, w, h);
1252 }
1253
1254 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1255 {
1256 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1257 DrawGraphicsBitmapInternal(bitmap, x, y, w, h);
1258 }
1259
1260 void wxCairoContext::DrawGraphicsBitmapInternal(const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1261 {
1262 wxCairoBitmapData* data = static_cast<wxCairoBitmapData*>(bmp.GetRefData());
1263 cairo_pattern_t* pattern = data->GetCairoPattern();
1264 wxSize size = data->GetSize();
1265 PushState();
1266
1267 // In case we're scaling the image by using a width and height different
1268 // than the bitmap's size create a pattern transformation on the surface and
1269 // draw the transformed pattern.
1270 wxDouble scaleX = w / size.GetWidth();
1271 wxDouble scaleY = h / size.GetHeight();
1272 cairo_scale(m_context, scaleX, scaleY);
1273
1274 // prepare to draw the image
1275 cairo_translate(m_context, x, y);
1276 cairo_set_source(m_context, pattern);
1277 // use the original size here since the context is scaled already...
1278 cairo_rectangle(m_context, 0, 0, size.GetWidth(), size.GetHeight());
1279 // fill the rectangle using the pattern
1280 cairo_fill(m_context);
1281
1282 // clean up
1283 PopState();
1284 }
1285
1286 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1287 {
1288 // An icon is a bitmap on wxGTK, so do this the easy way. When we want to
1289 // start using the Cairo backend on other platforms then we may need to
1290 // fiddle with this...
1291 DrawBitmap(icon, x, y, w, h);
1292 }
1293
1294
1295 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1296 {
1297 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::DrawText - no valid font set") );
1298
1299 if ( str.empty())
1300 return;
1301
1302 ((wxCairoFontData*)m_font.GetRefData())->Apply(this);
1303
1304 // Cairo's x,y for drawing text is at the baseline, so we need to adjust
1305 // the position we move to by the ascent.
1306 cairo_font_extents_t fe;
1307 cairo_font_extents(m_context, &fe);
1308 cairo_move_to(m_context, x, y+fe.ascent);
1309
1310 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1311 cairo_show_text(m_context,buf);
1312 }
1313
1314 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1315 wxDouble *descent, wxDouble *externalLeading ) const
1316 {
1317 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetTextExtent - no valid font set") );
1318
1319 if ( str.empty())
1320 return;
1321
1322 ((wxCairoFontData*)m_font.GetRefData())->Apply((wxCairoContext*)this);
1323
1324 if (width)
1325 {
1326 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
1327 cairo_text_extents_t te;
1328 cairo_text_extents(m_context, buf, &te);
1329 *width = te.width;
1330 }
1331
1332 if (height || descent || externalLeading)
1333 {
1334 cairo_font_extents_t fe;
1335 cairo_font_extents(m_context, &fe);
1336
1337 if (height)
1338 *height = fe.height;
1339 if ( descent )
1340 *descent = fe.descent;
1341 if ( externalLeading )
1342 *externalLeading = wxMax(0, fe.height - (fe.ascent + fe.descent));
1343 }
1344 }
1345
1346 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1347 {
1348 widths.Empty();
1349 widths.Add(0, text.length());
1350
1351 wxCHECK_RET( !m_font.IsNull(), wxT("wxCairoContext::GetPartialTextExtents - no valid font set") );
1352
1353 if (text.empty())
1354 return;
1355
1356 // TODO
1357 }
1358
1359 void * wxCairoContext::GetNativeContext()
1360 {
1361 return m_context;
1362 }
1363
1364 //-----------------------------------------------------------------------------
1365 // wxCairoRenderer declaration
1366 //-----------------------------------------------------------------------------
1367
1368 class WXDLLIMPEXP_CORE wxCairoRenderer : public wxGraphicsRenderer
1369 {
1370 public :
1371 wxCairoRenderer() {}
1372
1373 virtual ~wxCairoRenderer() {}
1374
1375 // Context
1376
1377 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1378
1379 #ifdef __WXMSW__
1380 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1381 #endif
1382
1383 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1384
1385 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1386
1387 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1388
1389 virtual wxGraphicsContext * CreateMeasuringContext();
1390
1391 // Path
1392
1393 virtual wxGraphicsPath CreatePath();
1394
1395 // Matrix
1396
1397 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1398 wxDouble tx=0.0, wxDouble ty=0.0);
1399
1400
1401 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1402
1403 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1404
1405 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1406 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1407 const wxColour&c1, const wxColour&c2) ;
1408
1409 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1410 // with radius r and color cColor
1411 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1412 const wxColour &oColor, const wxColour &cColor) ;
1413
1414 // sets the font
1415 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1416
1417 wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap ) ;
1418
1419 private :
1420 DECLARE_DYNAMIC_CLASS_NO_COPY(wxCairoRenderer)
1421 } ;
1422
1423 //-----------------------------------------------------------------------------
1424 // wxCairoRenderer implementation
1425 //-----------------------------------------------------------------------------
1426
1427 IMPLEMENT_DYNAMIC_CLASS(wxCairoRenderer,wxGraphicsRenderer)
1428
1429 static wxCairoRenderer gs_cairoGraphicsRenderer;
1430
1431 #ifdef __WXGTK__
1432 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1433 {
1434 return &gs_cairoGraphicsRenderer;
1435 }
1436 #endif
1437
1438 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxWindowDC& dc)
1439 {
1440 return new wxCairoContext(this,dc);
1441 }
1442
1443 #ifdef __WXMSW__
1444 wxGraphicsContext * wxCairoRenderer::CreateContext( const wxMemoryDC& dc)
1445 {
1446 return NULL;
1447 }
1448 #endif
1449
1450 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeContext( void * context )
1451 {
1452 return new wxCairoContext(this,(cairo_t*)context);
1453 }
1454
1455
1456 wxGraphicsContext * wxCairoRenderer::CreateContextFromNativeWindow( void * window )
1457 {
1458 #ifdef __WXGTK__
1459 return new wxCairoContext(this,(GdkDrawable*)window);
1460 #else
1461 return NULL;
1462 #endif
1463 }
1464
1465 wxGraphicsContext * wxCairoRenderer::CreateMeasuringContext()
1466 {
1467 return NULL;
1468 // TODO
1469 }
1470
1471 wxGraphicsContext * wxCairoRenderer::CreateContext( wxWindow* window )
1472 {
1473 return new wxCairoContext(this, window );
1474 }
1475
1476 // Path
1477
1478 wxGraphicsPath wxCairoRenderer::CreatePath()
1479 {
1480 wxGraphicsPath path;
1481 path.SetRefData( new wxCairoPathData(this) );
1482 return path;
1483 }
1484
1485
1486 // Matrix
1487
1488 wxGraphicsMatrix wxCairoRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1489 wxDouble tx, wxDouble ty)
1490
1491 {
1492 wxGraphicsMatrix m;
1493 wxCairoMatrixData* data = new wxCairoMatrixData( this );
1494 data->Set( a,b,c,d,tx,ty ) ;
1495 m.SetRefData(data);
1496 return m;
1497 }
1498
1499 wxGraphicsPen wxCairoRenderer::CreatePen(const wxPen& pen)
1500 {
1501 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1502 return wxNullGraphicsPen;
1503 else
1504 {
1505 wxGraphicsPen p;
1506 p.SetRefData(new wxCairoPenData( this, pen ));
1507 return p;
1508 }
1509 }
1510
1511 wxGraphicsBrush wxCairoRenderer::CreateBrush(const wxBrush& brush )
1512 {
1513 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1514 return wxNullGraphicsBrush;
1515 else
1516 {
1517 wxGraphicsBrush p;
1518 p.SetRefData(new wxCairoBrushData( this, brush ));
1519 return p;
1520 }
1521 }
1522
1523 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1524 wxGraphicsBrush wxCairoRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1525 const wxColour&c1, const wxColour&c2)
1526 {
1527 wxGraphicsBrush p;
1528 wxCairoBrushData* d = new wxCairoBrushData( this );
1529 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1530 p.SetRefData(d);
1531 return p;
1532 }
1533
1534 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1535 // with radius r and color cColor
1536 wxGraphicsBrush wxCairoRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1537 const wxColour &oColor, const wxColour &cColor)
1538 {
1539 wxGraphicsBrush p;
1540 wxCairoBrushData* d = new wxCairoBrushData( this );
1541 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1542 p.SetRefData(d);
1543 return p;
1544 }
1545
1546 // sets the font
1547 wxGraphicsFont wxCairoRenderer::CreateFont( const wxFont &font , const wxColour &col )
1548 {
1549 if ( font.Ok() )
1550 {
1551 wxGraphicsFont p;
1552 p.SetRefData(new wxCairoFontData( this , font, col ));
1553 return p;
1554 }
1555 else
1556 return wxNullGraphicsFont;
1557 }
1558
1559 wxGraphicsBitmap wxGraphicsRenderer::CreateBitmap( const wxBitmap& bmp )
1560 {
1561 if ( bmp.Ok() )
1562 {
1563 wxGraphicsBitmap p;
1564 p.SetRefData(new wxCairoBitmapData( this , bmp ));
1565 return p;
1566 }
1567 else
1568 return wxNullGraphicsBitmap;
1569 }
1570
1571 #endif // wxUSE_GRAPHICS_CONTEXT

  ViewVC Help
Powered by ViewVC 1.1.22