]> git.saurik.com Git - wxWidgets.git/blob - src/generic/graphicc.cpp
Wrap module in #if wxUSE_GRAPHICS_CONTEXT
[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: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dc.h"
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/image.h"
25 #include "wx/window.h"
26 #include "wx/dc.h"
27 #include "wx/utils.h"
28 #include "wx/dialog.h"
29 #include "wx/app.h"
30 #include "wx/bitmap.h"
31 #include "wx/dcmemory.h"
32 #include "wx/log.h"
33 #include "wx/icon.h"
34 #include "wx/dcprint.h"
35 #include "wx/module.h"
36 #endif
37
38 #include "wx/graphics.h"
39
40 #if wxUSE_GRAPHICS_CONTEXT
41
42 #include <vector>
43
44 using namespace std;
45
46 //-----------------------------------------------------------------------------
47 // constants
48 //-----------------------------------------------------------------------------
49
50 const double RAD2DEG = 180.0 / M_PI;
51
52 //-----------------------------------------------------------------------------
53 // Local functions
54 //-----------------------------------------------------------------------------
55
56 static inline double dmin(double a, double b)
57 {
58 return a < b ? a : b;
59 }
60 static inline double dmax(double a, double b)
61 {
62 return a > b ? a : b;
63 }
64
65 static inline double DegToRad(double deg)
66 {
67 return (deg * M_PI) / 180.0;
68 }
69 static inline double RadToDeg(double deg)
70 {
71 return (deg * 180.0) / M_PI;
72 }
73
74 //-----------------------------------------------------------------------------
75 // device context implementation
76 //
77 // more and more of the dc functionality should be implemented by calling
78 // the appropricate wxCairoContext, but we will have to do that step by step
79 // also coordinate conversions should be moved to native matrix ops
80 //-----------------------------------------------------------------------------
81
82 // we always stock two context states, one at entry, to be able to preserve the
83 // state we were called with, the other one after changing to HI Graphics orientation
84 // (this one is used for getting back clippings etc)
85
86 //-----------------------------------------------------------------------------
87 // wxGraphicsPath implementation
88 //-----------------------------------------------------------------------------
89
90 // TODO remove this dependency (gdiplus needs the macros)
91
92 #ifndef max
93 #define max(a,b) (((a) > (b)) ? (a) : (b))
94 #endif
95
96 #ifndef min
97 #define min(a,b) (((a) < (b)) ? (a) : (b))
98 #endif
99
100 #include <cairo.h>
101 #include <gtk/gtk.h>
102
103 class WXDLLEXPORT wxCairoPath : public wxGraphicsPath
104 {
105 DECLARE_NO_COPY_CLASS(wxCairoPath)
106 public :
107 wxCairoPath();
108 ~wxCairoPath();
109
110
111 //
112 // These are the path primitives from which everything else can be constructed
113 //
114
115 // begins a new subpath at (x,y)
116 virtual void MoveToPoint( wxDouble x, wxDouble y );
117
118 // adds a straight line from the current point to (x,y)
119 virtual void AddLineToPoint( wxDouble x, wxDouble y );
120
121 // adds a cubic Bezier curve from the current point, using two control points and an end point
122 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
123
124
125 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
126 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
127
128 // gets the last point of the current path, (0,0) if not yet set
129 virtual void GetCurrentPoint( wxDouble& x, wxDouble&y) ;
130
131 // closes the current sub-path
132 virtual void CloseSubpath();
133
134 //
135 // These are convenience functions which - if not available natively will be assembled
136 // using the primitives from above
137 //
138
139 /*
140
141 // appends a rectangle as a new closed subpath
142 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
143 // appends an ellipsis as a new closed subpath fitting the passed rectangle
144 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
145
146 // 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)
147 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
148 */
149
150 cairo_path_t* GetPath() const;
151 private :
152 cairo_t* m_pathContext;
153 };
154
155 wxCairoPath::wxCairoPath()
156 {
157 cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,1,1);
158 m_pathContext = cairo_create(surface);
159 cairo_surface_destroy (surface);
160 }
161
162 wxCairoPath::~wxCairoPath()
163 {
164 cairo_destroy(m_pathContext);
165 }
166
167 cairo_path_t* wxCairoPath::GetPath() const
168 {
169 return cairo_copy_path(m_pathContext) ;
170 }
171
172 //
173 // The Primitives
174 //
175
176 void wxCairoPath::MoveToPoint( wxDouble x , wxDouble y )
177 {
178 cairo_move_to(m_pathContext,x,y);
179 }
180
181 void wxCairoPath::AddLineToPoint( wxDouble x , wxDouble y )
182 {
183 cairo_line_to(m_pathContext,x,y);
184 }
185
186 void wxCairoPath::CloseSubpath()
187 {
188 cairo_close_path(m_pathContext);
189 }
190
191 void wxCairoPath::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
192 {
193 cairo_curve_to(m_pathContext,cx1,cy1,cx2,cy2,x,y);
194 }
195
196 // gets the last point of the current path, (0,0) if not yet set
197 void wxCairoPath::GetCurrentPoint( wxDouble& x, wxDouble&y)
198 {
199 double dx,dy;
200 cairo_get_current_point(m_pathContext,&dx,&dy);
201 x = dx;
202 y = dy;
203 }
204
205 void wxCairoPath::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
206 {
207 // as clockwise means positive in our system (y pointing downwards)
208 // TODO make this interpretation dependent of the
209 // real device trans
210 if ( clockwise||(endAngle-startAngle)>=2*M_PI)
211 cairo_arc(m_pathContext,x,y,r,startAngle,endAngle);
212 else
213 cairo_arc_negative(m_pathContext,x,y,r,startAngle,endAngle);
214 }
215
216 /*
217 void wxCairoPath::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
218 {
219 m_path->AddRectangle(RectF(x,y,w,h));
220 }
221 */
222 //
223 //
224 //
225 /*
226 // closes the current subpath
227 void wxCairoPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
228 {
229 // CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
230 }
231
232 */
233
234 class WXDLLEXPORT wxCairoContext : public wxGraphicsContext
235 {
236 DECLARE_NO_COPY_CLASS(wxCairoContext)
237
238 public:
239 wxCairoContext( const wxWindowDC& dc );
240 wxCairoContext();
241 virtual ~wxCairoContext();
242
243 virtual void Clip( const wxRegion &region );
244 virtual void StrokePath( const wxGraphicsPath *p );
245 virtual void FillPath( const wxGraphicsPath *p , int fillStyle = wxWINDING_RULE );
246
247 virtual wxGraphicsPath* CreatePath();
248 virtual void SetPen( const wxPen &pen );
249 virtual void SetBrush( const wxBrush &brush );
250 virtual void SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2) ;
251 virtual void SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
252 const wxColour &oColor, const wxColour &cColor);
253
254 virtual void Translate( wxDouble dx , wxDouble dy );
255 virtual void Scale( wxDouble xScale , wxDouble yScale );
256 virtual void Rotate( wxDouble angle );
257
258 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
259 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
260 virtual void PushState();
261 virtual void PopState();
262
263 virtual void SetFont( const wxFont &font );
264 virtual void SetTextColor( const wxColour &col );
265 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
266 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
267 wxDouble *descent, wxDouble *externalLeading ) const;
268 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
269
270 private:
271 cairo_t* m_context;
272 bool m_penTransparent;
273 bool m_brushTransparent;
274
275 wxPen m_pen;
276 wxBrush m_brush;
277 cairo_pattern_t* m_brushPattern;
278 wxColour m_textColour;
279 };
280
281
282
283
284 //-----------------------------------------------------------------------------
285 // wxCairoContext implementation
286 //-----------------------------------------------------------------------------
287
288 wxCairoContext::wxCairoContext( const wxWindowDC& dc )
289 {
290 m_context = gdk_cairo_create( dc.m_window ) ;
291 PushState();
292 PushState();
293 m_penTransparent = true;
294 m_brushTransparent = true;
295 m_brushPattern = NULL ;
296 }
297
298 wxCairoContext::~wxCairoContext()
299 {
300 if ( m_context )
301 {
302 PopState();
303 PopState();
304 cairo_destroy(m_context);
305 if ( m_brushPattern )
306 cairo_pattern_destroy(m_brushPattern);
307 }
308 }
309
310
311 void wxCairoContext::Clip( const wxRegion &region )
312 {
313 // ClipCGContextToRegion ( m_context, &bounds , (RgnHandle) dc->m_macCurrentClipRgn );
314 }
315
316 void wxCairoContext::StrokePath( const wxGraphicsPath *p )
317 {
318 if ( m_penTransparent )
319 return;
320
321 const wxCairoPath* path = dynamic_cast< const wxCairoPath*>( p );
322 cairo_path_t* cp = path->GetPath() ;
323 cairo_append_path(m_context,cp);
324
325 // setup pen
326
327 // TODO: * m_dc->m_scaleX
328 double penWidth = m_pen.GetWidth();
329 if (penWidth <= 0.0)
330 penWidth = 0.1;
331
332 cairo_set_line_width(m_context,penWidth);
333 cairo_set_source_rgba(m_context,m_pen.GetColour().Red()/255.0,
334 m_pen.GetColour().Green()/255.0, m_pen.GetColour().Blue()/255.0,m_pen.GetColour().Alpha()/255.0);
335
336 cairo_line_cap_t cap;
337 switch ( m_pen.GetCap() )
338 {
339 case wxCAP_ROUND :
340 cap = CAIRO_LINE_CAP_ROUND;
341 break;
342
343 case wxCAP_PROJECTING :
344 cap = CAIRO_LINE_CAP_SQUARE;
345 break;
346
347 case wxCAP_BUTT :
348 cap = CAIRO_LINE_CAP_BUTT;
349 break;
350
351 default :
352 cap = CAIRO_LINE_CAP_BUTT;
353 break;
354 }
355 cairo_set_line_cap(m_context,cap);
356
357 cairo_line_join_t join;
358 switch ( m_pen.GetJoin() )
359 {
360 case wxJOIN_BEVEL :
361 join = CAIRO_LINE_JOIN_BEVEL;
362 break;
363
364 case wxJOIN_MITER :
365 join = CAIRO_LINE_JOIN_MITER;
366 break;
367
368 case wxJOIN_ROUND :
369 join = CAIRO_LINE_JOIN_ROUND;
370 break;
371
372 default :
373 join = CAIRO_LINE_JOIN_MITER;
374 break;
375 }
376 cairo_set_line_join(m_context,join);
377
378 int num_dashes = 0;
379 const double * dashes = NULL;
380 double offset = 0.0;
381
382 const double dashUnit = penWidth < 1.0 ? 1.0 : penWidth;
383
384 double *userLengths = NULL;
385 const double dotted[] =
386 {
387 dashUnit , dashUnit + 2.0
388 };
389 const double short_dashed[] =
390 {
391 9.0 , 6.0
392 };
393 const double dashed[] =
394 {
395 19.0 , 9.0
396 };
397 const double dotted_dashed[] =
398 {
399 9.0 , 6.0 , 3.0 , 3.0
400 };
401
402 switch ( m_pen.GetStyle() )
403 {
404 case wxSOLID :
405 break;
406
407 case wxDOT :
408 dashes = dotted ;
409 num_dashes = WXSIZEOF(dotted)
410 ;
411 break;
412
413 case wxLONG_DASH :
414 dashes = dotted ;
415 num_dashes = WXSIZEOF(dashed);
416 break;
417
418 case wxSHORT_DASH :
419 dashes = dotted ;
420 num_dashes = WXSIZEOF(short_dashed);
421 break;
422
423 case wxDOT_DASH :
424 dashes = dotted ;
425 num_dashes = WXSIZEOF(dotted_dashed);
426 break;
427
428 case wxUSER_DASH :
429 {
430 wxDash *wxdashes ;
431 num_dashes = m_pen.GetDashes( &wxdashes ) ;
432 if ((wxdashes != NULL) && (num_dashes > 0))
433 {
434 userLengths = new double[num_dashes] ;
435 for ( int i = 0 ; i < num_dashes ; ++i )
436 {
437 userLengths[i] = wxdashes[i] * dashUnit ;
438
439 if ( i % 2 == 1 && userLengths[i] < dashUnit + 2.0 )
440 userLengths[i] = dashUnit + 2.0 ;
441 else if ( i % 2 == 0 && userLengths[i] < dashUnit )
442 userLengths[i] = dashUnit ;
443 }
444 }
445 dashes = userLengths ;
446 }
447 break;
448 case wxSTIPPLE :
449 {
450 /*
451 wxBitmap* bmp = pen.GetStipple();
452 if ( bmp && bmp->Ok() )
453 {
454 wxDELETE( m_penImage );
455 wxDELETE( m_penBrush );
456 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
457 m_penBrush = new TextureBrush(m_penImage);
458 m_pen->SetBrush( m_penBrush );
459 }
460 */
461 }
462 break;
463 default :
464 if ( m_pen.GetStyle() >= wxFIRST_HATCH && m_pen.GetStyle() <= wxLAST_HATCH )
465 {
466 /*
467 wxDELETE( m_penBrush );
468 HatchStyle style = HatchStyleHorizontal;
469 switch( pen.GetStyle() )
470 {
471 case wxBDIAGONAL_HATCH :
472 style = HatchStyleBackwardDiagonal;
473 break ;
474 case wxCROSSDIAG_HATCH :
475 style = HatchStyleDiagonalCross;
476 break ;
477 case wxFDIAGONAL_HATCH :
478 style = HatchStyleForwardDiagonal;
479 break ;
480 case wxCROSS_HATCH :
481 style = HatchStyleCross;
482 break ;
483 case wxHORIZONTAL_HATCH :
484 style = HatchStyleHorizontal;
485 break ;
486 case wxVERTICAL_HATCH :
487 style = HatchStyleVertical;
488 break ;
489
490 }
491 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
492 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
493 m_pen->SetBrush( m_penBrush )
494 */
495 }
496 break;
497 }
498
499 cairo_set_dash(m_context,(double*)dashes,num_dashes,offset);
500 if ( userLengths )
501 delete[] userLengths;
502 cairo_stroke(m_context);
503 cairo_path_destroy(cp);
504 }
505
506 void wxCairoContext::FillPath( const wxGraphicsPath *p , int fillStyle )
507 {
508 if ( !m_brushTransparent )
509 {
510 const wxCairoPath* path = dynamic_cast< const wxCairoPath*>( p );
511 cairo_path_t* cp = path->GetPath() ;
512 cairo_append_path(m_context,cp);
513
514 if ( m_brushPattern )
515 {
516 cairo_set_source(m_context,m_brushPattern);
517 }
518 else
519 {
520 cairo_set_source_rgba(m_context,m_brush.GetColour().Red()/255.0,
521 m_brush.GetColour().Green()/255.0,
522 m_brush.GetColour().Blue()/255.0,
523 m_brush.GetColour().Alpha()/255.0);
524 }
525
526 cairo_set_fill_rule(m_context,fillStyle==wxODDEVEN_RULE ? CAIRO_FILL_RULE_EVEN_ODD : CAIRO_FILL_RULE_WINDING);
527 cairo_fill(m_context);
528 cairo_path_destroy(cp);
529 }
530 }
531
532 wxGraphicsPath* wxCairoContext::CreatePath()
533 {
534 return new wxCairoPath();
535 }
536
537 void wxCairoContext::Rotate( wxDouble angle )
538 {
539 cairo_rotate(m_context,angle);
540 }
541
542 void wxCairoContext::Translate( wxDouble dx , wxDouble dy )
543 {
544 cairo_translate(m_context,dx,dy);
545 }
546
547 void wxCairoContext::Scale( wxDouble xScale , wxDouble yScale )
548 {
549 cairo_scale(m_context,xScale,yScale);
550 /*
551 PointF penWidth( m_pen->GetWidth(), 0);
552 Matrix matrix ;
553 if ( !m_penTransparent )
554 {
555 m_context->GetTransform(&matrix);
556 matrix.TransformVectors(&penWidth);
557 }
558 m_context->ScaleTransform(xScale,yScale);
559 if ( !m_penTransparent )
560 {
561 m_context->GetTransform(&matrix);
562 matrix.Invert();
563 matrix.TransformVectors(&penWidth) ;
564 m_pen->SetWidth( sqrt( penWidth.X*penWidth.X + penWidth.Y*penWidth.Y));
565 }
566 */
567 }
568
569 void wxCairoContext::PushState()
570 {
571 cairo_save(m_context);
572 }
573
574 void wxCairoContext::PopState()
575 {
576 cairo_restore(m_context);
577 }
578
579 void wxCairoContext::SetTextColor( const wxColour &col )
580 {
581 m_textColour = col;
582 }
583
584 void wxCairoContext::SetPen( const wxPen &pen )
585 {
586 m_pen = pen ;
587 m_penTransparent = pen.GetStyle() == wxTRANSPARENT;
588 if ( m_penTransparent )
589 return;
590
591 /*
592
593 case wxSTIPPLE :
594 {
595 wxBitmap* bmp = pen.GetStipple();
596 if ( bmp && bmp->Ok() )
597 {
598 wxDELETE( m_penImage );
599 wxDELETE( m_penBrush );
600 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
601 m_penBrush = new TextureBrush(m_penImage);
602 m_pen->SetBrush( m_penBrush );
603 }
604
605 }
606 break;
607 default :
608 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
609 {
610 wxDELETE( m_penBrush );
611 HatchStyle style = HatchStyleHorizontal;
612 switch( pen.GetStyle() )
613 {
614 case wxBDIAGONAL_HATCH :
615 style = HatchStyleBackwardDiagonal;
616 break ;
617 case wxCROSSDIAG_HATCH :
618 style = HatchStyleDiagonalCross;
619 break ;
620 case wxFDIAGONAL_HATCH :
621 style = HatchStyleForwardDiagonal;
622 break ;
623 case wxCROSS_HATCH :
624 style = HatchStyleCross;
625 break ;
626 case wxHORIZONTAL_HATCH :
627 style = HatchStyleHorizontal;
628 break ;
629 case wxVERTICAL_HATCH :
630 style = HatchStyleVertical;
631 break ;
632
633 }
634 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
635 pen.GetColour().Green() , pen.GetColour().Blue() ), Color.Transparent );
636 m_pen->SetBrush( m_penBrush );
637 }
638 break;
639 }
640 if ( dashStyle != DashStyleSolid )
641 m_pen->SetDashStyle(dashStyle);
642 */
643 }
644
645 void wxCairoContext::SetBrush( const wxBrush &brush )
646 {
647 m_brush = brush;
648 if (m_brushPattern)
649 {
650 cairo_pattern_destroy(m_brushPattern);
651 m_brushPattern = NULL;
652 }
653 m_brushTransparent = brush.GetStyle() == wxTRANSPARENT;
654
655 if ( m_brushTransparent )
656 return;
657 /*
658 wxDELETE(m_brush);
659
660 if ( brush.GetStyle() == wxSOLID)
661 {
662 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
663 brush.GetColour().Green() , brush.GetColour().Blue() ) );
664 }
665 else if ( brush.IsHatch() )
666 {
667 HatchStyle style = HatchStyleHorizontal;
668 switch( brush.GetStyle() )
669 {
670 case wxBDIAGONAL_HATCH :
671 style = HatchStyleBackwardDiagonal;
672 break ;
673 case wxCROSSDIAG_HATCH :
674 style = HatchStyleDiagonalCross;
675 break ;
676 case wxFDIAGONAL_HATCH :
677 style = HatchStyleForwardDiagonal;
678 break ;
679 case wxCROSS_HATCH :
680 style = HatchStyleCross;
681 break ;
682 case wxHORIZONTAL_HATCH :
683 style = HatchStyleHorizontal;
684 break ;
685 case wxVERTICAL_HATCH :
686 style = HatchStyleVertical;
687 break ;
688
689 }
690 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
691 brush.GetColour().Green() , brush.GetColour().Blue() ), Color.Transparent );
692 }
693 else
694 {
695 wxBitmap* bmp = brush.GetStipple();
696 if ( bmp && bmp->Ok() )
697 {
698 wxDELETE( m_brushImage );
699 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
700 m_brush = new TextureBrush(m_brushImage);
701 }
702 }
703 */
704 }
705
706 void wxCairoContext::SetLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2)
707 {
708 if ( m_brushPattern )
709 {
710 cairo_pattern_destroy(m_brushPattern);
711 m_brushPattern=NULL;
712 }
713
714 m_brushTransparent = false;
715 m_brushPattern = cairo_pattern_create_linear(x1,y1,x2,y2);
716 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,c1.Red()/255.0,
717 c1.Green()/255.0, c1.Blue()/255.0,c1.Alpha()/255.0);
718 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,c2.Red()/255.0,
719 c2.Green()/255.0, c2.Blue()/255.0,c2.Alpha()/255.0);
720 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
721 }
722
723 void wxCairoContext::SetRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
724 const wxColour &oColor, const wxColour &cColor)
725 {
726 if ( m_brushPattern )
727 {
728 cairo_pattern_destroy(m_brushPattern);
729 m_brushPattern=NULL;
730 }
731
732 m_brushTransparent = false;
733 m_brushPattern = cairo_pattern_create_radial(xo,yo,0.0,xc,yc,radius);
734 cairo_pattern_add_color_stop_rgba(m_brushPattern,0.0,oColor.Red()/255.0,
735 oColor.Green()/255.0, oColor.Blue()/255.0,oColor.Alpha()/255.0);
736 cairo_pattern_add_color_stop_rgba(m_brushPattern,1.0,cColor.Red()/255.0,
737 cColor.Green()/255.0, cColor.Blue()/255.0,cColor.Alpha()/255.0);
738 wxASSERT_MSG(cairo_pattern_status(m_brushPattern) == CAIRO_STATUS_SUCCESS, wxT("Couldn't create cairo pattern"));
739 }
740
741 void wxCairoContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
742 {
743 /*
744 Bitmap* image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
745 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
746 delete image ;
747 */
748 }
749
750 void wxCairoContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
751 {
752 /*
753 Bitmap* image = Bitmap::FromHICON((HICON)icon.GetHICON());
754 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
755 delete image ;
756 */
757 }
758
759
760 void wxCairoContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
761 {
762 if ( str.IsEmpty())
763 return ;
764 cairo_move_to(m_context,x,y);
765 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
766
767 cairo_set_source_rgba(m_context,m_textColour.Red()/255.0,
768 m_textColour.Green()/255.0, m_textColour.Blue()/255.0,m_textColour.Alpha()/255.0);
769 cairo_show_text(m_context,buf);
770
771 // TODO m_backgroundMode == wxSOLID
772 }
773
774 void wxCairoContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
775 wxDouble *descent, wxDouble *externalLeading ) const
776 {
777 /*
778 wxWCharBuffer s = str.wc_str( *wxConvUI );
779 FontFamily ffamily ;
780
781 m_font->GetFamily(&ffamily) ;
782
783 REAL factorY = m_context->GetDpiY() / 72.0 ;
784
785 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
786 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
787 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
788 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
789 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
790 m_font->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
791
792 if ( height )
793 *height = rHeight * factorY + 0.5 ;
794 if ( descent )
795 *descent = rDescent * factorY + 0.5 ;
796 if ( externalLeading )
797 *externalLeading = (rHeight - rAscent - rDescent) * factorY + 0.5 ;
798 // measuring empty strings is not guaranteed, so do it by hand
799 if ( str.IsEmpty())
800 {
801 if ( width )
802 *width = 0 ;
803 }
804 else
805 {
806 // MeasureString does return a rectangle that is way too large, so it is
807 // not usable here
808 RectF layoutRect(0,0, 100000.0f, 100000.0f);
809 StringFormat strFormat;
810 CharacterRange strRange(0,wcslen(s));
811 strFormat.SetMeasurableCharacterRanges(1,&strRange);
812 Region region ;
813 m_context->MeasureCharacterRanges(s, -1 , m_font,layoutRect, &strFormat,1,&region) ;
814 RectF bbox ;
815 region.GetBounds(&bbox,m_context);
816 if ( width )
817 *width = bbox.GetRight()-bbox.GetLeft()+0.5;
818 }
819 */
820 }
821
822 void wxCairoContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
823 {
824 widths.Empty();
825 widths.Add(0, text.length());
826
827 if (text.empty())
828 return;
829 /*
830 wxWCharBuffer ws = text.wc_str( *wxConvUI );
831 size_t len = wcslen( ws ) ;
832 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
833
834 RectF layoutRect(0,0, 100000.0f, 100000.0f);
835 StringFormat strFormat;
836
837 CharacterRange* ranges = new CharacterRange[len] ;
838 Region* regions = new Region[len];
839 for( int i = 0 ; i < len ; ++i)
840 {
841 ranges[i].First = i ;
842 ranges[i].Length = 1 ;
843 }
844 strFormat.SetMeasurableCharacterRanges(len,ranges);
845 m_context->MeasureCharacterRanges(ws, -1 , m_font,layoutRect, &strFormat,1,regions) ;
846
847 RectF bbox ;
848 for ( int i = 0 ; i < len ; ++i)
849 {
850 regions[i].GetBounds(&bbox,m_context);
851 widths[i] = bbox.GetRight()-bbox.GetLeft();
852 }
853 */
854 }
855
856 void wxCairoContext::SetFont( const wxFont &font )
857 {
858 cairo_select_font_face(m_context,font.GetFaceName().mb_str(wxConvUTF8),
859 font.GetStyle() == wxFONTSTYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC:CAIRO_FONT_SLANT_NORMAL,
860 font.GetWeight() == wxFONTWEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL);
861
862 cairo_set_font_size(m_context,font.GetPointSize());
863 // TODO UNDERLINE
864 // TODO FIX SIZE
865 }
866
867 wxGraphicsContext* wxGraphicsContext::Create( const wxWindowDC& dc )
868 {
869 return new wxCairoContext(dc);
870 }
871
872 #endif // wxUSE_GRAPHICS_CONTEXT