1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/graphics.cpp
3 // Purpose: wxGCDC class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #if wxUSE_GRAPHICS_CONTEXT
23 #include "wx/msw/wrapcdlg.h"
25 #include "wx/window.h"
27 #include "wx/dialog.h"
29 #include "wx/bitmap.h"
32 #include "wx/module.h"
33 // include all dc types that are used as a param
35 #include "wx/dcclient.h"
36 #include "wx/dcmemory.h"
37 #include "wx/dcprint.h"
42 #include "wx/private/graphics.h"
43 #include "wx/msw/wrapgdip.h"
44 #include "wx/msw/dc.h"
45 #if wxUSE_ENH_METAFILE
46 #include "wx/msw/enhmeta.h"
48 #include "wx/dcgraph.h"
50 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
52 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 const double RAD2DEG
= 180.0 / M_PI
;
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
70 inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
72 inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
73 inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
75 // translate a wxColour to a Color
76 inline Color
wxColourToColor(const wxColour
& col
)
78 return Color(col
.Alpha(), col
.Red(), col
.Green(), col
.Blue());
81 } // anonymous namespace
83 //-----------------------------------------------------------------------------
84 // device context implementation
86 // more and more of the dc functionality should be implemented by calling
87 // the appropricate wxGDIPlusContext, but we will have to do that step by step
88 // also coordinate conversions should be moved to native matrix ops
89 //-----------------------------------------------------------------------------
91 // we always stock two context states, one at entry, to be able to preserve the
92 // state we were called with, the other one after changing to HI Graphics orientation
93 // (this one is used for getting back clippings etc)
95 //-----------------------------------------------------------------------------
96 // wxGraphicsPath implementation
97 //-----------------------------------------------------------------------------
99 class wxGDIPlusContext
;
101 class wxGDIPlusPathData
: public wxGraphicsPathData
104 wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
105 ~wxGDIPlusPathData();
107 virtual wxGraphicsObjectRefData
*Clone() const;
110 // These are the path primitives from which everything else can be constructed
113 // begins a new subpath at (x,y)
114 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
116 // adds a straight line from the current point to (x,y)
117 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
119 // adds a cubic Bezier curve from the current point, using two control points and an end point
120 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
123 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
124 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
126 // gets the last point of the current path, (0,0) if not yet set
127 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
130 virtual void AddPath( const wxGraphicsPathData
* path
);
132 // closes the current sub-path
133 virtual void CloseSubpath();
136 // These are convenience functions which - if not available natively will be assembled
137 // using the primitives from above
140 // appends a rectangle as a new closed subpath
141 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
144 // appends an ellipsis as a new closed subpath fitting the passed rectangle
145 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
147 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
148 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
151 // returns the native path
152 virtual void * GetNativePath() const { return m_path
; }
154 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
155 virtual void UnGetNativePath(void * WXUNUSED(path
)) const {}
157 // transforms each point of this path by the matrix
158 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
160 // gets the bounding box enclosing all points (possibly including control points)
161 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
163 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
166 GraphicsPath
* m_path
;
169 class wxGDIPlusMatrixData
: public wxGraphicsMatrixData
172 wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
173 virtual ~wxGDIPlusMatrixData() ;
175 virtual wxGraphicsObjectRefData
* Clone() const ;
177 // concatenates the matrix
178 virtual void Concat( const wxGraphicsMatrixData
*t
);
180 // sets the matrix to the respective values
181 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
182 wxDouble tx
=0.0, wxDouble ty
=0.0);
184 // gets the component valuess of the matrix
185 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
186 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
188 // makes this the inverse matrix
189 virtual void Invert();
191 // returns true if the elements of the transformation matrix are equal ?
192 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
194 // return true if this is the identity matrix
195 virtual bool IsIdentity() const;
201 // add the translation to this matrix
202 virtual void Translate( wxDouble dx
, wxDouble dy
);
204 // add the scale to this matrix
205 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
207 // add the rotation to this matrix (radians)
208 virtual void Rotate( wxDouble angle
);
211 // apply the transforms
214 // applies that matrix to the point
215 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
217 // applies the matrix except for translations
218 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
220 // returns the native representation
221 virtual void * GetNativeMatrix() const;
226 class wxGDIPlusPenData
: public wxGraphicsObjectRefData
229 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
234 virtual wxDouble
GetWidth() { return m_width
; }
235 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
245 class wxGDIPlusBrushData
: public wxGraphicsObjectRefData
248 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
249 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
250 ~wxGDIPlusBrushData ();
252 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
253 wxDouble x2
, wxDouble y2
,
254 const wxGraphicsGradientStops
& stops
);
255 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
256 wxDouble xc
, wxDouble yc
,
258 const wxGraphicsGradientStops
& stops
);
260 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
266 // common part of Create{Linear,Radial}GradientBrush()
267 template <typename T
>
268 void SetGradientStops(T
*brush
, const wxGraphicsGradientStops
& stops
);
272 GraphicsPath
* m_brushPath
;
275 class WXDLLIMPEXP_CORE wxGDIPlusBitmapData
: public wxGraphicsObjectRefData
278 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
);
279 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
&bmp
);
280 ~wxGDIPlusBitmapData ();
282 virtual Bitmap
* GetGDIPlusBitmap() { return m_bitmap
; }
285 wxImage
ConvertToImage() const;
286 #endif // wxUSE_IMAGE
293 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
296 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
298 const wxColour
& col
);
299 wxGDIPlusFontData(wxGraphicsRenderer
* renderer
,
300 const wxString
& name
,
303 const wxColour
& col
);
304 ~wxGDIPlusFontData();
306 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
307 virtual Font
* GetGDIPlusFont() { return m_font
; }
310 // Common part of all ctors, flags here is a combination of values of
311 // FontStyle GDI+ enum.
312 void Init(const wxString
& name
,
322 class wxGDIPlusContext
: public wxGraphicsContext
325 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
326 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
327 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
328 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
329 wxGDIPlusContext(wxGraphicsRenderer
* renderer
);
331 virtual ~wxGDIPlusContext();
333 virtual void Clip( const wxRegion
®ion
);
334 // clips drawings to the rect
335 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
337 // resets the clipping to original extent
338 virtual void ResetClip();
340 virtual void * GetNativeContext();
342 virtual void StrokePath( const wxGraphicsPath
& p
);
343 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
345 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
347 // stroke lines connecting each of the points
348 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
351 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
353 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
355 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
357 virtual bool SetCompositionMode(wxCompositionMode op
);
359 virtual void BeginLayer(wxDouble opacity
);
361 virtual void EndLayer();
363 virtual void Translate( wxDouble dx
, wxDouble dy
);
364 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
365 virtual void Rotate( wxDouble angle
);
367 // concatenates this transform with the current transform of this context
368 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
370 // sets the transform of this context
371 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
373 // gets the matrix of this context
374 virtual wxGraphicsMatrix
GetTransform() const;
376 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
377 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
378 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
379 virtual void PushState();
380 virtual void PopState();
382 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
383 wxDouble
*descent
, wxDouble
*externalLeading
) const;
384 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
385 virtual bool ShouldOffset() const;
386 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
388 Graphics
* GetGraphics() const { return m_context
; }
392 wxDouble m_fontScaleRatio
;
394 // Used from ctors (including those in the derived classes) and takes
395 // ownership of the graphics pointer that must be non-NULL.
396 void Init(Graphics
* graphics
, int width
, int height
);
399 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
400 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
401 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
402 const wxGraphicsBrush
& backgroundBrush
);
405 wxStack
<GraphicsState
> m_stateStack
;
406 GraphicsState m_state1
;
407 GraphicsState m_state2
;
409 wxDECLARE_NO_COPY_CLASS(wxGDIPlusContext
);
414 class wxGDIPlusImageContext
: public wxGDIPlusContext
417 wxGDIPlusImageContext(wxGraphicsRenderer
* renderer
, wxImage
& image
) :
418 wxGDIPlusContext(renderer
),
420 m_bitmap(renderer
, image
)
424 new Graphics(m_bitmap
.GetGDIPlusBitmap()),
430 virtual ~wxGDIPlusImageContext()
432 m_image
= m_bitmap
.ConvertToImage();
437 wxGDIPlusBitmapData m_bitmap
;
439 wxDECLARE_NO_COPY_CLASS(wxGDIPlusImageContext
);
442 #endif // wxUSE_IMAGE
444 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
447 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
451 virtual ~wxGDIPlusMeasuringContext()
453 ReleaseDC( NULL
, m_hdc
);
460 class wxGDIPlusPrintingContext
: public wxGDIPlusContext
463 wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
464 virtual ~wxGDIPlusPrintingContext() { }
468 //-----------------------------------------------------------------------------
469 // wxGDIPlusRenderer declaration
470 //-----------------------------------------------------------------------------
472 class wxGDIPlusRenderer
: public wxGraphicsRenderer
481 virtual ~wxGDIPlusRenderer()
491 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
493 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
495 #if wxUSE_PRINTING_ARCHITECTURE
496 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
499 #if wxUSE_ENH_METAFILE
500 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
503 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
505 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
507 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
510 virtual wxGraphicsContext
* CreateContextFromImage(wxImage
& image
);
511 #endif // wxUSE_IMAGE
513 virtual wxGraphicsContext
* CreateMeasuringContext();
517 virtual wxGraphicsPath
CreatePath();
521 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
522 wxDouble tx
=0.0, wxDouble ty
=0.0);
525 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
527 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
529 virtual wxGraphicsBrush
530 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
531 wxDouble x2
, wxDouble y2
,
532 const wxGraphicsGradientStops
& stops
);
534 virtual wxGraphicsBrush
535 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
536 wxDouble xc
, wxDouble yc
,
538 const wxGraphicsGradientStops
& stops
);
540 // create a native bitmap representation
541 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
543 virtual wxGraphicsBitmap
CreateBitmapFromImage(const wxImage
& image
);
544 #endif // wxUSE_IMAGE
546 virtual wxGraphicsFont
CreateFont( const wxFont
& font
,
547 const wxColour
& col
);
549 virtual wxGraphicsFont
CreateFont(double size
,
550 const wxString
& facename
,
551 int flags
= wxFONTFLAG_DEFAULT
,
552 const wxColour
& col
= *wxBLACK
);
554 // create a graphics bitmap from a native bitmap
555 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
557 // create a subimage from a native image representation
558 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
561 bool EnsureIsLoaded();
564 friend class wxGDIPlusRendererModule
;
568 ULONG_PTR m_gditoken
;
570 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
573 //-----------------------------------------------------------------------------
574 // wxGDIPlusPen implementation
575 //-----------------------------------------------------------------------------
577 wxGDIPlusPenData::~wxGDIPlusPenData()
584 void wxGDIPlusPenData::Init()
591 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
592 : wxGraphicsObjectRefData(renderer
)
595 m_width
= pen
.GetWidth();
599 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
602 switch ( pen
.GetCap() )
608 case wxCAP_PROJECTING
:
613 cap
= LineCapFlat
; // TODO verify
620 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
623 switch ( pen
.GetJoin() )
626 join
= LineJoinBevel
;
630 join
= LineJoinMiter
;
634 join
= LineJoinRound
;
638 join
= LineJoinMiter
;
642 m_pen
->SetLineJoin(join
);
644 m_pen
->SetDashStyle(DashStyleSolid
);
646 DashStyle dashStyle
= DashStyleSolid
;
647 switch ( pen
.GetStyle() )
649 case wxPENSTYLE_SOLID
:
652 case wxPENSTYLE_DOT
:
653 dashStyle
= DashStyleDot
;
656 case wxPENSTYLE_LONG_DASH
:
657 dashStyle
= DashStyleDash
; // TODO verify
660 case wxPENSTYLE_SHORT_DASH
:
661 dashStyle
= DashStyleDash
;
664 case wxPENSTYLE_DOT_DASH
:
665 dashStyle
= DashStyleDashDot
;
667 case wxPENSTYLE_USER_DASH
:
669 dashStyle
= DashStyleCustom
;
671 int count
= pen
.GetDashes( &dashes
);
672 if ((dashes
!= NULL
) && (count
> 0))
674 REAL
*userLengths
= new REAL
[count
];
675 for ( int i
= 0; i
< count
; ++i
)
677 userLengths
[i
] = dashes
[i
];
679 m_pen
->SetDashPattern( userLengths
, count
);
680 delete[] userLengths
;
684 case wxPENSTYLE_STIPPLE
:
686 wxBitmap
* bmp
= pen
.GetStipple();
687 if ( bmp
&& bmp
->IsOk() )
689 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
691 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
696 m_penBrush
= new TextureBrush(m_penImage
);
697 m_pen
->SetBrush( m_penBrush
);
703 if ( pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
&&
704 pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
707 switch( pen
.GetStyle() )
709 case wxPENSTYLE_BDIAGONAL_HATCH
:
710 style
= HatchStyleBackwardDiagonal
;
712 case wxPENSTYLE_CROSSDIAG_HATCH
:
713 style
= HatchStyleDiagonalCross
;
715 case wxPENSTYLE_FDIAGONAL_HATCH
:
716 style
= HatchStyleForwardDiagonal
;
718 case wxPENSTYLE_CROSS_HATCH
:
719 style
= HatchStyleCross
;
721 case wxPENSTYLE_HORIZONTAL_HATCH
:
722 style
= HatchStyleHorizontal
;
724 case wxPENSTYLE_VERTICAL_HATCH
:
725 style
= HatchStyleVertical
;
728 style
= HatchStyleHorizontal
;
730 m_penBrush
= new HatchBrush
733 wxColourToColor(pen
.GetColour()),
736 m_pen
->SetBrush( m_penBrush
);
740 if ( dashStyle
!= DashStyleSolid
)
741 m_pen
->SetDashStyle(dashStyle
);
744 //-----------------------------------------------------------------------------
745 // wxGDIPlusBrush implementation
746 //-----------------------------------------------------------------------------
748 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
749 : wxGraphicsObjectRefData(renderer
)
754 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
755 : wxGraphicsObjectRefData(renderer
)
758 if ( brush
.GetStyle() == wxSOLID
)
760 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
762 else if ( brush
.IsHatch() )
765 switch( brush
.GetStyle() )
767 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
768 style
= HatchStyleBackwardDiagonal
;
770 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
771 style
= HatchStyleDiagonalCross
;
773 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
774 style
= HatchStyleForwardDiagonal
;
776 case wxBRUSHSTYLE_CROSS_HATCH
:
777 style
= HatchStyleCross
;
779 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
780 style
= HatchStyleHorizontal
;
782 case wxBRUSHSTYLE_VERTICAL_HATCH
:
783 style
= HatchStyleVertical
;
786 style
= HatchStyleHorizontal
;
788 m_brush
= new HatchBrush
791 wxColourToColor(brush
.GetColour()),
797 wxBitmap
* bmp
= brush
.GetStipple();
798 if ( bmp
&& bmp
->IsOk() )
800 wxDELETE( m_brushImage
);
801 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
803 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
808 m_brush
= new TextureBrush(m_brushImage
);
813 wxGDIPlusBrushData::~wxGDIPlusBrushData()
820 void wxGDIPlusBrushData::Init()
827 template <typename T
>
829 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
830 const wxGraphicsGradientStops
& stops
)
832 const unsigned numStops
= stops
.GetCount();
835 // initial and final colours are set during the brush creation, nothing
840 wxVector
<Color
> colors(numStops
);
841 wxVector
<REAL
> positions(numStops
);
843 for ( unsigned i
= 0; i
< numStops
; i
++ )
845 wxGraphicsGradientStop stop
= stops
.Item(i
);
847 colors
[i
] = wxColourToColor(stop
.GetColour());
848 positions
[i
] = stop
.GetPosition();
851 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
855 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
856 wxDouble x2
, wxDouble y2
,
857 const wxGraphicsGradientStops
& stops
)
859 LinearGradientBrush
* const
860 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
861 wxColourToColor(stops
.GetStartColour()),
862 wxColourToColor(stops
.GetEndColour()));
865 SetGradientStops(brush
, stops
);
869 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
870 wxDouble xc
, wxDouble yc
,
872 const wxGraphicsGradientStops
& stops
)
874 m_brushPath
= new GraphicsPath();
875 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
876 (REAL
)(2*radius
), (REAL
)(2*radius
));
878 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
880 brush
->SetCenterPoint(PointF(xo
, yo
));
881 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
883 const Color
col(wxColourToColor(stops
.GetEndColour()));
885 brush
->SetSurroundColors(&col
, &count
);
887 SetGradientStops(brush
, stops
);
890 //-----------------------------------------------------------------------------
891 // wxGDIPlusFont implementation
892 //-----------------------------------------------------------------------------
895 wxGDIPlusFontData::Init(const wxString
& name
,
901 m_font
= new Font(name
, size
, style
, fontUnit
);
903 m_textBrush
= new SolidBrush(wxColourToColor(col
));
906 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
908 const wxColour
& col
)
909 : wxGraphicsObjectRefData( renderer
)
911 int style
= FontStyleRegular
;
912 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
913 style
|= FontStyleItalic
;
914 if ( font
.GetUnderlined() )
915 style
|= FontStyleUnderline
;
916 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
917 style
|= FontStyleBold
;
919 Init(font
.GetFaceName(), font
.GetPointSize(), style
, col
, UnitPoint
);
922 wxGDIPlusFontData::wxGDIPlusFontData(wxGraphicsRenderer
* renderer
,
923 const wxString
& name
,
926 const wxColour
& col
) :
927 wxGraphicsObjectRefData(renderer
)
929 Init(name
, sizeInPixels
, style
, col
, UnitPixel
);
932 wxGDIPlusFontData::~wxGDIPlusFontData()
938 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
939 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
940 // bytes as parameter, since there is no real copying of the data going in, only references are stored
941 // m_helper has to be kept alive as well
943 //-----------------------------------------------------------------------------
944 // wxGDIPlusBitmapData implementation
945 //-----------------------------------------------------------------------------
947 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
948 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
953 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
954 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
959 Bitmap
* image
= NULL
;
962 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
964 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
970 size_t width
= interim
.GetWidth();
971 size_t height
= interim
.GetHeight();
972 Rect
bounds(0,0,width
,height
);
974 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
976 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
977 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
979 BitmapData dataMask
;
980 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
981 interimMask
.GetPixelFormat(),&dataMask
);
984 BitmapData imageData
;
985 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
987 BYTE maskPattern
= 0 ;
991 for ( size_t y
= 0 ; y
< height
; ++y
)
994 for( size_t x
= 0 ; x
< width
; ++x
)
999 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
1003 maskPattern
= maskPattern
>> 1;
1005 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
1006 if ( (maskByte
& maskPattern
) == 0 )
1011 interim
.GetPixel(x
,y
,&c
) ;
1012 *dest
= (c
.GetValue() | Color::AlphaMask
);
1017 image
->UnlockBits(&imageData
);
1019 interimMask
.UnlockBits(&dataMask
);
1020 interim
.UnlockBits(&dataMask
);
1024 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
1026 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
1031 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
1033 size_t width
= image
->GetWidth();
1034 size_t height
= image
->GetHeight();
1035 Rect
bounds(0,0,width
,height
);
1036 static BitmapData data
;
1040 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
1041 m_helper
->GetPixelFormat(),&data
);
1043 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1044 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
1046 m_helper
->UnlockBits(&data
);
1055 wxImage
wxGDIPlusBitmapData::ConvertToImage() const
1057 // We could use Bitmap::LockBits() and convert to wxImage directly but
1058 // passing by wxBitmap is easier. It would be nice to measure performance
1059 // of the two methods but for this the second one would need to be written
1062 if ( m_bitmap
->GetHBITMAP(Color(0xffffffff), &hbmp
) != Gdiplus::Ok
)
1066 bmp
.SetWidth(m_bitmap
->GetWidth());
1067 bmp
.SetHeight(m_bitmap
->GetHeight());
1068 bmp
.SetHBITMAP(hbmp
);
1069 bmp
.SetDepth(IsAlphaPixelFormat(m_bitmap
->GetPixelFormat()) ? 32 : 24);
1070 return bmp
.ConvertToImage();
1073 #endif // wxUSE_IMAGE
1075 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1081 // ----------------------------------------------------------------------------
1082 // wxGraphicsBitmap implementation
1083 // ----------------------------------------------------------------------------
1087 wxImage
wxGraphicsBitmap::ConvertToImage() const
1089 const wxGDIPlusBitmapData
* const
1090 data
= static_cast<wxGDIPlusBitmapData
*>(GetGraphicsData());
1092 return data
? data
->ConvertToImage() : wxNullImage
;
1095 #endif // wxUSE_IMAGE
1097 //-----------------------------------------------------------------------------
1098 // wxGDIPlusPath implementation
1099 //-----------------------------------------------------------------------------
1101 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
1106 m_path
= new GraphicsPath();
1109 wxGDIPlusPathData::~wxGDIPlusPathData()
1114 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
1116 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
1123 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1125 m_path
->StartFigure();
1126 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1129 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1131 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1134 void wxGDIPlusPathData::CloseSubpath()
1136 m_path
->CloseFigure();
1139 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1145 m_path
->GetLastPoint(&start
);
1146 m_path
->AddBezier(start
,c1
,c2
,end
);
1149 // gets the last point of the current path, (0,0) if not yet set
1150 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1153 m_path
->GetLastPoint(&start
);
1158 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1160 double sweepAngle
= endAngle
- startAngle
;
1161 if( fabs(sweepAngle
) >= 2*M_PI
)
1163 sweepAngle
= 2 * M_PI
;
1169 if( sweepAngle
< 0 )
1170 sweepAngle
+= 2 * M_PI
;
1174 if( sweepAngle
> 0 )
1175 sweepAngle
-= 2 * M_PI
;
1179 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
1182 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1184 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
1187 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
1189 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
1193 // transforms each point of this path by the matrix
1194 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1196 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
1199 // gets the bounding box enclosing all points (possibly including control points)
1200 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1203 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
1210 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1212 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1213 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
1216 //-----------------------------------------------------------------------------
1217 // wxGDIPlusMatrixData implementation
1218 //-----------------------------------------------------------------------------
1220 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
1221 : wxGraphicsMatrixData(renderer
)
1226 m_matrix
= new Matrix();
1229 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1234 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1236 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1239 // concatenates the matrix
1240 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1242 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1245 // sets the matrix to the respective values
1246 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1247 wxDouble tx
, wxDouble ty
)
1249 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1252 // gets the component valuess of the matrix
1253 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1254 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1257 m_matrix
->GetElements(elements
);
1258 if (a
) *a
= elements
[0];
1259 if (b
) *b
= elements
[1];
1260 if (c
) *c
= elements
[2];
1261 if (d
) *d
= elements
[3];
1262 if (tx
) *tx
= elements
[4];
1263 if (ty
) *ty
= elements
[5];
1266 // makes this the inverse matrix
1267 void wxGDIPlusMatrixData::Invert()
1272 // returns true if the elements of the transformation matrix are equal ?
1273 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1275 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1278 // return true if this is the identity matrix
1279 bool wxGDIPlusMatrixData::IsIdentity() const
1281 return m_matrix
->IsIdentity() == TRUE
;
1288 // add the translation to this matrix
1289 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1291 m_matrix
->Translate(dx
,dy
);
1294 // add the scale to this matrix
1295 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1297 m_matrix
->Scale(xScale
,yScale
);
1300 // add the rotation to this matrix (radians)
1301 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1303 m_matrix
->Rotate( RadToDeg(angle
) );
1307 // apply the transforms
1310 // applies that matrix to the point
1311 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1314 m_matrix
->TransformPoints(&pt
);
1319 // applies the matrix except for translations
1320 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1323 m_matrix
->TransformVectors(&pt
);
1328 // returns the native representation
1329 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1334 //-----------------------------------------------------------------------------
1335 // wxGDIPlusContext implementation
1336 //-----------------------------------------------------------------------------
1338 class wxGDIPlusOffsetHelper
1341 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1346 m_gr
->TranslateTransform( 0.5, 0.5 );
1348 ~wxGDIPlusOffsetHelper( )
1351 m_gr
->TranslateTransform( -0.5, -0.5 );
1358 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1359 : wxGraphicsContext(renderer
)
1361 Init(new Graphics(hdc
), width
, height
);
1364 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
)
1365 : wxGraphicsContext(renderer
)
1367 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1368 HDC hdc
= (HDC
) msw
->GetHDC();
1369 wxSize sz
= dc
.GetSize();
1371 Init(new Graphics(hdc
), sz
.x
, sz
.y
);
1374 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1375 : wxGraphicsContext(renderer
)
1377 RECT rect
= wxGetWindowRect(hwnd
);
1378 Init(new Graphics(hwnd
), rect
.right
- rect
.left
, rect
.bottom
- rect
.top
);
1379 m_enableOffset
= true;
1382 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1383 : wxGraphicsContext(renderer
)
1388 wxGDIPlusContext::wxGDIPlusContext(wxGraphicsRenderer
* renderer
)
1389 : wxGraphicsContext(renderer
)
1391 // Derived class must call Init() later but just set m_context to NULL for
1392 // safety to avoid crashing in our dtor if Init() ends up not being called.
1396 void wxGDIPlusContext::Init(Graphics
* graphics
, int width
, int height
)
1398 m_context
= graphics
;
1403 m_fontScaleRatio
= 1.0;
1405 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1406 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1407 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1408 m_state1
= m_context
->Save();
1409 m_state2
= m_context
->Save();
1412 wxGDIPlusContext::~wxGDIPlusContext()
1416 m_context
->Restore( m_state2
);
1417 m_context
->Restore( m_state1
);
1423 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1425 Region
rgn((HRGN
)region
.GetHRGN());
1426 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1429 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1431 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1434 void wxGDIPlusContext::ResetClip()
1436 m_context
->ResetClip();
1439 void wxGDIPlusContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1441 if (m_composition
== wxCOMPOSITION_DEST
)
1444 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1445 Brush
*brush
= m_brush
.IsNull() ? NULL
: ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush();
1446 Pen
*pen
= m_pen
.IsNull() ? NULL
: ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen();
1450 // the offset is used to fill only the inside of the rectangle and not paint underneath
1451 // its border which may influence a transparent Pen
1454 offset
= pen
->GetWidth();
1455 m_context
->FillRectangle( brush
, (REAL
)x
+ offset
/2, (REAL
)y
+ offset
/2, (REAL
)w
- offset
, (REAL
)h
- offset
);
1460 m_context
->DrawRectangle( pen
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1464 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1466 if (m_composition
== wxCOMPOSITION_DEST
)
1469 if ( !m_pen
.IsNull() )
1471 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1472 Point
*cpoints
= new Point
[n
];
1473 for (size_t i
= 0; i
< n
; i
++)
1475 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1476 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1478 } // for (size_t i = 0; i < n; i++)
1479 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1484 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1486 if (m_composition
== wxCOMPOSITION_DEST
)
1489 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1490 Point
*cpoints
= new Point
[n
];
1491 for (size_t i
= 0; i
< n
; i
++)
1493 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1494 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1496 } // for (int i = 0; i < n; i++)
1497 if ( !m_brush
.IsNull() )
1498 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1499 if ( !m_pen
.IsNull() )
1500 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1504 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1506 if (m_composition
== wxCOMPOSITION_DEST
)
1509 if ( !m_pen
.IsNull() )
1511 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1512 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1516 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1518 if (m_composition
== wxCOMPOSITION_DEST
)
1521 if ( !m_brush
.IsNull() )
1523 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1524 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1525 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1526 (GraphicsPath
*) path
.GetNativePath());
1530 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1532 if (m_antialias
== antialias
)
1535 m_antialias
= antialias
;
1537 SmoothingMode antialiasMode
;
1540 case wxANTIALIAS_DEFAULT
:
1541 antialiasMode
= SmoothingModeHighQuality
;
1543 case wxANTIALIAS_NONE
:
1544 antialiasMode
= SmoothingModeNone
;
1549 m_context
->SetSmoothingMode(antialiasMode
);
1553 bool wxGDIPlusContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
1559 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1561 if ( m_composition
== op
)
1566 if (m_composition
== wxCOMPOSITION_DEST
)
1569 CompositingMode cop
;
1572 case wxCOMPOSITION_SOURCE
:
1573 cop
= CompositingModeSourceCopy
;
1575 case wxCOMPOSITION_OVER
:
1576 cop
= CompositingModeSourceOver
;
1582 m_context
->SetCompositingMode(cop
);
1586 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1591 void wxGDIPlusContext::EndLayer()
1596 void wxGDIPlusContext::Rotate( wxDouble angle
)
1598 m_context
->RotateTransform( RadToDeg(angle
) );
1601 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1603 m_context
->TranslateTransform( dx
, dy
);
1606 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1608 m_context
->ScaleTransform(xScale
,yScale
);
1611 void wxGDIPlusContext::PushState()
1613 GraphicsState state
= m_context
->Save();
1614 m_stateStack
.push(state
);
1617 void wxGDIPlusContext::PopState()
1619 wxCHECK_RET( !m_stateStack
.empty(), wxT("No state to pop") );
1621 GraphicsState state
= m_stateStack
.top();
1623 m_context
->Restore(state
);
1626 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1628 if (m_composition
== wxCOMPOSITION_DEST
)
1631 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1634 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1636 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1637 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1638 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth(), image
->GetHeight(), UnitPixel
) ;
1639 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1642 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1646 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1648 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1649 DrawBitmap(bitmap
, x
, y
, w
, h
);
1652 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1654 if (m_composition
== wxCOMPOSITION_DEST
)
1657 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1658 // find out by looking at the bitmap data whether there really was alpha in it
1659 HICON hIcon
= (HICON
)icon
.GetHICON();
1661 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1662 if (!GetIconInfo(hIcon
,&iconInfo
))
1665 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1667 Bitmap
* image
= NULL
;
1669 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1670 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1671 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1673 image
= Bitmap::FromHICON(hIcon
);
1677 size_t width
= interim
.GetWidth();
1678 size_t height
= interim
.GetHeight();
1679 Rect
bounds(0,0,width
,height
);
1682 interim
.LockBits(&bounds
, ImageLockModeRead
,
1683 interim
.GetPixelFormat(),&data
);
1685 bool hasAlpha
= false;
1686 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1688 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1690 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1691 if ( ( *dest
& Color::AlphaMask
) != 0 )
1698 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1699 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1703 image
= Bitmap::FromHICON(hIcon
);
1706 interim
.UnlockBits(&data
);
1709 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1712 DeleteObject(iconInfo
.hbmColor
);
1713 DeleteObject(iconInfo
.hbmMask
);
1716 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1717 wxDouble x
, wxDouble y
,
1718 const wxGraphicsBrush
& brush
)
1720 if (m_composition
== wxCOMPOSITION_DEST
)
1723 wxCHECK_RET( !m_font
.IsNull(),
1724 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1729 wxGDIPlusFontData
* const
1730 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1731 wxGDIPlusBrushData
* const
1732 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1734 m_context
->DrawString
1736 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1737 -1, // length: string is NUL-terminated
1738 fontData
->GetGDIPlusFont(),
1740 StringFormat::GenericTypographic(),
1741 brushData
? brushData
->GetGDIPlusBrush()
1742 : fontData
->GetGDIPlusBrush()
1746 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1747 wxDouble
*descent
, wxDouble
*externalLeading
) const
1749 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1751 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1752 FontFamily ffamily
;
1753 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1755 f
->GetFamily(&ffamily
) ;
1757 REAL factorY
= m_fontScaleRatio
;
1759 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1760 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1761 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1762 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1763 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1764 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1767 *height
= rHeight
* factorY
;
1769 *descent
= rDescent
* factorY
;
1770 if ( externalLeading
)
1771 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1772 // measuring empty strings is not guaranteed, so do it by hand
1780 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1781 StringFormat
strFormat( StringFormat::GenericTypographic() );
1782 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1785 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1787 *width
= bounds
.Width
;
1789 *height
= bounds
.Height
;
1793 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1796 widths
.Add(0, text
.length());
1798 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1803 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1804 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1805 size_t len
= wcslen( ws
) ;
1806 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1808 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1809 StringFormat
strFormat( StringFormat::GenericTypographic() );
1811 size_t startPosition
= 0;
1812 size_t remainder
= len
;
1813 const size_t maxSpan
= 32;
1814 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1815 Region
* regions
= new Region
[maxSpan
];
1817 while( remainder
> 0 )
1819 size_t span
= wxMin( maxSpan
, remainder
);
1821 for( size_t i
= 0 ; i
< span
; ++i
)
1823 ranges
[i
].First
= 0 ;
1824 ranges
[i
].Length
= startPosition
+i
+1 ;
1826 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1827 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1828 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1831 for ( size_t i
= 0 ; i
< span
; ++i
)
1833 regions
[i
].GetBounds(&bbox
,m_context
);
1834 widths
[startPosition
+i
] = bbox
.Width
;
1837 startPosition
+= span
;
1844 bool wxGDIPlusContext::ShouldOffset() const
1846 if ( !m_enableOffset
)
1850 if ( !m_pen
.IsNull() )
1852 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1853 if ( penwidth
== 0 )
1856 return ( penwidth
% 2 ) == 1;
1859 void* wxGDIPlusContext::GetNativeContext()
1864 // concatenates this transform with the current transform of this context
1865 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1867 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1870 // sets the transform of this context
1871 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1873 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1876 // gets the matrix of this context
1877 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1879 wxGraphicsMatrix matrix
= CreateMatrix();
1880 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1884 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1890 //-----------------------------------------------------------------------------
1891 // wxGDIPlusPrintingContext implementation
1892 //-----------------------------------------------------------------------------
1894 wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
,
1896 : wxGDIPlusContext(renderer
, dc
)
1898 Graphics
* context
= GetGraphics();
1900 //m_context->SetPageUnit(UnitDocument);
1902 // Setup page scale, based on DPI ratio.
1903 // Antecedent should be 100dpi when the default page unit
1904 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1905 // instead. Note that calling SetPageScale() does not have effect on
1906 // non-printing DCs (that is, any other than wxPrinterDC or
1907 // wxEnhMetaFileDC).
1908 REAL dpiRatio
= 100.0 / context
->GetDpiY();
1909 context
->SetPageScale(dpiRatio
);
1911 // We use this modifier when measuring fonts. It is needed because the
1912 // page scale is modified above.
1913 m_fontScaleRatio
= context
->GetDpiY() / 72.0;
1916 //-----------------------------------------------------------------------------
1917 // wxGDIPlusRenderer implementation
1918 //-----------------------------------------------------------------------------
1920 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1922 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1924 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1926 return &gs_GDIPlusRenderer
;
1929 bool wxGDIPlusRenderer::EnsureIsLoaded()
1931 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1932 // if we already tried and failed (we don't want to spend lot of time
1933 // returning NULL from wxGraphicsContext::Create(), which may be called
1934 // relatively frequently):
1935 if ( m_loaded
== -1 )
1940 return m_loaded
== 1;
1943 // call EnsureIsLoaded() and return returnOnFail value if it fails
1944 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1945 if ( !EnsureIsLoaded() ) \
1946 return (returnOnFail)
1949 void wxGDIPlusRenderer::Load()
1951 GdiplusStartupInput input
;
1952 GdiplusStartupOutput output
;
1953 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1955 wxLogTrace("gdiplus", "successfully initialized GDI+");
1960 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1965 void wxGDIPlusRenderer::Unload()
1969 GdiplusShutdown(m_gditoken
);
1972 m_loaded
= -1; // next Load() will try again
1975 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1977 ENSURE_LOADED_OR_RETURN(NULL
);
1978 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1979 context
->EnableOffset(true);
1983 #if wxUSE_PRINTING_ARCHITECTURE
1984 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1986 ENSURE_LOADED_OR_RETURN(NULL
);
1987 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1992 #if wxUSE_ENH_METAFILE
1993 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1995 ENSURE_LOADED_OR_RETURN(NULL
);
1996 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
2001 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
2003 ENSURE_LOADED_OR_RETURN(NULL
);
2004 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
2005 context
->EnableOffset(true);
2010 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromImage(wxImage
& image
)
2012 ENSURE_LOADED_OR_RETURN(NULL
);
2013 wxGDIPlusContext
* context
= new wxGDIPlusImageContext(this, image
);
2014 context
->EnableOffset(true);
2018 #endif // wxUSE_IMAGE
2020 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
2022 ENSURE_LOADED_OR_RETURN(NULL
);
2023 return new wxGDIPlusMeasuringContext(this);
2026 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
2028 ENSURE_LOADED_OR_RETURN(NULL
);
2029 return new wxGDIPlusContext(this,(Graphics
*) context
);
2033 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
2035 ENSURE_LOADED_OR_RETURN(NULL
);
2036 return new wxGDIPlusContext(this,(HWND
) window
);
2039 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
2041 ENSURE_LOADED_OR_RETURN(NULL
);
2042 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
2047 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
2049 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
2051 m
.SetRefData( new wxGDIPlusPathData(this));
2058 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
2059 wxDouble tx
, wxDouble ty
)
2062 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
2064 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
2065 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
2070 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
2072 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2073 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
2074 return wxNullGraphicsPen
;
2078 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
2083 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
2085 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2086 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
2087 return wxNullGraphicsBrush
;
2091 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
2097 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2098 wxDouble x2
, wxDouble y2
,
2099 const wxGraphicsGradientStops
& stops
)
2101 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2103 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2104 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2110 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2111 wxDouble xc
, wxDouble yc
,
2113 const wxGraphicsGradientStops
& stops
)
2115 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2117 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2118 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
2124 wxGDIPlusRenderer::CreateFont( const wxFont
&font
,
2125 const wxColour
&col
)
2127 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2131 p
.SetRefData(new wxGDIPlusFontData( this, font
, col
));
2135 return wxNullGraphicsFont
;
2139 wxGDIPlusRenderer::CreateFont(double size
,
2140 const wxString
& facename
,
2142 const wxColour
& col
)
2144 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2146 // Convert wxFont flags to GDI+ style:
2147 int style
= FontStyleRegular
;
2148 if ( flags
& wxFONTFLAG_ITALIC
)
2149 style
|= FontStyleItalic
;
2150 if ( flags
& wxFONTFLAG_UNDERLINED
)
2151 style
|= FontStyleUnderline
;
2152 if ( flags
& wxFONTFLAG_BOLD
)
2153 style
|= FontStyleBold
;
2154 if ( flags
& wxFONTFLAG_STRIKETHROUGH
)
2155 style
|= FontStyleStrikeout
;
2159 f
.SetRefData(new wxGDIPlusFontData(this, facename
, size
, style
, col
));
2163 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
2165 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2166 if ( bitmap
.IsOk() )
2169 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
2173 return wxNullGraphicsBitmap
;
2178 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromImage(const wxImage
& image
)
2180 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2183 // Notice that we rely on conversion from wxImage to wxBitmap here but
2184 // we could probably do it more efficiently by converting from wxImage
2185 // to GDI+ Bitmap directly, i.e. copying wxImage pixels to the buffer
2186 // returned by Bitmap::LockBits(). However this would require writing
2187 // code specific for this task while like this we can reuse existing
2188 // code (see also wxGDIPlusBitmapData::ConvertToImage()).
2189 wxGraphicsBitmap gb
;
2190 gb
.SetRefData(new wxGDIPlusBitmapData(this, image
));
2194 return wxNullGraphicsBitmap
;
2197 #endif // wxUSE_IMAGE
2199 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
2201 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2202 if ( bitmap
!= NULL
)
2205 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
2209 return wxNullGraphicsBitmap
;
2212 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2214 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2215 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
2219 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
2223 return wxNullGraphicsBitmap
;
2226 // Shutdown GDI+ at app exit, before possible dll unload
2227 class wxGDIPlusRendererModule
: public wxModule
2230 virtual bool OnInit() { return true; }
2231 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
2234 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
2237 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
2239 // ----------------------------------------------------------------------------
2240 // wxMSW-specific parts of wxGCDC
2241 // ----------------------------------------------------------------------------
2243 WXHDC
wxGCDC::AcquireHDC()
2245 wxGraphicsContext
* const gc
= GetGraphicsContext();
2250 // we can't get the HDC if it is not a GDI+ context
2251 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2252 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2257 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2258 return g
? g
->GetHDC() : NULL
;
2261 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2266 wxGraphicsContext
* const gc
= GetGraphicsContext();
2267 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2270 // we can't get the HDC if it is not a GDI+ context
2271 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2272 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2277 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2278 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2280 g
->ReleaseHDC((HDC
)hdc
);
2283 #endif // wxUSE_GRAPHICS_CONTEXT