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
; }
289 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
292 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
293 const wxGDIPlusContext
* gc
,
295 const wxColour
& col
);
296 ~wxGDIPlusFontData();
298 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
299 virtual Font
* GetGDIPlusFont() { return m_font
; }
305 class wxGDIPlusContext
: public wxGraphicsContext
308 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
309 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
310 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
311 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
314 virtual ~wxGDIPlusContext();
316 virtual void Clip( const wxRegion
®ion
);
317 // clips drawings to the rect
318 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
320 // resets the clipping to original extent
321 virtual void ResetClip();
323 virtual void * GetNativeContext();
325 virtual void StrokePath( const wxGraphicsPath
& p
);
326 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
328 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
330 // stroke lines connecting each of the points
331 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
334 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
336 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
338 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
340 virtual bool SetCompositionMode(wxCompositionMode op
);
342 virtual void BeginLayer(wxDouble opacity
);
344 virtual void EndLayer();
346 virtual void Translate( wxDouble dx
, wxDouble dy
);
347 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
348 virtual void Rotate( wxDouble angle
);
350 // concatenates this transform with the current transform of this context
351 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
353 // sets the transform of this context
354 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
356 // gets the matrix of this context
357 virtual wxGraphicsMatrix
GetTransform() const;
359 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
360 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
361 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
362 virtual void PushState();
363 virtual void PopState();
365 // sets the font of this context
366 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) const;
368 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
369 wxDouble
*descent
, wxDouble
*externalLeading
) const;
370 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
371 virtual bool ShouldOffset() const;
372 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
374 Graphics
* GetGraphics() const { return m_context
; }
378 wxDouble m_fontScaleRatio
;
384 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
385 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
386 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
387 const wxGraphicsBrush
& backgroundBrush
);
390 wxStack
<GraphicsState
> m_stateStack
;
391 GraphicsState m_state1
;
392 GraphicsState m_state2
;
394 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
397 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
400 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
403 wxGDIPlusMeasuringContext()
407 virtual ~wxGDIPlusMeasuringContext()
409 ReleaseDC( NULL
, m_hdc
);
414 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
417 class wxGDIPlusPrintingContext
: public wxGDIPlusContext
420 wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
421 virtual ~wxGDIPlusPrintingContext() { }
425 //-----------------------------------------------------------------------------
426 // wxGDIPlusRenderer declaration
427 //-----------------------------------------------------------------------------
429 class wxGDIPlusRenderer
: public wxGraphicsRenderer
438 virtual ~wxGDIPlusRenderer()
448 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
450 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
452 #if wxUSE_PRINTING_ARCHITECTURE
453 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
456 #if wxUSE_ENH_METAFILE
457 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
460 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
462 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
464 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
466 virtual wxGraphicsContext
* CreateMeasuringContext();
470 virtual wxGraphicsPath
CreatePath();
474 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
475 wxDouble tx
=0.0, wxDouble ty
=0.0);
478 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
480 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
482 virtual wxGraphicsBrush
483 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
484 wxDouble x2
, wxDouble y2
,
485 const wxGraphicsGradientStops
& stops
);
487 virtual wxGraphicsBrush
488 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
489 wxDouble xc
, wxDouble yc
,
491 const wxGraphicsGradientStops
& stops
);
493 // create a native bitmap representation
494 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
496 // stub: should not be called directly
497 virtual wxGraphicsFont
CreateFont( const wxFont
& WXUNUSED(font
),
498 const wxColour
& WXUNUSED(col
) )
499 { wxFAIL
; return wxNullGraphicsFont
; }
501 // this is used to really create the font
502 wxGraphicsFont
CreateGDIPlusFont( const wxGDIPlusContext
* gc
,
504 const wxColour
&col
);
506 // create a graphics bitmap from a native bitmap
507 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
509 // create a subimage from a native image representation
510 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
513 bool EnsureIsLoaded();
516 friend class wxGDIPlusRendererModule
;
520 ULONG_PTR m_gditoken
;
522 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
525 //-----------------------------------------------------------------------------
526 // wxGDIPlusPen implementation
527 //-----------------------------------------------------------------------------
529 wxGDIPlusPenData::~wxGDIPlusPenData()
536 void wxGDIPlusPenData::Init()
543 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
544 : wxGraphicsObjectRefData(renderer
)
547 m_width
= pen
.GetWidth();
551 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
554 switch ( pen
.GetCap() )
560 case wxCAP_PROJECTING
:
565 cap
= LineCapFlat
; // TODO verify
572 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
575 switch ( pen
.GetJoin() )
578 join
= LineJoinBevel
;
582 join
= LineJoinMiter
;
586 join
= LineJoinRound
;
590 join
= LineJoinMiter
;
594 m_pen
->SetLineJoin(join
);
596 m_pen
->SetDashStyle(DashStyleSolid
);
598 DashStyle dashStyle
= DashStyleSolid
;
599 switch ( pen
.GetStyle() )
601 case wxPENSTYLE_SOLID
:
604 case wxPENSTYLE_DOT
:
605 dashStyle
= DashStyleDot
;
608 case wxPENSTYLE_LONG_DASH
:
609 dashStyle
= DashStyleDash
; // TODO verify
612 case wxPENSTYLE_SHORT_DASH
:
613 dashStyle
= DashStyleDash
;
616 case wxPENSTYLE_DOT_DASH
:
617 dashStyle
= DashStyleDashDot
;
619 case wxPENSTYLE_USER_DASH
:
621 dashStyle
= DashStyleCustom
;
623 int count
= pen
.GetDashes( &dashes
);
624 if ((dashes
!= NULL
) && (count
> 0))
626 REAL
*userLengths
= new REAL
[count
];
627 for ( int i
= 0; i
< count
; ++i
)
629 userLengths
[i
] = dashes
[i
];
631 m_pen
->SetDashPattern( userLengths
, count
);
632 delete[] userLengths
;
636 case wxPENSTYLE_STIPPLE
:
638 wxBitmap
* bmp
= pen
.GetStipple();
639 if ( bmp
&& bmp
->IsOk() )
641 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
643 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
648 m_penBrush
= new TextureBrush(m_penImage
);
649 m_pen
->SetBrush( m_penBrush
);
655 if ( pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
&&
656 pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
659 switch( pen
.GetStyle() )
661 case wxPENSTYLE_BDIAGONAL_HATCH
:
662 style
= HatchStyleBackwardDiagonal
;
664 case wxPENSTYLE_CROSSDIAG_HATCH
:
665 style
= HatchStyleDiagonalCross
;
667 case wxPENSTYLE_FDIAGONAL_HATCH
:
668 style
= HatchStyleForwardDiagonal
;
670 case wxPENSTYLE_CROSS_HATCH
:
671 style
= HatchStyleCross
;
673 case wxPENSTYLE_HORIZONTAL_HATCH
:
674 style
= HatchStyleHorizontal
;
676 case wxPENSTYLE_VERTICAL_HATCH
:
677 style
= HatchStyleVertical
;
680 style
= HatchStyleHorizontal
;
682 m_penBrush
= new HatchBrush
685 wxColourToColor(pen
.GetColour()),
688 m_pen
->SetBrush( m_penBrush
);
692 if ( dashStyle
!= DashStyleSolid
)
693 m_pen
->SetDashStyle(dashStyle
);
696 //-----------------------------------------------------------------------------
697 // wxGDIPlusBrush implementation
698 //-----------------------------------------------------------------------------
700 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
701 : wxGraphicsObjectRefData(renderer
)
706 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
707 : wxGraphicsObjectRefData(renderer
)
710 if ( brush
.GetStyle() == wxSOLID
)
712 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
714 else if ( brush
.IsHatch() )
717 switch( brush
.GetStyle() )
719 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
720 style
= HatchStyleBackwardDiagonal
;
722 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
723 style
= HatchStyleDiagonalCross
;
725 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
726 style
= HatchStyleForwardDiagonal
;
728 case wxBRUSHSTYLE_CROSS_HATCH
:
729 style
= HatchStyleCross
;
731 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
732 style
= HatchStyleHorizontal
;
734 case wxBRUSHSTYLE_VERTICAL_HATCH
:
735 style
= HatchStyleVertical
;
738 style
= HatchStyleHorizontal
;
740 m_brush
= new HatchBrush
743 wxColourToColor(brush
.GetColour()),
749 wxBitmap
* bmp
= brush
.GetStipple();
750 if ( bmp
&& bmp
->IsOk() )
752 wxDELETE( m_brushImage
);
753 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
755 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
760 m_brush
= new TextureBrush(m_brushImage
);
765 wxGDIPlusBrushData::~wxGDIPlusBrushData()
772 void wxGDIPlusBrushData::Init()
779 template <typename T
>
781 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
782 const wxGraphicsGradientStops
& stops
)
784 const unsigned numStops
= stops
.GetCount();
787 // initial and final colours are set during the brush creation, nothing
792 wxVector
<Color
> colors(numStops
);
793 wxVector
<REAL
> positions(numStops
);
795 for ( unsigned i
= 0; i
< numStops
; i
++ )
797 wxGraphicsGradientStop stop
= stops
.Item(i
);
799 colors
[i
] = wxColourToColor(stop
.GetColour());
800 positions
[i
] = stop
.GetPosition();
803 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
807 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
808 wxDouble x2
, wxDouble y2
,
809 const wxGraphicsGradientStops
& stops
)
811 LinearGradientBrush
* const
812 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
813 wxColourToColor(stops
.GetStartColour()),
814 wxColourToColor(stops
.GetEndColour()));
817 SetGradientStops(brush
, stops
);
821 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
822 wxDouble xc
, wxDouble yc
,
824 const wxGraphicsGradientStops
& stops
)
826 m_brushPath
= new GraphicsPath();
827 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
828 (REAL
)(2*radius
), (REAL
)(2*radius
));
830 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
832 brush
->SetCenterPoint(PointF(xo
, yo
));
833 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
835 const Color
col(wxColourToColor(stops
.GetEndColour()));
837 brush
->SetSurroundColors(&col
, &count
);
839 SetGradientStops(brush
, stops
);
842 //-----------------------------------------------------------------------------
843 // wxGDIPlusFont implementation
844 //-----------------------------------------------------------------------------
846 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
847 const wxGDIPlusContext
* gc
,
849 const wxColour
& col
)
850 : wxGraphicsObjectRefData( renderer
)
852 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
853 int style
= FontStyleRegular
;
854 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
855 style
|= FontStyleItalic
;
856 if ( font
.GetUnderlined() )
857 style
|= FontStyleUnderline
;
858 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
859 style
|= FontStyleBold
;
861 Graphics
* context
= gc
->GetGraphics();
863 Unit fontUnit
= context
->GetPageUnit();
864 // if fontUnit is UnitDisplay, then specify UnitPixel, otherwise
865 // you'll get a "InvalidParameter" from GDI+
866 if ( fontUnit
== UnitDisplay
)
867 fontUnit
= UnitPixel
;
869 REAL points
= font
.GetPointSize();
871 // This scaling is needed when we use unit other than the
872 // default UnitPoint. It works for both display and printing.
873 REAL size
= points
* (100.0 / 72.0);
875 // NB: font unit should match context's unit. We can use UnitPixel,
876 // as that is what the print context should use.
877 m_font
= new Font( s
, size
, style
, fontUnit
);
879 m_textBrush
= new SolidBrush(wxColourToColor(col
));
882 wxGDIPlusFontData::~wxGDIPlusFontData()
888 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
889 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
890 // bytes as parameter, since there is no real copying of the data going in, only references are stored
891 // m_helper has to be kept alive as well
893 //-----------------------------------------------------------------------------
894 // wxGDIPlusBitmapData implementation
895 //-----------------------------------------------------------------------------
897 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
898 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
903 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
904 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
909 Bitmap
* image
= NULL
;
912 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
914 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
920 size_t width
= interim
.GetWidth();
921 size_t height
= interim
.GetHeight();
922 Rect
bounds(0,0,width
,height
);
924 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
926 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
927 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
929 BitmapData dataMask
;
930 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
931 interimMask
.GetPixelFormat(),&dataMask
);
934 BitmapData imageData
;
935 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
937 BYTE maskPattern
= 0 ;
941 for ( size_t y
= 0 ; y
< height
; ++y
)
944 for( size_t x
= 0 ; x
< width
; ++x
)
949 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
953 maskPattern
= maskPattern
>> 1;
955 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
956 if ( (maskByte
& maskPattern
) == 0 )
961 interim
.GetPixel(x
,y
,&c
) ;
962 *dest
= (c
.GetValue() | Color::AlphaMask
);
967 image
->UnlockBits(&imageData
);
969 interimMask
.UnlockBits(&dataMask
);
970 interim
.UnlockBits(&dataMask
);
974 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
976 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
981 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
983 size_t width
= image
->GetWidth();
984 size_t height
= image
->GetHeight();
985 Rect
bounds(0,0,width
,height
);
986 static BitmapData data
;
990 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
991 m_helper
->GetPixelFormat(),&data
);
993 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
994 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
996 m_helper
->UnlockBits(&data
);
1003 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1009 //-----------------------------------------------------------------------------
1010 // wxGDIPlusPath implementation
1011 //-----------------------------------------------------------------------------
1013 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
1018 m_path
= new GraphicsPath();
1021 wxGDIPlusPathData::~wxGDIPlusPathData()
1026 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
1028 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
1035 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1037 m_path
->StartFigure();
1038 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1041 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1043 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1046 void wxGDIPlusPathData::CloseSubpath()
1048 m_path
->CloseFigure();
1051 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1057 m_path
->GetLastPoint(&start
);
1058 m_path
->AddBezier(start
,c1
,c2
,end
);
1061 // gets the last point of the current path, (0,0) if not yet set
1062 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1065 m_path
->GetLastPoint(&start
);
1070 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1072 double sweepAngle
= endAngle
- startAngle
;
1073 if( fabs(sweepAngle
) >= 2*M_PI
)
1075 sweepAngle
= 2 * M_PI
;
1081 if( sweepAngle
< 0 )
1082 sweepAngle
+= 2 * M_PI
;
1086 if( sweepAngle
> 0 )
1087 sweepAngle
-= 2 * M_PI
;
1091 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
1094 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1096 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
1099 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
1101 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
1105 // transforms each point of this path by the matrix
1106 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1108 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
1111 // gets the bounding box enclosing all points (possibly including control points)
1112 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1115 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
1122 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1124 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1125 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
1128 //-----------------------------------------------------------------------------
1129 // wxGDIPlusMatrixData implementation
1130 //-----------------------------------------------------------------------------
1132 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
1133 : wxGraphicsMatrixData(renderer
)
1138 m_matrix
= new Matrix();
1141 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1146 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1148 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1151 // concatenates the matrix
1152 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1154 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1157 // sets the matrix to the respective values
1158 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1159 wxDouble tx
, wxDouble ty
)
1161 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1164 // gets the component valuess of the matrix
1165 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1166 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1169 m_matrix
->GetElements(elements
);
1170 if (a
) *a
= elements
[0];
1171 if (b
) *b
= elements
[1];
1172 if (c
) *c
= elements
[2];
1173 if (d
) *d
= elements
[3];
1174 if (tx
) *tx
= elements
[4];
1175 if (ty
) *ty
= elements
[5];
1178 // makes this the inverse matrix
1179 void wxGDIPlusMatrixData::Invert()
1184 // returns true if the elements of the transformation matrix are equal ?
1185 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1187 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1190 // return true if this is the identity matrix
1191 bool wxGDIPlusMatrixData::IsIdentity() const
1193 return m_matrix
->IsIdentity() == TRUE
;
1200 // add the translation to this matrix
1201 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1203 m_matrix
->Translate(dx
,dy
);
1206 // add the scale to this matrix
1207 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1209 m_matrix
->Scale(xScale
,yScale
);
1212 // add the rotation to this matrix (radians)
1213 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1215 m_matrix
->Rotate( RadToDeg(angle
) );
1219 // apply the transforms
1222 // applies that matrix to the point
1223 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1226 m_matrix
->TransformPoints(&pt
);
1231 // applies the matrix except for translations
1232 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1235 m_matrix
->TransformVectors(&pt
);
1240 // returns the native representation
1241 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1246 //-----------------------------------------------------------------------------
1247 // wxGDIPlusContext implementation
1248 //-----------------------------------------------------------------------------
1250 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1251 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1253 class wxGDIPlusOffsetHelper
1256 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1261 m_gr
->TranslateTransform( 0.5, 0.5 );
1263 ~wxGDIPlusOffsetHelper( )
1266 m_gr
->TranslateTransform( -0.5, -0.5 );
1273 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1274 : wxGraphicsContext(renderer
)
1277 m_context
= new Graphics( hdc
);
1283 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
)
1284 : wxGraphicsContext(renderer
)
1288 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1289 HDC hdc
= (HDC
) msw
->GetHDC();
1291 m_context
= new Graphics(hdc
);
1292 wxSize sz
= dc
.GetSize();
1299 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1300 : wxGraphicsContext(renderer
)
1303 m_enableOffset
= true;
1304 m_context
= new Graphics( hwnd
);
1305 RECT rect
= wxGetWindowRect(hwnd
);
1306 m_width
= rect
.right
- rect
.left
;
1307 m_height
= rect
.bottom
- rect
.top
;
1311 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1312 : wxGraphicsContext(renderer
)
1319 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1324 void wxGDIPlusContext::Init()
1331 m_fontScaleRatio
= 1.0;
1334 void wxGDIPlusContext::SetDefaults()
1336 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1337 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1338 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1339 m_state1
= m_context
->Save();
1340 m_state2
= m_context
->Save();
1343 wxGDIPlusContext::~wxGDIPlusContext()
1347 m_context
->Restore( m_state2
);
1348 m_context
->Restore( m_state1
);
1354 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1356 Region
rgn((HRGN
)region
.GetHRGN());
1357 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1360 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1362 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1365 void wxGDIPlusContext::ResetClip()
1367 m_context
->ResetClip();
1370 void wxGDIPlusContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1372 if (m_composition
== wxCOMPOSITION_DEST
)
1375 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1376 Brush
*brush
= m_brush
.IsNull() ? NULL
: ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush();
1377 Pen
*pen
= m_pen
.IsNull() ? NULL
: ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen();
1381 // the offset is used to fill only the inside of the rectangle and not paint underneath
1382 // its border which may influence a transparent Pen
1385 offset
= pen
->GetWidth();
1386 m_context
->FillRectangle( brush
, (REAL
)x
+ offset
/2, (REAL
)y
+ offset
/2, (REAL
)w
- offset
, (REAL
)h
- offset
);
1391 m_context
->DrawRectangle( pen
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1395 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1397 if (m_composition
== wxCOMPOSITION_DEST
)
1400 if ( !m_pen
.IsNull() )
1402 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1403 Point
*cpoints
= new Point
[n
];
1404 for (size_t i
= 0; i
< n
; i
++)
1406 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1407 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1409 } // for (size_t i = 0; i < n; i++)
1410 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1415 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1417 if (m_composition
== wxCOMPOSITION_DEST
)
1420 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1421 Point
*cpoints
= new Point
[n
];
1422 for (size_t i
= 0; i
< n
; i
++)
1424 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1425 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1427 } // for (int i = 0; i < n; i++)
1428 if ( !m_brush
.IsNull() )
1429 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1430 if ( !m_pen
.IsNull() )
1431 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1435 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1437 if (m_composition
== wxCOMPOSITION_DEST
)
1440 if ( !m_pen
.IsNull() )
1442 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1443 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1447 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1449 if (m_composition
== wxCOMPOSITION_DEST
)
1452 if ( !m_brush
.IsNull() )
1454 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1455 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1456 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1457 (GraphicsPath
*) path
.GetNativePath());
1461 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1463 if (m_antialias
== antialias
)
1466 m_antialias
= antialias
;
1468 SmoothingMode antialiasMode
;
1471 case wxANTIALIAS_DEFAULT
:
1472 antialiasMode
= SmoothingModeHighQuality
;
1474 case wxANTIALIAS_NONE
:
1475 antialiasMode
= SmoothingModeNone
;
1480 m_context
->SetSmoothingMode(antialiasMode
);
1484 bool wxGDIPlusContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
1490 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1492 if ( m_composition
== op
)
1497 if (m_composition
== wxCOMPOSITION_DEST
)
1500 CompositingMode cop
;
1503 case wxCOMPOSITION_SOURCE
:
1504 cop
= CompositingModeSourceCopy
;
1506 case wxCOMPOSITION_OVER
:
1507 cop
= CompositingModeSourceOver
;
1513 m_context
->SetCompositingMode(cop
);
1517 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1522 void wxGDIPlusContext::EndLayer()
1527 void wxGDIPlusContext::Rotate( wxDouble angle
)
1529 m_context
->RotateTransform( RadToDeg(angle
) );
1532 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1534 m_context
->TranslateTransform( dx
, dy
);
1537 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1539 m_context
->ScaleTransform(xScale
,yScale
);
1542 void wxGDIPlusContext::PushState()
1544 GraphicsState state
= m_context
->Save();
1545 m_stateStack
.push(state
);
1548 void wxGDIPlusContext::PopState()
1550 wxCHECK_RET( !m_stateStack
.empty(), wxT("No state to pop") );
1552 GraphicsState state
= m_stateStack
.top();
1554 m_context
->Restore(state
);
1557 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1559 if (m_composition
== wxCOMPOSITION_DEST
)
1562 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1565 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1567 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1568 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1569 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth(), image
->GetHeight(), UnitPixel
) ;
1570 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1573 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1577 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1579 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1580 DrawBitmap(bitmap
, x
, y
, w
, h
);
1583 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1585 if (m_composition
== wxCOMPOSITION_DEST
)
1588 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1589 // find out by looking at the bitmap data whether there really was alpha in it
1590 HICON hIcon
= (HICON
)icon
.GetHICON();
1592 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1593 if (!GetIconInfo(hIcon
,&iconInfo
))
1596 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1598 Bitmap
* image
= NULL
;
1600 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1601 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1602 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1604 image
= Bitmap::FromHICON(hIcon
);
1608 size_t width
= interim
.GetWidth();
1609 size_t height
= interim
.GetHeight();
1610 Rect
bounds(0,0,width
,height
);
1613 interim
.LockBits(&bounds
, ImageLockModeRead
,
1614 interim
.GetPixelFormat(),&data
);
1616 bool hasAlpha
= false;
1617 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1619 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1621 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1622 if ( ( *dest
& Color::AlphaMask
) != 0 )
1629 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1630 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1634 image
= Bitmap::FromHICON(hIcon
);
1637 interim
.UnlockBits(&data
);
1640 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1643 DeleteObject(iconInfo
.hbmColor
);
1644 DeleteObject(iconInfo
.hbmMask
);
1647 wxGraphicsFont
wxGDIPlusContext::CreateFont( const wxFont
&font
,
1648 const wxColour
&col
) const
1650 wxGDIPlusRenderer
* renderer
=
1651 static_cast<wxGDIPlusRenderer
*>(GetRenderer());
1652 return renderer
->CreateGDIPlusFont(this, font
, col
);
1655 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1656 wxDouble x
, wxDouble y
,
1657 const wxGraphicsBrush
& brush
)
1659 if (m_composition
== wxCOMPOSITION_DEST
)
1662 wxCHECK_RET( !m_font
.IsNull(),
1663 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1668 wxGDIPlusFontData
* const
1669 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1670 wxGDIPlusBrushData
* const
1671 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1673 m_context
->DrawString
1675 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1676 -1, // length: string is NUL-terminated
1677 fontData
->GetGDIPlusFont(),
1679 StringFormat::GenericTypographic(),
1680 brushData
? brushData
->GetGDIPlusBrush()
1681 : fontData
->GetGDIPlusBrush()
1685 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1686 wxDouble
*descent
, wxDouble
*externalLeading
) const
1688 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1690 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1691 FontFamily ffamily
;
1692 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1694 f
->GetFamily(&ffamily
) ;
1696 REAL factorY
= m_fontScaleRatio
;
1698 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1699 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1700 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1701 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1702 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1703 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1706 *height
= rHeight
* factorY
;
1708 *descent
= rDescent
* factorY
;
1709 if ( externalLeading
)
1710 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1711 // measuring empty strings is not guaranteed, so do it by hand
1719 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1720 StringFormat
strFormat( StringFormat::GenericTypographic() );
1721 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1724 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1726 *width
= bounds
.Width
;
1728 *height
= bounds
.Height
;
1732 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1735 widths
.Add(0, text
.length());
1737 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1742 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1743 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1744 size_t len
= wcslen( ws
) ;
1745 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1747 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1748 StringFormat
strFormat( StringFormat::GenericTypographic() );
1750 size_t startPosition
= 0;
1751 size_t remainder
= len
;
1752 const size_t maxSpan
= 32;
1753 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1754 Region
* regions
= new Region
[maxSpan
];
1756 while( remainder
> 0 )
1758 size_t span
= wxMin( maxSpan
, remainder
);
1760 for( size_t i
= 0 ; i
< span
; ++i
)
1762 ranges
[i
].First
= 0 ;
1763 ranges
[i
].Length
= startPosition
+i
+1 ;
1765 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1766 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1767 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1770 for ( size_t i
= 0 ; i
< span
; ++i
)
1772 regions
[i
].GetBounds(&bbox
,m_context
);
1773 widths
[startPosition
+i
] = bbox
.Width
;
1776 startPosition
+= span
;
1783 bool wxGDIPlusContext::ShouldOffset() const
1785 if ( !m_enableOffset
)
1789 if ( !m_pen
.IsNull() )
1791 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1792 if ( penwidth
== 0 )
1795 return ( penwidth
% 2 ) == 1;
1798 void* wxGDIPlusContext::GetNativeContext()
1803 // concatenates this transform with the current transform of this context
1804 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1806 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1809 // sets the transform of this context
1810 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1812 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1815 // gets the matrix of this context
1816 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1818 wxGraphicsMatrix matrix
= CreateMatrix();
1819 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1823 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1829 //-----------------------------------------------------------------------------
1830 // wxGDIPlusPrintingContext implementation
1831 //-----------------------------------------------------------------------------
1833 wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
,
1835 : wxGDIPlusContext(renderer
, dc
)
1837 Graphics
* context
= GetGraphics();
1839 //m_context->SetPageUnit(UnitDocument);
1841 // Setup page scale, based on DPI ratio.
1842 // Antecedent should be 100dpi when the default page unit
1843 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1844 // instead. Note that calling SetPageScale() does not have effect on
1845 // non-printing DCs (that is, any other than wxPrinterDC or
1846 // wxEnhMetaFileDC).
1847 REAL dpiRatio
= 100.0 / context
->GetDpiY();
1848 context
->SetPageScale(dpiRatio
);
1850 // We use this modifier when measuring fonts. It is needed because the
1851 // page scale is modified above.
1852 m_fontScaleRatio
= context
->GetDpiY() / 72.0;
1855 //-----------------------------------------------------------------------------
1856 // wxGDIPlusRenderer implementation
1857 //-----------------------------------------------------------------------------
1859 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1861 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1863 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1865 return &gs_GDIPlusRenderer
;
1868 bool wxGDIPlusRenderer::EnsureIsLoaded()
1870 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1871 // if we already tried and failed (we don't want to spend lot of time
1872 // returning NULL from wxGraphicsContext::Create(), which may be called
1873 // relatively frequently):
1874 if ( m_loaded
== -1 )
1879 return m_loaded
== 1;
1882 // call EnsureIsLoaded() and return returnOnFail value if it fails
1883 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1884 if ( !EnsureIsLoaded() ) \
1885 return (returnOnFail)
1888 void wxGDIPlusRenderer::Load()
1890 GdiplusStartupInput input
;
1891 GdiplusStartupOutput output
;
1892 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1894 wxLogTrace("gdiplus", "successfully initialized GDI+");
1899 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1904 void wxGDIPlusRenderer::Unload()
1908 GdiplusShutdown(m_gditoken
);
1911 m_loaded
= -1; // next Load() will try again
1914 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1916 ENSURE_LOADED_OR_RETURN(NULL
);
1917 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1918 context
->EnableOffset(true);
1922 #if wxUSE_PRINTING_ARCHITECTURE
1923 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1925 ENSURE_LOADED_OR_RETURN(NULL
);
1926 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1931 #if wxUSE_ENH_METAFILE
1932 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1934 ENSURE_LOADED_OR_RETURN(NULL
);
1935 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1940 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1942 ENSURE_LOADED_OR_RETURN(NULL
);
1943 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1944 context
->EnableOffset(true);
1948 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1950 ENSURE_LOADED_OR_RETURN(NULL
);
1951 return new wxGDIPlusMeasuringContext(this);
1954 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1956 ENSURE_LOADED_OR_RETURN(NULL
);
1957 return new wxGDIPlusContext(this,(Graphics
*) context
);
1961 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1963 ENSURE_LOADED_OR_RETURN(NULL
);
1964 return new wxGDIPlusContext(this,(HWND
) window
);
1967 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1969 ENSURE_LOADED_OR_RETURN(NULL
);
1970 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1975 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1977 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1979 m
.SetRefData( new wxGDIPlusPathData(this));
1986 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1987 wxDouble tx
, wxDouble ty
)
1990 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1992 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1993 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1998 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
2000 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
2001 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
2002 return wxNullGraphicsPen
;
2006 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
2011 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
2013 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2014 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
2015 return wxNullGraphicsBrush
;
2019 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
2025 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2026 wxDouble x2
, wxDouble y2
,
2027 const wxGraphicsGradientStops
& stops
)
2029 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2031 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2032 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2038 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2039 wxDouble xc
, wxDouble yc
,
2041 const wxGraphicsGradientStops
& stops
)
2043 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2045 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2046 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
2052 wxGDIPlusRenderer::CreateGDIPlusFont( const wxGDIPlusContext
* gc
,
2054 const wxColour
&col
)
2056 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2060 p
.SetRefData(new wxGDIPlusFontData( this, gc
, font
, col
));
2064 return wxNullGraphicsFont
;
2067 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
2069 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2070 if ( bitmap
.IsOk() )
2073 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
2077 return wxNullGraphicsBitmap
;
2080 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
2082 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2083 if ( bitmap
!= NULL
)
2086 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
2090 return wxNullGraphicsBitmap
;
2093 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2095 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2096 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
2100 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
2104 return wxNullGraphicsBitmap
;
2107 // Shutdown GDI+ at app exit, before possible dll unload
2108 class wxGDIPlusRendererModule
: public wxModule
2111 virtual bool OnInit() { return true; }
2112 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
2115 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
2118 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
2120 // ----------------------------------------------------------------------------
2121 // wxMSW-specific parts of wxGCDC
2122 // ----------------------------------------------------------------------------
2124 WXHDC
wxGCDC::AcquireHDC()
2126 wxGraphicsContext
* const gc
= GetGraphicsContext();
2130 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2131 return g
? g
->GetHDC() : NULL
;
2134 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2139 wxGraphicsContext
* const gc
= GetGraphicsContext();
2140 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2142 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2143 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2145 g
->ReleaseHDC((HDC
)hdc
);
2148 #endif // wxUSE_GRAPHICS_CONTEXT