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"
40 #include "wx/private/graphics.h"
41 #include "wx/msw/wrapgdip.h"
42 #include "wx/msw/dc.h"
46 WX_DECLARE_STACK(GraphicsState
, GraphicsStates
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 const double RAD2DEG
= 180.0 / M_PI
;
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
59 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
61 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
62 static inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
64 //-----------------------------------------------------------------------------
65 // device context implementation
67 // more and more of the dc functionality should be implemented by calling
68 // the appropricate wxGDIPlusContext, but we will have to do that step by step
69 // also coordinate conversions should be moved to native matrix ops
70 //-----------------------------------------------------------------------------
72 // we always stock two context states, one at entry, to be able to preserve the
73 // state we were called with, the other one after changing to HI Graphics orientation
74 // (this one is used for getting back clippings etc)
76 //-----------------------------------------------------------------------------
77 // wxGraphicsPath implementation
78 //-----------------------------------------------------------------------------
80 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
82 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
86 class wxGDIPlusPathData
: public wxGraphicsPathData
89 wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
92 virtual wxGraphicsObjectRefData
*Clone() const;
95 // These are the path primitives from which everything else can be constructed
98 // begins a new subpath at (x,y)
99 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
101 // adds a straight line from the current point to (x,y)
102 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
104 // adds a cubic Bezier curve from the current point, using two control points and an end point
105 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
108 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
109 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
111 // gets the last point of the current path, (0,0) if not yet set
112 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
115 virtual void AddPath( const wxGraphicsPathData
* path
);
117 // closes the current sub-path
118 virtual void CloseSubpath();
121 // These are convenience functions which - if not available natively will be assembled
122 // using the primitives from above
125 // appends a rectangle as a new closed subpath
126 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
129 // appends an ellipsis as a new closed subpath fitting the passed rectangle
130 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
132 // 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)
133 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
136 // returns the native path
137 virtual void * GetNativePath() const { return m_path
; }
139 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
140 virtual void UnGetNativePath(void * WXUNUSED(path
)) const {}
142 // transforms each point of this path by the matrix
143 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
145 // gets the bounding box enclosing all points (possibly including control points)
146 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
148 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
151 GraphicsPath
* m_path
;
154 class wxGDIPlusMatrixData
: public wxGraphicsMatrixData
157 wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
158 virtual ~wxGDIPlusMatrixData() ;
160 virtual wxGraphicsObjectRefData
* Clone() const ;
162 // concatenates the matrix
163 virtual void Concat( const wxGraphicsMatrixData
*t
);
165 // sets the matrix to the respective values
166 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
167 wxDouble tx
=0.0, wxDouble ty
=0.0);
169 // gets the component valuess of the matrix
170 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
171 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
173 // makes this the inverse matrix
174 virtual void Invert();
176 // returns true if the elements of the transformation matrix are equal ?
177 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
179 // return true if this is the identity matrix
180 virtual bool IsIdentity() const;
186 // add the translation to this matrix
187 virtual void Translate( wxDouble dx
, wxDouble dy
);
189 // add the scale to this matrix
190 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
192 // add the rotation to this matrix (radians)
193 virtual void Rotate( wxDouble angle
);
196 // apply the transforms
199 // applies that matrix to the point
200 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
202 // applies the matrix except for translations
203 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
205 // returns the native representation
206 virtual void * GetNativeMatrix() const;
211 class wxGDIPlusPenData
: public wxGraphicsObjectRefData
214 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
219 virtual wxDouble
GetWidth() { return m_width
; }
220 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
230 class wxGDIPlusBrushData
: public wxGraphicsObjectRefData
233 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
234 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
235 ~wxGDIPlusBrushData ();
237 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
238 const wxColour
&c1
, const wxColour
&c2
);
239 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
240 const wxColour
&oColor
, const wxColour
&cColor
);
241 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
249 GraphicsPath
* m_brushPath
;
252 class WXDLLIMPEXP_CORE wxGDIPlusBitmapData
: public wxGraphicsObjectRefData
255 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
);
256 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
&bmp
);
257 ~wxGDIPlusBitmapData ();
259 virtual Bitmap
* GetGDIPlusBitmap() { return m_bitmap
; }
266 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
269 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
270 ~wxGDIPlusFontData();
272 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
273 virtual Font
* GetGDIPlusFont() { return m_font
; }
279 class wxGDIPlusContext
: public wxGraphicsContext
282 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
283 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
284 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
287 virtual ~wxGDIPlusContext();
289 virtual void Clip( const wxRegion
®ion
);
290 // clips drawings to the rect
291 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
293 // resets the clipping to original extent
294 virtual void ResetClip();
296 virtual void * GetNativeContext();
298 virtual void StrokePath( const wxGraphicsPath
& p
);
299 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
301 // stroke lines connecting each of the points
302 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
305 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
307 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
309 virtual bool SetCompositionMode(wxCompositionMode op
);
311 virtual void BeginLayer(wxDouble opacity
);
313 virtual void EndLayer();
315 virtual void Translate( wxDouble dx
, wxDouble dy
);
316 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
317 virtual void Rotate( wxDouble angle
);
319 // concatenates this transform with the current transform of this context
320 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
322 // sets the transform of this context
323 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
325 // gets the matrix of this context
326 virtual wxGraphicsMatrix
GetTransform() const;
328 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
329 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
330 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
331 virtual void PushState();
332 virtual void PopState();
334 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
335 wxDouble
*descent
, wxDouble
*externalLeading
) const;
336 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
337 virtual bool ShouldOffset() const;
338 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
344 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
345 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
346 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
347 const wxGraphicsBrush
& backgroundBrush
);
350 GraphicsStates m_stateStack
;
351 GraphicsState m_state1
;
352 GraphicsState m_state2
;
357 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
360 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
363 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
366 wxGDIPlusMeasuringContext()
370 virtual ~wxGDIPlusMeasuringContext()
372 ReleaseDC( NULL
, m_hdc
);
377 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
380 //-----------------------------------------------------------------------------
381 // wxGDIPlusPen implementation
382 //-----------------------------------------------------------------------------
384 wxGDIPlusPenData::~wxGDIPlusPenData()
391 void wxGDIPlusPenData::Init()
398 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
399 : wxGraphicsObjectRefData(renderer
)
402 m_width
= pen
.GetWidth();
406 m_pen
= new Pen(Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
407 pen
.GetColour().Green() , pen
.GetColour().Blue() ), m_width
);
410 switch ( pen
.GetCap() )
416 case wxCAP_PROJECTING
:
421 cap
= LineCapFlat
; // TODO verify
428 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
431 switch ( pen
.GetJoin() )
434 join
= LineJoinBevel
;
438 join
= LineJoinMiter
;
442 join
= LineJoinRound
;
446 join
= LineJoinMiter
;
450 m_pen
->SetLineJoin(join
);
452 m_pen
->SetDashStyle(DashStyleSolid
);
454 DashStyle dashStyle
= DashStyleSolid
;
455 switch ( pen
.GetStyle() )
461 dashStyle
= DashStyleDot
;
465 dashStyle
= DashStyleDash
; // TODO verify
469 dashStyle
= DashStyleDash
;
473 dashStyle
= DashStyleDashDot
;
477 dashStyle
= DashStyleCustom
;
479 int count
= pen
.GetDashes( &dashes
);
480 if ((dashes
!= NULL
) && (count
> 0))
482 REAL
*userLengths
= new REAL
[count
];
483 for ( int i
= 0; i
< count
; ++i
)
485 userLengths
[i
] = dashes
[i
];
487 m_pen
->SetDashPattern( userLengths
, count
);
488 delete[] userLengths
;
494 wxBitmap
* bmp
= pen
.GetStipple();
495 if ( bmp
&& bmp
->Ok() )
497 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
498 m_penBrush
= new TextureBrush(m_penImage
);
499 m_pen
->SetBrush( m_penBrush
);
505 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
507 HatchStyle style
= HatchStyleHorizontal
;
508 switch( pen
.GetStyle() )
510 case wxBDIAGONAL_HATCH
:
511 style
= HatchStyleBackwardDiagonal
;
513 case wxCROSSDIAG_HATCH
:
514 style
= HatchStyleDiagonalCross
;
516 case wxFDIAGONAL_HATCH
:
517 style
= HatchStyleForwardDiagonal
;
520 style
= HatchStyleCross
;
522 case wxHORIZONTAL_HATCH
:
523 style
= HatchStyleHorizontal
;
525 case wxVERTICAL_HATCH
:
526 style
= HatchStyleVertical
;
530 m_penBrush
= new HatchBrush(style
,Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
531 pen
.GetColour().Green() , pen
.GetColour().Blue() ), Color::Transparent
);
532 m_pen
->SetBrush( m_penBrush
);
536 if ( dashStyle
!= DashStyleSolid
)
537 m_pen
->SetDashStyle(dashStyle
);
540 //-----------------------------------------------------------------------------
541 // wxGDIPlusBrush implementation
542 //-----------------------------------------------------------------------------
544 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
545 : wxGraphicsObjectRefData(renderer
)
550 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
551 : wxGraphicsObjectRefData(renderer
)
554 if ( brush
.GetStyle() == wxSOLID
)
556 m_brush
= new SolidBrush( Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
557 brush
.GetColour().Green() , brush
.GetColour().Blue() ) );
559 else if ( brush
.IsHatch() )
561 HatchStyle style
= HatchStyleHorizontal
;
562 switch( brush
.GetStyle() )
564 case wxBDIAGONAL_HATCH
:
565 style
= HatchStyleBackwardDiagonal
;
567 case wxCROSSDIAG_HATCH
:
568 style
= HatchStyleDiagonalCross
;
570 case wxFDIAGONAL_HATCH
:
571 style
= HatchStyleForwardDiagonal
;
574 style
= HatchStyleCross
;
576 case wxHORIZONTAL_HATCH
:
577 style
= HatchStyleHorizontal
;
579 case wxVERTICAL_HATCH
:
580 style
= HatchStyleVertical
;
584 m_brush
= new HatchBrush(style
,Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
585 brush
.GetColour().Green() , brush
.GetColour().Blue() ), Color::Transparent
);
589 wxBitmap
* bmp
= brush
.GetStipple();
590 if ( bmp
&& bmp
->Ok() )
592 wxDELETE( m_brushImage
);
593 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
594 m_brush
= new TextureBrush(m_brushImage
);
599 wxGDIPlusBrushData::~wxGDIPlusBrushData()
606 void wxGDIPlusBrushData::Init()
613 void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
615 m_brush
= new LinearGradientBrush( PointF( x1
,y1
) , PointF( x2
,y2
),
616 Color( c1
.Alpha(), c1
.Red(),c1
.Green() , c1
.Blue() ),
617 Color( c2
.Alpha(), c2
.Red(),c2
.Green() , c2
.Blue() ));
620 void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
621 const wxColour
&oColor
, const wxColour
&cColor
)
623 // Create a path that consists of a single circle.
624 m_brushPath
= new GraphicsPath();
625 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
), (REAL
)(2*radius
), (REAL
)(2*radius
));
627 PathGradientBrush
*b
= new PathGradientBrush(m_brushPath
);
629 b
->SetCenterPoint( PointF(xo
,yo
));
630 b
->SetCenterColor(Color( oColor
.Alpha(), oColor
.Red(),oColor
.Green() , oColor
.Blue() ));
632 Color colors
[] = {Color( cColor
.Alpha(), cColor
.Red(),cColor
.Green() , cColor
.Blue() )};
634 b
->SetSurroundColors(colors
, &count
);
637 //-----------------------------------------------------------------------------
638 // wxGDIPlusFont implementation
639 //-----------------------------------------------------------------------------
641 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
642 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
647 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
648 int size
= font
.GetPointSize();
649 int style
= FontStyleRegular
;
650 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
651 style
|= FontStyleItalic
;
652 if ( font
.GetUnderlined() )
653 style
|= FontStyleUnderline
;
654 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
655 style
|= FontStyleBold
;
656 m_font
= new Font( s
, size
, style
);
657 m_textBrush
= new SolidBrush( Color( col
.Alpha() , col
.Red() ,
658 col
.Green() , col
.Blue() ));
661 wxGDIPlusFontData::~wxGDIPlusFontData()
667 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
668 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
669 // bytes as parameter, since there is no real copying of the data going in, only references are stored
670 // m_helper has to be kept alive as well
672 //-----------------------------------------------------------------------------
673 // wxGDIPlusBitmapData implementation
674 //-----------------------------------------------------------------------------
676 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
677 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
682 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
683 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
688 Bitmap
* image
= NULL
;
691 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
693 size_t width
= interim
.GetWidth();
694 size_t height
= interim
.GetHeight();
695 Rect
bounds(0,0,width
,height
);
697 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
699 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
700 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
702 BitmapData dataMask
;
703 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
704 interimMask
.GetPixelFormat(),&dataMask
);
707 BitmapData imageData
;
708 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
710 BYTE maskPattern
= 0 ;
714 for ( size_t y
= 0 ; y
< height
; ++y
)
717 for( size_t x
= 0 ; x
< width
; ++x
)
722 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
726 maskPattern
= maskPattern
>> 1;
728 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
729 if ( (maskByte
& maskPattern
) == 0 )
734 interim
.GetPixel(x
,y
,&c
) ;
735 *dest
= (c
.GetValue() | Color::AlphaMask
);
740 image
->UnlockBits(&imageData
);
742 interimMask
.UnlockBits(&dataMask
);
743 interim
.UnlockBits(&dataMask
);
747 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
748 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
750 size_t width
= image
->GetWidth();
751 size_t height
= image
->GetHeight();
752 Rect
bounds(0,0,width
,height
);
753 static BitmapData data
;
757 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
758 m_helper
->GetPixelFormat(),&data
);
760 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
761 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
763 m_helper
->UnlockBits(&data
);
770 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
776 //-----------------------------------------------------------------------------
777 // wxGDIPlusPath implementation
778 //-----------------------------------------------------------------------------
780 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
785 m_path
= new GraphicsPath();
788 wxGDIPlusPathData::~wxGDIPlusPathData()
793 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
795 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
802 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
804 m_path
->StartFigure();
805 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
808 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
810 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
813 void wxGDIPlusPathData::CloseSubpath()
815 m_path
->CloseFigure();
818 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
824 m_path
->GetLastPoint(&start
);
825 m_path
->AddBezier(start
,c1
,c2
,end
);
828 // gets the last point of the current path, (0,0) if not yet set
829 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
832 m_path
->GetLastPoint(&start
);
837 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
839 double sweepAngle
= endAngle
- startAngle
;
840 if( fabs(sweepAngle
) >= 2*M_PI
)
842 sweepAngle
= 2 * M_PI
;
849 sweepAngle
+= 2 * M_PI
;
854 sweepAngle
-= 2 * M_PI
;
858 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
861 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
863 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
866 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
868 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
872 // transforms each point of this path by the matrix
873 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
875 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
878 // gets the bounding box enclosing all points (possibly including control points)
879 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
882 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
889 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
891 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
892 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
895 //-----------------------------------------------------------------------------
896 // wxGDIPlusMatrixData implementation
897 //-----------------------------------------------------------------------------
899 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
900 : wxGraphicsMatrixData(renderer
)
905 m_matrix
= new Matrix();
908 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
913 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
915 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
918 // concatenates the matrix
919 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
921 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
924 // sets the matrix to the respective values
925 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
926 wxDouble tx
, wxDouble ty
)
928 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
931 // gets the component valuess of the matrix
932 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
933 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
936 m_matrix
->GetElements(elements
);
937 if (a
) *a
= elements
[0];
938 if (b
) *b
= elements
[1];
939 if (c
) *c
= elements
[2];
940 if (d
) *d
= elements
[3];
941 if (tx
) *tx
= elements
[4];
942 if (ty
) *ty
= elements
[5];
945 // makes this the inverse matrix
946 void wxGDIPlusMatrixData::Invert()
951 // returns true if the elements of the transformation matrix are equal ?
952 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
954 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
957 // return true if this is the identity matrix
958 bool wxGDIPlusMatrixData::IsIdentity() const
960 return m_matrix
->IsIdentity() == TRUE
;
967 // add the translation to this matrix
968 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
970 m_matrix
->Translate(dx
,dy
);
973 // add the scale to this matrix
974 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
976 m_matrix
->Scale(xScale
,yScale
);
979 // add the rotation to this matrix (radians)
980 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
982 m_matrix
->Rotate( angle
);
986 // apply the transforms
989 // applies that matrix to the point
990 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
993 m_matrix
->TransformPoints(&pt
);
998 // applies the matrix except for translations
999 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1002 m_matrix
->TransformVectors(&pt
);
1007 // returns the native representation
1008 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1013 //-----------------------------------------------------------------------------
1014 // wxGDIPlusContext implementation
1015 //-----------------------------------------------------------------------------
1017 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1018 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1020 class wxGDIPlusOffsetHelper
1023 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1028 m_gr
->TranslateTransform( 0.5, 0.5 );
1030 ~wxGDIPlusOffsetHelper( )
1033 m_gr
->TranslateTransform( -0.5, -0.5 );
1040 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1041 : wxGraphicsContext(renderer
)
1044 m_context
= new Graphics( hdc
);
1050 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1051 : wxGraphicsContext(renderer
)
1054 m_context
= new Graphics( hwnd
);
1055 RECT rect
= wxGetWindowRect(hwnd
);
1056 m_width
= rect
.right
- rect
.left
;
1057 m_height
= rect
.bottom
- rect
.top
;
1061 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1062 : wxGraphicsContext(renderer
)
1069 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1074 void wxGDIPlusContext::Init()
1083 void wxGDIPlusContext::SetDefaults()
1085 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1086 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1087 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1088 m_state1
= m_context
->Save();
1089 m_state2
= m_context
->Save();
1092 wxGDIPlusContext::~wxGDIPlusContext()
1096 m_context
->Restore( m_state2
);
1097 m_context
->Restore( m_state1
);
1103 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1105 Region
rgn((HRGN
)region
.GetHRGN());
1106 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1109 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1111 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1114 void wxGDIPlusContext::ResetClip()
1116 m_context
->ResetClip();
1119 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1121 if (m_composition
== wxCOMPOSITION_DEST
)
1124 if ( !m_pen
.IsNull() )
1126 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1127 Point
*cpoints
= new Point
[n
];
1128 for (size_t i
= 0; i
< n
; i
++)
1130 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1131 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1133 } // for (size_t i = 0; i < n; i++)
1134 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1139 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1141 if (m_composition
== wxCOMPOSITION_DEST
)
1144 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1145 Point
*cpoints
= new Point
[n
];
1146 for (size_t i
= 0; i
< n
; i
++)
1148 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1149 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1151 } // for (int i = 0; i < n; i++)
1152 if ( !m_brush
.IsNull() )
1153 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1154 if ( !m_pen
.IsNull() )
1155 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1159 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1161 if (m_composition
== wxCOMPOSITION_DEST
)
1164 if ( !m_pen
.IsNull() )
1166 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1167 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1171 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1173 if (m_composition
== wxCOMPOSITION_DEST
)
1176 if ( !m_brush
.IsNull() )
1178 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1179 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1180 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1181 (GraphicsPath
*) path
.GetNativePath());
1185 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1187 if (m_antialias
== antialias
)
1190 m_antialias
= antialias
;
1192 SmoothingMode antialiasMode
;
1195 case wxANTIALIAS_DEFAULT
:
1196 antialiasMode
= SmoothingModeHighQuality
;
1198 case wxANTIALIAS_NONE
:
1199 antialiasMode
= SmoothingModeNone
;
1204 m_context
->SetSmoothingMode(antialiasMode
);
1208 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1210 if ( m_composition
== op
)
1215 if (m_composition
== wxCOMPOSITION_DEST
)
1218 CompositingMode cop
;
1221 case wxCOMPOSITION_SOURCE
:
1222 cop
= CompositingModeSourceCopy
;
1224 case wxCOMPOSITION_OVER
:
1225 cop
= CompositingModeSourceOver
;
1231 m_context
->SetCompositingMode(cop
);
1235 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1240 void wxGDIPlusContext::EndLayer()
1245 void wxGDIPlusContext::Rotate( wxDouble angle
)
1247 m_context
->RotateTransform( RadToDeg(angle
) );
1250 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1252 m_context
->TranslateTransform( dx
, dy
);
1255 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1257 m_context
->ScaleTransform(xScale
,yScale
);
1260 void wxGDIPlusContext::PushState()
1262 GraphicsState state
= m_context
->Save();
1263 m_stateStack
.push(state
);
1266 void wxGDIPlusContext::PopState()
1268 GraphicsState state
= m_stateStack
.top();
1270 m_context
->Restore(state
);
1273 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1275 if (m_composition
== wxCOMPOSITION_DEST
)
1278 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1281 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1283 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1284 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1285 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1286 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1289 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1293 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1295 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1296 DrawBitmap(bitmap
, x
, y
, w
, h
);
1299 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1301 if (m_composition
== wxCOMPOSITION_DEST
)
1304 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1305 // find out by looking at the bitmap data whether there really was alpha in it
1306 HICON hIcon
= (HICON
)icon
.GetHICON();
1308 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1309 if (!GetIconInfo(hIcon
,&iconInfo
))
1312 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1314 Bitmap
* image
= NULL
;
1316 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1317 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1318 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1320 image
= Bitmap::FromHICON(hIcon
);
1324 size_t width
= interim
.GetWidth();
1325 size_t height
= interim
.GetHeight();
1326 Rect
bounds(0,0,width
,height
);
1329 interim
.LockBits(&bounds
, ImageLockModeRead
,
1330 interim
.GetPixelFormat(),&data
);
1332 bool hasAlpha
= false;
1333 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1335 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1337 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1338 if ( ( *dest
& Color::AlphaMask
) != 0 )
1345 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1346 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1350 image
= Bitmap::FromHICON(hIcon
);
1353 interim
.UnlockBits(&data
);
1356 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1359 DeleteObject(iconInfo
.hbmColor
);
1360 DeleteObject(iconInfo
.hbmMask
);
1363 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1364 wxDouble x
, wxDouble y
,
1365 const wxGraphicsBrush
& brush
)
1367 if (m_composition
== wxCOMPOSITION_DEST
)
1370 wxCHECK_RET( !m_font
.IsNull(),
1371 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1376 wxGDIPlusFontData
* const
1377 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1378 wxGDIPlusBrushData
* const
1379 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1381 m_context
->DrawString
1383 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1384 -1, // length: string is NUL-terminated
1385 fontData
->GetGDIPlusFont(),
1387 StringFormat::GenericTypographic(),
1388 brushData
? brushData
->GetGDIPlusBrush()
1389 : fontData
->GetGDIPlusBrush()
1393 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1394 wxDouble
*descent
, wxDouble
*externalLeading
) const
1396 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1398 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1399 FontFamily ffamily
;
1400 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1402 f
->GetFamily(&ffamily
) ;
1404 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1406 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1407 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1408 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1409 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1410 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1411 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1414 *height
= rHeight
* factorY
;
1416 *descent
= rDescent
* factorY
;
1417 if ( externalLeading
)
1418 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1419 // measuring empty strings is not guaranteed, so do it by hand
1427 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1428 StringFormat
strFormat( StringFormat::GenericTypographic() );
1429 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1432 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1434 *width
= bounds
.Width
;
1438 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1441 widths
.Add(0, text
.length());
1443 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1448 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1449 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1450 size_t len
= wcslen( ws
) ;
1451 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1453 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1454 StringFormat
strFormat( StringFormat::GenericTypographic() );
1456 CharacterRange
* ranges
= new CharacterRange
[len
] ;
1457 Region
* regions
= new Region
[len
];
1458 for( size_t i
= 0 ; i
< len
; ++i
)
1460 ranges
[i
].First
= i
;
1461 ranges
[i
].Length
= 1 ;
1463 strFormat
.SetMeasurableCharacterRanges(len
,ranges
);
1464 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1465 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,1,regions
) ;
1468 for ( size_t i
= 0 ; i
< len
; ++i
)
1470 regions
[i
].GetBounds(&bbox
,m_context
);
1471 widths
[i
] = bbox
.GetRight()-bbox
.GetLeft();
1475 bool wxGDIPlusContext::ShouldOffset() const
1478 if ( !m_pen
.IsNull() )
1480 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1481 if ( penwidth
== 0 )
1484 return ( penwidth
% 2 ) == 1;
1487 void* wxGDIPlusContext::GetNativeContext()
1492 // concatenates this transform with the current transform of this context
1493 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1495 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1498 // sets the transform of this context
1499 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1501 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1504 // gets the matrix of this context
1505 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1507 wxGraphicsMatrix matrix
= CreateMatrix();
1508 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1512 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1517 //-----------------------------------------------------------------------------
1518 // wxGDIPlusRenderer declaration
1519 //-----------------------------------------------------------------------------
1521 class wxGDIPlusRenderer
: public wxGraphicsRenderer
1530 virtual ~wxGDIPlusRenderer()
1532 if ( m_loaded
== 1 )
1540 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1542 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1544 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1546 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1548 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1550 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1552 virtual wxGraphicsContext
* CreateMeasuringContext();
1556 virtual wxGraphicsPath
CreatePath();
1560 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1561 wxDouble tx
=0.0, wxDouble ty
=0.0);
1564 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1566 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1568 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1569 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1570 const wxColour
&c1
, const wxColour
&c2
) ;
1572 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1573 // with radius r and color cColor
1574 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1575 const wxColour
&oColor
, const wxColour
&cColor
) ;
1578 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1580 // create a native bitmap representation
1581 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1583 // create a subimage from a native image representation
1584 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1587 bool EnsureIsLoaded();
1590 friend class wxGDIPlusRendererModule
;
1594 ULONG_PTR m_gditoken
;
1596 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1599 //-----------------------------------------------------------------------------
1600 // wxGDIPlusRenderer implementation
1601 //-----------------------------------------------------------------------------
1603 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1605 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1607 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1609 return &gs_GDIPlusRenderer
;
1612 bool wxGDIPlusRenderer::EnsureIsLoaded()
1614 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1615 // if we already tried and failed (we don't want to spend lot of time
1616 // returning NULL from wxGraphicsContext::Create(), which may be called
1617 // relatively frequently):
1618 if ( m_loaded
== -1 )
1623 return m_loaded
== 1;
1626 // call EnsureIsLoaded() and return returnOnFail value if it fails
1627 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1628 if ( !EnsureIsLoaded() ) \
1629 return (returnOnFail)
1632 void wxGDIPlusRenderer::Load()
1634 GdiplusStartupInput input
;
1635 GdiplusStartupOutput output
;
1636 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1638 wxLogTrace("gdiplus", "successfully initialized GDI+");
1643 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1648 void wxGDIPlusRenderer::Unload()
1652 GdiplusShutdown(m_gditoken
);
1655 m_loaded
= -1; // next Load() will try again
1658 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1660 ENSURE_LOADED_OR_RETURN(NULL
);
1661 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1662 wxSize sz
= dc
.GetSize();
1663 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1666 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1668 ENSURE_LOADED_OR_RETURN(NULL
);
1669 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1670 wxSize sz
= dc
.GetSize();
1671 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1674 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1676 ENSURE_LOADED_OR_RETURN(NULL
);
1677 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1678 wxSize sz
= dc
.GetSize();
1679 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1682 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1684 ENSURE_LOADED_OR_RETURN(NULL
);
1685 return new wxGDIPlusMeasuringContext(this);
1688 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1690 ENSURE_LOADED_OR_RETURN(NULL
);
1691 return new wxGDIPlusContext(this,(Graphics
*) context
);
1695 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1697 ENSURE_LOADED_OR_RETURN(NULL
);
1698 return new wxGDIPlusContext(this,(HWND
) window
);
1701 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1703 ENSURE_LOADED_OR_RETURN(NULL
);
1704 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1709 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1711 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1713 m
.SetRefData( new wxGDIPlusPathData(this));
1720 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1721 wxDouble tx
, wxDouble ty
)
1724 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1726 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1727 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1732 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1734 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1735 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1736 return wxNullGraphicsPen
;
1740 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1745 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1747 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1748 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1749 return wxNullGraphicsBrush
;
1753 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1758 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1759 wxGraphicsBrush
wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1760 const wxColour
&c1
, const wxColour
&c2
)
1762 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1764 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1765 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1770 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1771 // with radius r and color cColor
1772 wxGraphicsBrush
wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1773 const wxColour
&oColor
, const wxColour
&cColor
)
1775 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1777 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1778 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1784 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1786 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
1790 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1794 return wxNullGraphicsFont
;
1797 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
1799 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1803 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
1807 return wxNullGraphicsBitmap
;
1810 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1812 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1813 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
1817 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
1821 return wxNullGraphicsBitmap
;
1824 // Shutdown GDI+ at app exit, before possible dll unload
1825 class wxGDIPlusRendererModule
: public wxModule
1828 virtual bool OnInit() { return true; }
1829 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
1832 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
1835 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
1837 #endif // wxUSE_GRAPHICS_CONTEXT