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 void Translate( wxDouble dx
, wxDouble dy
);
308 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
309 virtual void Rotate( wxDouble angle
);
311 // concatenates this transform with the current transform of this context
312 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
314 // sets the transform of this context
315 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
317 // gets the matrix of this context
318 virtual wxGraphicsMatrix
GetTransform() const;
320 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
321 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
322 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
323 virtual void PushState();
324 virtual void PopState();
326 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
327 wxDouble
*descent
, wxDouble
*externalLeading
) const;
328 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
329 virtual bool ShouldOffset() const;
330 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
336 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
337 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
338 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
339 const wxGraphicsBrush
& backgroundBrush
);
342 GraphicsStates m_stateStack
;
343 GraphicsState m_state1
;
344 GraphicsState m_state2
;
349 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
352 class WXDLLIMPEXP_CORE wxGDIPlusMeasuringContext
: public wxGDIPlusContext
355 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
358 wxGDIPlusMeasuringContext()
362 virtual ~wxGDIPlusMeasuringContext()
364 ReleaseDC( NULL
, m_hdc
);
369 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
372 //-----------------------------------------------------------------------------
373 // wxGDIPlusPen implementation
374 //-----------------------------------------------------------------------------
376 wxGDIPlusPenData::~wxGDIPlusPenData()
383 void wxGDIPlusPenData::Init()
390 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
391 : wxGraphicsObjectRefData(renderer
)
394 m_width
= pen
.GetWidth();
398 m_pen
= new Pen(Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
399 pen
.GetColour().Green() , pen
.GetColour().Blue() ), m_width
);
402 switch ( pen
.GetCap() )
408 case wxCAP_PROJECTING
:
413 cap
= LineCapFlat
; // TODO verify
420 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
423 switch ( pen
.GetJoin() )
426 join
= LineJoinBevel
;
430 join
= LineJoinMiter
;
434 join
= LineJoinRound
;
438 join
= LineJoinMiter
;
442 m_pen
->SetLineJoin(join
);
444 m_pen
->SetDashStyle(DashStyleSolid
);
446 DashStyle dashStyle
= DashStyleSolid
;
447 switch ( pen
.GetStyle() )
453 dashStyle
= DashStyleDot
;
457 dashStyle
= DashStyleDash
; // TODO verify
461 dashStyle
= DashStyleDash
;
465 dashStyle
= DashStyleDashDot
;
469 dashStyle
= DashStyleCustom
;
471 int count
= pen
.GetDashes( &dashes
);
472 if ((dashes
!= NULL
) && (count
> 0))
474 REAL
*userLengths
= new REAL
[count
];
475 for ( int i
= 0; i
< count
; ++i
)
477 userLengths
[i
] = dashes
[i
];
479 m_pen
->SetDashPattern( userLengths
, count
);
480 delete[] userLengths
;
486 wxBitmap
* bmp
= pen
.GetStipple();
487 if ( bmp
&& bmp
->Ok() )
489 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
490 m_penBrush
= new TextureBrush(m_penImage
);
491 m_pen
->SetBrush( m_penBrush
);
497 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
499 HatchStyle style
= HatchStyleHorizontal
;
500 switch( pen
.GetStyle() )
502 case wxBDIAGONAL_HATCH
:
503 style
= HatchStyleBackwardDiagonal
;
505 case wxCROSSDIAG_HATCH
:
506 style
= HatchStyleDiagonalCross
;
508 case wxFDIAGONAL_HATCH
:
509 style
= HatchStyleForwardDiagonal
;
512 style
= HatchStyleCross
;
514 case wxHORIZONTAL_HATCH
:
515 style
= HatchStyleHorizontal
;
517 case wxVERTICAL_HATCH
:
518 style
= HatchStyleVertical
;
522 m_penBrush
= new HatchBrush(style
,Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
523 pen
.GetColour().Green() , pen
.GetColour().Blue() ), Color::Transparent
);
524 m_pen
->SetBrush( m_penBrush
);
528 if ( dashStyle
!= DashStyleSolid
)
529 m_pen
->SetDashStyle(dashStyle
);
532 //-----------------------------------------------------------------------------
533 // wxGDIPlusBrush implementation
534 //-----------------------------------------------------------------------------
536 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
537 : wxGraphicsObjectRefData(renderer
)
542 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
543 : wxGraphicsObjectRefData(renderer
)
546 if ( brush
.GetStyle() == wxSOLID
)
548 m_brush
= new SolidBrush( Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
549 brush
.GetColour().Green() , brush
.GetColour().Blue() ) );
551 else if ( brush
.IsHatch() )
553 HatchStyle style
= HatchStyleHorizontal
;
554 switch( brush
.GetStyle() )
556 case wxBDIAGONAL_HATCH
:
557 style
= HatchStyleBackwardDiagonal
;
559 case wxCROSSDIAG_HATCH
:
560 style
= HatchStyleDiagonalCross
;
562 case wxFDIAGONAL_HATCH
:
563 style
= HatchStyleForwardDiagonal
;
566 style
= HatchStyleCross
;
568 case wxHORIZONTAL_HATCH
:
569 style
= HatchStyleHorizontal
;
571 case wxVERTICAL_HATCH
:
572 style
= HatchStyleVertical
;
576 m_brush
= new HatchBrush(style
,Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
577 brush
.GetColour().Green() , brush
.GetColour().Blue() ), Color::Transparent
);
581 wxBitmap
* bmp
= brush
.GetStipple();
582 if ( bmp
&& bmp
->Ok() )
584 wxDELETE( m_brushImage
);
585 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
586 m_brush
= new TextureBrush(m_brushImage
);
591 wxGDIPlusBrushData::~wxGDIPlusBrushData()
598 void wxGDIPlusBrushData::Init()
605 void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
607 m_brush
= new LinearGradientBrush( PointF( x1
,y1
) , PointF( x2
,y2
),
608 Color( c1
.Alpha(), c1
.Red(),c1
.Green() , c1
.Blue() ),
609 Color( c2
.Alpha(), c2
.Red(),c2
.Green() , c2
.Blue() ));
612 void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
613 const wxColour
&oColor
, const wxColour
&cColor
)
615 // Create a path that consists of a single circle.
616 m_brushPath
= new GraphicsPath();
617 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
), (REAL
)(2*radius
), (REAL
)(2*radius
));
619 PathGradientBrush
*b
= new PathGradientBrush(m_brushPath
);
621 b
->SetCenterPoint( PointF(xo
,yo
));
622 b
->SetCenterColor(Color( oColor
.Alpha(), oColor
.Red(),oColor
.Green() , oColor
.Blue() ));
624 Color colors
[] = {Color( cColor
.Alpha(), cColor
.Red(),cColor
.Green() , cColor
.Blue() )};
626 b
->SetSurroundColors(colors
, &count
);
629 //-----------------------------------------------------------------------------
630 // wxGDIPlusFont implementation
631 //-----------------------------------------------------------------------------
633 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
634 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
639 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
640 int size
= font
.GetPointSize();
641 int style
= FontStyleRegular
;
642 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
643 style
|= FontStyleItalic
;
644 if ( font
.GetUnderlined() )
645 style
|= FontStyleUnderline
;
646 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
647 style
|= FontStyleBold
;
648 m_font
= new Font( s
, size
, style
);
649 m_textBrush
= new SolidBrush( Color( col
.Alpha() , col
.Red() ,
650 col
.Green() , col
.Blue() ));
653 wxGDIPlusFontData::~wxGDIPlusFontData()
659 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
660 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
661 // bytes as parameter, since there is no real copying of the data going in, only references are stored
662 // m_helper has to be kept alive as well
664 //-----------------------------------------------------------------------------
665 // wxGDIPlusBitmapData implementation
666 //-----------------------------------------------------------------------------
668 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
669 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
674 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
675 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
680 Bitmap
* image
= NULL
;
683 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
685 size_t width
= interim
.GetWidth();
686 size_t height
= interim
.GetHeight();
687 Rect
bounds(0,0,width
,height
);
689 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
691 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
692 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
694 BitmapData dataMask
;
695 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
696 interimMask
.GetPixelFormat(),&dataMask
);
699 BitmapData imageData
;
700 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
702 BYTE maskPattern
= 0 ;
706 for ( size_t y
= 0 ; y
< height
; ++y
)
709 for( size_t x
= 0 ; x
< width
; ++x
)
714 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
718 maskPattern
= maskPattern
>> 1;
720 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
721 if ( (maskByte
& maskPattern
) == 0 )
726 interim
.GetPixel(x
,y
,&c
) ;
727 *dest
= (c
.GetValue() | Color::AlphaMask
);
732 image
->UnlockBits(&imageData
);
734 interimMask
.UnlockBits(&dataMask
);
735 interim
.UnlockBits(&dataMask
);
739 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
740 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
742 size_t width
= image
->GetWidth();
743 size_t height
= image
->GetHeight();
744 Rect
bounds(0,0,width
,height
);
745 static BitmapData data
;
749 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
750 m_helper
->GetPixelFormat(),&data
);
752 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
753 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
755 m_helper
->UnlockBits(&data
);
762 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
768 //-----------------------------------------------------------------------------
769 // wxGDIPlusPath implementation
770 //-----------------------------------------------------------------------------
772 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
777 m_path
= new GraphicsPath();
780 wxGDIPlusPathData::~wxGDIPlusPathData()
785 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
787 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
794 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
796 m_path
->StartFigure();
797 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
800 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
802 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
805 void wxGDIPlusPathData::CloseSubpath()
807 m_path
->CloseFigure();
810 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
816 m_path
->GetLastPoint(&start
);
817 m_path
->AddBezier(start
,c1
,c2
,end
);
820 // gets the last point of the current path, (0,0) if not yet set
821 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
824 m_path
->GetLastPoint(&start
);
829 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
831 double sweepAngle
= endAngle
- startAngle
;
832 if( fabs(sweepAngle
) >= 2*M_PI
)
834 sweepAngle
= 2 * M_PI
;
841 sweepAngle
+= 2 * M_PI
;
846 sweepAngle
-= 2 * M_PI
;
850 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
853 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
855 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
858 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
860 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
864 // transforms each point of this path by the matrix
865 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
867 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
870 // gets the bounding box enclosing all points (possibly including control points)
871 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
874 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
881 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
883 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
884 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
887 //-----------------------------------------------------------------------------
888 // wxGDIPlusMatrixData implementation
889 //-----------------------------------------------------------------------------
891 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
892 : wxGraphicsMatrixData(renderer
)
897 m_matrix
= new Matrix();
900 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
905 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
907 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
910 // concatenates the matrix
911 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
913 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
916 // sets the matrix to the respective values
917 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
918 wxDouble tx
, wxDouble ty
)
920 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
923 // gets the component valuess of the matrix
924 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
925 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
928 m_matrix
->GetElements(elements
);
929 if (a
) *a
= elements
[0];
930 if (b
) *b
= elements
[1];
931 if (c
) *c
= elements
[2];
932 if (d
) *d
= elements
[3];
933 if (tx
) *tx
= elements
[4];
934 if (ty
) *ty
= elements
[5];
937 // makes this the inverse matrix
938 void wxGDIPlusMatrixData::Invert()
943 // returns true if the elements of the transformation matrix are equal ?
944 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
946 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
949 // return true if this is the identity matrix
950 bool wxGDIPlusMatrixData::IsIdentity() const
952 return m_matrix
->IsIdentity() == TRUE
;
959 // add the translation to this matrix
960 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
962 m_matrix
->Translate(dx
,dy
);
965 // add the scale to this matrix
966 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
968 m_matrix
->Scale(xScale
,yScale
);
971 // add the rotation to this matrix (radians)
972 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
974 m_matrix
->Rotate( angle
);
978 // apply the transforms
981 // applies that matrix to the point
982 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
985 m_matrix
->TransformPoints(&pt
);
990 // applies the matrix except for translations
991 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
994 m_matrix
->TransformVectors(&pt
);
999 // returns the native representation
1000 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1005 //-----------------------------------------------------------------------------
1006 // wxGDIPlusContext implementation
1007 //-----------------------------------------------------------------------------
1009 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1010 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1012 class wxGDIPlusOffsetHelper
1015 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1020 m_gr
->TranslateTransform( 0.5, 0.5 );
1022 ~wxGDIPlusOffsetHelper( )
1025 m_gr
->TranslateTransform( -0.5, -0.5 );
1032 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1033 : wxGraphicsContext(renderer
)
1036 m_context
= new Graphics( hdc
);
1042 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1043 : wxGraphicsContext(renderer
)
1046 m_context
= new Graphics( hwnd
);
1047 RECT rect
= wxGetWindowRect(hwnd
);
1048 m_width
= rect
.right
- rect
.left
;
1049 m_height
= rect
.bottom
- rect
.top
;
1053 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1054 : wxGraphicsContext(renderer
)
1061 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1066 void wxGDIPlusContext::Init()
1075 void wxGDIPlusContext::SetDefaults()
1077 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1078 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1079 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1080 m_state1
= m_context
->Save();
1081 m_state2
= m_context
->Save();
1084 wxGDIPlusContext::~wxGDIPlusContext()
1088 m_context
->Restore( m_state2
);
1089 m_context
->Restore( m_state1
);
1095 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1097 Region
rgn((HRGN
)region
.GetHRGN());
1098 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1101 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1103 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1106 void wxGDIPlusContext::ResetClip()
1108 m_context
->ResetClip();
1111 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1113 if ( !m_pen
.IsNull() )
1115 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1116 Point
*cpoints
= new Point
[n
];
1117 for (size_t i
= 0; i
< n
; i
++)
1119 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1120 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1122 } // for (size_t i = 0; i < n; i++)
1123 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1128 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1130 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1131 Point
*cpoints
= new Point
[n
];
1132 for (size_t i
= 0; i
< n
; i
++)
1134 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1135 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1137 } // for (int i = 0; i < n; i++)
1138 if ( !m_brush
.IsNull() )
1139 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1140 if ( !m_pen
.IsNull() )
1141 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1145 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1147 if ( !m_pen
.IsNull() )
1149 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1150 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1154 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1156 if ( !m_brush
.IsNull() )
1158 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1159 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1160 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1161 (GraphicsPath
*) path
.GetNativePath());
1165 void wxGDIPlusContext::Rotate( wxDouble angle
)
1167 m_context
->RotateTransform( RadToDeg(angle
) );
1170 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1172 m_context
->TranslateTransform( dx
, dy
);
1175 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1177 m_context
->ScaleTransform(xScale
,yScale
);
1180 void wxGDIPlusContext::PushState()
1182 GraphicsState state
= m_context
->Save();
1183 m_stateStack
.push(state
);
1186 void wxGDIPlusContext::PopState()
1188 GraphicsState state
= m_stateStack
.top();
1190 m_context
->Restore(state
);
1193 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1195 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1198 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1200 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1201 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1202 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1203 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1206 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1210 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1212 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1213 DrawBitmap(bitmap
, x
, y
, w
, h
);
1216 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1218 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1219 // find out by looking at the bitmap data whether there really was alpha in it
1220 HICON hIcon
= (HICON
)icon
.GetHICON();
1222 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1223 if (!GetIconInfo(hIcon
,&iconInfo
))
1226 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1228 Bitmap
* image
= NULL
;
1230 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1231 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1232 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1234 image
= Bitmap::FromHICON(hIcon
);
1238 size_t width
= interim
.GetWidth();
1239 size_t height
= interim
.GetHeight();
1240 Rect
bounds(0,0,width
,height
);
1243 interim
.LockBits(&bounds
, ImageLockModeRead
,
1244 interim
.GetPixelFormat(),&data
);
1246 bool hasAlpha
= false;
1247 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1249 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1251 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1252 if ( ( *dest
& Color::AlphaMask
) != 0 )
1259 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1260 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1264 image
= Bitmap::FromHICON(hIcon
);
1267 interim
.UnlockBits(&data
);
1270 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1273 DeleteObject(iconInfo
.hbmColor
);
1274 DeleteObject(iconInfo
.hbmMask
);
1277 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1278 wxDouble x
, wxDouble y
,
1279 const wxGraphicsBrush
& brush
)
1281 wxCHECK_RET( !m_font
.IsNull(),
1282 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1287 wxGDIPlusFontData
* const
1288 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1289 wxGDIPlusBrushData
* const
1290 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1292 m_context
->DrawString
1294 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1295 -1, // length: string is NUL-terminated
1296 fontData
->GetGDIPlusFont(),
1298 StringFormat::GenericTypographic(),
1299 brushData
? brushData
->GetGDIPlusBrush()
1300 : fontData
->GetGDIPlusBrush()
1304 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1305 wxDouble
*descent
, wxDouble
*externalLeading
) const
1307 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1309 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1310 FontFamily ffamily
;
1311 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1313 f
->GetFamily(&ffamily
) ;
1315 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1317 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1318 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1319 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1320 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1321 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1322 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1325 *height
= rHeight
* factorY
;
1327 *descent
= rDescent
* factorY
;
1328 if ( externalLeading
)
1329 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1330 // measuring empty strings is not guaranteed, so do it by hand
1338 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1339 StringFormat
strFormat( StringFormat::GenericTypographic() );
1340 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1343 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1345 *width
= bounds
.Width
;
1349 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1352 widths
.Add(0, text
.length());
1354 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1359 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1360 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1361 size_t len
= wcslen( ws
) ;
1362 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1364 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1365 StringFormat
strFormat( StringFormat::GenericTypographic() );
1367 CharacterRange
* ranges
= new CharacterRange
[len
] ;
1368 Region
* regions
= new Region
[len
];
1369 for( size_t i
= 0 ; i
< len
; ++i
)
1371 ranges
[i
].First
= i
;
1372 ranges
[i
].Length
= 1 ;
1374 strFormat
.SetMeasurableCharacterRanges(len
,ranges
);
1375 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1376 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,1,regions
) ;
1379 for ( size_t i
= 0 ; i
< len
; ++i
)
1381 regions
[i
].GetBounds(&bbox
,m_context
);
1382 widths
[i
] = bbox
.GetRight()-bbox
.GetLeft();
1386 bool wxGDIPlusContext::ShouldOffset() const
1389 if ( !m_pen
.IsNull() )
1391 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1392 if ( penwidth
== 0 )
1395 return ( penwidth
% 2 ) == 1;
1398 void* wxGDIPlusContext::GetNativeContext()
1403 // concatenates this transform with the current transform of this context
1404 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1406 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1409 // sets the transform of this context
1410 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1412 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1415 // gets the matrix of this context
1416 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1418 wxGraphicsMatrix matrix
= CreateMatrix();
1419 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1423 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1428 //-----------------------------------------------------------------------------
1429 // wxGDIPlusRenderer declaration
1430 //-----------------------------------------------------------------------------
1432 class wxGDIPlusRenderer
: public wxGraphicsRenderer
1441 virtual ~wxGDIPlusRenderer()
1443 if ( m_loaded
== 1 )
1451 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1453 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1455 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
1457 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1459 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1461 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1463 virtual wxGraphicsContext
* CreateMeasuringContext();
1467 virtual wxGraphicsPath
CreatePath();
1471 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1472 wxDouble tx
=0.0, wxDouble ty
=0.0);
1475 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1477 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1479 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1480 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1481 const wxColour
&c1
, const wxColour
&c2
) ;
1483 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1484 // with radius r and color cColor
1485 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1486 const wxColour
&oColor
, const wxColour
&cColor
) ;
1489 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1491 // create a native bitmap representation
1492 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
1494 // create a subimage from a native image representation
1495 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
1498 bool EnsureIsLoaded();
1501 friend class wxGDIPlusRendererModule
;
1505 ULONG_PTR m_gditoken
;
1507 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1510 //-----------------------------------------------------------------------------
1511 // wxGDIPlusRenderer implementation
1512 //-----------------------------------------------------------------------------
1514 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1516 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1518 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1520 return &gs_GDIPlusRenderer
;
1523 bool wxGDIPlusRenderer::EnsureIsLoaded()
1525 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1526 // if we already tried and failed (we don't want to spend lot of time
1527 // returning NULL from wxGraphicsContext::Create(), which may be called
1528 // relatively frequently):
1529 if ( m_loaded
== -1 )
1534 return m_loaded
== 1;
1537 // call EnsureIsLoaded() and return returnOnFail value if it fails
1538 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1539 if ( !EnsureIsLoaded() ) \
1540 return (returnOnFail)
1543 void wxGDIPlusRenderer::Load()
1545 GdiplusStartupInput input
;
1546 GdiplusStartupOutput output
;
1547 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1549 wxLogTrace("gdiplus", "successfully initialized GDI+");
1554 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1559 void wxGDIPlusRenderer::Unload()
1563 GdiplusShutdown(m_gditoken
);
1566 m_loaded
= -1; // next Load() will try again
1569 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1571 ENSURE_LOADED_OR_RETURN(NULL
);
1572 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1573 wxSize sz
= dc
.GetSize();
1574 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1577 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1579 ENSURE_LOADED_OR_RETURN(NULL
);
1580 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1581 wxSize sz
= dc
.GetSize();
1582 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1585 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1587 ENSURE_LOADED_OR_RETURN(NULL
);
1588 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1589 wxSize sz
= dc
.GetSize();
1590 return new wxGDIPlusContext(this,(HDC
) msw
->GetHDC(), sz
.x
, sz
.y
);
1593 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1595 ENSURE_LOADED_OR_RETURN(NULL
);
1596 return new wxGDIPlusMeasuringContext(this);
1599 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1601 ENSURE_LOADED_OR_RETURN(NULL
);
1602 return new wxGDIPlusContext(this,(Graphics
*) context
);
1606 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1608 ENSURE_LOADED_OR_RETURN(NULL
);
1609 return new wxGDIPlusContext(this,(HWND
) window
);
1612 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1614 ENSURE_LOADED_OR_RETURN(NULL
);
1615 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1620 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1622 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1624 m
.SetRefData( new wxGDIPlusPathData(this));
1631 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1632 wxDouble tx
, wxDouble ty
)
1635 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1637 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1638 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1643 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1645 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1646 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1647 return wxNullGraphicsPen
;
1651 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1656 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1658 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1659 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1660 return wxNullGraphicsBrush
;
1664 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1669 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1670 wxGraphicsBrush
wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1671 const wxColour
&c1
, const wxColour
&c2
)
1673 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1675 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1676 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1681 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1682 // with radius r and color cColor
1683 wxGraphicsBrush
wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1684 const wxColour
&oColor
, const wxColour
&cColor
)
1686 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1688 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1689 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1695 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1697 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
1701 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1705 return wxNullGraphicsFont
;
1708 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
1710 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1714 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
1718 return wxNullGraphicsBitmap
;
1721 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1723 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
1724 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
1728 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
1732 return wxNullGraphicsBitmap
;
1735 // Shutdown GDI+ at app exit, before possible dll unload
1736 class wxGDIPlusRendererModule
: public wxModule
1739 virtual bool OnInit() { return true; }
1740 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
1743 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
1746 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
1748 #endif // wxUSE_GRAPHICS_CONTEXT