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"
43 #if wxUSE_ENH_METAFILE
44 #include "wx/msw/enhmeta.h"
46 #include "wx/dcgraph.h"
48 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
50 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
56 WX_DECLARE_STACK(GraphicsState
, GraphicsStates
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 const double RAD2DEG
= 180.0 / M_PI
;
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
72 inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
74 inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
75 inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
77 // translate a wxColour to a Color
78 inline Color
wxColourToColor(const wxColour
& col
)
80 return Color(col
.Alpha(), col
.Red(), col
.Green(), col
.Blue());
83 } // anonymous namespace
85 //-----------------------------------------------------------------------------
86 // device context implementation
88 // more and more of the dc functionality should be implemented by calling
89 // the appropricate wxGDIPlusContext, but we will have to do that step by step
90 // also coordinate conversions should be moved to native matrix ops
91 //-----------------------------------------------------------------------------
93 // we always stock two context states, one at entry, to be able to preserve the
94 // state we were called with, the other one after changing to HI Graphics orientation
95 // (this one is used for getting back clippings etc)
97 //-----------------------------------------------------------------------------
98 // wxGraphicsPath implementation
99 //-----------------------------------------------------------------------------
101 class wxGDIPlusContext
;
103 class wxGDIPlusPathData
: public wxGraphicsPathData
106 wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
107 ~wxGDIPlusPathData();
109 virtual wxGraphicsObjectRefData
*Clone() const;
112 // These are the path primitives from which everything else can be constructed
115 // begins a new subpath at (x,y)
116 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
118 // adds a straight line from the current point to (x,y)
119 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
121 // adds a cubic Bezier curve from the current point, using two control points and an end point
122 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
125 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
126 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
128 // gets the last point of the current path, (0,0) if not yet set
129 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
132 virtual void AddPath( const wxGraphicsPathData
* path
);
134 // closes the current sub-path
135 virtual void CloseSubpath();
138 // These are convenience functions which - if not available natively will be assembled
139 // using the primitives from above
142 // appends a rectangle as a new closed subpath
143 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
146 // appends an ellipsis as a new closed subpath fitting the passed rectangle
147 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
149 // 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)
150 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
153 // returns the native path
154 virtual void * GetNativePath() const { return m_path
; }
156 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
157 virtual void UnGetNativePath(void * WXUNUSED(path
)) const {}
159 // transforms each point of this path by the matrix
160 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
162 // gets the bounding box enclosing all points (possibly including control points)
163 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
165 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
168 GraphicsPath
* m_path
;
171 class wxGDIPlusMatrixData
: public wxGraphicsMatrixData
174 wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
175 virtual ~wxGDIPlusMatrixData() ;
177 virtual wxGraphicsObjectRefData
* Clone() const ;
179 // concatenates the matrix
180 virtual void Concat( const wxGraphicsMatrixData
*t
);
182 // sets the matrix to the respective values
183 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
184 wxDouble tx
=0.0, wxDouble ty
=0.0);
186 // gets the component valuess of the matrix
187 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
188 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
190 // makes this the inverse matrix
191 virtual void Invert();
193 // returns true if the elements of the transformation matrix are equal ?
194 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
196 // return true if this is the identity matrix
197 virtual bool IsIdentity() const;
203 // add the translation to this matrix
204 virtual void Translate( wxDouble dx
, wxDouble dy
);
206 // add the scale to this matrix
207 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
209 // add the rotation to this matrix (radians)
210 virtual void Rotate( wxDouble angle
);
213 // apply the transforms
216 // applies that matrix to the point
217 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
219 // applies the matrix except for translations
220 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
222 // returns the native representation
223 virtual void * GetNativeMatrix() const;
228 class wxGDIPlusPenData
: public wxGraphicsObjectRefData
231 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
236 virtual wxDouble
GetWidth() { return m_width
; }
237 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
247 class wxGDIPlusBrushData
: public wxGraphicsObjectRefData
250 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
251 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
252 ~wxGDIPlusBrushData ();
254 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
255 wxDouble x2
, wxDouble y2
,
256 const wxGraphicsGradientStops
& stops
);
257 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
258 wxDouble xc
, wxDouble yc
,
260 const wxGraphicsGradientStops
& stops
);
262 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
268 // common part of Create{Linear,Radial}GradientBrush()
269 template <typename T
>
270 void SetGradientStops(T
*brush
, const wxGraphicsGradientStops
& stops
);
274 GraphicsPath
* m_brushPath
;
277 class WXDLLIMPEXP_CORE wxGDIPlusBitmapData
: public wxGraphicsObjectRefData
280 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
);
281 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
&bmp
);
282 ~wxGDIPlusBitmapData ();
284 virtual Bitmap
* GetGDIPlusBitmap() { return m_bitmap
; }
291 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
294 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
295 const wxGDIPlusContext
* gc
,
297 const wxColour
& col
);
298 ~wxGDIPlusFontData();
300 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
301 virtual Font
* GetGDIPlusFont() { return m_font
; }
307 class wxGDIPlusContext
: public wxGraphicsContext
310 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
311 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
312 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
313 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
316 virtual ~wxGDIPlusContext();
318 virtual void Clip( const wxRegion
®ion
);
319 // clips drawings to the rect
320 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
322 // resets the clipping to original extent
323 virtual void ResetClip();
325 virtual void * GetNativeContext();
327 virtual void StrokePath( const wxGraphicsPath
& p
);
328 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
330 // stroke lines connecting each of the points
331 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
334 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
336 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
338 virtual bool SetCompositionMode(wxCompositionMode op
);
340 virtual void BeginLayer(wxDouble opacity
);
342 virtual void EndLayer();
344 virtual void Translate( wxDouble dx
, wxDouble dy
);
345 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
346 virtual void Rotate( wxDouble angle
);
348 // concatenates this transform with the current transform of this context
349 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
351 // sets the transform of this context
352 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
354 // gets the matrix of this context
355 virtual wxGraphicsMatrix
GetTransform() const;
357 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
358 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
359 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
360 virtual void PushState();
361 virtual void PopState();
363 // sets the font of this context
364 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) const;
366 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
367 wxDouble
*descent
, wxDouble
*externalLeading
) const;
368 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
369 virtual bool ShouldOffset() const;
370 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
372 Graphics
* GetGraphics() const { return m_context
; }
376 wxDouble m_fontScaleRatio
;
382 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
383 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
384 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
385 const wxGraphicsBrush
& backgroundBrush
);
388 GraphicsStates m_stateStack
;
389 GraphicsState m_state1
;
390 GraphicsState m_state2
;
395 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
398 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
401 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
404 wxGDIPlusMeasuringContext()
408 virtual ~wxGDIPlusMeasuringContext()
410 ReleaseDC( NULL
, m_hdc
);
415 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
418 class wxGDIPlusPrintingContext
: public wxGDIPlusContext
421 wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
422 virtual ~wxGDIPlusPrintingContext() { }
426 //-----------------------------------------------------------------------------
427 // wxGDIPlusRenderer declaration
428 //-----------------------------------------------------------------------------
430 class wxGDIPlusRenderer
: public wxGraphicsRenderer
439 virtual ~wxGDIPlusRenderer()
449 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
451 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
453 #if wxUSE_PRINTING_ARCHITECTURE
454 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
457 #if wxUSE_ENH_METAFILE
458 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
461 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
463 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
465 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
467 virtual wxGraphicsContext
* CreateMeasuringContext();
471 virtual wxGraphicsPath
CreatePath();
475 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
476 wxDouble tx
=0.0, wxDouble ty
=0.0);
479 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
481 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
483 virtual wxGraphicsBrush
484 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
485 wxDouble x2
, wxDouble y2
,
486 const wxGraphicsGradientStops
& stops
);
488 virtual wxGraphicsBrush
489 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
490 wxDouble xc
, wxDouble yc
,
492 const wxGraphicsGradientStops
& stops
);
494 // create a native bitmap representation
495 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
497 // stub: should not be called directly
498 virtual wxGraphicsFont
CreateFont( const wxFont
& WXUNUSED(font
),
499 const wxColour
& WXUNUSED(col
) )
500 { wxFAIL
; return wxNullGraphicsFont
; }
502 // this is used to really create the font
503 wxGraphicsFont
CreateGDIPlusFont( const wxGDIPlusContext
* gc
,
505 const wxColour
&col
);
507 // create a graphics bitmap from a native bitmap
508 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
510 // create a subimage from a native image representation
511 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
514 bool EnsureIsLoaded();
517 friend class wxGDIPlusRendererModule
;
521 ULONG_PTR m_gditoken
;
523 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
526 //-----------------------------------------------------------------------------
527 // wxGDIPlusPen implementation
528 //-----------------------------------------------------------------------------
530 wxGDIPlusPenData::~wxGDIPlusPenData()
537 void wxGDIPlusPenData::Init()
544 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
545 : wxGraphicsObjectRefData(renderer
)
548 m_width
= pen
.GetWidth();
552 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
555 switch ( pen
.GetCap() )
561 case wxCAP_PROJECTING
:
566 cap
= LineCapFlat
; // TODO verify
573 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
576 switch ( pen
.GetJoin() )
579 join
= LineJoinBevel
;
583 join
= LineJoinMiter
;
587 join
= LineJoinRound
;
591 join
= LineJoinMiter
;
595 m_pen
->SetLineJoin(join
);
597 m_pen
->SetDashStyle(DashStyleSolid
);
599 DashStyle dashStyle
= DashStyleSolid
;
600 switch ( pen
.GetStyle() )
606 dashStyle
= DashStyleDot
;
610 dashStyle
= DashStyleDash
; // TODO verify
614 dashStyle
= DashStyleDash
;
618 dashStyle
= DashStyleDashDot
;
622 dashStyle
= DashStyleCustom
;
624 int count
= pen
.GetDashes( &dashes
);
625 if ((dashes
!= NULL
) && (count
> 0))
627 REAL
*userLengths
= new REAL
[count
];
628 for ( int i
= 0; i
< count
; ++i
)
630 userLengths
[i
] = dashes
[i
];
632 m_pen
->SetDashPattern( userLengths
, count
);
633 delete[] userLengths
;
639 wxBitmap
* bmp
= pen
.GetStipple();
640 if ( bmp
&& bmp
->Ok() )
642 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
644 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
649 m_penBrush
= new TextureBrush(m_penImage
);
650 m_pen
->SetBrush( m_penBrush
);
656 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
658 HatchStyle style
= HatchStyleHorizontal
;
659 switch( pen
.GetStyle() )
661 case wxBDIAGONAL_HATCH
:
662 style
= HatchStyleBackwardDiagonal
;
664 case wxCROSSDIAG_HATCH
:
665 style
= HatchStyleDiagonalCross
;
667 case wxFDIAGONAL_HATCH
:
668 style
= HatchStyleForwardDiagonal
;
671 style
= HatchStyleCross
;
673 case wxHORIZONTAL_HATCH
:
674 style
= HatchStyleHorizontal
;
676 case wxVERTICAL_HATCH
:
677 style
= HatchStyleVertical
;
681 m_penBrush
= new HatchBrush
684 wxColourToColor(pen
.GetColour()),
687 m_pen
->SetBrush( m_penBrush
);
691 if ( dashStyle
!= DashStyleSolid
)
692 m_pen
->SetDashStyle(dashStyle
);
695 //-----------------------------------------------------------------------------
696 // wxGDIPlusBrush implementation
697 //-----------------------------------------------------------------------------
699 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
700 : wxGraphicsObjectRefData(renderer
)
705 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
706 : wxGraphicsObjectRefData(renderer
)
709 if ( brush
.GetStyle() == wxSOLID
)
711 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
713 else if ( brush
.IsHatch() )
715 HatchStyle style
= HatchStyleHorizontal
;
716 switch( brush
.GetStyle() )
718 case wxBDIAGONAL_HATCH
:
719 style
= HatchStyleBackwardDiagonal
;
721 case wxCROSSDIAG_HATCH
:
722 style
= HatchStyleDiagonalCross
;
724 case wxFDIAGONAL_HATCH
:
725 style
= HatchStyleForwardDiagonal
;
728 style
= HatchStyleCross
;
730 case wxHORIZONTAL_HATCH
:
731 style
= HatchStyleHorizontal
;
733 case wxVERTICAL_HATCH
:
734 style
= HatchStyleVertical
;
738 m_brush
= new HatchBrush
741 wxColourToColor(brush
.GetColour()),
747 wxBitmap
* bmp
= brush
.GetStipple();
748 if ( bmp
&& bmp
->Ok() )
750 wxDELETE( m_brushImage
);
751 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
753 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
758 m_brush
= new TextureBrush(m_brushImage
);
763 wxGDIPlusBrushData::~wxGDIPlusBrushData()
770 void wxGDIPlusBrushData::Init()
777 template <typename T
>
779 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
780 const wxGraphicsGradientStops
& stops
)
782 const unsigned numStops
= stops
.GetCount();
785 // initial and final colours are set during the brush creation, nothing
790 wxVector
<Color
> colors(numStops
);
791 wxVector
<REAL
> positions(numStops
);
793 for ( unsigned i
= 0; i
< numStops
; i
++ )
795 wxGraphicsGradientStop stop
= stops
.Item(i
);
797 colors
[i
] = wxColourToColor(stop
.GetColour());
798 positions
[i
] = stop
.GetPosition();
801 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
805 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
806 wxDouble x2
, wxDouble y2
,
807 const wxGraphicsGradientStops
& stops
)
809 LinearGradientBrush
* const
810 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
811 wxColourToColor(stops
.GetStartColour()),
812 wxColourToColor(stops
.GetEndColour()));
815 SetGradientStops(brush
, stops
);
819 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
820 wxDouble xc
, wxDouble yc
,
822 const wxGraphicsGradientStops
& stops
)
824 m_brushPath
= new GraphicsPath();
825 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
826 (REAL
)(2*radius
), (REAL
)(2*radius
));
828 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
830 brush
->SetCenterPoint(PointF(xo
, yo
));
831 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
833 const Color
col(wxColourToColor(stops
.GetEndColour()));
835 brush
->SetSurroundColors(&col
, &count
);
837 SetGradientStops(brush
, stops
);
840 //-----------------------------------------------------------------------------
841 // wxGDIPlusFont implementation
842 //-----------------------------------------------------------------------------
844 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
845 const wxGDIPlusContext
* gc
,
847 const wxColour
& col
)
848 : wxGraphicsObjectRefData( renderer
)
850 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
851 int style
= FontStyleRegular
;
852 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
853 style
|= FontStyleItalic
;
854 if ( font
.GetUnderlined() )
855 style
|= FontStyleUnderline
;
856 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
857 style
|= FontStyleBold
;
859 Graphics
* context
= gc
->GetGraphics();
861 Unit fontUnit
= context
->GetPageUnit();
862 // if fontUnit is UnitDisplay, then specify UnitPixel, otherwise
863 // you'll get a "InvalidParameter" from GDI+
864 if ( fontUnit
== UnitDisplay
)
865 fontUnit
= UnitPixel
;
867 REAL points
= font
.GetPointSize();
869 // This scaling is needed when we use unit other than the
870 // default UnitPoint. It works for both display and printing.
871 REAL size
= points
* (100.0 / 72.0);
873 // NB: font unit should match context's unit. We can use UnitPixel,
874 // as that is what the print context should use.
875 m_font
= new Font( s
, size
, style
, fontUnit
);
877 m_textBrush
= new SolidBrush(wxColourToColor(col
));
880 wxGDIPlusFontData::~wxGDIPlusFontData()
886 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
887 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
888 // bytes as parameter, since there is no real copying of the data going in, only references are stored
889 // m_helper has to be kept alive as well
891 //-----------------------------------------------------------------------------
892 // wxGDIPlusBitmapData implementation
893 //-----------------------------------------------------------------------------
895 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
896 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
901 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
902 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
907 Bitmap
* image
= NULL
;
910 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
912 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
918 size_t width
= interim
.GetWidth();
919 size_t height
= interim
.GetHeight();
920 Rect
bounds(0,0,width
,height
);
922 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
924 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
925 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
927 BitmapData dataMask
;
928 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
929 interimMask
.GetPixelFormat(),&dataMask
);
932 BitmapData imageData
;
933 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
935 BYTE maskPattern
= 0 ;
939 for ( size_t y
= 0 ; y
< height
; ++y
)
942 for( size_t x
= 0 ; x
< width
; ++x
)
947 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
951 maskPattern
= maskPattern
>> 1;
953 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
954 if ( (maskByte
& maskPattern
) == 0 )
959 interim
.GetPixel(x
,y
,&c
) ;
960 *dest
= (c
.GetValue() | Color::AlphaMask
);
965 image
->UnlockBits(&imageData
);
967 interimMask
.UnlockBits(&dataMask
);
968 interim
.UnlockBits(&dataMask
);
972 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
974 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
979 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
981 size_t width
= image
->GetWidth();
982 size_t height
= image
->GetHeight();
983 Rect
bounds(0,0,width
,height
);
984 static BitmapData data
;
988 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
989 m_helper
->GetPixelFormat(),&data
);
991 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
992 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
994 m_helper
->UnlockBits(&data
);
1001 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1007 //-----------------------------------------------------------------------------
1008 // wxGDIPlusPath implementation
1009 //-----------------------------------------------------------------------------
1011 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
1016 m_path
= new GraphicsPath();
1019 wxGDIPlusPathData::~wxGDIPlusPathData()
1024 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
1026 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
1033 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1035 m_path
->StartFigure();
1036 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1039 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1041 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1044 void wxGDIPlusPathData::CloseSubpath()
1046 m_path
->CloseFigure();
1049 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1055 m_path
->GetLastPoint(&start
);
1056 m_path
->AddBezier(start
,c1
,c2
,end
);
1059 // gets the last point of the current path, (0,0) if not yet set
1060 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1063 m_path
->GetLastPoint(&start
);
1068 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1070 double sweepAngle
= endAngle
- startAngle
;
1071 if( fabs(sweepAngle
) >= 2*M_PI
)
1073 sweepAngle
= 2 * M_PI
;
1079 if( sweepAngle
< 0 )
1080 sweepAngle
+= 2 * M_PI
;
1084 if( sweepAngle
> 0 )
1085 sweepAngle
-= 2 * M_PI
;
1089 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
1092 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1094 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
1097 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
1099 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
1103 // transforms each point of this path by the matrix
1104 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1106 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
1109 // gets the bounding box enclosing all points (possibly including control points)
1110 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1113 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
1120 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1122 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1123 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
1126 //-----------------------------------------------------------------------------
1127 // wxGDIPlusMatrixData implementation
1128 //-----------------------------------------------------------------------------
1130 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
1131 : wxGraphicsMatrixData(renderer
)
1136 m_matrix
= new Matrix();
1139 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1144 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1146 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1149 // concatenates the matrix
1150 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1152 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1155 // sets the matrix to the respective values
1156 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1157 wxDouble tx
, wxDouble ty
)
1159 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1162 // gets the component valuess of the matrix
1163 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1164 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1167 m_matrix
->GetElements(elements
);
1168 if (a
) *a
= elements
[0];
1169 if (b
) *b
= elements
[1];
1170 if (c
) *c
= elements
[2];
1171 if (d
) *d
= elements
[3];
1172 if (tx
) *tx
= elements
[4];
1173 if (ty
) *ty
= elements
[5];
1176 // makes this the inverse matrix
1177 void wxGDIPlusMatrixData::Invert()
1182 // returns true if the elements of the transformation matrix are equal ?
1183 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1185 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1188 // return true if this is the identity matrix
1189 bool wxGDIPlusMatrixData::IsIdentity() const
1191 return m_matrix
->IsIdentity() == TRUE
;
1198 // add the translation to this matrix
1199 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1201 m_matrix
->Translate(dx
,dy
);
1204 // add the scale to this matrix
1205 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1207 m_matrix
->Scale(xScale
,yScale
);
1210 // add the rotation to this matrix (radians)
1211 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1213 m_matrix
->Rotate( RadToDeg(angle
) );
1217 // apply the transforms
1220 // applies that matrix to the point
1221 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1224 m_matrix
->TransformPoints(&pt
);
1229 // applies the matrix except for translations
1230 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1233 m_matrix
->TransformVectors(&pt
);
1238 // returns the native representation
1239 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1244 //-----------------------------------------------------------------------------
1245 // wxGDIPlusContext implementation
1246 //-----------------------------------------------------------------------------
1248 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1249 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1251 class wxGDIPlusOffsetHelper
1254 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1259 m_gr
->TranslateTransform( 0.5, 0.5 );
1261 ~wxGDIPlusOffsetHelper( )
1264 m_gr
->TranslateTransform( -0.5, -0.5 );
1271 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1272 : wxGraphicsContext(renderer
)
1275 m_context
= new Graphics( hdc
);
1281 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
)
1282 : wxGraphicsContext(renderer
)
1286 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1287 HDC hdc
= (HDC
) msw
->GetHDC();
1289 m_context
= new Graphics(hdc
);
1290 wxSize sz
= dc
.GetSize();
1297 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1298 : wxGraphicsContext(renderer
)
1301 m_context
= new Graphics( hwnd
);
1302 RECT rect
= wxGetWindowRect(hwnd
);
1303 m_width
= rect
.right
- rect
.left
;
1304 m_height
= rect
.bottom
- rect
.top
;
1308 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1309 : wxGraphicsContext(renderer
)
1316 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1321 void wxGDIPlusContext::Init()
1328 m_fontScaleRatio
= 1.0;
1331 void wxGDIPlusContext::SetDefaults()
1333 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1334 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1335 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1336 m_state1
= m_context
->Save();
1337 m_state2
= m_context
->Save();
1340 wxGDIPlusContext::~wxGDIPlusContext()
1344 m_context
->Restore( m_state2
);
1345 m_context
->Restore( m_state1
);
1351 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1353 Region
rgn((HRGN
)region
.GetHRGN());
1354 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1357 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1359 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1362 void wxGDIPlusContext::ResetClip()
1364 m_context
->ResetClip();
1367 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1369 if (m_composition
== wxCOMPOSITION_DEST
)
1372 if ( !m_pen
.IsNull() )
1374 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1375 Point
*cpoints
= new Point
[n
];
1376 for (size_t i
= 0; i
< n
; i
++)
1378 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1379 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1381 } // for (size_t i = 0; i < n; i++)
1382 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1387 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1389 if (m_composition
== wxCOMPOSITION_DEST
)
1392 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1393 Point
*cpoints
= new Point
[n
];
1394 for (size_t i
= 0; i
< n
; i
++)
1396 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1397 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1399 } // for (int i = 0; i < n; i++)
1400 if ( !m_brush
.IsNull() )
1401 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1402 if ( !m_pen
.IsNull() )
1403 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1407 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1409 if (m_composition
== wxCOMPOSITION_DEST
)
1412 if ( !m_pen
.IsNull() )
1414 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1415 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1419 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1421 if (m_composition
== wxCOMPOSITION_DEST
)
1424 if ( !m_brush
.IsNull() )
1426 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1427 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1428 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1429 (GraphicsPath
*) path
.GetNativePath());
1433 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1435 if (m_antialias
== antialias
)
1438 m_antialias
= antialias
;
1440 SmoothingMode antialiasMode
;
1443 case wxANTIALIAS_DEFAULT
:
1444 antialiasMode
= SmoothingModeHighQuality
;
1446 case wxANTIALIAS_NONE
:
1447 antialiasMode
= SmoothingModeNone
;
1452 m_context
->SetSmoothingMode(antialiasMode
);
1456 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1458 if ( m_composition
== op
)
1463 if (m_composition
== wxCOMPOSITION_DEST
)
1466 CompositingMode cop
;
1469 case wxCOMPOSITION_SOURCE
:
1470 cop
= CompositingModeSourceCopy
;
1472 case wxCOMPOSITION_OVER
:
1473 cop
= CompositingModeSourceOver
;
1479 m_context
->SetCompositingMode(cop
);
1483 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1488 void wxGDIPlusContext::EndLayer()
1493 void wxGDIPlusContext::Rotate( wxDouble angle
)
1495 m_context
->RotateTransform( RadToDeg(angle
) );
1498 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1500 m_context
->TranslateTransform( dx
, dy
);
1503 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1505 m_context
->ScaleTransform(xScale
,yScale
);
1508 void wxGDIPlusContext::PushState()
1510 GraphicsState state
= m_context
->Save();
1511 m_stateStack
.push(state
);
1514 void wxGDIPlusContext::PopState()
1516 GraphicsState state
= m_stateStack
.top();
1518 m_context
->Restore(state
);
1521 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1523 if (m_composition
== wxCOMPOSITION_DEST
)
1526 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1529 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1531 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1532 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1533 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1534 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1537 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1541 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1543 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1544 DrawBitmap(bitmap
, x
, y
, w
, h
);
1547 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1549 if (m_composition
== wxCOMPOSITION_DEST
)
1552 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1553 // find out by looking at the bitmap data whether there really was alpha in it
1554 HICON hIcon
= (HICON
)icon
.GetHICON();
1556 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1557 if (!GetIconInfo(hIcon
,&iconInfo
))
1560 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1562 Bitmap
* image
= NULL
;
1564 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1565 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1566 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1568 image
= Bitmap::FromHICON(hIcon
);
1572 size_t width
= interim
.GetWidth();
1573 size_t height
= interim
.GetHeight();
1574 Rect
bounds(0,0,width
,height
);
1577 interim
.LockBits(&bounds
, ImageLockModeRead
,
1578 interim
.GetPixelFormat(),&data
);
1580 bool hasAlpha
= false;
1581 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1583 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1585 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1586 if ( ( *dest
& Color::AlphaMask
) != 0 )
1593 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1594 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1598 image
= Bitmap::FromHICON(hIcon
);
1601 interim
.UnlockBits(&data
);
1604 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1607 DeleteObject(iconInfo
.hbmColor
);
1608 DeleteObject(iconInfo
.hbmMask
);
1611 wxGraphicsFont
wxGDIPlusContext::CreateFont( const wxFont
&font
,
1612 const wxColour
&col
) const
1614 wxGDIPlusRenderer
* renderer
=
1615 static_cast<wxGDIPlusRenderer
*>(GetRenderer());
1616 return renderer
->CreateGDIPlusFont(this, font
, col
);
1619 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1620 wxDouble x
, wxDouble y
,
1621 const wxGraphicsBrush
& brush
)
1623 if (m_composition
== wxCOMPOSITION_DEST
)
1626 wxCHECK_RET( !m_font
.IsNull(),
1627 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1632 wxGDIPlusFontData
* const
1633 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1634 wxGDIPlusBrushData
* const
1635 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1637 m_context
->DrawString
1639 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1640 -1, // length: string is NUL-terminated
1641 fontData
->GetGDIPlusFont(),
1643 StringFormat::GenericTypographic(),
1644 brushData
? brushData
->GetGDIPlusBrush()
1645 : fontData
->GetGDIPlusBrush()
1649 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1650 wxDouble
*descent
, wxDouble
*externalLeading
) const
1652 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1654 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1655 FontFamily ffamily
;
1656 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1658 f
->GetFamily(&ffamily
) ;
1660 REAL factorY
= m_fontScaleRatio
;
1662 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1663 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1664 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1665 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1666 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1667 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1670 *height
= rHeight
* factorY
;
1672 *descent
= rDescent
* factorY
;
1673 if ( externalLeading
)
1674 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1675 // measuring empty strings is not guaranteed, so do it by hand
1683 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1684 StringFormat
strFormat( StringFormat::GenericTypographic() );
1685 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1688 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1690 *width
= bounds
.Width
;
1692 *height
= bounds
.Height
;
1696 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1699 widths
.Add(0, text
.length());
1701 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1706 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1707 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1708 size_t len
= wcslen( ws
) ;
1709 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1711 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1712 StringFormat
strFormat( StringFormat::GenericTypographic() );
1714 size_t startPosition
= 0;
1715 size_t remainder
= len
;
1716 const size_t maxSpan
= 32;
1717 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1718 Region
* regions
= new Region
[maxSpan
];
1720 while( remainder
> 0 )
1722 size_t span
= wxMin( maxSpan
, remainder
);
1724 for( size_t i
= 0 ; i
< span
; ++i
)
1726 ranges
[i
].First
= 0 ;
1727 ranges
[i
].Length
= startPosition
+i
+1 ;
1729 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1730 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1731 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1734 for ( size_t i
= 0 ; i
< span
; ++i
)
1736 regions
[i
].GetBounds(&bbox
,m_context
);
1737 widths
[startPosition
+i
] = bbox
.Width
;
1740 startPosition
+= span
;
1747 bool wxGDIPlusContext::ShouldOffset() const
1750 if ( !m_pen
.IsNull() )
1752 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1753 if ( penwidth
== 0 )
1756 return ( penwidth
% 2 ) == 1;
1759 void* wxGDIPlusContext::GetNativeContext()
1764 // concatenates this transform with the current transform of this context
1765 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1767 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1770 // sets the transform of this context
1771 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1773 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1776 // gets the matrix of this context
1777 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1779 wxGraphicsMatrix matrix
= CreateMatrix();
1780 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1784 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1790 //-----------------------------------------------------------------------------
1791 // wxGDIPlusPrintingContext implementation
1792 //-----------------------------------------------------------------------------
1794 wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
,
1796 : wxGDIPlusContext(renderer
, dc
)
1798 Graphics
* context
= GetGraphics();
1800 //m_context->SetPageUnit(UnitDocument);
1802 // Setup page scale, based on DPI ratio.
1803 // Antecedent should be 100dpi when the default page unit
1804 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1805 // instead. Note that calling SetPageScale() does not have effect on
1806 // non-printing DCs (that is, any other than wxPrinterDC or
1807 // wxEnhMetaFileDC).
1808 REAL dpiRatio
= 100.0 / context
->GetDpiY();
1809 context
->SetPageScale(dpiRatio
);
1811 // We use this modifier when measuring fonts. It is needed because the
1812 // page scale is modified above.
1813 m_fontScaleRatio
= context
->GetDpiY() / 72.0;
1816 //-----------------------------------------------------------------------------
1817 // wxGDIPlusRenderer implementation
1818 //-----------------------------------------------------------------------------
1820 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1822 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1824 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1826 return &gs_GDIPlusRenderer
;
1829 bool wxGDIPlusRenderer::EnsureIsLoaded()
1831 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1832 // if we already tried and failed (we don't want to spend lot of time
1833 // returning NULL from wxGraphicsContext::Create(), which may be called
1834 // relatively frequently):
1835 if ( m_loaded
== -1 )
1840 return m_loaded
== 1;
1843 // call EnsureIsLoaded() and return returnOnFail value if it fails
1844 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1845 if ( !EnsureIsLoaded() ) \
1846 return (returnOnFail)
1849 void wxGDIPlusRenderer::Load()
1851 GdiplusStartupInput input
;
1852 GdiplusStartupOutput output
;
1853 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1855 wxLogTrace("gdiplus", "successfully initialized GDI+");
1860 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1865 void wxGDIPlusRenderer::Unload()
1869 GdiplusShutdown(m_gditoken
);
1872 m_loaded
= -1; // next Load() will try again
1875 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1877 ENSURE_LOADED_OR_RETURN(NULL
);
1878 return new wxGDIPlusContext(this, dc
);
1881 #if wxUSE_PRINTING_ARCHITECTURE
1882 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1884 ENSURE_LOADED_OR_RETURN(NULL
);
1885 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1890 #if wxUSE_ENH_METAFILE
1891 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1893 ENSURE_LOADED_OR_RETURN(NULL
);
1894 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1899 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1901 ENSURE_LOADED_OR_RETURN(NULL
);
1902 return new wxGDIPlusContext(this, dc
);
1905 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1907 ENSURE_LOADED_OR_RETURN(NULL
);
1908 return new wxGDIPlusMeasuringContext(this);
1911 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1913 ENSURE_LOADED_OR_RETURN(NULL
);
1914 return new wxGDIPlusContext(this,(Graphics
*) context
);
1918 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1920 ENSURE_LOADED_OR_RETURN(NULL
);
1921 return new wxGDIPlusContext(this,(HWND
) window
);
1924 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1926 ENSURE_LOADED_OR_RETURN(NULL
);
1927 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1932 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1934 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1936 m
.SetRefData( new wxGDIPlusPathData(this));
1943 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1944 wxDouble tx
, wxDouble ty
)
1947 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1949 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1950 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1955 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1957 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1958 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1959 return wxNullGraphicsPen
;
1963 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1968 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1970 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1971 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1972 return wxNullGraphicsBrush
;
1976 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1982 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1983 wxDouble x2
, wxDouble y2
,
1984 const wxGraphicsGradientStops
& stops
)
1986 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1988 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1989 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1995 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1996 wxDouble xc
, wxDouble yc
,
1998 const wxGraphicsGradientStops
& stops
)
2000 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2002 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2003 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
2009 wxGDIPlusRenderer::CreateGDIPlusFont( const wxGDIPlusContext
* gc
,
2011 const wxColour
&col
)
2013 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2017 p
.SetRefData(new wxGDIPlusFontData( this, gc
, font
, col
));
2021 return wxNullGraphicsFont
;
2024 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
2026 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2030 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
2034 return wxNullGraphicsBitmap
;
2037 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
2039 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2040 if ( bitmap
!= NULL
)
2043 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
2047 return wxNullGraphicsBitmap
;
2050 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2052 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2053 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
2057 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
2061 return wxNullGraphicsBitmap
;
2064 // Shutdown GDI+ at app exit, before possible dll unload
2065 class wxGDIPlusRendererModule
: public wxModule
2068 virtual bool OnInit() { return true; }
2069 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
2072 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
2075 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
2077 // ----------------------------------------------------------------------------
2078 // wxMSW-specific parts of wxGCDC
2079 // ----------------------------------------------------------------------------
2081 WXHDC
wxGCDC::AcquireHDC()
2083 wxGraphicsContext
* const gc
= GetGraphicsContext();
2087 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2088 return g
? g
->GetHDC() : NULL
;
2091 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2096 wxGraphicsContext
* const gc
= GetGraphicsContext();
2097 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2099 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2100 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2102 g
->ReleaseHDC((HDC
)hdc
);
2105 #endif // wxUSE_GRAPHICS_CONTEXT