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(),
521 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
526 m_penBrush
= new TextureBrush(m_penImage
);
527 m_pen
->SetBrush( m_penBrush
);
533 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
535 HatchStyle style
= HatchStyleHorizontal
;
536 switch( pen
.GetStyle() )
538 case wxBDIAGONAL_HATCH
:
539 style
= HatchStyleBackwardDiagonal
;
541 case wxCROSSDIAG_HATCH
:
542 style
= HatchStyleDiagonalCross
;
544 case wxFDIAGONAL_HATCH
:
545 style
= HatchStyleForwardDiagonal
;
548 style
= HatchStyleCross
;
550 case wxHORIZONTAL_HATCH
:
551 style
= HatchStyleHorizontal
;
553 case wxVERTICAL_HATCH
:
554 style
= HatchStyleVertical
;
558 m_penBrush
= new HatchBrush
561 wxColourToColor(pen
.GetColour()),
564 m_pen
->SetBrush( m_penBrush
);
568 if ( dashStyle
!= DashStyleSolid
)
569 m_pen
->SetDashStyle(dashStyle
);
572 //-----------------------------------------------------------------------------
573 // wxGDIPlusBrush implementation
574 //-----------------------------------------------------------------------------
576 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
577 : wxGraphicsObjectRefData(renderer
)
582 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
583 : wxGraphicsObjectRefData(renderer
)
586 if ( brush
.GetStyle() == wxSOLID
)
588 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
590 else if ( brush
.IsHatch() )
592 HatchStyle style
= HatchStyleHorizontal
;
593 switch( brush
.GetStyle() )
595 case wxBDIAGONAL_HATCH
:
596 style
= HatchStyleBackwardDiagonal
;
598 case wxCROSSDIAG_HATCH
:
599 style
= HatchStyleDiagonalCross
;
601 case wxFDIAGONAL_HATCH
:
602 style
= HatchStyleForwardDiagonal
;
605 style
= HatchStyleCross
;
607 case wxHORIZONTAL_HATCH
:
608 style
= HatchStyleHorizontal
;
610 case wxVERTICAL_HATCH
:
611 style
= HatchStyleVertical
;
615 m_brush
= new HatchBrush
618 wxColourToColor(brush
.GetColour()),
624 wxBitmap
* bmp
= brush
.GetStipple();
625 if ( bmp
&& bmp
->Ok() )
627 wxDELETE( m_brushImage
);
628 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
630 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
635 m_brush
= new TextureBrush(m_brushImage
);
640 wxGDIPlusBrushData::~wxGDIPlusBrushData()
647 void wxGDIPlusBrushData::Init()
654 template <typename T
>
656 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
657 const wxGraphicsGradientStops
& stops
)
659 const unsigned numStops
= stops
.GetCount();
662 // initial and final colours are set during the brush creation, nothing
667 wxVector
<Color
> colors(numStops
);
668 wxVector
<REAL
> positions(numStops
);
670 for ( unsigned i
= 0; i
< numStops
; i
++ )
672 wxGraphicsGradientStop stop
= stops
.Item(i
);
674 colors
[i
] = wxColourToColor(stop
.GetColour());
675 positions
[i
] = stop
.GetPosition();
678 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
682 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
683 wxDouble x2
, wxDouble y2
,
684 const wxGraphicsGradientStops
& stops
)
686 LinearGradientBrush
* const
687 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
688 wxColourToColor(stops
.GetStartColour()),
689 wxColourToColor(stops
.GetEndColour()));
692 SetGradientStops(brush
, stops
);
696 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
697 wxDouble xc
, wxDouble yc
,
699 const wxGraphicsGradientStops
& stops
)
701 m_brushPath
= new GraphicsPath();
702 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
703 (REAL
)(2*radius
), (REAL
)(2*radius
));
705 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
707 brush
->SetCenterPoint(PointF(xo
, yo
));
708 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
710 const Color
col(wxColourToColor(stops
.GetEndColour()));
712 brush
->SetSurroundColors(&col
, &count
);
714 SetGradientStops(brush
, stops
);
717 //-----------------------------------------------------------------------------
718 // wxGDIPlusFont implementation
719 //-----------------------------------------------------------------------------
721 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
722 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
727 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
728 int size
= font
.GetPointSize();
729 int style
= FontStyleRegular
;
730 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
731 style
|= FontStyleItalic
;
732 if ( font
.GetUnderlined() )
733 style
|= FontStyleUnderline
;
734 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
735 style
|= FontStyleBold
;
736 m_font
= new Font( s
, size
, style
);
737 m_textBrush
= new SolidBrush(wxColourToColor(col
));
740 wxGDIPlusFontData::~wxGDIPlusFontData()
746 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
747 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
748 // bytes as parameter, since there is no real copying of the data going in, only references are stored
749 // m_helper has to be kept alive as well
751 //-----------------------------------------------------------------------------
752 // wxGDIPlusBitmapData implementation
753 //-----------------------------------------------------------------------------
755 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
756 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
761 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
762 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
767 Bitmap
* image
= NULL
;
770 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
772 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
778 size_t width
= interim
.GetWidth();
779 size_t height
= interim
.GetHeight();
780 Rect
bounds(0,0,width
,height
);
782 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
784 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
785 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
787 BitmapData dataMask
;
788 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
789 interimMask
.GetPixelFormat(),&dataMask
);
792 BitmapData imageData
;
793 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
795 BYTE maskPattern
= 0 ;
799 for ( size_t y
= 0 ; y
< height
; ++y
)
802 for( size_t x
= 0 ; x
< width
; ++x
)
807 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
811 maskPattern
= maskPattern
>> 1;
813 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
814 if ( (maskByte
& maskPattern
) == 0 )
819 interim
.GetPixel(x
,y
,&c
) ;
820 *dest
= (c
.GetValue() | Color::AlphaMask
);
825 image
->UnlockBits(&imageData
);
827 interimMask
.UnlockBits(&dataMask
);
828 interim
.UnlockBits(&dataMask
);
832 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
834 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
839 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
841 size_t width
= image
->GetWidth();
842 size_t height
= image
->GetHeight();
843 Rect
bounds(0,0,width
,height
);
844 static BitmapData data
;
848 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
849 m_helper
->GetPixelFormat(),&data
);
851 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
852 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
854 m_helper
->UnlockBits(&data
);
861 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
867 //-----------------------------------------------------------------------------
868 // wxGDIPlusPath implementation
869 //-----------------------------------------------------------------------------
871 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
876 m_path
= new GraphicsPath();
879 wxGDIPlusPathData::~wxGDIPlusPathData()
884 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
886 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
893 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
895 m_path
->StartFigure();
896 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
899 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
901 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
904 void wxGDIPlusPathData::CloseSubpath()
906 m_path
->CloseFigure();
909 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
915 m_path
->GetLastPoint(&start
);
916 m_path
->AddBezier(start
,c1
,c2
,end
);
919 // gets the last point of the current path, (0,0) if not yet set
920 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
923 m_path
->GetLastPoint(&start
);
928 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
930 double sweepAngle
= endAngle
- startAngle
;
931 if( fabs(sweepAngle
) >= 2*M_PI
)
933 sweepAngle
= 2 * M_PI
;
940 sweepAngle
+= 2 * M_PI
;
945 sweepAngle
-= 2 * M_PI
;
949 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
952 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
954 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
957 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
959 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
963 // transforms each point of this path by the matrix
964 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
966 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
969 // gets the bounding box enclosing all points (possibly including control points)
970 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
973 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
980 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
982 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
983 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
986 //-----------------------------------------------------------------------------
987 // wxGDIPlusMatrixData implementation
988 //-----------------------------------------------------------------------------
990 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
991 : wxGraphicsMatrixData(renderer
)
996 m_matrix
= new Matrix();
999 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1004 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1006 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1009 // concatenates the matrix
1010 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1012 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1015 // sets the matrix to the respective values
1016 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1017 wxDouble tx
, wxDouble ty
)
1019 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1022 // gets the component valuess of the matrix
1023 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1024 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1027 m_matrix
->GetElements(elements
);
1028 if (a
) *a
= elements
[0];
1029 if (b
) *b
= elements
[1];
1030 if (c
) *c
= elements
[2];
1031 if (d
) *d
= elements
[3];
1032 if (tx
) *tx
= elements
[4];
1033 if (ty
) *ty
= elements
[5];
1036 // makes this the inverse matrix
1037 void wxGDIPlusMatrixData::Invert()
1042 // returns true if the elements of the transformation matrix are equal ?
1043 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1045 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1048 // return true if this is the identity matrix
1049 bool wxGDIPlusMatrixData::IsIdentity() const
1051 return m_matrix
->IsIdentity() == TRUE
;
1058 // add the translation to this matrix
1059 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1061 m_matrix
->Translate(dx
,dy
);
1064 // add the scale to this matrix
1065 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1067 m_matrix
->Scale(xScale
,yScale
);
1070 // add the rotation to this matrix (radians)
1071 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1073 m_matrix
->Rotate( RadToDeg(angle
) );
1077 // apply the transforms
1080 // applies that matrix to the point
1081 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1084 m_matrix
->TransformPoints(&pt
);
1089 // applies the matrix except for translations
1090 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1093 m_matrix
->TransformVectors(&pt
);
1098 // returns the native representation
1099 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1104 //-----------------------------------------------------------------------------
1105 // wxGDIPlusContext implementation
1106 //-----------------------------------------------------------------------------
1108 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1109 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1111 class wxGDIPlusOffsetHelper
1114 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1119 m_gr
->TranslateTransform( 0.5, 0.5 );
1121 ~wxGDIPlusOffsetHelper( )
1124 m_gr
->TranslateTransform( -0.5, -0.5 );
1131 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1132 : wxGraphicsContext(renderer
)
1135 m_context
= new Graphics( hdc
);
1141 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1142 : wxGraphicsContext(renderer
)
1145 m_context
= new Graphics( hwnd
);
1146 RECT rect
= wxGetWindowRect(hwnd
);
1147 m_width
= rect
.right
- rect
.left
;
1148 m_height
= rect
.bottom
- rect
.top
;
1152 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1153 : wxGraphicsContext(renderer
)
1160 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1165 void wxGDIPlusContext::Init()
1174 void wxGDIPlusContext::SetDefaults()
1176 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1177 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1178 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1179 m_state1
= m_context
->Save();
1180 m_state2
= m_context
->Save();
1182 // Setup page scale, based on DPI ratio.
1183 // Antecedent should be 100dpi when the default page unit (UnitDisplay)
1184 // is used. Page unit UnitDocument would require 300dpi instead.
1185 // Note that calling SetPageScale() does not have effect on non-printing
1186 // DCs (that is, any other than wxPrinterDC or wxEnhMetaFileDC).
1187 REAL dpiRatio
= 100.0 / m_context
->GetDpiY();
1188 m_context
->SetPageScale(dpiRatio
);
1191 wxGDIPlusContext::~wxGDIPlusContext()
1195 m_context
->Restore( m_state2
);
1196 m_context
->Restore( m_state1
);
1202 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1204 Region
rgn((HRGN
)region
.GetHRGN());
1205 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1208 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1210 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1213 void wxGDIPlusContext::ResetClip()
1215 m_context
->ResetClip();
1218 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1220 if (m_composition
== wxCOMPOSITION_DEST
)
1223 if ( !m_pen
.IsNull() )
1225 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1226 Point
*cpoints
= new Point
[n
];
1227 for (size_t i
= 0; i
< n
; i
++)
1229 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1230 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1232 } // for (size_t i = 0; i < n; i++)
1233 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1238 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1240 if (m_composition
== wxCOMPOSITION_DEST
)
1243 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1244 Point
*cpoints
= new Point
[n
];
1245 for (size_t i
= 0; i
< n
; i
++)
1247 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1248 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1250 } // for (int i = 0; i < n; i++)
1251 if ( !m_brush
.IsNull() )
1252 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1253 if ( !m_pen
.IsNull() )
1254 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1258 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1260 if (m_composition
== wxCOMPOSITION_DEST
)
1263 if ( !m_pen
.IsNull() )
1265 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1266 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1270 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1272 if (m_composition
== wxCOMPOSITION_DEST
)
1275 if ( !m_brush
.IsNull() )
1277 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1278 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1279 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1280 (GraphicsPath
*) path
.GetNativePath());
1284 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1286 if (m_antialias
== antialias
)
1289 m_antialias
= antialias
;
1291 SmoothingMode antialiasMode
;
1294 case wxANTIALIAS_DEFAULT
:
1295 antialiasMode
= SmoothingModeHighQuality
;
1297 case wxANTIALIAS_NONE
:
1298 antialiasMode
= SmoothingModeNone
;
1303 m_context
->SetSmoothingMode(antialiasMode
);
1307 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1309 if ( m_composition
== op
)
1314 if (m_composition
== wxCOMPOSITION_DEST
)
1317 CompositingMode cop
;
1320 case wxCOMPOSITION_SOURCE
:
1321 cop
= CompositingModeSourceCopy
;
1323 case wxCOMPOSITION_OVER
:
1324 cop
= CompositingModeSourceOver
;
1330 m_context
->SetCompositingMode(cop
);
1334 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1339 void wxGDIPlusContext::EndLayer()
1344 void wxGDIPlusContext::Rotate( wxDouble angle
)
1346 m_context
->RotateTransform( RadToDeg(angle
) );
1349 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1351 m_context
->TranslateTransform( dx
, dy
);
1354 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1356 m_context
->ScaleTransform(xScale
,yScale
);
1359 void wxGDIPlusContext::PushState()
1361 GraphicsState state
= m_context
->Save();
1362 m_stateStack
.push(state
);
1365 void wxGDIPlusContext::PopState()
1367 GraphicsState state
= m_stateStack
.top();
1369 m_context
->Restore(state
);
1372 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1374 if (m_composition
== wxCOMPOSITION_DEST
)
1377 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1380 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1382 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1383 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1384 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1385 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1388 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1392 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1394 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1395 DrawBitmap(bitmap
, x
, y
, w
, h
);
1398 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1400 if (m_composition
== wxCOMPOSITION_DEST
)
1403 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1404 // find out by looking at the bitmap data whether there really was alpha in it
1405 HICON hIcon
= (HICON
)icon
.GetHICON();
1407 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1408 if (!GetIconInfo(hIcon
,&iconInfo
))
1411 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1413 Bitmap
* image
= NULL
;
1415 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1416 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1417 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1419 image
= Bitmap::FromHICON(hIcon
);
1423 size_t width
= interim
.GetWidth();
1424 size_t height
= interim
.GetHeight();
1425 Rect
bounds(0,0,width
,height
);
1428 interim
.LockBits(&bounds
, ImageLockModeRead
,
1429 interim
.GetPixelFormat(),&data
);
1431 bool hasAlpha
= false;
1432 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1434 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1436 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1437 if ( ( *dest
& Color::AlphaMask
) != 0 )
1444 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1445 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1449 image
= Bitmap::FromHICON(hIcon
);
1452 interim
.UnlockBits(&data
);
1455 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1458 DeleteObject(iconInfo
.hbmColor
);
1459 DeleteObject(iconInfo
.hbmMask
);
1462 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1463 wxDouble x
, wxDouble y
,
1464 const wxGraphicsBrush
& brush
)
1466 if (m_composition
== wxCOMPOSITION_DEST
)
1469 wxCHECK_RET( !m_font
.IsNull(),
1470 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1475 wxGDIPlusFontData
* const
1476 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1477 wxGDIPlusBrushData
* const
1478 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1480 m_context
->DrawString
1482 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1483 -1, // length: string is NUL-terminated
1484 fontData
->GetGDIPlusFont(),
1486 StringFormat::GenericTypographic(),
1487 brushData
? brushData
->GetGDIPlusBrush()
1488 : fontData
->GetGDIPlusBrush()
1492 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1493 wxDouble
*descent
, wxDouble
*externalLeading
) const
1495 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1497 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1498 FontFamily ffamily
;
1499 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1501 f
->GetFamily(&ffamily
) ;
1503 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1505 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1506 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1507 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1508 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1509 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1510 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1513 *height
= rHeight
* factorY
;
1515 *descent
= rDescent
* factorY
;
1516 if ( externalLeading
)
1517 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1518 // measuring empty strings is not guaranteed, so do it by hand
1526 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1527 StringFormat
strFormat( StringFormat::GenericTypographic() );
1528 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1531 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1533 *width
= bounds
.Width
;
1535 *height
= bounds
.Height
;
1539 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1542 widths
.Add(0, text
.length());
1544 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1549 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1550 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1551 size_t len
= wcslen( ws
) ;
1552 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1554 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1555 StringFormat
strFormat( StringFormat::GenericTypographic() );
1557 size_t startPosition
= 0;
1558 size_t remainder
= len
;
1559 const size_t maxSpan
= 32;
1560 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1561 Region
* regions
= new Region
[maxSpan
];
1563 while( remainder
> 0 )
1565 size_t span
= wxMin( maxSpan
, remainder
);
1567 for( size_t i
= 0 ; i
< span
; ++i
)
1569 ranges
[i
].First
= 0 ;
1570 ranges
[i
].Length
= startPosition
+i
+1 ;
1572 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1573 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1574 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1577 for ( size_t i
= 0 ; i
< span
; ++i
)
1579 regions
[i
].GetBounds(&bbox
,m_context
);
1580 widths
[startPosition
+i
] = bbox
.Width
;
1583 startPosition
+= span
;
1590 bool wxGDIPlusContext::ShouldOffset() const
1593 if ( !m_pen
.IsNull() )
1595 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1596 if ( penwidth
== 0 )
1599 return ( penwidth
% 2 ) == 1;
1602 void* wxGDIPlusContext::GetNativeContext()
1607 // concatenates this transform with the current transform of this context
1608 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1610 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1613 // sets the transform of this context
1614 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1616 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1619 // gets the matrix of this context
1620 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1622 wxGraphicsMatrix matrix
= CreateMatrix();
1623 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1627 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1632 //-----------------------------------------------------------------------------
1633 // wxGDIPlusRenderer declaration
1634 //-----------------------------------------------------------------------------
1636 class wxGDIPlusRenderer
: public wxGraphicsRenderer
1645 virtual ~wxGDIPlusRenderer()
1647 if ( m_loaded
== 1 )
1655 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1657 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1659 #if wxUSE_PRINTING_ARCHITECTURE
1660 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1663 #if wxUSE_ENH_METAFILE
1664 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
1667 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1669 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1671 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1673 virtual wxGraphicsContext
* CreateMeasuringContext();
1677 virtual wxGraphicsPath
CreatePath();
1681 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1682 wxDouble tx
=0.0, wxDouble ty
=0.0);
1685 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1687 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1689 virtual wxGraphicsBrush
1690 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1691 wxDouble x2
, wxDouble y2
,
1692 const wxGraphicsGradientStops
& stops
);
1694 virtual wxGraphicsBrush
1695 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1696 wxDouble xc
, wxDouble yc
,
1698 const wxGraphicsGradientStops
& stops
);
1700 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1702 // create a native bitmap representation
1703 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1705 // create a graphics bitmap from a native bitmap
1706 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
1708 // create a subimage from a native image representation
1709 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1712 bool EnsureIsLoaded();
1715 friend class wxGDIPlusRendererModule
;
1719 ULONG_PTR m_gditoken
;
1721 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1724 //-----------------------------------------------------------------------------
1725 // wxGDIPlusRenderer implementation
1726 //-----------------------------------------------------------------------------
1728 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1730 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1732 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1734 return &gs_GDIPlusRenderer
;
1737 bool wxGDIPlusRenderer::EnsureIsLoaded()
1739 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1740 // if we already tried and failed (we don't want to spend lot of time
1741 // returning NULL from wxGraphicsContext::Create(), which may be called
1742 // relatively frequently):
1743 if ( m_loaded
== -1 )
1748 return m_loaded
== 1;
1751 // call EnsureIsLoaded() and return returnOnFail value if it fails
1752 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1753 if ( !EnsureIsLoaded() ) \
1754 return (returnOnFail)
1757 void wxGDIPlusRenderer::Load()
1759 GdiplusStartupInput input
;
1760 GdiplusStartupOutput output
;
1761 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1763 wxLogTrace("gdiplus", "successfully initialized GDI+");
1768 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1773 void wxGDIPlusRenderer::Unload()
1777 GdiplusShutdown(m_gditoken
);
1780 m_loaded
= -1; // next Load() will try again
1783 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1785 ENSURE_LOADED_OR_RETURN(NULL
);
1786 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1787 wxSize sz
= dc
.GetSize();
1788 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1791 #if wxUSE_PRINTING_ARCHITECTURE
1792 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1794 ENSURE_LOADED_OR_RETURN(NULL
);
1795 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1796 wxSize sz
= dc
.GetSize();
1797 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1801 #if wxUSE_ENH_METAFILE
1802 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1804 ENSURE_LOADED_OR_RETURN(NULL
);
1805 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1806 wxSize sz
= dc
.GetSize();
1807 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1811 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1813 ENSURE_LOADED_OR_RETURN(NULL
);
1814 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1815 wxSize sz
= dc
.GetSize();
1816 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1819 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1821 ENSURE_LOADED_OR_RETURN(NULL
);
1822 return new wxGDIPlusMeasuringContext(this);
1825 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1827 ENSURE_LOADED_OR_RETURN(NULL
);
1828 return new wxGDIPlusContext(this,(Graphics
*) context
);
1832 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1834 ENSURE_LOADED_OR_RETURN(NULL
);
1835 return new wxGDIPlusContext(this,(HWND
) window
);
1838 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1840 ENSURE_LOADED_OR_RETURN(NULL
);
1841 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1846 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1848 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1850 m
.SetRefData( new wxGDIPlusPathData(this));
1857 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1858 wxDouble tx
, wxDouble ty
)
1861 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1863 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1864 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1869 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1871 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1872 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1873 return wxNullGraphicsPen
;
1877 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1882 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1884 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1885 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1886 return wxNullGraphicsBrush
;
1890 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1896 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1897 wxDouble x2
, wxDouble y2
,
1898 const wxGraphicsGradientStops
& stops
)
1900 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1902 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1903 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1909 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1910 wxDouble xc
, wxDouble yc
,
1912 const wxGraphicsGradientStops
& stops
)
1914 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1916 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1917 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
1923 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1925 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
1929 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1933 return wxNullGraphicsFont
;
1936 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
1938 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1942 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
1946 return wxNullGraphicsBitmap
;
1949 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
1951 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1952 if ( bitmap
!= NULL
)
1955 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
1959 return wxNullGraphicsBitmap
;
1962 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1964 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1965 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
1969 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
1973 return wxNullGraphicsBitmap
;
1976 // Shutdown GDI+ at app exit, before possible dll unload
1977 class wxGDIPlusRendererModule
: public wxModule
1980 virtual bool OnInit() { return true; }
1981 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
1984 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
1987 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
1989 // ----------------------------------------------------------------------------
1990 // wxMSW-specific parts of wxGCDC
1991 // ----------------------------------------------------------------------------
1993 WXHDC
wxGCDC::AcquireHDC()
1995 wxGraphicsContext
* const gc
= GetGraphicsContext();
1999 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2000 return g
? g
->GetHDC() : NULL
;
2003 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2008 wxGraphicsContext
* const gc
= GetGraphicsContext();
2009 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2011 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2012 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2014 g
->ReleaseHDC((HDC
)hdc
);
2017 #endif // wxUSE_GRAPHICS_CONTEXT