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();
301 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
302 virtual Font
* GetGDIPlusFont() { return m_font
; }
305 // Common part of all ctors, flags here is a combination of values of
306 // FontStyle GDI+ enum.
307 void Init(const wxString
& name
,
310 const wxColour
& col
);
316 class wxGDIPlusContext
: public wxGraphicsContext
319 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
320 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
321 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
322 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
323 wxGDIPlusContext(wxGraphicsRenderer
* renderer
);
325 virtual ~wxGDIPlusContext();
327 virtual void Clip( const wxRegion
®ion
);
328 // clips drawings to the rect
329 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
331 // resets the clipping to original extent
332 virtual void ResetClip();
334 virtual void * GetNativeContext();
336 virtual void StrokePath( const wxGraphicsPath
& p
);
337 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
339 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
341 // stroke lines connecting each of the points
342 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
345 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
347 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
349 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
351 virtual bool SetCompositionMode(wxCompositionMode op
);
353 virtual void BeginLayer(wxDouble opacity
);
355 virtual void EndLayer();
357 virtual void Translate( wxDouble dx
, wxDouble dy
);
358 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
359 virtual void Rotate( wxDouble angle
);
361 // concatenates this transform with the current transform of this context
362 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
364 // sets the transform of this context
365 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
367 // gets the matrix of this context
368 virtual wxGraphicsMatrix
GetTransform() const;
370 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
371 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
372 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
373 virtual void PushState();
374 virtual void PopState();
376 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
377 wxDouble
*descent
, wxDouble
*externalLeading
) const;
378 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
379 virtual bool ShouldOffset() const;
380 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
382 Graphics
* GetGraphics() const { return m_context
; }
386 wxDouble m_fontScaleRatio
;
388 // Used from ctors (including those in the derived classes) and takes
389 // ownership of the graphics pointer that must be non-NULL.
390 void Init(Graphics
* graphics
, int width
, int height
);
393 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
394 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
395 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
396 const wxGraphicsBrush
& backgroundBrush
);
399 wxStack
<GraphicsState
> m_stateStack
;
400 GraphicsState m_state1
;
401 GraphicsState m_state2
;
403 wxDECLARE_NO_COPY_CLASS(wxGDIPlusContext
);
408 class wxGDIPlusImageContext
: public wxGDIPlusContext
411 wxGDIPlusImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
412 wxGDIPlusContext(renderer
),
414 m_bitmap(renderer
, image
)
418 new Graphics(m_bitmap
.GetGDIPlusBitmap()),
424 virtual ~wxGDIPlusImageContext()
426 m_image
= m_bitmap
.ConvertToImage();
431 wxGDIPlusBitmapData m_bitmap
;
433 wxDECLARE_NO_COPY_CLASS(wxGDIPlusImageContext
);
436 #endif // wxUSE_IMAGE
438 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
441 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
445 virtual ~wxGDIPlusMeasuringContext()
447 ReleaseDC( NULL
, m_hdc
);
454 class wxGDIPlusPrintingContext
: public wxGDIPlusContext
457 wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
458 virtual ~wxGDIPlusPrintingContext() { }
462 //-----------------------------------------------------------------------------
463 // wxGDIPlusRenderer declaration
464 //-----------------------------------------------------------------------------
466 class wxGDIPlusRenderer
: public wxGraphicsRenderer
475 virtual ~wxGDIPlusRenderer()
485 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
487 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
489 #if wxUSE_PRINTING_ARCHITECTURE
490 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
493 #if wxUSE_ENH_METAFILE
494 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
497 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
499 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
501 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
504 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
505 #endif // wxUSE_IMAGE
507 virtual wxGraphicsContext
* CreateMeasuringContext();
511 virtual wxGraphicsPath
CreatePath();
515 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
516 wxDouble tx
=0.0, wxDouble ty
=0.0);
519 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
521 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
523 virtual wxGraphicsBrush
524 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
525 wxDouble x2
, wxDouble y2
,
526 const wxGraphicsGradientStops
& stops
);
528 virtual wxGraphicsBrush
529 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
530 wxDouble xc
, wxDouble yc
,
532 const wxGraphicsGradientStops
& stops
);
534 // create a native bitmap representation
535 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
537 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
538 #endif // wxUSE_IMAGE
540 virtual wxGraphicsFont
CreateFont( const wxFont
& font
,
541 const wxColour
& col
);
543 // create a graphics bitmap from a native bitmap
544 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
546 // create a subimage from a native image representation
547 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
550 bool EnsureIsLoaded();
553 friend class wxGDIPlusRendererModule
;
557 ULONG_PTR m_gditoken
;
559 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
562 //-----------------------------------------------------------------------------
563 // wxGDIPlusPen implementation
564 //-----------------------------------------------------------------------------
566 wxGDIPlusPenData::~wxGDIPlusPenData()
573 void wxGDIPlusPenData::Init()
580 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
581 : wxGraphicsObjectRefData(renderer
)
584 m_width
= pen
.GetWidth();
588 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
591 switch ( pen
.GetCap() )
597 case wxCAP_PROJECTING
:
602 cap
= LineCapFlat
; // TODO verify
609 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
612 switch ( pen
.GetJoin() )
615 join
= LineJoinBevel
;
619 join
= LineJoinMiter
;
623 join
= LineJoinRound
;
627 join
= LineJoinMiter
;
631 m_pen
->SetLineJoin(join
);
633 m_pen
->SetDashStyle(DashStyleSolid
);
635 DashStyle dashStyle
= DashStyleSolid
;
636 switch ( pen
.GetStyle() )
638 case wxPENSTYLE_SOLID
:
641 case wxPENSTYLE_DOT
:
642 dashStyle
= DashStyleDot
;
645 case wxPENSTYLE_LONG_DASH
:
646 dashStyle
= DashStyleDash
; // TODO verify
649 case wxPENSTYLE_SHORT_DASH
:
650 dashStyle
= DashStyleDash
;
653 case wxPENSTYLE_DOT_DASH
:
654 dashStyle
= DashStyleDashDot
;
656 case wxPENSTYLE_USER_DASH
:
658 dashStyle
= DashStyleCustom
;
660 int count
= pen
.GetDashes( &dashes
);
661 if ((dashes
!= NULL
) && (count
> 0))
663 REAL
*userLengths
= new REAL
[count
];
664 for ( int i
= 0; i
< count
; ++i
)
666 userLengths
[i
] = dashes
[i
];
668 m_pen
->SetDashPattern( userLengths
, count
);
669 delete[] userLengths
;
673 case wxPENSTYLE_STIPPLE
:
675 wxBitmap
* bmp
= pen
.GetStipple();
676 if ( bmp
&& bmp
->IsOk() )
678 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
680 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
685 m_penBrush
= new TextureBrush(m_penImage
);
686 m_pen
->SetBrush( m_penBrush
);
692 if ( pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
&&
693 pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
696 switch( pen
.GetStyle() )
698 case wxPENSTYLE_BDIAGONAL_HATCH
:
699 style
= HatchStyleBackwardDiagonal
;
701 case wxPENSTYLE_CROSSDIAG_HATCH
:
702 style
= HatchStyleDiagonalCross
;
704 case wxPENSTYLE_FDIAGONAL_HATCH
:
705 style
= HatchStyleForwardDiagonal
;
707 case wxPENSTYLE_CROSS_HATCH
:
708 style
= HatchStyleCross
;
710 case wxPENSTYLE_HORIZONTAL_HATCH
:
711 style
= HatchStyleHorizontal
;
713 case wxPENSTYLE_VERTICAL_HATCH
:
714 style
= HatchStyleVertical
;
717 style
= HatchStyleHorizontal
;
719 m_penBrush
= new HatchBrush
722 wxColourToColor(pen
.GetColour()),
725 m_pen
->SetBrush( m_penBrush
);
729 if ( dashStyle
!= DashStyleSolid
)
730 m_pen
->SetDashStyle(dashStyle
);
733 //-----------------------------------------------------------------------------
734 // wxGDIPlusBrush implementation
735 //-----------------------------------------------------------------------------
737 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
738 : wxGraphicsObjectRefData(renderer
)
743 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
744 : wxGraphicsObjectRefData(renderer
)
747 if ( brush
.GetStyle() == wxSOLID
)
749 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
751 else if ( brush
.IsHatch() )
754 switch( brush
.GetStyle() )
756 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
757 style
= HatchStyleBackwardDiagonal
;
759 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
760 style
= HatchStyleDiagonalCross
;
762 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
763 style
= HatchStyleForwardDiagonal
;
765 case wxBRUSHSTYLE_CROSS_HATCH
:
766 style
= HatchStyleCross
;
768 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
769 style
= HatchStyleHorizontal
;
771 case wxBRUSHSTYLE_VERTICAL_HATCH
:
772 style
= HatchStyleVertical
;
775 style
= HatchStyleHorizontal
;
777 m_brush
= new HatchBrush
780 wxColourToColor(brush
.GetColour()),
786 wxBitmap
* bmp
= brush
.GetStipple();
787 if ( bmp
&& bmp
->IsOk() )
789 wxDELETE( m_brushImage
);
790 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
792 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
797 m_brush
= new TextureBrush(m_brushImage
);
802 wxGDIPlusBrushData::~wxGDIPlusBrushData()
809 void wxGDIPlusBrushData::Init()
816 template <typename T
>
818 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
819 const wxGraphicsGradientStops
& stops
)
821 const unsigned numStops
= stops
.GetCount();
824 // initial and final colours are set during the brush creation, nothing
829 wxVector
<Color
> colors(numStops
);
830 wxVector
<REAL
> positions(numStops
);
832 for ( unsigned i
= 0; i
< numStops
; i
++ )
834 wxGraphicsGradientStop stop
= stops
.Item(i
);
836 colors
[i
] = wxColourToColor(stop
.GetColour());
837 positions
[i
] = stop
.GetPosition();
840 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
844 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
845 wxDouble x2
, wxDouble y2
,
846 const wxGraphicsGradientStops
& stops
)
848 LinearGradientBrush
* const
849 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
850 wxColourToColor(stops
.GetStartColour()),
851 wxColourToColor(stops
.GetEndColour()));
854 SetGradientStops(brush
, stops
);
858 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
859 wxDouble xc
, wxDouble yc
,
861 const wxGraphicsGradientStops
& stops
)
863 m_brushPath
= new GraphicsPath();
864 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
865 (REAL
)(2*radius
), (REAL
)(2*radius
));
867 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
869 brush
->SetCenterPoint(PointF(xo
, yo
));
870 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
872 const Color
col(wxColourToColor(stops
.GetEndColour()));
874 brush
->SetSurroundColors(&col
, &count
);
876 SetGradientStops(brush
, stops
);
879 //-----------------------------------------------------------------------------
880 // wxGDIPlusFont implementation
881 //-----------------------------------------------------------------------------
884 wxGDIPlusFontData::Init(const wxString
& name
,
889 m_font
= new Font(name
, size
, style
, UnitPoint
);
891 m_textBrush
= new SolidBrush(wxColourToColor(col
));
894 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
896 const wxColour
& col
)
897 : wxGraphicsObjectRefData( renderer
)
899 int style
= FontStyleRegular
;
900 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
901 style
|= FontStyleItalic
;
902 if ( font
.GetUnderlined() )
903 style
|= FontStyleUnderline
;
904 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
905 style
|= FontStyleBold
;
907 Init(font
.GetFaceName(), font
.GetPointSize(), style
, col
);
910 wxGDIPlusFontData::~wxGDIPlusFontData()
916 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
917 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
918 // bytes as parameter, since there is no real copying of the data going in, only references are stored
919 // m_helper has to be kept alive as well
921 //-----------------------------------------------------------------------------
922 // wxGDIPlusBitmapData implementation
923 //-----------------------------------------------------------------------------
925 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
926 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
931 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
932 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
937 Bitmap
* image
= NULL
;
940 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
942 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
948 size_t width
= interim
.GetWidth();
949 size_t height
= interim
.GetHeight();
950 Rect
bounds(0,0,width
,height
);
952 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
954 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
955 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
957 BitmapData dataMask
;
958 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
959 interimMask
.GetPixelFormat(),&dataMask
);
962 BitmapData imageData
;
963 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
965 BYTE maskPattern
= 0 ;
969 for ( size_t y
= 0 ; y
< height
; ++y
)
972 for( size_t x
= 0 ; x
< width
; ++x
)
977 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
981 maskPattern
= maskPattern
>> 1;
983 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
984 if ( (maskByte
& maskPattern
) == 0 )
989 interim
.GetPixel(x
,y
,&c
) ;
990 *dest
= (c
.GetValue() | Color::AlphaMask
);
995 image
->UnlockBits(&imageData
);
997 interimMask
.UnlockBits(&dataMask
);
998 interim
.UnlockBits(&dataMask
);
1002 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
1004 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
1009 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
1011 size_t width
= image
->GetWidth();
1012 size_t height
= image
->GetHeight();
1013 Rect
bounds(0,0,width
,height
);
1014 static BitmapData data
;
1018 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
1019 m_helper
->GetPixelFormat(),&data
);
1021 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1022 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
1024 m_helper
->UnlockBits(&data
);
1033 wxImage
wxGDIPlusBitmapData::ConvertToImage() const
1035 // We could use Bitmap::LockBits() and convert to wxImage directly but
1036 // passing by wxBitmap is easier. It would be nice to measure performance
1037 // of the two methods but for this the second one would need to be written
1040 if ( m_bitmap
->GetHBITMAP(Color(0xffffffff), &hbmp
) != Gdiplus::Ok
)
1044 bmp
.SetWidth(m_bitmap
->GetWidth());
1045 bmp
.SetHeight(m_bitmap
->GetHeight());
1046 bmp
.SetHBITMAP(hbmp
);
1047 bmp
.SetDepth(IsAlphaPixelFormat(m_bitmap
->GetPixelFormat()) ? 32 : 24);
1048 return bmp
.ConvertToImage();
1051 #endif // wxUSE_IMAGE
1053 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1059 // ----------------------------------------------------------------------------
1060 // wxGraphicsBitmap implementation
1061 // ----------------------------------------------------------------------------
1065 wxImage
wxGraphicsBitmap::ConvertToImage() const
1067 const wxGDIPlusBitmapData
* const
1068 data
= static_cast<wxGDIPlusBitmapData
*>(GetGraphicsData());
1070 return data
? data
->ConvertToImage() : wxNullImage
;
1073 #endif // wxUSE_IMAGE
1075 //-----------------------------------------------------------------------------
1076 // wxGDIPlusPath implementation
1077 //-----------------------------------------------------------------------------
1079 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
1084 m_path
= new GraphicsPath();
1087 wxGDIPlusPathData::~wxGDIPlusPathData()
1092 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
1094 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
1101 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1103 m_path
->StartFigure();
1104 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1107 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1109 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1112 void wxGDIPlusPathData::CloseSubpath()
1114 m_path
->CloseFigure();
1117 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1123 m_path
->GetLastPoint(&start
);
1124 m_path
->AddBezier(start
,c1
,c2
,end
);
1127 // gets the last point of the current path, (0,0) if not yet set
1128 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1131 m_path
->GetLastPoint(&start
);
1136 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1138 double sweepAngle
= endAngle
- startAngle
;
1139 if( fabs(sweepAngle
) >= 2*M_PI
)
1141 sweepAngle
= 2 * M_PI
;
1147 if( sweepAngle
< 0 )
1148 sweepAngle
+= 2 * M_PI
;
1152 if( sweepAngle
> 0 )
1153 sweepAngle
-= 2 * M_PI
;
1157 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
1160 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1162 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
1165 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
1167 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
1171 // transforms each point of this path by the matrix
1172 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1174 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
1177 // gets the bounding box enclosing all points (possibly including control points)
1178 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1181 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
1188 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1190 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1191 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
1194 //-----------------------------------------------------------------------------
1195 // wxGDIPlusMatrixData implementation
1196 //-----------------------------------------------------------------------------
1198 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
1199 : wxGraphicsMatrixData(renderer
)
1204 m_matrix
= new Matrix();
1207 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1212 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1214 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1217 // concatenates the matrix
1218 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1220 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1223 // sets the matrix to the respective values
1224 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1225 wxDouble tx
, wxDouble ty
)
1227 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1230 // gets the component valuess of the matrix
1231 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1232 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1235 m_matrix
->GetElements(elements
);
1236 if (a
) *a
= elements
[0];
1237 if (b
) *b
= elements
[1];
1238 if (c
) *c
= elements
[2];
1239 if (d
) *d
= elements
[3];
1240 if (tx
) *tx
= elements
[4];
1241 if (ty
) *ty
= elements
[5];
1244 // makes this the inverse matrix
1245 void wxGDIPlusMatrixData::Invert()
1250 // returns true if the elements of the transformation matrix are equal ?
1251 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1253 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1256 // return true if this is the identity matrix
1257 bool wxGDIPlusMatrixData::IsIdentity() const
1259 return m_matrix
->IsIdentity() == TRUE
;
1266 // add the translation to this matrix
1267 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1269 m_matrix
->Translate(dx
,dy
);
1272 // add the scale to this matrix
1273 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1275 m_matrix
->Scale(xScale
,yScale
);
1278 // add the rotation to this matrix (radians)
1279 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1281 m_matrix
->Rotate( RadToDeg(angle
) );
1285 // apply the transforms
1288 // applies that matrix to the point
1289 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1292 m_matrix
->TransformPoints(&pt
);
1297 // applies the matrix except for translations
1298 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1301 m_matrix
->TransformVectors(&pt
);
1306 // returns the native representation
1307 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1312 //-----------------------------------------------------------------------------
1313 // wxGDIPlusContext implementation
1314 //-----------------------------------------------------------------------------
1316 class wxGDIPlusOffsetHelper
1319 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1324 m_gr
->TranslateTransform( 0.5, 0.5 );
1326 ~wxGDIPlusOffsetHelper( )
1329 m_gr
->TranslateTransform( -0.5, -0.5 );
1336 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1337 : wxGraphicsContext(renderer
)
1339 Init(new Graphics(hdc
), width
, height
);
1342 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
)
1343 : wxGraphicsContext(renderer
)
1345 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1346 HDC hdc
= (HDC
) msw
->GetHDC();
1347 wxSize sz
= dc
.GetSize();
1349 Init(new Graphics(hdc
), sz
.x
, sz
.y
);
1352 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1353 : wxGraphicsContext(renderer
)
1355 RECT rect
= wxGetWindowRect(hwnd
);
1356 Init(new Graphics(hwnd
), rect
.right
- rect
.left
, rect
.bottom
- rect
.top
);
1357 m_enableOffset
= true;
1360 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1361 : wxGraphicsContext(renderer
)
1366 wxGDIPlusContext::wxGDIPlusContext(wxGraphicsRenderer
* renderer
)
1367 : wxGraphicsContext(renderer
)
1369 // Derived class must call Init() later but just set m_context to NULL for
1370 // safety to avoid crashing in our dtor if Init() ends up not being called.
1374 void wxGDIPlusContext::Init(Graphics
* graphics
, int width
, int height
)
1376 m_context
= graphics
;
1381 m_fontScaleRatio
= 1.0;
1383 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1384 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1385 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1386 m_state1
= m_context
->Save();
1387 m_state2
= m_context
->Save();
1390 wxGDIPlusContext::~wxGDIPlusContext()
1394 m_context
->Restore( m_state2
);
1395 m_context
->Restore( m_state1
);
1401 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1403 Region
rgn((HRGN
)region
.GetHRGN());
1404 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1407 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1409 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1412 void wxGDIPlusContext::ResetClip()
1414 m_context
->ResetClip();
1417 void wxGDIPlusContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1419 if (m_composition
== wxCOMPOSITION_DEST
)
1422 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1423 Brush
*brush
= m_brush
.IsNull() ? NULL
: ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush();
1424 Pen
*pen
= m_pen
.IsNull() ? NULL
: ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen();
1428 // the offset is used to fill only the inside of the rectangle and not paint underneath
1429 // its border which may influence a transparent Pen
1432 offset
= pen
->GetWidth();
1433 m_context
->FillRectangle( brush
, (REAL
)x
+ offset
/2, (REAL
)y
+ offset
/2, (REAL
)w
- offset
, (REAL
)h
- offset
);
1438 m_context
->DrawRectangle( pen
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1442 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1444 if (m_composition
== wxCOMPOSITION_DEST
)
1447 if ( !m_pen
.IsNull() )
1449 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1450 Point
*cpoints
= new Point
[n
];
1451 for (size_t i
= 0; i
< n
; i
++)
1453 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1454 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1456 } // for (size_t i = 0; i < n; i++)
1457 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1462 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1464 if (m_composition
== wxCOMPOSITION_DEST
)
1467 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1468 Point
*cpoints
= new Point
[n
];
1469 for (size_t i
= 0; i
< n
; i
++)
1471 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1472 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1474 } // for (int i = 0; i < n; i++)
1475 if ( !m_brush
.IsNull() )
1476 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1477 if ( !m_pen
.IsNull() )
1478 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1482 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1484 if (m_composition
== wxCOMPOSITION_DEST
)
1487 if ( !m_pen
.IsNull() )
1489 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1490 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1494 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1496 if (m_composition
== wxCOMPOSITION_DEST
)
1499 if ( !m_brush
.IsNull() )
1501 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1502 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1503 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1504 (GraphicsPath
*) path
.GetNativePath());
1508 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1510 if (m_antialias
== antialias
)
1513 m_antialias
= antialias
;
1515 SmoothingMode antialiasMode
;
1518 case wxANTIALIAS_DEFAULT
:
1519 antialiasMode
= SmoothingModeHighQuality
;
1521 case wxANTIALIAS_NONE
:
1522 antialiasMode
= SmoothingModeNone
;
1527 m_context
->SetSmoothingMode(antialiasMode
);
1531 bool wxGDIPlusContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
1537 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1539 if ( m_composition
== op
)
1544 if (m_composition
== wxCOMPOSITION_DEST
)
1547 CompositingMode cop
;
1550 case wxCOMPOSITION_SOURCE
:
1551 cop
= CompositingModeSourceCopy
;
1553 case wxCOMPOSITION_OVER
:
1554 cop
= CompositingModeSourceOver
;
1560 m_context
->SetCompositingMode(cop
);
1564 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1569 void wxGDIPlusContext::EndLayer()
1574 void wxGDIPlusContext::Rotate( wxDouble angle
)
1576 m_context
->RotateTransform( RadToDeg(angle
) );
1579 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1581 m_context
->TranslateTransform( dx
, dy
);
1584 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1586 m_context
->ScaleTransform(xScale
,yScale
);
1589 void wxGDIPlusContext::PushState()
1591 GraphicsState state
= m_context
->Save();
1592 m_stateStack
.push(state
);
1595 void wxGDIPlusContext::PopState()
1597 wxCHECK_RET( !m_stateStack
.empty(), wxT("No state to pop") );
1599 GraphicsState state
= m_stateStack
.top();
1601 m_context
->Restore(state
);
1604 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1606 if (m_composition
== wxCOMPOSITION_DEST
)
1609 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1612 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1614 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1615 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1616 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth(), image
->GetHeight(), UnitPixel
) ;
1617 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1620 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1624 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1626 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1627 DrawBitmap(bitmap
, x
, y
, w
, h
);
1630 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1632 if (m_composition
== wxCOMPOSITION_DEST
)
1635 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1636 // find out by looking at the bitmap data whether there really was alpha in it
1637 HICON hIcon
= (HICON
)icon
.GetHICON();
1639 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1640 if (!GetIconInfo(hIcon
,&iconInfo
))
1643 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1645 Bitmap
* image
= NULL
;
1647 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1648 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1649 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1651 image
= Bitmap::FromHICON(hIcon
);
1655 size_t width
= interim
.GetWidth();
1656 size_t height
= interim
.GetHeight();
1657 Rect
bounds(0,0,width
,height
);
1660 interim
.LockBits(&bounds
, ImageLockModeRead
,
1661 interim
.GetPixelFormat(),&data
);
1663 bool hasAlpha
= false;
1664 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1666 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1668 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1669 if ( ( *dest
& Color::AlphaMask
) != 0 )
1676 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1677 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1681 image
= Bitmap::FromHICON(hIcon
);
1684 interim
.UnlockBits(&data
);
1687 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1690 DeleteObject(iconInfo
.hbmColor
);
1691 DeleteObject(iconInfo
.hbmMask
);
1694 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1695 wxDouble x
, wxDouble y
,
1696 const wxGraphicsBrush
& brush
)
1698 if (m_composition
== wxCOMPOSITION_DEST
)
1701 wxCHECK_RET( !m_font
.IsNull(),
1702 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1707 wxGDIPlusFontData
* const
1708 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1709 wxGDIPlusBrushData
* const
1710 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1712 m_context
->DrawString
1714 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1715 -1, // length: string is NUL-terminated
1716 fontData
->GetGDIPlusFont(),
1718 StringFormat::GenericTypographic(),
1719 brushData
? brushData
->GetGDIPlusBrush()
1720 : fontData
->GetGDIPlusBrush()
1724 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1725 wxDouble
*descent
, wxDouble
*externalLeading
) const
1727 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1729 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1730 FontFamily ffamily
;
1731 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1733 f
->GetFamily(&ffamily
) ;
1735 REAL factorY
= m_fontScaleRatio
;
1737 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1738 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1739 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1740 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1741 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1742 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1745 *height
= rHeight
* factorY
;
1747 *descent
= rDescent
* factorY
;
1748 if ( externalLeading
)
1749 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1750 // measuring empty strings is not guaranteed, so do it by hand
1758 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1759 StringFormat
strFormat( StringFormat::GenericTypographic() );
1760 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1763 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1765 *width
= bounds
.Width
;
1767 *height
= bounds
.Height
;
1771 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1774 widths
.Add(0, text
.length());
1776 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1781 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1782 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1783 size_t len
= wcslen( ws
) ;
1784 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1786 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1787 StringFormat
strFormat( StringFormat::GenericTypographic() );
1789 size_t startPosition
= 0;
1790 size_t remainder
= len
;
1791 const size_t maxSpan
= 32;
1792 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1793 Region
* regions
= new Region
[maxSpan
];
1795 while( remainder
> 0 )
1797 size_t span
= wxMin( maxSpan
, remainder
);
1799 for( size_t i
= 0 ; i
< span
; ++i
)
1801 ranges
[i
].First
= 0 ;
1802 ranges
[i
].Length
= startPosition
+i
+1 ;
1804 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1805 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1806 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1809 for ( size_t i
= 0 ; i
< span
; ++i
)
1811 regions
[i
].GetBounds(&bbox
,m_context
);
1812 widths
[startPosition
+i
] = bbox
.Width
;
1815 startPosition
+= span
;
1822 bool wxGDIPlusContext::ShouldOffset() const
1824 if ( !m_enableOffset
)
1828 if ( !m_pen
.IsNull() )
1830 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1831 if ( penwidth
== 0 )
1834 return ( penwidth
% 2 ) == 1;
1837 void* wxGDIPlusContext::GetNativeContext()
1842 // concatenates this transform with the current transform of this context
1843 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1845 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1848 // sets the transform of this context
1849 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1851 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1854 // gets the matrix of this context
1855 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1857 wxGraphicsMatrix matrix
= CreateMatrix();
1858 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1862 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1868 //-----------------------------------------------------------------------------
1869 // wxGDIPlusPrintingContext implementation
1870 //-----------------------------------------------------------------------------
1872 wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
,
1874 : wxGDIPlusContext(renderer
, dc
)
1876 Graphics
* context
= GetGraphics();
1878 //m_context->SetPageUnit(UnitDocument);
1880 // Setup page scale, based on DPI ratio.
1881 // Antecedent should be 100dpi when the default page unit
1882 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1883 // instead. Note that calling SetPageScale() does not have effect on
1884 // non-printing DCs (that is, any other than wxPrinterDC or
1885 // wxEnhMetaFileDC).
1886 REAL dpiRatio
= 100.0 / context
->GetDpiY();
1887 context
->SetPageScale(dpiRatio
);
1889 // We use this modifier when measuring fonts. It is needed because the
1890 // page scale is modified above.
1891 m_fontScaleRatio
= context
->GetDpiY() / 72.0;
1894 //-----------------------------------------------------------------------------
1895 // wxGDIPlusRenderer implementation
1896 //-----------------------------------------------------------------------------
1898 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1900 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1902 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1904 return &gs_GDIPlusRenderer
;
1907 bool wxGDIPlusRenderer::EnsureIsLoaded()
1909 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1910 // if we already tried and failed (we don't want to spend lot of time
1911 // returning NULL from wxGraphicsContext::Create(), which may be called
1912 // relatively frequently):
1913 if ( m_loaded
== -1 )
1918 return m_loaded
== 1;
1921 // call EnsureIsLoaded() and return returnOnFail value if it fails
1922 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1923 if ( !EnsureIsLoaded() ) \
1924 return (returnOnFail)
1927 void wxGDIPlusRenderer::Load()
1929 GdiplusStartupInput input
;
1930 GdiplusStartupOutput output
;
1931 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1933 wxLogTrace("gdiplus", "successfully initialized GDI+");
1938 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1943 void wxGDIPlusRenderer::Unload()
1947 GdiplusShutdown(m_gditoken
);
1950 m_loaded
= -1; // next Load() will try again
1953 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1955 ENSURE_LOADED_OR_RETURN(NULL
);
1956 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1957 context
->EnableOffset(true);
1961 #if wxUSE_PRINTING_ARCHITECTURE
1962 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1964 ENSURE_LOADED_OR_RETURN(NULL
);
1965 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1970 #if wxUSE_ENH_METAFILE
1971 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1973 ENSURE_LOADED_OR_RETURN(NULL
);
1974 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1979 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1981 ENSURE_LOADED_OR_RETURN(NULL
);
1982 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1983 context
->EnableOffset(true);
1988 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromImage(wxImage
& image
)
1990 ENSURE_LOADED_OR_RETURN(NULL
);
1991 wxGDIPlusContext
* context
= new wxGDIPlusImageContext(this, image
);
1992 context
->EnableOffset(true);
1996 #endif // wxUSE_IMAGE
1998 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
2000 ENSURE_LOADED_OR_RETURN(NULL
);
2001 return new wxGDIPlusMeasuringContext(this);
2004 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
2006 ENSURE_LOADED_OR_RETURN(NULL
);
2007 return new wxGDIPlusContext(this,(Graphics
*) context
);
2011 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
2013 ENSURE_LOADED_OR_RETURN(NULL
);
2014 return new wxGDIPlusContext(this,(HWND
) window
);
2017 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
2019 ENSURE_LOADED_OR_RETURN(NULL
);
2020 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
2025 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
2027 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2029 m
.SetRefData( new wxGDIPlusPathData(this));
2036 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2037 wxDouble tx
, wxDouble ty
)
2040 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2042 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
2043 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2048 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
2050 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2051 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
2052 return wxNullGraphicsPen
;
2056 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
2061 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
2063 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2064 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
2065 return wxNullGraphicsBrush
;
2069 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
2075 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2076 wxDouble x2
, wxDouble y2
,
2077 const wxGraphicsGradientStops
& stops
)
2079 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2081 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2082 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2088 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2089 wxDouble xc
, wxDouble yc
,
2091 const wxGraphicsGradientStops
& stops
)
2093 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2095 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2096 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
2102 wxGDIPlusRenderer::CreateFont( const wxFont
&font
,
2103 const wxColour
&col
)
2105 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2109 p
.SetRefData(new wxGDIPlusFontData( this, font
, col
));
2113 return wxNullGraphicsFont
;
2116 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
2118 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2119 if ( bitmap
.IsOk() )
2122 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
2126 return wxNullGraphicsBitmap
;
2131 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromImage(const wxImage
& image
)
2133 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2136 // Notice that we rely on conversion from wxImage to wxBitmap here but
2137 // we could probably do it more efficiently by converting from wxImage
2138 // to GDI+ Bitmap directly, i.e. copying wxImage pixels to the buffer
2139 // returned by Bitmap::LockBits(). However this would require writing
2140 // code specific for this task while like this we can reuse existing
2141 // code (see also wxGDIPlusBitmapData::ConvertToImage()).
2142 wxGraphicsBitmap gb
;
2143 gb
.SetRefData(new wxGDIPlusBitmapData(this, image
));
2147 return wxNullGraphicsBitmap
;
2150 #endif // wxUSE_IMAGE
2152 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
2154 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2155 if ( bitmap
!= NULL
)
2158 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
2162 return wxNullGraphicsBitmap
;
2165 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2167 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2168 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
2172 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
2176 return wxNullGraphicsBitmap
;
2179 // Shutdown GDI+ at app exit, before possible dll unload
2180 class wxGDIPlusRendererModule
: public wxModule
2183 virtual bool OnInit() { return true; }
2184 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
2187 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
2190 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
2192 // ----------------------------------------------------------------------------
2193 // wxMSW-specific parts of wxGCDC
2194 // ----------------------------------------------------------------------------
2196 WXHDC
wxGCDC::AcquireHDC()
2198 wxGraphicsContext
* const gc
= GetGraphicsContext();
2203 // we can't get the HDC if it is not a GDI+ context
2204 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2205 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2210 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2211 return g
? g
->GetHDC() : NULL
;
2214 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2219 wxGraphicsContext
* const gc
= GetGraphicsContext();
2220 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2223 // we can't get the HDC if it is not a GDI+ context
2224 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2225 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2230 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2231 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2233 g
->ReleaseHDC((HDC
)hdc
);
2236 #endif // wxUSE_GRAPHICS_CONTEXT