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__)
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 const double RAD2DEG
= 180.0 / M_PI
;
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
70 inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
72 inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
73 inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
75 // translate a wxColour to a Color
76 inline Color
wxColourToColor(const wxColour
& col
)
78 return Color(col
.Alpha(), col
.Red(), col
.Green(), col
.Blue());
81 } // anonymous namespace
83 //-----------------------------------------------------------------------------
84 // device context implementation
86 // more and more of the dc functionality should be implemented by calling
87 // the appropricate wxGDIPlusContext, but we will have to do that step by step
88 // also coordinate conversions should be moved to native matrix ops
89 //-----------------------------------------------------------------------------
91 // we always stock two context states, one at entry, to be able to preserve the
92 // state we were called with, the other one after changing to HI Graphics orientation
93 // (this one is used for getting back clippings etc)
95 //-----------------------------------------------------------------------------
96 // wxGraphicsPath implementation
97 //-----------------------------------------------------------------------------
99 class wxGDIPlusContext
;
101 class wxGDIPlusPathData
: public wxGraphicsPathData
104 wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
= NULL
);
105 ~wxGDIPlusPathData();
107 virtual wxGraphicsObjectRefData
*Clone() const;
110 // These are the path primitives from which everything else can be constructed
113 // begins a new subpath at (x,y)
114 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
116 // adds a straight line from the current point to (x,y)
117 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
119 // adds a cubic Bezier curve from the current point, using two control points and an end point
120 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
123 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
124 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
126 // gets the last point of the current path, (0,0) if not yet set
127 virtual void GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const;
130 virtual void AddPath( const wxGraphicsPathData
* path
);
132 // closes the current sub-path
133 virtual void CloseSubpath();
136 // These are convenience functions which - if not available natively will be assembled
137 // using the primitives from above
140 // appends a rectangle as a new closed subpath
141 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
144 // appends an ellipsis as a new closed subpath fitting the passed rectangle
145 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
147 // 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)
148 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
151 // returns the native path
152 virtual void * GetNativePath() const { return m_path
; }
154 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
155 virtual void UnGetNativePath(void * WXUNUSED(path
)) const {}
157 // transforms each point of this path by the matrix
158 virtual void Transform( const wxGraphicsMatrixData
* matrix
) ;
160 // gets the bounding box enclosing all points (possibly including control points)
161 virtual void GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const;
163 virtual bool Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
) const;
166 GraphicsPath
* m_path
;
169 class wxGDIPlusMatrixData
: public wxGraphicsMatrixData
172 wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
= NULL
) ;
173 virtual ~wxGDIPlusMatrixData() ;
175 virtual wxGraphicsObjectRefData
* Clone() const ;
177 // concatenates the matrix
178 virtual void Concat( const wxGraphicsMatrixData
*t
);
180 // sets the matrix to the respective values
181 virtual void Set(wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
182 wxDouble tx
=0.0, wxDouble ty
=0.0);
184 // gets the component valuess of the matrix
185 virtual void Get(wxDouble
* a
=NULL
, wxDouble
* b
=NULL
, wxDouble
* c
=NULL
,
186 wxDouble
* d
=NULL
, wxDouble
* tx
=NULL
, wxDouble
* ty
=NULL
) const;
188 // makes this the inverse matrix
189 virtual void Invert();
191 // returns true if the elements of the transformation matrix are equal ?
192 virtual bool IsEqual( const wxGraphicsMatrixData
* t
) const ;
194 // return true if this is the identity matrix
195 virtual bool IsIdentity() const;
201 // add the translation to this matrix
202 virtual void Translate( wxDouble dx
, wxDouble dy
);
204 // add the scale to this matrix
205 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
207 // add the rotation to this matrix (radians)
208 virtual void Rotate( wxDouble angle
);
211 // apply the transforms
214 // applies that matrix to the point
215 virtual void TransformPoint( wxDouble
*x
, wxDouble
*y
) const;
217 // applies the matrix except for translations
218 virtual void TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const;
220 // returns the native representation
221 virtual void * GetNativeMatrix() const;
226 class wxGDIPlusPenData
: public wxGraphicsObjectRefData
229 wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
);
234 virtual wxDouble
GetWidth() { return m_width
; }
235 virtual Pen
* GetGDIPlusPen() { return m_pen
; }
245 class wxGDIPlusBrushData
: public wxGraphicsObjectRefData
248 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
);
249 wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
);
250 ~wxGDIPlusBrushData ();
252 void CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
253 wxDouble x2
, wxDouble y2
,
254 const wxGraphicsGradientStops
& stops
);
255 void CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
256 wxDouble xc
, wxDouble yc
,
258 const wxGraphicsGradientStops
& stops
);
260 virtual Brush
* GetGDIPlusBrush() { return m_brush
; }
266 // common part of Create{Linear,Radial}GradientBrush()
267 template <typename T
>
268 void SetGradientStops(T
*brush
, const wxGraphicsGradientStops
& stops
);
272 GraphicsPath
* m_brushPath
;
275 class WXDLLIMPEXP_CORE wxGDIPlusBitmapData
: public wxGraphicsObjectRefData
278 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
);
279 wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, const wxBitmap
&bmp
);
280 ~wxGDIPlusBitmapData ();
282 virtual Bitmap
* GetGDIPlusBitmap() { return m_bitmap
; }
289 class wxGDIPlusFontData
: public wxGraphicsObjectRefData
292 wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
293 const wxGDIPlusContext
* gc
,
295 const wxColour
& col
);
296 ~wxGDIPlusFontData();
298 virtual Brush
* GetGDIPlusBrush() { return m_textBrush
; }
299 virtual Font
* GetGDIPlusFont() { return m_font
; }
305 class wxGDIPlusContext
: public wxGraphicsContext
308 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
309 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
);
310 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
);
311 wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
);
314 virtual ~wxGDIPlusContext();
316 virtual void Clip( const wxRegion
®ion
);
317 // clips drawings to the rect
318 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
320 // resets the clipping to original extent
321 virtual void ResetClip();
323 virtual void * GetNativeContext();
325 virtual void StrokePath( const wxGraphicsPath
& p
);
326 virtual void FillPath( const wxGraphicsPath
& p
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
328 // stroke lines connecting each of the points
329 virtual void StrokeLines( size_t n
, const wxPoint2DDouble
*points
);
332 virtual void DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode fillStyle
= wxODDEVEN_RULE
);
334 virtual bool SetAntialiasMode(wxAntialiasMode antialias
);
336 virtual bool SetCompositionMode(wxCompositionMode op
);
338 virtual void BeginLayer(wxDouble opacity
);
340 virtual void EndLayer();
342 virtual void Translate( wxDouble dx
, wxDouble dy
);
343 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
344 virtual void Rotate( wxDouble angle
);
346 // concatenates this transform with the current transform of this context
347 virtual void ConcatTransform( const wxGraphicsMatrix
& matrix
);
349 // sets the transform of this context
350 virtual void SetTransform( const wxGraphicsMatrix
& matrix
);
352 // gets the matrix of this context
353 virtual wxGraphicsMatrix
GetTransform() const;
355 virtual void DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
356 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
357 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
358 virtual void PushState();
359 virtual void PopState();
361 // sets the font of this context
362 virtual wxGraphicsFont
CreateFont( const wxFont
&font
, const wxColour
&col
= *wxBLACK
) const;
364 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
365 wxDouble
*descent
, wxDouble
*externalLeading
) const;
366 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
367 virtual bool ShouldOffset() const;
368 virtual void GetSize( wxDouble
* width
, wxDouble
*height
);
370 Graphics
* GetGraphics() const { return m_context
; }
374 wxDouble m_fontScaleRatio
;
380 virtual void DoDrawText(const wxString
& str
, wxDouble x
, wxDouble y
)
381 { DoDrawFilledText(str
, x
, y
, wxNullGraphicsBrush
); }
382 virtual void DoDrawFilledText(const wxString
& str
, wxDouble x
, wxDouble y
,
383 const wxGraphicsBrush
& backgroundBrush
);
386 wxStack
<GraphicsState
> m_stateStack
;
387 GraphicsState m_state1
;
388 GraphicsState m_state2
;
393 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
396 class wxGDIPlusMeasuringContext
: public wxGDIPlusContext
399 wxGDIPlusMeasuringContext( wxGraphicsRenderer
* renderer
) : wxGDIPlusContext( renderer
, m_hdc
= GetDC(NULL
), 1000, 1000 )
402 wxGDIPlusMeasuringContext()
406 virtual ~wxGDIPlusMeasuringContext()
408 ReleaseDC( NULL
, m_hdc
);
413 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext
)
416 class wxGDIPlusPrintingContext
: public wxGDIPlusContext
419 wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
);
420 virtual ~wxGDIPlusPrintingContext() { }
424 //-----------------------------------------------------------------------------
425 // wxGDIPlusRenderer declaration
426 //-----------------------------------------------------------------------------
428 class wxGDIPlusRenderer
: public wxGraphicsRenderer
437 virtual ~wxGDIPlusRenderer()
447 virtual wxGraphicsContext
* CreateContext( const wxWindowDC
& dc
);
449 virtual wxGraphicsContext
* CreateContext( const wxMemoryDC
& dc
);
451 #if wxUSE_PRINTING_ARCHITECTURE
452 virtual wxGraphicsContext
* CreateContext( const wxPrinterDC
& dc
);
455 #if wxUSE_ENH_METAFILE
456 virtual wxGraphicsContext
* CreateContext( const wxEnhMetaFileDC
& dc
);
459 virtual wxGraphicsContext
* CreateContextFromNativeContext( void * context
);
461 virtual wxGraphicsContext
* CreateContextFromNativeWindow( void * window
);
463 virtual wxGraphicsContext
* CreateContext( wxWindow
* window
);
465 virtual wxGraphicsContext
* CreateMeasuringContext();
469 virtual wxGraphicsPath
CreatePath();
473 virtual wxGraphicsMatrix
CreateMatrix( wxDouble a
=1.0, wxDouble b
=0.0, wxDouble c
=0.0, wxDouble d
=1.0,
474 wxDouble tx
=0.0, wxDouble ty
=0.0);
477 virtual wxGraphicsPen
CreatePen(const wxPen
& pen
) ;
479 virtual wxGraphicsBrush
CreateBrush(const wxBrush
& brush
) ;
481 virtual wxGraphicsBrush
482 CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
483 wxDouble x2
, wxDouble y2
,
484 const wxGraphicsGradientStops
& stops
);
486 virtual wxGraphicsBrush
487 CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
488 wxDouble xc
, wxDouble yc
,
490 const wxGraphicsGradientStops
& stops
);
492 // create a native bitmap representation
493 virtual wxGraphicsBitmap
CreateBitmap( const wxBitmap
&bitmap
);
495 // stub: should not be called directly
496 virtual wxGraphicsFont
CreateFont( const wxFont
& WXUNUSED(font
),
497 const wxColour
& WXUNUSED(col
) )
498 { wxFAIL
; return wxNullGraphicsFont
; }
500 // this is used to really create the font
501 wxGraphicsFont
CreateGDIPlusFont( const wxGDIPlusContext
* gc
,
503 const wxColour
&col
);
505 // create a graphics bitmap from a native bitmap
506 virtual wxGraphicsBitmap
CreateBitmapFromNativeBitmap( void* bitmap
);
508 // create a subimage from a native image representation
509 virtual wxGraphicsBitmap
CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
512 bool EnsureIsLoaded();
515 friend class wxGDIPlusRendererModule
;
519 ULONG_PTR m_gditoken
;
521 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer
)
524 //-----------------------------------------------------------------------------
525 // wxGDIPlusPen implementation
526 //-----------------------------------------------------------------------------
528 wxGDIPlusPenData::~wxGDIPlusPenData()
535 void wxGDIPlusPenData::Init()
542 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer
* renderer
, const wxPen
&pen
)
543 : wxGraphicsObjectRefData(renderer
)
546 m_width
= pen
.GetWidth();
550 m_pen
= new Pen(wxColourToColor(pen
.GetColour()), m_width
);
553 switch ( pen
.GetCap() )
559 case wxCAP_PROJECTING
:
564 cap
= LineCapFlat
; // TODO verify
571 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
574 switch ( pen
.GetJoin() )
577 join
= LineJoinBevel
;
581 join
= LineJoinMiter
;
585 join
= LineJoinRound
;
589 join
= LineJoinMiter
;
593 m_pen
->SetLineJoin(join
);
595 m_pen
->SetDashStyle(DashStyleSolid
);
597 DashStyle dashStyle
= DashStyleSolid
;
598 switch ( pen
.GetStyle() )
604 dashStyle
= DashStyleDot
;
608 dashStyle
= DashStyleDash
; // TODO verify
612 dashStyle
= DashStyleDash
;
616 dashStyle
= DashStyleDashDot
;
620 dashStyle
= DashStyleCustom
;
622 int count
= pen
.GetDashes( &dashes
);
623 if ((dashes
!= NULL
) && (count
> 0))
625 REAL
*userLengths
= new REAL
[count
];
626 for ( int i
= 0; i
< count
; ++i
)
628 userLengths
[i
] = dashes
[i
];
630 m_pen
->SetDashPattern( userLengths
, count
);
631 delete[] userLengths
;
637 wxBitmap
* bmp
= pen
.GetStipple();
638 if ( bmp
&& bmp
->Ok() )
640 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
642 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
647 m_penBrush
= new TextureBrush(m_penImage
);
648 m_pen
->SetBrush( m_penBrush
);
654 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
656 HatchStyle style
= HatchStyleHorizontal
;
657 switch( pen
.GetStyle() )
659 case wxBDIAGONAL_HATCH
:
660 style
= HatchStyleBackwardDiagonal
;
662 case wxCROSSDIAG_HATCH
:
663 style
= HatchStyleDiagonalCross
;
665 case wxFDIAGONAL_HATCH
:
666 style
= HatchStyleForwardDiagonal
;
669 style
= HatchStyleCross
;
671 case wxHORIZONTAL_HATCH
:
672 style
= HatchStyleHorizontal
;
674 case wxVERTICAL_HATCH
:
675 style
= HatchStyleVertical
;
679 m_penBrush
= new HatchBrush
682 wxColourToColor(pen
.GetColour()),
685 m_pen
->SetBrush( m_penBrush
);
689 if ( dashStyle
!= DashStyleSolid
)
690 m_pen
->SetDashStyle(dashStyle
);
693 //-----------------------------------------------------------------------------
694 // wxGDIPlusBrush implementation
695 //-----------------------------------------------------------------------------
697 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
)
698 : wxGraphicsObjectRefData(renderer
)
703 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer
* renderer
, const wxBrush
&brush
)
704 : wxGraphicsObjectRefData(renderer
)
707 if ( brush
.GetStyle() == wxSOLID
)
709 m_brush
= new SolidBrush(wxColourToColor( brush
.GetColour()));
711 else if ( brush
.IsHatch() )
713 HatchStyle style
= HatchStyleHorizontal
;
714 switch( brush
.GetStyle() )
716 case wxBDIAGONAL_HATCH
:
717 style
= HatchStyleBackwardDiagonal
;
719 case wxCROSSDIAG_HATCH
:
720 style
= HatchStyleDiagonalCross
;
722 case wxFDIAGONAL_HATCH
:
723 style
= HatchStyleForwardDiagonal
;
726 style
= HatchStyleCross
;
728 case wxHORIZONTAL_HATCH
:
729 style
= HatchStyleHorizontal
;
731 case wxVERTICAL_HATCH
:
732 style
= HatchStyleVertical
;
736 m_brush
= new HatchBrush
739 wxColourToColor(brush
.GetColour()),
745 wxBitmap
* bmp
= brush
.GetStipple();
746 if ( bmp
&& bmp
->Ok() )
748 wxDELETE( m_brushImage
);
749 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),
751 (HPALETTE
)bmp
->GetPalette()->GetHPALETTE()
756 m_brush
= new TextureBrush(m_brushImage
);
761 wxGDIPlusBrushData::~wxGDIPlusBrushData()
768 void wxGDIPlusBrushData::Init()
775 template <typename T
>
777 wxGDIPlusBrushData::SetGradientStops(T
*brush
,
778 const wxGraphicsGradientStops
& stops
)
780 const unsigned numStops
= stops
.GetCount();
783 // initial and final colours are set during the brush creation, nothing
788 wxVector
<Color
> colors(numStops
);
789 wxVector
<REAL
> positions(numStops
);
791 for ( unsigned i
= 0; i
< numStops
; i
++ )
793 wxGraphicsGradientStop stop
= stops
.Item(i
);
795 colors
[i
] = wxColourToColor(stop
.GetColour());
796 positions
[i
] = stop
.GetPosition();
799 brush
->SetInterpolationColors(&colors
[0], &positions
[0], numStops
);
803 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
804 wxDouble x2
, wxDouble y2
,
805 const wxGraphicsGradientStops
& stops
)
807 LinearGradientBrush
* const
808 brush
= new LinearGradientBrush(PointF(x1
, y1
) , PointF(x2
, y2
),
809 wxColourToColor(stops
.GetStartColour()),
810 wxColourToColor(stops
.GetEndColour()));
813 SetGradientStops(brush
, stops
);
817 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
818 wxDouble xc
, wxDouble yc
,
820 const wxGraphicsGradientStops
& stops
)
822 m_brushPath
= new GraphicsPath();
823 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
),
824 (REAL
)(2*radius
), (REAL
)(2*radius
));
826 PathGradientBrush
* const brush
= new PathGradientBrush(m_brushPath
);
828 brush
->SetCenterPoint(PointF(xo
, yo
));
829 brush
->SetCenterColor(wxColourToColor(stops
.GetStartColour()));
831 const Color
col(wxColourToColor(stops
.GetEndColour()));
833 brush
->SetSurroundColors(&col
, &count
);
835 SetGradientStops(brush
, stops
);
838 //-----------------------------------------------------------------------------
839 // wxGDIPlusFont implementation
840 //-----------------------------------------------------------------------------
842 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer
* renderer
,
843 const wxGDIPlusContext
* gc
,
845 const wxColour
& col
)
846 : wxGraphicsObjectRefData( renderer
)
848 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
849 int style
= FontStyleRegular
;
850 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
851 style
|= FontStyleItalic
;
852 if ( font
.GetUnderlined() )
853 style
|= FontStyleUnderline
;
854 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
855 style
|= FontStyleBold
;
857 Graphics
* context
= gc
->GetGraphics();
859 Unit fontUnit
= context
->GetPageUnit();
860 // if fontUnit is UnitDisplay, then specify UnitPixel, otherwise
861 // you'll get a "InvalidParameter" from GDI+
862 if ( fontUnit
== UnitDisplay
)
863 fontUnit
= UnitPixel
;
865 REAL points
= font
.GetPointSize();
867 // This scaling is needed when we use unit other than the
868 // default UnitPoint. It works for both display and printing.
869 REAL size
= points
* (100.0 / 72.0);
871 // NB: font unit should match context's unit. We can use UnitPixel,
872 // as that is what the print context should use.
873 m_font
= new Font( s
, size
, style
, fontUnit
);
875 m_textBrush
= new SolidBrush(wxColourToColor(col
));
878 wxGDIPlusFontData::~wxGDIPlusFontData()
884 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
885 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
886 // bytes as parameter, since there is no real copying of the data going in, only references are stored
887 // m_helper has to be kept alive as well
889 //-----------------------------------------------------------------------------
890 // wxGDIPlusBitmapData implementation
891 //-----------------------------------------------------------------------------
893 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
, Bitmap
* bitmap
) :
894 wxGraphicsObjectRefData( renderer
), m_bitmap( bitmap
)
899 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer
* renderer
,
900 const wxBitmap
&bmp
) : wxGraphicsObjectRefData( renderer
)
905 Bitmap
* image
= NULL
;
908 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),
910 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
916 size_t width
= interim
.GetWidth();
917 size_t height
= interim
.GetHeight();
918 Rect
bounds(0,0,width
,height
);
920 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
922 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
923 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
925 BitmapData dataMask
;
926 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
927 interimMask
.GetPixelFormat(),&dataMask
);
930 BitmapData imageData
;
931 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
933 BYTE maskPattern
= 0 ;
937 for ( size_t y
= 0 ; y
< height
; ++y
)
940 for( size_t x
= 0 ; x
< width
; ++x
)
945 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
949 maskPattern
= maskPattern
>> 1;
951 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
952 if ( (maskByte
& maskPattern
) == 0 )
957 interim
.GetPixel(x
,y
,&c
) ;
958 *dest
= (c
.GetValue() | Color::AlphaMask
);
963 image
->UnlockBits(&imageData
);
965 interimMask
.UnlockBits(&dataMask
);
966 interim
.UnlockBits(&dataMask
);
970 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),
972 (HPALETTE
)bmp
.GetPalette()->GetHPALETTE()
977 if ( bmp
.HasAlpha() && GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
979 size_t width
= image
->GetWidth();
980 size_t height
= image
->GetHeight();
981 Rect
bounds(0,0,width
,height
);
982 static BitmapData data
;
986 m_helper
->LockBits(&bounds
, ImageLockModeRead
,
987 m_helper
->GetPixelFormat(),&data
);
989 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
990 PixelFormat32bppPARGB
, (BYTE
*) data
.Scan0
);
992 m_helper
->UnlockBits(&data
);
999 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1005 //-----------------------------------------------------------------------------
1006 // wxGDIPlusPath implementation
1007 //-----------------------------------------------------------------------------
1009 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer
* renderer
, GraphicsPath
* path
) : wxGraphicsPathData(renderer
)
1014 m_path
= new GraphicsPath();
1017 wxGDIPlusPathData::~wxGDIPlusPathData()
1022 wxGraphicsObjectRefData
* wxGDIPlusPathData::Clone() const
1024 return new wxGDIPlusPathData( GetRenderer() , m_path
->Clone());
1031 void wxGDIPlusPathData::MoveToPoint( wxDouble x
, wxDouble y
)
1033 m_path
->StartFigure();
1034 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1037 void wxGDIPlusPathData::AddLineToPoint( wxDouble x
, wxDouble y
)
1039 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
1042 void wxGDIPlusPathData::CloseSubpath()
1044 m_path
->CloseFigure();
1047 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
1053 m_path
->GetLastPoint(&start
);
1054 m_path
->AddBezier(start
,c1
,c2
,end
);
1057 // gets the last point of the current path, (0,0) if not yet set
1058 void wxGDIPlusPathData::GetCurrentPoint( wxDouble
* x
, wxDouble
* y
) const
1061 m_path
->GetLastPoint(&start
);
1066 void wxGDIPlusPathData::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
1068 double sweepAngle
= endAngle
- startAngle
;
1069 if( fabs(sweepAngle
) >= 2*M_PI
)
1071 sweepAngle
= 2 * M_PI
;
1077 if( sweepAngle
< 0 )
1078 sweepAngle
+= 2 * M_PI
;
1082 if( sweepAngle
> 0 )
1083 sweepAngle
-= 2 * M_PI
;
1087 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
1090 void wxGDIPlusPathData::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1092 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
1095 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData
* path
)
1097 m_path
->AddPath( (GraphicsPath
*) path
->GetNativePath(), FALSE
);
1101 // transforms each point of this path by the matrix
1102 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData
* matrix
)
1104 m_path
->Transform( (Matrix
*) matrix
->GetNativeMatrix() );
1107 // gets the bounding box enclosing all points (possibly including control points)
1108 void wxGDIPlusPathData::GetBox(wxDouble
*x
, wxDouble
*y
, wxDouble
*w
, wxDouble
*h
) const
1111 m_path
->GetBounds( &bounds
, NULL
, NULL
) ;
1118 bool wxGDIPlusPathData::Contains( wxDouble x
, wxDouble y
, wxPolygonFillMode fillStyle
) const
1120 m_path
->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1121 return m_path
->IsVisible( (FLOAT
) x
,(FLOAT
) y
) == TRUE
;
1124 //-----------------------------------------------------------------------------
1125 // wxGDIPlusMatrixData implementation
1126 //-----------------------------------------------------------------------------
1128 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer
* renderer
, Matrix
* matrix
)
1129 : wxGraphicsMatrixData(renderer
)
1134 m_matrix
= new Matrix();
1137 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1142 wxGraphicsObjectRefData
*wxGDIPlusMatrixData::Clone() const
1144 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix
->Clone());
1147 // concatenates the matrix
1148 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData
*t
)
1150 m_matrix
->Multiply( (Matrix
*) t
->GetNativeMatrix());
1153 // sets the matrix to the respective values
1154 void wxGDIPlusMatrixData::Set(wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1155 wxDouble tx
, wxDouble ty
)
1157 m_matrix
->SetElements(a
,b
,c
,d
,tx
,ty
);
1160 // gets the component valuess of the matrix
1161 void wxGDIPlusMatrixData::Get(wxDouble
* a
, wxDouble
* b
, wxDouble
* c
,
1162 wxDouble
* d
, wxDouble
* tx
, wxDouble
* ty
) const
1165 m_matrix
->GetElements(elements
);
1166 if (a
) *a
= elements
[0];
1167 if (b
) *b
= elements
[1];
1168 if (c
) *c
= elements
[2];
1169 if (d
) *d
= elements
[3];
1170 if (tx
) *tx
= elements
[4];
1171 if (ty
) *ty
= elements
[5];
1174 // makes this the inverse matrix
1175 void wxGDIPlusMatrixData::Invert()
1180 // returns true if the elements of the transformation matrix are equal ?
1181 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData
* t
) const
1183 return m_matrix
->Equals((Matrix
*) t
->GetNativeMatrix())== TRUE
;
1186 // return true if this is the identity matrix
1187 bool wxGDIPlusMatrixData::IsIdentity() const
1189 return m_matrix
->IsIdentity() == TRUE
;
1196 // add the translation to this matrix
1197 void wxGDIPlusMatrixData::Translate( wxDouble dx
, wxDouble dy
)
1199 m_matrix
->Translate(dx
,dy
);
1202 // add the scale to this matrix
1203 void wxGDIPlusMatrixData::Scale( wxDouble xScale
, wxDouble yScale
)
1205 m_matrix
->Scale(xScale
,yScale
);
1208 // add the rotation to this matrix (radians)
1209 void wxGDIPlusMatrixData::Rotate( wxDouble angle
)
1211 m_matrix
->Rotate( RadToDeg(angle
) );
1215 // apply the transforms
1218 // applies that matrix to the point
1219 void wxGDIPlusMatrixData::TransformPoint( wxDouble
*x
, wxDouble
*y
) const
1222 m_matrix
->TransformPoints(&pt
);
1227 // applies the matrix except for translations
1228 void wxGDIPlusMatrixData::TransformDistance( wxDouble
*dx
, wxDouble
*dy
) const
1231 m_matrix
->TransformVectors(&pt
);
1236 // returns the native representation
1237 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1242 //-----------------------------------------------------------------------------
1243 // wxGDIPlusContext implementation
1244 //-----------------------------------------------------------------------------
1246 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
1247 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext
,wxGDIPlusContext
)
1249 class wxGDIPlusOffsetHelper
1252 wxGDIPlusOffsetHelper( Graphics
* gr
, bool offset
)
1257 m_gr
->TranslateTransform( 0.5, 0.5 );
1259 ~wxGDIPlusOffsetHelper( )
1262 m_gr
->TranslateTransform( -0.5, -0.5 );
1269 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HDC hdc
, wxDouble width
, wxDouble height
)
1270 : wxGraphicsContext(renderer
)
1273 m_context
= new Graphics( hdc
);
1279 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, const wxDC
& dc
)
1280 : wxGraphicsContext(renderer
)
1284 wxMSWDCImpl
*msw
= wxDynamicCast( dc
.GetImpl() , wxMSWDCImpl
);
1285 HDC hdc
= (HDC
) msw
->GetHDC();
1287 m_context
= new Graphics(hdc
);
1288 wxSize sz
= dc
.GetSize();
1295 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, HWND hwnd
)
1296 : wxGraphicsContext(renderer
)
1299 m_context
= new Graphics( hwnd
);
1300 RECT rect
= wxGetWindowRect(hwnd
);
1301 m_width
= rect
.right
- rect
.left
;
1302 m_height
= rect
.bottom
- rect
.top
;
1306 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer
* renderer
, Graphics
* gr
)
1307 : wxGraphicsContext(renderer
)
1314 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL
)
1319 void wxGDIPlusContext::Init()
1326 m_fontScaleRatio
= 1.0;
1329 void wxGDIPlusContext::SetDefaults()
1331 m_context
->SetTextRenderingHint(TextRenderingHintSystemDefault
);
1332 m_context
->SetPixelOffsetMode(PixelOffsetModeHalf
);
1333 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
1334 m_state1
= m_context
->Save();
1335 m_state2
= m_context
->Save();
1338 wxGDIPlusContext::~wxGDIPlusContext()
1342 m_context
->Restore( m_state2
);
1343 m_context
->Restore( m_state1
);
1349 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
1351 Region
rgn((HRGN
)region
.GetHRGN());
1352 m_context
->SetClip(&rgn
,CombineModeIntersect
);
1355 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1357 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
1360 void wxGDIPlusContext::ResetClip()
1362 m_context
->ResetClip();
1365 void wxGDIPlusContext::StrokeLines( size_t n
, const wxPoint2DDouble
*points
)
1367 if (m_composition
== wxCOMPOSITION_DEST
)
1370 if ( !m_pen
.IsNull() )
1372 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1373 Point
*cpoints
= new Point
[n
];
1374 for (size_t i
= 0; i
< n
; i
++)
1376 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1377 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1379 } // for (size_t i = 0; i < n; i++)
1380 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1385 void wxGDIPlusContext::DrawLines( size_t n
, const wxPoint2DDouble
*points
, wxPolygonFillMode
WXUNUSED(fillStyle
) )
1387 if (m_composition
== wxCOMPOSITION_DEST
)
1390 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1391 Point
*cpoints
= new Point
[n
];
1392 for (size_t i
= 0; i
< n
; i
++)
1394 cpoints
[i
].X
= (int)(points
[i
].m_x
);
1395 cpoints
[i
].Y
= (int)(points
[i
].m_y
);
1397 } // for (int i = 0; i < n; i++)
1398 if ( !m_brush
.IsNull() )
1399 m_context
->FillPolygon( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() , cpoints
, n
) ;
1400 if ( !m_pen
.IsNull() )
1401 m_context
->DrawLines( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , cpoints
, n
) ;
1405 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
& path
)
1407 if (m_composition
== wxCOMPOSITION_DEST
)
1410 if ( !m_pen
.IsNull() )
1412 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1413 m_context
->DrawPath( ((wxGDIPlusPenData
*)m_pen
.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath
*) path
.GetNativePath() );
1417 void wxGDIPlusContext::FillPath( const wxGraphicsPath
& path
, wxPolygonFillMode fillStyle
)
1419 if (m_composition
== wxCOMPOSITION_DEST
)
1422 if ( !m_brush
.IsNull() )
1424 wxGDIPlusOffsetHelper
helper( m_context
, ShouldOffset() );
1425 ((GraphicsPath
*) path
.GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
1426 m_context
->FillPath( ((wxGDIPlusBrushData
*)m_brush
.GetRefData())->GetGDIPlusBrush() ,
1427 (GraphicsPath
*) path
.GetNativePath());
1431 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias
)
1433 if (m_antialias
== antialias
)
1436 m_antialias
= antialias
;
1438 SmoothingMode antialiasMode
;
1441 case wxANTIALIAS_DEFAULT
:
1442 antialiasMode
= SmoothingModeHighQuality
;
1444 case wxANTIALIAS_NONE
:
1445 antialiasMode
= SmoothingModeNone
;
1450 m_context
->SetSmoothingMode(antialiasMode
);
1454 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op
)
1456 if ( m_composition
== op
)
1461 if (m_composition
== wxCOMPOSITION_DEST
)
1464 CompositingMode cop
;
1467 case wxCOMPOSITION_SOURCE
:
1468 cop
= CompositingModeSourceCopy
;
1470 case wxCOMPOSITION_OVER
:
1471 cop
= CompositingModeSourceOver
;
1477 m_context
->SetCompositingMode(cop
);
1481 void wxGDIPlusContext::BeginLayer(wxDouble
/* opacity */)
1486 void wxGDIPlusContext::EndLayer()
1491 void wxGDIPlusContext::Rotate( wxDouble angle
)
1493 m_context
->RotateTransform( RadToDeg(angle
) );
1496 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
1498 m_context
->TranslateTransform( dx
, dy
);
1501 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
1503 m_context
->ScaleTransform(xScale
,yScale
);
1506 void wxGDIPlusContext::PushState()
1508 GraphicsState state
= m_context
->Save();
1509 m_stateStack
.push(state
);
1512 void wxGDIPlusContext::PopState()
1514 GraphicsState state
= m_stateStack
.top();
1516 m_context
->Restore(state
);
1519 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1521 if (m_composition
== wxCOMPOSITION_DEST
)
1524 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bmp
.GetRefData())->GetGDIPlusBitmap();
1527 if( image
->GetWidth() != (UINT
) w
|| image
->GetHeight() != (UINT
) h
)
1529 Rect
drawRect((REAL
) x
, (REAL
)y
, (REAL
)w
, (REAL
)h
);
1530 m_context
->SetPixelOffsetMode( PixelOffsetModeNone
);
1531 m_context
->DrawImage(image
, drawRect
, 0 , 0 , image
->GetWidth()-1, image
->GetHeight()-1, UnitPixel
) ;
1532 m_context
->SetPixelOffsetMode( PixelOffsetModeHalf
);
1535 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1539 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1541 wxGraphicsBitmap bitmap
= GetRenderer()->CreateBitmap(bmp
);
1542 DrawBitmap(bitmap
, x
, y
, w
, h
);
1545 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
1547 if (m_composition
== wxCOMPOSITION_DEST
)
1550 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1551 // find out by looking at the bitmap data whether there really was alpha in it
1552 HICON hIcon
= (HICON
)icon
.GetHICON();
1554 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1555 if (!GetIconInfo(hIcon
,&iconInfo
))
1558 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
1560 Bitmap
* image
= NULL
;
1562 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1563 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1564 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
1566 image
= Bitmap::FromHICON(hIcon
);
1570 size_t width
= interim
.GetWidth();
1571 size_t height
= interim
.GetHeight();
1572 Rect
bounds(0,0,width
,height
);
1575 interim
.LockBits(&bounds
, ImageLockModeRead
,
1576 interim
.GetPixelFormat(),&data
);
1578 bool hasAlpha
= false;
1579 for ( size_t y
= 0 ; y
< height
&& !hasAlpha
; ++y
)
1581 for( size_t x
= 0 ; x
< width
&& !hasAlpha
; ++x
)
1583 ARGB
*dest
= (ARGB
*)((BYTE
*)data
.Scan0
+ data
.Stride
*y
+ x
*4);
1584 if ( ( *dest
& Color::AlphaMask
) != 0 )
1591 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
1592 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
1596 image
= Bitmap::FromHICON(hIcon
);
1599 interim
.UnlockBits(&data
);
1602 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
1605 DeleteObject(iconInfo
.hbmColor
);
1606 DeleteObject(iconInfo
.hbmMask
);
1609 wxGraphicsFont
wxGDIPlusContext::CreateFont( const wxFont
&font
,
1610 const wxColour
&col
) const
1612 wxGDIPlusRenderer
* renderer
=
1613 static_cast<wxGDIPlusRenderer
*>(GetRenderer());
1614 return renderer
->CreateGDIPlusFont(this, font
, col
);
1617 void wxGDIPlusContext::DoDrawFilledText(const wxString
& str
,
1618 wxDouble x
, wxDouble y
,
1619 const wxGraphicsBrush
& brush
)
1621 if (m_composition
== wxCOMPOSITION_DEST
)
1624 wxCHECK_RET( !m_font
.IsNull(),
1625 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1630 wxGDIPlusFontData
* const
1631 fontData
= (wxGDIPlusFontData
*)m_font
.GetRefData();
1632 wxGDIPlusBrushData
* const
1633 brushData
= (wxGDIPlusBrushData
*)brush
.GetRefData();
1635 m_context
->DrawString
1637 str
.wc_str(*wxConvUI
), // string to draw, always Unicode
1638 -1, // length: string is NUL-terminated
1639 fontData
->GetGDIPlusFont(),
1641 StringFormat::GenericTypographic(),
1642 brushData
? brushData
->GetGDIPlusBrush()
1643 : fontData
->GetGDIPlusBrush()
1647 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
1648 wxDouble
*descent
, wxDouble
*externalLeading
) const
1650 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1652 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
1653 FontFamily ffamily
;
1654 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1656 f
->GetFamily(&ffamily
) ;
1658 REAL factorY
= m_fontScaleRatio
;
1660 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
1661 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1662 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
1663 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1664 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
1665 f
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
1668 *height
= rHeight
* factorY
;
1670 *descent
= rDescent
* factorY
;
1671 if ( externalLeading
)
1672 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
;
1673 // measuring empty strings is not guaranteed, so do it by hand
1681 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1682 StringFormat
strFormat( StringFormat::GenericTypographic() );
1683 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1686 m_context
->MeasureString((const wchar_t *) s
, wcslen(s
) , f
, layoutRect
, &strFormat
, &bounds
) ;
1688 *width
= bounds
.Width
;
1690 *height
= bounds
.Height
;
1694 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
1697 widths
.Add(0, text
.length());
1699 wxCHECK_RET( !m_font
.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1704 Font
* f
= ((wxGDIPlusFontData
*)m_font
.GetRefData())->GetGDIPlusFont();
1705 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
1706 size_t len
= wcslen( ws
) ;
1707 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1709 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
1710 StringFormat
strFormat( StringFormat::GenericTypographic() );
1712 size_t startPosition
= 0;
1713 size_t remainder
= len
;
1714 const size_t maxSpan
= 32;
1715 CharacterRange
* ranges
= new CharacterRange
[maxSpan
] ;
1716 Region
* regions
= new Region
[maxSpan
];
1718 while( remainder
> 0 )
1720 size_t span
= wxMin( maxSpan
, remainder
);
1722 for( size_t i
= 0 ; i
< span
; ++i
)
1724 ranges
[i
].First
= 0 ;
1725 ranges
[i
].Length
= startPosition
+i
+1 ;
1727 strFormat
.SetMeasurableCharacterRanges(span
,ranges
);
1728 strFormat
.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces
| strFormat
.GetFormatFlags() );
1729 m_context
->MeasureCharacterRanges(ws
, -1 , f
,layoutRect
, &strFormat
,span
,regions
) ;
1732 for ( size_t i
= 0 ; i
< span
; ++i
)
1734 regions
[i
].GetBounds(&bbox
,m_context
);
1735 widths
[startPosition
+i
] = bbox
.Width
;
1738 startPosition
+= span
;
1745 bool wxGDIPlusContext::ShouldOffset() const
1748 if ( !m_pen
.IsNull() )
1750 penwidth
= (int)((wxGDIPlusPenData
*)m_pen
.GetRefData())->GetWidth();
1751 if ( penwidth
== 0 )
1754 return ( penwidth
% 2 ) == 1;
1757 void* wxGDIPlusContext::GetNativeContext()
1762 // concatenates this transform with the current transform of this context
1763 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix
& matrix
)
1765 m_context
->MultiplyTransform((Matrix
*) matrix
.GetNativeMatrix());
1768 // sets the transform of this context
1769 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix
& matrix
)
1771 m_context
->SetTransform((Matrix
*) matrix
.GetNativeMatrix());
1774 // gets the matrix of this context
1775 wxGraphicsMatrix
wxGDIPlusContext::GetTransform() const
1777 wxGraphicsMatrix matrix
= CreateMatrix();
1778 m_context
->GetTransform((Matrix
*) matrix
.GetNativeMatrix());
1782 void wxGDIPlusContext::GetSize( wxDouble
* width
, wxDouble
*height
)
1788 //-----------------------------------------------------------------------------
1789 // wxGDIPlusPrintingContext implementation
1790 //-----------------------------------------------------------------------------
1792 wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer
* renderer
,
1794 : wxGDIPlusContext(renderer
, dc
)
1796 Graphics
* context
= GetGraphics();
1798 //m_context->SetPageUnit(UnitDocument);
1800 // Setup page scale, based on DPI ratio.
1801 // Antecedent should be 100dpi when the default page unit
1802 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1803 // instead. Note that calling SetPageScale() does not have effect on
1804 // non-printing DCs (that is, any other than wxPrinterDC or
1805 // wxEnhMetaFileDC).
1806 REAL dpiRatio
= 100.0 / context
->GetDpiY();
1807 context
->SetPageScale(dpiRatio
);
1809 // We use this modifier when measuring fonts. It is needed because the
1810 // page scale is modified above.
1811 m_fontScaleRatio
= context
->GetDpiY() / 72.0;
1814 //-----------------------------------------------------------------------------
1815 // wxGDIPlusRenderer implementation
1816 //-----------------------------------------------------------------------------
1818 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer
,wxGraphicsRenderer
)
1820 static wxGDIPlusRenderer gs_GDIPlusRenderer
;
1822 wxGraphicsRenderer
* wxGraphicsRenderer::GetDefaultRenderer()
1824 return &gs_GDIPlusRenderer
;
1827 bool wxGDIPlusRenderer::EnsureIsLoaded()
1829 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1830 // if we already tried and failed (we don't want to spend lot of time
1831 // returning NULL from wxGraphicsContext::Create(), which may be called
1832 // relatively frequently):
1833 if ( m_loaded
== -1 )
1838 return m_loaded
== 1;
1841 // call EnsureIsLoaded() and return returnOnFail value if it fails
1842 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1843 if ( !EnsureIsLoaded() ) \
1844 return (returnOnFail)
1847 void wxGDIPlusRenderer::Load()
1849 GdiplusStartupInput input
;
1850 GdiplusStartupOutput output
;
1851 if ( GdiplusStartup(&m_gditoken
,&input
,&output
) == Gdiplus::Ok
)
1853 wxLogTrace("gdiplus", "successfully initialized GDI+");
1858 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1863 void wxGDIPlusRenderer::Unload()
1867 GdiplusShutdown(m_gditoken
);
1870 m_loaded
= -1; // next Load() will try again
1873 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxWindowDC
& dc
)
1875 ENSURE_LOADED_OR_RETURN(NULL
);
1876 return new wxGDIPlusContext(this, dc
);
1879 #if wxUSE_PRINTING_ARCHITECTURE
1880 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxPrinterDC
& dc
)
1882 ENSURE_LOADED_OR_RETURN(NULL
);
1883 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1888 #if wxUSE_ENH_METAFILE
1889 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC
& dc
)
1891 ENSURE_LOADED_OR_RETURN(NULL
);
1892 wxGDIPlusContext
* context
= new wxGDIPlusPrintingContext(this, dc
);
1897 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( const wxMemoryDC
& dc
)
1899 ENSURE_LOADED_OR_RETURN(NULL
);
1900 return new wxGDIPlusContext(this, dc
);
1903 wxGraphicsContext
* wxGDIPlusRenderer::CreateMeasuringContext()
1905 ENSURE_LOADED_OR_RETURN(NULL
);
1906 return new wxGDIPlusMeasuringContext(this);
1909 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeContext( void * context
)
1911 ENSURE_LOADED_OR_RETURN(NULL
);
1912 return new wxGDIPlusContext(this,(Graphics
*) context
);
1916 wxGraphicsContext
* wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window
)
1918 ENSURE_LOADED_OR_RETURN(NULL
);
1919 return new wxGDIPlusContext(this,(HWND
) window
);
1922 wxGraphicsContext
* wxGDIPlusRenderer::CreateContext( wxWindow
* window
)
1924 ENSURE_LOADED_OR_RETURN(NULL
);
1925 return new wxGDIPlusContext(this, (HWND
) window
->GetHWND() );
1930 wxGraphicsPath
wxGDIPlusRenderer::CreatePath()
1932 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath
);
1934 m
.SetRefData( new wxGDIPlusPathData(this));
1941 wxGraphicsMatrix
wxGDIPlusRenderer::CreateMatrix( wxDouble a
, wxDouble b
, wxDouble c
, wxDouble d
,
1942 wxDouble tx
, wxDouble ty
)
1945 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix
);
1947 wxGDIPlusMatrixData
* data
= new wxGDIPlusMatrixData( this );
1948 data
->Set( a
,b
,c
,d
,tx
,ty
) ;
1953 wxGraphicsPen
wxGDIPlusRenderer::CreatePen(const wxPen
& pen
)
1955 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen
);
1956 if ( !pen
.Ok() || pen
.GetStyle() == wxTRANSPARENT
)
1957 return wxNullGraphicsPen
;
1961 p
.SetRefData(new wxGDIPlusPenData( this, pen
));
1966 wxGraphicsBrush
wxGDIPlusRenderer::CreateBrush(const wxBrush
& brush
)
1968 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1969 if ( !brush
.Ok() || brush
.GetStyle() == wxTRANSPARENT
)
1970 return wxNullGraphicsBrush
;
1974 p
.SetRefData(new wxGDIPlusBrushData( this, brush
));
1980 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1
, wxDouble y1
,
1981 wxDouble x2
, wxDouble y2
,
1982 const wxGraphicsGradientStops
& stops
)
1984 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
1986 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
1987 d
->CreateLinearGradientBrush(x1
, y1
, x2
, y2
, stops
);
1993 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo
, wxDouble yo
,
1994 wxDouble xc
, wxDouble yc
,
1996 const wxGraphicsGradientStops
& stops
)
1998 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush
);
2000 wxGDIPlusBrushData
* d
= new wxGDIPlusBrushData( this );
2001 d
->CreateRadialGradientBrush(xo
,yo
,xc
,yc
,radius
,stops
);
2007 wxGDIPlusRenderer::CreateGDIPlusFont( const wxGDIPlusContext
* gc
,
2009 const wxColour
&col
)
2011 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont
);
2015 p
.SetRefData(new wxGDIPlusFontData( this, gc
, font
, col
));
2019 return wxNullGraphicsFont
;
2022 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmap( const wxBitmap
&bitmap
)
2024 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2028 p
.SetRefData(new wxGDIPlusBitmapData( this , bitmap
));
2032 return wxNullGraphicsBitmap
;
2035 wxGraphicsBitmap
wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap
)
2037 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2038 if ( bitmap
!= NULL
)
2041 p
.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap
*) bitmap
));
2045 return wxNullGraphicsBitmap
;
2048 wxGraphicsBitmap
wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap
&bitmap
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
2050 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap
);
2051 Bitmap
* image
= static_cast<wxGDIPlusBitmapData
*>(bitmap
.GetRefData())->GetGDIPlusBitmap();
2055 p
.SetRefData(new wxGDIPlusBitmapData( this , image
->Clone( (REAL
) x
, (REAL
) y
, (REAL
) w
, (REAL
) h
, PixelFormat32bppPARGB
) ));
2059 return wxNullGraphicsBitmap
;
2062 // Shutdown GDI+ at app exit, before possible dll unload
2063 class wxGDIPlusRendererModule
: public wxModule
2066 virtual bool OnInit() { return true; }
2067 virtual void OnExit() { gs_GDIPlusRenderer
.Unload(); }
2070 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule
)
2073 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule
, wxModule
)
2075 // ----------------------------------------------------------------------------
2076 // wxMSW-specific parts of wxGCDC
2077 // ----------------------------------------------------------------------------
2079 WXHDC
wxGCDC::AcquireHDC()
2081 wxGraphicsContext
* const gc
= GetGraphicsContext();
2085 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2086 return g
? g
->GetHDC() : NULL
;
2089 void wxGCDC::ReleaseHDC(WXHDC hdc
)
2094 wxGraphicsContext
* const gc
= GetGraphicsContext();
2095 wxCHECK_RET( gc
, "can't release HDC because there is no wxGraphicsContext" );
2097 Graphics
* const g
= static_cast<Graphics
*>(gc
->GetNativeContext());
2098 wxCHECK_RET( g
, "can't release HDC because there is no Graphics" );
2100 g
->ReleaseHDC((HDC
)hdc
);
2103 #endif // wxUSE_GRAPHICS_CONTEXT