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 wxGDIPlusPathData
: public wxGraphicsPathData
98 wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
101 virtual wxGraphicsObjectRefData
*Clone() const;
104 // These are the path primitives from which everything else can be constructed
107 // begins a new subpath at (x,y)
108 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
110 // adds a straight line from the current point to (x,y)
111 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
113 // adds a cubic Bezier curve from the current point, using two control points and an end point
114 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
117 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
118 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
120 // gets the last point of the current path, (0,0) if not yet set
121 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
124 virtual void AddPath( const wxGraphicsPathData
* path
);
126 // closes the current sub-path
127 virtual void CloseSubpath();
130 // These are convenience functions which - if not available natively will be assembled
131 // using the primitives from above
134 // appends a rectangle as a new closed subpath
135 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
138 // appends an ellipsis as a new closed subpath fitting the passed rectangle
139 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
141 // 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)
142 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
145 // returns the native path
146 virtual void * GetNativePath() const { return m_path
; }
148 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
149 virtual void UnGetNativePath(void * WXUNUSED(path
)) const {}
151 // transforms each point of this path by the matrix
152 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
154 // gets the bounding box enclosing all points (possibly including control points)
155 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
157 virtual bool Contains( wxDouble x
, wxDouble y
, int fillStyle
= wxODDEVEN_RULE
) const;
160 GraphicsPath
* m_path
;
163 class WXDLLIMPEXP_CORE wxGDIPlusMatrixData
: public wxGraphicsMatrixData
166 wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
167 virtual ~wxGDIPlusMatrixData() ;
169 virtual wxGraphicsObjectRefData
* Clone() const ;
171 // concatenates the matrix
172 virtual void Concat( const wxGraphicsMatrixData
*t
);
174 // sets the matrix to the respective values
175 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
176 wxDouble tx
=0.0, wxDouble ty
=0.0);
178 // gets the component valuess of the matrix
179 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
180 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
182 // makes this the inverse matrix
183 virtual void Invert();
185 // returns true if the elements of the transformation matrix are equal ?
186 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
188 // return true if this is the identity matrix
189 virtual bool IsIdentity() const;
195 // add the translation to this matrix
196 virtual void Translate( wxDouble dx
, wxDouble dy
);
198 // add the scale to this matrix
199 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
201 // add the rotation to this matrix (radians)
202 virtual void Rotate( wxDouble angle
);
205 // apply the transforms
208 // applies that matrix to the point
209 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
211 // applies the matrix except for translations
212 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
214 // returns the native representation
215 virtual void * GetNativeMatrix() const;
220 class WXDLLIMPEXP_CORE wxGDIPlusPenData
: public wxGraphicsObjectRefData
223 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
228 virtual wxDouble
GetWidth() { return m_width
; }
229 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
239 class WXDLLIMPEXP_CORE wxGDIPlusBrushData
: public wxGraphicsObjectRefData
242 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
243 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
244 ~wxGDIPlusBrushData ();
246 void CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
247 const wxColour
&c1
, const wxColour
&c2
);
248 void CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
249 const wxColour
&oColor
, const wxColour
&cColor
);
250 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
258 GraphicsPath
* m_brushPath
;
261 class WXDLLIMPEXP_CORE wxGDIPlusFontData
: public wxGraphicsObjectRefData
264 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
, const wxColour
& col
);
265 ~wxGDIPlusFontData();
267 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
268 virtual Font
* GetGDIPlusFont() { return m_font
; }
274 class WXDLLIMPEXP_CORE wxGDIPlusContext
: public wxGraphicsContext
277 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
);
278 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
279 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
282 virtual ~wxGDIPlusContext();
284 virtual void Clip( const wxRegion
®ion
);
285 // clips drawings to the rect
286 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
288 // resets the clipping to original extent
289 virtual void ResetClip();
291 virtual void * GetNativeContext();
293 virtual void StrokePath( const wxGraphicsPath
& p
);
294 virtual void FillPath( const wxGraphicsPath
& p
, int fillStyle
= wxODDEVEN_RULE
);
296 virtual void Translate( wxDouble dx
, wxDouble dy
);
297 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
298 virtual void Rotate( wxDouble angle
);
300 // concatenates this transform with the current transform of this context
301 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
303 // sets the transform of this context
304 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
306 // gets the matrix of this context
307 virtual wxGraphicsMatrix
GetTransform() const;
309 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
310 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
311 virtual void PushState();
312 virtual void PopState();
314 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
315 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
316 wxDouble
*descent
, wxDouble
*externalLeading
) const;
317 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
324 vector
<GraphicsState
> m_stateStack
;
325 GraphicsState m_state1
;
326 GraphicsState m_state2
;
328 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
331 //-----------------------------------------------------------------------------
332 // wxGDIPlusPen implementation
333 //-----------------------------------------------------------------------------
335 wxGDIPlusPenData::~wxGDIPlusPenData()
342 void wxGDIPlusPenData::Init()
349 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
350 : wxGraphicsObjectRefData(renderer
)
353 m_width
= pen
.GetWidth();
357 m_pen
= new Pen(Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
358 pen
.GetColour().Green() , pen
.GetColour().Blue() ), m_width
);
361 switch ( pen
.GetCap() )
367 case wxCAP_PROJECTING
:
372 cap
= LineCapFlat
; // TODO verify
379 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
382 switch ( pen
.GetJoin() )
385 join
= LineJoinBevel
;
389 join
= LineJoinMiter
;
393 join
= LineJoinRound
;
397 join
= LineJoinMiter
;
401 m_pen
->SetLineJoin(join
);
403 m_pen
->SetDashStyle(DashStyleSolid
);
405 DashStyle dashStyle
= DashStyleSolid
;
406 switch ( pen
.GetStyle() )
412 dashStyle
= DashStyleDot
;
416 dashStyle
= DashStyleDash
; // TODO verify
420 dashStyle
= DashStyleDash
;
424 dashStyle
= DashStyleDashDot
;
428 dashStyle
= DashStyleCustom
;
430 int count
= pen
.GetDashes( &dashes
);
431 if ((dashes
!= NULL
) && (count
> 0))
433 REAL
*userLengths
= new REAL
[count
];
434 for ( int i
= 0; i
< count
; ++i
)
436 userLengths
[i
] = dashes
[i
];
438 m_pen
->SetDashPattern( userLengths
, count
);
439 delete[] userLengths
;
445 wxBitmap
* bmp
= pen
.GetStipple();
446 if ( bmp
&& bmp
->Ok() )
448 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
449 m_penBrush
= new TextureBrush(m_penImage
);
450 m_pen
->SetBrush( m_penBrush
);
456 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
458 HatchStyle style
= HatchStyleHorizontal
;
459 switch( pen
.GetStyle() )
461 case wxBDIAGONAL_HATCH
:
462 style
= HatchStyleBackwardDiagonal
;
464 case wxCROSSDIAG_HATCH
:
465 style
= HatchStyleDiagonalCross
;
467 case wxFDIAGONAL_HATCH
:
468 style
= HatchStyleForwardDiagonal
;
471 style
= HatchStyleCross
;
473 case wxHORIZONTAL_HATCH
:
474 style
= HatchStyleHorizontal
;
476 case wxVERTICAL_HATCH
:
477 style
= HatchStyleVertical
;
481 m_penBrush
= new HatchBrush(style
,Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
482 pen
.GetColour().Green() , pen
.GetColour().Blue() ), Color::Transparent
);
483 m_pen
->SetBrush( m_penBrush
);
487 if ( dashStyle
!= DashStyleSolid
)
488 m_pen
->SetDashStyle(dashStyle
);
491 //-----------------------------------------------------------------------------
492 // wxGDIPlusBrush implementation
493 //-----------------------------------------------------------------------------
495 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
496 : wxGraphicsObjectRefData(renderer
)
501 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
502 : wxGraphicsObjectRefData(renderer
)
505 if ( brush
.GetStyle() == wxSOLID
)
507 m_brush
= new SolidBrush( Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
508 brush
.GetColour().Green() , brush
.GetColour().Blue() ) );
510 else if ( brush
.IsHatch() )
512 HatchStyle style
= HatchStyleHorizontal
;
513 switch( brush
.GetStyle() )
515 case wxBDIAGONAL_HATCH
:
516 style
= HatchStyleBackwardDiagonal
;
518 case wxCROSSDIAG_HATCH
:
519 style
= HatchStyleDiagonalCross
;
521 case wxFDIAGONAL_HATCH
:
522 style
= HatchStyleForwardDiagonal
;
525 style
= HatchStyleCross
;
527 case wxHORIZONTAL_HATCH
:
528 style
= HatchStyleHorizontal
;
530 case wxVERTICAL_HATCH
:
531 style
= HatchStyleVertical
;
535 m_brush
= new HatchBrush(style
,Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
536 brush
.GetColour().Green() , brush
.GetColour().Blue() ), Color::Transparent
);
540 wxBitmap
* bmp
= brush
.GetStipple();
541 if ( bmp
&& bmp
->Ok() )
543 wxDELETE( m_brushImage
);
544 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
545 m_brush
= new TextureBrush(m_brushImage
);
550 wxGDIPlusBrushData::~wxGDIPlusBrushData()
557 void wxGDIPlusBrushData::Init()
564 void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
566 m_brush
= new LinearGradientBrush( PointF( x1
,y1
) , PointF( x2
,y2
),
567 Color( c1
.Alpha(), c1
.Red(),c1
.Green() , c1
.Blue() ),
568 Color( c2
.Alpha(), c2
.Red(),c2
.Green() , c2
.Blue() ));
571 void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
572 const wxColour
&oColor
, const wxColour
&cColor
)
574 // Create a path that consists of a single circle.
575 m_brushPath
= new GraphicsPath();
576 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
), (REAL
)(2*radius
), (REAL
)(2*radius
));
578 PathGradientBrush
*b
= new PathGradientBrush(m_brushPath
);
580 b
->SetCenterPoint( PointF(xo
,yo
));
581 b
->SetCenterColor(Color( oColor
.Alpha(), oColor
.Red(),oColor
.Green() , oColor
.Blue() ));
583 Color colors
[] = {Color( cColor
.Alpha(), cColor
.Red(),cColor
.Green() , cColor
.Blue() )};
585 b
->SetSurroundColors(colors
, &count
);
588 //-----------------------------------------------------------------------------
589 // wxGDIPlusFont implementation
590 //-----------------------------------------------------------------------------
592 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
, const wxFont
&font
,
593 const wxColour
& col
) : wxGraphicsObjectRefData( renderer
)
598 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
599 int size
= font
.GetPointSize();
600 int style
= FontStyleRegular
;
601 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
602 style
|= FontStyleItalic
;
603 if ( font
.GetUnderlined() )
604 style
|= FontStyleUnderline
;
605 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
606 style
|= FontStyleBold
;
607 m_font
= new Font( s
, size
, style
);
608 m_textBrush
= new SolidBrush( Color( col
.Alpha() , col
.Red() ,
609 col
.Green() , col
.Blue() ));
612 wxGDIPlusFontData::~wxGDIPlusFontData()
618 //-----------------------------------------------------------------------------
619 // wxGDIPlusPath implementation
620 //-----------------------------------------------------------------------------
622 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
627 m_path
= new GraphicsPath();
630 wxGDIPlusPathData::~wxGDIPlusPathData()
635 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
637 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
644 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
646 m_path
->StartFigure();
647 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
650 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
652 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
655 void wxGDIPlusPathData::CloseSubpath()
657 m_path
->CloseFigure();
660 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
666 m_path
->GetLastPoint(&start
);
667 m_path
->AddBezier(start
,c1
,c2
,end
);
670 // gets the last point of the current path, (0,0) if not yet set
671 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
674 m_path
->GetLastPoint(&start
);
679 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
681 double sweepAngle
= endAngle
- startAngle
;
682 if( abs(sweepAngle
) >= 2*M_PI
)
684 sweepAngle
= 2 * M_PI
;
691 sweepAngle
+= 2 * M_PI
;
696 sweepAngle
-= 2 * M_PI
;
700 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
703 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
705 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
708 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
710 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
714 // transforms each point of this path by the matrix
715 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
717 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
720 // gets the bounding box enclosing all points (possibly including control points)
721 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
724 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
731 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, int fillStyle
) const
733 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
734 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
737 //-----------------------------------------------------------------------------
738 // wxGDIPlusMatrixData implementation
739 //-----------------------------------------------------------------------------
741 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
742 : wxGraphicsMatrixData(renderer
)
747 m_matrix
= new Matrix();
750 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
755 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
757 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
760 // concatenates the matrix
761 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
763 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
766 // sets the matrix to the respective values
767 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
768 wxDouble tx
, wxDouble ty
)
770 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
773 // gets the component valuess of the matrix
774 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
775 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
778 m_matrix
->GetElements(elements
);
779 if (a
) *a
= elements
[0];
780 if (b
) *b
= elements
[1];
781 if (c
) *c
= elements
[2];
782 if (d
) *d
= elements
[3];
783 if (tx
) *tx
= elements
[4];
784 if (ty
) *ty
= elements
[5];
787 // makes this the inverse matrix
788 void wxGDIPlusMatrixData::Invert()
793 // returns true if the elements of the transformation matrix are equal ?
794 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
796 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
799 // return true if this is the identity matrix
800 bool wxGDIPlusMatrixData::IsIdentity() const
802 return m_matrix
->IsIdentity() == TRUE
;
809 // add the translation to this matrix
810 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
812 m_matrix
->Translate(dx
,dy
);
815 // add the scale to this matrix
816 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
818 m_matrix
->Scale(xScale
,yScale
);
821 // add the rotation to this matrix (radians)
822 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
824 m_matrix
->Rotate( angle
);
828 // apply the transforms
831 // applies that matrix to the point
832 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
835 m_matrix
->TransformPoints(&pt
);
840 // applies the matrix except for translations
841 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
844 m_matrix
->TransformVectors(&pt
);
849 // returns the native representation
850 void * wxGDIPlusMatrixData::GetNativeMatrix() const
855 //-----------------------------------------------------------------------------
856 // wxGDIPlusContext implementation
857 //-----------------------------------------------------------------------------
859 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
861 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
)
862 : wxGraphicsContext(renderer
)
865 m_context
= new Graphics( hdc
);
869 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
870 : wxGraphicsContext(renderer
)
873 m_context
= new Graphics( hwnd
);
877 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
878 : wxGraphicsContext(renderer
)
885 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
890 void wxGDIPlusContext::Init()
897 void wxGDIPlusContext::SetDefaults()
899 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
900 m_state1
= m_context
->Save();
901 m_state2
= m_context
->Save();
904 wxGDIPlusContext::~wxGDIPlusContext()
908 m_context
->Restore( m_state2
);
909 m_context
->Restore( m_state1
);
915 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
917 Region
rgn((HRGN
)region
.GetHRGN());
918 m_context
->SetClip(&rgn
,CombineModeIntersect
);
921 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
923 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
926 void wxGDIPlusContext::ResetClip()
928 m_context
->ResetClip();
931 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
933 if ( !m_pen
.IsNull() )
935 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
939 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, int fillStyle
)
941 if ( !m_brush
.IsNull() )
943 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
944 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
945 (GraphicsPath
*) path
.GetNativePath());
949 void wxGDIPlusContext::Rotate( wxDouble angle
)
951 m_context
->RotateTransform( RadToDeg(angle
) );
954 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
956 m_context
->TranslateTransform( dx
, dy
);
959 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
961 m_context
->ScaleTransform(xScale
,yScale
);
964 void wxGDIPlusContext::PushState()
966 GraphicsState state
= m_context
->Save();
967 m_stateStack
.push_back(state
);
970 void wxGDIPlusContext::PopState()
972 GraphicsState state
= m_stateStack
.back();
973 m_stateStack
.pop_back();
974 m_context
->Restore(state
);
977 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
978 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
979 // bytes as parameter
981 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
983 Bitmap
* image
= NULL
;
984 Bitmap
* helper
= NULL
;
987 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
989 size_t width
= interim
.GetWidth();
990 size_t height
= interim
.GetHeight();
991 Rect
bounds(0,0,width
,height
);
993 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
995 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
996 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
998 BitmapData dataMask
;
999 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
1000 interimMask
.GetPixelFormat(),&dataMask
);
1003 BitmapData imageData
;
1004 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
1006 BYTE maskPattern
= 0 ;
1010 for ( size_t y
= 0 ; y
< height
; ++y
)
1013 for( size_t x
= 0 ; x
< width
; ++x
)
1018 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
1022 maskPattern
= maskPattern
>> 1;
1024 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
1025 if ( (maskByte
& maskPattern
) == 0 )
1030 interim
.GetPixel(x
,y
,&c
) ;
1031 *dest
= (c
.GetValue() | Color::AlphaMask
);
1036 image
->UnlockBits(&imageData
);
1038 interimMask
.UnlockBits(&dataMask
);
1039 interim
.UnlockBits(&dataMask
);
1043 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
1044 if ( GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
1046 size_t width
= image
->GetWidth();
1047 size_t height
= image
->GetHeight();
1048 Rect
bounds(0,0,width
,height
);
1053 helper
->LockBits(&bounds
, ImageLockModeRead
,
1054 helper
->GetPixelFormat(),&data
);
1056 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1057 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1059 helper
->UnlockBits(&data
);
1063 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1068 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1070 HICON hIcon
= (HICON
)icon
.GetHICON();
1072 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1073 if (!GetIconInfo(hIcon
,&iconInfo
))
1076 BITMAP iconBmpData
;
1077 GetObject(iconInfo
.hbmColor
,sizeof(BITMAP
),&iconBmpData
);
1078 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1080 Bitmap
* image
= NULL
;
1082 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1084 image
= Bitmap::FromHICON(hIcon
);
1088 size_t width
= interim
.GetWidth();
1089 size_t height
= interim
.GetHeight();
1090 Rect
bounds(0,0,width
,height
);
1093 interim
.LockBits(&bounds
, ImageLockModeRead
,
1094 interim
.GetPixelFormat(),&data
);
1095 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1096 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1097 interim
.UnlockBits(&data
);
1100 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1103 DeleteObject(iconInfo
.hbmColor
);
1104 DeleteObject(iconInfo
.hbmMask
);
1107 void wxGDIPlusContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
1109 if ( m_font
.IsNull() || str
.IsEmpty())
1112 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1113 m_context
->DrawString( s
, -1 , ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont() ,
1114 PointF( x
, y
) , ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusBrush() );
1115 // TODO m_backgroundMode == wxSOLID
1118 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1119 wxDouble
*descent
, wxDouble
*externalLeading
) const
1121 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1122 FontFamily ffamily
;
1123 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1125 f
->GetFamily(&ffamily
) ;
1127 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
1129 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1130 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1131 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1132 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1133 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1134 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1137 *height
= rHeight
* factorY
+ 0.5 ;
1139 *descent
= rDescent
* factorY
+ 0.5 ;
1140 if ( externalLeading
)
1141 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
+ 0.5 ;
1142 // measuring empty strings is not guaranteed, so do it by hand
1150 // MeasureString does return a rectangle that is way too large, so it is
1152 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1153 StringFormat strFormat
;
1154 CharacterRange
strRange(0,wcslen(s
));
1155 strFormat
.SetMeasurableCharacterRanges(1,&strRange
);
1157 m_context
->MeasureCharacterRanges(s
, -1 , f
,layoutRect
, &strFormat
,1,®ion
) ;
1159 region
.GetBounds(&bbox
,m_context
);
1161 *width
= bbox
.GetRight()-bbox
.GetLeft()+0.5;
1165 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1168 widths
.Add(0, text
.length());
1173 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1174 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1175 size_t len
= wcslen( ws
) ;
1176 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1178 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1179 StringFormat strFormat
;
1181 CharacterRange
* ranges
= new CharacterRange
[len
] ;
1182 Region
* regions
= new Region
[len
];
1183 for( size_t i
= 0 ; i
< len
; ++i
)
1185 ranges
[i
].First
= i
;
1186 ranges
[i
].Length
= 1 ;
1188 strFormat
.SetMeasurableCharacterRanges(len
,ranges
);
1189 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,1,regions
) ;
1192 for ( size_t i
= 0 ; i
< len
; ++i
)
1194 regions
[i
].GetBounds(&bbox
,m_context
);
1195 widths
[i
] = bbox
.GetRight()-bbox
.GetLeft();
1199 void* wxGDIPlusContext::GetNativeContext()
1204 // concatenates this transform with the current transform of this context
1205 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1207 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1210 // sets the transform of this context
1211 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1213 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1216 // gets the matrix of this context
1217 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1219 wxGraphicsMatrix matrix
= CreateMatrix();
1220 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1223 //-----------------------------------------------------------------------------
1224 // wxGDIPlusRenderer declaration
1225 //-----------------------------------------------------------------------------
1227 class WXDLLIMPEXP_CORE wxGDIPlusRenderer
: public wxGraphicsRenderer
1236 virtual ~wxGDIPlusRenderer()
1246 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
1248 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
1250 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
1252 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
1254 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
1256 virtual wxGraphicsContext
* CreateMeasuringContext();
1260 virtual wxGraphicsPath
CreatePath();
1264 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
1265 wxDouble tx
=0.0, wxDouble ty
=0.0);
1268 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
1270 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
1272 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1273 virtual wxGraphicsBrush
CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1274 const wxColour
&c1
, const wxColour
&c2
) ;
1276 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1277 // with radius r and color cColor
1278 virtual wxGraphicsBrush
CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1279 const wxColour
&oColor
, const wxColour
&cColor
) ;
1282 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) ;
1284 void EnsureIsLoaded();
1290 ULONG_PTR m_gditoken
;
1292 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
1295 //-----------------------------------------------------------------------------
1296 // wxGDIPlusRenderer implementation
1297 //-----------------------------------------------------------------------------
1299 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1301 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1303 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1305 return &gs_GDIPlusRenderer
;
1308 void wxGDIPlusRenderer::EnsureIsLoaded()
1316 void wxGDIPlusRenderer::Load()
1318 GdiplusStartupInput input
;
1319 GdiplusStartupOutput output
;
1320 GdiplusStartup(&m_gditoken
,&input
,&output
);
1324 void wxGDIPlusRenderer::Unload()
1327 GdiplusShutdown(m_gditoken
);
1330 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1333 return new wxGDIPlusContext(this,(HDC
) dc
.GetHDC());
1336 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1339 return new wxGDIPlusContext(this,(HDC
) dc
.GetHDC());
1342 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1346 // TODO use GetDC(NULL) but then we have to release it from the context
1347 //return new wxGDIPlusContext(this,(HDC) dc.GetHDC());
1350 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1353 return new wxGDIPlusContext(this,(Graphics
*) context
);
1357 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1360 return new wxGDIPlusContext(this,(HWND
) window
);
1363 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1366 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1371 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1375 m
.SetRefData( new wxGDIPlusPathData(this));
1382 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1383 wxDouble tx
, wxDouble ty
)
1388 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1389 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1394 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1397 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1398 return wxNullGraphicsPen
;
1402 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1407 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1410 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1411 return wxNullGraphicsBrush
;
1415 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1420 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1421 wxGraphicsBrush
wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
,
1422 const wxColour
&c1
, const wxColour
&c2
)
1426 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1427 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, c1
, c2
);
1432 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1433 // with radius r and color cColor
1434 wxGraphicsBrush
wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
1435 const wxColour
&oColor
, const wxColour
&cColor
)
1439 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1440 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,oColor
,cColor
);
1446 wxGraphicsFont
wxGDIPlusRenderer::CreateFont( const wxFont
&font
, const wxColour
&col
)
1452 p
.SetRefData(new wxGDIPlusFontData( this , font
, col
));
1456 return wxNullGraphicsFont
;
1459 #endif // wxUSE_GRAPHICS_CONTEXT