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();
1155 // Setup page scale, based on DPI ratio.
1156 // Antecedent should be 100dpi when the default page unit (UnitDisplay)
1157 // is used. Page unit UnitDocument would require 300dpi instead.
1158 // Note that calling SetPageScale() does not have effect on non-printing
1159 // DCs (that is, any other than wxPrinterDC or wxEnhMetaFileDC).
1160 REAL dpiRatio
= 100.0 / m_context
->GetDpiY();
1161 m_context
->SetPageScale(dpiRatio
);
1164 wxGDIPlusContext::~wxGDIPlusContext()
1168 m_context
->Restore( m_state2
);
1169 m_context
->Restore( m_state1
);
1175 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1177 Region
rgn((HRGN
)region
.GetHRGN());
1178 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1181 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1183 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1186 void wxGDIPlusContext::ResetClip()
1188 m_context
->ResetClip();
1191 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1193 if (m_composition
== wxCOMPOSITION_DEST
)
1196 if ( !m_pen
.IsNull() )
1198 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1199 Point
*cpoints
= new Point
[n
];
1200 for (size_t i
= 0; i
< n
; i
++)
1202 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1203 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1205 } // for (size_t i = 0; i < n; i++)
1206 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1211 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1213 if (m_composition
== wxCOMPOSITION_DEST
)
1216 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1217 Point
*cpoints
= new Point
[n
];
1218 for (size_t i
= 0; i
< n
; i
++)
1220 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1221 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1223 } // for (int i = 0; i < n; i++)
1224 if ( !m_brush
.IsNull() )
1225 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1226 if ( !m_pen
.IsNull() )
1227 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1231 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1233 if (m_composition
== wxCOMPOSITION_DEST
)
1236 if ( !m_pen
.IsNull() )
1238 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1239 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1243 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1245 if (m_composition
== wxCOMPOSITION_DEST
)
1248 if ( !m_brush
.IsNull() )
1250 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1251 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1252 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1253 (GraphicsPath
*) path
.GetNativePath());
1257 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1259 if (m_antialias
== antialias
)
1262 m_antialias
= antialias
;
1264 SmoothingMode antialiasMode
;
1267 case wxANTIALIAS_DEFAULT
:
1268 antialiasMode
= SmoothingModeHighQuality
;
1270 case wxANTIALIAS_NONE
:
1271 antialiasMode
= SmoothingModeNone
;
1276 m_context
->SetSmoothingMode(antialiasMode
);
1280 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1282 if ( m_composition
== op
)
1287 if (m_composition
== wxCOMPOSITION_DEST
)
1290 CompositingMode cop
;
1293 case wxCOMPOSITION_SOURCE
:
1294 cop
= CompositingModeSourceCopy
;
1296 case wxCOMPOSITION_OVER
:
1297 cop
= CompositingModeSourceOver
;
1303 m_context
->SetCompositingMode(cop
);
1307 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1312 void wxGDIPlusContext::EndLayer()
1317 void wxGDIPlusContext::Rotate( wxDouble angle
)
1319 m_context
->RotateTransform( RadToDeg(angle
) );
1322 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1324 m_context
->TranslateTransform( dx
, dy
);
1327 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1329 m_context
->ScaleTransform(xScale
,yScale
);
1332 void wxGDIPlusContext::PushState()
1334 GraphicsState state
= m_context
->Save();
1335 m_stateStack
.push(state
);
1338 void wxGDIPlusContext::PopState()
1340 GraphicsState state
= m_stateStack
.top();
1342 m_context
->Restore(state
);
1345 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1347 if (m_composition
== wxCOMPOSITION_DEST
)
1350 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1353 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1355 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1356 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1357 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1358 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1361 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1365 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1367 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1368 DrawBitmap(bitmap
, x
, y
, w
, h
);
1371 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1373 if (m_composition
== wxCOMPOSITION_DEST
)
1376 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1377 // find out by looking at the bitmap data whether there really was alpha in it
1378 HICON hIcon
= (HICON
)icon
.GetHICON();
1380 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1381 if (!GetIconInfo(hIcon
,&iconInfo
))
1384 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1386 Bitmap
* image
= NULL
;
1388 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1389 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1390 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1392 image
= Bitmap::FromHICON(hIcon
);
1396 size_t width
= interim
.GetWidth();
1397 size_t height
= interim
.GetHeight();
1398 Rect
bounds(0,0,width
,height
);
1401 interim
.LockBits(&bounds
, ImageLockModeRead
,
1402 interim
.GetPixelFormat(),&data
);
1404 bool hasAlpha
= false;
1405 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1407 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1409 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1410 if ( ( *dest
& Color::AlphaMask
) != 0 )
1417 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1418 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1422 image
= Bitmap::FromHICON(hIcon
);
1425 interim
.UnlockBits(&data
);
1428 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1431 DeleteObject(iconInfo
.hbmColor
);
1432 DeleteObject(iconInfo
.hbmMask
);
1435 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1436 wxDouble x
, wxDouble y
,
1437 const wxGraphicsBrush
& brush
)
1439 if (m_composition
== wxCOMPOSITION_DEST
)
1442 wxCHECK_RET( !m_font
.IsNull(),
1443 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1448 wxGDIPlusFontData
* const
1449 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1450 wxGDIPlusBrushData
* const
1451 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1453 m_context
->DrawString
1455 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1456 -1, // length: string is NUL-terminated
1457 fontData
->GetGDIPlusFont(),
1459 StringFormat::GenericTypographic(),
1460 brushData
? brushData
->GetGDIPlusBrush()
1461 : fontData
->GetGDIPlusBrush()
1465 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1466 wxDouble
*descent
, wxDouble
*externalLeading
) const
1468 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1470 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1471 FontFamily ffamily
;
1472 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1474 f
->GetFamily(&ffamily
) ;
1476 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1478 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1479 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1480 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1481 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1482 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1483 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1486 *height
= rHeight
* factorY
;
1488 *descent
= rDescent
* factorY
;
1489 if ( externalLeading
)
1490 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1491 // measuring empty strings is not guaranteed, so do it by hand
1499 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1500 StringFormat
strFormat( StringFormat::GenericTypographic() );
1501 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1504 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1506 *width
= bounds
.Width
;
1508 *height
= bounds
.Height
;
1512 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1515 widths
.Add(0, text
.length());
1517 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1522 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1523 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1524 size_t len
= wcslen( ws
) ;
1525 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1527 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1528 StringFormat
strFormat( StringFormat::GenericTypographic() );
1530 size_t startPosition
= 0;
1531 size_t remainder
= len
;
1532 const size_t maxSpan
= 32;
1533 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1534 Region
* regions
= new Region
[maxSpan
];
1536 while( remainder
> 0 )
1538 size_t span
= wxMin( maxSpan
, remainder
);
1540 for( size_t i
= 0 ; i
< span
; ++i
)
1542 ranges
[i
].First
= 0 ;
1543 ranges
[i
].Length
= startPosition
+i
+1 ;
1545 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1546 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1547 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1550 for ( size_t i
= 0 ; i
< span
; ++i
)
1552 regions
[i
].GetBounds(&bbox
,m_context
);
1553 widths
[startPosition
+i
] = bbox
.Width
;
1556 startPosition
+= span
;
1563 bool wxGDIPlusContext::ShouldOffset() const
1566 if ( !m_pen
.IsNull() )
1568 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1569 if ( penwidth
== 0 )
1572 return ( penwidth
% 2 ) == 1;
1575 void* wxGDIPlusContext::GetNativeContext()
1580 // concatenates this transform with the current transform of this context
1581 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1583 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1586 // sets the transform of this context
1587 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1589 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1592 // gets the matrix of this context
1593 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1595 wxGraphicsMatrix matrix
= CreateMatrix();
1596 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1600 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1605 //-----------------------------------------------------------------------------
1606 // wxGDIPlusRenderer declaration
1607 //-----------------------------------------------------------------------------
1609 class wxGDIPlusRenderer
: public wxGraphicsRenderer
1618 virtual ~wxGDIPlusRenderer()
1620 if ( m_loaded
== 1 )
1628 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1630 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1632 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1634 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1636 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1638 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1640 virtual wxGraphicsContext
* CreateMeasuringContext();
1644 virtual wxGraphicsPath
CreatePath();
1648 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1649 wxDouble tx
=0.0, wxDouble ty
=0.0);
1652 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1654 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1656 virtual wxGraphicsBrush
1657 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1658 wxDouble x2
, wxDouble y2
,
1659 const wxGraphicsGradientStops
& stops
);
1661 virtual wxGraphicsBrush
1662 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1663 wxDouble xc
, wxDouble yc
,
1665 const wxGraphicsGradientStops
& stops
);
1667 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1669 // create a native bitmap representation
1670 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1672 // create a graphics bitmap from a native bitmap
1673 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1675 // create a subimage from a native image representation
1676 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1679 bool EnsureIsLoaded();
1682 friend class wxGDIPlusRendererModule
;
1686 ULONG_PTR m_gditoken
;
1688 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1691 //-----------------------------------------------------------------------------
1692 // wxGDIPlusRenderer implementation
1693 //-----------------------------------------------------------------------------
1695 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1697 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1699 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1701 return &gs_GDIPlusRenderer
;
1704 bool wxGDIPlusRenderer::EnsureIsLoaded()
1706 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1707 // if we already tried and failed (we don't want to spend lot of time
1708 // returning NULL from wxGraphicsContext::Create(), which may be called
1709 // relatively frequently):
1710 if ( m_loaded
== -1 )
1715 return m_loaded
== 1;
1718 // call EnsureIsLoaded() and return returnOnFail value if it fails
1719 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1720 if ( !EnsureIsLoaded() ) \
1721 return (returnOnFail)
1724 void wxGDIPlusRenderer::Load()
1726 GdiplusStartupInput input
;
1727 GdiplusStartupOutput output
;
1728 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1730 wxLogTrace("gdiplus", "successfully initialized GDI+");
1735 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1740 void wxGDIPlusRenderer::Unload()
1744 GdiplusShutdown(m_gditoken
);
1747 m_loaded
= -1; // next Load() will try again
1750 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1752 ENSURE_LOADED_OR_RETURN(NULL
);
1753 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1754 wxSize sz
= dc
.GetSize();
1755 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1758 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1760 ENSURE_LOADED_OR_RETURN(NULL
);
1761 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1762 wxSize sz
= dc
.GetSize();
1763 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1766 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1768 ENSURE_LOADED_OR_RETURN(NULL
);
1769 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1770 wxSize sz
= dc
.GetSize();
1771 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1774 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1776 ENSURE_LOADED_OR_RETURN(NULL
);
1777 return new wxGDIPlusMeasuringContext(this);
1780 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1782 ENSURE_LOADED_OR_RETURN(NULL
);
1783 return new wxGDIPlusContext(this,(Graphics
*) context
);
1787 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1789 ENSURE_LOADED_OR_RETURN(NULL
);
1790 return new wxGDIPlusContext(this,(HWND
) window
);
1793 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1795 ENSURE_LOADED_OR_RETURN(NULL
);
1796 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1801 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1803 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1805 m
.SetRefData( new wxGDIPlusPathData(this));
1812 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1813 wxDouble tx
, wxDouble ty
)
1816 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1818 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1819 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1824 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1826 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1827 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1828 return wxNullGraphicsPen
;
1832 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1837 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1839 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1840 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1841 return wxNullGraphicsBrush
;
1845 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1851 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1852 wxDouble x2
, wxDouble y2
,
1853 const wxGraphicsGradientStops
& stops
)
1855 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1857 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1858 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1864 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1865 wxDouble xc
, wxDouble yc
,
1867 const wxGraphicsGradientStops
& stops
)
1869 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1871 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1872 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
1878 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1880 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
1884 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1888 return wxNullGraphicsFont
;
1891 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
1893 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1897 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
1901 return wxNullGraphicsBitmap
;
1904 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
1906 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1907 if ( bitmap
!= NULL
)
1910 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
1914 return wxNullGraphicsBitmap
;
1917 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1919 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1920 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
1924 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
1928 return wxNullGraphicsBitmap
;
1931 // Shutdown GDI+ at app exit, before possible dll unload
1932 class wxGDIPlusRendererModule
: public wxModule
1935 virtual bool OnInit() { return true; }
1936 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
1939 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
1942 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
1944 // ----------------------------------------------------------------------------
1945 // wxMSW-specific parts of wxGCDC
1946 // ----------------------------------------------------------------------------
1948 WXHDC
wxGCDC::AcquireHDC()
1950 wxGraphicsContext
* const gc
= GetGraphicsContext();
1954 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
1955 return g
? g
->GetHDC() : NULL
;
1958 void wxGCDC::ReleaseHDC(WXHDC hdc
)
1963 wxGraphicsContext
* const gc
= GetGraphicsContext();
1964 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
1966 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
1967 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
1969 g
->ReleaseHDC((HDC
)hdc
);
1972 #endif // wxUSE_GRAPHICS_CONTEXT