]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/graphics.cpp
OSXTimer for all variants
[wxWidgets.git] / src / msw / graphics.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/graphics.cpp
3// Purpose: wxGCDC class
4// Author: Stefan Csomor
5// Modified by:
6// Created: 2006-09-30
7// RCS-ID: $Id$
8// Copyright: (c) 2006 Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#include "wx/dc.h"
15
16#ifdef __BORLANDC__
17#pragma hdrstop
18#endif
19
20#if wxUSE_GRAPHICS_CONTEXT
21
22#ifndef WX_PRECOMP
23 #include "wx/msw/wrapcdlg.h"
24 #include "wx/image.h"
25 #include "wx/window.h"
26 #include "wx/utils.h"
27 #include "wx/dialog.h"
28 #include "wx/app.h"
29 #include "wx/bitmap.h"
30 #include "wx/log.h"
31 #include "wx/icon.h"
32 #include "wx/module.h"
33 // include all dc types that are used as a param
34 #include "wx/dc.h"
35 #include "wx/dcclient.h"
36 #include "wx/dcmemory.h"
37 #include "wx/dcprint.h"
38#endif
39
40#include "wx/private/graphics.h"
41#include "wx/msw/wrapgdip.h"
42#include "wx/msw/dc.h"
43
44#include "wx/stack.h"
45
46WX_DECLARE_STACK(GraphicsState, GraphicsStates);
47
48//-----------------------------------------------------------------------------
49// constants
50//-----------------------------------------------------------------------------
51
52const double RAD2DEG = 180.0 / M_PI;
53
54//-----------------------------------------------------------------------------
55// Local functions
56//-----------------------------------------------------------------------------
57
58static inline double dmin(double a, double b) { return a < b ? a : b; }
59static inline double dmax(double a, double b) { return a > b ? a : b; }
60
61static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
62static inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; }
63
64//-----------------------------------------------------------------------------
65// device context implementation
66//
67// more and more of the dc functionality should be implemented by calling
68// the appropricate wxGDIPlusContext, but we will have to do that step by step
69// also coordinate conversions should be moved to native matrix ops
70//-----------------------------------------------------------------------------
71
72// we always stock two context states, one at entry, to be able to preserve the
73// state we were called with, the other one after changing to HI Graphics orientation
74// (this one is used for getting back clippings etc)
75
76//-----------------------------------------------------------------------------
77// wxGraphicsPath implementation
78//-----------------------------------------------------------------------------
79
80#include "wx/msw/private.h" // needs to be before #include <commdlg.h>
81
82#if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
83#include <commdlg.h>
84#endif
85
86class wxGDIPlusPathData : public wxGraphicsPathData
87{
88public :
89 wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path = NULL);
90 ~wxGDIPlusPathData();
91
92 virtual wxGraphicsObjectRefData *Clone() const;
93
94 //
95 // These are the path primitives from which everything else can be constructed
96 //
97
98 // begins a new subpath at (x,y)
99 virtual void MoveToPoint( wxDouble x, wxDouble y );
100
101 // adds a straight line from the current point to (x,y)
102 virtual void AddLineToPoint( wxDouble x, wxDouble y );
103
104 // adds a cubic Bezier curve from the current point, using two control points and an end point
105 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
106
107
108 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
109 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
110
111 // gets the last point of the current path, (0,0) if not yet set
112 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
113
114 // adds another path
115 virtual void AddPath( const wxGraphicsPathData* path );
116
117 // closes the current sub-path
118 virtual void CloseSubpath();
119
120 //
121 // These are convenience functions which - if not available natively will be assembled
122 // using the primitives from above
123 //
124
125 // appends a rectangle as a new closed subpath
126 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
127 /*
128
129 // appends an ellipsis as a new closed subpath fitting the passed rectangle
130 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
131
132 // 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)
133 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
134*/
135
136 // returns the native path
137 virtual void * GetNativePath() const { return m_path; }
138
139 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
140 virtual void UnGetNativePath(void * WXUNUSED(path)) const {}
141
142 // transforms each point of this path by the matrix
143 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
144
145 // gets the bounding box enclosing all points (possibly including control points)
146 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
147
148 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxODDEVEN_RULE) const;
149
150private :
151 GraphicsPath* m_path;
152};
153
154class wxGDIPlusMatrixData : public wxGraphicsMatrixData
155{
156public :
157 wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix = NULL) ;
158 virtual ~wxGDIPlusMatrixData() ;
159
160 virtual wxGraphicsObjectRefData* Clone() const ;
161
162 // concatenates the matrix
163 virtual void Concat( const wxGraphicsMatrixData *t );
164
165 // sets the matrix to the respective values
166 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
167 wxDouble tx=0.0, wxDouble ty=0.0);
168
169 // gets the component valuess of the matrix
170 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
171 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
172
173 // makes this the inverse matrix
174 virtual void Invert();
175
176 // returns true if the elements of the transformation matrix are equal ?
177 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
178
179 // return true if this is the identity matrix
180 virtual bool IsIdentity() const;
181
182 //
183 // transformation
184 //
185
186 // add the translation to this matrix
187 virtual void Translate( wxDouble dx , wxDouble dy );
188
189 // add the scale to this matrix
190 virtual void Scale( wxDouble xScale , wxDouble yScale );
191
192 // add the rotation to this matrix (radians)
193 virtual void Rotate( wxDouble angle );
194
195 //
196 // apply the transforms
197 //
198
199 // applies that matrix to the point
200 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
201
202 // applies the matrix except for translations
203 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
204
205 // returns the native representation
206 virtual void * GetNativeMatrix() const;
207private:
208 Matrix* m_matrix ;
209} ;
210
211class wxGDIPlusPenData : public wxGraphicsObjectRefData
212{
213public:
214 wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
215 ~wxGDIPlusPenData();
216
217 void Init();
218
219 virtual wxDouble GetWidth() { return m_width; }
220 virtual Pen* GetGDIPlusPen() { return m_pen; }
221
222protected :
223 Pen* m_pen;
224 Image* m_penImage;
225 Brush* m_penBrush;
226
227 wxDouble m_width;
228};
229
230class wxGDIPlusBrushData : public wxGraphicsObjectRefData
231{
232public:
233 wxGDIPlusBrushData( wxGraphicsRenderer* renderer );
234 wxGDIPlusBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
235 ~wxGDIPlusBrushData ();
236
237 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
238 const wxColour&c1, const wxColour&c2 );
239 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
240 const wxColour &oColor, const wxColour &cColor );
241 virtual Brush* GetGDIPlusBrush() { return m_brush; }
242
243protected:
244 virtual void Init();
245
246private :
247 Brush* m_brush;
248 Image* m_brushImage;
249 GraphicsPath* m_brushPath;
250};
251
252class WXDLLIMPEXP_CORE wxGDIPlusBitmapData : public wxGraphicsObjectRefData
253{
254public:
255 wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap );
256 wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, const wxBitmap &bmp );
257 ~wxGDIPlusBitmapData ();
258
259 virtual Bitmap* GetGDIPlusBitmap() { return m_bitmap; }
260
261private :
262 Bitmap* m_bitmap;
263 Bitmap* m_helper;
264};
265
266class wxGDIPlusFontData : public wxGraphicsObjectRefData
267{
268public:
269 wxGDIPlusFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
270 ~wxGDIPlusFontData();
271
272 virtual Brush* GetGDIPlusBrush() { return m_textBrush; }
273 virtual Font* GetGDIPlusFont() { return m_font; }
274private :
275 Brush* m_textBrush;
276 Font* m_font;
277};
278
279class wxGDIPlusContext : public wxGraphicsContext
280{
281public:
282 wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc );
283 wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd );
284 wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr);
285 wxGDIPlusContext();
286
287 virtual ~wxGDIPlusContext();
288
289 virtual void Clip( const wxRegion &region );
290 // clips drawings to the rect
291 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
292
293 // resets the clipping to original extent
294 virtual void ResetClip();
295
296 virtual void * GetNativeContext();
297
298 virtual void StrokePath( const wxGraphicsPath& p );
299 virtual void FillPath( const wxGraphicsPath& p , int fillStyle = wxODDEVEN_RULE );
300
301 // stroke lines connecting each of the points
302 virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
303
304 // draws a polygon
305 virtual void DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle = wxODDEVEN_RULE );
306
307 virtual void Translate( wxDouble dx , wxDouble dy );
308 virtual void Scale( wxDouble xScale , wxDouble yScale );
309 virtual void Rotate( wxDouble angle );
310
311 // concatenates this transform with the current transform of this context
312 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
313
314 // sets the transform of this context
315 virtual void SetTransform( const wxGraphicsMatrix& matrix );
316
317 // gets the matrix of this context
318 virtual wxGraphicsMatrix GetTransform() const;
319
320 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
321 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
322 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
323 virtual void PushState();
324 virtual void PopState();
325
326 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
327 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
328 wxDouble *descent, wxDouble *externalLeading ) const;
329 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
330 virtual bool ShouldOffset() const;
331
332private:
333 void Init();
334 void SetDefaults();
335
336 Graphics* m_context;
337 GraphicsStates m_stateStack;
338 GraphicsState m_state1;
339 GraphicsState m_state2;
340
341 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext)
342};
343
344class WXDLLIMPEXP_CORE wxGDIPlusMeasuringContext : public wxGDIPlusContext
345{
346public:
347 wxGDIPlusMeasuringContext( wxGraphicsRenderer* renderer ) : wxGDIPlusContext( renderer , m_hdc = GetDC(NULL) )
348 {
349 }
350 wxGDIPlusMeasuringContext()
351 {
352 }
353
354 virtual ~wxGDIPlusMeasuringContext()
355 {
356 ReleaseDC( NULL, m_hdc );
357 }
358
359private:
360 HDC m_hdc ;
361 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext)
362} ;
363
364//-----------------------------------------------------------------------------
365// wxGDIPlusPen implementation
366//-----------------------------------------------------------------------------
367
368wxGDIPlusPenData::~wxGDIPlusPenData()
369{
370 delete m_pen;
371 delete m_penImage;
372 delete m_penBrush;
373}
374
375void wxGDIPlusPenData::Init()
376{
377 m_pen = NULL ;
378 m_penImage = NULL;
379 m_penBrush = NULL;
380}
381
382wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
383: wxGraphicsObjectRefData(renderer)
384{
385 Init();
386 m_width = pen.GetWidth();
387 if (m_width <= 0.0)
388 m_width = 0.1;
389
390 m_pen = new Pen(Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
391 pen.GetColour().Green() , pen.GetColour().Blue() ), m_width );
392
393 LineCap cap;
394 switch ( pen.GetCap() )
395 {
396 case wxCAP_ROUND :
397 cap = LineCapRound;
398 break;
399
400 case wxCAP_PROJECTING :
401 cap = LineCapSquare;
402 break;
403
404 case wxCAP_BUTT :
405 cap = LineCapFlat; // TODO verify
406 break;
407
408 default :
409 cap = LineCapFlat;
410 break;
411 }
412 m_pen->SetLineCap(cap,cap, DashCapFlat);
413
414 LineJoin join;
415 switch ( pen.GetJoin() )
416 {
417 case wxJOIN_BEVEL :
418 join = LineJoinBevel;
419 break;
420
421 case wxJOIN_MITER :
422 join = LineJoinMiter;
423 break;
424
425 case wxJOIN_ROUND :
426 join = LineJoinRound;
427 break;
428
429 default :
430 join = LineJoinMiter;
431 break;
432 }
433
434 m_pen->SetLineJoin(join);
435
436 m_pen->SetDashStyle(DashStyleSolid);
437
438 DashStyle dashStyle = DashStyleSolid;
439 switch ( pen.GetStyle() )
440 {
441 case wxSOLID :
442 break;
443
444 case wxDOT :
445 dashStyle = DashStyleDot;
446 break;
447
448 case wxLONG_DASH :
449 dashStyle = DashStyleDash; // TODO verify
450 break;
451
452 case wxSHORT_DASH :
453 dashStyle = DashStyleDash;
454 break;
455
456 case wxDOT_DASH :
457 dashStyle = DashStyleDashDot;
458 break;
459 case wxUSER_DASH :
460 {
461 dashStyle = DashStyleCustom;
462 wxDash *dashes;
463 int count = pen.GetDashes( &dashes );
464 if ((dashes != NULL) && (count > 0))
465 {
466 REAL *userLengths = new REAL[count];
467 for ( int i = 0; i < count; ++i )
468 {
469 userLengths[i] = dashes[i];
470 }
471 m_pen->SetDashPattern( userLengths, count);
472 delete[] userLengths;
473 }
474 }
475 break;
476 case wxSTIPPLE :
477 {
478 wxBitmap* bmp = pen.GetStipple();
479 if ( bmp && bmp->Ok() )
480 {
481 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
482 m_penBrush = new TextureBrush(m_penImage);
483 m_pen->SetBrush( m_penBrush );
484 }
485
486 }
487 break;
488 default :
489 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
490 {
491 HatchStyle style = HatchStyleHorizontal;
492 switch( pen.GetStyle() )
493 {
494 case wxBDIAGONAL_HATCH :
495 style = HatchStyleBackwardDiagonal;
496 break ;
497 case wxCROSSDIAG_HATCH :
498 style = HatchStyleDiagonalCross;
499 break ;
500 case wxFDIAGONAL_HATCH :
501 style = HatchStyleForwardDiagonal;
502 break ;
503 case wxCROSS_HATCH :
504 style = HatchStyleCross;
505 break ;
506 case wxHORIZONTAL_HATCH :
507 style = HatchStyleHorizontal;
508 break ;
509 case wxVERTICAL_HATCH :
510 style = HatchStyleVertical;
511 break ;
512
513 }
514 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
515 pen.GetColour().Green() , pen.GetColour().Blue() ), Color::Transparent );
516 m_pen->SetBrush( m_penBrush );
517 }
518 break;
519 }
520 if ( dashStyle != DashStyleSolid )
521 m_pen->SetDashStyle(dashStyle);
522}
523
524//-----------------------------------------------------------------------------
525// wxGDIPlusBrush implementation
526//-----------------------------------------------------------------------------
527
528wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer )
529: wxGraphicsObjectRefData(renderer)
530{
531 Init();
532}
533
534wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer , const wxBrush &brush )
535: wxGraphicsObjectRefData(renderer)
536{
537 Init();
538 if ( brush.GetStyle() == wxSOLID)
539 {
540 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
541 brush.GetColour().Green() , brush.GetColour().Blue() ) );
542 }
543 else if ( brush.IsHatch() )
544 {
545 HatchStyle style = HatchStyleHorizontal;
546 switch( brush.GetStyle() )
547 {
548 case wxBDIAGONAL_HATCH :
549 style = HatchStyleBackwardDiagonal;
550 break ;
551 case wxCROSSDIAG_HATCH :
552 style = HatchStyleDiagonalCross;
553 break ;
554 case wxFDIAGONAL_HATCH :
555 style = HatchStyleForwardDiagonal;
556 break ;
557 case wxCROSS_HATCH :
558 style = HatchStyleCross;
559 break ;
560 case wxHORIZONTAL_HATCH :
561 style = HatchStyleHorizontal;
562 break ;
563 case wxVERTICAL_HATCH :
564 style = HatchStyleVertical;
565 break ;
566
567 }
568 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
569 brush.GetColour().Green() , brush.GetColour().Blue() ), Color::Transparent );
570 }
571 else
572 {
573 wxBitmap* bmp = brush.GetStipple();
574 if ( bmp && bmp->Ok() )
575 {
576 wxDELETE( m_brushImage );
577 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
578 m_brush = new TextureBrush(m_brushImage);
579 }
580 }
581}
582
583wxGDIPlusBrushData::~wxGDIPlusBrushData()
584{
585 delete m_brush;
586 delete m_brushImage;
587 delete m_brushPath;
588};
589
590void wxGDIPlusBrushData::Init()
591{
592 m_brush = NULL;
593 m_brushImage= NULL;
594 m_brushPath= NULL;
595}
596
597void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2)
598{
599 m_brush = new LinearGradientBrush( PointF( x1,y1) , PointF( x2,y2),
600 Color( c1.Alpha(), c1.Red(),c1.Green() , c1.Blue() ),
601 Color( c2.Alpha(), c2.Red(),c2.Green() , c2.Blue() ));
602}
603
604void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
605 const wxColour &oColor, const wxColour &cColor)
606{
607 // Create a path that consists of a single circle.
608 m_brushPath = new GraphicsPath();
609 m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius), (REAL)(2*radius), (REAL)(2*radius));
610
611 PathGradientBrush *b = new PathGradientBrush(m_brushPath);
612 m_brush = b;
613 b->SetCenterPoint( PointF(xo,yo));
614 b->SetCenterColor(Color( oColor.Alpha(), oColor.Red(),oColor.Green() , oColor.Blue() ));
615
616 Color colors[] = {Color( cColor.Alpha(), cColor.Red(),cColor.Green() , cColor.Blue() )};
617 int count = 1;
618 b->SetSurroundColors(colors, &count);
619}
620
621//-----------------------------------------------------------------------------
622// wxGDIPlusFont implementation
623//-----------------------------------------------------------------------------
624
625wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer* renderer, const wxFont &font,
626 const wxColour& col ) : wxGraphicsObjectRefData( renderer )
627{
628 m_textBrush = NULL;
629 m_font = NULL;
630
631 wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI );
632 int size = font.GetPointSize();
633 int style = FontStyleRegular;
634 if ( font.GetStyle() == wxFONTSTYLE_ITALIC )
635 style |= FontStyleItalic;
636 if ( font.GetUnderlined() )
637 style |= FontStyleUnderline;
638 if ( font.GetWeight() == wxFONTWEIGHT_BOLD )
639 style |= FontStyleBold;
640 m_font = new Font( s , size , style );
641 m_textBrush = new SolidBrush( Color( col.Alpha() , col.Red() ,
642 col.Green() , col.Blue() ));
643}
644
645wxGDIPlusFontData::~wxGDIPlusFontData()
646{
647 delete m_textBrush;
648 delete m_font;
649}
650
651// the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
652// premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
653// bytes as parameter, since there is no real copying of the data going in, only references are stored
654// m_helper has to be kept alive as well
655
656//-----------------------------------------------------------------------------
657// wxGDIPlusBitmapData implementation
658//-----------------------------------------------------------------------------
659
660wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap ) :
661 wxGraphicsObjectRefData( renderer ), m_bitmap( bitmap )
662{
663 m_helper = NULL;
664}
665
666wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer,
667 const wxBitmap &bmp) : wxGraphicsObjectRefData( renderer )
668{
669 m_bitmap = NULL;
670 m_helper = NULL;
671
672 Bitmap* image = NULL;
673 if ( bmp.GetMask() )
674 {
675 Bitmap interim((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE()) ;
676
677 size_t width = interim.GetWidth();
678 size_t height = interim.GetHeight();
679 Rect bounds(0,0,width,height);
680
681 image = new Bitmap(width,height,PixelFormat32bppPARGB) ;
682
683 Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL);
684 wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed);
685
686 BitmapData dataMask ;
687 interimMask.LockBits(&bounds,ImageLockModeRead,
688 interimMask.GetPixelFormat(),&dataMask);
689
690
691 BitmapData imageData ;
692 image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData);
693
694 BYTE maskPattern = 0 ;
695 BYTE maskByte = 0;
696 size_t maskIndex ;
697
698 for ( size_t y = 0 ; y < height ; ++y)
699 {
700 maskIndex = 0 ;
701 for( size_t x = 0 ; x < width; ++x)
702 {
703 if ( x % 8 == 0)
704 {
705 maskPattern = 0x80;
706 maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex);
707 maskIndex++;
708 }
709 else
710 maskPattern = maskPattern >> 1;
711
712 ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4);
713 if ( (maskByte & maskPattern) == 0 )
714 *dest = 0x00000000;
715 else
716 {
717 Color c ;
718 interim.GetPixel(x,y,&c) ;
719 *dest = (c.GetValue() | Color::AlphaMask);
720 }
721 }
722 }
723
724 image->UnlockBits(&imageData);
725
726 interimMask.UnlockBits(&dataMask);
727 interim.UnlockBits(&dataMask);
728 }
729 else
730 {
731 image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
732 if ( bmp.HasAlpha() && GetPixelFormatSize(image->GetPixelFormat()) == 32 )
733 {
734 size_t width = image->GetWidth();
735 size_t height = image->GetHeight();
736 Rect bounds(0,0,width,height);
737 static BitmapData data ;
738
739 m_helper = image ;
740 image = NULL ;
741 m_helper->LockBits(&bounds, ImageLockModeRead,
742 m_helper->GetPixelFormat(),&data);
743
744 image = new Bitmap(data.Width, data.Height, data.Stride,
745 PixelFormat32bppPARGB , (BYTE*) data.Scan0);
746
747 m_helper->UnlockBits(&data);
748 }
749 }
750 if ( image )
751 m_bitmap = image;
752}
753
754wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
755{
756 delete m_bitmap;
757 delete m_helper;
758}
759
760//-----------------------------------------------------------------------------
761// wxGDIPlusPath implementation
762//-----------------------------------------------------------------------------
763
764wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path ) : wxGraphicsPathData(renderer)
765{
766 if ( path )
767 m_path = path;
768 else
769 m_path = new GraphicsPath();
770}
771
772wxGDIPlusPathData::~wxGDIPlusPathData()
773{
774 delete m_path;
775}
776
777wxGraphicsObjectRefData* wxGDIPlusPathData::Clone() const
778{
779 return new wxGDIPlusPathData( GetRenderer() , m_path->Clone());
780}
781
782//
783// The Primitives
784//
785
786void wxGDIPlusPathData::MoveToPoint( wxDouble x , wxDouble y )
787{
788 m_path->StartFigure();
789 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
790}
791
792void wxGDIPlusPathData::AddLineToPoint( wxDouble x , wxDouble y )
793{
794 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
795}
796
797void wxGDIPlusPathData::CloseSubpath()
798{
799 m_path->CloseFigure();
800}
801
802void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
803{
804 PointF c1(cx1,cy1);
805 PointF c2(cx2,cy2);
806 PointF end(x,y);
807 PointF start;
808 m_path->GetLastPoint(&start);
809 m_path->AddBezier(start,c1,c2,end);
810}
811
812// gets the last point of the current path, (0,0) if not yet set
813void wxGDIPlusPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
814{
815 PointF start;
816 m_path->GetLastPoint(&start);
817 *x = start.X ;
818 *y = start.Y ;
819}
820
821void wxGDIPlusPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
822{
823 double sweepAngle = endAngle - startAngle ;
824 if( fabs(sweepAngle) >= 2*M_PI)
825 {
826 sweepAngle = 2 * M_PI;
827 }
828 else
829 {
830 if ( clockwise )
831 {
832 if( sweepAngle < 0 )
833 sweepAngle += 2 * M_PI;
834 }
835 else
836 {
837 if( sweepAngle > 0 )
838 sweepAngle -= 2 * M_PI;
839
840 }
841 }
842 m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle));
843}
844
845void wxGDIPlusPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
846{
847 m_path->AddRectangle(RectF(x,y,w,h));
848}
849
850void wxGDIPlusPathData::AddPath( const wxGraphicsPathData* path )
851{
852 m_path->AddPath( (GraphicsPath*) path->GetNativePath(), FALSE);
853}
854
855
856// transforms each point of this path by the matrix
857void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData* matrix )
858{
859 m_path->Transform( (Matrix*) matrix->GetNativeMatrix() );
860}
861
862// gets the bounding box enclosing all points (possibly including control points)
863void wxGDIPlusPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
864{
865 RectF bounds;
866 m_path->GetBounds( &bounds, NULL, NULL) ;
867 *x = bounds.X;
868 *y = bounds.Y;
869 *w = bounds.Width;
870 *h = bounds.Height;
871}
872
873bool wxGDIPlusPathData::Contains( wxDouble x, wxDouble y, int fillStyle ) const
874{
875 m_path->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
876 return m_path->IsVisible( (FLOAT) x,(FLOAT) y) == TRUE ;
877}
878
879//-----------------------------------------------------------------------------
880// wxGDIPlusMatrixData implementation
881//-----------------------------------------------------------------------------
882
883wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix )
884 : wxGraphicsMatrixData(renderer)
885{
886 if ( matrix )
887 m_matrix = matrix ;
888 else
889 m_matrix = new Matrix();
890}
891
892wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
893{
894 delete m_matrix;
895}
896
897wxGraphicsObjectRefData *wxGDIPlusMatrixData::Clone() const
898{
899 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix->Clone());
900}
901
902// concatenates the matrix
903void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData *t )
904{
905 m_matrix->Multiply( (Matrix*) t->GetNativeMatrix());
906}
907
908// sets the matrix to the respective values
909void wxGDIPlusMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
910 wxDouble tx, wxDouble ty)
911{
912 m_matrix->SetElements(a,b,c,d,tx,ty);
913}
914
915// gets the component valuess of the matrix
916void wxGDIPlusMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
917 wxDouble* d, wxDouble* tx, wxDouble* ty) const
918{
919 REAL elements[6];
920 m_matrix->GetElements(elements);
921 if (a) *a = elements[0];
922 if (b) *b = elements[1];
923 if (c) *c = elements[2];
924 if (d) *d = elements[3];
925 if (tx) *tx= elements[4];
926 if (ty) *ty= elements[5];
927}
928
929// makes this the inverse matrix
930void wxGDIPlusMatrixData::Invert()
931{
932 m_matrix->Invert();
933}
934
935// returns true if the elements of the transformation matrix are equal ?
936bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
937{
938 return m_matrix->Equals((Matrix*) t->GetNativeMatrix())== TRUE ;
939}
940
941// return true if this is the identity matrix
942bool wxGDIPlusMatrixData::IsIdentity() const
943{
944 return m_matrix->IsIdentity() == TRUE ;
945}
946
947//
948// transformation
949//
950
951// add the translation to this matrix
952void wxGDIPlusMatrixData::Translate( wxDouble dx , wxDouble dy )
953{
954 m_matrix->Translate(dx,dy);
955}
956
957// add the scale to this matrix
958void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale )
959{
960 m_matrix->Scale(xScale,yScale);
961}
962
963// add the rotation to this matrix (radians)
964void wxGDIPlusMatrixData::Rotate( wxDouble angle )
965{
966 m_matrix->Rotate( angle );
967}
968
969//
970// apply the transforms
971//
972
973// applies that matrix to the point
974void wxGDIPlusMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
975{
976 PointF pt(*x,*y);
977 m_matrix->TransformPoints(&pt);
978 *x = pt.X;
979 *y = pt.Y;
980}
981
982// applies the matrix except for translations
983void wxGDIPlusMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
984{
985 PointF pt(*dx,*dy);
986 m_matrix->TransformVectors(&pt);
987 *dx = pt.X;
988 *dy = pt.Y;
989}
990
991// returns the native representation
992void * wxGDIPlusMatrixData::GetNativeMatrix() const
993{
994 return m_matrix;
995}
996
997//-----------------------------------------------------------------------------
998// wxGDIPlusContext implementation
999//-----------------------------------------------------------------------------
1000
1001IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext,wxGraphicsContext)
1002IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext,wxGDIPlusContext)
1003
1004class wxGDIPlusOffsetHelper
1005{
1006public :
1007 wxGDIPlusOffsetHelper( Graphics* gr , bool offset )
1008 {
1009 m_gr = gr;
1010 m_offset = offset;
1011 if ( m_offset )
1012 m_gr->TranslateTransform( 0.5, 0.5 );
1013 }
1014 ~wxGDIPlusOffsetHelper( )
1015 {
1016 if ( m_offset )
1017 m_gr->TranslateTransform( -0.5, -0.5 );
1018 }
1019public :
1020 Graphics* m_gr;
1021 bool m_offset;
1022} ;
1023
1024wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc )
1025 : wxGraphicsContext(renderer)
1026{
1027 Init();
1028 m_context = new Graphics( hdc);
1029 SetDefaults();
1030}
1031
1032wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd )
1033 : wxGraphicsContext(renderer)
1034{
1035 Init();
1036 m_context = new Graphics( hwnd);
1037 SetDefaults();
1038}
1039
1040wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr )
1041 : wxGraphicsContext(renderer)
1042{
1043 Init();
1044 m_context = gr;
1045 SetDefaults();
1046}
1047
1048wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL)
1049{
1050 Init();
1051}
1052
1053void wxGDIPlusContext::Init()
1054{
1055 m_context = NULL;
1056 m_state1 = 0;
1057 m_state2= 0;
1058}
1059
1060void wxGDIPlusContext::SetDefaults()
1061{
1062 m_context->SetTextRenderingHint(TextRenderingHintSystemDefault);
1063 m_context->SetPixelOffsetMode(PixelOffsetModeHalf);
1064 m_context->SetSmoothingMode(SmoothingModeHighQuality);
1065 m_state1 = m_context->Save();
1066 m_state2 = m_context->Save();
1067}
1068
1069wxGDIPlusContext::~wxGDIPlusContext()
1070{
1071 if ( m_context )
1072 {
1073 m_context->Restore( m_state2 );
1074 m_context->Restore( m_state1 );
1075 delete m_context;
1076 }
1077}
1078
1079
1080void wxGDIPlusContext::Clip( const wxRegion &region )
1081{
1082 Region rgn((HRGN)region.GetHRGN());
1083 m_context->SetClip(&rgn,CombineModeIntersect);
1084}
1085
1086void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1087{
1088 m_context->SetClip(RectF(x,y,w,h),CombineModeIntersect);
1089}
1090
1091void wxGDIPlusContext::ResetClip()
1092{
1093 m_context->ResetClip();
1094}
1095
1096void wxGDIPlusContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
1097{
1098 if ( !m_pen.IsNull() )
1099 {
1100 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1101 Point *cpoints = new Point[n];
1102 for (size_t i = 0; i < n; i++)
1103 {
1104 cpoints[i].X = (int)(points[i].m_x );
1105 cpoints[i].Y = (int)(points[i].m_y );
1106
1107 } // for (size_t i = 0; i < n; i++)
1108 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1109 delete[] cpoints;
1110 }
1111}
1112
1113void wxGDIPlusContext::DrawLines( size_t n, const wxPoint2DDouble *points, int WXUNUSED(fillStyle) )
1114{
1115 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1116 Point *cpoints = new Point[n];
1117 for (size_t i = 0; i < n; i++)
1118 {
1119 cpoints[i].X = (int)(points[i].m_x );
1120 cpoints[i].Y = (int)(points[i].m_y );
1121
1122 } // for (int i = 0; i < n; i++)
1123 if ( !m_brush.IsNull() )
1124 m_context->FillPolygon( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , cpoints , n ) ;
1125 if ( !m_pen.IsNull() )
1126 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1127 delete[] cpoints;
1128}
1129
1130void wxGDIPlusContext::StrokePath( const wxGraphicsPath& path )
1131{
1132 if ( !m_pen.IsNull() )
1133 {
1134 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1135 m_context->DrawPath( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath*) path.GetNativePath() );
1136 }
1137}
1138
1139void wxGDIPlusContext::FillPath( const wxGraphicsPath& path , int fillStyle )
1140{
1141 if ( !m_brush.IsNull() )
1142 {
1143 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1144 ((GraphicsPath*) path.GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
1145 m_context->FillPath( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() ,
1146 (GraphicsPath*) path.GetNativePath());
1147 }
1148}
1149
1150void wxGDIPlusContext::Rotate( wxDouble angle )
1151{
1152 m_context->RotateTransform( RadToDeg(angle) );
1153}
1154
1155void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
1156{
1157 m_context->TranslateTransform( dx , dy );
1158}
1159
1160void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
1161{
1162 m_context->ScaleTransform(xScale,yScale);
1163}
1164
1165void wxGDIPlusContext::PushState()
1166{
1167 GraphicsState state = m_context->Save();
1168 m_stateStack.push(state);
1169}
1170
1171void wxGDIPlusContext::PopState()
1172{
1173 GraphicsState state = m_stateStack.top();
1174 m_stateStack.pop();
1175 m_context->Restore(state);
1176}
1177
1178void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1179{
1180 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bmp.GetRefData())->GetGDIPlusBitmap();
1181 if ( image )
1182 {
1183 if( image->GetWidth() != (UINT) w || image->GetHeight() != (UINT) h )
1184 {
1185 Rect drawRect((REAL) x, (REAL)y, (REAL)w, (REAL)h);
1186 m_context->SetPixelOffsetMode( PixelOffsetModeNone );
1187 m_context->DrawImage(image, drawRect, 0 , 0 , image->GetWidth()-1, image->GetHeight()-1, UnitPixel ) ;
1188 m_context->SetPixelOffsetMode( PixelOffsetModeHalf );
1189 }
1190 else
1191 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1192 }
1193}
1194
1195void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1196{
1197 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1198 DrawBitmap(bitmap, x, y, w, h);
1199}
1200
1201void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1202{
1203 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1204 // find out by looking at the bitmap data whether there really was alpha in it
1205 HICON hIcon = (HICON)icon.GetHICON();
1206 ICONINFO iconInfo ;
1207 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1208 if (!GetIconInfo(hIcon,&iconInfo))
1209 return;
1210
1211 Bitmap interim(iconInfo.hbmColor,NULL);
1212
1213 Bitmap* image = NULL ;
1214
1215 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1216 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1217 if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 )
1218 {
1219 image = Bitmap::FromHICON(hIcon);
1220 }
1221 else
1222 {
1223 size_t width = interim.GetWidth();
1224 size_t height = interim.GetHeight();
1225 Rect bounds(0,0,width,height);
1226 BitmapData data ;
1227
1228 interim.LockBits(&bounds, ImageLockModeRead,
1229 interim.GetPixelFormat(),&data);
1230
1231 bool hasAlpha = false;
1232 for ( size_t y = 0 ; y < height && !hasAlpha ; ++y)
1233 {
1234 for( size_t x = 0 ; x < width && !hasAlpha; ++x)
1235 {
1236 ARGB *dest = (ARGB*)((BYTE*)data.Scan0 + data.Stride*y + x*4);
1237 if ( ( *dest & Color::AlphaMask ) != 0 )
1238 hasAlpha = true;
1239 }
1240 }
1241
1242 if ( hasAlpha )
1243 {
1244 image = new Bitmap(data.Width, data.Height, data.Stride,
1245 PixelFormat32bppARGB , (BYTE*) data.Scan0);
1246 }
1247 else
1248 {
1249 image = Bitmap::FromHICON(hIcon);
1250 }
1251
1252 interim.UnlockBits(&data);
1253 }
1254
1255 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1256
1257 delete image ;
1258 DeleteObject(iconInfo.hbmColor);
1259 DeleteObject(iconInfo.hbmMask);
1260}
1261
1262void wxGDIPlusContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1263{
1264 if ( m_font.IsNull() || str.IsEmpty())
1265 return ;
1266
1267 wxWCharBuffer s = str.wc_str( *wxConvUI );
1268 m_context->DrawString( s , -1 , ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont() ,
1269 PointF( x , y ) , StringFormat::GenericTypographic() , ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusBrush() );
1270}
1271
1272void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1273 wxDouble *descent, wxDouble *externalLeading ) const
1274{
1275 wxWCharBuffer s = str.wc_str( *wxConvUI );
1276 FontFamily ffamily ;
1277 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
1278
1279 f->GetFamily(&ffamily) ;
1280
1281 REAL factorY = m_context->GetDpiY() / 72.0 ;
1282
1283 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
1284 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1285 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
1286 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1287 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
1288 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1289
1290 if ( height )
1291 *height = rHeight * factorY;
1292 if ( descent )
1293 *descent = rDescent * factorY;
1294 if ( externalLeading )
1295 *externalLeading = (rHeight - rAscent - rDescent) * factorY;
1296 // measuring empty strings is not guaranteed, so do it by hand
1297 if ( str.IsEmpty())
1298 {
1299 if ( width )
1300 *width = 0 ;
1301 }
1302 else
1303 {
1304 RectF layoutRect(0,0, 100000.0f, 100000.0f);
1305 StringFormat strFormat( StringFormat::GenericTypographic() );
1306 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1307
1308 RectF bounds ;
1309 m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ;
1310 if ( width )
1311 *width = bounds.Width;
1312 }
1313}
1314
1315void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1316{
1317 widths.Empty();
1318 widths.Add(0, text.length());
1319
1320 if (text.empty())
1321 return;
1322
1323 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
1324 wxWCharBuffer ws = text.wc_str( *wxConvUI );
1325 size_t len = wcslen( ws ) ;
1326 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1327
1328 RectF layoutRect(0,0, 100000.0f, 100000.0f);
1329 StringFormat strFormat( StringFormat::GenericTypographic() );
1330
1331 CharacterRange* ranges = new CharacterRange[len] ;
1332 Region* regions = new Region[len];
1333 for( size_t i = 0 ; i < len ; ++i)
1334 {
1335 ranges[i].First = i ;
1336 ranges[i].Length = 1 ;
1337 }
1338 strFormat.SetMeasurableCharacterRanges(len,ranges);
1339 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1340 m_context->MeasureCharacterRanges(ws, -1 , f,layoutRect, &strFormat,1,regions) ;
1341
1342 RectF bbox ;
1343 for ( size_t i = 0 ; i < len ; ++i)
1344 {
1345 regions[i].GetBounds(&bbox,m_context);
1346 widths[i] = bbox.GetRight()-bbox.GetLeft();
1347 }
1348}
1349
1350bool wxGDIPlusContext::ShouldOffset() const
1351{
1352 int penwidth = 0 ;
1353 if ( !m_pen.IsNull() )
1354 {
1355 penwidth = (int)((wxGDIPlusPenData*)m_pen.GetRefData())->GetWidth();
1356 if ( penwidth == 0 )
1357 penwidth = 1;
1358 }
1359 return ( penwidth % 2 ) == 1;
1360}
1361
1362void* wxGDIPlusContext::GetNativeContext()
1363{
1364 return m_context;
1365}
1366
1367// concatenates this transform with the current transform of this context
1368void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1369{
1370 m_context->MultiplyTransform((Matrix*) matrix.GetNativeMatrix());
1371}
1372
1373// sets the transform of this context
1374void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix& matrix )
1375{
1376 m_context->SetTransform((Matrix*) matrix.GetNativeMatrix());
1377}
1378
1379// gets the matrix of this context
1380wxGraphicsMatrix wxGDIPlusContext::GetTransform() const
1381{
1382 wxGraphicsMatrix matrix = CreateMatrix();
1383 m_context->GetTransform((Matrix*) matrix.GetNativeMatrix());
1384 return matrix;
1385}
1386//-----------------------------------------------------------------------------
1387// wxGDIPlusRenderer declaration
1388//-----------------------------------------------------------------------------
1389
1390class wxGDIPlusRenderer : public wxGraphicsRenderer
1391{
1392public :
1393 wxGDIPlusRenderer()
1394 {
1395 m_loaded = -1;
1396 m_gditoken = 0;
1397 }
1398
1399 virtual ~wxGDIPlusRenderer()
1400 {
1401 if ( m_loaded == 1 )
1402 {
1403 Unload();
1404 }
1405 }
1406
1407 // Context
1408
1409 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1410
1411 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1412
1413 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
1414
1415 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1416
1417 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1418
1419 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1420
1421 virtual wxGraphicsContext * CreateMeasuringContext();
1422
1423 // Path
1424
1425 virtual wxGraphicsPath CreatePath();
1426
1427 // Matrix
1428
1429 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1430 wxDouble tx=0.0, wxDouble ty=0.0);
1431
1432
1433 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1434
1435 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1436
1437 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1438 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1439 const wxColour&c1, const wxColour&c2) ;
1440
1441 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1442 // with radius r and color cColor
1443 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1444 const wxColour &oColor, const wxColour &cColor) ;
1445
1446 // sets the font
1447 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1448
1449 // create a native bitmap representation
1450 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
1451
1452 // create a subimage from a native image representation
1453 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1454
1455protected :
1456 bool EnsureIsLoaded();
1457 void Load();
1458 void Unload();
1459 friend class wxGDIPlusRendererModule;
1460
1461private :
1462 int m_loaded;
1463 ULONG_PTR m_gditoken;
1464
1465 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer)
1466} ;
1467
1468//-----------------------------------------------------------------------------
1469// wxGDIPlusRenderer implementation
1470//-----------------------------------------------------------------------------
1471
1472IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer,wxGraphicsRenderer)
1473
1474static wxGDIPlusRenderer gs_GDIPlusRenderer;
1475
1476wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1477{
1478 return &gs_GDIPlusRenderer;
1479}
1480
1481bool wxGDIPlusRenderer::EnsureIsLoaded()
1482{
1483 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1484 // if we already tried and failed (we don't want to spend lot of time
1485 // returning NULL from wxGraphicsContext::Create(), which may be called
1486 // relatively frequently):
1487 if ( m_loaded == -1 )
1488 {
1489 Load();
1490 }
1491
1492 return m_loaded == 1;
1493}
1494
1495// call EnsureIsLoaded() and return returnOnFail value if it fails
1496#define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1497 if ( !EnsureIsLoaded() ) \
1498 return (returnOnFail)
1499
1500
1501void wxGDIPlusRenderer::Load()
1502{
1503 GdiplusStartupInput input;
1504 GdiplusStartupOutput output;
1505 if ( GdiplusStartup(&m_gditoken,&input,&output) == Gdiplus::Ok )
1506 {
1507 wxLogTrace("gdiplus", "successfully initialized GDI+");
1508 m_loaded = 1;
1509 }
1510 else
1511 {
1512 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1513 m_loaded = 0;
1514 }
1515}
1516
1517void wxGDIPlusRenderer::Unload()
1518{
1519 if ( m_gditoken )
1520 {
1521 GdiplusShutdown(m_gditoken);
1522 m_gditoken = NULL;
1523 }
1524 m_loaded = -1; // next Load() will try again
1525}
1526
1527wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxWindowDC& dc)
1528{
1529 ENSURE_LOADED_OR_RETURN(NULL);
1530 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1531 return new wxGDIPlusContext(this,(HDC) msw->GetHDC());
1532}
1533
1534wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxPrinterDC& dc)
1535{
1536 ENSURE_LOADED_OR_RETURN(NULL);
1537 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1538 return new wxGDIPlusContext(this,(HDC) msw->GetHDC());
1539}
1540
1541wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxMemoryDC& dc)
1542{
1543 ENSURE_LOADED_OR_RETURN(NULL);
1544 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1545 return new wxGDIPlusContext(this,(HDC) msw->GetHDC());
1546}
1547
1548wxGraphicsContext * wxGDIPlusRenderer::CreateMeasuringContext()
1549{
1550 ENSURE_LOADED_OR_RETURN(NULL);
1551 return new wxGDIPlusMeasuringContext(this);
1552}
1553
1554wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeContext( void * context )
1555{
1556 ENSURE_LOADED_OR_RETURN(NULL);
1557 return new wxGDIPlusContext(this,(Graphics*) context);
1558}
1559
1560
1561wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window )
1562{
1563 ENSURE_LOADED_OR_RETURN(NULL);
1564 return new wxGDIPlusContext(this,(HWND) window);
1565}
1566
1567wxGraphicsContext * wxGDIPlusRenderer::CreateContext( wxWindow* window )
1568{
1569 ENSURE_LOADED_OR_RETURN(NULL);
1570 return new wxGDIPlusContext(this, (HWND) window->GetHWND() );
1571}
1572
1573// Path
1574
1575wxGraphicsPath wxGDIPlusRenderer::CreatePath()
1576{
1577 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath);
1578 wxGraphicsPath m;
1579 m.SetRefData( new wxGDIPlusPathData(this));
1580 return m;
1581}
1582
1583
1584// Matrix
1585
1586wxGraphicsMatrix wxGDIPlusRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1587 wxDouble tx, wxDouble ty)
1588
1589{
1590 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix);
1591 wxGraphicsMatrix m;
1592 wxGDIPlusMatrixData* data = new wxGDIPlusMatrixData( this );
1593 data->Set( a,b,c,d,tx,ty ) ;
1594 m.SetRefData(data);
1595 return m;
1596}
1597
1598wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen)
1599{
1600 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen);
1601 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1602 return wxNullGraphicsPen;
1603 else
1604 {
1605 wxGraphicsPen p;
1606 p.SetRefData(new wxGDIPlusPenData( this, pen ));
1607 return p;
1608 }
1609}
1610
1611wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush )
1612{
1613 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
1614 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1615 return wxNullGraphicsBrush;
1616 else
1617 {
1618 wxGraphicsBrush p;
1619 p.SetRefData(new wxGDIPlusBrushData( this, brush ));
1620 return p;
1621 }
1622}
1623
1624// sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1625wxGraphicsBrush wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1626 const wxColour&c1, const wxColour&c2)
1627{
1628 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
1629 wxGraphicsBrush p;
1630 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
1631 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1632 p.SetRefData(d);
1633 return p;
1634 }
1635
1636// sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1637// with radius r and color cColor
1638wxGraphicsBrush wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1639 const wxColour &oColor, const wxColour &cColor)
1640{
1641 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
1642 wxGraphicsBrush p;
1643 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
1644 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1645 p.SetRefData(d);
1646 return p;
1647}
1648
1649// sets the font
1650wxGraphicsFont wxGDIPlusRenderer::CreateFont( const wxFont &font , const wxColour &col )
1651{
1652 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
1653 if ( font.Ok() )
1654 {
1655 wxGraphicsFont p;
1656 p.SetRefData(new wxGDIPlusFontData( this , font, col ));
1657 return p;
1658 }
1659 else
1660 return wxNullGraphicsFont;
1661}
1662
1663wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmap( const wxBitmap &bitmap )
1664{
1665 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
1666 if ( bitmap.Ok() )
1667 {
1668 wxGraphicsBitmap p;
1669 p.SetRefData(new wxGDIPlusBitmapData( this , bitmap ));
1670 return p;
1671 }
1672 else
1673 return wxNullGraphicsBitmap;
1674}
1675
1676wxGraphicsBitmap wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1677{
1678 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
1679 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bitmap.GetRefData())->GetGDIPlusBitmap();
1680 if ( image )
1681 {
1682 wxGraphicsBitmap p;
1683 p.SetRefData(new wxGDIPlusBitmapData( this , image->Clone( (REAL) x , (REAL) y , (REAL) w , (REAL) h , PixelFormat32bppPARGB) ));
1684 return p;
1685 }
1686 else
1687 return wxNullGraphicsBitmap;
1688}
1689
1690// Shutdown GDI+ at app exit, before possible dll unload
1691class wxGDIPlusRendererModule : public wxModule
1692{
1693public:
1694 virtual bool OnInit() { return true; }
1695 virtual void OnExit() { gs_GDIPlusRenderer.Unload(); }
1696
1697private:
1698 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule)
1699};
1700
1701IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule)
1702
1703#endif // wxUSE_GRAPHICS_CONTEXT