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