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"
42 #include "wx/private/graphics.h"
43 #include "wx/msw/wrapgdip.h"
44 #include "wx/msw/dc.h"
45 #if wxUSE_ENH_METAFILE
46 #include "wx/msw/enhmeta.h"
48 #include "wx/dcgraph.h"
50 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
52 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 const double RAD2DEG
= 180.0 / M_PI
;
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
70 inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
72 inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
73 inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
75 // translate a wxColour to a Color
76 inline Color
wxColourToColor(const wxColour
& col
)
78 return Color(col
.Alpha(), col
.Red(), col
.Green(), col
.Blue());
81 } // anonymous namespace
83 //-----------------------------------------------------------------------------
84 // device context implementation
86 // more and more of the dc functionality should be implemented by calling
87 // the appropricate wxGDIPlusContext, but we will have to do that step by step
88 // also coordinate conversions should be moved to native matrix ops
89 //-----------------------------------------------------------------------------
91 // we always stock two context states, one at entry, to be able to preserve the
92 // state we were called with, the other one after changing to HI Graphics orientation
93 // (this one is used for getting back clippings etc)
95 //-----------------------------------------------------------------------------
96 // wxGraphicsPath implementation
97 //-----------------------------------------------------------------------------
99 class wxGDIPlusContext
;
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
; }
285 wxImage
ConvertToImage() const;
286 #endif // wxUSE_IMAGE
293 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
296 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
298 const wxColour
& col
);
299 wxGDIPlusFontData(wxGraphicsRenderer
* renderer
,
300 const wxString
& name
,
303 const wxColour
& col
);
304 ~wxGDIPlusFontData();
306 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
307 virtual Font
* GetGDIPlusFont() { return m_font
; }
310 // Common part of all ctors, flags here is a combination of values of
311 // FontStyle GDI+ enum.
312 void Init(const wxString
& name
,
322 class wxGDIPlusContext
: public wxGraphicsContext
325 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
326 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
327 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
328 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
329 wxGDIPlusContext(wxGraphicsRenderer
* renderer
);
331 virtual ~wxGDIPlusContext();
333 virtual void Clip( const wxRegion
®ion
);
334 // clips drawings to the rect
335 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
337 // resets the clipping to original extent
338 virtual void ResetClip();
340 virtual void * GetNativeContext();
342 virtual void StrokePath( const wxGraphicsPath
& p
);
343 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
345 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
347 // stroke lines connecting each of the points
348 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
351 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
353 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
355 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
357 virtual bool SetCompositionMode(wxCompositionMode op
);
359 virtual void BeginLayer(wxDouble opacity
);
361 virtual void EndLayer();
363 virtual void Translate( wxDouble dx
, wxDouble dy
);
364 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
365 virtual void Rotate( wxDouble angle
);
367 // concatenates this transform with the current transform of this context
368 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
370 // sets the transform of this context
371 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
373 // gets the matrix of this context
374 virtual wxGraphicsMatrix
GetTransform() const;
376 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
377 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
378 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
379 virtual void PushState();
380 virtual void PopState();
382 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
383 wxDouble
*descent
, wxDouble
*externalLeading
) const;
384 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
385 virtual bool ShouldOffset() const;
386 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
388 Graphics
* GetGraphics() const { return m_context
; }
392 wxDouble m_fontScaleRatio
;
394 // Used from ctors (including those in the derived classes) and takes
395 // ownership of the graphics pointer that must be non-NULL.
396 void Init(Graphics
* graphics
, int width
, int height
);
399 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
);
402 wxStack
<GraphicsState
> m_stateStack
;
403 GraphicsState m_state1
;
404 GraphicsState m_state2
;
406 wxDECLARE_NO_COPY_CLASS(wxGDIPlusContext
);
411 class wxGDIPlusImageContext
: public wxGDIPlusContext
414 wxGDIPlusImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
415 wxGDIPlusContext(renderer
),
417 m_bitmap(renderer
, image
)
421 new Graphics(m_bitmap
.GetGDIPlusBitmap()),
427 virtual ~wxGDIPlusImageContext()
429 m_image
= m_bitmap
.ConvertToImage();
434 wxGDIPlusBitmapData m_bitmap
;
436 wxDECLARE_NO_COPY_CLASS(wxGDIPlusImageContext
);
439 #endif // wxUSE_IMAGE
441 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
444 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
448 virtual ~wxGDIPlusMeasuringContext()
450 ReleaseDC( NULL
, m_hdc
);
457 class wxGDIPlusPrintingContext
: public wxGDIPlusContext
460 wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
461 virtual ~wxGDIPlusPrintingContext() { }
465 //-----------------------------------------------------------------------------
466 // wxGDIPlusRenderer declaration
467 //-----------------------------------------------------------------------------
469 class wxGDIPlusRenderer
: public wxGraphicsRenderer
478 virtual ~wxGDIPlusRenderer()
488 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
490 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
492 #if wxUSE_PRINTING_ARCHITECTURE
493 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
496 #if wxUSE_ENH_METAFILE
497 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
500 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
502 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
504 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
507 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
508 #endif // wxUSE_IMAGE
510 virtual wxGraphicsContext
* CreateMeasuringContext();
514 virtual wxGraphicsPath
CreatePath();
518 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
519 wxDouble tx
=0.0, wxDouble ty
=0.0);
522 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
524 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
526 virtual wxGraphicsBrush
527 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
528 wxDouble x2
, wxDouble y2
,
529 const wxGraphicsGradientStops
& stops
);
531 virtual wxGraphicsBrush
532 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
533 wxDouble xc
, wxDouble yc
,
535 const wxGraphicsGradientStops
& stops
);
537 // create a native bitmap representation
538 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
540 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
541 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
542 #endif // wxUSE_IMAGE
544 virtual wxGraphicsFont
CreateFont( const wxFont
& font
,
545 const wxColour
& col
);
547 virtual wxGraphicsFont
CreateFont(double size
,
548 const wxString
& facename
,
549 int flags
= wxFONTFLAG_DEFAULT
,
550 const wxColour
& col
= *wxBLACK
);
552 // create a graphics bitmap from a native bitmap
553 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
555 // create a subimage from a native image representation
556 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
559 bool EnsureIsLoaded();
562 friend class wxGDIPlusRendererModule
;
566 ULONG_PTR m_gditoken
;
568 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
571 //-----------------------------------------------------------------------------
572 // wxGDIPlusPen implementation
573 //-----------------------------------------------------------------------------
575 wxGDIPlusPenData::~wxGDIPlusPenData()
582 void wxGDIPlusPenData::Init()
589 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
590 : wxGraphicsObjectRefData(renderer
)
593 m_width
= pen
.GetWidth();
597 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
600 switch ( pen
.GetCap() )
606 case wxCAP_PROJECTING
:
611 cap
= LineCapFlat
; // TODO verify
618 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
621 switch ( pen
.GetJoin() )
624 join
= LineJoinBevel
;
628 join
= LineJoinMiter
;
632 join
= LineJoinRound
;
636 join
= LineJoinMiter
;
640 m_pen
->SetLineJoin(join
);
642 m_pen
->SetDashStyle(DashStyleSolid
);
644 DashStyle dashStyle
= DashStyleSolid
;
645 switch ( pen
.GetStyle() )
647 case wxPENSTYLE_SOLID
:
650 case wxPENSTYLE_DOT
:
651 dashStyle
= DashStyleDot
;
654 case wxPENSTYLE_LONG_DASH
:
655 dashStyle
= DashStyleDash
; // TODO verify
658 case wxPENSTYLE_SHORT_DASH
:
659 dashStyle
= DashStyleDash
;
662 case wxPENSTYLE_DOT_DASH
:
663 dashStyle
= DashStyleDashDot
;
665 case wxPENSTYLE_USER_DASH
:
667 dashStyle
= DashStyleCustom
;
669 int count
= pen
.GetDashes( &dashes
);
670 if ((dashes
!= NULL
) && (count
> 0))
672 REAL
*userLengths
= new REAL
[count
];
673 for ( int i
= 0; i
< count
; ++i
)
675 userLengths
[i
] = dashes
[i
];
677 m_pen
->SetDashPattern( userLengths
, count
);
678 delete[] userLengths
;
682 case wxPENSTYLE_STIPPLE
:
684 wxBitmap
* bmp
= pen
.GetStipple();
685 if ( bmp
&& bmp
->IsOk() )
687 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
689 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
694 m_penBrush
= new TextureBrush(m_penImage
);
695 m_pen
->SetBrush( m_penBrush
);
701 if ( pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
&&
702 pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
705 switch( pen
.GetStyle() )
707 case wxPENSTYLE_BDIAGONAL_HATCH
:
708 style
= HatchStyleBackwardDiagonal
;
710 case wxPENSTYLE_CROSSDIAG_HATCH
:
711 style
= HatchStyleDiagonalCross
;
713 case wxPENSTYLE_FDIAGONAL_HATCH
:
714 style
= HatchStyleForwardDiagonal
;
716 case wxPENSTYLE_CROSS_HATCH
:
717 style
= HatchStyleCross
;
719 case wxPENSTYLE_HORIZONTAL_HATCH
:
720 style
= HatchStyleHorizontal
;
722 case wxPENSTYLE_VERTICAL_HATCH
:
723 style
= HatchStyleVertical
;
726 style
= HatchStyleHorizontal
;
728 m_penBrush
= new HatchBrush
731 wxColourToColor(pen
.GetColour()),
734 m_pen
->SetBrush( m_penBrush
);
738 if ( dashStyle
!= DashStyleSolid
)
739 m_pen
->SetDashStyle(dashStyle
);
742 //-----------------------------------------------------------------------------
743 // wxGDIPlusBrush implementation
744 //-----------------------------------------------------------------------------
746 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
747 : wxGraphicsObjectRefData(renderer
)
752 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
753 : wxGraphicsObjectRefData(renderer
)
756 if ( brush
.GetStyle() == wxSOLID
)
758 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
760 else if ( brush
.IsHatch() )
763 switch( brush
.GetStyle() )
765 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
766 style
= HatchStyleBackwardDiagonal
;
768 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
769 style
= HatchStyleDiagonalCross
;
771 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
772 style
= HatchStyleForwardDiagonal
;
774 case wxBRUSHSTYLE_CROSS_HATCH
:
775 style
= HatchStyleCross
;
777 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
778 style
= HatchStyleHorizontal
;
780 case wxBRUSHSTYLE_VERTICAL_HATCH
:
781 style
= HatchStyleVertical
;
784 style
= HatchStyleHorizontal
;
786 m_brush
= new HatchBrush
789 wxColourToColor(brush
.GetColour()),
795 wxBitmap
* bmp
= brush
.GetStipple();
796 if ( bmp
&& bmp
->IsOk() )
798 wxDELETE( m_brushImage
);
799 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
801 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
806 m_brush
= new TextureBrush(m_brushImage
);
811 wxGDIPlusBrushData::~wxGDIPlusBrushData()
818 void wxGDIPlusBrushData::Init()
825 template <typename T
>
827 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
828 const wxGraphicsGradientStops
& stops
)
830 const unsigned numStops
= stops
.GetCount();
833 // initial and final colours are set during the brush creation, nothing
838 wxVector
<Color
> colors(numStops
);
839 wxVector
<REAL
> positions(numStops
);
841 for ( unsigned i
= 0; i
< numStops
; i
++ )
843 wxGraphicsGradientStop stop
= stops
.Item(i
);
845 colors
[i
] = wxColourToColor(stop
.GetColour());
846 positions
[i
] = stop
.GetPosition();
849 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
853 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
854 wxDouble x2
, wxDouble y2
,
855 const wxGraphicsGradientStops
& stops
)
857 LinearGradientBrush
* const
858 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
859 wxColourToColor(stops
.GetStartColour()),
860 wxColourToColor(stops
.GetEndColour()));
863 SetGradientStops(brush
, stops
);
867 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
868 wxDouble xc
, wxDouble yc
,
870 const wxGraphicsGradientStops
& stops
)
872 m_brushPath
= new GraphicsPath();
873 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
874 (REAL
)(2*radius
), (REAL
)(2*radius
));
876 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
878 brush
->SetCenterPoint(PointF(xo
, yo
));
879 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
881 const Color
col(wxColourToColor(stops
.GetEndColour()));
883 brush
->SetSurroundColors(&col
, &count
);
885 SetGradientStops(brush
, stops
);
888 //-----------------------------------------------------------------------------
889 // wxGDIPlusFont implementation
890 //-----------------------------------------------------------------------------
893 wxGDIPlusFontData::Init(const wxString
& name
,
899 m_font
= new Font(name
.wc_str(), size
, style
, fontUnit
);
901 m_textBrush
= new SolidBrush(wxColourToColor(col
));
904 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
906 const wxColour
& col
)
907 : wxGraphicsObjectRefData( renderer
)
909 int style
= FontStyleRegular
;
910 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
911 style
|= FontStyleItalic
;
912 if ( font
.GetUnderlined() )
913 style
|= FontStyleUnderline
;
914 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
915 style
|= FontStyleBold
;
917 Init(font
.GetFaceName(), font
.GetPointSize(), style
, col
, UnitPoint
);
920 wxGDIPlusFontData::wxGDIPlusFontData(wxGraphicsRenderer
* renderer
,
921 const wxString
& name
,
924 const wxColour
& col
) :
925 wxGraphicsObjectRefData(renderer
)
927 Init(name
, sizeInPixels
, style
, col
, UnitPixel
);
930 wxGDIPlusFontData::~wxGDIPlusFontData()
936 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
937 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
938 // bytes as parameter, since there is no real copying of the data going in, only references are stored
939 // m_helper has to be kept alive as well
941 //-----------------------------------------------------------------------------
942 // wxGDIPlusBitmapData implementation
943 //-----------------------------------------------------------------------------
945 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
946 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
951 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
952 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
957 Bitmap
* image
= NULL
;
960 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
962 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
968 size_t width
= interim
.GetWidth();
969 size_t height
= interim
.GetHeight();
970 Rect
bounds(0,0,width
,height
);
972 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
974 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
975 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
977 BitmapData dataMask
;
978 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
979 interimMask
.GetPixelFormat(),&dataMask
);
982 BitmapData imageData
;
983 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
985 BYTE maskPattern
= 0 ;
989 for ( size_t y
= 0 ; y
< height
; ++y
)
992 for( size_t x
= 0 ; x
< width
; ++x
)
997 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
1001 maskPattern
= maskPattern
>> 1;
1003 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
1004 if ( (maskByte
& maskPattern
) == 0 )
1009 interim
.GetPixel(x
,y
,&c
) ;
1010 *dest
= (c
.GetValue() | Color::AlphaMask
);
1015 image
->UnlockBits(&imageData
);
1017 interimMask
.UnlockBits(&dataMask
);
1018 interim
.UnlockBits(&dataMask
);
1022 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
1024 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
1029 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
1031 size_t width
= image
->GetWidth();
1032 size_t height
= image
->GetHeight();
1033 Rect
bounds(0,0,width
,height
);
1034 static BitmapData data
;
1038 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
1039 m_helper
->GetPixelFormat(),&data
);
1041 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1042 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
1044 m_helper
->UnlockBits(&data
);
1053 wxImage
wxGDIPlusBitmapData::ConvertToImage() const
1055 // We could use Bitmap::LockBits() and convert to wxImage directly but
1056 // passing by wxBitmap is easier. It would be nice to measure performance
1057 // of the two methods but for this the second one would need to be written
1060 if ( m_bitmap
->GetHBITMAP(Color(0xffffffff), &hbmp
) != Gdiplus::Ok
)
1064 bmp
.SetWidth(m_bitmap
->GetWidth());
1065 bmp
.SetHeight(m_bitmap
->GetHeight());
1066 bmp
.SetHBITMAP(hbmp
);
1067 bmp
.SetDepth(IsAlphaPixelFormat(m_bitmap
->GetPixelFormat()) ? 32 : 24);
1068 return bmp
.ConvertToImage();
1071 #endif // wxUSE_IMAGE
1073 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1079 //-----------------------------------------------------------------------------
1080 // wxGDIPlusPath implementation
1081 //-----------------------------------------------------------------------------
1083 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
1088 m_path
= new GraphicsPath();
1091 wxGDIPlusPathData::~wxGDIPlusPathData()
1096 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
1098 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
1105 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1107 m_path
->StartFigure();
1108 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1111 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1113 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1116 void wxGDIPlusPathData::CloseSubpath()
1118 m_path
->CloseFigure();
1121 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1127 m_path
->GetLastPoint(&start
);
1128 m_path
->AddBezier(start
,c1
,c2
,end
);
1131 // gets the last point of the current path, (0,0) if not yet set
1132 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1135 m_path
->GetLastPoint(&start
);
1140 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1142 double sweepAngle
= endAngle
- startAngle
;
1143 if( fabs(sweepAngle
) >= 2*M_PI
)
1145 sweepAngle
= 2 * M_PI
;
1151 if( sweepAngle
< 0 )
1152 sweepAngle
+= 2 * M_PI
;
1156 if( sweepAngle
> 0 )
1157 sweepAngle
-= 2 * M_PI
;
1161 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
1164 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1166 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
1169 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
1171 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
1175 // transforms each point of this path by the matrix
1176 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1178 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
1181 // gets the bounding box enclosing all points (possibly including control points)
1182 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1185 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
1192 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1194 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1195 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
1198 //-----------------------------------------------------------------------------
1199 // wxGDIPlusMatrixData implementation
1200 //-----------------------------------------------------------------------------
1202 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
1203 : wxGraphicsMatrixData(renderer
)
1208 m_matrix
= new Matrix();
1211 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1216 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1218 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1221 // concatenates the matrix
1222 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1224 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1227 // sets the matrix to the respective values
1228 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1229 wxDouble tx
, wxDouble ty
)
1231 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1234 // gets the component valuess of the matrix
1235 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1236 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1239 m_matrix
->GetElements(elements
);
1240 if (a
) *a
= elements
[0];
1241 if (b
) *b
= elements
[1];
1242 if (c
) *c
= elements
[2];
1243 if (d
) *d
= elements
[3];
1244 if (tx
) *tx
= elements
[4];
1245 if (ty
) *ty
= elements
[5];
1248 // makes this the inverse matrix
1249 void wxGDIPlusMatrixData::Invert()
1254 // returns true if the elements of the transformation matrix are equal ?
1255 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1257 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1260 // return true if this is the identity matrix
1261 bool wxGDIPlusMatrixData::IsIdentity() const
1263 return m_matrix
->IsIdentity() == TRUE
;
1270 // add the translation to this matrix
1271 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1273 m_matrix
->Translate(dx
,dy
);
1276 // add the scale to this matrix
1277 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1279 m_matrix
->Scale(xScale
,yScale
);
1282 // add the rotation to this matrix (radians)
1283 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1285 m_matrix
->Rotate( RadToDeg(angle
) );
1289 // apply the transforms
1292 // applies that matrix to the point
1293 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1296 m_matrix
->TransformPoints(&pt
);
1301 // applies the matrix except for translations
1302 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1305 m_matrix
->TransformVectors(&pt
);
1310 // returns the native representation
1311 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1316 //-----------------------------------------------------------------------------
1317 // wxGDIPlusContext implementation
1318 //-----------------------------------------------------------------------------
1320 class wxGDIPlusOffsetHelper
1323 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1328 m_gr
->TranslateTransform( 0.5, 0.5 );
1330 ~wxGDIPlusOffsetHelper( )
1333 m_gr
->TranslateTransform( -0.5, -0.5 );
1340 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1341 : wxGraphicsContext(renderer
)
1343 Init(new Graphics(hdc
), width
, height
);
1346 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
)
1347 : wxGraphicsContext(renderer
)
1349 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1350 HDC hdc
= (HDC
) msw
->GetHDC();
1351 wxSize sz
= dc
.GetSize();
1353 Init(new Graphics(hdc
), sz
.x
, sz
.y
);
1356 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1357 : wxGraphicsContext(renderer
)
1359 RECT rect
= wxGetWindowRect(hwnd
);
1360 Init(new Graphics(hwnd
), rect
.right
- rect
.left
, rect
.bottom
- rect
.top
);
1361 m_enableOffset
= true;
1364 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1365 : wxGraphicsContext(renderer
)
1370 wxGDIPlusContext::wxGDIPlusContext(wxGraphicsRenderer
* renderer
)
1371 : wxGraphicsContext(renderer
)
1373 // Derived class must call Init() later but just set m_context to NULL for
1374 // safety to avoid crashing in our dtor if Init() ends up not being called.
1378 void wxGDIPlusContext::Init(Graphics
* graphics
, int width
, int height
)
1380 m_context
= graphics
;
1385 m_fontScaleRatio
= 1.0;
1387 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1388 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1389 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1390 m_context
->SetInterpolationMode(InterpolationModeHighQuality
);
1391 m_state1
= m_context
->Save();
1392 m_state2
= m_context
->Save();
1395 wxGDIPlusContext::~wxGDIPlusContext()
1399 m_context
->Restore( m_state2
);
1400 m_context
->Restore( m_state1
);
1406 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1408 Region
rgn((HRGN
)region
.GetHRGN());
1409 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1412 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1414 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1417 void wxGDIPlusContext::ResetClip()
1419 m_context
->ResetClip();
1422 void wxGDIPlusContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1424 if (m_composition
== wxCOMPOSITION_DEST
)
1427 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1428 Brush
*brush
= m_brush
.IsNull() ? NULL
: ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush();
1429 Pen
*pen
= m_pen
.IsNull() ? NULL
: ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen();
1433 // the offset is used to fill only the inside of the rectangle and not paint underneath
1434 // its border which may influence a transparent Pen
1437 offset
= pen
->GetWidth();
1438 m_context
->FillRectangle( brush
, (REAL
)x
+ offset
/2, (REAL
)y
+ offset
/2, (REAL
)w
- offset
, (REAL
)h
- offset
);
1443 m_context
->DrawRectangle( pen
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1447 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1449 if (m_composition
== wxCOMPOSITION_DEST
)
1452 if ( !m_pen
.IsNull() )
1454 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1455 Point
*cpoints
= new Point
[n
];
1456 for (size_t i
= 0; i
< n
; i
++)
1458 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1459 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1461 } // for (size_t i = 0; i < n; i++)
1462 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1467 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1469 if (m_composition
== wxCOMPOSITION_DEST
)
1472 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1473 Point
*cpoints
= new Point
[n
];
1474 for (size_t i
= 0; i
< n
; i
++)
1476 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1477 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1479 } // for (int i = 0; i < n; i++)
1480 if ( !m_brush
.IsNull() )
1481 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1482 if ( !m_pen
.IsNull() )
1483 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1487 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1489 if (m_composition
== wxCOMPOSITION_DEST
)
1492 if ( !m_pen
.IsNull() )
1494 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1495 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1499 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1501 if (m_composition
== wxCOMPOSITION_DEST
)
1504 if ( !m_brush
.IsNull() )
1506 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1507 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1508 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1509 (GraphicsPath
*) path
.GetNativePath());
1513 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1515 if (m_antialias
== antialias
)
1518 m_antialias
= antialias
;
1520 SmoothingMode antialiasMode
;
1523 case wxANTIALIAS_DEFAULT
:
1524 antialiasMode
= SmoothingModeHighQuality
;
1526 case wxANTIALIAS_NONE
:
1527 antialiasMode
= SmoothingModeNone
;
1532 m_context
->SetSmoothingMode(antialiasMode
);
1536 bool wxGDIPlusContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1538 if (m_interpolation
== interpolation
)
1541 InterpolationMode interpolationMode
= InterpolationModeDefault
;
1542 switch (interpolation
)
1544 case wxINTERPOLATION_DEFAULT
:
1545 interpolationMode
= InterpolationModeDefault
;
1548 case wxINTERPOLATION_NONE
:
1549 interpolationMode
= InterpolationModeNearestNeighbor
;
1552 case wxINTERPOLATION_FAST
:
1553 interpolationMode
= InterpolationModeLowQuality
;
1556 case wxINTERPOLATION_GOOD
:
1557 interpolationMode
= InterpolationModeHighQuality
;
1560 case wxINTERPOLATION_BEST
:
1561 interpolationMode
= InterpolationModeHighQualityBicubic
;
1568 if ( m_context
->SetInterpolationMode(interpolationMode
) != Gdiplus::Ok
)
1571 m_interpolation
= interpolation
;
1576 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1578 if ( m_composition
== op
)
1583 if (m_composition
== wxCOMPOSITION_DEST
)
1586 CompositingMode cop
;
1589 case wxCOMPOSITION_SOURCE
:
1590 cop
= CompositingModeSourceCopy
;
1592 case wxCOMPOSITION_OVER
:
1593 cop
= CompositingModeSourceOver
;
1599 m_context
->SetCompositingMode(cop
);
1603 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1608 void wxGDIPlusContext::EndLayer()
1613 void wxGDIPlusContext::Rotate( wxDouble angle
)
1615 m_context
->RotateTransform( RadToDeg(angle
) );
1618 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1620 m_context
->TranslateTransform( dx
, dy
);
1623 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1625 m_context
->ScaleTransform(xScale
,yScale
);
1628 void wxGDIPlusContext::PushState()
1630 GraphicsState state
= m_context
->Save();
1631 m_stateStack
.push(state
);
1634 void wxGDIPlusContext::PopState()
1636 wxCHECK_RET( !m_stateStack
.empty(), wxT("No state to pop") );
1638 GraphicsState state
= m_stateStack
.top();
1640 m_context
->Restore(state
);
1643 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1645 if (m_composition
== wxCOMPOSITION_DEST
)
1648 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1651 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1653 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1654 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1655 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth(), image
->GetHeight(), UnitPixel
) ;
1656 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1659 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1663 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1665 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1666 DrawBitmap(bitmap
, x
, y
, w
, h
);
1669 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1671 if (m_composition
== wxCOMPOSITION_DEST
)
1674 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1675 // find out by looking at the bitmap data whether there really was alpha in it
1676 HICON hIcon
= (HICON
)icon
.GetHICON();
1678 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1679 if (!GetIconInfo(hIcon
,&iconInfo
))
1682 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1684 Bitmap
* image
= NULL
;
1686 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1687 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1688 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1690 image
= Bitmap::FromHICON(hIcon
);
1694 size_t width
= interim
.GetWidth();
1695 size_t height
= interim
.GetHeight();
1696 Rect
bounds(0,0,width
,height
);
1699 interim
.LockBits(&bounds
, ImageLockModeRead
,
1700 interim
.GetPixelFormat(),&data
);
1702 bool hasAlpha
= false;
1703 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1705 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1707 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1708 if ( ( *dest
& Color::AlphaMask
) != 0 )
1715 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1716 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1720 image
= Bitmap::FromHICON(hIcon
);
1723 interim
.UnlockBits(&data
);
1726 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1729 DeleteObject(iconInfo
.hbmColor
);
1730 DeleteObject(iconInfo
.hbmMask
);
1733 void wxGDIPlusContext::DoDrawText(const wxString
& str
,
1734 wxDouble x
, wxDouble y
)
1736 if (m_composition
== wxCOMPOSITION_DEST
)
1739 wxCHECK_RET( !m_font
.IsNull(),
1740 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1745 wxGDIPlusFontData
* const
1746 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1748 m_context
->DrawString
1750 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1751 -1, // length: string is NUL-terminated
1752 fontData
->GetGDIPlusFont(),
1754 StringFormat::GenericTypographic(),
1755 fontData
->GetGDIPlusBrush()
1759 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1760 wxDouble
*descent
, wxDouble
*externalLeading
) const
1762 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1764 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1765 FontFamily ffamily
;
1766 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1768 f
->GetFamily(&ffamily
) ;
1770 REAL factorY
= m_fontScaleRatio
;
1772 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1773 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1774 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1775 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1776 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1777 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1780 *height
= rHeight
* factorY
;
1782 *descent
= rDescent
* factorY
;
1783 if ( externalLeading
)
1784 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1785 // measuring empty strings is not guaranteed, so do it by hand
1793 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1794 StringFormat
strFormat( StringFormat::GenericTypographic() );
1795 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1798 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1800 *width
= bounds
.Width
;
1802 *height
= bounds
.Height
;
1806 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1809 widths
.Add(0, text
.length());
1811 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1816 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1817 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1818 size_t len
= wcslen( ws
) ;
1819 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1821 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1822 StringFormat
strFormat( StringFormat::GenericTypographic() );
1824 size_t startPosition
= 0;
1825 size_t remainder
= len
;
1826 const size_t maxSpan
= 32;
1827 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1828 Region
* regions
= new Region
[maxSpan
];
1830 while( remainder
> 0 )
1832 size_t span
= wxMin( maxSpan
, remainder
);
1834 for( size_t i
= 0 ; i
< span
; ++i
)
1836 ranges
[i
].First
= 0 ;
1837 ranges
[i
].Length
= startPosition
+i
+1 ;
1839 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1840 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1841 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1844 for ( size_t i
= 0 ; i
< span
; ++i
)
1846 regions
[i
].GetBounds(&bbox
,m_context
);
1847 widths
[startPosition
+i
] = bbox
.Width
;
1850 startPosition
+= span
;
1857 bool wxGDIPlusContext::ShouldOffset() const
1859 if ( !m_enableOffset
)
1863 if ( !m_pen
.IsNull() )
1865 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1866 if ( penwidth
== 0 )
1869 return ( penwidth
% 2 ) == 1;
1872 void* wxGDIPlusContext::GetNativeContext()
1877 // concatenates this transform with the current transform of this context
1878 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1880 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1883 // sets the transform of this context
1884 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1886 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1889 // gets the matrix of this context
1890 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1892 wxGraphicsMatrix matrix
= CreateMatrix();
1893 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1897 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1903 //-----------------------------------------------------------------------------
1904 // wxGDIPlusPrintingContext implementation
1905 //-----------------------------------------------------------------------------
1907 wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
,
1909 : wxGDIPlusContext(renderer
, dc
)
1911 Graphics
* context
= GetGraphics();
1913 //m_context->SetPageUnit(UnitDocument);
1915 // Setup page scale, based on DPI ratio.
1916 // Antecedent should be 100dpi when the default page unit
1917 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1918 // instead. Note that calling SetPageScale() does not have effect on
1919 // non-printing DCs (that is, any other than wxPrinterDC or
1920 // wxEnhMetaFileDC).
1921 REAL dpiRatio
= 100.0 / context
->GetDpiY();
1922 context
->SetPageScale(dpiRatio
);
1924 // We use this modifier when measuring fonts. It is needed because the
1925 // page scale is modified above.
1926 m_fontScaleRatio
= context
->GetDpiY() / 72.0;
1929 //-----------------------------------------------------------------------------
1930 // wxGDIPlusRenderer implementation
1931 //-----------------------------------------------------------------------------
1933 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1935 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1937 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1939 return &gs_GDIPlusRenderer
;
1942 bool wxGDIPlusRenderer::EnsureIsLoaded()
1944 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1945 // if we already tried and failed (we don't want to spend lot of time
1946 // returning NULL from wxGraphicsContext::Create(), which may be called
1947 // relatively frequently):
1948 if ( m_loaded
== -1 )
1953 return m_loaded
== 1;
1956 // call EnsureIsLoaded() and return returnOnFail value if it fails
1957 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1958 if ( !EnsureIsLoaded() ) \
1959 return (returnOnFail)
1962 void wxGDIPlusRenderer::Load()
1964 GdiplusStartupInput input
;
1965 GdiplusStartupOutput output
;
1966 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1968 wxLogTrace("gdiplus", "successfully initialized GDI+");
1973 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1978 void wxGDIPlusRenderer::Unload()
1982 GdiplusShutdown(m_gditoken
);
1985 m_loaded
= -1; // next Load() will try again
1988 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1990 ENSURE_LOADED_OR_RETURN(NULL
);
1991 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1992 context
->EnableOffset(true);
1996 #if wxUSE_PRINTING_ARCHITECTURE
1997 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1999 ENSURE_LOADED_OR_RETURN(NULL
);
2000 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
2005 #if wxUSE_ENH_METAFILE
2006 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
2008 ENSURE_LOADED_OR_RETURN(NULL
);
2009 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
2014 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
2016 ENSURE_LOADED_OR_RETURN(NULL
);
2017 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
2018 context
->EnableOffset(true);
2023 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromImage(wxImage
& image
)
2025 ENSURE_LOADED_OR_RETURN(NULL
);
2026 wxGDIPlusContext
* context
= new wxGDIPlusImageContext(this, image
);
2027 context
->EnableOffset(true);
2031 #endif // wxUSE_IMAGE
2033 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
2035 ENSURE_LOADED_OR_RETURN(NULL
);
2036 return new wxGDIPlusMeasuringContext(this);
2039 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
2041 ENSURE_LOADED_OR_RETURN(NULL
);
2042 return new wxGDIPlusContext(this,(Graphics
*) context
);
2046 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
2048 ENSURE_LOADED_OR_RETURN(NULL
);
2049 return new wxGDIPlusContext(this,(HWND
) window
);
2052 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
2054 ENSURE_LOADED_OR_RETURN(NULL
);
2055 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
2060 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
2062 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2064 m
.SetRefData( new wxGDIPlusPathData(this));
2071 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2072 wxDouble tx
, wxDouble ty
)
2075 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2077 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
2078 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2083 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
2085 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2086 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
2087 return wxNullGraphicsPen
;
2091 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
2096 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
2098 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2099 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
2100 return wxNullGraphicsBrush
;
2104 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
2110 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2111 wxDouble x2
, wxDouble y2
,
2112 const wxGraphicsGradientStops
& stops
)
2114 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2116 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2117 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2123 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2124 wxDouble xc
, wxDouble yc
,
2126 const wxGraphicsGradientStops
& stops
)
2128 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2130 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2131 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
2137 wxGDIPlusRenderer::CreateFont( const wxFont
&font
,
2138 const wxColour
&col
)
2140 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2144 p
.SetRefData(new wxGDIPlusFontData( this, font
, col
));
2148 return wxNullGraphicsFont
;
2152 wxGDIPlusRenderer::CreateFont(double size
,
2153 const wxString
& facename
,
2155 const wxColour
& col
)
2157 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2159 // Convert wxFont flags to GDI+ style:
2160 int style
= FontStyleRegular
;
2161 if ( flags
& wxFONTFLAG_ITALIC
)
2162 style
|= FontStyleItalic
;
2163 if ( flags
& wxFONTFLAG_UNDERLINED
)
2164 style
|= FontStyleUnderline
;
2165 if ( flags
& wxFONTFLAG_BOLD
)
2166 style
|= FontStyleBold
;
2167 if ( flags
& wxFONTFLAG_STRIKETHROUGH
)
2168 style
|= FontStyleStrikeout
;
2172 f
.SetRefData(new wxGDIPlusFontData(this, facename
, size
, style
, col
));
2176 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
2178 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2179 if ( bitmap
.IsOk() )
2182 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
2186 return wxNullGraphicsBitmap
;
2191 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromImage(const wxImage
& image
)
2193 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2196 // Notice that we rely on conversion from wxImage to wxBitmap here but
2197 // we could probably do it more efficiently by converting from wxImage
2198 // to GDI+ Bitmap directly, i.e. copying wxImage pixels to the buffer
2199 // returned by Bitmap::LockBits(). However this would require writing
2200 // code specific for this task while like this we can reuse existing
2201 // code (see also wxGDIPlusBitmapData::ConvertToImage()).
2202 wxGraphicsBitmap gb
;
2203 gb
.SetRefData(new wxGDIPlusBitmapData(this, image
));
2207 return wxNullGraphicsBitmap
;
2211 wxImage
wxGDIPlusRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
2213 ENSURE_LOADED_OR_RETURN(wxNullImage
);
2214 const wxGDIPlusBitmapData
* const
2215 data
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetGraphicsData());
2217 return data
? data
->ConvertToImage() : wxNullImage
;
2220 #endif // wxUSE_IMAGE
2223 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
2225 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2226 if ( bitmap
!= NULL
)
2229 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
2233 return wxNullGraphicsBitmap
;
2236 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2238 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2239 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
2243 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
2247 return wxNullGraphicsBitmap
;
2250 // Shutdown GDI+ at app exit, before possible dll unload
2251 class wxGDIPlusRendererModule
: public wxModule
2254 virtual bool OnInit() { return true; }
2255 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
2258 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
2261 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
2263 // ----------------------------------------------------------------------------
2264 // wxMSW-specific parts of wxGCDC
2265 // ----------------------------------------------------------------------------
2267 WXHDC
wxGCDC::AcquireHDC()
2269 wxGraphicsContext
* const gc
= GetGraphicsContext();
2274 // we can't get the HDC if it is not a GDI+ context
2275 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2276 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2281 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2282 return g
? g
->GetHDC() : NULL
;
2285 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2290 wxGraphicsContext
* const gc
= GetGraphicsContext();
2291 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2294 // we can't get the HDC if it is not a GDI+ context
2295 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2296 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2301 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2302 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2304 g
->ReleaseHDC((HDC
)hdc
);
2307 #endif // wxUSE_GRAPHICS_CONTEXT