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 #if wxUSE_ENH_METAFILE
44 #include "wx/msw/enhmeta.h"
46 #include "wx/dcgraph.h"
48 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
50 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
56 WX_DECLARE_STACK(GraphicsState
, GraphicsStates
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 const double RAD2DEG
= 180.0 / M_PI
;
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
72 inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
74 inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
75 inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
77 // translate a wxColour to a Color
78 inline Color
wxColourToColor(const wxColour
& col
)
80 return Color(col
.Alpha(), col
.Red(), col
.Green(), col
.Blue());
83 } // anonymous namespace
85 //-----------------------------------------------------------------------------
86 // device context implementation
88 // more and more of the dc functionality should be implemented by calling
89 // the appropricate wxGDIPlusContext, but we will have to do that step by step
90 // also coordinate conversions should be moved to native matrix ops
91 //-----------------------------------------------------------------------------
93 // we always stock two context states, one at entry, to be able to preserve the
94 // state we were called with, the other one after changing to HI Graphics orientation
95 // (this one is used for getting back clippings etc)
97 //-----------------------------------------------------------------------------
98 // wxGraphicsPath implementation
99 //-----------------------------------------------------------------------------
101 class wxGDIPlusPathData
: public wxGraphicsPathData
104 wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
105 ~wxGDIPlusPathData();
107 virtual wxGraphicsObjectRefData
*Clone() const;
110 // These are the path primitives from which everything else can be constructed
113 // begins a new subpath at (x,y)
114 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
116 // adds a straight line from the current point to (x,y)
117 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
119 // adds a cubic Bezier curve from the current point, using two control points and an end point
120 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
123 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
124 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
126 // gets the last point of the current path, (0,0) if not yet set
127 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
130 virtual void AddPath( const wxGraphicsPathData
* path
);
132 // closes the current sub-path
133 virtual void CloseSubpath();
136 // These are convenience functions which - if not available natively will be assembled
137 // using the primitives from above
140 // appends a rectangle as a new closed subpath
141 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
144 // appends an ellipsis as a new closed subpath fitting the passed rectangle
145 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
147 // 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)
148 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
151 // returns the native path
152 virtual void * GetNativePath() const { return m_path
; }
154 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
155 virtual void UnGetNativePath(void * WXUNUSED(path
)) const {}
157 // transforms each point of this path by the matrix
158 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
160 // gets the bounding box enclosing all points (possibly including control points)
161 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
163 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
166 GraphicsPath
* m_path
;
169 class wxGDIPlusMatrixData
: public wxGraphicsMatrixData
172 wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
173 virtual ~wxGDIPlusMatrixData() ;
175 virtual wxGraphicsObjectRefData
* Clone() const ;
177 // concatenates the matrix
178 virtual void Concat( const wxGraphicsMatrixData
*t
);
180 // sets the matrix to the respective values
181 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
182 wxDouble tx
=0.0, wxDouble ty
=0.0);
184 // gets the component valuess of the matrix
185 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
186 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
188 // makes this the inverse matrix
189 virtual void Invert();
191 // returns true if the elements of the transformation matrix are equal ?
192 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
194 // return true if this is the identity matrix
195 virtual bool IsIdentity() const;
201 // add the translation to this matrix
202 virtual void Translate( wxDouble dx
, wxDouble dy
);
204 // add the scale to this matrix
205 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
207 // add the rotation to this matrix (radians)
208 virtual void Rotate( wxDouble angle
);
211 // apply the transforms
214 // applies that matrix to the point
215 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
217 // applies the matrix except for translations
218 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
220 // returns the native representation
221 virtual void * GetNativeMatrix() const;
226 class wxGDIPlusPenData
: public wxGraphicsObjectRefData
229 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
234 virtual wxDouble
GetWidth() { return m_width
; }
235 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
245 class wxGDIPlusBrushData
: public wxGraphicsObjectRefData
248 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
249 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
250 ~wxGDIPlusBrushData ();
252 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
253 wxDouble x2
, wxDouble y2
,
254 const wxGraphicsGradientStops
& stops
);
255 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
256 wxDouble xc
, wxDouble yc
,
258 const wxGraphicsGradientStops
& stops
);
260 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
266 // common part of Create{Linear,Radial}GradientBrush()
267 template <typename T
>
268 void SetGradientStops(T
*brush
, const wxGraphicsGradientStops
& stops
);
272 GraphicsPath
* m_brushPath
;
275 class WXDLLIMPEXP_CORE wxGDIPlusBitmapData
: public wxGraphicsObjectRefData
278 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
);
279 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
&bmp
);
280 ~wxGDIPlusBitmapData ();
282 virtual Bitmap
* GetGDIPlusBitmap() { return m_bitmap
; }
289 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
292 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
293 ~wxGDIPlusFontData();
295 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
296 virtual Font
* GetGDIPlusFont() { return m_font
; }
302 class wxGDIPlusContext
: public wxGraphicsContext
305 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
306 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
307 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
310 virtual ~wxGDIPlusContext();
312 virtual void Clip( const wxRegion
®ion
);
313 // clips drawings to the rect
314 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
316 // resets the clipping to original extent
317 virtual void ResetClip();
319 virtual void * GetNativeContext();
321 virtual void StrokePath( const wxGraphicsPath
& p
);
322 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
324 // stroke lines connecting each of the points
325 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
328 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
330 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
332 virtual bool SetCompositionMode(wxCompositionMode op
);
334 virtual void BeginLayer(wxDouble opacity
);
336 virtual void EndLayer();
338 virtual void Translate( wxDouble dx
, wxDouble dy
);
339 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
340 virtual void Rotate( wxDouble angle
);
342 // concatenates this transform with the current transform of this context
343 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
345 // sets the transform of this context
346 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
348 // gets the matrix of this context
349 virtual wxGraphicsMatrix
GetTransform() const;
351 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
352 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
353 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
354 virtual void PushState();
355 virtual void PopState();
357 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
358 wxDouble
*descent
, wxDouble
*externalLeading
) const;
359 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
360 virtual bool ShouldOffset() const;
361 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
367 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
368 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
369 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
370 const wxGraphicsBrush
& backgroundBrush
);
373 GraphicsStates m_stateStack
;
374 GraphicsState m_state1
;
375 GraphicsState m_state2
;
380 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
383 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
386 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
389 wxGDIPlusMeasuringContext()
393 virtual ~wxGDIPlusMeasuringContext()
395 ReleaseDC( NULL
, m_hdc
);
400 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
403 //-----------------------------------------------------------------------------
404 // wxGDIPlusPen implementation
405 //-----------------------------------------------------------------------------
407 wxGDIPlusPenData::~wxGDIPlusPenData()
414 void wxGDIPlusPenData::Init()
421 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
422 : wxGraphicsObjectRefData(renderer
)
425 m_width
= pen
.GetWidth();
429 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
432 switch ( pen
.GetCap() )
438 case wxCAP_PROJECTING
:
443 cap
= LineCapFlat
; // TODO verify
450 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
453 switch ( pen
.GetJoin() )
456 join
= LineJoinBevel
;
460 join
= LineJoinMiter
;
464 join
= LineJoinRound
;
468 join
= LineJoinMiter
;
472 m_pen
->SetLineJoin(join
);
474 m_pen
->SetDashStyle(DashStyleSolid
);
476 DashStyle dashStyle
= DashStyleSolid
;
477 switch ( pen
.GetStyle() )
483 dashStyle
= DashStyleDot
;
487 dashStyle
= DashStyleDash
; // TODO verify
491 dashStyle
= DashStyleDash
;
495 dashStyle
= DashStyleDashDot
;
499 dashStyle
= DashStyleCustom
;
501 int count
= pen
.GetDashes( &dashes
);
502 if ((dashes
!= NULL
) && (count
> 0))
504 REAL
*userLengths
= new REAL
[count
];
505 for ( int i
= 0; i
< count
; ++i
)
507 userLengths
[i
] = dashes
[i
];
509 m_pen
->SetDashPattern( userLengths
, count
);
510 delete[] userLengths
;
516 wxBitmap
* bmp
= pen
.GetStipple();
517 if ( bmp
&& bmp
->Ok() )
519 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
520 m_penBrush
= new TextureBrush(m_penImage
);
521 m_pen
->SetBrush( m_penBrush
);
527 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
529 HatchStyle style
= HatchStyleHorizontal
;
530 switch( pen
.GetStyle() )
532 case wxBDIAGONAL_HATCH
:
533 style
= HatchStyleBackwardDiagonal
;
535 case wxCROSSDIAG_HATCH
:
536 style
= HatchStyleDiagonalCross
;
538 case wxFDIAGONAL_HATCH
:
539 style
= HatchStyleForwardDiagonal
;
542 style
= HatchStyleCross
;
544 case wxHORIZONTAL_HATCH
:
545 style
= HatchStyleHorizontal
;
547 case wxVERTICAL_HATCH
:
548 style
= HatchStyleVertical
;
552 m_penBrush
= new HatchBrush
555 wxColourToColor(pen
.GetColour()),
558 m_pen
->SetBrush( m_penBrush
);
562 if ( dashStyle
!= DashStyleSolid
)
563 m_pen
->SetDashStyle(dashStyle
);
566 //-----------------------------------------------------------------------------
567 // wxGDIPlusBrush implementation
568 //-----------------------------------------------------------------------------
570 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
571 : wxGraphicsObjectRefData(renderer
)
576 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
577 : wxGraphicsObjectRefData(renderer
)
580 if ( brush
.GetStyle() == wxSOLID
)
582 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
584 else if ( brush
.IsHatch() )
586 HatchStyle style
= HatchStyleHorizontal
;
587 switch( brush
.GetStyle() )
589 case wxBDIAGONAL_HATCH
:
590 style
= HatchStyleBackwardDiagonal
;
592 case wxCROSSDIAG_HATCH
:
593 style
= HatchStyleDiagonalCross
;
595 case wxFDIAGONAL_HATCH
:
596 style
= HatchStyleForwardDiagonal
;
599 style
= HatchStyleCross
;
601 case wxHORIZONTAL_HATCH
:
602 style
= HatchStyleHorizontal
;
604 case wxVERTICAL_HATCH
:
605 style
= HatchStyleVertical
;
609 m_brush
= new HatchBrush
612 wxColourToColor(brush
.GetColour()),
618 wxBitmap
* bmp
= brush
.GetStipple();
619 if ( bmp
&& bmp
->Ok() )
621 wxDELETE( m_brushImage
);
622 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
623 m_brush
= new TextureBrush(m_brushImage
);
628 wxGDIPlusBrushData::~wxGDIPlusBrushData()
635 void wxGDIPlusBrushData::Init()
642 template <typename T
>
644 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
645 const wxGraphicsGradientStops
& stops
)
647 const unsigned numStops
= stops
.GetCount();
650 // initial and final colours are set during the brush creation, nothing
655 wxVector
<Color
> colors(numStops
);
656 wxVector
<REAL
> positions(numStops
);
658 for ( unsigned i
= 0; i
< numStops
; i
++ )
660 wxGraphicsGradientStop stop
= stops
.Item(i
);
662 colors
[i
] = wxColourToColor(stop
.GetColour());
663 positions
[i
] = stop
.GetPosition();
666 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
670 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
671 wxDouble x2
, wxDouble y2
,
672 const wxGraphicsGradientStops
& stops
)
674 LinearGradientBrush
* const
675 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
676 wxColourToColor(stops
.GetStartColour()),
677 wxColourToColor(stops
.GetEndColour()));
680 SetGradientStops(brush
, stops
);
684 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
685 wxDouble xc
, wxDouble yc
,
687 const wxGraphicsGradientStops
& stops
)
689 m_brushPath
= new GraphicsPath();
690 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
691 (REAL
)(2*radius
), (REAL
)(2*radius
));
693 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
695 brush
->SetCenterPoint(PointF(xo
, yo
));
696 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
698 const Color
col(wxColourToColor(stops
.GetEndColour()));
700 brush
->SetSurroundColors(&col
, &count
);
702 SetGradientStops(brush
, stops
);
705 //-----------------------------------------------------------------------------
706 // wxGDIPlusFont implementation
707 //-----------------------------------------------------------------------------
709 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
710 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
715 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
716 int size
= font
.GetPointSize();
717 int style
= FontStyleRegular
;
718 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
719 style
|= FontStyleItalic
;
720 if ( font
.GetUnderlined() )
721 style
|= FontStyleUnderline
;
722 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
723 style
|= FontStyleBold
;
724 m_font
= new Font( s
, size
, style
);
725 m_textBrush
= new SolidBrush(wxColourToColor(col
));
728 wxGDIPlusFontData::~wxGDIPlusFontData()
734 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
735 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
736 // bytes as parameter, since there is no real copying of the data going in, only references are stored
737 // m_helper has to be kept alive as well
739 //-----------------------------------------------------------------------------
740 // wxGDIPlusBitmapData implementation
741 //-----------------------------------------------------------------------------
743 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
744 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
749 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
750 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
755 Bitmap
* image
= NULL
;
758 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
760 size_t width
= interim
.GetWidth();
761 size_t height
= interim
.GetHeight();
762 Rect
bounds(0,0,width
,height
);
764 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
766 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
767 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
769 BitmapData dataMask
;
770 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
771 interimMask
.GetPixelFormat(),&dataMask
);
774 BitmapData imageData
;
775 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
777 BYTE maskPattern
= 0 ;
781 for ( size_t y
= 0 ; y
< height
; ++y
)
784 for( size_t x
= 0 ; x
< width
; ++x
)
789 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
793 maskPattern
= maskPattern
>> 1;
795 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
796 if ( (maskByte
& maskPattern
) == 0 )
801 interim
.GetPixel(x
,y
,&c
) ;
802 *dest
= (c
.GetValue() | Color::AlphaMask
);
807 image
->UnlockBits(&imageData
);
809 interimMask
.UnlockBits(&dataMask
);
810 interim
.UnlockBits(&dataMask
);
814 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
815 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
817 size_t width
= image
->GetWidth();
818 size_t height
= image
->GetHeight();
819 Rect
bounds(0,0,width
,height
);
820 static BitmapData data
;
824 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
825 m_helper
->GetPixelFormat(),&data
);
827 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
828 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
830 m_helper
->UnlockBits(&data
);
837 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
843 //-----------------------------------------------------------------------------
844 // wxGDIPlusPath implementation
845 //-----------------------------------------------------------------------------
847 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
852 m_path
= new GraphicsPath();
855 wxGDIPlusPathData::~wxGDIPlusPathData()
860 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
862 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
869 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
871 m_path
->StartFigure();
872 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
875 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
877 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
880 void wxGDIPlusPathData::CloseSubpath()
882 m_path
->CloseFigure();
885 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
891 m_path
->GetLastPoint(&start
);
892 m_path
->AddBezier(start
,c1
,c2
,end
);
895 // gets the last point of the current path, (0,0) if not yet set
896 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
899 m_path
->GetLastPoint(&start
);
904 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
906 double sweepAngle
= endAngle
- startAngle
;
907 if( fabs(sweepAngle
) >= 2*M_PI
)
909 sweepAngle
= 2 * M_PI
;
916 sweepAngle
+= 2 * M_PI
;
921 sweepAngle
-= 2 * M_PI
;
925 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
928 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
930 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
933 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
935 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
939 // transforms each point of this path by the matrix
940 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
942 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
945 // gets the bounding box enclosing all points (possibly including control points)
946 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
949 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
956 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
958 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
959 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
962 //-----------------------------------------------------------------------------
963 // wxGDIPlusMatrixData implementation
964 //-----------------------------------------------------------------------------
966 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
967 : wxGraphicsMatrixData(renderer
)
972 m_matrix
= new Matrix();
975 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
980 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
982 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
985 // concatenates the matrix
986 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
988 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
991 // sets the matrix to the respective values
992 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
993 wxDouble tx
, wxDouble ty
)
995 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
998 // gets the component valuess of the matrix
999 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1000 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1003 m_matrix
->GetElements(elements
);
1004 if (a
) *a
= elements
[0];
1005 if (b
) *b
= elements
[1];
1006 if (c
) *c
= elements
[2];
1007 if (d
) *d
= elements
[3];
1008 if (tx
) *tx
= elements
[4];
1009 if (ty
) *ty
= elements
[5];
1012 // makes this the inverse matrix
1013 void wxGDIPlusMatrixData::Invert()
1018 // returns true if the elements of the transformation matrix are equal ?
1019 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1021 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1024 // return true if this is the identity matrix
1025 bool wxGDIPlusMatrixData::IsIdentity() const
1027 return m_matrix
->IsIdentity() == TRUE
;
1034 // add the translation to this matrix
1035 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1037 m_matrix
->Translate(dx
,dy
);
1040 // add the scale to this matrix
1041 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1043 m_matrix
->Scale(xScale
,yScale
);
1046 // add the rotation to this matrix (radians)
1047 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1049 m_matrix
->Rotate( RadToDeg(angle
) );
1053 // apply the transforms
1056 // applies that matrix to the point
1057 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1060 m_matrix
->TransformPoints(&pt
);
1065 // applies the matrix except for translations
1066 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1069 m_matrix
->TransformVectors(&pt
);
1074 // returns the native representation
1075 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1080 //-----------------------------------------------------------------------------
1081 // wxGDIPlusContext implementation
1082 //-----------------------------------------------------------------------------
1084 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1085 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1087 class wxGDIPlusOffsetHelper
1090 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1095 m_gr
->TranslateTransform( 0.5, 0.5 );
1097 ~wxGDIPlusOffsetHelper( )
1100 m_gr
->TranslateTransform( -0.5, -0.5 );
1107 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1108 : wxGraphicsContext(renderer
)
1111 m_context
= new Graphics( hdc
);
1117 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1118 : wxGraphicsContext(renderer
)
1121 m_context
= new Graphics( hwnd
);
1122 RECT rect
= wxGetWindowRect(hwnd
);
1123 m_width
= rect
.right
- rect
.left
;
1124 m_height
= rect
.bottom
- rect
.top
;
1128 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1129 : wxGraphicsContext(renderer
)
1136 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1141 void wxGDIPlusContext::Init()
1150 void wxGDIPlusContext::SetDefaults()
1152 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1153 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1154 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1155 m_state1
= m_context
->Save();
1156 m_state2
= m_context
->Save();
1158 // Setup page scale, based on DPI ratio.
1159 // Antecedent should be 100dpi when the default page unit (UnitDisplay)
1160 // is used. Page unit UnitDocument would require 300dpi instead.
1161 // Note that calling SetPageScale() does not have effect on non-printing
1162 // DCs (that is, any other than wxPrinterDC or wxEnhMetaFileDC).
1163 REAL dpiRatio
= 100.0 / m_context
->GetDpiY();
1164 m_context
->SetPageScale(dpiRatio
);
1167 wxGDIPlusContext::~wxGDIPlusContext()
1171 m_context
->Restore( m_state2
);
1172 m_context
->Restore( m_state1
);
1178 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1180 Region
rgn((HRGN
)region
.GetHRGN());
1181 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1184 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1186 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1189 void wxGDIPlusContext::ResetClip()
1191 m_context
->ResetClip();
1194 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1196 if (m_composition
== wxCOMPOSITION_DEST
)
1199 if ( !m_pen
.IsNull() )
1201 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1202 Point
*cpoints
= new Point
[n
];
1203 for (size_t i
= 0; i
< n
; i
++)
1205 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1206 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1208 } // for (size_t i = 0; i < n; i++)
1209 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1214 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1216 if (m_composition
== wxCOMPOSITION_DEST
)
1219 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1220 Point
*cpoints
= new Point
[n
];
1221 for (size_t i
= 0; i
< n
; i
++)
1223 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1224 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1226 } // for (int i = 0; i < n; i++)
1227 if ( !m_brush
.IsNull() )
1228 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1229 if ( !m_pen
.IsNull() )
1230 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1234 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1236 if (m_composition
== wxCOMPOSITION_DEST
)
1239 if ( !m_pen
.IsNull() )
1241 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1242 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1246 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1248 if (m_composition
== wxCOMPOSITION_DEST
)
1251 if ( !m_brush
.IsNull() )
1253 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1254 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1255 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1256 (GraphicsPath
*) path
.GetNativePath());
1260 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1262 if (m_antialias
== antialias
)
1265 m_antialias
= antialias
;
1267 SmoothingMode antialiasMode
;
1270 case wxANTIALIAS_DEFAULT
:
1271 antialiasMode
= SmoothingModeHighQuality
;
1273 case wxANTIALIAS_NONE
:
1274 antialiasMode
= SmoothingModeNone
;
1279 m_context
->SetSmoothingMode(antialiasMode
);
1283 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1285 if ( m_composition
== op
)
1290 if (m_composition
== wxCOMPOSITION_DEST
)
1293 CompositingMode cop
;
1296 case wxCOMPOSITION_SOURCE
:
1297 cop
= CompositingModeSourceCopy
;
1299 case wxCOMPOSITION_OVER
:
1300 cop
= CompositingModeSourceOver
;
1306 m_context
->SetCompositingMode(cop
);
1310 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1315 void wxGDIPlusContext::EndLayer()
1320 void wxGDIPlusContext::Rotate( wxDouble angle
)
1322 m_context
->RotateTransform( RadToDeg(angle
) );
1325 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1327 m_context
->TranslateTransform( dx
, dy
);
1330 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1332 m_context
->ScaleTransform(xScale
,yScale
);
1335 void wxGDIPlusContext::PushState()
1337 GraphicsState state
= m_context
->Save();
1338 m_stateStack
.push(state
);
1341 void wxGDIPlusContext::PopState()
1343 GraphicsState state
= m_stateStack
.top();
1345 m_context
->Restore(state
);
1348 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1350 if (m_composition
== wxCOMPOSITION_DEST
)
1353 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1356 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1358 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1359 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1360 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1361 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1364 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1368 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1370 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1371 DrawBitmap(bitmap
, x
, y
, w
, h
);
1374 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1376 if (m_composition
== wxCOMPOSITION_DEST
)
1379 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1380 // find out by looking at the bitmap data whether there really was alpha in it
1381 HICON hIcon
= (HICON
)icon
.GetHICON();
1383 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1384 if (!GetIconInfo(hIcon
,&iconInfo
))
1387 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1389 Bitmap
* image
= NULL
;
1391 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1392 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1393 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1395 image
= Bitmap::FromHICON(hIcon
);
1399 size_t width
= interim
.GetWidth();
1400 size_t height
= interim
.GetHeight();
1401 Rect
bounds(0,0,width
,height
);
1404 interim
.LockBits(&bounds
, ImageLockModeRead
,
1405 interim
.GetPixelFormat(),&data
);
1407 bool hasAlpha
= false;
1408 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1410 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1412 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1413 if ( ( *dest
& Color::AlphaMask
) != 0 )
1420 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1421 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1425 image
= Bitmap::FromHICON(hIcon
);
1428 interim
.UnlockBits(&data
);
1431 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1434 DeleteObject(iconInfo
.hbmColor
);
1435 DeleteObject(iconInfo
.hbmMask
);
1438 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1439 wxDouble x
, wxDouble y
,
1440 const wxGraphicsBrush
& brush
)
1442 if (m_composition
== wxCOMPOSITION_DEST
)
1445 wxCHECK_RET( !m_font
.IsNull(),
1446 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1451 wxGDIPlusFontData
* const
1452 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1453 wxGDIPlusBrushData
* const
1454 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1456 m_context
->DrawString
1458 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1459 -1, // length: string is NUL-terminated
1460 fontData
->GetGDIPlusFont(),
1462 StringFormat::GenericTypographic(),
1463 brushData
? brushData
->GetGDIPlusBrush()
1464 : fontData
->GetGDIPlusBrush()
1468 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1469 wxDouble
*descent
, wxDouble
*externalLeading
) const
1471 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1473 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1474 FontFamily ffamily
;
1475 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1477 f
->GetFamily(&ffamily
) ;
1479 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1481 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1482 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1483 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1484 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1485 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1486 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1489 *height
= rHeight
* factorY
;
1491 *descent
= rDescent
* factorY
;
1492 if ( externalLeading
)
1493 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1494 // measuring empty strings is not guaranteed, so do it by hand
1502 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1503 StringFormat
strFormat( StringFormat::GenericTypographic() );
1504 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1507 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1509 *width
= bounds
.Width
;
1511 *height
= bounds
.Height
;
1515 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1518 widths
.Add(0, text
.length());
1520 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1525 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1526 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1527 size_t len
= wcslen( ws
) ;
1528 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1530 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1531 StringFormat
strFormat( StringFormat::GenericTypographic() );
1533 size_t startPosition
= 0;
1534 size_t remainder
= len
;
1535 const size_t maxSpan
= 32;
1536 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1537 Region
* regions
= new Region
[maxSpan
];
1539 while( remainder
> 0 )
1541 size_t span
= wxMin( maxSpan
, remainder
);
1543 for( size_t i
= 0 ; i
< span
; ++i
)
1545 ranges
[i
].First
= 0 ;
1546 ranges
[i
].Length
= startPosition
+i
+1 ;
1548 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1549 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1550 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1553 for ( size_t i
= 0 ; i
< span
; ++i
)
1555 regions
[i
].GetBounds(&bbox
,m_context
);
1556 widths
[startPosition
+i
] = bbox
.Width
;
1559 startPosition
+= span
;
1566 bool wxGDIPlusContext::ShouldOffset() const
1569 if ( !m_pen
.IsNull() )
1571 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1572 if ( penwidth
== 0 )
1575 return ( penwidth
% 2 ) == 1;
1578 void* wxGDIPlusContext::GetNativeContext()
1583 // concatenates this transform with the current transform of this context
1584 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1586 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1589 // sets the transform of this context
1590 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1592 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1595 // gets the matrix of this context
1596 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1598 wxGraphicsMatrix matrix
= CreateMatrix();
1599 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1603 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1608 //-----------------------------------------------------------------------------
1609 // wxGDIPlusRenderer declaration
1610 //-----------------------------------------------------------------------------
1612 class wxGDIPlusRenderer
: public wxGraphicsRenderer
1621 virtual ~wxGDIPlusRenderer()
1623 if ( m_loaded
== 1 )
1631 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1633 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1635 #if wxUSE_PRINTING_ARCHITECTURE
1636 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1639 #if wxUSE_ENH_METAFILE
1640 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
1643 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1645 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1647 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1649 virtual wxGraphicsContext
* CreateMeasuringContext();
1653 virtual wxGraphicsPath
CreatePath();
1657 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1658 wxDouble tx
=0.0, wxDouble ty
=0.0);
1661 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1663 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1665 virtual wxGraphicsBrush
1666 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1667 wxDouble x2
, wxDouble y2
,
1668 const wxGraphicsGradientStops
& stops
);
1670 virtual wxGraphicsBrush
1671 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1672 wxDouble xc
, wxDouble yc
,
1674 const wxGraphicsGradientStops
& stops
);
1676 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1678 // create a native bitmap representation
1679 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1681 // create a graphics bitmap from a native bitmap
1682 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1684 // create a subimage from a native image representation
1685 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1688 bool EnsureIsLoaded();
1691 friend class wxGDIPlusRendererModule
;
1695 ULONG_PTR m_gditoken
;
1697 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1700 //-----------------------------------------------------------------------------
1701 // wxGDIPlusRenderer implementation
1702 //-----------------------------------------------------------------------------
1704 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1706 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1708 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1710 return &gs_GDIPlusRenderer
;
1713 bool wxGDIPlusRenderer::EnsureIsLoaded()
1715 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1716 // if we already tried and failed (we don't want to spend lot of time
1717 // returning NULL from wxGraphicsContext::Create(), which may be called
1718 // relatively frequently):
1719 if ( m_loaded
== -1 )
1724 return m_loaded
== 1;
1727 // call EnsureIsLoaded() and return returnOnFail value if it fails
1728 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1729 if ( !EnsureIsLoaded() ) \
1730 return (returnOnFail)
1733 void wxGDIPlusRenderer::Load()
1735 GdiplusStartupInput input
;
1736 GdiplusStartupOutput output
;
1737 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1739 wxLogTrace("gdiplus", "successfully initialized GDI+");
1744 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1749 void wxGDIPlusRenderer::Unload()
1753 GdiplusShutdown(m_gditoken
);
1756 m_loaded
= -1; // next Load() will try again
1759 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1761 ENSURE_LOADED_OR_RETURN(NULL
);
1762 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1763 wxSize sz
= dc
.GetSize();
1764 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1767 #if wxUSE_PRINTING_ARCHITECTURE
1768 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1770 ENSURE_LOADED_OR_RETURN(NULL
);
1771 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1772 wxSize sz
= dc
.GetSize();
1773 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1777 #if wxUSE_ENH_METAFILE
1778 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1780 ENSURE_LOADED_OR_RETURN(NULL
);
1781 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1782 wxSize sz
= dc
.GetSize();
1783 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1787 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1789 ENSURE_LOADED_OR_RETURN(NULL
);
1790 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1791 wxSize sz
= dc
.GetSize();
1792 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1795 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1797 ENSURE_LOADED_OR_RETURN(NULL
);
1798 return new wxGDIPlusMeasuringContext(this);
1801 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1803 ENSURE_LOADED_OR_RETURN(NULL
);
1804 return new wxGDIPlusContext(this,(Graphics
*) context
);
1808 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1810 ENSURE_LOADED_OR_RETURN(NULL
);
1811 return new wxGDIPlusContext(this,(HWND
) window
);
1814 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1816 ENSURE_LOADED_OR_RETURN(NULL
);
1817 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1822 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1824 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1826 m
.SetRefData( new wxGDIPlusPathData(this));
1833 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1834 wxDouble tx
, wxDouble ty
)
1837 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1839 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1840 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1845 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1847 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1848 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1849 return wxNullGraphicsPen
;
1853 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1858 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1860 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1861 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1862 return wxNullGraphicsBrush
;
1866 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1872 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1873 wxDouble x2
, wxDouble y2
,
1874 const wxGraphicsGradientStops
& stops
)
1876 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1878 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1879 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1885 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1886 wxDouble xc
, wxDouble yc
,
1888 const wxGraphicsGradientStops
& stops
)
1890 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1892 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1893 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
1899 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1901 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
1905 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1909 return wxNullGraphicsFont
;
1912 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
1914 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1918 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
1922 return wxNullGraphicsBitmap
;
1925 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
1927 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1928 if ( bitmap
!= NULL
)
1931 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
1935 return wxNullGraphicsBitmap
;
1938 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1940 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1941 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
1945 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
1949 return wxNullGraphicsBitmap
;
1952 // Shutdown GDI+ at app exit, before possible dll unload
1953 class wxGDIPlusRendererModule
: public wxModule
1956 virtual bool OnInit() { return true; }
1957 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
1960 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
1963 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
1965 // ----------------------------------------------------------------------------
1966 // wxMSW-specific parts of wxGCDC
1967 // ----------------------------------------------------------------------------
1969 WXHDC
wxGCDC::AcquireHDC()
1971 wxGraphicsContext
* const gc
= GetGraphicsContext();
1975 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
1976 return g
? g
->GetHDC() : NULL
;
1979 void wxGCDC::ReleaseHDC(WXHDC hdc
)
1984 wxGraphicsContext
* const gc
= GetGraphicsContext();
1985 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
1987 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
1988 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
1990 g
->ReleaseHDC((HDC
)hdc
);
1993 #endif // wxUSE_GRAPHICS_CONTEXT