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
; }
302 // Common part of all ctors, flags here is a combination of values of
303 // FontStyle GDI+ enum.
304 void Init(const wxString
& name
,
308 Unit fontUnit
= UnitPixel
);
314 class wxGDIPlusContext
: public wxGraphicsContext
317 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
318 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
319 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
320 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
321 wxGDIPlusContext(wxGraphicsRenderer
* renderer
);
323 virtual ~wxGDIPlusContext();
325 virtual void Clip( const wxRegion
®ion
);
326 // clips drawings to the rect
327 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
329 // resets the clipping to original extent
330 virtual void ResetClip();
332 virtual void * GetNativeContext();
334 virtual void StrokePath( const wxGraphicsPath
& p
);
335 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
337 virtual void DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
339 // stroke lines connecting each of the points
340 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
343 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
345 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
347 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation
);
349 virtual bool SetCompositionMode(wxCompositionMode op
);
351 virtual void BeginLayer(wxDouble opacity
);
353 virtual void EndLayer();
355 virtual void Translate( wxDouble dx
, wxDouble dy
);
356 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
357 virtual void Rotate( wxDouble angle
);
359 // concatenates this transform with the current transform of this context
360 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
362 // sets the transform of this context
363 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
365 // gets the matrix of this context
366 virtual wxGraphicsMatrix
GetTransform() const;
368 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
369 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
370 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
371 virtual void PushState();
372 virtual void PopState();
374 // sets the font of this context
375 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) const;
377 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
378 wxDouble
*descent
, wxDouble
*externalLeading
) const;
379 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
380 virtual bool ShouldOffset() const;
381 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
383 Graphics
* GetGraphics() const { return m_context
; }
387 wxDouble m_fontScaleRatio
;
389 // Used from ctors (including those in the derived classes) and takes
390 // ownership of the graphics pointer that must be non-NULL.
391 void Init(Graphics
* graphics
, int width
, int height
);
394 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
395 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
396 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
397 const wxGraphicsBrush
& backgroundBrush
);
400 wxStack
<GraphicsState
> m_stateStack
;
401 GraphicsState m_state1
;
402 GraphicsState m_state2
;
404 wxDECLARE_NO_COPY_CLASS(wxGDIPlusContext
);
407 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
410 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
414 virtual ~wxGDIPlusMeasuringContext()
416 ReleaseDC( NULL
, m_hdc
);
423 class wxGDIPlusPrintingContext
: public wxGDIPlusContext
426 wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
427 virtual ~wxGDIPlusPrintingContext() { }
431 //-----------------------------------------------------------------------------
432 // wxGDIPlusRenderer declaration
433 //-----------------------------------------------------------------------------
435 class wxGDIPlusRenderer
: public wxGraphicsRenderer
444 virtual ~wxGDIPlusRenderer()
454 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
456 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
458 #if wxUSE_PRINTING_ARCHITECTURE
459 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
462 #if wxUSE_ENH_METAFILE
463 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
466 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
468 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
470 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
472 virtual wxGraphicsContext
* CreateMeasuringContext();
476 virtual wxGraphicsPath
CreatePath();
480 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
481 wxDouble tx
=0.0, wxDouble ty
=0.0);
484 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
486 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
488 virtual wxGraphicsBrush
489 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
490 wxDouble x2
, wxDouble y2
,
491 const wxGraphicsGradientStops
& stops
);
493 virtual wxGraphicsBrush
494 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
495 wxDouble xc
, wxDouble yc
,
497 const wxGraphicsGradientStops
& stops
);
499 // create a native bitmap representation
500 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
502 // stub: should not be called directly
503 virtual wxGraphicsFont
CreateFont( const wxFont
& WXUNUSED(font
),
504 const wxColour
& WXUNUSED(col
) )
505 { wxFAIL
; return wxNullGraphicsFont
; }
507 // this is used to really create the font
508 wxGraphicsFont
CreateGDIPlusFont( const wxGDIPlusContext
* gc
,
510 const wxColour
&col
);
512 // create a graphics bitmap from a native bitmap
513 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
515 // create a subimage from a native image representation
516 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
519 bool EnsureIsLoaded();
522 friend class wxGDIPlusRendererModule
;
526 ULONG_PTR m_gditoken
;
528 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
531 //-----------------------------------------------------------------------------
532 // wxGDIPlusPen implementation
533 //-----------------------------------------------------------------------------
535 wxGDIPlusPenData::~wxGDIPlusPenData()
542 void wxGDIPlusPenData::Init()
549 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
550 : wxGraphicsObjectRefData(renderer
)
553 m_width
= pen
.GetWidth();
557 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
560 switch ( pen
.GetCap() )
566 case wxCAP_PROJECTING
:
571 cap
= LineCapFlat
; // TODO verify
578 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
581 switch ( pen
.GetJoin() )
584 join
= LineJoinBevel
;
588 join
= LineJoinMiter
;
592 join
= LineJoinRound
;
596 join
= LineJoinMiter
;
600 m_pen
->SetLineJoin(join
);
602 m_pen
->SetDashStyle(DashStyleSolid
);
604 DashStyle dashStyle
= DashStyleSolid
;
605 switch ( pen
.GetStyle() )
607 case wxPENSTYLE_SOLID
:
610 case wxPENSTYLE_DOT
:
611 dashStyle
= DashStyleDot
;
614 case wxPENSTYLE_LONG_DASH
:
615 dashStyle
= DashStyleDash
; // TODO verify
618 case wxPENSTYLE_SHORT_DASH
:
619 dashStyle
= DashStyleDash
;
622 case wxPENSTYLE_DOT_DASH
:
623 dashStyle
= DashStyleDashDot
;
625 case wxPENSTYLE_USER_DASH
:
627 dashStyle
= DashStyleCustom
;
629 int count
= pen
.GetDashes( &dashes
);
630 if ((dashes
!= NULL
) && (count
> 0))
632 REAL
*userLengths
= new REAL
[count
];
633 for ( int i
= 0; i
< count
; ++i
)
635 userLengths
[i
] = dashes
[i
];
637 m_pen
->SetDashPattern( userLengths
, count
);
638 delete[] userLengths
;
642 case wxPENSTYLE_STIPPLE
:
644 wxBitmap
* bmp
= pen
.GetStipple();
645 if ( bmp
&& bmp
->IsOk() )
647 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
649 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
654 m_penBrush
= new TextureBrush(m_penImage
);
655 m_pen
->SetBrush( m_penBrush
);
661 if ( pen
.GetStyle() >= wxPENSTYLE_FIRST_HATCH
&&
662 pen
.GetStyle() <= wxPENSTYLE_LAST_HATCH
)
665 switch( pen
.GetStyle() )
667 case wxPENSTYLE_BDIAGONAL_HATCH
:
668 style
= HatchStyleBackwardDiagonal
;
670 case wxPENSTYLE_CROSSDIAG_HATCH
:
671 style
= HatchStyleDiagonalCross
;
673 case wxPENSTYLE_FDIAGONAL_HATCH
:
674 style
= HatchStyleForwardDiagonal
;
676 case wxPENSTYLE_CROSS_HATCH
:
677 style
= HatchStyleCross
;
679 case wxPENSTYLE_HORIZONTAL_HATCH
:
680 style
= HatchStyleHorizontal
;
682 case wxPENSTYLE_VERTICAL_HATCH
:
683 style
= HatchStyleVertical
;
686 style
= HatchStyleHorizontal
;
688 m_penBrush
= new HatchBrush
691 wxColourToColor(pen
.GetColour()),
694 m_pen
->SetBrush( m_penBrush
);
698 if ( dashStyle
!= DashStyleSolid
)
699 m_pen
->SetDashStyle(dashStyle
);
702 //-----------------------------------------------------------------------------
703 // wxGDIPlusBrush implementation
704 //-----------------------------------------------------------------------------
706 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
707 : wxGraphicsObjectRefData(renderer
)
712 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
713 : wxGraphicsObjectRefData(renderer
)
716 if ( brush
.GetStyle() == wxSOLID
)
718 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
720 else if ( brush
.IsHatch() )
723 switch( brush
.GetStyle() )
725 case wxBRUSHSTYLE_BDIAGONAL_HATCH
:
726 style
= HatchStyleBackwardDiagonal
;
728 case wxBRUSHSTYLE_CROSSDIAG_HATCH
:
729 style
= HatchStyleDiagonalCross
;
731 case wxBRUSHSTYLE_FDIAGONAL_HATCH
:
732 style
= HatchStyleForwardDiagonal
;
734 case wxBRUSHSTYLE_CROSS_HATCH
:
735 style
= HatchStyleCross
;
737 case wxBRUSHSTYLE_HORIZONTAL_HATCH
:
738 style
= HatchStyleHorizontal
;
740 case wxBRUSHSTYLE_VERTICAL_HATCH
:
741 style
= HatchStyleVertical
;
744 style
= HatchStyleHorizontal
;
746 m_brush
= new HatchBrush
749 wxColourToColor(brush
.GetColour()),
755 wxBitmap
* bmp
= brush
.GetStipple();
756 if ( bmp
&& bmp
->IsOk() )
758 wxDELETE( m_brushImage
);
759 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
761 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
766 m_brush
= new TextureBrush(m_brushImage
);
771 wxGDIPlusBrushData::~wxGDIPlusBrushData()
778 void wxGDIPlusBrushData::Init()
785 template <typename T
>
787 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
788 const wxGraphicsGradientStops
& stops
)
790 const unsigned numStops
= stops
.GetCount();
793 // initial and final colours are set during the brush creation, nothing
798 wxVector
<Color
> colors(numStops
);
799 wxVector
<REAL
> positions(numStops
);
801 for ( unsigned i
= 0; i
< numStops
; i
++ )
803 wxGraphicsGradientStop stop
= stops
.Item(i
);
805 colors
[i
] = wxColourToColor(stop
.GetColour());
806 positions
[i
] = stop
.GetPosition();
809 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
813 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
814 wxDouble x2
, wxDouble y2
,
815 const wxGraphicsGradientStops
& stops
)
817 LinearGradientBrush
* const
818 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
819 wxColourToColor(stops
.GetStartColour()),
820 wxColourToColor(stops
.GetEndColour()));
823 SetGradientStops(brush
, stops
);
827 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
828 wxDouble xc
, wxDouble yc
,
830 const wxGraphicsGradientStops
& stops
)
832 m_brushPath
= new GraphicsPath();
833 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
834 (REAL
)(2*radius
), (REAL
)(2*radius
));
836 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
838 brush
->SetCenterPoint(PointF(xo
, yo
));
839 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
841 const Color
col(wxColourToColor(stops
.GetEndColour()));
843 brush
->SetSurroundColors(&col
, &count
);
845 SetGradientStops(brush
, stops
);
848 //-----------------------------------------------------------------------------
849 // wxGDIPlusFont implementation
850 //-----------------------------------------------------------------------------
853 wxGDIPlusFontData::Init(const wxString
& name
,
859 // This scaling is needed when we use unit other than the
860 // default UnitPoint. It works for both display and printing.
861 size
*= 100.0f
/ 72.0f
;
863 m_font
= new Font(name
, size
, style
, fontUnit
);
865 m_textBrush
= new SolidBrush(wxColourToColor(col
));
868 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
869 const wxGDIPlusContext
* gc
,
871 const wxColour
& col
)
872 : wxGraphicsObjectRefData( renderer
)
874 int style
= FontStyleRegular
;
875 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
876 style
|= FontStyleItalic
;
877 if ( font
.GetUnderlined() )
878 style
|= FontStyleUnderline
;
879 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
880 style
|= FontStyleBold
;
882 Graphics
* context
= gc
->GetGraphics();
884 Unit fontUnit
= context
->GetPageUnit();
885 // if fontUnit is UnitDisplay, then specify UnitPixel, otherwise
886 // you'll get a "InvalidParameter" from GDI+
887 if ( fontUnit
== UnitDisplay
)
888 fontUnit
= UnitPixel
;
890 // NB: font unit should match context's unit. We can use UnitPixel,
891 // as that is what the print context should use.
892 Init(font
.GetFaceName(), font
.GetPointSize(), style
, col
, fontUnit
);
895 wxGDIPlusFontData::~wxGDIPlusFontData()
901 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
902 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
903 // bytes as parameter, since there is no real copying of the data going in, only references are stored
904 // m_helper has to be kept alive as well
906 //-----------------------------------------------------------------------------
907 // wxGDIPlusBitmapData implementation
908 //-----------------------------------------------------------------------------
910 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
911 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
916 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
917 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
922 Bitmap
* image
= NULL
;
925 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
927 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
933 size_t width
= interim
.GetWidth();
934 size_t height
= interim
.GetHeight();
935 Rect
bounds(0,0,width
,height
);
937 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
939 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
940 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
942 BitmapData dataMask
;
943 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
944 interimMask
.GetPixelFormat(),&dataMask
);
947 BitmapData imageData
;
948 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
950 BYTE maskPattern
= 0 ;
954 for ( size_t y
= 0 ; y
< height
; ++y
)
957 for( size_t x
= 0 ; x
< width
; ++x
)
962 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
966 maskPattern
= maskPattern
>> 1;
968 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
969 if ( (maskByte
& maskPattern
) == 0 )
974 interim
.GetPixel(x
,y
,&c
) ;
975 *dest
= (c
.GetValue() | Color::AlphaMask
);
980 image
->UnlockBits(&imageData
);
982 interimMask
.UnlockBits(&dataMask
);
983 interim
.UnlockBits(&dataMask
);
987 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
989 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
994 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
996 size_t width
= image
->GetWidth();
997 size_t height
= image
->GetHeight();
998 Rect
bounds(0,0,width
,height
);
999 static BitmapData data
;
1003 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
1004 m_helper
->GetPixelFormat(),&data
);
1006 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1007 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
1009 m_helper
->UnlockBits(&data
);
1016 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1022 //-----------------------------------------------------------------------------
1023 // wxGDIPlusPath implementation
1024 //-----------------------------------------------------------------------------
1026 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
1031 m_path
= new GraphicsPath();
1034 wxGDIPlusPathData::~wxGDIPlusPathData()
1039 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
1041 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
1048 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1050 m_path
->StartFigure();
1051 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1054 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1056 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1059 void wxGDIPlusPathData::CloseSubpath()
1061 m_path
->CloseFigure();
1064 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1070 m_path
->GetLastPoint(&start
);
1071 m_path
->AddBezier(start
,c1
,c2
,end
);
1074 // gets the last point of the current path, (0,0) if not yet set
1075 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1078 m_path
->GetLastPoint(&start
);
1083 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1085 double sweepAngle
= endAngle
- startAngle
;
1086 if( fabs(sweepAngle
) >= 2*M_PI
)
1088 sweepAngle
= 2 * M_PI
;
1094 if( sweepAngle
< 0 )
1095 sweepAngle
+= 2 * M_PI
;
1099 if( sweepAngle
> 0 )
1100 sweepAngle
-= 2 * M_PI
;
1104 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
1107 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1109 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
1112 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
1114 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
1118 // transforms each point of this path by the matrix
1119 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1121 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
1124 // gets the bounding box enclosing all points (possibly including control points)
1125 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1128 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
1135 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1137 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1138 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
1141 //-----------------------------------------------------------------------------
1142 // wxGDIPlusMatrixData implementation
1143 //-----------------------------------------------------------------------------
1145 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
1146 : wxGraphicsMatrixData(renderer
)
1151 m_matrix
= new Matrix();
1154 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1159 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1161 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1164 // concatenates the matrix
1165 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1167 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1170 // sets the matrix to the respective values
1171 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1172 wxDouble tx
, wxDouble ty
)
1174 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1177 // gets the component valuess of the matrix
1178 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1179 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1182 m_matrix
->GetElements(elements
);
1183 if (a
) *a
= elements
[0];
1184 if (b
) *b
= elements
[1];
1185 if (c
) *c
= elements
[2];
1186 if (d
) *d
= elements
[3];
1187 if (tx
) *tx
= elements
[4];
1188 if (ty
) *ty
= elements
[5];
1191 // makes this the inverse matrix
1192 void wxGDIPlusMatrixData::Invert()
1197 // returns true if the elements of the transformation matrix are equal ?
1198 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1200 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1203 // return true if this is the identity matrix
1204 bool wxGDIPlusMatrixData::IsIdentity() const
1206 return m_matrix
->IsIdentity() == TRUE
;
1213 // add the translation to this matrix
1214 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1216 m_matrix
->Translate(dx
,dy
);
1219 // add the scale to this matrix
1220 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1222 m_matrix
->Scale(xScale
,yScale
);
1225 // add the rotation to this matrix (radians)
1226 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1228 m_matrix
->Rotate( RadToDeg(angle
) );
1232 // apply the transforms
1235 // applies that matrix to the point
1236 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1239 m_matrix
->TransformPoints(&pt
);
1244 // applies the matrix except for translations
1245 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1248 m_matrix
->TransformVectors(&pt
);
1253 // returns the native representation
1254 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1259 //-----------------------------------------------------------------------------
1260 // wxGDIPlusContext implementation
1261 //-----------------------------------------------------------------------------
1263 class wxGDIPlusOffsetHelper
1266 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1271 m_gr
->TranslateTransform( 0.5, 0.5 );
1273 ~wxGDIPlusOffsetHelper( )
1276 m_gr
->TranslateTransform( -0.5, -0.5 );
1283 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1284 : wxGraphicsContext(renderer
)
1286 Init(new Graphics(hdc
), width
, height
);
1289 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
)
1290 : wxGraphicsContext(renderer
)
1292 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1293 HDC hdc
= (HDC
) msw
->GetHDC();
1294 wxSize sz
= dc
.GetSize();
1296 Init(new Graphics(hdc
), sz
.x
, sz
.y
);
1299 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1300 : wxGraphicsContext(renderer
)
1302 RECT rect
= wxGetWindowRect(hwnd
);
1303 Init(new Graphics(hwnd
), rect
.right
- rect
.left
, rect
.bottom
- rect
.top
);
1304 m_enableOffset
= true;
1307 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1308 : wxGraphicsContext(renderer
)
1313 wxGDIPlusContext::wxGDIPlusContext(wxGraphicsRenderer
* renderer
)
1314 : wxGraphicsContext(renderer
)
1316 // Derived class must call Init() later but just set m_context to NULL for
1317 // safety to avoid crashing in our dtor if Init() ends up not being called.
1321 void wxGDIPlusContext::Init(Graphics
* graphics
, int width
, int height
)
1323 m_context
= graphics
;
1328 m_fontScaleRatio
= 1.0;
1330 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1331 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1332 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1333 m_state1
= m_context
->Save();
1334 m_state2
= m_context
->Save();
1337 wxGDIPlusContext::~wxGDIPlusContext()
1341 m_context
->Restore( m_state2
);
1342 m_context
->Restore( m_state1
);
1348 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1350 Region
rgn((HRGN
)region
.GetHRGN());
1351 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1354 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1356 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1359 void wxGDIPlusContext::ResetClip()
1361 m_context
->ResetClip();
1364 void wxGDIPlusContext::DrawRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1366 if (m_composition
== wxCOMPOSITION_DEST
)
1369 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1370 Brush
*brush
= m_brush
.IsNull() ? NULL
: ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush();
1371 Pen
*pen
= m_pen
.IsNull() ? NULL
: ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen();
1375 // the offset is used to fill only the inside of the rectangle and not paint underneath
1376 // its border which may influence a transparent Pen
1379 offset
= pen
->GetWidth();
1380 m_context
->FillRectangle( brush
, (REAL
)x
+ offset
/2, (REAL
)y
+ offset
/2, (REAL
)w
- offset
, (REAL
)h
- offset
);
1385 m_context
->DrawRectangle( pen
, (REAL
)x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1389 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1391 if (m_composition
== wxCOMPOSITION_DEST
)
1394 if ( !m_pen
.IsNull() )
1396 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1397 Point
*cpoints
= new Point
[n
];
1398 for (size_t i
= 0; i
< n
; i
++)
1400 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1401 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1403 } // for (size_t i = 0; i < n; i++)
1404 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1409 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1411 if (m_composition
== wxCOMPOSITION_DEST
)
1414 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1415 Point
*cpoints
= new Point
[n
];
1416 for (size_t i
= 0; i
< n
; i
++)
1418 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1419 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1421 } // for (int i = 0; i < n; i++)
1422 if ( !m_brush
.IsNull() )
1423 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1424 if ( !m_pen
.IsNull() )
1425 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1429 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1431 if (m_composition
== wxCOMPOSITION_DEST
)
1434 if ( !m_pen
.IsNull() )
1436 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1437 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1441 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1443 if (m_composition
== wxCOMPOSITION_DEST
)
1446 if ( !m_brush
.IsNull() )
1448 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1449 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1450 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1451 (GraphicsPath
*) path
.GetNativePath());
1455 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1457 if (m_antialias
== antialias
)
1460 m_antialias
= antialias
;
1462 SmoothingMode antialiasMode
;
1465 case wxANTIALIAS_DEFAULT
:
1466 antialiasMode
= SmoothingModeHighQuality
;
1468 case wxANTIALIAS_NONE
:
1469 antialiasMode
= SmoothingModeNone
;
1474 m_context
->SetSmoothingMode(antialiasMode
);
1478 bool wxGDIPlusContext::SetInterpolationQuality(wxInterpolationQuality
WXUNUSED(interpolation
))
1484 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1486 if ( m_composition
== op
)
1491 if (m_composition
== wxCOMPOSITION_DEST
)
1494 CompositingMode cop
;
1497 case wxCOMPOSITION_SOURCE
:
1498 cop
= CompositingModeSourceCopy
;
1500 case wxCOMPOSITION_OVER
:
1501 cop
= CompositingModeSourceOver
;
1507 m_context
->SetCompositingMode(cop
);
1511 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1516 void wxGDIPlusContext::EndLayer()
1521 void wxGDIPlusContext::Rotate( wxDouble angle
)
1523 m_context
->RotateTransform( RadToDeg(angle
) );
1526 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1528 m_context
->TranslateTransform( dx
, dy
);
1531 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1533 m_context
->ScaleTransform(xScale
,yScale
);
1536 void wxGDIPlusContext::PushState()
1538 GraphicsState state
= m_context
->Save();
1539 m_stateStack
.push(state
);
1542 void wxGDIPlusContext::PopState()
1544 wxCHECK_RET( !m_stateStack
.empty(), wxT("No state to pop") );
1546 GraphicsState state
= m_stateStack
.top();
1548 m_context
->Restore(state
);
1551 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1553 if (m_composition
== wxCOMPOSITION_DEST
)
1556 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1559 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1561 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1562 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1563 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth(), image
->GetHeight(), UnitPixel
) ;
1564 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1567 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1571 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1573 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1574 DrawBitmap(bitmap
, x
, y
, w
, h
);
1577 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1579 if (m_composition
== wxCOMPOSITION_DEST
)
1582 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1583 // find out by looking at the bitmap data whether there really was alpha in it
1584 HICON hIcon
= (HICON
)icon
.GetHICON();
1586 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1587 if (!GetIconInfo(hIcon
,&iconInfo
))
1590 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1592 Bitmap
* image
= NULL
;
1594 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1595 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1596 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1598 image
= Bitmap::FromHICON(hIcon
);
1602 size_t width
= interim
.GetWidth();
1603 size_t height
= interim
.GetHeight();
1604 Rect
bounds(0,0,width
,height
);
1607 interim
.LockBits(&bounds
, ImageLockModeRead
,
1608 interim
.GetPixelFormat(),&data
);
1610 bool hasAlpha
= false;
1611 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1613 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1615 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1616 if ( ( *dest
& Color::AlphaMask
) != 0 )
1623 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1624 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1628 image
= Bitmap::FromHICON(hIcon
);
1631 interim
.UnlockBits(&data
);
1634 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1637 DeleteObject(iconInfo
.hbmColor
);
1638 DeleteObject(iconInfo
.hbmMask
);
1641 wxGraphicsFont
wxGDIPlusContext::CreateFont( const wxFont
&font
,
1642 const wxColour
&col
) const
1644 wxGDIPlusRenderer
* renderer
=
1645 static_cast<wxGDIPlusRenderer
*>(GetRenderer());
1646 return renderer
->CreateGDIPlusFont(this, font
, col
);
1649 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1650 wxDouble x
, wxDouble y
,
1651 const wxGraphicsBrush
& brush
)
1653 if (m_composition
== wxCOMPOSITION_DEST
)
1656 wxCHECK_RET( !m_font
.IsNull(),
1657 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1662 wxGDIPlusFontData
* const
1663 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1664 wxGDIPlusBrushData
* const
1665 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1667 m_context
->DrawString
1669 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1670 -1, // length: string is NUL-terminated
1671 fontData
->GetGDIPlusFont(),
1673 StringFormat::GenericTypographic(),
1674 brushData
? brushData
->GetGDIPlusBrush()
1675 : fontData
->GetGDIPlusBrush()
1679 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1680 wxDouble
*descent
, wxDouble
*externalLeading
) const
1682 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1684 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1685 FontFamily ffamily
;
1686 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1688 f
->GetFamily(&ffamily
) ;
1690 REAL factorY
= m_fontScaleRatio
;
1692 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1693 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1694 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1695 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1696 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1697 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1700 *height
= rHeight
* factorY
;
1702 *descent
= rDescent
* factorY
;
1703 if ( externalLeading
)
1704 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1705 // measuring empty strings is not guaranteed, so do it by hand
1713 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1714 StringFormat
strFormat( StringFormat::GenericTypographic() );
1715 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1718 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1720 *width
= bounds
.Width
;
1722 *height
= bounds
.Height
;
1726 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1729 widths
.Add(0, text
.length());
1731 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1736 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1737 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1738 size_t len
= wcslen( ws
) ;
1739 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1741 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1742 StringFormat
strFormat( StringFormat::GenericTypographic() );
1744 size_t startPosition
= 0;
1745 size_t remainder
= len
;
1746 const size_t maxSpan
= 32;
1747 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1748 Region
* regions
= new Region
[maxSpan
];
1750 while( remainder
> 0 )
1752 size_t span
= wxMin( maxSpan
, remainder
);
1754 for( size_t i
= 0 ; i
< span
; ++i
)
1756 ranges
[i
].First
= 0 ;
1757 ranges
[i
].Length
= startPosition
+i
+1 ;
1759 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1760 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1761 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1764 for ( size_t i
= 0 ; i
< span
; ++i
)
1766 regions
[i
].GetBounds(&bbox
,m_context
);
1767 widths
[startPosition
+i
] = bbox
.Width
;
1770 startPosition
+= span
;
1777 bool wxGDIPlusContext::ShouldOffset() const
1779 if ( !m_enableOffset
)
1783 if ( !m_pen
.IsNull() )
1785 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1786 if ( penwidth
== 0 )
1789 return ( penwidth
% 2 ) == 1;
1792 void* wxGDIPlusContext::GetNativeContext()
1797 // concatenates this transform with the current transform of this context
1798 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1800 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1803 // sets the transform of this context
1804 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1806 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1809 // gets the matrix of this context
1810 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1812 wxGraphicsMatrix matrix
= CreateMatrix();
1813 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1817 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1823 //-----------------------------------------------------------------------------
1824 // wxGDIPlusPrintingContext implementation
1825 //-----------------------------------------------------------------------------
1827 wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
,
1829 : wxGDIPlusContext(renderer
, dc
)
1831 Graphics
* context
= GetGraphics();
1833 //m_context->SetPageUnit(UnitDocument);
1835 // Setup page scale, based on DPI ratio.
1836 // Antecedent should be 100dpi when the default page unit
1837 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1838 // instead. Note that calling SetPageScale() does not have effect on
1839 // non-printing DCs (that is, any other than wxPrinterDC or
1840 // wxEnhMetaFileDC).
1841 REAL dpiRatio
= 100.0 / context
->GetDpiY();
1842 context
->SetPageScale(dpiRatio
);
1844 // We use this modifier when measuring fonts. It is needed because the
1845 // page scale is modified above.
1846 m_fontScaleRatio
= context
->GetDpiY() / 72.0;
1849 //-----------------------------------------------------------------------------
1850 // wxGDIPlusRenderer implementation
1851 //-----------------------------------------------------------------------------
1853 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1855 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1857 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1859 return &gs_GDIPlusRenderer
;
1862 bool wxGDIPlusRenderer::EnsureIsLoaded()
1864 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1865 // if we already tried and failed (we don't want to spend lot of time
1866 // returning NULL from wxGraphicsContext::Create(), which may be called
1867 // relatively frequently):
1868 if ( m_loaded
== -1 )
1873 return m_loaded
== 1;
1876 // call EnsureIsLoaded() and return returnOnFail value if it fails
1877 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1878 if ( !EnsureIsLoaded() ) \
1879 return (returnOnFail)
1882 void wxGDIPlusRenderer::Load()
1884 GdiplusStartupInput input
;
1885 GdiplusStartupOutput output
;
1886 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1888 wxLogTrace("gdiplus", "successfully initialized GDI+");
1893 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1898 void wxGDIPlusRenderer::Unload()
1902 GdiplusShutdown(m_gditoken
);
1905 m_loaded
= -1; // next Load() will try again
1908 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1910 ENSURE_LOADED_OR_RETURN(NULL
);
1911 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1912 context
->EnableOffset(true);
1916 #if wxUSE_PRINTING_ARCHITECTURE
1917 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1919 ENSURE_LOADED_OR_RETURN(NULL
);
1920 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1925 #if wxUSE_ENH_METAFILE
1926 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1928 ENSURE_LOADED_OR_RETURN(NULL
);
1929 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1934 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1936 ENSURE_LOADED_OR_RETURN(NULL
);
1937 wxGDIPlusContext
* context
= new wxGDIPlusContext(this, dc
);
1938 context
->EnableOffset(true);
1942 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1944 ENSURE_LOADED_OR_RETURN(NULL
);
1945 return new wxGDIPlusMeasuringContext(this);
1948 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1950 ENSURE_LOADED_OR_RETURN(NULL
);
1951 return new wxGDIPlusContext(this,(Graphics
*) context
);
1955 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1957 ENSURE_LOADED_OR_RETURN(NULL
);
1958 return new wxGDIPlusContext(this,(HWND
) window
);
1961 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1963 ENSURE_LOADED_OR_RETURN(NULL
);
1964 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1969 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1971 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1973 m
.SetRefData( new wxGDIPlusPathData(this));
1980 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1981 wxDouble tx
, wxDouble ty
)
1984 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1986 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1987 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1992 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1994 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1995 if ( !pen
.IsOk() || pen
.GetStyle() == wxTRANSPARENT
)
1996 return wxNullGraphicsPen
;
2000 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
2005 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
2007 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2008 if ( !brush
.IsOk() || brush
.GetStyle() == wxTRANSPARENT
)
2009 return wxNullGraphicsBrush
;
2013 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
2019 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
2020 wxDouble x2
, wxDouble y2
,
2021 const wxGraphicsGradientStops
& stops
)
2023 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2025 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2026 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
2032 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
2033 wxDouble xc
, wxDouble yc
,
2035 const wxGraphicsGradientStops
& stops
)
2037 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2039 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2040 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
2046 wxGDIPlusRenderer::CreateGDIPlusFont( const wxGDIPlusContext
* gc
,
2048 const wxColour
&col
)
2050 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2054 p
.SetRefData(new wxGDIPlusFontData( this, gc
, font
, col
));
2058 return wxNullGraphicsFont
;
2061 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
2063 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2064 if ( bitmap
.IsOk() )
2067 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
2071 return wxNullGraphicsBitmap
;
2074 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
2076 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2077 if ( bitmap
!= NULL
)
2080 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
2084 return wxNullGraphicsBitmap
;
2087 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2089 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2090 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
2094 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
2098 return wxNullGraphicsBitmap
;
2101 // Shutdown GDI+ at app exit, before possible dll unload
2102 class wxGDIPlusRendererModule
: public wxModule
2105 virtual bool OnInit() { return true; }
2106 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
2109 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
2112 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
2114 // ----------------------------------------------------------------------------
2115 // wxMSW-specific parts of wxGCDC
2116 // ----------------------------------------------------------------------------
2118 WXHDC
wxGCDC::AcquireHDC()
2120 wxGraphicsContext
* const gc
= GetGraphicsContext();
2125 // we can't get the HDC if it is not a GDI+ context
2126 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2127 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2132 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2133 return g
? g
->GetHDC() : NULL
;
2136 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2141 wxGraphicsContext
* const gc
= GetGraphicsContext();
2142 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2145 // we can't get the HDC if it is not a GDI+ context
2146 wxGraphicsRenderer
* r1
= gc
->GetRenderer();
2147 wxGraphicsRenderer
* r2
= wxGraphicsRenderer::GetCairoRenderer();
2152 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2153 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2155 g
->ReleaseHDC((HDC
)hdc
);
2158 #endif // wxUSE_GRAPHICS_CONTEXT