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"
43 #include "wx/dcgraph.h"
45 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
47 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
53 WX_DECLARE_STACK(GraphicsState
, GraphicsStates
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 const double RAD2DEG
= 180.0 / M_PI
;
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
69 inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
71 inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
72 inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
74 // translate a wxColour to a Color
75 inline Color
wxColourToColor(const wxColour
& col
)
77 return Color(col
.Alpha(), col
.Red(), col
.Green(), col
.Blue());
80 } // anonymous namespace
82 //-----------------------------------------------------------------------------
83 // device context implementation
85 // more and more of the dc functionality should be implemented by calling
86 // the appropricate wxGDIPlusContext, but we will have to do that step by step
87 // also coordinate conversions should be moved to native matrix ops
88 //-----------------------------------------------------------------------------
90 // we always stock two context states, one at entry, to be able to preserve the
91 // state we were called with, the other one after changing to HI Graphics orientation
92 // (this one is used for getting back clippings etc)
94 //-----------------------------------------------------------------------------
95 // wxGraphicsPath implementation
96 //-----------------------------------------------------------------------------
98 class wxGDIPlusPathData
: public wxGraphicsPathData
101 wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
102 ~wxGDIPlusPathData();
104 virtual wxGraphicsObjectRefData
*Clone() const;
107 // These are the path primitives from which everything else can be constructed
110 // begins a new subpath at (x,y)
111 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
113 // adds a straight line from the current point to (x,y)
114 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
116 // adds a cubic Bezier curve from the current point, using two control points and an end point
117 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
120 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
121 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
123 // gets the last point of the current path, (0,0) if not yet set
124 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
127 virtual void AddPath( const wxGraphicsPathData
* path
);
129 // closes the current sub-path
130 virtual void CloseSubpath();
133 // These are convenience functions which - if not available natively will be assembled
134 // using the primitives from above
137 // appends a rectangle as a new closed subpath
138 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
141 // appends an ellipsis as a new closed subpath fitting the passed rectangle
142 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
144 // 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)
145 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
148 // returns the native path
149 virtual void * GetNativePath() const { return m_path
; }
151 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
152 virtual void UnGetNativePath(void * WXUNUSED(path
)) const {}
154 // transforms each point of this path by the matrix
155 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
157 // gets the bounding box enclosing all points (possibly including control points)
158 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
160 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
163 GraphicsPath
* m_path
;
166 class wxGDIPlusMatrixData
: public wxGraphicsMatrixData
169 wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
170 virtual ~wxGDIPlusMatrixData() ;
172 virtual wxGraphicsObjectRefData
* Clone() const ;
174 // concatenates the matrix
175 virtual void Concat( const wxGraphicsMatrixData
*t
);
177 // sets the matrix to the respective values
178 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
179 wxDouble tx
=0.0, wxDouble ty
=0.0);
181 // gets the component valuess of the matrix
182 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
183 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
185 // makes this the inverse matrix
186 virtual void Invert();
188 // returns true if the elements of the transformation matrix are equal ?
189 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
191 // return true if this is the identity matrix
192 virtual bool IsIdentity() const;
198 // add the translation to this matrix
199 virtual void Translate( wxDouble dx
, wxDouble dy
);
201 // add the scale to this matrix
202 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
204 // add the rotation to this matrix (radians)
205 virtual void Rotate( wxDouble angle
);
208 // apply the transforms
211 // applies that matrix to the point
212 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
214 // applies the matrix except for translations
215 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
217 // returns the native representation
218 virtual void * GetNativeMatrix() const;
223 class wxGDIPlusPenData
: public wxGraphicsObjectRefData
226 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
231 virtual wxDouble
GetWidth() { return m_width
; }
232 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
242 class wxGDIPlusBrushData
: public wxGraphicsObjectRefData
245 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
246 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
247 ~wxGDIPlusBrushData ();
249 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
250 wxDouble x2
, wxDouble y2
,
251 const wxGraphicsGradientStops
& stops
);
252 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
253 wxDouble xc
, wxDouble yc
,
255 const wxGraphicsGradientStops
& stops
);
257 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
263 // common part of Create{Linear,Radial}GradientBrush()
264 template <typename T
>
265 void SetGradientStops(T
*brush
, const wxGraphicsGradientStops
& stops
);
269 GraphicsPath
* m_brushPath
;
272 class WXDLLIMPEXP_CORE wxGDIPlusBitmapData
: public wxGraphicsObjectRefData
275 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
);
276 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
&bmp
);
277 ~wxGDIPlusBitmapData ();
279 virtual Bitmap
* GetGDIPlusBitmap() { return m_bitmap
; }
286 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
289 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
290 ~wxGDIPlusFontData();
292 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
293 virtual Font
* GetGDIPlusFont() { return m_font
; }
299 class wxGDIPlusContext
: public wxGraphicsContext
302 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
303 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
304 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
307 virtual ~wxGDIPlusContext();
309 virtual void Clip( const wxRegion
®ion
);
310 // clips drawings to the rect
311 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
313 // resets the clipping to original extent
314 virtual void ResetClip();
316 virtual void * GetNativeContext();
318 virtual void StrokePath( const wxGraphicsPath
& p
);
319 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
321 // stroke lines connecting each of the points
322 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
325 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
327 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
329 virtual bool SetCompositionMode(wxCompositionMode op
);
331 virtual void BeginLayer(wxDouble opacity
);
333 virtual void EndLayer();
335 virtual void Translate( wxDouble dx
, wxDouble dy
);
336 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
337 virtual void Rotate( wxDouble angle
);
339 // concatenates this transform with the current transform of this context
340 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
342 // sets the transform of this context
343 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
345 // gets the matrix of this context
346 virtual wxGraphicsMatrix
GetTransform() const;
348 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
349 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
350 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
351 virtual void PushState();
352 virtual void PopState();
354 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
355 wxDouble
*descent
, wxDouble
*externalLeading
) const;
356 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
357 virtual bool ShouldOffset() const;
358 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
364 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
365 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
366 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
367 const wxGraphicsBrush
& backgroundBrush
);
370 GraphicsStates m_stateStack
;
371 GraphicsState m_state1
;
372 GraphicsState m_state2
;
377 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
380 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
383 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
386 wxGDIPlusMeasuringContext()
390 virtual ~wxGDIPlusMeasuringContext()
392 ReleaseDC( NULL
, m_hdc
);
397 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
400 //-----------------------------------------------------------------------------
401 // wxGDIPlusPen implementation
402 //-----------------------------------------------------------------------------
404 wxGDIPlusPenData::~wxGDIPlusPenData()
411 void wxGDIPlusPenData::Init()
418 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
419 : wxGraphicsObjectRefData(renderer
)
422 m_width
= pen
.GetWidth();
426 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
429 switch ( pen
.GetCap() )
435 case wxCAP_PROJECTING
:
440 cap
= LineCapFlat
; // TODO verify
447 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
450 switch ( pen
.GetJoin() )
453 join
= LineJoinBevel
;
457 join
= LineJoinMiter
;
461 join
= LineJoinRound
;
465 join
= LineJoinMiter
;
469 m_pen
->SetLineJoin(join
);
471 m_pen
->SetDashStyle(DashStyleSolid
);
473 DashStyle dashStyle
= DashStyleSolid
;
474 switch ( pen
.GetStyle() )
480 dashStyle
= DashStyleDot
;
484 dashStyle
= DashStyleDash
; // TODO verify
488 dashStyle
= DashStyleDash
;
492 dashStyle
= DashStyleDashDot
;
496 dashStyle
= DashStyleCustom
;
498 int count
= pen
.GetDashes( &dashes
);
499 if ((dashes
!= NULL
) && (count
> 0))
501 REAL
*userLengths
= new REAL
[count
];
502 for ( int i
= 0; i
< count
; ++i
)
504 userLengths
[i
] = dashes
[i
];
506 m_pen
->SetDashPattern( userLengths
, count
);
507 delete[] userLengths
;
513 wxBitmap
* bmp
= pen
.GetStipple();
514 if ( bmp
&& bmp
->Ok() )
516 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
517 m_penBrush
= new TextureBrush(m_penImage
);
518 m_pen
->SetBrush( m_penBrush
);
524 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
526 HatchStyle style
= HatchStyleHorizontal
;
527 switch( pen
.GetStyle() )
529 case wxBDIAGONAL_HATCH
:
530 style
= HatchStyleBackwardDiagonal
;
532 case wxCROSSDIAG_HATCH
:
533 style
= HatchStyleDiagonalCross
;
535 case wxFDIAGONAL_HATCH
:
536 style
= HatchStyleForwardDiagonal
;
539 style
= HatchStyleCross
;
541 case wxHORIZONTAL_HATCH
:
542 style
= HatchStyleHorizontal
;
544 case wxVERTICAL_HATCH
:
545 style
= HatchStyleVertical
;
549 m_penBrush
= new HatchBrush
552 wxColourToColor(pen
.GetColour()),
555 m_pen
->SetBrush( m_penBrush
);
559 if ( dashStyle
!= DashStyleSolid
)
560 m_pen
->SetDashStyle(dashStyle
);
563 //-----------------------------------------------------------------------------
564 // wxGDIPlusBrush implementation
565 //-----------------------------------------------------------------------------
567 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
568 : wxGraphicsObjectRefData(renderer
)
573 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
574 : wxGraphicsObjectRefData(renderer
)
577 if ( brush
.GetStyle() == wxSOLID
)
579 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
581 else if ( brush
.IsHatch() )
583 HatchStyle style
= HatchStyleHorizontal
;
584 switch( brush
.GetStyle() )
586 case wxBDIAGONAL_HATCH
:
587 style
= HatchStyleBackwardDiagonal
;
589 case wxCROSSDIAG_HATCH
:
590 style
= HatchStyleDiagonalCross
;
592 case wxFDIAGONAL_HATCH
:
593 style
= HatchStyleForwardDiagonal
;
596 style
= HatchStyleCross
;
598 case wxHORIZONTAL_HATCH
:
599 style
= HatchStyleHorizontal
;
601 case wxVERTICAL_HATCH
:
602 style
= HatchStyleVertical
;
606 m_brush
= new HatchBrush
609 wxColourToColor(brush
.GetColour()),
615 wxBitmap
* bmp
= brush
.GetStipple();
616 if ( bmp
&& bmp
->Ok() )
618 wxDELETE( m_brushImage
);
619 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
620 m_brush
= new TextureBrush(m_brushImage
);
625 wxGDIPlusBrushData::~wxGDIPlusBrushData()
632 void wxGDIPlusBrushData::Init()
639 template <typename T
>
641 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
642 const wxGraphicsGradientStops
& stops
)
644 const unsigned numStops
= stops
.GetCount();
647 // initial and final colours are set during the brush creation, nothing
652 wxVector
<Color
> colors(numStops
);
653 wxVector
<REAL
> positions(numStops
);
655 for ( unsigned i
= 0; i
< numStops
; i
++ )
657 wxGraphicsGradientStop stop
= stops
.Item(i
);
659 colors
[i
] = wxColourToColor(stop
.GetColour());
660 positions
[i
] = stop
.GetPosition();
663 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
667 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
668 wxDouble x2
, wxDouble y2
,
669 const wxGraphicsGradientStops
& stops
)
671 LinearGradientBrush
* const
672 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
673 wxColourToColor(stops
.GetStartColour()),
674 wxColourToColor(stops
.GetEndColour()));
677 SetGradientStops(brush
, stops
);
681 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
682 wxDouble xc
, wxDouble yc
,
684 const wxGraphicsGradientStops
& stops
)
686 m_brushPath
= new GraphicsPath();
687 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
688 (REAL
)(2*radius
), (REAL
)(2*radius
));
690 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
692 brush
->SetCenterPoint(PointF(xo
, yo
));
693 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
695 const Color
col(wxColourToColor(stops
.GetEndColour()));
697 brush
->SetSurroundColors(&col
, &count
);
699 SetGradientStops(brush
, stops
);
702 //-----------------------------------------------------------------------------
703 // wxGDIPlusFont implementation
704 //-----------------------------------------------------------------------------
706 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
707 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
712 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
713 int size
= font
.GetPointSize();
714 int style
= FontStyleRegular
;
715 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
716 style
|= FontStyleItalic
;
717 if ( font
.GetUnderlined() )
718 style
|= FontStyleUnderline
;
719 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
720 style
|= FontStyleBold
;
721 m_font
= new Font( s
, size
, style
);
722 m_textBrush
= new SolidBrush(wxColourToColor(col
));
725 wxGDIPlusFontData::~wxGDIPlusFontData()
731 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
732 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
733 // bytes as parameter, since there is no real copying of the data going in, only references are stored
734 // m_helper has to be kept alive as well
736 //-----------------------------------------------------------------------------
737 // wxGDIPlusBitmapData implementation
738 //-----------------------------------------------------------------------------
740 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
741 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
746 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
747 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
752 Bitmap
* image
= NULL
;
755 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
757 size_t width
= interim
.GetWidth();
758 size_t height
= interim
.GetHeight();
759 Rect
bounds(0,0,width
,height
);
761 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
763 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
764 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
766 BitmapData dataMask
;
767 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
768 interimMask
.GetPixelFormat(),&dataMask
);
771 BitmapData imageData
;
772 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
774 BYTE maskPattern
= 0 ;
778 for ( size_t y
= 0 ; y
< height
; ++y
)
781 for( size_t x
= 0 ; x
< width
; ++x
)
786 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
790 maskPattern
= maskPattern
>> 1;
792 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
793 if ( (maskByte
& maskPattern
) == 0 )
798 interim
.GetPixel(x
,y
,&c
) ;
799 *dest
= (c
.GetValue() | Color::AlphaMask
);
804 image
->UnlockBits(&imageData
);
806 interimMask
.UnlockBits(&dataMask
);
807 interim
.UnlockBits(&dataMask
);
811 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
812 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
814 size_t width
= image
->GetWidth();
815 size_t height
= image
->GetHeight();
816 Rect
bounds(0,0,width
,height
);
817 static BitmapData data
;
821 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
822 m_helper
->GetPixelFormat(),&data
);
824 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
825 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
827 m_helper
->UnlockBits(&data
);
834 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
840 //-----------------------------------------------------------------------------
841 // wxGDIPlusPath implementation
842 //-----------------------------------------------------------------------------
844 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
849 m_path
= new GraphicsPath();
852 wxGDIPlusPathData::~wxGDIPlusPathData()
857 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
859 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
866 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
868 m_path
->StartFigure();
869 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
872 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
874 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
877 void wxGDIPlusPathData::CloseSubpath()
879 m_path
->CloseFigure();
882 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
888 m_path
->GetLastPoint(&start
);
889 m_path
->AddBezier(start
,c1
,c2
,end
);
892 // gets the last point of the current path, (0,0) if not yet set
893 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
896 m_path
->GetLastPoint(&start
);
901 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
903 double sweepAngle
= endAngle
- startAngle
;
904 if( fabs(sweepAngle
) >= 2*M_PI
)
906 sweepAngle
= 2 * M_PI
;
913 sweepAngle
+= 2 * M_PI
;
918 sweepAngle
-= 2 * M_PI
;
922 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
925 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
927 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
930 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
932 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
936 // transforms each point of this path by the matrix
937 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
939 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
942 // gets the bounding box enclosing all points (possibly including control points)
943 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
946 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
953 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
955 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
956 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
959 //-----------------------------------------------------------------------------
960 // wxGDIPlusMatrixData implementation
961 //-----------------------------------------------------------------------------
963 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
964 : wxGraphicsMatrixData(renderer
)
969 m_matrix
= new Matrix();
972 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
977 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
979 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
982 // concatenates the matrix
983 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
985 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
988 // sets the matrix to the respective values
989 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
990 wxDouble tx
, wxDouble ty
)
992 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
995 // gets the component valuess of the matrix
996 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
997 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1000 m_matrix
->GetElements(elements
);
1001 if (a
) *a
= elements
[0];
1002 if (b
) *b
= elements
[1];
1003 if (c
) *c
= elements
[2];
1004 if (d
) *d
= elements
[3];
1005 if (tx
) *tx
= elements
[4];
1006 if (ty
) *ty
= elements
[5];
1009 // makes this the inverse matrix
1010 void wxGDIPlusMatrixData::Invert()
1015 // returns true if the elements of the transformation matrix are equal ?
1016 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1018 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1021 // return true if this is the identity matrix
1022 bool wxGDIPlusMatrixData::IsIdentity() const
1024 return m_matrix
->IsIdentity() == TRUE
;
1031 // add the translation to this matrix
1032 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1034 m_matrix
->Translate(dx
,dy
);
1037 // add the scale to this matrix
1038 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1040 m_matrix
->Scale(xScale
,yScale
);
1043 // add the rotation to this matrix (radians)
1044 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1046 m_matrix
->Rotate( angle
);
1050 // apply the transforms
1053 // applies that matrix to the point
1054 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1057 m_matrix
->TransformPoints(&pt
);
1062 // applies the matrix except for translations
1063 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1066 m_matrix
->TransformVectors(&pt
);
1071 // returns the native representation
1072 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1077 //-----------------------------------------------------------------------------
1078 // wxGDIPlusContext implementation
1079 //-----------------------------------------------------------------------------
1081 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1082 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1084 class wxGDIPlusOffsetHelper
1087 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1092 m_gr
->TranslateTransform( 0.5, 0.5 );
1094 ~wxGDIPlusOffsetHelper( )
1097 m_gr
->TranslateTransform( -0.5, -0.5 );
1104 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1105 : wxGraphicsContext(renderer
)
1108 m_context
= new Graphics( hdc
);
1114 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1115 : wxGraphicsContext(renderer
)
1118 m_context
= new Graphics( hwnd
);
1119 RECT rect
= wxGetWindowRect(hwnd
);
1120 m_width
= rect
.right
- rect
.left
;
1121 m_height
= rect
.bottom
- rect
.top
;
1125 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1126 : wxGraphicsContext(renderer
)
1133 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1138 void wxGDIPlusContext::Init()
1147 void wxGDIPlusContext::SetDefaults()
1149 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1150 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1151 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1152 m_state1
= m_context
->Save();
1153 m_state2
= m_context
->Save();
1156 wxGDIPlusContext::~wxGDIPlusContext()
1160 m_context
->Restore( m_state2
);
1161 m_context
->Restore( m_state1
);
1167 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1169 Region
rgn((HRGN
)region
.GetHRGN());
1170 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1173 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1175 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1178 void wxGDIPlusContext::ResetClip()
1180 m_context
->ResetClip();
1183 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1185 if (m_composition
== wxCOMPOSITION_DEST
)
1188 if ( !m_pen
.IsNull() )
1190 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1191 Point
*cpoints
= new Point
[n
];
1192 for (size_t i
= 0; i
< n
; i
++)
1194 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1195 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1197 } // for (size_t i = 0; i < n; i++)
1198 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1203 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1205 if (m_composition
== wxCOMPOSITION_DEST
)
1208 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1209 Point
*cpoints
= new Point
[n
];
1210 for (size_t i
= 0; i
< n
; i
++)
1212 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1213 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1215 } // for (int i = 0; i < n; i++)
1216 if ( !m_brush
.IsNull() )
1217 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1218 if ( !m_pen
.IsNull() )
1219 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1223 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1225 if (m_composition
== wxCOMPOSITION_DEST
)
1228 if ( !m_pen
.IsNull() )
1230 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1231 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1235 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1237 if (m_composition
== wxCOMPOSITION_DEST
)
1240 if ( !m_brush
.IsNull() )
1242 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1243 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1244 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1245 (GraphicsPath
*) path
.GetNativePath());
1249 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1251 if (m_antialias
== antialias
)
1254 m_antialias
= antialias
;
1256 SmoothingMode antialiasMode
;
1259 case wxANTIALIAS_DEFAULT
:
1260 antialiasMode
= SmoothingModeHighQuality
;
1262 case wxANTIALIAS_NONE
:
1263 antialiasMode
= SmoothingModeNone
;
1268 m_context
->SetSmoothingMode(antialiasMode
);
1272 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1274 if ( m_composition
== op
)
1279 if (m_composition
== wxCOMPOSITION_DEST
)
1282 CompositingMode cop
;
1285 case wxCOMPOSITION_SOURCE
:
1286 cop
= CompositingModeSourceCopy
;
1288 case wxCOMPOSITION_OVER
:
1289 cop
= CompositingModeSourceOver
;
1295 m_context
->SetCompositingMode(cop
);
1299 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1304 void wxGDIPlusContext::EndLayer()
1309 void wxGDIPlusContext::Rotate( wxDouble angle
)
1311 m_context
->RotateTransform( RadToDeg(angle
) );
1314 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1316 m_context
->TranslateTransform( dx
, dy
);
1319 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1321 m_context
->ScaleTransform(xScale
,yScale
);
1324 void wxGDIPlusContext::PushState()
1326 GraphicsState state
= m_context
->Save();
1327 m_stateStack
.push(state
);
1330 void wxGDIPlusContext::PopState()
1332 GraphicsState state
= m_stateStack
.top();
1334 m_context
->Restore(state
);
1337 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1339 if (m_composition
== wxCOMPOSITION_DEST
)
1342 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1345 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1347 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1348 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1349 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1350 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1353 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1357 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1359 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1360 DrawBitmap(bitmap
, x
, y
, w
, h
);
1363 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1365 if (m_composition
== wxCOMPOSITION_DEST
)
1368 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1369 // find out by looking at the bitmap data whether there really was alpha in it
1370 HICON hIcon
= (HICON
)icon
.GetHICON();
1372 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1373 if (!GetIconInfo(hIcon
,&iconInfo
))
1376 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1378 Bitmap
* image
= NULL
;
1380 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1381 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1382 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1384 image
= Bitmap::FromHICON(hIcon
);
1388 size_t width
= interim
.GetWidth();
1389 size_t height
= interim
.GetHeight();
1390 Rect
bounds(0,0,width
,height
);
1393 interim
.LockBits(&bounds
, ImageLockModeRead
,
1394 interim
.GetPixelFormat(),&data
);
1396 bool hasAlpha
= false;
1397 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1399 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1401 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1402 if ( ( *dest
& Color::AlphaMask
) != 0 )
1409 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1410 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1414 image
= Bitmap::FromHICON(hIcon
);
1417 interim
.UnlockBits(&data
);
1420 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1423 DeleteObject(iconInfo
.hbmColor
);
1424 DeleteObject(iconInfo
.hbmMask
);
1427 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1428 wxDouble x
, wxDouble y
,
1429 const wxGraphicsBrush
& brush
)
1431 if (m_composition
== wxCOMPOSITION_DEST
)
1434 wxCHECK_RET( !m_font
.IsNull(),
1435 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1440 wxGDIPlusFontData
* const
1441 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1442 wxGDIPlusBrushData
* const
1443 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1445 m_context
->DrawString
1447 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1448 -1, // length: string is NUL-terminated
1449 fontData
->GetGDIPlusFont(),
1451 StringFormat::GenericTypographic(),
1452 brushData
? brushData
->GetGDIPlusBrush()
1453 : fontData
->GetGDIPlusBrush()
1457 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1458 wxDouble
*descent
, wxDouble
*externalLeading
) const
1460 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1462 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1463 FontFamily ffamily
;
1464 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1466 f
->GetFamily(&ffamily
) ;
1468 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1470 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1471 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1472 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1473 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1474 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1475 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1478 *height
= rHeight
* factorY
;
1480 *descent
= rDescent
* factorY
;
1481 if ( externalLeading
)
1482 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1483 // measuring empty strings is not guaranteed, so do it by hand
1491 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1492 StringFormat
strFormat( StringFormat::GenericTypographic() );
1493 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1496 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1498 *width
= bounds
.Width
;
1502 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1505 widths
.Add(0, text
.length());
1507 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1512 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1513 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1514 size_t len
= wcslen( ws
) ;
1515 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1517 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1518 StringFormat
strFormat( StringFormat::GenericTypographic() );
1520 size_t startPosition
= 0;
1521 size_t remainder
= len
;
1522 const size_t maxSpan
= 32;
1523 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1524 Region
* regions
= new Region
[maxSpan
];
1526 while( remainder
> 0 )
1528 size_t span
= wxMin( maxSpan
, remainder
);
1530 for( size_t i
= 0 ; i
< span
; ++i
)
1532 ranges
[i
].First
= 0 ;
1533 ranges
[i
].Length
= startPosition
+i
+1 ;
1535 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1536 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1537 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1540 for ( size_t i
= 0 ; i
< span
; ++i
)
1542 regions
[i
].GetBounds(&bbox
,m_context
);
1543 widths
[startPosition
+i
] = bbox
.Width
;
1546 startPosition
+= span
;
1553 bool wxGDIPlusContext::ShouldOffset() const
1556 if ( !m_pen
.IsNull() )
1558 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1559 if ( penwidth
== 0 )
1562 return ( penwidth
% 2 ) == 1;
1565 void* wxGDIPlusContext::GetNativeContext()
1570 // concatenates this transform with the current transform of this context
1571 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1573 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1576 // sets the transform of this context
1577 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1579 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1582 // gets the matrix of this context
1583 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1585 wxGraphicsMatrix matrix
= CreateMatrix();
1586 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1590 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1595 //-----------------------------------------------------------------------------
1596 // wxGDIPlusRenderer declaration
1597 //-----------------------------------------------------------------------------
1599 class wxGDIPlusRenderer
: public wxGraphicsRenderer
1608 virtual ~wxGDIPlusRenderer()
1610 if ( m_loaded
== 1 )
1618 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1620 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1622 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1624 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1626 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1628 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1630 virtual wxGraphicsContext
* CreateMeasuringContext();
1634 virtual wxGraphicsPath
CreatePath();
1638 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1639 wxDouble tx
=0.0, wxDouble ty
=0.0);
1642 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1644 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1646 virtual wxGraphicsBrush
1647 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1648 wxDouble x2
, wxDouble y2
,
1649 const wxGraphicsGradientStops
& stops
);
1651 virtual wxGraphicsBrush
1652 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1653 wxDouble xc
, wxDouble yc
,
1655 const wxGraphicsGradientStops
& stops
);
1657 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1659 // create a native bitmap representation
1660 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1662 // create a graphics bitmap from a native bitmap
1663 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1665 // create a subimage from a native image representation
1666 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1669 bool EnsureIsLoaded();
1672 friend class wxGDIPlusRendererModule
;
1676 ULONG_PTR m_gditoken
;
1678 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1681 //-----------------------------------------------------------------------------
1682 // wxGDIPlusRenderer implementation
1683 //-----------------------------------------------------------------------------
1685 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1687 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1689 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1691 return &gs_GDIPlusRenderer
;
1694 bool wxGDIPlusRenderer::EnsureIsLoaded()
1696 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1697 // if we already tried and failed (we don't want to spend lot of time
1698 // returning NULL from wxGraphicsContext::Create(), which may be called
1699 // relatively frequently):
1700 if ( m_loaded
== -1 )
1705 return m_loaded
== 1;
1708 // call EnsureIsLoaded() and return returnOnFail value if it fails
1709 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1710 if ( !EnsureIsLoaded() ) \
1711 return (returnOnFail)
1714 void wxGDIPlusRenderer::Load()
1716 GdiplusStartupInput input
;
1717 GdiplusStartupOutput output
;
1718 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1720 wxLogTrace("gdiplus", "successfully initialized GDI+");
1725 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1730 void wxGDIPlusRenderer::Unload()
1734 GdiplusShutdown(m_gditoken
);
1737 m_loaded
= -1; // next Load() will try again
1740 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1742 ENSURE_LOADED_OR_RETURN(NULL
);
1743 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1744 wxSize sz
= dc
.GetSize();
1745 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1748 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1750 ENSURE_LOADED_OR_RETURN(NULL
);
1751 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1752 wxSize sz
= dc
.GetSize();
1753 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1756 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1758 ENSURE_LOADED_OR_RETURN(NULL
);
1759 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1760 wxSize sz
= dc
.GetSize();
1761 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1764 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1766 ENSURE_LOADED_OR_RETURN(NULL
);
1767 return new wxGDIPlusMeasuringContext(this);
1770 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1772 ENSURE_LOADED_OR_RETURN(NULL
);
1773 return new wxGDIPlusContext(this,(Graphics
*) context
);
1777 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1779 ENSURE_LOADED_OR_RETURN(NULL
);
1780 return new wxGDIPlusContext(this,(HWND
) window
);
1783 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1785 ENSURE_LOADED_OR_RETURN(NULL
);
1786 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1791 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1793 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1795 m
.SetRefData( new wxGDIPlusPathData(this));
1802 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1803 wxDouble tx
, wxDouble ty
)
1806 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1808 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1809 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1814 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1816 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1817 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1818 return wxNullGraphicsPen
;
1822 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1827 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1829 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1830 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1831 return wxNullGraphicsBrush
;
1835 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1841 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1842 wxDouble x2
, wxDouble y2
,
1843 const wxGraphicsGradientStops
& stops
)
1845 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1847 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1848 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1854 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1855 wxDouble xc
, wxDouble yc
,
1857 const wxGraphicsGradientStops
& stops
)
1859 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1861 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1862 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
1868 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1870 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
1874 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1878 return wxNullGraphicsFont
;
1881 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
1883 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1887 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
1891 return wxNullGraphicsBitmap
;
1894 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
1896 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1897 if ( bitmap
!= NULL
)
1900 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
1904 return wxNullGraphicsBitmap
;
1907 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1909 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1910 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
1914 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
1918 return wxNullGraphicsBitmap
;
1921 // Shutdown GDI+ at app exit, before possible dll unload
1922 class wxGDIPlusRendererModule
: public wxModule
1925 virtual bool OnInit() { return true; }
1926 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
1929 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
1932 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
1934 // ----------------------------------------------------------------------------
1935 // wxMSW-specific parts of wxGCDC
1936 // ----------------------------------------------------------------------------
1938 WXHDC
wxGCDC::AcquireHDC()
1940 wxGraphicsContext
* const gc
= GetGraphicsContext();
1944 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
1945 return g
? g
->GetHDC() : NULL
;
1948 void wxGCDC::ReleaseHDC(WXHDC hdc
)
1953 wxGraphicsContext
* const gc
= GetGraphicsContext();
1954 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
1956 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
1957 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
1959 g
->ReleaseHDC((HDC
)hdc
);
1962 #endif // wxUSE_GRAPHICS_CONTEXT