1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/graphics.cpp
3 // Purpose: wxGCDC class
4 // Author: Stefan Csomor
8 // Copyright: (c) 2006 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
21 #include "wx/msw/wrapcdlg.h"
23 #include "wx/window.h"
26 #include "wx/dialog.h"
28 #include "wx/bitmap.h"
29 #include "wx/dcmemory.h"
32 #include "wx/dcprint.h"
33 #include "wx/module.h"
36 #include "wx/graphics.h"
38 #if wxUSE_GRAPHICS_CONTEXT
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 const double RAD2DEG
= 180.0 / M_PI
;
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 static inline double dmin(double a
, double b
) { return a
< b
? a
: b
; }
55 static inline double dmax(double a
, double b
) { return a
> b
? a
: b
; }
57 static inline double DegToRad(double deg
) { return (deg
* M_PI
) / 180.0; }
58 static inline double RadToDeg(double deg
) { return (deg
* 180.0) / M_PI
; }
60 //-----------------------------------------------------------------------------
61 // device context implementation
63 // more and more of the dc functionality should be implemented by calling
64 // the appropricate wxGDIPlusContext, but we will have to do that step by step
65 // also coordinate conversions should be moved to native matrix ops
66 //-----------------------------------------------------------------------------
68 // we always stock two context states, one at entry, to be able to preserve the
69 // state we were called with, the other one after changing to HI Graphics orientation
70 // (this one is used for getting back clippings etc)
72 //-----------------------------------------------------------------------------
73 // wxGraphicsPath implementation
74 //-----------------------------------------------------------------------------
76 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
78 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
82 // TODO remove this dependency (gdiplus needs the macros)
85 #define max(a,b) (((a) > (b)) ? (a) : (b))
89 #define min(a,b) (((a) < (b)) ? (a) : (b))
93 using namespace Gdiplus
;
111 void EnsureIsLoaded()
120 GdiplusStartupInput input
;
121 GdiplusStartupOutput output
;
122 GdiplusStartup(&m_gditoken
,&input
,&output
);
128 GdiplusShutdown(m_gditoken
);
136 static GDILoader gGDILoader
;
138 class WXDLLEXPORT wxGDIPlusPath
: public wxGraphicsPath
146 // These are the path primitives from which everything else can be constructed
149 // begins a new subpath at (x,y)
150 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
152 // adds a straight line from the current point to (x,y)
153 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
155 // adds a cubic Bezier curve from the current point, using two control points and an end point
156 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
159 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
160 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
162 // gets the last point of the current path, (0,0) if not yet set
163 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) ;
165 // closes the current sub-path
166 virtual void CloseSubpath();
169 // These are convenience functions which - if not available natively will be assembled
170 // using the primitives from above
173 // appends a rectangle as a new closed subpath
174 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
177 // appends an ellipsis as a new closed subpath fitting the passed rectangle
178 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
180 // 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)
181 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
184 // returns the native path
185 virtual void * GetNativePath() const { return m_path
; }
187 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
188 virtual void UnGetNativePath(void * WXUNUSED(path
)) {}
191 GraphicsPath
* m_path
;
192 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusPath
)
195 class WXDLLEXPORT wxGDIPlusContext
: public wxGraphicsContext
198 wxGDIPlusContext( HDC hdc
);
199 wxGDIPlusContext( HWND hwnd
);
200 wxGDIPlusContext( Graphics
* gr
);
203 virtual ~wxGDIPlusContext();
205 virtual void Clip( const wxRegion
®ion
);
206 // clips drawings to the rect
207 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
209 // resets the clipping to original extent
210 virtual void ResetClip();
212 virtual void * GetNativeContext();
214 virtual void StrokePath( const wxGraphicsPath
*p
);
215 virtual void FillPath( const wxGraphicsPath
*p
, int fillStyle
= wxWINDING_RULE
);
217 virtual wxGraphicsPath
* CreatePath();
218 virtual void SetPen( const wxPen
&pen
);
219 virtual void SetBrush( const wxBrush
&brush
);
220 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
) ;
221 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
222 const wxColour
&oColor
, const wxColour
&cColor
);
224 virtual void Translate( wxDouble dx
, wxDouble dy
);
225 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
226 virtual void Rotate( wxDouble angle
);
228 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
229 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
230 virtual void PushState();
231 virtual void PopState();
233 virtual void SetFont( const wxFont
&font
);
234 virtual void SetTextColor( const wxColour
&col
);
235 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
236 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
237 wxDouble
*descent
, wxDouble
*externalLeading
) const;
238 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
245 vector
<GraphicsState
> m_stateStack
;
246 GraphicsState m_state1
;
247 GraphicsState m_state2
;
250 bool m_penTransparent
;
255 bool m_brushTransparent
;
257 GraphicsPath
* m_brushPath
;
263 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext
)
266 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusPath
,wxGraphicsPath
)
268 wxGDIPlusPath::wxGDIPlusPath()
270 m_path
= new GraphicsPath();
273 wxGDIPlusPath::~wxGDIPlusPath()
282 void wxGDIPlusPath::MoveToPoint( wxDouble x
, wxDouble y
)
284 m_path
->StartFigure();
285 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
288 void wxGDIPlusPath::AddLineToPoint( wxDouble x
, wxDouble y
)
290 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
293 void wxGDIPlusPath::CloseSubpath()
295 m_path
->CloseFigure();
298 void wxGDIPlusPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
304 m_path
->GetLastPoint(&start
);
305 m_path
->AddBezier(start
,c1
,c2
,end
);
308 // gets the last point of the current path, (0,0) if not yet set
309 void wxGDIPlusPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
312 m_path
->GetLastPoint(&start
);
317 void wxGDIPlusPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
319 double sweepAngle
= endAngle
- startAngle
;
320 if( abs(sweepAngle
) >= 2*M_PI
)
322 sweepAngle
= 2 * M_PI
;
329 sweepAngle
+= 2 * M_PI
;
334 sweepAngle
-= 2 * M_PI
;
338 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
341 void wxGDIPlusPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
343 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
346 //-----------------------------------------------------------------------------
347 // wxGDIPlusContext implementation
348 //-----------------------------------------------------------------------------
350 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext
,wxGraphicsContext
)
352 wxGDIPlusContext::wxGDIPlusContext( HDC hdc
)
355 m_context
= new Graphics( hdc
);
359 wxGDIPlusContext::wxGDIPlusContext( HWND hwnd
)
362 m_context
= new Graphics( hwnd
);
366 wxGDIPlusContext::wxGDIPlusContext( Graphics
* gr
)
373 void wxGDIPlusContext::Init()
375 gGDILoader
.EnsureIsLoaded();
381 m_penTransparent
= true;
386 m_brushTransparent
= true;
394 void wxGDIPlusContext::SetDefaults()
396 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
397 m_state1
= m_context
->Save();
398 m_state2
= m_context
->Save();
401 m_penTransparent
= false;
402 m_pen
= new Pen((ARGB
)Color::Black
);
406 m_brushTransparent
= false;
407 m_brush
= new SolidBrush((ARGB
)Color::White
);
410 m_textBrush
= new SolidBrush((ARGB
)Color::Black
);
411 m_font
= new Font( L
"Arial" , 9 , FontStyleRegular
);
414 wxGDIPlusContext::~wxGDIPlusContext()
418 m_context
->Restore( m_state2
);
419 m_context
->Restore( m_state1
);
433 void wxGDIPlusContext::Clip( const wxRegion
®ion
)
435 m_context
->SetClip((HRGN
)region
.GetHRGN(),CombineModeIntersect
);
438 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
440 m_context
->SetClip(RectF(x
,y
,w
,h
),CombineModeIntersect
);
443 void wxGDIPlusContext::ResetClip()
445 m_context
->ResetClip();
448 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
*path
)
450 if ( m_penTransparent
)
453 m_context
->DrawPath( m_pen
, (GraphicsPath
*) path
->GetNativePath() );
456 void wxGDIPlusContext::FillPath( const wxGraphicsPath
*path
, int fillStyle
)
458 if ( !m_brushTransparent
)
460 ((GraphicsPath
*) path
->GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
461 m_context
->FillPath( m_brush
, (GraphicsPath
*) path
->GetNativePath());
465 wxGraphicsPath
* wxGDIPlusContext::CreatePath()
467 return new wxGDIPlusPath();
470 void wxGDIPlusContext::Rotate( wxDouble angle
)
472 m_context
->RotateTransform( RadToDeg(angle
) );
475 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
477 m_context
->TranslateTransform( dx
, dy
);
480 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
482 m_context
->ScaleTransform(xScale
,yScale
);
485 void wxGDIPlusContext::PushState()
487 GraphicsState state
= m_context
->Save();
488 m_stateStack
.push_back(state
);
491 void wxGDIPlusContext::PopState()
493 GraphicsState state
= m_stateStack
.back();
494 m_stateStack
.pop_back();
495 m_context
->Restore(state
);
498 void wxGDIPlusContext::SetTextColor( const wxColour
&col
)
501 m_textBrush
= new SolidBrush( Color( col
.Alpha() , col
.Red() ,
502 col
.Green() , col
.Blue() ));
505 void wxGDIPlusContext::SetPen( const wxPen
&pen
)
507 m_penTransparent
= pen
.GetStyle() == wxTRANSPARENT
;
508 if ( m_penTransparent
)
511 m_pen
->SetColor( Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
512 pen
.GetColour().Green() , pen
.GetColour().Blue() ) );
514 // TODO: * m_dc->m_scaleX
515 double penWidth
= pen
.GetWidth();
519 m_pen
->SetWidth(penWidth
);
522 switch ( pen
.GetCap() )
528 case wxCAP_PROJECTING
:
533 cap
= LineCapFlat
; // TODO verify
540 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
543 switch ( pen
.GetJoin() )
546 join
= LineJoinBevel
;
550 join
= LineJoinMiter
;
554 join
= LineJoinRound
;
558 join
= LineJoinMiter
;
562 m_pen
->SetLineJoin(join
);
564 m_pen
->SetDashStyle(DashStyleSolid
);
566 DashStyle dashStyle
= DashStyleSolid
;
567 switch ( pen
.GetStyle() )
573 dashStyle
= DashStyleDot
;
577 dashStyle
= DashStyleDash
; // TODO verify
581 dashStyle
= DashStyleDash
;
585 dashStyle
= DashStyleDashDot
;
589 dashStyle
= DashStyleCustom
;
591 int count
= pen
.GetDashes( &dashes
);
592 if ((dashes
!= NULL
) && (count
> 0))
594 REAL
*userLengths
= new REAL
[count
];
595 for ( int i
= 0; i
< count
; ++i
)
597 userLengths
[i
] = dashes
[i
];
599 m_pen
->SetDashPattern( userLengths
, count
);
600 delete[] userLengths
;
606 wxBitmap
* bmp
= pen
.GetStipple();
607 if ( bmp
&& bmp
->Ok() )
609 wxDELETE( m_penImage
);
610 wxDELETE( m_penBrush
);
611 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
612 m_penBrush
= new TextureBrush(m_penImage
);
613 m_pen
->SetBrush( m_penBrush
);
619 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
621 wxDELETE( m_penBrush
);
622 HatchStyle style
= HatchStyleHorizontal
;
623 switch( pen
.GetStyle() )
625 case wxBDIAGONAL_HATCH
:
626 style
= HatchStyleBackwardDiagonal
;
628 case wxCROSSDIAG_HATCH
:
629 style
= HatchStyleDiagonalCross
;
631 case wxFDIAGONAL_HATCH
:
632 style
= HatchStyleForwardDiagonal
;
635 style
= HatchStyleCross
;
637 case wxHORIZONTAL_HATCH
:
638 style
= HatchStyleHorizontal
;
640 case wxVERTICAL_HATCH
:
641 style
= HatchStyleVertical
;
645 m_penBrush
= new HatchBrush(style
,Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
646 pen
.GetColour().Green() , pen
.GetColour().Blue() ), Color::Transparent
);
647 m_pen
->SetBrush( m_penBrush
);
651 if ( dashStyle
!= DashStyleSolid
)
652 m_pen
->SetDashStyle(dashStyle
);
655 void wxGDIPlusContext::SetBrush( const wxBrush
&brush
)
658 if ( m_context
== NULL
)
661 m_brushTransparent
= brush
.GetStyle() == wxTRANSPARENT
;
663 if ( m_brushTransparent
)
668 if ( brush
.GetStyle() == wxSOLID
)
670 m_brush
= new SolidBrush( Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
671 brush
.GetColour().Green() , brush
.GetColour().Blue() ) );
673 else if ( brush
.IsHatch() )
675 HatchStyle style
= HatchStyleHorizontal
;
676 switch( brush
.GetStyle() )
678 case wxBDIAGONAL_HATCH
:
679 style
= HatchStyleBackwardDiagonal
;
681 case wxCROSSDIAG_HATCH
:
682 style
= HatchStyleDiagonalCross
;
684 case wxFDIAGONAL_HATCH
:
685 style
= HatchStyleForwardDiagonal
;
688 style
= HatchStyleCross
;
690 case wxHORIZONTAL_HATCH
:
691 style
= HatchStyleHorizontal
;
693 case wxVERTICAL_HATCH
:
694 style
= HatchStyleVertical
;
698 m_brush
= new HatchBrush(style
,Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
699 brush
.GetColour().Green() , brush
.GetColour().Blue() ), Color::Transparent
);
703 wxBitmap
* bmp
= brush
.GetStipple();
704 if ( bmp
&& bmp
->Ok() )
706 wxDELETE( m_brushImage
);
707 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
708 m_brush
= new TextureBrush(m_brushImage
);
713 void wxGDIPlusContext::SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
715 m_brushTransparent
= false ;
719 m_brush
= new LinearGradientBrush( PointF( x1
,y1
) , PointF( x2
,y2
),
720 Color( c1
.Alpha(), c1
.Red(),c1
.Green() , c1
.Blue() ),
721 Color( c2
.Alpha(), c2
.Red(),c2
.Green() , c2
.Blue() ));
724 void wxGDIPlusContext::SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
725 const wxColour
&oColor
, const wxColour
&cColor
)
727 m_brushTransparent
= false ;
730 wxDELETE(m_brushPath
);
732 // Create a path that consists of a single circle.
733 m_brushPath
= new GraphicsPath();
734 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
), (REAL
)(2*radius
), (REAL
)(2*radius
));
736 PathGradientBrush
*b
= new PathGradientBrush(m_brushPath
);
738 b
->SetCenterPoint( PointF(xo
,yo
));
739 b
->SetCenterColor(Color( oColor
.Alpha(), oColor
.Red(),oColor
.Green() , oColor
.Blue() ));
741 Color colors
[] = {Color( cColor
.Alpha(), cColor
.Red(),cColor
.Green() , cColor
.Blue() )};
743 b
->SetSurroundColors(colors
, &count
);
746 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
747 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
748 // bytes as parameter
750 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
752 Bitmap
* image
= NULL
;
753 Bitmap
* helper
= NULL
;
756 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
758 size_t width
= interim
.GetWidth();
759 size_t height
= interim
.GetHeight();
760 Rect
bounds(0,0,width
,height
);
762 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
764 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
765 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
767 BitmapData dataMask
;
768 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
769 interimMask
.GetPixelFormat(),&dataMask
);
772 BitmapData imageData
;
773 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
775 BYTE maskPattern
= 0 ;
779 for ( size_t y
= 0 ; y
< height
; ++y
)
782 for( size_t x
= 0 ; x
< width
; ++x
)
787 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
791 maskPattern
= maskPattern
>> 1;
793 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
794 if ( (maskByte
& maskPattern
) == 0 )
799 interim
.GetPixel(x
,y
,&c
) ;
800 *dest
= (c
.GetValue() | Color::AlphaMask
);
805 image
->UnlockBits(&imageData
);
807 interimMask
.UnlockBits(&dataMask
);
808 interim
.UnlockBits(&dataMask
);
812 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
813 if ( GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
815 size_t width
= image
->GetWidth();
816 size_t height
= image
->GetHeight();
817 Rect
bounds(0,0,width
,height
);
822 helper
->LockBits(&bounds
, ImageLockModeRead
,
823 helper
->GetPixelFormat(),&data
);
825 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
826 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
828 helper
->UnlockBits(&data
);
832 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
837 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
839 HICON hIcon
= (HICON
)icon
.GetHICON();
841 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
842 if (!GetIconInfo(hIcon
,&iconInfo
))
846 GetObject(iconInfo
.hbmColor
,sizeof(BITMAP
),&iconBmpData
);
847 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
849 Bitmap
* image
= NULL
;
851 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
853 image
= Bitmap::FromHICON(hIcon
);
857 size_t width
= interim
.GetWidth();
858 size_t height
= interim
.GetHeight();
859 Rect
bounds(0,0,width
,height
);
862 interim
.LockBits(&bounds
, ImageLockModeRead
,
863 interim
.GetPixelFormat(),&data
);
864 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
865 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
866 interim
.UnlockBits(&data
);
869 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
872 DeleteObject(iconInfo
.hbmColor
);
873 DeleteObject(iconInfo
.hbmMask
);
877 void wxGDIPlusContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
882 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
883 m_context
->DrawString( s
, -1 , m_font
, PointF( x
, y
) , m_textBrush
);
884 // TODO m_backgroundMode == wxSOLID
887 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
888 wxDouble
*descent
, wxDouble
*externalLeading
) const
890 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
893 m_font
->GetFamily(&ffamily
) ;
895 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
897 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
898 m_font
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
899 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
900 m_font
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
901 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
902 m_font
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
905 *height
= rHeight
* factorY
+ 0.5 ;
907 *descent
= rDescent
* factorY
+ 0.5 ;
908 if ( externalLeading
)
909 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
+ 0.5 ;
910 // measuring empty strings is not guaranteed, so do it by hand
918 // MeasureString does return a rectangle that is way too large, so it is
920 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
921 StringFormat strFormat
;
922 CharacterRange
strRange(0,wcslen(s
));
923 strFormat
.SetMeasurableCharacterRanges(1,&strRange
);
925 m_context
->MeasureCharacterRanges(s
, -1 , m_font
,layoutRect
, &strFormat
,1,®ion
) ;
927 region
.GetBounds(&bbox
,m_context
);
929 *width
= bbox
.GetRight()-bbox
.GetLeft()+0.5;
933 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
936 widths
.Add(0, text
.length());
941 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
942 size_t len
= wcslen( ws
) ;
943 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
945 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
946 StringFormat strFormat
;
948 CharacterRange
* ranges
= new CharacterRange
[len
] ;
949 Region
* regions
= new Region
[len
];
950 for( size_t i
= 0 ; i
< len
; ++i
)
952 ranges
[i
].First
= i
;
953 ranges
[i
].Length
= 1 ;
955 strFormat
.SetMeasurableCharacterRanges(len
,ranges
);
956 m_context
->MeasureCharacterRanges(ws
, -1 , m_font
,layoutRect
, &strFormat
,1,regions
) ;
959 for ( size_t i
= 0 ; i
< len
; ++i
)
961 regions
[i
].GetBounds(&bbox
,m_context
);
962 widths
[i
] = bbox
.GetRight()-bbox
.GetLeft();
966 void wxGDIPlusContext::SetFont( const wxFont
&font
)
968 wxASSERT( font
.Ok());
970 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
971 int size
= font
.GetPointSize();
972 int style
= FontStyleRegular
;
973 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
974 style
|= FontStyleItalic
;
975 if ( font
.GetUnderlined() )
976 style
|= FontStyleUnderline
;
977 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
978 style
|= FontStyleBold
;
979 m_font
= new Font( s
, size
, style
);
982 void* wxGDIPlusContext::GetNativeContext()
987 wxGraphicsContext
* wxGraphicsContext::Create( const wxWindowDC
& dc
)
989 return new wxGDIPlusContext( (HDC
) dc
.GetHDC() );
992 wxGraphicsContext
* wxGraphicsContext::Create( wxWindow
* window
)
994 return new wxGDIPlusContext( (HWND
) window
->GetHWND() );
997 wxGraphicsContext
* wxGraphicsContext::CreateFromNative( void * context
)
999 return new wxGDIPlusContext( (Graphics
*) context
);
1004 #endif // wxUSE_GRAPHICS_CONTEXT