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
);
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;
337 GraphicsStates m_stateStack
;
338 GraphicsState m_state1
;
339 GraphicsState m_state2
;
341 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
344 class WXDLLIMPEXP_CORE wxGDIPlusMeasuringContext
: public wxGDIPlusContext
347 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
) )
350 wxGDIPlusMeasuringContext()
354 virtual ~wxGDIPlusMeasuringContext()
356 ReleaseDC( NULL
, m_hdc
);
361 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
364 //-----------------------------------------------------------------------------
365 // wxGDIPlusPen implementation
366 //-----------------------------------------------------------------------------
368 wxGDIPlusPenData::~wxGDIPlusPenData()
375 void wxGDIPlusPenData::Init()
382 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
383 : wxGraphicsObjectRefData(renderer
)
386 m_width
= pen
.GetWidth();
390 m_pen
= new Pen(Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
391 pen
.GetColour().Green() , pen
.GetColour().Blue() ), m_width
);
394 switch ( pen
.GetCap() )
400 case wxCAP_PROJECTING
:
405 cap
= LineCapFlat
; // TODO verify
412 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
415 switch ( pen
.GetJoin() )
418 join
= LineJoinBevel
;
422 join
= LineJoinMiter
;
426 join
= LineJoinRound
;
430 join
= LineJoinMiter
;
434 m_pen
->SetLineJoin(join
);
436 m_pen
->SetDashStyle(DashStyleSolid
);
438 DashStyle dashStyle
= DashStyleSolid
;
439 switch ( pen
.GetStyle() )
445 dashStyle
= DashStyleDot
;
449 dashStyle
= DashStyleDash
; // TODO verify
453 dashStyle
= DashStyleDash
;
457 dashStyle
= DashStyleDashDot
;
461 dashStyle
= DashStyleCustom
;
463 int count
= pen
.GetDashes( &dashes
);
464 if ((dashes
!= NULL
) && (count
> 0))
466 REAL
*userLengths
= new REAL
[count
];
467 for ( int i
= 0; i
< count
; ++i
)
469 userLengths
[i
] = dashes
[i
];
471 m_pen
->SetDashPattern( userLengths
, count
);
472 delete[] userLengths
;
478 wxBitmap
* bmp
= pen
.GetStipple();
479 if ( bmp
&& bmp
->Ok() )
481 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
482 m_penBrush
= new TextureBrush(m_penImage
);
483 m_pen
->SetBrush( m_penBrush
);
489 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
491 HatchStyle style
= HatchStyleHorizontal
;
492 switch( pen
.GetStyle() )
494 case wxBDIAGONAL_HATCH
:
495 style
= HatchStyleBackwardDiagonal
;
497 case wxCROSSDIAG_HATCH
:
498 style
= HatchStyleDiagonalCross
;
500 case wxFDIAGONAL_HATCH
:
501 style
= HatchStyleForwardDiagonal
;
504 style
= HatchStyleCross
;
506 case wxHORIZONTAL_HATCH
:
507 style
= HatchStyleHorizontal
;
509 case wxVERTICAL_HATCH
:
510 style
= HatchStyleVertical
;
514 m_penBrush
= new HatchBrush(style
,Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
515 pen
.GetColour().Green() , pen
.GetColour().Blue() ), Color::Transparent
);
516 m_pen
->SetBrush( m_penBrush
);
520 if ( dashStyle
!= DashStyleSolid
)
521 m_pen
->SetDashStyle(dashStyle
);
524 //-----------------------------------------------------------------------------
525 // wxGDIPlusBrush implementation
526 //-----------------------------------------------------------------------------
528 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
529 : wxGraphicsObjectRefData(renderer
)
534 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
535 : wxGraphicsObjectRefData(renderer
)
538 if ( brush
.GetStyle() == wxSOLID
)
540 m_brush
= new SolidBrush( Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
541 brush
.GetColour().Green() , brush
.GetColour().Blue() ) );
543 else if ( brush
.IsHatch() )
545 HatchStyle style
= HatchStyleHorizontal
;
546 switch( brush
.GetStyle() )
548 case wxBDIAGONAL_HATCH
:
549 style
= HatchStyleBackwardDiagonal
;
551 case wxCROSSDIAG_HATCH
:
552 style
= HatchStyleDiagonalCross
;
554 case wxFDIAGONAL_HATCH
:
555 style
= HatchStyleForwardDiagonal
;
558 style
= HatchStyleCross
;
560 case wxHORIZONTAL_HATCH
:
561 style
= HatchStyleHorizontal
;
563 case wxVERTICAL_HATCH
:
564 style
= HatchStyleVertical
;
568 m_brush
= new HatchBrush(style
,Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
569 brush
.GetColour().Green() , brush
.GetColour().Blue() ), Color::Transparent
);
573 wxBitmap
* bmp
= brush
.GetStipple();
574 if ( bmp
&& bmp
->Ok() )
576 wxDELETE( m_brushImage
);
577 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
578 m_brush
= new TextureBrush(m_brushImage
);
583 wxGDIPlusBrushData::~wxGDIPlusBrushData()
590 void wxGDIPlusBrushData::Init()
597 void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
599 m_brush
= new LinearGradientBrush( PointF( x1
,y1
) , PointF( x2
,y2
),
600 Color( c1
.Alpha(), c1
.Red(),c1
.Green() , c1
.Blue() ),
601 Color( c2
.Alpha(), c2
.Red(),c2
.Green() , c2
.Blue() ));
604 void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
605 const wxColour
&oColor
, const wxColour
&cColor
)
607 // Create a path that consists of a single circle.
608 m_brushPath
= new GraphicsPath();
609 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
), (REAL
)(2*radius
), (REAL
)(2*radius
));
611 PathGradientBrush
*b
= new PathGradientBrush(m_brushPath
);
613 b
->SetCenterPoint( PointF(xo
,yo
));
614 b
->SetCenterColor(Color( oColor
.Alpha(), oColor
.Red(),oColor
.Green() , oColor
.Blue() ));
616 Color colors
[] = {Color( cColor
.Alpha(), cColor
.Red(),cColor
.Green() , cColor
.Blue() )};
618 b
->SetSurroundColors(colors
, &count
);
621 //-----------------------------------------------------------------------------
622 // wxGDIPlusFont implementation
623 //-----------------------------------------------------------------------------
625 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
626 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
631 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
632 int size
= font
.GetPointSize();
633 int style
= FontStyleRegular
;
634 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
635 style
|= FontStyleItalic
;
636 if ( font
.GetUnderlined() )
637 style
|= FontStyleUnderline
;
638 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
639 style
|= FontStyleBold
;
640 m_font
= new Font( s
, size
, style
);
641 m_textBrush
= new SolidBrush( Color( col
.Alpha() , col
.Red() ,
642 col
.Green() , col
.Blue() ));
645 wxGDIPlusFontData::~wxGDIPlusFontData()
651 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
652 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
653 // bytes as parameter, since there is no real copying of the data going in, only references are stored
654 // m_helper has to be kept alive as well
656 //-----------------------------------------------------------------------------
657 // wxGDIPlusBitmapData implementation
658 //-----------------------------------------------------------------------------
660 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
661 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
666 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
667 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
672 Bitmap
* image
= NULL
;
675 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
677 size_t width
= interim
.GetWidth();
678 size_t height
= interim
.GetHeight();
679 Rect
bounds(0,0,width
,height
);
681 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
683 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
684 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
686 BitmapData dataMask
;
687 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
688 interimMask
.GetPixelFormat(),&dataMask
);
691 BitmapData imageData
;
692 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
694 BYTE maskPattern
= 0 ;
698 for ( size_t y
= 0 ; y
< height
; ++y
)
701 for( size_t x
= 0 ; x
< width
; ++x
)
706 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
710 maskPattern
= maskPattern
>> 1;
712 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
713 if ( (maskByte
& maskPattern
) == 0 )
718 interim
.GetPixel(x
,y
,&c
) ;
719 *dest
= (c
.GetValue() | Color::AlphaMask
);
724 image
->UnlockBits(&imageData
);
726 interimMask
.UnlockBits(&dataMask
);
727 interim
.UnlockBits(&dataMask
);
731 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
732 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
734 size_t width
= image
->GetWidth();
735 size_t height
= image
->GetHeight();
736 Rect
bounds(0,0,width
,height
);
737 static BitmapData data
;
741 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
742 m_helper
->GetPixelFormat(),&data
);
744 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
745 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
747 m_helper
->UnlockBits(&data
);
754 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
760 //-----------------------------------------------------------------------------
761 // wxGDIPlusPath implementation
762 //-----------------------------------------------------------------------------
764 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
769 m_path
= new GraphicsPath();
772 wxGDIPlusPathData::~wxGDIPlusPathData()
777 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
779 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
786 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
788 m_path
->StartFigure();
789 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
792 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
794 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
797 void wxGDIPlusPathData::CloseSubpath()
799 m_path
->CloseFigure();
802 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
808 m_path
->GetLastPoint(&start
);
809 m_path
->AddBezier(start
,c1
,c2
,end
);
812 // gets the last point of the current path, (0,0) if not yet set
813 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
816 m_path
->GetLastPoint(&start
);
821 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
823 double sweepAngle
= endAngle
- startAngle
;
824 if( fabs(sweepAngle
) >= 2*M_PI
)
826 sweepAngle
= 2 * M_PI
;
833 sweepAngle
+= 2 * M_PI
;
838 sweepAngle
-= 2 * M_PI
;
842 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
845 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
847 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
850 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
852 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
856 // transforms each point of this path by the matrix
857 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
859 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
862 // gets the bounding box enclosing all points (possibly including control points)
863 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
866 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
873 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
875 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
876 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
879 //-----------------------------------------------------------------------------
880 // wxGDIPlusMatrixData implementation
881 //-----------------------------------------------------------------------------
883 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
884 : wxGraphicsMatrixData(renderer
)
889 m_matrix
= new Matrix();
892 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
897 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
899 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
902 // concatenates the matrix
903 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
905 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
908 // sets the matrix to the respective values
909 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
910 wxDouble tx
, wxDouble ty
)
912 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
915 // gets the component valuess of the matrix
916 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
917 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
920 m_matrix
->GetElements(elements
);
921 if (a
) *a
= elements
[0];
922 if (b
) *b
= elements
[1];
923 if (c
) *c
= elements
[2];
924 if (d
) *d
= elements
[3];
925 if (tx
) *tx
= elements
[4];
926 if (ty
) *ty
= elements
[5];
929 // makes this the inverse matrix
930 void wxGDIPlusMatrixData::Invert()
935 // returns true if the elements of the transformation matrix are equal ?
936 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
938 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
941 // return true if this is the identity matrix
942 bool wxGDIPlusMatrixData::IsIdentity() const
944 return m_matrix
->IsIdentity() == TRUE
;
951 // add the translation to this matrix
952 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
954 m_matrix
->Translate(dx
,dy
);
957 // add the scale to this matrix
958 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
960 m_matrix
->Scale(xScale
,yScale
);
963 // add the rotation to this matrix (radians)
964 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
966 m_matrix
->Rotate( angle
);
970 // apply the transforms
973 // applies that matrix to the point
974 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
977 m_matrix
->TransformPoints(&pt
);
982 // applies the matrix except for translations
983 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
986 m_matrix
->TransformVectors(&pt
);
991 // returns the native representation
992 void * wxGDIPlusMatrixData::GetNativeMatrix() const
997 //-----------------------------------------------------------------------------
998 // wxGDIPlusContext implementation
999 //-----------------------------------------------------------------------------
1001 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1002 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1004 class wxGDIPlusOffsetHelper
1007 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1012 m_gr
->TranslateTransform( 0.5, 0.5 );
1014 ~wxGDIPlusOffsetHelper( )
1017 m_gr
->TranslateTransform( -0.5, -0.5 );
1024 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
)
1025 : wxGraphicsContext(renderer
)
1028 m_context
= new Graphics( hdc
);
1032 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1033 : wxGraphicsContext(renderer
)
1036 m_context
= new Graphics( hwnd
);
1040 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1041 : wxGraphicsContext(renderer
)
1048 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1053 void wxGDIPlusContext::Init()
1060 void wxGDIPlusContext::SetDefaults()
1062 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1063 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1064 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1065 m_state1
= m_context
->Save();
1066 m_state2
= m_context
->Save();
1069 wxGDIPlusContext::~wxGDIPlusContext()
1073 m_context
->Restore( m_state2
);
1074 m_context
->Restore( m_state1
);
1080 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1082 Region
rgn((HRGN
)region
.GetHRGN());
1083 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1086 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1088 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1091 void wxGDIPlusContext::ResetClip()
1093 m_context
->ResetClip();
1096 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1098 if ( !m_pen
.IsNull() )
1100 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1101 Point
*cpoints
= new Point
[n
];
1102 for (size_t i
= 0; i
< n
; i
++)
1104 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1105 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1107 } // for (size_t i = 0; i < n; i++)
1108 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1113 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, int WXUNUSED(fillStyle
) )
1115 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1116 Point
*cpoints
= new Point
[n
];
1117 for (size_t i
= 0; i
< n
; i
++)
1119 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1120 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1122 } // for (int i = 0; i < n; i++)
1123 if ( !m_brush
.IsNull() )
1124 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1125 if ( !m_pen
.IsNull() )
1126 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1130 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1132 if ( !m_pen
.IsNull() )
1134 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1135 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1139 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, int fillStyle
)
1141 if ( !m_brush
.IsNull() )
1143 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1144 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1145 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1146 (GraphicsPath
*) path
.GetNativePath());
1150 void wxGDIPlusContext::Rotate( wxDouble angle
)
1152 m_context
->RotateTransform( RadToDeg(angle
) );
1155 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1157 m_context
->TranslateTransform( dx
, dy
);
1160 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1162 m_context
->ScaleTransform(xScale
,yScale
);
1165 void wxGDIPlusContext::PushState()
1167 GraphicsState state
= m_context
->Save();
1168 m_stateStack
.push(state
);
1171 void wxGDIPlusContext::PopState()
1173 GraphicsState state
= m_stateStack
.top();
1175 m_context
->Restore(state
);
1178 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1180 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1183 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1185 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1186 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1187 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1188 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1191 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1195 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1197 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1198 DrawBitmap(bitmap
, x
, y
, w
, h
);
1201 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1203 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1204 // find out by looking at the bitmap data whether there really was alpha in it
1205 HICON hIcon
= (HICON
)icon
.GetHICON();
1207 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1208 if (!GetIconInfo(hIcon
,&iconInfo
))
1211 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1213 Bitmap
* image
= NULL
;
1215 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1216 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1217 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1219 image
= Bitmap::FromHICON(hIcon
);
1223 size_t width
= interim
.GetWidth();
1224 size_t height
= interim
.GetHeight();
1225 Rect
bounds(0,0,width
,height
);
1228 interim
.LockBits(&bounds
, ImageLockModeRead
,
1229 interim
.GetPixelFormat(),&data
);
1231 bool hasAlpha
= false;
1232 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1234 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1236 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1237 if ( ( *dest
& Color::AlphaMask
) != 0 )
1244 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1245 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1249 image
= Bitmap::FromHICON(hIcon
);
1252 interim
.UnlockBits(&data
);
1255 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1258 DeleteObject(iconInfo
.hbmColor
);
1259 DeleteObject(iconInfo
.hbmMask
);
1262 void wxGDIPlusContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1264 if ( m_font
.IsNull() || str
.IsEmpty())
1267 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1268 m_context
->DrawString( s
, -1 , ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont() ,
1269 PointF( x
, y
) , StringFormat::GenericTypographic() , ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusBrush() );
1272 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1273 wxDouble
*descent
, wxDouble
*externalLeading
) const
1275 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1276 FontFamily ffamily
;
1277 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1279 f
->GetFamily(&ffamily
) ;
1281 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1283 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1284 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1285 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1286 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1287 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1288 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1291 *height
= rHeight
* factorY
;
1293 *descent
= rDescent
* factorY
;
1294 if ( externalLeading
)
1295 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1296 // measuring empty strings is not guaranteed, so do it by hand
1304 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1305 StringFormat
strFormat( StringFormat::GenericTypographic() );
1306 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1309 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1311 *width
= bounds
.Width
;
1315 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1318 widths
.Add(0, text
.length());
1323 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1324 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1325 size_t len
= wcslen( ws
) ;
1326 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1328 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1329 StringFormat
strFormat( StringFormat::GenericTypographic() );
1331 CharacterRange
* ranges
= new CharacterRange
[len
] ;
1332 Region
* regions
= new Region
[len
];
1333 for( size_t i
= 0 ; i
< len
; ++i
)
1335 ranges
[i
].First
= i
;
1336 ranges
[i
].Length
= 1 ;
1338 strFormat
.SetMeasurableCharacterRanges(len
,ranges
);
1339 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1340 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,1,regions
) ;
1343 for ( size_t i
= 0 ; i
< len
; ++i
)
1345 regions
[i
].GetBounds(&bbox
,m_context
);
1346 widths
[i
] = bbox
.GetRight()-bbox
.GetLeft();
1350 bool wxGDIPlusContext::ShouldOffset() const
1353 if ( !m_pen
.IsNull() )
1355 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1356 if ( penwidth
== 0 )
1359 return ( penwidth
% 2 ) == 1;
1362 void* wxGDIPlusContext::GetNativeContext()
1367 // concatenates this transform with the current transform of this context
1368 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1370 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1373 // sets the transform of this context
1374 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1376 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1379 // gets the matrix of this context
1380 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1382 wxGraphicsMatrix matrix
= CreateMatrix();
1383 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1386 //-----------------------------------------------------------------------------
1387 // wxGDIPlusRenderer declaration
1388 //-----------------------------------------------------------------------------
1390 class wxGDIPlusRenderer
: public wxGraphicsRenderer
1399 virtual ~wxGDIPlusRenderer()
1401 if ( m_loaded
== 1 )
1409 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1411 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1413 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1415 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1417 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1419 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1421 virtual wxGraphicsContext
* CreateMeasuringContext();
1425 virtual wxGraphicsPath
CreatePath();
1429 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1430 wxDouble tx
=0.0, wxDouble ty
=0.0);
1433 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1435 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1437 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1438 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1439 const wxColour
&c1
, const wxColour
&c2
) ;
1441 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1442 // with radius r and color cColor
1443 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1444 const wxColour
&oColor
, const wxColour
&cColor
) ;
1447 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1449 // create a native bitmap representation
1450 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1452 // create a subimage from a native image representation
1453 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1456 bool EnsureIsLoaded();
1459 friend class wxGDIPlusRendererModule
;
1463 ULONG_PTR m_gditoken
;
1465 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1468 //-----------------------------------------------------------------------------
1469 // wxGDIPlusRenderer implementation
1470 //-----------------------------------------------------------------------------
1472 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1474 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1476 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1478 return &gs_GDIPlusRenderer
;
1481 bool wxGDIPlusRenderer::EnsureIsLoaded()
1483 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1484 // if we already tried and failed (we don't want to spend lot of time
1485 // returning NULL from wxGraphicsContext::Create(), which may be called
1486 // relatively frequently):
1487 if ( m_loaded
== -1 )
1492 return m_loaded
== 1;
1495 // call EnsureIsLoaded() and return returnOnFail value if it fails
1496 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1497 if ( !EnsureIsLoaded() ) \
1498 return (returnOnFail)
1501 void wxGDIPlusRenderer::Load()
1503 GdiplusStartupInput input
;
1504 GdiplusStartupOutput output
;
1505 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1507 wxLogTrace("gdiplus", "successfully initialized GDI+");
1512 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1517 void wxGDIPlusRenderer::Unload()
1521 GdiplusShutdown(m_gditoken
);
1524 m_loaded
= -1; // next Load() will try again
1527 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1529 ENSURE_LOADED_OR_RETURN(NULL
);
1530 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1531 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC());
1534 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1536 ENSURE_LOADED_OR_RETURN(NULL
);
1537 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1538 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC());
1541 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1543 ENSURE_LOADED_OR_RETURN(NULL
);
1544 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1545 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC());
1548 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1550 ENSURE_LOADED_OR_RETURN(NULL
);
1551 return new wxGDIPlusMeasuringContext(this);
1554 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1556 ENSURE_LOADED_OR_RETURN(NULL
);
1557 return new wxGDIPlusContext(this,(Graphics
*) context
);
1561 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1563 ENSURE_LOADED_OR_RETURN(NULL
);
1564 return new wxGDIPlusContext(this,(HWND
) window
);
1567 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1569 ENSURE_LOADED_OR_RETURN(NULL
);
1570 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1575 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1577 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1579 m
.SetRefData( new wxGDIPlusPathData(this));
1586 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1587 wxDouble tx
, wxDouble ty
)
1590 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1592 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1593 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1598 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1600 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1601 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1602 return wxNullGraphicsPen
;
1606 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1611 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1613 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1614 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1615 return wxNullGraphicsBrush
;
1619 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1624 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1625 wxGraphicsBrush
wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1626 const wxColour
&c1
, const wxColour
&c2
)
1628 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1630 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1631 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1636 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1637 // with radius r and color cColor
1638 wxGraphicsBrush
wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1639 const wxColour
&oColor
, const wxColour
&cColor
)
1641 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1643 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1644 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1650 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1652 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
1656 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1660 return wxNullGraphicsFont
;
1663 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
1665 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1669 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
1673 return wxNullGraphicsBitmap
;
1676 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1678 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1679 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
1683 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
1687 return wxNullGraphicsBitmap
;
1690 // Shutdown GDI+ at app exit, before possible dll unload
1691 class wxGDIPlusRendererModule
: public wxModule
1694 virtual bool OnInit() { return true; }
1695 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
1698 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
1701 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
1703 #endif // wxUSE_GRAPHICS_CONTEXT