1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/graphics.cpp
3 // Purpose: wxGCDC class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #if wxUSE_GRAPHICS_CONTEXT
23 #include "wx/msw/wrapcdlg.h"
25 #include "wx/window.h"
27 #include "wx/dialog.h"
29 #include "wx/bitmap.h"
32 #include "wx/module.h"
33 // include all dc types that are used as a param
35 #include "wx/dcclient.h"
36 #include "wx/dcmemory.h"
37 #include "wx/dcprint.h"
40 #include "wx/private/graphics.h"
41 #include "wx/msw/wrapgdip.h"
42 #include "wx/msw/dc.h"
46 WX_DECLARE_STACK(GraphicsState
, GraphicsStates
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 const double RAD2DEG
= 180.0 / M_PI
;
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
59 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
61 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
62 static inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
64 //-----------------------------------------------------------------------------
65 // device context implementation
67 // more and more of the dc functionality should be implemented by calling
68 // the appropricate wxGDIPlusContext, but we will have to do that step by step
69 // also coordinate conversions should be moved to native matrix ops
70 //-----------------------------------------------------------------------------
72 // we always stock two context states, one at entry, to be able to preserve the
73 // state we were called with, the other one after changing to HI Graphics orientation
74 // (this one is used for getting back clippings etc)
76 //-----------------------------------------------------------------------------
77 // wxGraphicsPath implementation
78 //-----------------------------------------------------------------------------
80 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
82 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
86 class wxGDIPlusPathData
: public wxGraphicsPathData
89 wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
92 virtual wxGraphicsObjectRefData
*Clone() const;
95 // These are the path primitives from which everything else can be constructed
98 // begins a new subpath at (x,y)
99 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
101 // adds a straight line from the current point to (x,y)
102 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
104 // adds a cubic Bezier curve from the current point, using two control points and an end point
105 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
108 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
109 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
111 // gets the last point of the current path, (0,0) if not yet set
112 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
115 virtual void AddPath( const wxGraphicsPathData
* path
);
117 // closes the current sub-path
118 virtual void CloseSubpath();
121 // These are convenience functions which - if not available natively will be assembled
122 // using the primitives from above
125 // appends a rectangle as a new closed subpath
126 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
129 // appends an ellipsis as a new closed subpath fitting the passed rectangle
130 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
132 // 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)
133 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
136 // returns the native path
137 virtual void * GetNativePath() const { return m_path
; }
139 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
140 virtual void UnGetNativePath(void * WXUNUSED(path
)) const {}
142 // transforms each point of this path by the matrix
143 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
145 // gets the bounding box enclosing all points (possibly including control points)
146 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
148 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
151 GraphicsPath
* m_path
;
154 class wxGDIPlusMatrixData
: public wxGraphicsMatrixData
157 wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
158 virtual ~wxGDIPlusMatrixData() ;
160 virtual wxGraphicsObjectRefData
* Clone() const ;
162 // concatenates the matrix
163 virtual void Concat( const wxGraphicsMatrixData
*t
);
165 // sets the matrix to the respective values
166 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
167 wxDouble tx
=0.0, wxDouble ty
=0.0);
169 // gets the component valuess of the matrix
170 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
171 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
173 // makes this the inverse matrix
174 virtual void Invert();
176 // returns true if the elements of the transformation matrix are equal ?
177 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
179 // return true if this is the identity matrix
180 virtual bool IsIdentity() const;
186 // add the translation to this matrix
187 virtual void Translate( wxDouble dx
, wxDouble dy
);
189 // add the scale to this matrix
190 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
192 // add the rotation to this matrix (radians)
193 virtual void Rotate( wxDouble angle
);
196 // apply the transforms
199 // applies that matrix to the point
200 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
202 // applies the matrix except for translations
203 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
205 // returns the native representation
206 virtual void * GetNativeMatrix() const;
211 class wxGDIPlusPenData
: public wxGraphicsObjectRefData
214 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
219 virtual wxDouble
GetWidth() { return m_width
; }
220 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
230 class wxGDIPlusBrushData
: public wxGraphicsObjectRefData
233 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
234 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
235 ~wxGDIPlusBrushData ();
237 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
238 const wxColour
&c1
, const wxColour
&c2
);
239 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
240 const wxColour
&oColor
, const wxColour
&cColor
);
241 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
249 GraphicsPath
* m_brushPath
;
252 class WXDLLIMPEXP_CORE wxGDIPlusBitmapData
: public wxGraphicsObjectRefData
255 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
);
256 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
&bmp
);
257 ~wxGDIPlusBitmapData ();
259 virtual Bitmap
* GetGDIPlusBitmap() { return m_bitmap
; }
266 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
269 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
270 ~wxGDIPlusFontData();
272 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
273 virtual Font
* GetGDIPlusFont() { return m_font
; }
279 class wxGDIPlusContext
: public wxGraphicsContext
282 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
283 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
284 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
287 virtual ~wxGDIPlusContext();
289 virtual void Clip( const wxRegion
®ion
);
290 // clips drawings to the rect
291 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
293 // resets the clipping to original extent
294 virtual void ResetClip();
296 virtual void * GetNativeContext();
298 virtual void StrokePath( const wxGraphicsPath
& p
);
299 virtual void FillPath( const wxGraphicsPath
& p
, int fillStyle
= wxODDEVEN_RULE
);
301 // stroke lines connecting each of the points
302 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
305 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, int fillStyle
= wxODDEVEN_RULE
);
307 virtual void Translate( wxDouble dx
, wxDouble dy
);
308 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
309 virtual void Rotate( wxDouble angle
);
311 // concatenates this transform with the current transform of this context
312 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
314 // sets the transform of this context
315 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
317 // gets the matrix of this context
318 virtual wxGraphicsMatrix
GetTransform() const;
320 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
321 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
322 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
323 virtual void PushState();
324 virtual void PopState();
326 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
327 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
328 wxDouble
*descent
, wxDouble
*externalLeading
) const;
329 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
330 virtual bool ShouldOffset() const;
331 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
338 GraphicsStates m_stateStack
;
339 GraphicsState m_state1
;
340 GraphicsState m_state2
;
345 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
348 class WXDLLIMPEXP_CORE wxGDIPlusMeasuringContext
: public wxGDIPlusContext
351 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
354 wxGDIPlusMeasuringContext()
358 virtual ~wxGDIPlusMeasuringContext()
360 ReleaseDC( NULL
, m_hdc
);
365 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
368 //-----------------------------------------------------------------------------
369 // wxGDIPlusPen implementation
370 //-----------------------------------------------------------------------------
372 wxGDIPlusPenData::~wxGDIPlusPenData()
379 void wxGDIPlusPenData::Init()
386 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
387 : wxGraphicsObjectRefData(renderer
)
390 m_width
= pen
.GetWidth();
394 m_pen
= new Pen(Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
395 pen
.GetColour().Green() , pen
.GetColour().Blue() ), m_width
);
398 switch ( pen
.GetCap() )
404 case wxCAP_PROJECTING
:
409 cap
= LineCapFlat
; // TODO verify
416 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
419 switch ( pen
.GetJoin() )
422 join
= LineJoinBevel
;
426 join
= LineJoinMiter
;
430 join
= LineJoinRound
;
434 join
= LineJoinMiter
;
438 m_pen
->SetLineJoin(join
);
440 m_pen
->SetDashStyle(DashStyleSolid
);
442 DashStyle dashStyle
= DashStyleSolid
;
443 switch ( pen
.GetStyle() )
449 dashStyle
= DashStyleDot
;
453 dashStyle
= DashStyleDash
; // TODO verify
457 dashStyle
= DashStyleDash
;
461 dashStyle
= DashStyleDashDot
;
465 dashStyle
= DashStyleCustom
;
467 int count
= pen
.GetDashes( &dashes
);
468 if ((dashes
!= NULL
) && (count
> 0))
470 REAL
*userLengths
= new REAL
[count
];
471 for ( int i
= 0; i
< count
; ++i
)
473 userLengths
[i
] = dashes
[i
];
475 m_pen
->SetDashPattern( userLengths
, count
);
476 delete[] userLengths
;
482 wxBitmap
* bmp
= pen
.GetStipple();
483 if ( bmp
&& bmp
->Ok() )
485 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
486 m_penBrush
= new TextureBrush(m_penImage
);
487 m_pen
->SetBrush( m_penBrush
);
493 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
495 HatchStyle style
= HatchStyleHorizontal
;
496 switch( pen
.GetStyle() )
498 case wxBDIAGONAL_HATCH
:
499 style
= HatchStyleBackwardDiagonal
;
501 case wxCROSSDIAG_HATCH
:
502 style
= HatchStyleDiagonalCross
;
504 case wxFDIAGONAL_HATCH
:
505 style
= HatchStyleForwardDiagonal
;
508 style
= HatchStyleCross
;
510 case wxHORIZONTAL_HATCH
:
511 style
= HatchStyleHorizontal
;
513 case wxVERTICAL_HATCH
:
514 style
= HatchStyleVertical
;
518 m_penBrush
= new HatchBrush(style
,Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
519 pen
.GetColour().Green() , pen
.GetColour().Blue() ), Color::Transparent
);
520 m_pen
->SetBrush( m_penBrush
);
524 if ( dashStyle
!= DashStyleSolid
)
525 m_pen
->SetDashStyle(dashStyle
);
528 //-----------------------------------------------------------------------------
529 // wxGDIPlusBrush implementation
530 //-----------------------------------------------------------------------------
532 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
533 : wxGraphicsObjectRefData(renderer
)
538 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
539 : wxGraphicsObjectRefData(renderer
)
542 if ( brush
.GetStyle() == wxSOLID
)
544 m_brush
= new SolidBrush( Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
545 brush
.GetColour().Green() , brush
.GetColour().Blue() ) );
547 else if ( brush
.IsHatch() )
549 HatchStyle style
= HatchStyleHorizontal
;
550 switch( brush
.GetStyle() )
552 case wxBDIAGONAL_HATCH
:
553 style
= HatchStyleBackwardDiagonal
;
555 case wxCROSSDIAG_HATCH
:
556 style
= HatchStyleDiagonalCross
;
558 case wxFDIAGONAL_HATCH
:
559 style
= HatchStyleForwardDiagonal
;
562 style
= HatchStyleCross
;
564 case wxHORIZONTAL_HATCH
:
565 style
= HatchStyleHorizontal
;
567 case wxVERTICAL_HATCH
:
568 style
= HatchStyleVertical
;
572 m_brush
= new HatchBrush(style
,Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
573 brush
.GetColour().Green() , brush
.GetColour().Blue() ), Color::Transparent
);
577 wxBitmap
* bmp
= brush
.GetStipple();
578 if ( bmp
&& bmp
->Ok() )
580 wxDELETE( m_brushImage
);
581 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
582 m_brush
= new TextureBrush(m_brushImage
);
587 wxGDIPlusBrushData::~wxGDIPlusBrushData()
594 void wxGDIPlusBrushData::Init()
601 void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
603 m_brush
= new LinearGradientBrush( PointF( x1
,y1
) , PointF( x2
,y2
),
604 Color( c1
.Alpha(), c1
.Red(),c1
.Green() , c1
.Blue() ),
605 Color( c2
.Alpha(), c2
.Red(),c2
.Green() , c2
.Blue() ));
608 void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
609 const wxColour
&oColor
, const wxColour
&cColor
)
611 // Create a path that consists of a single circle.
612 m_brushPath
= new GraphicsPath();
613 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
), (REAL
)(2*radius
), (REAL
)(2*radius
));
615 PathGradientBrush
*b
= new PathGradientBrush(m_brushPath
);
617 b
->SetCenterPoint( PointF(xo
,yo
));
618 b
->SetCenterColor(Color( oColor
.Alpha(), oColor
.Red(),oColor
.Green() , oColor
.Blue() ));
620 Color colors
[] = {Color( cColor
.Alpha(), cColor
.Red(),cColor
.Green() , cColor
.Blue() )};
622 b
->SetSurroundColors(colors
, &count
);
625 //-----------------------------------------------------------------------------
626 // wxGDIPlusFont implementation
627 //-----------------------------------------------------------------------------
629 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
630 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
635 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
636 int size
= font
.GetPointSize();
637 int style
= FontStyleRegular
;
638 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
639 style
|= FontStyleItalic
;
640 if ( font
.GetUnderlined() )
641 style
|= FontStyleUnderline
;
642 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
643 style
|= FontStyleBold
;
644 m_font
= new Font( s
, size
, style
);
645 m_textBrush
= new SolidBrush( Color( col
.Alpha() , col
.Red() ,
646 col
.Green() , col
.Blue() ));
649 wxGDIPlusFontData::~wxGDIPlusFontData()
655 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
656 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
657 // bytes as parameter, since there is no real copying of the data going in, only references are stored
658 // m_helper has to be kept alive as well
660 //-----------------------------------------------------------------------------
661 // wxGDIPlusBitmapData implementation
662 //-----------------------------------------------------------------------------
664 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
665 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
670 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
671 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
676 Bitmap
* image
= NULL
;
679 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
681 size_t width
= interim
.GetWidth();
682 size_t height
= interim
.GetHeight();
683 Rect
bounds(0,0,width
,height
);
685 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
687 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
688 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
690 BitmapData dataMask
;
691 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
692 interimMask
.GetPixelFormat(),&dataMask
);
695 BitmapData imageData
;
696 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
698 BYTE maskPattern
= 0 ;
702 for ( size_t y
= 0 ; y
< height
; ++y
)
705 for( size_t x
= 0 ; x
< width
; ++x
)
710 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
714 maskPattern
= maskPattern
>> 1;
716 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
717 if ( (maskByte
& maskPattern
) == 0 )
722 interim
.GetPixel(x
,y
,&c
) ;
723 *dest
= (c
.GetValue() | Color::AlphaMask
);
728 image
->UnlockBits(&imageData
);
730 interimMask
.UnlockBits(&dataMask
);
731 interim
.UnlockBits(&dataMask
);
735 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
736 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
738 size_t width
= image
->GetWidth();
739 size_t height
= image
->GetHeight();
740 Rect
bounds(0,0,width
,height
);
741 static BitmapData data
;
745 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
746 m_helper
->GetPixelFormat(),&data
);
748 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
749 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
751 m_helper
->UnlockBits(&data
);
758 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
764 //-----------------------------------------------------------------------------
765 // wxGDIPlusPath implementation
766 //-----------------------------------------------------------------------------
768 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
773 m_path
= new GraphicsPath();
776 wxGDIPlusPathData::~wxGDIPlusPathData()
781 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
783 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
790 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
792 m_path
->StartFigure();
793 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
796 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
798 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
801 void wxGDIPlusPathData::CloseSubpath()
803 m_path
->CloseFigure();
806 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
812 m_path
->GetLastPoint(&start
);
813 m_path
->AddBezier(start
,c1
,c2
,end
);
816 // gets the last point of the current path, (0,0) if not yet set
817 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
820 m_path
->GetLastPoint(&start
);
825 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
827 double sweepAngle
= endAngle
- startAngle
;
828 if( fabs(sweepAngle
) >= 2*M_PI
)
830 sweepAngle
= 2 * M_PI
;
837 sweepAngle
+= 2 * M_PI
;
842 sweepAngle
-= 2 * M_PI
;
846 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
849 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
851 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
854 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
856 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
860 // transforms each point of this path by the matrix
861 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
863 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
866 // gets the bounding box enclosing all points (possibly including control points)
867 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
870 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
877 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
879 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
880 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
883 //-----------------------------------------------------------------------------
884 // wxGDIPlusMatrixData implementation
885 //-----------------------------------------------------------------------------
887 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
888 : wxGraphicsMatrixData(renderer
)
893 m_matrix
= new Matrix();
896 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
901 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
903 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
906 // concatenates the matrix
907 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
909 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
912 // sets the matrix to the respective values
913 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
914 wxDouble tx
, wxDouble ty
)
916 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
919 // gets the component valuess of the matrix
920 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
921 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
924 m_matrix
->GetElements(elements
);
925 if (a
) *a
= elements
[0];
926 if (b
) *b
= elements
[1];
927 if (c
) *c
= elements
[2];
928 if (d
) *d
= elements
[3];
929 if (tx
) *tx
= elements
[4];
930 if (ty
) *ty
= elements
[5];
933 // makes this the inverse matrix
934 void wxGDIPlusMatrixData::Invert()
939 // returns true if the elements of the transformation matrix are equal ?
940 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
942 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
945 // return true if this is the identity matrix
946 bool wxGDIPlusMatrixData::IsIdentity() const
948 return m_matrix
->IsIdentity() == TRUE
;
955 // add the translation to this matrix
956 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
958 m_matrix
->Translate(dx
,dy
);
961 // add the scale to this matrix
962 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
964 m_matrix
->Scale(xScale
,yScale
);
967 // add the rotation to this matrix (radians)
968 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
970 m_matrix
->Rotate( angle
);
974 // apply the transforms
977 // applies that matrix to the point
978 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
981 m_matrix
->TransformPoints(&pt
);
986 // applies the matrix except for translations
987 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
990 m_matrix
->TransformVectors(&pt
);
995 // returns the native representation
996 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1001 //-----------------------------------------------------------------------------
1002 // wxGDIPlusContext implementation
1003 //-----------------------------------------------------------------------------
1005 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1006 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1008 class wxGDIPlusOffsetHelper
1011 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1016 m_gr
->TranslateTransform( 0.5, 0.5 );
1018 ~wxGDIPlusOffsetHelper( )
1021 m_gr
->TranslateTransform( -0.5, -0.5 );
1028 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1029 : wxGraphicsContext(renderer
)
1032 m_context
= new Graphics( hdc
);
1038 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1039 : wxGraphicsContext(renderer
)
1042 m_context
= new Graphics( hwnd
);
1043 RECT rect
= wxGetWindowRect(hwnd
);
1044 m_width
= rect
.right
- rect
.left
;
1045 m_height
= rect
.bottom
- rect
.top
;
1049 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1050 : wxGraphicsContext(renderer
)
1057 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1062 void wxGDIPlusContext::Init()
1071 void wxGDIPlusContext::SetDefaults()
1073 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1074 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1075 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1076 m_state1
= m_context
->Save();
1077 m_state2
= m_context
->Save();
1080 wxGDIPlusContext::~wxGDIPlusContext()
1084 m_context
->Restore( m_state2
);
1085 m_context
->Restore( m_state1
);
1091 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1093 Region
rgn((HRGN
)region
.GetHRGN());
1094 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1097 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1099 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1102 void wxGDIPlusContext::ResetClip()
1104 m_context
->ResetClip();
1107 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1109 if ( !m_pen
.IsNull() )
1111 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1112 Point
*cpoints
= new Point
[n
];
1113 for (size_t i
= 0; i
< n
; i
++)
1115 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1116 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1118 } // for (size_t i = 0; i < n; i++)
1119 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1124 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int WXUNUSED(fillStyle
) )
1126 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1127 Point
*cpoints
= new Point
[n
];
1128 for (size_t i
= 0; i
< n
; i
++)
1130 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1131 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1133 } // for (int i = 0; i < n; i++)
1134 if ( !m_brush
.IsNull() )
1135 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1136 if ( !m_pen
.IsNull() )
1137 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1141 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1143 if ( !m_pen
.IsNull() )
1145 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1146 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1150 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, int fillStyle
)
1152 if ( !m_brush
.IsNull() )
1154 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1155 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1156 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1157 (GraphicsPath
*) path
.GetNativePath());
1161 void wxGDIPlusContext::Rotate( wxDouble angle
)
1163 m_context
->RotateTransform( RadToDeg(angle
) );
1166 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1168 m_context
->TranslateTransform( dx
, dy
);
1171 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1173 m_context
->ScaleTransform(xScale
,yScale
);
1176 void wxGDIPlusContext::PushState()
1178 GraphicsState state
= m_context
->Save();
1179 m_stateStack
.push(state
);
1182 void wxGDIPlusContext::PopState()
1184 GraphicsState state
= m_stateStack
.top();
1186 m_context
->Restore(state
);
1189 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1191 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1194 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1196 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1197 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1198 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1199 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1202 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1206 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1208 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1209 DrawBitmap(bitmap
, x
, y
, w
, h
);
1212 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1214 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1215 // find out by looking at the bitmap data whether there really was alpha in it
1216 HICON hIcon
= (HICON
)icon
.GetHICON();
1218 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1219 if (!GetIconInfo(hIcon
,&iconInfo
))
1222 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1224 Bitmap
* image
= NULL
;
1226 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1227 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1228 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1230 image
= Bitmap::FromHICON(hIcon
);
1234 size_t width
= interim
.GetWidth();
1235 size_t height
= interim
.GetHeight();
1236 Rect
bounds(0,0,width
,height
);
1239 interim
.LockBits(&bounds
, ImageLockModeRead
,
1240 interim
.GetPixelFormat(),&data
);
1242 bool hasAlpha
= false;
1243 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1245 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1247 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1248 if ( ( *dest
& Color::AlphaMask
) != 0 )
1255 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1256 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1260 image
= Bitmap::FromHICON(hIcon
);
1263 interim
.UnlockBits(&data
);
1266 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1269 DeleteObject(iconInfo
.hbmColor
);
1270 DeleteObject(iconInfo
.hbmMask
);
1273 void wxGDIPlusContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1275 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::DrawText - no valid font set") );
1280 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1281 m_context
->DrawString( s
, -1 , ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont() ,
1282 PointF( x
, y
) , StringFormat::GenericTypographic() , ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusBrush() );
1285 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1286 wxDouble
*descent
, wxDouble
*externalLeading
) const
1288 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1290 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1291 FontFamily ffamily
;
1292 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1294 f
->GetFamily(&ffamily
) ;
1296 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1298 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1299 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1300 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1301 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1302 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1303 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1306 *height
= rHeight
* factorY
;
1308 *descent
= rDescent
* factorY
;
1309 if ( externalLeading
)
1310 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1311 // measuring empty strings is not guaranteed, so do it by hand
1319 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1320 StringFormat
strFormat( StringFormat::GenericTypographic() );
1321 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1324 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1326 *width
= bounds
.Width
;
1330 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1333 widths
.Add(0, text
.length());
1335 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1340 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1341 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1342 size_t len
= wcslen( ws
) ;
1343 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1345 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1346 StringFormat
strFormat( StringFormat::GenericTypographic() );
1348 CharacterRange
* ranges
= new CharacterRange
[len
] ;
1349 Region
* regions
= new Region
[len
];
1350 for( size_t i
= 0 ; i
< len
; ++i
)
1352 ranges
[i
].First
= i
;
1353 ranges
[i
].Length
= 1 ;
1355 strFormat
.SetMeasurableCharacterRanges(len
,ranges
);
1356 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1357 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,1,regions
) ;
1360 for ( size_t i
= 0 ; i
< len
; ++i
)
1362 regions
[i
].GetBounds(&bbox
,m_context
);
1363 widths
[i
] = bbox
.GetRight()-bbox
.GetLeft();
1367 bool wxGDIPlusContext::ShouldOffset() const
1370 if ( !m_pen
.IsNull() )
1372 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1373 if ( penwidth
== 0 )
1376 return ( penwidth
% 2 ) == 1;
1379 void* wxGDIPlusContext::GetNativeContext()
1384 // concatenates this transform with the current transform of this context
1385 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1387 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1390 // sets the transform of this context
1391 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1393 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1396 // gets the matrix of this context
1397 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1399 wxGraphicsMatrix matrix
= CreateMatrix();
1400 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1404 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1409 //-----------------------------------------------------------------------------
1410 // wxGDIPlusRenderer declaration
1411 //-----------------------------------------------------------------------------
1413 class wxGDIPlusRenderer
: public wxGraphicsRenderer
1422 virtual ~wxGDIPlusRenderer()
1424 if ( m_loaded
== 1 )
1432 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1434 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1436 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1438 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1440 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1442 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1444 virtual wxGraphicsContext
* CreateMeasuringContext();
1448 virtual wxGraphicsPath
CreatePath();
1452 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1453 wxDouble tx
=0.0, wxDouble ty
=0.0);
1456 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1458 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1460 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1461 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1462 const wxColour
&c1
, const wxColour
&c2
) ;
1464 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1465 // with radius r and color cColor
1466 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1467 const wxColour
&oColor
, const wxColour
&cColor
) ;
1470 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1472 // create a native bitmap representation
1473 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1475 // create a subimage from a native image representation
1476 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1479 bool EnsureIsLoaded();
1482 friend class wxGDIPlusRendererModule
;
1486 ULONG_PTR m_gditoken
;
1488 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1491 //-----------------------------------------------------------------------------
1492 // wxGDIPlusRenderer implementation
1493 //-----------------------------------------------------------------------------
1495 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1497 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1499 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1501 return &gs_GDIPlusRenderer
;
1504 bool wxGDIPlusRenderer::EnsureIsLoaded()
1506 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1507 // if we already tried and failed (we don't want to spend lot of time
1508 // returning NULL from wxGraphicsContext::Create(), which may be called
1509 // relatively frequently):
1510 if ( m_loaded
== -1 )
1515 return m_loaded
== 1;
1518 // call EnsureIsLoaded() and return returnOnFail value if it fails
1519 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1520 if ( !EnsureIsLoaded() ) \
1521 return (returnOnFail)
1524 void wxGDIPlusRenderer::Load()
1526 GdiplusStartupInput input
;
1527 GdiplusStartupOutput output
;
1528 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1530 wxLogTrace("gdiplus", "successfully initialized GDI+");
1535 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1540 void wxGDIPlusRenderer::Unload()
1544 GdiplusShutdown(m_gditoken
);
1547 m_loaded
= -1; // next Load() will try again
1550 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1552 ENSURE_LOADED_OR_RETURN(NULL
);
1553 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1554 wxSize sz
= dc
.GetSize();
1555 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1558 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1560 ENSURE_LOADED_OR_RETURN(NULL
);
1561 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1562 wxSize sz
= dc
.GetSize();
1563 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1566 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1568 ENSURE_LOADED_OR_RETURN(NULL
);
1569 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1570 wxSize sz
= dc
.GetSize();
1571 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1574 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1576 ENSURE_LOADED_OR_RETURN(NULL
);
1577 return new wxGDIPlusMeasuringContext(this);
1580 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1582 ENSURE_LOADED_OR_RETURN(NULL
);
1583 return new wxGDIPlusContext(this,(Graphics
*) context
);
1587 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1589 ENSURE_LOADED_OR_RETURN(NULL
);
1590 return new wxGDIPlusContext(this,(HWND
) window
);
1593 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1595 ENSURE_LOADED_OR_RETURN(NULL
);
1596 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1601 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1603 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1605 m
.SetRefData( new wxGDIPlusPathData(this));
1612 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1613 wxDouble tx
, wxDouble ty
)
1616 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1618 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1619 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1624 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1626 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1627 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1628 return wxNullGraphicsPen
;
1632 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1637 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1639 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1640 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1641 return wxNullGraphicsBrush
;
1645 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1650 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1651 wxGraphicsBrush
wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1652 const wxColour
&c1
, const wxColour
&c2
)
1654 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1656 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1657 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1662 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1663 // with radius r and color cColor
1664 wxGraphicsBrush
wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1665 const wxColour
&oColor
, const wxColour
&cColor
)
1667 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1669 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1670 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1676 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1678 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
1682 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1686 return wxNullGraphicsFont
;
1689 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
1691 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1695 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
1699 return wxNullGraphicsBitmap
;
1702 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1704 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1705 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
1709 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
1713 return wxNullGraphicsBitmap
;
1716 // Shutdown GDI+ at app exit, before possible dll unload
1717 class wxGDIPlusRendererModule
: public wxModule
1720 virtual bool OnInit() { return true; }
1721 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
1724 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
1727 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
1729 #endif // wxUSE_GRAPHICS_CONTEXT