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
140 DECLARE_NO_COPY_CLASS(wxGDIPlusPath
)
147 // These are the path primitives from which everything else can be constructed
150 // begins a new subpath at (x,y)
151 virtual void MoveToPoint( wxDouble x
, wxDouble y
);
153 // adds a straight line from the current point to (x,y)
154 virtual void AddLineToPoint( wxDouble x
, wxDouble y
);
156 // adds a cubic Bezier curve from the current point, using two control points and an end point
157 virtual void AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
);
160 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
161 virtual void AddArc( wxDouble x
, wxDouble y
, wxDouble r
, wxDouble startAngle
, wxDouble endAngle
, bool clockwise
) ;
163 // gets the last point of the current path, (0,0) if not yet set
164 virtual void GetCurrentPoint( wxDouble
& x
, wxDouble
&y
) ;
166 // closes the current sub-path
167 virtual void CloseSubpath();
170 // These are convenience functions which - if not available natively will be assembled
171 // using the primitives from above
174 // appends a rectangle as a new closed subpath
175 virtual void AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
) ;
178 // appends an ellipsis as a new closed subpath fitting the passed rectangle
179 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
181 // 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)
182 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
185 // returns the native path
186 virtual void * GetNativePath() const { return m_path
; }
188 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
189 virtual void UnGetNativePath(void *p
) {}
192 GraphicsPath
* m_path
;
195 class WXDLLEXPORT wxGDIPlusContext
: public wxGraphicsContext
197 DECLARE_NO_COPY_CLASS(wxGDIPlusContext
)
200 wxGDIPlusContext( WXHDC hdc
);
202 virtual ~wxGDIPlusContext();
204 virtual void Clip( const wxRegion
®ion
);
205 // clips drawings to the rect
206 virtual void Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
208 // resets the clipping to original extent
209 virtual void ResetClip();
211 virtual void * GetNativeContext();
213 virtual void StrokePath( const wxGraphicsPath
*p
);
214 virtual void FillPath( const wxGraphicsPath
*p
, int fillStyle
= wxWINDING_RULE
);
216 virtual wxGraphicsPath
* CreatePath();
217 virtual void SetPen( const wxPen
&pen
);
218 virtual void SetBrush( const wxBrush
&brush
);
219 virtual void SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
) ;
220 virtual void SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
221 const wxColour
&oColor
, const wxColour
&cColor
);
223 virtual void Translate( wxDouble dx
, wxDouble dy
);
224 virtual void Scale( wxDouble xScale
, wxDouble yScale
);
225 virtual void Rotate( wxDouble angle
);
227 virtual void DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
228 virtual void DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
);
229 virtual void PushState();
230 virtual void PopState();
232 virtual void SetFont( const wxFont
&font
);
233 virtual void SetTextColor( const wxColour
&col
);
234 virtual void DrawText( const wxString
&str
, wxDouble x
, wxDouble y
);
235 virtual void GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
236 wxDouble
*descent
, wxDouble
*externalLeading
) const;
237 virtual void GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const;
241 vector
<GraphicsState
> m_stateStack
;
242 GraphicsState m_state1
;
243 GraphicsState m_state2
;
246 bool m_penTransparent
;
251 bool m_brushTransparent
;
253 GraphicsPath
* m_brushPath
;
261 wxGDIPlusPath::wxGDIPlusPath()
263 m_path
= new GraphicsPath();
266 wxGDIPlusPath::~wxGDIPlusPath()
275 void wxGDIPlusPath::MoveToPoint( wxDouble x
, wxDouble y
)
277 m_path
->StartFigure();
278 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
281 void wxGDIPlusPath::AddLineToPoint( wxDouble x
, wxDouble y
)
283 m_path
->AddLine((REAL
) x
,(REAL
) y
,(REAL
) x
,(REAL
) y
);
286 void wxGDIPlusPath::CloseSubpath()
288 m_path
->CloseFigure();
291 void wxGDIPlusPath::AddCurveToPoint( wxDouble cx1
, wxDouble cy1
, wxDouble cx2
, wxDouble cy2
, wxDouble x
, wxDouble y
)
297 m_path
->GetLastPoint(&start
);
298 m_path
->AddBezier(start
,c1
,c2
,end
);
301 // gets the last point of the current path, (0,0) if not yet set
302 void wxGDIPlusPath::GetCurrentPoint( wxDouble
& x
, wxDouble
&y
)
305 m_path
->GetLastPoint(&start
);
310 void wxGDIPlusPath::AddArc( wxDouble x
, wxDouble y
, wxDouble r
, double startAngle
, double endAngle
, bool clockwise
)
312 double sweepAngle
= endAngle
- startAngle
;
313 if( abs(sweepAngle
) >= 2*M_PI
)
315 sweepAngle
= 2 * M_PI
;
322 sweepAngle
+= 2 * M_PI
;
327 sweepAngle
-= 2 * M_PI
;
331 m_path
->AddArc((REAL
) (x
-r
),(REAL
) (y
-r
),(REAL
) (2*r
),(REAL
) (2*r
),RadToDeg(startAngle
),RadToDeg(sweepAngle
));
334 void wxGDIPlusPath::AddRectangle( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
336 m_path
->AddRectangle(RectF(x
,y
,w
,h
));
343 // closes the current subpath
344 void wxGDIPlusPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
346 // CGPathAddArcToPoint( m_path, NULL , x1, y1, x2, y2, r);
351 //-----------------------------------------------------------------------------
352 // wxGDIPlusContext implementation
353 //-----------------------------------------------------------------------------
355 wxGDIPlusContext::wxGDIPlusContext( WXHDC hdc
)
357 gGDILoader
.EnsureIsLoaded();
358 m_context
= new Graphics( (HDC
) hdc
);
359 m_context
->SetSmoothingMode(SmoothingModeHighQuality
);
360 m_state1
= m_context
->Save();
361 m_state2
= m_context
->Save();
365 m_penTransparent
= false;
366 m_pen
= new Pen((ARGB
)Color::Black
);
370 m_brushTransparent
= false;
371 m_brush
= new SolidBrush((ARGB
)Color::White
);
374 m_textBrush
= new SolidBrush((ARGB
)Color::Black
);
375 m_font
= new Font( L
"Arial" , 9 , FontStyleRegular
);
378 wxGDIPlusContext::~wxGDIPlusContext()
382 m_context
->Restore( m_state2
);
383 m_context
->Restore( m_state1
);
397 void wxGDIPlusContext::Clip( const wxRegion
& WXUNUSED(region
) )
402 void wxGDIPlusContext::Clip( wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
407 void wxGDIPlusContext::ResetClip()
412 void wxGDIPlusContext::StrokePath( const wxGraphicsPath
*path
)
414 if ( m_penTransparent
)
417 m_context
->DrawPath( m_pen
, (GraphicsPath
*) path
->GetNativePath() );
420 void wxGDIPlusContext::FillPath( const wxGraphicsPath
*path
, int fillStyle
)
422 if ( !m_brushTransparent
)
424 ((GraphicsPath
*) path
->GetNativePath())->SetFillMode( fillStyle
== wxODDEVEN_RULE
? FillModeAlternate
: FillModeWinding
);
425 m_context
->FillPath( m_brush
, (GraphicsPath
*) path
->GetNativePath());
429 wxGraphicsPath
* wxGDIPlusContext::CreatePath()
431 return new wxGDIPlusPath();
434 void wxGDIPlusContext::Rotate( wxDouble angle
)
436 m_context
->RotateTransform( RadToDeg(angle
) );
439 void wxGDIPlusContext::Translate( wxDouble dx
, wxDouble dy
)
441 m_context
->TranslateTransform( dx
, dy
);
444 void wxGDIPlusContext::Scale( wxDouble xScale
, wxDouble yScale
)
446 m_context
->ScaleTransform(xScale
,yScale
);
449 void wxGDIPlusContext::PushState()
451 GraphicsState state
= m_context
->Save();
452 m_stateStack
.push_back(state
);
455 void wxGDIPlusContext::PopState()
457 GraphicsState state
= m_stateStack
.back();
458 m_stateStack
.pop_back();
459 m_context
->Restore(state
);
462 void wxGDIPlusContext::SetTextColor( const wxColour
&col
)
465 m_textBrush
= new SolidBrush( Color( col
.Alpha() , col
.Red() ,
466 col
.Green() , col
.Blue() ));
469 void wxGDIPlusContext::SetPen( const wxPen
&pen
)
471 m_penTransparent
= pen
.GetStyle() == wxTRANSPARENT
;
472 if ( m_penTransparent
)
475 m_pen
->SetColor( Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
476 pen
.GetColour().Green() , pen
.GetColour().Blue() ) );
478 // TODO: * m_dc->m_scaleX
479 double penWidth
= pen
.GetWidth();
483 m_pen
->SetWidth(penWidth
);
486 switch ( pen
.GetCap() )
492 case wxCAP_PROJECTING
:
497 cap
= LineCapFlat
; // TODO verify
504 m_pen
->SetLineCap(cap
,cap
, DashCapFlat
);
507 switch ( pen
.GetJoin() )
510 join
= LineJoinBevel
;
514 join
= LineJoinMiter
;
518 join
= LineJoinRound
;
522 join
= LineJoinMiter
;
526 m_pen
->SetLineJoin(join
);
528 m_pen
->SetDashStyle(DashStyleSolid
);
530 DashStyle dashStyle
= DashStyleSolid
;
531 switch ( pen
.GetStyle() )
537 dashStyle
= DashStyleDot
;
541 dashStyle
= DashStyleDash
; // TODO verify
545 dashStyle
= DashStyleDash
;
549 dashStyle
= DashStyleDashDot
;
553 dashStyle
= DashStyleCustom
;
555 int count
= pen
.GetDashes( &dashes
);
556 if ((dashes
!= NULL
) && (count
> 0))
558 REAL
*userLengths
= new REAL
[count
];
559 for ( int i
= 0; i
< count
; ++i
)
561 userLengths
[i
] = dashes
[i
];
563 m_pen
->SetDashPattern( userLengths
, count
);
564 delete[] userLengths
;
570 wxBitmap
* bmp
= pen
.GetStipple();
571 if ( bmp
&& bmp
->Ok() )
573 wxDELETE( m_penImage
);
574 wxDELETE( m_penBrush
);
575 m_penImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
576 m_penBrush
= new TextureBrush(m_penImage
);
577 m_pen
->SetBrush( m_penBrush
);
583 if ( pen
.GetStyle() >= wxFIRST_HATCH
&& pen
.GetStyle() <= wxLAST_HATCH
)
585 wxDELETE( m_penBrush
);
586 HatchStyle style
= HatchStyleHorizontal
;
587 switch( pen
.GetStyle() )
589 case wxBDIAGONAL_HATCH
:
590 style
= HatchStyleBackwardDiagonal
;
592 case wxCROSSDIAG_HATCH
:
593 style
= HatchStyleDiagonalCross
;
595 case wxFDIAGONAL_HATCH
:
596 style
= HatchStyleForwardDiagonal
;
599 style
= HatchStyleCross
;
601 case wxHORIZONTAL_HATCH
:
602 style
= HatchStyleHorizontal
;
604 case wxVERTICAL_HATCH
:
605 style
= HatchStyleVertical
;
609 m_penBrush
= new HatchBrush(style
,Color( pen
.GetColour().Alpha() , pen
.GetColour().Red() ,
610 pen
.GetColour().Green() , pen
.GetColour().Blue() ), Color::Transparent
);
611 m_pen
->SetBrush( m_penBrush
);
615 if ( dashStyle
!= DashStyleSolid
)
616 m_pen
->SetDashStyle(dashStyle
);
619 void wxGDIPlusContext::SetBrush( const wxBrush
&brush
)
622 if ( m_context
== NULL
)
625 m_brushTransparent
= brush
.GetStyle() == wxTRANSPARENT
;
627 if ( m_brushTransparent
)
632 if ( brush
.GetStyle() == wxSOLID
)
634 m_brush
= new SolidBrush( Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
635 brush
.GetColour().Green() , brush
.GetColour().Blue() ) );
637 else if ( brush
.IsHatch() )
639 HatchStyle style
= HatchStyleHorizontal
;
640 switch( brush
.GetStyle() )
642 case wxBDIAGONAL_HATCH
:
643 style
= HatchStyleBackwardDiagonal
;
645 case wxCROSSDIAG_HATCH
:
646 style
= HatchStyleDiagonalCross
;
648 case wxFDIAGONAL_HATCH
:
649 style
= HatchStyleForwardDiagonal
;
652 style
= HatchStyleCross
;
654 case wxHORIZONTAL_HATCH
:
655 style
= HatchStyleHorizontal
;
657 case wxVERTICAL_HATCH
:
658 style
= HatchStyleVertical
;
662 m_brush
= new HatchBrush(style
,Color( brush
.GetColour().Alpha() , brush
.GetColour().Red() ,
663 brush
.GetColour().Green() , brush
.GetColour().Blue() ), Color::Transparent
);
667 wxBitmap
* bmp
= brush
.GetStipple();
668 if ( bmp
&& bmp
->Ok() )
670 wxDELETE( m_brushImage
);
671 m_brushImage
= Bitmap::FromHBITMAP((HBITMAP
)bmp
->GetHBITMAP(),(HPALETTE
)bmp
->GetPalette()->GetHPALETTE());
672 m_brush
= new TextureBrush(m_brushImage
);
677 void wxGDIPlusContext::SetLinearGradientBrush( wxDouble x1
, wxDouble y1
, wxDouble x2
, wxDouble y2
, const wxColour
&c1
, const wxColour
&c2
)
679 m_brushTransparent
= false ;
683 m_brush
= new LinearGradientBrush( PointF( x1
,y1
) , PointF( x2
,y2
),
684 Color( c1
.Alpha(), c1
.Red(),c1
.Green() , c1
.Blue() ),
685 Color( c2
.Alpha(), c2
.Red(),c2
.Green() , c2
.Blue() ));
688 void wxGDIPlusContext::SetRadialGradientBrush( wxDouble xo
, wxDouble yo
, wxDouble xc
, wxDouble yc
, wxDouble radius
,
689 const wxColour
&oColor
, const wxColour
&cColor
)
691 m_brushTransparent
= false ;
694 wxDELETE(m_brushPath
);
696 // Create a path that consists of a single circle.
697 m_brushPath
= new GraphicsPath();
698 m_brushPath
->AddEllipse( (REAL
)(xc
-radius
), (REAL
)(yc
-radius
), (REAL
)(2*radius
), (REAL
)(2*radius
));
700 PathGradientBrush
*b
= new PathGradientBrush(m_brushPath
);
702 b
->SetCenterPoint( PointF(xo
,yo
));
703 b
->SetCenterColor(Color( oColor
.Alpha(), oColor
.Red(),oColor
.Green() , oColor
.Blue() ));
705 Color colors
[] = {Color( cColor
.Alpha(), cColor
.Red(),cColor
.Green() , cColor
.Blue() )};
707 b
->SetSurroundColors(colors
, &count
);
710 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
711 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
712 // bytes as parameter
714 void wxGDIPlusContext::DrawBitmap( const wxBitmap
&bmp
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
716 Bitmap
* image
= NULL
;
717 Bitmap
* helper
= NULL
;
720 Bitmap
interim((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE()) ;
722 size_t width
= interim
.GetWidth();
723 size_t height
= interim
.GetHeight();
724 Rect
bounds(0,0,width
,height
);
726 image
= new Bitmap(width
,height
,PixelFormat32bppPARGB
) ;
728 Bitmap
interimMask((HBITMAP
)bmp
.GetMask()->GetMaskBitmap(),NULL
);
729 wxASSERT(interimMask
.GetPixelFormat() == PixelFormat1bppIndexed
);
731 BitmapData dataMask
;
732 interimMask
.LockBits(&bounds
,ImageLockModeRead
,
733 interimMask
.GetPixelFormat(),&dataMask
);
736 BitmapData imageData
;
737 image
->LockBits(&bounds
,ImageLockModeWrite
, PixelFormat32bppPARGB
, &imageData
);
739 BYTE maskPattern
= 0 ;
743 for ( size_t y
= 0 ; y
< height
; ++y
)
746 for( size_t x
= 0 ; x
< width
; ++x
)
751 maskByte
= *((BYTE
*)dataMask
.Scan0
+ dataMask
.Stride
*y
+ maskIndex
);
755 maskPattern
= maskPattern
>> 1;
757 ARGB
*dest
= (ARGB
*)((BYTE
*)imageData
.Scan0
+ imageData
.Stride
*y
+ x
*4);
758 if ( (maskByte
& maskPattern
) == 0 )
763 interim
.GetPixel(x
,y
,&c
) ;
764 *dest
= (c
.GetValue() | Color::AlphaMask
);
769 image
->UnlockBits(&imageData
);
771 interimMask
.UnlockBits(&dataMask
);
772 interim
.UnlockBits(&dataMask
);
776 image
= Bitmap::FromHBITMAP((HBITMAP
)bmp
.GetHBITMAP(),(HPALETTE
)bmp
.GetPalette()->GetHPALETTE());
777 if ( GetPixelFormatSize(image
->GetPixelFormat()) == 32 )
779 size_t width
= image
->GetWidth();
780 size_t height
= image
->GetHeight();
781 Rect
bounds(0,0,width
,height
);
786 helper
->LockBits(&bounds
, ImageLockModeRead
,
787 helper
->GetPixelFormat(),&data
);
789 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
790 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
792 helper
->UnlockBits(&data
);
796 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
801 void wxGDIPlusContext::DrawIcon( const wxIcon
&icon
, wxDouble x
, wxDouble y
, wxDouble w
, wxDouble h
)
803 HICON hIcon
= (HICON
)icon
.GetHICON();
805 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
806 if (!GetIconInfo(hIcon
,&iconInfo
))
810 GetObject(iconInfo
.hbmColor
,sizeof(BITMAP
),&iconBmpData
);
811 Bitmap
interim(iconInfo
.hbmColor
,NULL
);
813 Bitmap
* image
= NULL
;
815 if( GetPixelFormatSize(interim
.GetPixelFormat())!= 32 )
817 image
= Bitmap::FromHICON(hIcon
);
821 size_t width
= interim
.GetWidth();
822 size_t height
= interim
.GetHeight();
823 Rect
bounds(0,0,width
,height
);
826 interim
.LockBits(&bounds
, ImageLockModeRead
,
827 interim
.GetPixelFormat(),&data
);
828 image
= new Bitmap(data
.Width
, data
.Height
, data
.Stride
,
829 PixelFormat32bppARGB
, (BYTE
*) data
.Scan0
);
830 interim
.UnlockBits(&data
);
833 m_context
->DrawImage(image
,(REAL
) x
,(REAL
) y
,(REAL
) w
,(REAL
) h
) ;
836 DeleteObject(iconInfo
.hbmColor
);
837 DeleteObject(iconInfo
.hbmMask
);
841 void wxGDIPlusContext::DrawText( const wxString
&str
, wxDouble x
, wxDouble y
)
846 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
847 m_context
->DrawString( s
, -1 , m_font
, PointF( x
, y
) , m_textBrush
);
848 // TODO m_backgroundMode == wxSOLID
851 void wxGDIPlusContext::GetTextExtent( const wxString
&str
, wxDouble
*width
, wxDouble
*height
,
852 wxDouble
*descent
, wxDouble
*externalLeading
) const
854 wxWCharBuffer s
= str
.wc_str( *wxConvUI
);
857 m_font
->GetFamily(&ffamily
) ;
859 REAL factorY
= m_context
->GetDpiY() / 72.0 ;
861 REAL rDescent
= ffamily
.GetCellDescent(FontStyleRegular
) *
862 m_font
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
863 REAL rAscent
= ffamily
.GetCellAscent(FontStyleRegular
) *
864 m_font
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
865 REAL rHeight
= ffamily
.GetLineSpacing(FontStyleRegular
) *
866 m_font
->GetSize() / ffamily
.GetEmHeight(FontStyleRegular
);
869 *height
= rHeight
* factorY
+ 0.5 ;
871 *descent
= rDescent
* factorY
+ 0.5 ;
872 if ( externalLeading
)
873 *externalLeading
= (rHeight
- rAscent
- rDescent
) * factorY
+ 0.5 ;
874 // measuring empty strings is not guaranteed, so do it by hand
882 // MeasureString does return a rectangle that is way too large, so it is
884 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
885 StringFormat strFormat
;
886 CharacterRange
strRange(0,wcslen(s
));
887 strFormat
.SetMeasurableCharacterRanges(1,&strRange
);
889 m_context
->MeasureCharacterRanges(s
, -1 , m_font
,layoutRect
, &strFormat
,1,®ion
) ;
891 region
.GetBounds(&bbox
,m_context
);
893 *width
= bbox
.GetRight()-bbox
.GetLeft()+0.5;
897 void wxGDIPlusContext::GetPartialTextExtents(const wxString
& text
, wxArrayDouble
& widths
) const
900 widths
.Add(0, text
.length());
905 wxWCharBuffer ws
= text
.wc_str( *wxConvUI
);
906 size_t len
= wcslen( ws
) ;
907 wxASSERT_MSG(text
.length() == len
, wxT("GetPartialTextExtents not yet implemented for multichar situations"));
909 RectF
layoutRect(0,0, 100000.0f
, 100000.0f
);
910 StringFormat strFormat
;
912 CharacterRange
* ranges
= new CharacterRange
[len
] ;
913 Region
* regions
= new Region
[len
];
914 for( size_t i
= 0 ; i
< len
; ++i
)
916 ranges
[i
].First
= i
;
917 ranges
[i
].Length
= 1 ;
919 strFormat
.SetMeasurableCharacterRanges(len
,ranges
);
920 m_context
->MeasureCharacterRanges(ws
, -1 , m_font
,layoutRect
, &strFormat
,1,regions
) ;
923 for ( size_t i
= 0 ; i
< len
; ++i
)
925 regions
[i
].GetBounds(&bbox
,m_context
);
926 widths
[i
] = bbox
.GetRight()-bbox
.GetLeft();
930 void wxGDIPlusContext::SetFont( const wxFont
&font
)
932 wxASSERT( font
.Ok());
934 wxWCharBuffer s
= font
.GetFaceName().wc_str( *wxConvUI
);
935 int size
= font
.GetPointSize();
936 int style
= FontStyleRegular
;
937 if ( font
.GetStyle() == wxFONTSTYLE_ITALIC
)
938 style
|= FontStyleItalic
;
939 if ( font
.GetUnderlined() )
940 style
|= FontStyleUnderline
;
941 if ( font
.GetWeight() == wxFONTWEIGHT_BOLD
)
942 style
|= FontStyleBold
;
943 m_font
= new Font( s
, size
, style
);
946 void* wxGDIPlusContext::GetNativeContext()
951 wxGraphicsContext
* wxGraphicsContext::Create( const wxWindowDC
& dc
)
953 return new wxGDIPlusContext( (HDC
) dc
.GetHDC() );
956 wxGraphicsContext
* wxGraphicsContext::Create( wxWindow
* window
)
961 wxGraphicsContext
* wxGraphicsContext::CreateFromNative( void * context
)
968 #endif // wxUSE_GRAPHICS_CONTEXT