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"
21 #include "wx/msw/wrapcdlg.h"
23 #include "wx/window.h"
26 #include "wx/dialog.h"
28 #include "wx/bitmap.h"
29 #include "wx/dcmemory.h"
32 #include "wx/dcprint.h"
33 #include "wx/module.h"
36 #include "wx/graphics.h"
38 #if wxUSE_GRAPHICS_CONTEXT
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 const double RAD2DEG
= 180.0 / M_PI
;
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
55 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
57 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
58 static inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
60 //-----------------------------------------------------------------------------
61 // device context implementation
63 // more and more of the dc functionality should be implemented by calling
64 // the appropricate wxGDIPlusContext, but we will have to do that step by step
65 // also coordinate conversions should be moved to native matrix ops
66 //-----------------------------------------------------------------------------
68 // we always stock two context states, one at entry, to be able to preserve the
69 // state we were called with, the other one after changing to HI Graphics orientation
70 // (this one is used for getting back clippings etc)
72 //-----------------------------------------------------------------------------
73 // wxGraphicsPath implementation
74 //-----------------------------------------------------------------------------
76 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
78 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
82 // TODO remove this dependency (gdiplus needs the macros)
85 #define max(a,b) (((a) > (b)) ? (a) : (b))
89 #define min(a,b) (((a) < (b)) ? (a) : (b))
93 using namespace Gdiplus
;
95 class WXDLLIMPEXP_CORE wxGDIPlusPath
: public wxGraphicsPath
99 wxGDIPlusPath(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
102 virtual wxGraphicsPath
*Clone() const;
105 // These are the path primitives from which everything else can be constructed
108 // begins a new subpath at (x,y)
109 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
111 // adds a straight line from the current point to (x,y)
112 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
114 // adds a cubic Bezier curve from the current point, using two control points and an end point
115 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
118 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
119 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
121 // gets the last point of the current path, (0,0) if not yet set
122 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) ;
125 virtual void AddPath( const wxGraphicsPath
* path
);
127 // closes the current sub-path
128 virtual void CloseSubpath();
131 // These are convenience functions which - if not available natively will be assembled
132 // using the primitives from above
135 // appends a rectangle as a new closed subpath
136 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
139 // appends an ellipsis as a new closed subpath fitting the passed rectangle
140 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
142 // 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)
143 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
146 // returns the native path
147 virtual void * GetNativePath() const { return m_path
; }
149 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
150 virtual void UnGetNativePath(void * WXUNUSED(path
)) {}
152 // transforms each point of this path by the matrix
153 virtual void Transform( wxGraphicsMatrix
* matrix
) ;
155 // gets the bounding box enclosing all points (possibly including control points)
156 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) ;
158 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxWINDING_RULE
) ;
161 GraphicsPath
* m_path
;
162 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusPath
)
165 class WXDLLIMPEXP_CORE wxGDIPlusMatrix
: public wxGraphicsMatrix
170 wxGDIPlusMatrix(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
171 virtual ~wxGDIPlusMatrix() ;
173 virtual wxGraphicsMatrix
*Clone() const ;
175 // concatenates the matrix
176 virtual void Concat( const wxGraphicsMatrix
*t
);
178 // copies the passed in matrix
179 virtual void Copy( const wxGraphicsMatrix
*t
);
181 // sets the matrix to the respective values
182 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
183 wxDouble tx
=0.0, wxDouble ty
=0.0);
185 // makes this the inverse matrix
186 virtual void Invert();
188 // returns true if the elements of the transformation matrix are equal ?
189 virtual bool IsEqual( const wxGraphicsMatrix
* t
) const ;
191 // return true if this is the identity matrix
192 virtual bool IsIdentity();
198 // add the translation to this matrix
199 virtual void Translate( wxDouble dx
, wxDouble dy
);
201 // add the scale to this matrix
202 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
204 // add the rotation to this matrix (radians)
205 virtual void Rotate( wxDouble angle
);
208 // apply the transforms
211 // applies that matrix to the point
212 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
);
214 // applies the matrix except for translations
215 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
);
217 // returns the native representation
218 virtual void * GetNativeMatrix() const;
222 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMatrix
)
225 class WXDLLIMPEXP_CORE wxGDIPlusPenData
: public wxGraphicsObjectRefData
228 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
233 virtual wxDouble
GetWidth() { return m_width
; }
234 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
244 class WXDLLIMPEXP_CORE wxGDIPlusBrushData
: public wxGraphicsObjectRefData
247 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
248 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
249 ~wxGDIPlusBrushData ();
251 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
252 const wxColour
&c1
, const wxColour
&c2
);
253 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
254 const wxColour
&oColor
, const wxColour
&cColor
);
255 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
263 GraphicsPath
* m_brushPath
;
266 class WXDLLIMPEXP_CORE 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 WXDLLIMPEXP_CORE wxGDIPlusContext
: public wxGraphicsContext
282 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
);
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
, int fillStyle
= wxWINDING_RULE
);
301 virtual void Translate( wxDouble dx
, wxDouble dy
);
302 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
303 virtual void Rotate( wxDouble angle
);
305 // concatenates this transform with the current transform of this context
306 virtual void ConcatTransform( const wxGraphicsMatrix
* matrix
);
308 // sets the transform of this context
309 virtual void SetTransform( const wxGraphicsMatrix
* matrix
);
311 // gets the matrix of this context
312 virtual void GetTransform( wxGraphicsMatrix
* matrix
);
314 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
315 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
316 virtual void PushState();
317 virtual void PopState();
319 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
320 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
321 wxDouble
*descent
, wxDouble
*externalLeading
) const;
322 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
329 vector
<GraphicsState
> m_stateStack
;
330 GraphicsState m_state1
;
331 GraphicsState m_state2
;
333 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
336 //-----------------------------------------------------------------------------
337 // wxGDIPlusPen implementation
338 //-----------------------------------------------------------------------------
340 wxGDIPlusPenData::~wxGDIPlusPenData()
347 void wxGDIPlusPenData::Init()
354 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
355 : wxGraphicsObjectRefData(renderer
)
358 m_width
= pen
.GetWidth();
362 m_pen
= new Pen(Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
363 pen
.GetColour().Green() , pen
.GetColour().Blue() ), m_width
);
366 switch ( pen
.GetCap() )
372 case wxCAP_PROJECTING
:
377 cap
= LineCapFlat
; // TODO verify
384 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
387 switch ( pen
.GetJoin() )
390 join
= LineJoinBevel
;
394 join
= LineJoinMiter
;
398 join
= LineJoinRound
;
402 join
= LineJoinMiter
;
406 m_pen
->SetLineJoin(join
);
408 m_pen
->SetDashStyle(DashStyleSolid
);
410 DashStyle dashStyle
= DashStyleSolid
;
411 switch ( pen
.GetStyle() )
417 dashStyle
= DashStyleDot
;
421 dashStyle
= DashStyleDash
; // TODO verify
425 dashStyle
= DashStyleDash
;
429 dashStyle
= DashStyleDashDot
;
433 dashStyle
= DashStyleCustom
;
435 int count
= pen
.GetDashes( &dashes
);
436 if ((dashes
!= NULL
) && (count
> 0))
438 REAL
*userLengths
= new REAL
[count
];
439 for ( int i
= 0; i
< count
; ++i
)
441 userLengths
[i
] = dashes
[i
];
443 m_pen
->SetDashPattern( userLengths
, count
);
444 delete[] userLengths
;
450 wxBitmap
* bmp
= pen
.GetStipple();
451 if ( bmp
&& bmp
->Ok() )
453 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
454 m_penBrush
= new TextureBrush(m_penImage
);
455 m_pen
->SetBrush( m_penBrush
);
461 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
463 HatchStyle style
= HatchStyleHorizontal
;
464 switch( pen
.GetStyle() )
466 case wxBDIAGONAL_HATCH
:
467 style
= HatchStyleBackwardDiagonal
;
469 case wxCROSSDIAG_HATCH
:
470 style
= HatchStyleDiagonalCross
;
472 case wxFDIAGONAL_HATCH
:
473 style
= HatchStyleForwardDiagonal
;
476 style
= HatchStyleCross
;
478 case wxHORIZONTAL_HATCH
:
479 style
= HatchStyleHorizontal
;
481 case wxVERTICAL_HATCH
:
482 style
= HatchStyleVertical
;
486 m_penBrush
= new HatchBrush(style
,Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
487 pen
.GetColour().Green() , pen
.GetColour().Blue() ), Color::Transparent
);
488 m_pen
->SetBrush( m_penBrush
);
492 if ( dashStyle
!= DashStyleSolid
)
493 m_pen
->SetDashStyle(dashStyle
);
496 //-----------------------------------------------------------------------------
497 // wxGDIPlusBrush implementation
498 //-----------------------------------------------------------------------------
500 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
501 : wxGraphicsObjectRefData(renderer
)
506 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
507 : wxGraphicsObjectRefData(renderer
)
510 if ( brush
.GetStyle() == wxSOLID
)
512 m_brush
= new SolidBrush( Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
513 brush
.GetColour().Green() , brush
.GetColour().Blue() ) );
515 else if ( brush
.IsHatch() )
517 HatchStyle style
= HatchStyleHorizontal
;
518 switch( brush
.GetStyle() )
520 case wxBDIAGONAL_HATCH
:
521 style
= HatchStyleBackwardDiagonal
;
523 case wxCROSSDIAG_HATCH
:
524 style
= HatchStyleDiagonalCross
;
526 case wxFDIAGONAL_HATCH
:
527 style
= HatchStyleForwardDiagonal
;
530 style
= HatchStyleCross
;
532 case wxHORIZONTAL_HATCH
:
533 style
= HatchStyleHorizontal
;
535 case wxVERTICAL_HATCH
:
536 style
= HatchStyleVertical
;
540 m_brush
= new HatchBrush(style
,Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
541 brush
.GetColour().Green() , brush
.GetColour().Blue() ), Color::Transparent
);
545 wxBitmap
* bmp
= brush
.GetStipple();
546 if ( bmp
&& bmp
->Ok() )
548 wxDELETE( m_brushImage
);
549 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
550 m_brush
= new TextureBrush(m_brushImage
);
555 wxGDIPlusBrushData::~wxGDIPlusBrushData()
562 void wxGDIPlusBrushData::Init()
569 void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
571 m_brush
= new LinearGradientBrush( PointF( x1
,y1
) , PointF( x2
,y2
),
572 Color( c1
.Alpha(), c1
.Red(),c1
.Green() , c1
.Blue() ),
573 Color( c2
.Alpha(), c2
.Red(),c2
.Green() , c2
.Blue() ));
576 void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
577 const wxColour
&oColor
, const wxColour
&cColor
)
579 // Create a path that consists of a single circle.
580 m_brushPath
= new GraphicsPath();
581 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
), (REAL
)(2*radius
), (REAL
)(2*radius
));
583 PathGradientBrush
*b
= new PathGradientBrush(m_brushPath
);
585 b
->SetCenterPoint( PointF(xo
,yo
));
586 b
->SetCenterColor(Color( oColor
.Alpha(), oColor
.Red(),oColor
.Green() , oColor
.Blue() ));
588 Color colors
[] = {Color( cColor
.Alpha(), cColor
.Red(),cColor
.Green() , cColor
.Blue() )};
590 b
->SetSurroundColors(colors
, &count
);
593 //-----------------------------------------------------------------------------
594 // wxGDIPlusFont implementation
595 //-----------------------------------------------------------------------------
597 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
598 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
603 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
604 int size
= font
.GetPointSize();
605 int style
= FontStyleRegular
;
606 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
607 style
|= FontStyleItalic
;
608 if ( font
.GetUnderlined() )
609 style
|= FontStyleUnderline
;
610 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
611 style
|= FontStyleBold
;
612 m_font
= new Font( s
, size
, style
);
613 m_textBrush
= new SolidBrush( Color( col
.Alpha() , col
.Red() ,
614 col
.Green() , col
.Blue() ));
617 wxGDIPlusFontData::~wxGDIPlusFontData()
623 //-----------------------------------------------------------------------------
624 // wxGDIPlusPath implementation
625 //-----------------------------------------------------------------------------
627 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusPath
,wxGraphicsPath
)
629 wxGDIPlusPath::wxGDIPlusPath(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPath(renderer
)
634 m_path
= new GraphicsPath();
637 wxGDIPlusPath::wxGDIPlusPath() : wxGraphicsPath(NULL
)
639 wxLogDebug(wxT("Illegal Constructor called"));
643 wxGDIPlusPath::~wxGDIPlusPath()
648 wxGraphicsPath
* wxGDIPlusPath::Clone() const
650 return new wxGDIPlusPath( GetRenderer() , m_path
->Clone());
659 void wxGDIPlusPath::MoveToPoint( wxDouble x
, wxDouble y
)
661 m_path
->StartFigure();
662 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
665 void wxGDIPlusPath::AddLineToPoint( wxDouble x
, wxDouble y
)
667 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
670 void wxGDIPlusPath::CloseSubpath()
672 m_path
->CloseFigure();
675 void wxGDIPlusPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
681 m_path
->GetLastPoint(&start
);
682 m_path
->AddBezier(start
,c1
,c2
,end
);
685 // gets the last point of the current path, (0,0) if not yet set
686 void wxGDIPlusPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
689 m_path
->GetLastPoint(&start
);
694 void wxGDIPlusPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
696 double sweepAngle
= endAngle
- startAngle
;
697 if( abs(sweepAngle
) >= 2*M_PI
)
699 sweepAngle
= 2 * M_PI
;
706 sweepAngle
+= 2 * M_PI
;
711 sweepAngle
-= 2 * M_PI
;
715 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
718 void wxGDIPlusPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
720 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
723 void wxGDIPlusPath::AddPath( const wxGraphicsPath
* path
)
725 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
729 // transforms each point of this path by the matrix
730 void wxGDIPlusPath::Transform( wxGraphicsMatrix
* matrix
)
732 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
735 // gets the bounding box enclosing all points (possibly including control points)
736 void wxGDIPlusPath::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
)
739 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
746 bool wxGDIPlusPath::Contains( wxDouble x
, wxDouble y
, int fillStyle
)
748 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
749 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
752 //-----------------------------------------------------------------------------
753 // wxGDIPlusMatrix implementation
754 //-----------------------------------------------------------------------------
756 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMatrix
,wxGraphicsMatrix
)
758 wxGDIPlusMatrix::wxGDIPlusMatrix() : wxGraphicsMatrix(NULL
)
760 wxLogDebug(wxT("Illegal Constructor called"));
763 wxGDIPlusMatrix::wxGDIPlusMatrix(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
764 : wxGraphicsMatrix(renderer
)
769 m_matrix
= new Matrix();
772 wxGDIPlusMatrix::~wxGDIPlusMatrix()
777 wxGraphicsMatrix
*wxGDIPlusMatrix::Clone() const
779 return new wxGDIPlusMatrix( GetRenderer(), m_matrix
->Clone());
782 // concatenates the matrix
783 void wxGDIPlusMatrix::Concat( const wxGraphicsMatrix
*t
)
785 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
788 // copies the passed in matrix
789 void wxGDIPlusMatrix::Copy( const wxGraphicsMatrix
*t
)
792 m_matrix
= ((Matrix
*) t
->GetNativeMatrix())->Clone();
795 // sets the matrix to the respective values
796 void wxGDIPlusMatrix::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
797 wxDouble tx
, wxDouble ty
)
799 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
802 // makes this the inverse matrix
803 void wxGDIPlusMatrix::Invert()
808 // returns true if the elements of the transformation matrix are equal ?
809 bool wxGDIPlusMatrix::IsEqual( const wxGraphicsMatrix
* t
) const
811 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
814 // return true if this is the identity matrix
815 bool wxGDIPlusMatrix::IsIdentity()
817 return m_matrix
->IsIdentity() == TRUE
;
824 // add the translation to this matrix
825 void wxGDIPlusMatrix::Translate( wxDouble dx
, wxDouble dy
)
827 m_matrix
->Translate(dx
,dy
);
830 // add the scale to this matrix
831 void wxGDIPlusMatrix::Scale( wxDouble xScale
, wxDouble yScale
)
833 m_matrix
->Scale(xScale
,yScale
);
836 // add the rotation to this matrix (radians)
837 void wxGDIPlusMatrix::Rotate( wxDouble angle
)
839 m_matrix
->Rotate( angle
);
843 // apply the transforms
846 // applies that matrix to the point
847 void wxGDIPlusMatrix::TransformPoint( wxDouble
*x
, wxDouble
*y
)
850 m_matrix
->TransformPoints(&pt
);
855 // applies the matrix except for translations
856 void wxGDIPlusMatrix::TransformDistance( wxDouble
*dx
, wxDouble
*dy
)
859 m_matrix
->TransformVectors(&pt
);
864 // returns the native representation
865 void * wxGDIPlusMatrix::GetNativeMatrix() const
870 //-----------------------------------------------------------------------------
871 // wxGDIPlusContext implementation
872 //-----------------------------------------------------------------------------
874 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
876 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
)
877 : wxGraphicsContext(renderer
)
880 m_context
= new Graphics( hdc
);
884 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
885 : wxGraphicsContext(renderer
)
888 m_context
= new Graphics( hwnd
);
892 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
893 : wxGraphicsContext(renderer
)
900 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
905 void wxGDIPlusContext::Init()
912 void wxGDIPlusContext::SetDefaults()
914 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
915 m_state1
= m_context
->Save();
916 m_state2
= m_context
->Save();
919 wxGDIPlusContext::~wxGDIPlusContext()
923 m_context
->Restore( m_state2
);
924 m_context
->Restore( m_state1
);
930 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
932 m_context
->SetClip((HRGN
)region
.GetHRGN(),CombineModeIntersect
);
935 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
937 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
940 void wxGDIPlusContext::ResetClip()
942 m_context
->ResetClip();
945 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
*path
)
947 if ( !m_pen
.IsNull() )
949 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
->GetNativePath() );
953 void wxGDIPlusContext::FillPath( const wxGraphicsPath
*path
, int fillStyle
)
955 if ( !m_brush
.IsNull() )
957 ((GraphicsPath
*) path
->GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
958 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
959 (GraphicsPath
*) path
->GetNativePath());
963 void wxGDIPlusContext::Rotate( wxDouble angle
)
965 m_context
->RotateTransform( RadToDeg(angle
) );
968 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
970 m_context
->TranslateTransform( dx
, dy
);
973 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
975 m_context
->ScaleTransform(xScale
,yScale
);
978 void wxGDIPlusContext::PushState()
980 GraphicsState state
= m_context
->Save();
981 m_stateStack
.push_back(state
);
984 void wxGDIPlusContext::PopState()
986 GraphicsState state
= m_stateStack
.back();
987 m_stateStack
.pop_back();
988 m_context
->Restore(state
);
991 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
992 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
993 // bytes as parameter
995 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
997 Bitmap
* image
= NULL
;
998 Bitmap
* helper
= NULL
;
1001 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
1003 size_t width
= interim
.GetWidth();
1004 size_t height
= interim
.GetHeight();
1005 Rect
bounds(0,0,width
,height
);
1007 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
1009 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
1010 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
1012 BitmapData dataMask
;
1013 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
1014 interimMask
.GetPixelFormat(),&dataMask
);
1017 BitmapData imageData
;
1018 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
1020 BYTE maskPattern
= 0 ;
1024 for ( size_t y
= 0 ; y
< height
; ++y
)
1027 for( size_t x
= 0 ; x
< width
; ++x
)
1032 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
1036 maskPattern
= maskPattern
>> 1;
1038 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
1039 if ( (maskByte
& maskPattern
) == 0 )
1044 interim
.GetPixel(x
,y
,&c
) ;
1045 *dest
= (c
.GetValue() | Color::AlphaMask
);
1050 image
->UnlockBits(&imageData
);
1052 interimMask
.UnlockBits(&dataMask
);
1053 interim
.UnlockBits(&dataMask
);
1057 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
1058 if ( GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
1060 size_t width
= image
->GetWidth();
1061 size_t height
= image
->GetHeight();
1062 Rect
bounds(0,0,width
,height
);
1067 helper
->LockBits(&bounds
, ImageLockModeRead
,
1068 helper
->GetPixelFormat(),&data
);
1070 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1071 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1073 helper
->UnlockBits(&data
);
1077 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1082 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1084 HICON hIcon
= (HICON
)icon
.GetHICON();
1086 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1087 if (!GetIconInfo(hIcon
,&iconInfo
))
1090 BITMAP iconBmpData
;
1091 GetObject(iconInfo
.hbmColor
,sizeof(BITMAP
),&iconBmpData
);
1092 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1094 Bitmap
* image
= NULL
;
1096 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1098 image
= Bitmap::FromHICON(hIcon
);
1102 size_t width
= interim
.GetWidth();
1103 size_t height
= interim
.GetHeight();
1104 Rect
bounds(0,0,width
,height
);
1107 interim
.LockBits(&bounds
, ImageLockModeRead
,
1108 interim
.GetPixelFormat(),&data
);
1109 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1110 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1111 interim
.UnlockBits(&data
);
1114 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1117 DeleteObject(iconInfo
.hbmColor
);
1118 DeleteObject(iconInfo
.hbmMask
);
1121 void wxGDIPlusContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1123 if ( m_font
.IsNull() || str
.IsEmpty())
1126 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1127 m_context
->DrawString( s
, -1 , ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont() ,
1128 PointF( x
, y
) , ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusBrush() );
1129 // TODO m_backgroundMode == wxSOLID
1132 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1133 wxDouble
*descent
, wxDouble
*externalLeading
) const
1135 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1136 FontFamily ffamily
;
1137 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1139 f
->GetFamily(&ffamily
) ;
1141 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1143 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1144 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1145 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1146 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1147 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1148 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1151 *height
= rHeight
* factorY
+ 0.5 ;
1153 *descent
= rDescent
* factorY
+ 0.5 ;
1154 if ( externalLeading
)
1155 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
+ 0.5 ;
1156 // measuring empty strings is not guaranteed, so do it by hand
1164 // MeasureString does return a rectangle that is way too large, so it is
1166 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1167 StringFormat strFormat
;
1168 CharacterRange
strRange(0,wcslen(s
));
1169 strFormat
.SetMeasurableCharacterRanges(1,&strRange
);
1171 m_context
->MeasureCharacterRanges(s
, -1 , f
,layoutRect
, &strFormat
,1,®ion
) ;
1173 region
.GetBounds(&bbox
,m_context
);
1175 *width
= bbox
.GetRight()-bbox
.GetLeft()+0.5;
1179 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1182 widths
.Add(0, text
.length());
1187 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1188 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1189 size_t len
= wcslen( ws
) ;
1190 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1192 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1193 StringFormat strFormat
;
1195 CharacterRange
* ranges
= new CharacterRange
[len
] ;
1196 Region
* regions
= new Region
[len
];
1197 for( size_t i
= 0 ; i
< len
; ++i
)
1199 ranges
[i
].First
= i
;
1200 ranges
[i
].Length
= 1 ;
1202 strFormat
.SetMeasurableCharacterRanges(len
,ranges
);
1203 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,1,regions
) ;
1206 for ( size_t i
= 0 ; i
< len
; ++i
)
1208 regions
[i
].GetBounds(&bbox
,m_context
);
1209 widths
[i
] = bbox
.GetRight()-bbox
.GetLeft();
1213 void* wxGDIPlusContext::GetNativeContext()
1218 // concatenates this transform with the current transform of this context
1219 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
* matrix
)
1221 m_context
->MultiplyTransform((Matrix
*) matrix
->GetNativeMatrix());
1224 // sets the transform of this context
1225 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
* matrix
)
1227 m_context
->SetTransform((Matrix
*) matrix
->GetNativeMatrix());
1230 // gets the matrix of this context
1231 void wxGDIPlusContext::GetTransform( wxGraphicsMatrix
* matrix
)
1233 m_context
->GetTransform((Matrix
*) matrix
->GetNativeMatrix());
1235 //-----------------------------------------------------------------------------
1236 // wxGDIPlusRenderer declaration
1237 //-----------------------------------------------------------------------------
1239 class WXDLLIMPEXP_CORE wxGDIPlusRenderer
: public wxGraphicsRenderer
1248 virtual ~wxGDIPlusRenderer()
1258 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1260 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1262 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1264 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1268 virtual wxGraphicsPath
* CreatePath();
1272 virtual wxGraphicsMatrix
* CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1273 wxDouble tx
=0.0, wxDouble ty
=0.0);
1276 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1278 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1280 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1281 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1282 const wxColour
&c1
, const wxColour
&c2
) ;
1284 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1285 // with radius r and color cColor
1286 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1287 const wxColour
&oColor
, const wxColour
&cColor
) ;
1290 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1292 void EnsureIsLoaded();
1300 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1303 //-----------------------------------------------------------------------------
1304 // wxGDIPlusRenderer implementation
1305 //-----------------------------------------------------------------------------
1307 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1309 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1311 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1313 return &gs_GDIPlusRenderer
;
1316 void wxGDIPlusRenderer::EnsureIsLoaded()
1324 void wxGDIPlusRenderer::Load()
1326 GdiplusStartupInput input
;
1327 GdiplusStartupOutput output
;
1328 GdiplusStartup(&m_gditoken
,&input
,&output
);
1332 void wxGDIPlusRenderer::Unload()
1335 GdiplusShutdown(m_gditoken
);
1338 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1341 return new wxGDIPlusContext(this,(HDC
) dc
.GetHDC());
1344 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1347 return new wxGDIPlusContext(this,(Graphics
*) context
);
1351 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1354 return new wxGDIPlusContext(this,(HWND
) window
);
1357 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1360 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1365 wxGraphicsPath
* wxGDIPlusRenderer::CreatePath()
1368 return new wxGDIPlusPath( this );
1374 wxGraphicsMatrix
* wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1375 wxDouble tx
, wxDouble ty
)
1379 wxGDIPlusMatrix
* m
= new wxGDIPlusMatrix( this );
1380 m
->Set( a
,b
,c
,d
,tx
,ty
) ;
1384 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1387 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1388 return wxNullGraphicsPen
;
1392 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1397 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1400 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1401 return wxNullGraphicsBrush
;
1405 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1410 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1411 wxGraphicsBrush
wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1412 const wxColour
&c1
, const wxColour
&c2
)
1416 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1417 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1422 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1423 // with radius r and color cColor
1424 wxGraphicsBrush
wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1425 const wxColour
&oColor
, const wxColour
&cColor
)
1429 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1430 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1436 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1442 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1446 return wxNullGraphicsFont
;
1449 #endif // wxUSE_GRAPHICS_CONTEXT