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 wxGraphicsBitmapData
278 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
);
279 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
&bmp
);
280 ~wxGDIPlusBitmapData ();
282 virtual Bitmap
* GetGDIPlusBitmap() { return m_bitmap
; }
283 virtual void* GetNativeBitmap() const { return m_bitmap
; }
286 wxImage
ConvertToImage() const;
287 #endif // wxUSE_IMAGE
294 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
297 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
299 const wxColour
& col
);
300 wxGDIPlusFontData(wxGraphicsRenderer
* renderer
,
301 const wxString
& name
,
304 const wxColour
& col
);
305 ~wxGDIPlusFontData();
307 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
308 virtual Font
* GetGDIPlusFont() { return m_font
; }
311 // Common part of all ctors, flags here is a combination of values of
312 // FontStyle GDI+ enum.
313 void Init(const wxString
& name
,
323 class wxGDIPlusContext
: public wxGraphicsContext
326 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
327 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
328 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
329 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
330 wxGDIPlusContext(wxGraphicsRenderer
* renderer
);
332 virtual ~wxGDIPlusContext();
334 virtual void Clip( const wxRegion
®ion
);
335 // clips drawings to the rect
336 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
338 // resets the clipping to original extent
339 virtual void ResetClip();
341 virtual void * GetNativeContext();
343 virtual void StrokePath( const wxGraphicsPath
& p
);
344 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
346 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
348 // stroke lines connecting each of the points
349 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
352 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
354 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
356 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
358 virtual bool SetCompositionMode(wxCompositionMode op
);
360 virtual void BeginLayer(wxDouble opacity
);
362 virtual void EndLayer();
364 virtual void Translate( wxDouble dx
, wxDouble dy
);
365 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
366 virtual void Rotate( wxDouble angle
);
368 // concatenates this transform with the current transform of this context
369 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
371 // sets the transform of this context
372 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
374 // gets the matrix of this context
375 virtual wxGraphicsMatrix
GetTransform() const;
377 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
378 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
379 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
380 virtual void PushState();
381 virtual void PopState();
383 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
384 wxDouble
*descent
, wxDouble
*externalLeading
) const;
385 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
386 virtual bool ShouldOffset() const;
387 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
389 Graphics
* GetGraphics() const { return m_context
; }
393 wxDouble m_fontScaleRatio
;
395 // Used from ctors (including those in the derived classes) and takes
396 // ownership of the graphics pointer that must be non-NULL.
397 void Init(Graphics
* graphics
, int width
, int height
);
400 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
);
403 wxStack
<GraphicsState
> m_stateStack
;
404 GraphicsState m_state1
;
405 GraphicsState m_state2
;
407 wxDECLARE_NO_COPY_CLASS(wxGDIPlusContext
);
412 class wxGDIPlusImageContext
: public wxGDIPlusContext
415 wxGDIPlusImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
416 wxGDIPlusContext(renderer
),
418 m_bitmap(renderer
, image
)
422 new Graphics(m_bitmap
.GetGDIPlusBitmap()),
428 virtual ~wxGDIPlusImageContext()
430 m_image
= m_bitmap
.ConvertToImage();
435 wxGDIPlusBitmapData m_bitmap
;
437 wxDECLARE_NO_COPY_CLASS(wxGDIPlusImageContext
);
440 #endif // wxUSE_IMAGE
442 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
445 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
449 virtual ~wxGDIPlusMeasuringContext()
451 ReleaseDC( NULL
, m_hdc
);
458 class wxGDIPlusPrintingContext
: public wxGDIPlusContext
461 wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
462 virtual ~wxGDIPlusPrintingContext() { }
466 //-----------------------------------------------------------------------------
467 // wxGDIPlusRenderer declaration
468 //-----------------------------------------------------------------------------
470 class wxGDIPlusRenderer
: public wxGraphicsRenderer
479 virtual ~wxGDIPlusRenderer()
489 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
491 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
493 #if wxUSE_PRINTING_ARCHITECTURE
494 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
497 #if wxUSE_ENH_METAFILE
498 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
501 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
503 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
505 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
508 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
509 #endif // wxUSE_IMAGE
511 virtual wxGraphicsContext
* CreateMeasuringContext();
515 virtual wxGraphicsPath
CreatePath();
519 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
520 wxDouble tx
=0.0, wxDouble ty
=0.0);
523 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
525 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
527 virtual wxGraphicsBrush
528 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
529 wxDouble x2
, wxDouble y2
,
530 const wxGraphicsGradientStops
& stops
);
532 virtual wxGraphicsBrush
533 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
534 wxDouble xc
, wxDouble yc
,
536 const wxGraphicsGradientStops
& stops
);
538 // create a native bitmap representation
539 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
541 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
542 virtual wxImage
CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
);
543 #endif // wxUSE_IMAGE
545 virtual wxGraphicsFont
CreateFont( const wxFont
& font
,
546 const wxColour
& col
);
548 virtual wxGraphicsFont
CreateFont(double size
,
549 const wxString
& facename
,
550 int flags
= wxFONTFLAG_DEFAULT
,
551 const wxColour
& col
= *wxBLACK
);
553 // create a graphics bitmap from a native bitmap
554 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
556 // create a subimage from a native image representation
557 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
560 bool EnsureIsLoaded();
563 friend class wxGDIPlusRendererModule
;
567 ULONG_PTR m_gditoken
;
569 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
572 //-----------------------------------------------------------------------------
573 // wxGDIPlusPen implementation
574 //-----------------------------------------------------------------------------
576 wxGDIPlusPenData::~wxGDIPlusPenData()
583 void wxGDIPlusPenData::Init()
590 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
591 : wxGraphicsObjectRefData(renderer
)
594 m_width
= pen
.GetWidth();
598 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
601 switch ( pen
.GetCap() )
607 case wxCAP_PROJECTING
:
612 cap
= LineCapFlat
; // TODO verify
619 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
622 switch ( pen
.GetJoin() )
625 join
= LineJoinBevel
;
629 join
= LineJoinMiter
;
633 join
= LineJoinRound
;
637 join
= LineJoinMiter
;
641 m_pen
->SetLineJoin(join
);
643 m_pen
->SetDashStyle(DashStyleSolid
);
645 DashStyle dashStyle
= DashStyleSolid
;
646 switch ( pen
.GetStyle() )
648 case wxPENSTYLE_SOLID
:
651 case wxPENSTYLE_DOT
:
652 dashStyle
= DashStyleDot
;
655 case wxPENSTYLE_LONG_DASH
:
656 dashStyle
= DashStyleDash
; // TODO verify
659 case wxPENSTYLE_SHORT_DASH
:
660 dashStyle
= DashStyleDash
;
663 case wxPENSTYLE_DOT_DASH
:
664 dashStyle
= DashStyleDashDot
;
666 case wxPENSTYLE_USER_DASH
:
668 dashStyle
= DashStyleCustom
;
670 int count
= pen
.GetDashes( &dashes
);
671 if ((dashes
!= NULL
) && (count
> 0))
673 REAL
*userLengths
= new REAL
[count
];
674 for ( int i
= 0; i
< count
; ++i
)
676 userLengths
[i
] = dashes
[i
];
678 m_pen
->SetDashPattern( userLengths
, count
);
679 delete[] userLengths
;
683 case wxPENSTYLE_STIPPLE
:
685 wxBitmap
* bmp
= pen
.GetStipple();
686 if ( bmp
&& bmp
->IsOk() )
688 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
690 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
695 m_penBrush
= new TextureBrush(m_penImage
);
696 m_pen
->SetBrush( m_penBrush
);
702 if ( pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
&&
703 pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
706 switch( pen
.GetStyle() )
708 case wxPENSTYLE_BDIAGONAL_HATCH
:
709 style
= HatchStyleBackwardDiagonal
;
711 case wxPENSTYLE_CROSSDIAG_HATCH
:
712 style
= HatchStyleDiagonalCross
;
714 case wxPENSTYLE_FDIAGONAL_HATCH
:
715 style
= HatchStyleForwardDiagonal
;
717 case wxPENSTYLE_CROSS_HATCH
:
718 style
= HatchStyleCross
;
720 case wxPENSTYLE_HORIZONTAL_HATCH
:
721 style
= HatchStyleHorizontal
;
723 case wxPENSTYLE_VERTICAL_HATCH
:
724 style
= HatchStyleVertical
;
727 style
= HatchStyleHorizontal
;
729 m_penBrush
= new HatchBrush
732 wxColourToColor(pen
.GetColour()),
735 m_pen
->SetBrush( m_penBrush
);
739 if ( dashStyle
!= DashStyleSolid
)
740 m_pen
->SetDashStyle(dashStyle
);
743 //-----------------------------------------------------------------------------
744 // wxGDIPlusBrush implementation
745 //-----------------------------------------------------------------------------
747 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
748 : wxGraphicsObjectRefData(renderer
)
753 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
754 : wxGraphicsObjectRefData(renderer
)
757 if ( brush
.GetStyle() == wxSOLID
)
759 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
761 else if ( brush
.IsHatch() )
764 switch( brush
.GetStyle() )
766 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
767 style
= HatchStyleBackwardDiagonal
;
769 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
770 style
= HatchStyleDiagonalCross
;
772 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
773 style
= HatchStyleForwardDiagonal
;
775 case wxBRUSHSTYLE_CROSS_HATCH
:
776 style
= HatchStyleCross
;
778 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
779 style
= HatchStyleHorizontal
;
781 case wxBRUSHSTYLE_VERTICAL_HATCH
:
782 style
= HatchStyleVertical
;
785 style
= HatchStyleHorizontal
;
787 m_brush
= new HatchBrush
790 wxColourToColor(brush
.GetColour()),
796 wxBitmap
* bmp
= brush
.GetStipple();
797 if ( bmp
&& bmp
->IsOk() )
799 wxDELETE( m_brushImage
);
800 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
802 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
807 m_brush
= new TextureBrush(m_brushImage
);
812 wxGDIPlusBrushData::~wxGDIPlusBrushData()
819 void wxGDIPlusBrushData::Init()
826 template <typename T
>
828 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
829 const wxGraphicsGradientStops
& stops
)
831 const unsigned numStops
= stops
.GetCount();
834 // initial and final colours are set during the brush creation, nothing
839 wxVector
<Color
> colors(numStops
);
840 wxVector
<REAL
> positions(numStops
);
842 for ( unsigned i
= 0; i
< numStops
; i
++ )
844 wxGraphicsGradientStop stop
= stops
.Item(i
);
846 colors
[i
] = wxColourToColor(stop
.GetColour());
847 positions
[i
] = stop
.GetPosition();
850 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
854 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
855 wxDouble x2
, wxDouble y2
,
856 const wxGraphicsGradientStops
& stops
)
858 LinearGradientBrush
* const
859 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
860 wxColourToColor(stops
.GetStartColour()),
861 wxColourToColor(stops
.GetEndColour()));
864 SetGradientStops(brush
, stops
);
868 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
869 wxDouble xc
, wxDouble yc
,
871 const wxGraphicsGradientStops
& stops
)
873 m_brushPath
= new GraphicsPath();
874 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
875 (REAL
)(2*radius
), (REAL
)(2*radius
));
877 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
879 brush
->SetCenterPoint(PointF(xo
, yo
));
880 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
882 const Color
col(wxColourToColor(stops
.GetEndColour()));
884 brush
->SetSurroundColors(&col
, &count
);
886 SetGradientStops(brush
, stops
);
889 //-----------------------------------------------------------------------------
890 // wxGDIPlusFont implementation
891 //-----------------------------------------------------------------------------
894 wxGDIPlusFontData::Init(const wxString
& name
,
900 m_font
= new Font(name
.wc_str(), size
, style
, fontUnit
);
902 m_textBrush
= new SolidBrush(wxColourToColor(col
));
905 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
907 const wxColour
& col
)
908 : wxGraphicsObjectRefData( renderer
)
910 int style
= FontStyleRegular
;
911 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
912 style
|= FontStyleItalic
;
913 if ( font
.GetUnderlined() )
914 style
|= FontStyleUnderline
;
915 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
916 style
|= FontStyleBold
;
918 Init(font
.GetFaceName(), font
.GetPointSize(), style
, col
, UnitPoint
);
921 wxGDIPlusFontData::wxGDIPlusFontData(wxGraphicsRenderer
* renderer
,
922 const wxString
& name
,
925 const wxColour
& col
) :
926 wxGraphicsObjectRefData(renderer
)
928 Init(name
, sizeInPixels
, style
, col
, UnitPixel
);
931 wxGDIPlusFontData::~wxGDIPlusFontData()
937 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
938 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
939 // bytes as parameter, since there is no real copying of the data going in, only references are stored
940 // m_helper has to be kept alive as well
942 //-----------------------------------------------------------------------------
943 // wxGDIPlusBitmapData implementation
944 //-----------------------------------------------------------------------------
946 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
947 wxGraphicsBitmapData( renderer
), m_bitmap( bitmap
)
952 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
953 const wxBitmap
&bmp
) : wxGraphicsBitmapData( renderer
)
958 Bitmap
* image
= NULL
;
961 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
963 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
969 size_t width
= interim
.GetWidth();
970 size_t height
= interim
.GetHeight();
971 Rect
bounds(0,0,width
,height
);
973 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
975 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
976 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
978 BitmapData dataMask
;
979 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
980 interimMask
.GetPixelFormat(),&dataMask
);
983 BitmapData imageData
;
984 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
986 BYTE maskPattern
= 0 ;
990 for ( size_t y
= 0 ; y
< height
; ++y
)
993 for( size_t x
= 0 ; x
< width
; ++x
)
998 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
1002 maskPattern
= maskPattern
>> 1;
1004 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
1005 if ( (maskByte
& maskPattern
) == 0 )
1010 interim
.GetPixel(x
,y
,&c
) ;
1011 *dest
= (c
.GetValue() | Color::AlphaMask
);
1016 image
->UnlockBits(&imageData
);
1018 interimMask
.UnlockBits(&dataMask
);
1019 interim
.UnlockBits(&dataMask
);
1023 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
1025 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
1030 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
1032 size_t width
= image
->GetWidth();
1033 size_t height
= image
->GetHeight();
1034 Rect
bounds(0,0,width
,height
);
1035 static BitmapData data
;
1039 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
1040 m_helper
->GetPixelFormat(),&data
);
1042 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1043 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
1045 m_helper
->UnlockBits(&data
);
1054 wxImage
wxGDIPlusBitmapData::ConvertToImage() const
1056 // We could use Bitmap::LockBits() and convert to wxImage directly but
1057 // passing by wxBitmap is easier. It would be nice to measure performance
1058 // of the two methods but for this the second one would need to be written
1061 if ( m_bitmap
->GetHBITMAP(Color(0xffffffff), &hbmp
) != Gdiplus::Ok
)
1065 bmp
.SetWidth(m_bitmap
->GetWidth());
1066 bmp
.SetHeight(m_bitmap
->GetHeight());
1067 bmp
.SetHBITMAP(hbmp
);
1068 bmp
.SetDepth(IsAlphaPixelFormat(m_bitmap
->GetPixelFormat()) ? 32 : 24);
1069 return bmp
.ConvertToImage();
1072 #endif // wxUSE_IMAGE
1074 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1080 //-----------------------------------------------------------------------------
1081 // wxGDIPlusPath implementation
1082 //-----------------------------------------------------------------------------
1084 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
1089 m_path
= new GraphicsPath();
1092 wxGDIPlusPathData::~wxGDIPlusPathData()
1097 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
1099 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
1106 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1108 m_path
->StartFigure();
1109 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1112 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1114 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1117 void wxGDIPlusPathData::CloseSubpath()
1119 m_path
->CloseFigure();
1122 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1128 m_path
->GetLastPoint(&start
);
1129 m_path
->AddBezier(start
,c1
,c2
,end
);
1132 // gets the last point of the current path, (0,0) if not yet set
1133 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1136 m_path
->GetLastPoint(&start
);
1141 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1143 double sweepAngle
= endAngle
- startAngle
;
1144 if( fabs(sweepAngle
) >= 2*M_PI
)
1146 sweepAngle
= 2 * M_PI
;
1152 if( sweepAngle
< 0 )
1153 sweepAngle
+= 2 * M_PI
;
1157 if( sweepAngle
> 0 )
1158 sweepAngle
-= 2 * M_PI
;
1162 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
1165 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1167 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
1170 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
1172 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
1176 // transforms each point of this path by the matrix
1177 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1179 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
1182 // gets the bounding box enclosing all points (possibly including control points)
1183 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1186 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
1193 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1195 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1196 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
1199 //-----------------------------------------------------------------------------
1200 // wxGDIPlusMatrixData implementation
1201 //-----------------------------------------------------------------------------
1203 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
1204 : wxGraphicsMatrixData(renderer
)
1209 m_matrix
= new Matrix();
1212 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1217 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1219 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1222 // concatenates the matrix
1223 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1225 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1228 // sets the matrix to the respective values
1229 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1230 wxDouble tx
, wxDouble ty
)
1232 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1235 // gets the component valuess of the matrix
1236 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1237 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1240 m_matrix
->GetElements(elements
);
1241 if (a
) *a
= elements
[0];
1242 if (b
) *b
= elements
[1];
1243 if (c
) *c
= elements
[2];
1244 if (d
) *d
= elements
[3];
1245 if (tx
) *tx
= elements
[4];
1246 if (ty
) *ty
= elements
[5];
1249 // makes this the inverse matrix
1250 void wxGDIPlusMatrixData::Invert()
1255 // returns true if the elements of the transformation matrix are equal ?
1256 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1258 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1261 // return true if this is the identity matrix
1262 bool wxGDIPlusMatrixData::IsIdentity() const
1264 return m_matrix
->IsIdentity() == TRUE
;
1271 // add the translation to this matrix
1272 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1274 m_matrix
->Translate(dx
,dy
);
1277 // add the scale to this matrix
1278 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1280 m_matrix
->Scale(xScale
,yScale
);
1283 // add the rotation to this matrix (radians)
1284 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1286 m_matrix
->Rotate( RadToDeg(angle
) );
1290 // apply the transforms
1293 // applies that matrix to the point
1294 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1297 m_matrix
->TransformPoints(&pt
);
1302 // applies the matrix except for translations
1303 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1306 m_matrix
->TransformVectors(&pt
);
1311 // returns the native representation
1312 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1317 //-----------------------------------------------------------------------------
1318 // wxGDIPlusContext implementation
1319 //-----------------------------------------------------------------------------
1321 class wxGDIPlusOffsetHelper
1324 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1329 m_gr
->TranslateTransform( 0.5, 0.5 );
1331 ~wxGDIPlusOffsetHelper( )
1334 m_gr
->TranslateTransform( -0.5, -0.5 );
1341 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1342 : wxGraphicsContext(renderer
)
1344 Init(new Graphics(hdc
), width
, height
);
1347 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
)
1348 : wxGraphicsContext(renderer
)
1350 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1351 HDC hdc
= (HDC
) msw
->GetHDC();
1352 wxSize sz
= dc
.GetSize();
1354 Init(new Graphics(hdc
), sz
.x
, sz
.y
);
1357 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1358 : wxGraphicsContext(renderer
)
1360 RECT rect
= wxGetWindowRect(hwnd
);
1361 Init(new Graphics(hwnd
), rect
.right
- rect
.left
, rect
.bottom
- rect
.top
);
1362 m_enableOffset
= true;
1365 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1366 : wxGraphicsContext(renderer
)
1371 wxGDIPlusContext::wxGDIPlusContext(wxGraphicsRenderer
* renderer
)
1372 : wxGraphicsContext(renderer
)
1374 // Derived class must call Init() later but just set m_context to NULL for
1375 // safety to avoid crashing in our dtor if Init() ends up not being called.
1379 void wxGDIPlusContext::Init(Graphics
* graphics
, int width
, int height
)
1381 m_context
= graphics
;
1386 m_fontScaleRatio
= 1.0;
1388 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1389 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1390 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1391 m_context
->SetInterpolationMode(InterpolationModeHighQuality
);
1392 m_state1
= m_context
->Save();
1393 m_state2
= m_context
->Save();
1396 wxGDIPlusContext::~wxGDIPlusContext()
1400 m_context
->Restore( m_state2
);
1401 m_context
->Restore( m_state1
);
1407 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1409 Region
rgn((HRGN
)region
.GetHRGN());
1410 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1413 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1415 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1418 void wxGDIPlusContext::ResetClip()
1420 m_context
->ResetClip();
1423 void wxGDIPlusContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1425 if (m_composition
== wxCOMPOSITION_DEST
)
1428 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1429 Brush
*brush
= m_brush
.IsNull() ? NULL
: ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush();
1430 Pen
*pen
= m_pen
.IsNull() ? NULL
: ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen();
1434 // the offset is used to fill only the inside of the rectangle and not paint underneath
1435 // its border which may influence a transparent Pen
1438 offset
= pen
->GetWidth();
1439 m_context
->FillRectangle( brush
, (REAL
)x
+ offset
/2, (REAL
)y
+ offset
/2, (REAL
)w
- offset
, (REAL
)h
- offset
);
1444 m_context
->DrawRectangle( pen
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1448 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1450 if (m_composition
== wxCOMPOSITION_DEST
)
1453 if ( !m_pen
.IsNull() )
1455 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1456 Point
*cpoints
= new Point
[n
];
1457 for (size_t i
= 0; i
< n
; i
++)
1459 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1460 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1462 } // for (size_t i = 0; i < n; i++)
1463 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1468 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1470 if (m_composition
== wxCOMPOSITION_DEST
)
1473 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1474 Point
*cpoints
= new Point
[n
];
1475 for (size_t i
= 0; i
< n
; i
++)
1477 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1478 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1480 } // for (int i = 0; i < n; i++)
1481 if ( !m_brush
.IsNull() )
1482 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1483 if ( !m_pen
.IsNull() )
1484 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1488 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1490 if (m_composition
== wxCOMPOSITION_DEST
)
1493 if ( !m_pen
.IsNull() )
1495 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1496 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1500 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1502 if (m_composition
== wxCOMPOSITION_DEST
)
1505 if ( !m_brush
.IsNull() )
1507 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1508 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1509 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1510 (GraphicsPath
*) path
.GetNativePath());
1514 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1516 if (m_antialias
== antialias
)
1519 m_antialias
= antialias
;
1521 SmoothingMode antialiasMode
;
1524 case wxANTIALIAS_DEFAULT
:
1525 antialiasMode
= SmoothingModeHighQuality
;
1527 case wxANTIALIAS_NONE
:
1528 antialiasMode
= SmoothingModeNone
;
1533 m_context
->SetSmoothingMode(antialiasMode
);
1537 bool wxGDIPlusContext::SetInterpolationQuality(wxInterpolationQuality interpolation
)
1539 if (m_interpolation
== interpolation
)
1542 InterpolationMode interpolationMode
= InterpolationModeDefault
;
1543 switch (interpolation
)
1545 case wxINTERPOLATION_DEFAULT
:
1546 interpolationMode
= InterpolationModeDefault
;
1549 case wxINTERPOLATION_NONE
:
1550 interpolationMode
= InterpolationModeNearestNeighbor
;
1553 case wxINTERPOLATION_FAST
:
1554 interpolationMode
= InterpolationModeLowQuality
;
1557 case wxINTERPOLATION_GOOD
:
1558 interpolationMode
= InterpolationModeHighQuality
;
1561 case wxINTERPOLATION_BEST
:
1562 interpolationMode
= InterpolationModeHighQualityBicubic
;
1569 if ( m_context
->SetInterpolationMode(interpolationMode
) != Gdiplus::Ok
)
1572 m_interpolation
= interpolation
;
1577 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1579 if ( m_composition
== op
)
1584 if (m_composition
== wxCOMPOSITION_DEST
)
1587 CompositingMode cop
;
1590 case wxCOMPOSITION_SOURCE
:
1591 cop
= CompositingModeSourceCopy
;
1593 case wxCOMPOSITION_OVER
:
1594 cop
= CompositingModeSourceOver
;
1600 m_context
->SetCompositingMode(cop
);
1604 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1609 void wxGDIPlusContext::EndLayer()
1614 void wxGDIPlusContext::Rotate( wxDouble angle
)
1616 m_context
->RotateTransform( RadToDeg(angle
) );
1619 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1621 m_context
->TranslateTransform( dx
, dy
);
1624 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1626 m_context
->ScaleTransform(xScale
,yScale
);
1629 void wxGDIPlusContext::PushState()
1631 GraphicsState state
= m_context
->Save();
1632 m_stateStack
.push(state
);
1635 void wxGDIPlusContext::PopState()
1637 wxCHECK_RET( !m_stateStack
.empty(), wxT("No state to pop") );
1639 GraphicsState state
= m_stateStack
.top();
1641 m_context
->Restore(state
);
1644 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1646 if (m_composition
== wxCOMPOSITION_DEST
)
1649 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1652 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1654 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1655 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1656 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth(), image
->GetHeight(), UnitPixel
) ;
1657 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1660 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1664 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1666 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1667 DrawBitmap(bitmap
, x
, y
, w
, h
);
1670 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1672 if (m_composition
== wxCOMPOSITION_DEST
)
1675 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1676 // find out by looking at the bitmap data whether there really was alpha in it
1677 HICON hIcon
= (HICON
)icon
.GetHICON();
1679 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1680 if (!GetIconInfo(hIcon
,&iconInfo
))
1683 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1685 Bitmap
* image
= NULL
;
1687 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1688 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1689 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1691 image
= Bitmap::FromHICON(hIcon
);
1695 size_t width
= interim
.GetWidth();
1696 size_t height
= interim
.GetHeight();
1697 Rect
bounds(0,0,width
,height
);
1700 interim
.LockBits(&bounds
, ImageLockModeRead
,
1701 interim
.GetPixelFormat(),&data
);
1703 bool hasAlpha
= false;
1704 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1706 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1708 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1709 if ( ( *dest
& Color::AlphaMask
) != 0 )
1716 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1717 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1721 image
= Bitmap::FromHICON(hIcon
);
1724 interim
.UnlockBits(&data
);
1727 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1730 DeleteObject(iconInfo
.hbmColor
);
1731 DeleteObject(iconInfo
.hbmMask
);
1734 void wxGDIPlusContext::DoDrawText(const wxString
& str
,
1735 wxDouble x
, wxDouble y
)
1737 if (m_composition
== wxCOMPOSITION_DEST
)
1740 wxCHECK_RET( !m_font
.IsNull(),
1741 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1746 wxGDIPlusFontData
* const
1747 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1749 m_context
->DrawString
1751 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1752 -1, // length: string is NUL-terminated
1753 fontData
->GetGDIPlusFont(),
1755 StringFormat::GenericTypographic(),
1756 fontData
->GetGDIPlusBrush()
1760 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1761 wxDouble
*descent
, wxDouble
*externalLeading
) const
1763 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1765 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1766 FontFamily ffamily
;
1767 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1769 f
->GetFamily(&ffamily
) ;
1771 REAL factorY
= m_fontScaleRatio
;
1773 // Notice that we must use the real font style or the results would be
1774 // incorrect for italic/bold fonts.
1775 const INT style
= f
->GetStyle();
1776 const REAL size
= f
->GetSize();
1777 const REAL emHeight
= ffamily
.GetEmHeight(style
);
1778 REAL rDescent
= ffamily
.GetCellDescent(style
) * size
/ emHeight
;
1779 REAL rAscent
= ffamily
.GetCellAscent(style
) * size
/ emHeight
;
1780 REAL rHeight
= ffamily
.GetLineSpacing(style
) * size
/ emHeight
;
1783 *height
= rHeight
* factorY
;
1785 *descent
= rDescent
* factorY
;
1786 if ( externalLeading
)
1787 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1788 // measuring empty strings is not guaranteed, so do it by hand
1796 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1797 StringFormat
strFormat( StringFormat::GenericTypographic() );
1798 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1801 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1803 *width
= bounds
.Width
;
1805 *height
= bounds
.Height
;
1809 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1812 widths
.Add(0, text
.length());
1814 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1819 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1820 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1821 size_t len
= wcslen( ws
) ;
1822 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1824 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1825 StringFormat
strFormat( StringFormat::GenericTypographic() );
1827 size_t startPosition
= 0;
1828 size_t remainder
= len
;
1829 const size_t maxSpan
= 32;
1830 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1831 Region
* regions
= new Region
[maxSpan
];
1833 while( remainder
> 0 )
1835 size_t span
= wxMin( maxSpan
, remainder
);
1837 for( size_t i
= 0 ; i
< span
; ++i
)
1839 ranges
[i
].First
= 0 ;
1840 ranges
[i
].Length
= startPosition
+i
+1 ;
1842 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1843 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1844 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1847 for ( size_t i
= 0 ; i
< span
; ++i
)
1849 regions
[i
].GetBounds(&bbox
,m_context
);
1850 widths
[startPosition
+i
] = bbox
.Width
;
1853 startPosition
+= span
;
1860 bool wxGDIPlusContext::ShouldOffset() const
1862 if ( !m_enableOffset
)
1866 if ( !m_pen
.IsNull() )
1868 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1869 if ( penwidth
== 0 )
1872 return ( penwidth
% 2 ) == 1;
1875 void* wxGDIPlusContext::GetNativeContext()
1880 // concatenates this transform with the current transform of this context
1881 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1883 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1886 // sets the transform of this context
1887 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1889 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1892 // gets the matrix of this context
1893 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1895 wxGraphicsMatrix matrix
= CreateMatrix();
1896 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1900 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1906 //-----------------------------------------------------------------------------
1907 // wxGDIPlusPrintingContext implementation
1908 //-----------------------------------------------------------------------------
1910 wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
,
1912 : wxGDIPlusContext(renderer
, dc
)
1914 Graphics
* context
= GetGraphics();
1916 //m_context->SetPageUnit(UnitDocument);
1918 // Setup page scale, based on DPI ratio.
1919 // Antecedent should be 100dpi when the default page unit
1920 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1921 // instead. Note that calling SetPageScale() does not have effect on
1922 // non-printing DCs (that is, any other than wxPrinterDC or
1923 // wxEnhMetaFileDC).
1924 REAL dpiRatio
= 100.0 / context
->GetDpiY();
1925 context
->SetPageScale(dpiRatio
);
1927 // We use this modifier when measuring fonts. It is needed because the
1928 // page scale is modified above.
1929 m_fontScaleRatio
= context
->GetDpiY() / 72.0;
1932 //-----------------------------------------------------------------------------
1933 // wxGDIPlusRenderer implementation
1934 //-----------------------------------------------------------------------------
1936 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1938 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1940 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1942 return &gs_GDIPlusRenderer
;
1945 bool wxGDIPlusRenderer::EnsureIsLoaded()
1947 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1948 // if we already tried and failed (we don't want to spend lot of time
1949 // returning NULL from wxGraphicsContext::Create(), which may be called
1950 // relatively frequently):
1951 if ( m_loaded
== -1 )
1956 return m_loaded
== 1;
1959 // call EnsureIsLoaded() and return returnOnFail value if it fails
1960 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1961 if ( !EnsureIsLoaded() ) \
1962 return (returnOnFail)
1965 void wxGDIPlusRenderer::Load()
1967 GdiplusStartupInput input
;
1968 GdiplusStartupOutput output
;
1969 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1971 wxLogTrace("gdiplus", "successfully initialized GDI+");
1976 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1981 void wxGDIPlusRenderer::Unload()
1985 GdiplusShutdown(m_gditoken
);
1988 m_loaded
= -1; // next Load() will try again
1991 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1993 ENSURE_LOADED_OR_RETURN(NULL
);
1994 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1995 context
->EnableOffset(true);
1999 #if wxUSE_PRINTING_ARCHITECTURE
2000 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
2002 ENSURE_LOADED_OR_RETURN(NULL
);
2003 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
2008 #if wxUSE_ENH_METAFILE
2009 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
2011 ENSURE_LOADED_OR_RETURN(NULL
);
2012 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
2017 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
2019 ENSURE_LOADED_OR_RETURN(NULL
);
2020 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
2021 context
->EnableOffset(true);
2026 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromImage(wxImage
& image
)
2028 ENSURE_LOADED_OR_RETURN(NULL
);
2029 wxGDIPlusContext
* context
= new wxGDIPlusImageContext(this, image
);
2030 context
->EnableOffset(true);
2034 #endif // wxUSE_IMAGE
2036 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
2038 ENSURE_LOADED_OR_RETURN(NULL
);
2039 return new wxGDIPlusMeasuringContext(this);
2042 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
2044 ENSURE_LOADED_OR_RETURN(NULL
);
2045 return new wxGDIPlusContext(this,(Graphics
*) context
);
2049 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
2051 ENSURE_LOADED_OR_RETURN(NULL
);
2052 return new wxGDIPlusContext(this,(HWND
) window
);
2055 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
2057 ENSURE_LOADED_OR_RETURN(NULL
);
2058 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
2063 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
2065 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2067 m
.SetRefData( new wxGDIPlusPathData(this));
2074 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2075 wxDouble tx
, wxDouble ty
)
2078 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2080 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
2081 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2086 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
2088 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2089 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
2090 return wxNullGraphicsPen
;
2094 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
2099 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
2101 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2102 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
2103 return wxNullGraphicsBrush
;
2107 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
2113 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2114 wxDouble x2
, wxDouble y2
,
2115 const wxGraphicsGradientStops
& stops
)
2117 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2119 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2120 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2126 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2127 wxDouble xc
, wxDouble yc
,
2129 const wxGraphicsGradientStops
& stops
)
2131 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2133 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2134 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
2140 wxGDIPlusRenderer::CreateFont( const wxFont
&font
,
2141 const wxColour
&col
)
2143 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2147 p
.SetRefData(new wxGDIPlusFontData( this, font
, col
));
2151 return wxNullGraphicsFont
;
2155 wxGDIPlusRenderer::CreateFont(double size
,
2156 const wxString
& facename
,
2158 const wxColour
& col
)
2160 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2162 // Convert wxFont flags to GDI+ style:
2163 int style
= FontStyleRegular
;
2164 if ( flags
& wxFONTFLAG_ITALIC
)
2165 style
|= FontStyleItalic
;
2166 if ( flags
& wxFONTFLAG_UNDERLINED
)
2167 style
|= FontStyleUnderline
;
2168 if ( flags
& wxFONTFLAG_BOLD
)
2169 style
|= FontStyleBold
;
2170 if ( flags
& wxFONTFLAG_STRIKETHROUGH
)
2171 style
|= FontStyleStrikeout
;
2175 f
.SetRefData(new wxGDIPlusFontData(this, facename
, size
, style
, col
));
2179 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
2181 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2182 if ( bitmap
.IsOk() )
2185 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
2189 return wxNullGraphicsBitmap
;
2194 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromImage(const wxImage
& image
)
2196 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2199 // Notice that we rely on conversion from wxImage to wxBitmap here but
2200 // we could probably do it more efficiently by converting from wxImage
2201 // to GDI+ Bitmap directly, i.e. copying wxImage pixels to the buffer
2202 // returned by Bitmap::LockBits(). However this would require writing
2203 // code specific for this task while like this we can reuse existing
2204 // code (see also wxGDIPlusBitmapData::ConvertToImage()).
2205 wxGraphicsBitmap gb
;
2206 gb
.SetRefData(new wxGDIPlusBitmapData(this, image
));
2210 return wxNullGraphicsBitmap
;
2214 wxImage
wxGDIPlusRenderer::CreateImageFromBitmap(const wxGraphicsBitmap
& bmp
)
2216 ENSURE_LOADED_OR_RETURN(wxNullImage
);
2217 const wxGDIPlusBitmapData
* const
2218 data
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetGraphicsData());
2220 return data
? data
->ConvertToImage() : wxNullImage
;
2223 #endif // wxUSE_IMAGE
2226 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
2228 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2229 if ( bitmap
!= NULL
)
2232 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
2236 return wxNullGraphicsBitmap
;
2239 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2241 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2242 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
2246 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
2250 return wxNullGraphicsBitmap
;
2253 // Shutdown GDI+ at app exit, before possible dll unload
2254 class wxGDIPlusRendererModule
: public wxModule
2257 virtual bool OnInit() { return true; }
2258 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
2261 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
2264 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
2266 // ----------------------------------------------------------------------------
2267 // wxMSW-specific parts of wxGCDC
2268 // ----------------------------------------------------------------------------
2270 WXHDC
wxGCDC::AcquireHDC()
2272 wxGraphicsContext
* const gc
= GetGraphicsContext();
2277 // we can't get the HDC if it is not a GDI+ context
2278 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2279 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2284 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2285 return g
? g
->GetHDC() : NULL
;
2288 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2293 wxGraphicsContext
* const gc
= GetGraphicsContext();
2294 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2297 // we can't get the HDC if it is not a GDI+ context
2298 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2299 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2304 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2305 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2307 g
->ReleaseHDC((HDC
)hdc
);
2310 #endif // wxUSE_GRAPHICS_CONTEXT