]> git.saurik.com Git - wxWidgets.git/blame - src/msw/graphics.cpp
Don't consider fully opaque bitmaps as having alpha in wxMSW.
[wxWidgets.git] / src / msw / graphics.cpp
CommitLineData
6fea499c 1/////////////////////////////////////////////////////////////////////////////
e0876d73 2// Name: src/msw/graphics.cpp
6fea499c
SC
3// Purpose: wxGCDC class
4// Author: Stefan Csomor
5// Modified by:
e0876d73 6// Created: 2006-09-30
6fea499c 7// RCS-ID: $Id$
e0876d73 8// Copyright: (c) 2006 Stefan Csomor
6fea499c
SC
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
6fea499c 14#ifdef __BORLANDC__
b4715d08 15 #pragma hdrstop
6fea499c
SC
16#endif
17
b4715d08
VZ
18#include "wx/dc.h"
19
0024ec50
VZ
20#if wxUSE_GRAPHICS_CONTEXT
21
6fea499c 22#ifndef WX_PRECOMP
bdba6fdc
VZ
23 #include "wx/msw/wrapcdlg.h"
24 #include "wx/image.h"
25 #include "wx/window.h"
bdba6fdc
VZ
26 #include "wx/utils.h"
27 #include "wx/dialog.h"
28 #include "wx/app.h"
29 #include "wx/bitmap.h"
bdba6fdc
VZ
30 #include "wx/log.h"
31 #include "wx/icon.h"
bdba6fdc 32 #include "wx/module.h"
f772729b
SC
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"
6fea499c
SC
38#endif
39
623cf1fa 40#include "wx/private/graphics.h"
bdba6fdc 41#include "wx/msw/wrapgdip.h"
bce28872 42#include "wx/msw/dc.h"
c929ad91
VZ
43#if wxUSE_ENH_METAFILE
44 #include "wx/msw/enhmeta.h"
45#endif
7fa3cf6a 46#include "wx/dcgraph.h"
6fea499c 47
f49c80c1
VZ
48#include "wx/msw/private.h" // needs to be before #include <commdlg.h>
49
50#if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
51#include <commdlg.h>
52#endif
53
bdba6fdc 54#include "wx/stack.h"
6fea499c 55
f49c80c1
VZ
56namespace
57{
58
6fea499c
SC
59//-----------------------------------------------------------------------------
60// constants
61//-----------------------------------------------------------------------------
62
63const double RAD2DEG = 180.0 / M_PI;
64
65//-----------------------------------------------------------------------------
66// Local functions
67//-----------------------------------------------------------------------------
68
f49c80c1
VZ
69inline double dmin(double a, double b) { return a < b ? a : b; }
70inline double dmax(double a, double b) { return a > b ? a : b; }
71
72inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
73inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; }
6fea499c 74
f49c80c1
VZ
75// translate a wxColour to a Color
76inline Color wxColourToColor(const wxColour& col)
77{
78 return Color(col.Alpha(), col.Red(), col.Green(), col.Blue());
79}
80
81} // anonymous namespace
6fea499c
SC
82
83//-----------------------------------------------------------------------------
84// device context implementation
85//
86// more and more of the dc functionality should be implemented by calling
87// the appropricate wxGDIPlusContext, but we will have to do that step by step
88// also coordinate conversions should be moved to native matrix ops
89//-----------------------------------------------------------------------------
90
91// we always stock two context states, one at entry, to be able to preserve the
92// state we were called with, the other one after changing to HI Graphics orientation
93// (this one is used for getting back clippings etc)
94
95//-----------------------------------------------------------------------------
96// wxGraphicsPath implementation
97//-----------------------------------------------------------------------------
98
eb40c2d8
JS
99class wxGDIPlusContext;
100
eb4083ef 101class wxGDIPlusPathData : public wxGraphicsPathData
6fea499c 102{
6fea499c 103public :
a4e73390
SC
104 wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path = NULL);
105 ~wxGDIPlusPathData();
6fea499c 106
a4e73390 107 virtual wxGraphicsObjectRefData *Clone() const;
6fea499c
SC
108
109 //
110 // These are the path primitives from which everything else can be constructed
111 //
112
113 // begins a new subpath at (x,y)
114 virtual void MoveToPoint( wxDouble x, wxDouble y );
115
cb3a0d42 116 // adds a straight line from the current point to (x,y)
6fea499c
SC
117 virtual void AddLineToPoint( wxDouble x, wxDouble y );
118
119 // adds a cubic Bezier curve from the current point, using two control points and an end point
120 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
121
122
cb3a0d42 123 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
6fea499c
SC
124 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
125
126 // gets the last point of the current path, (0,0) if not yet set
a4e73390 127 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
6fea499c 128
83576e68 129 // adds another path
a4e73390 130 virtual void AddPath( const wxGraphicsPathData* path );
83576e68 131
6fea499c
SC
132 // closes the current sub-path
133 virtual void CloseSubpath();
134
135 //
cb3a0d42 136 // These are convenience functions which - if not available natively will be assembled
6fea499c
SC
137 // using the primitives from above
138 //
139
cb3a0d42 140 // appends a rectangle as a new closed subpath
6fea499c
SC
141 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
142 /*
143
144 // appends an ellipsis as a new closed subpath fitting the passed rectangle
145 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
146
147 // draws a an arc to two tangents connecting (current) to (x1,y1) and (x1,y1) to (x2,y2), also a straight line from (current) to (x1,y1)
148 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
149*/
150
cb3a0d42
VZ
151 // returns the native path
152 virtual void * GetNativePath() const { return m_path; }
153
154 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
155 virtual void UnGetNativePath(void * WXUNUSED(path)) const {}
f540e5bd 156
83576e68 157 // transforms each point of this path by the matrix
a4e73390 158 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
83576e68
SC
159
160 // gets the bounding box enclosing all points (possibly including control points)
a4e73390 161 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
83576e68 162
94a007ec 163 virtual bool Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle = wxODDEVEN_RULE) const;
83576e68 164
6fea499c
SC
165private :
166 GraphicsPath* m_path;
167};
168
eb4083ef 169class wxGDIPlusMatrixData : public wxGraphicsMatrixData
83576e68
SC
170{
171public :
a4e73390
SC
172 wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix = NULL) ;
173 virtual ~wxGDIPlusMatrixData() ;
83576e68 174
a4e73390 175 virtual wxGraphicsObjectRefData* Clone() const ;
83576e68
SC
176
177 // concatenates the matrix
a4e73390 178 virtual void Concat( const wxGraphicsMatrixData *t );
83576e68
SC
179
180 // sets the matrix to the respective values
cb3a0d42 181 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
83576e68
SC
182 wxDouble tx=0.0, wxDouble ty=0.0);
183
248802d0
RD
184 // gets the component valuess of the matrix
185 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
186 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
f4322df6 187
83576e68
SC
188 // makes this the inverse matrix
189 virtual void Invert();
190
191 // returns true if the elements of the transformation matrix are equal ?
a4e73390 192 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
83576e68
SC
193
194 // return true if this is the identity matrix
a4e73390 195 virtual bool IsIdentity() const;
83576e68
SC
196
197 //
198 // transformation
199 //
200
201 // add the translation to this matrix
202 virtual void Translate( wxDouble dx , wxDouble dy );
203
204 // add the scale to this matrix
205 virtual void Scale( wxDouble xScale , wxDouble yScale );
206
207 // add the rotation to this matrix (radians)
cb3a0d42 208 virtual void Rotate( wxDouble angle );
83576e68
SC
209
210 //
211 // apply the transforms
212 //
213
214 // applies that matrix to the point
a4e73390 215 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
83576e68
SC
216
217 // applies the matrix except for translations
a4e73390 218 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
83576e68
SC
219
220 // returns the native representation
221 virtual void * GetNativeMatrix() const;
222private:
223 Matrix* m_matrix ;
83576e68
SC
224} ;
225
eb4083ef 226class wxGDIPlusPenData : public wxGraphicsObjectRefData
83576e68
SC
227{
228public:
2c820406
SC
229 wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
230 ~wxGDIPlusPenData();
83576e68
SC
231
232 void Init();
233
83576e68
SC
234 virtual wxDouble GetWidth() { return m_width; }
235 virtual Pen* GetGDIPlusPen() { return m_pen; }
236
237protected :
238 Pen* m_pen;
239 Image* m_penImage;
240 Brush* m_penBrush;
241
242 wxDouble m_width;
83576e68
SC
243};
244
eb4083ef 245class wxGDIPlusBrushData : public wxGraphicsObjectRefData
83576e68
SC
246{
247public:
2c820406
SC
248 wxGDIPlusBrushData( wxGraphicsRenderer* renderer );
249 wxGDIPlusBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
250 ~wxGDIPlusBrushData ();
83576e68 251
4ee4c7b9
VZ
252 void CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
253 wxDouble x2, wxDouble y2,
254 const wxGraphicsGradientStops& stops);
255 void CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
256 wxDouble xc, wxDouble yc,
257 wxDouble radius,
258 const wxGraphicsGradientStops& stops);
259
83576e68
SC
260 virtual Brush* GetGDIPlusBrush() { return m_brush; }
261
262protected:
263 virtual void Init();
264
4ee4c7b9
VZ
265private:
266 // common part of Create{Linear,Radial}GradientBrush()
267 template <typename T>
268 void SetGradientStops(T *brush, const wxGraphicsGradientStops& stops);
269
83576e68
SC
270 Brush* m_brush;
271 Image* m_brushImage;
272 GraphicsPath* m_brushPath;
83576e68
SC
273};
274
bce28872
SC
275class WXDLLIMPEXP_CORE wxGDIPlusBitmapData : public wxGraphicsObjectRefData
276{
277public:
278 wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap );
279 wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, const wxBitmap &bmp );
280 ~wxGDIPlusBitmapData ();
281
282 virtual Bitmap* GetGDIPlusBitmap() { return m_bitmap; }
283
284private :
285 Bitmap* m_bitmap;
286 Bitmap* m_helper;
287};
288
eb4083ef 289class wxGDIPlusFontData : public wxGraphicsObjectRefData
83576e68
SC
290{
291public:
eb40c2d8
JS
292 wxGDIPlusFontData( wxGraphicsRenderer* renderer,
293 const wxGDIPlusContext* gc,
294 const wxFont &font,
295 const wxColour& col );
2c820406 296 ~wxGDIPlusFontData();
83576e68 297
83576e68
SC
298 virtual Brush* GetGDIPlusBrush() { return m_textBrush; }
299 virtual Font* GetGDIPlusFont() { return m_font; }
300private :
301 Brush* m_textBrush;
302 Font* m_font;
83576e68
SC
303};
304
eb4083ef 305class wxGDIPlusContext : public wxGraphicsContext
6fea499c 306{
6fea499c 307public:
eb40c2d8
JS
308 wxGDIPlusContext( wxGraphicsRenderer* renderer, const wxDC& dc );
309 wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc, wxDouble width, wxDouble height );
83576e68
SC
310 wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd );
311 wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr);
6fea499c 312 wxGDIPlusContext();
9f339b18 313
6fea499c
SC
314 virtual ~wxGDIPlusContext();
315
316 virtual void Clip( const wxRegion &region );
539e2795
SC
317 // clips drawings to the rect
318 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
539e2795 319
cb3a0d42
VZ
320 // resets the clipping to original extent
321 virtual void ResetClip();
322
323 virtual void * GetNativeContext();
324
a4e73390 325 virtual void StrokePath( const wxGraphicsPath& p );
94a007ec 326 virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
6fea499c 327
03647350 328 // stroke lines connecting each of the points
0c7bd159
SC
329 virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
330
331 // draws a polygon
94a007ec 332 virtual void DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
0c7bd159 333
bf02a7f9
SC
334 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
335
336 virtual bool SetCompositionMode(wxCompositionMode op);
337
338 virtual void BeginLayer(wxDouble opacity);
339
340 virtual void EndLayer();
03647350 341
cb3a0d42
VZ
342 virtual void Translate( wxDouble dx , wxDouble dy );
343 virtual void Scale( wxDouble xScale , wxDouble yScale );
344 virtual void Rotate( wxDouble angle );
6fea499c 345
83576e68 346 // concatenates this transform with the current transform of this context
a4e73390 347 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
83576e68
SC
348
349 // sets the transform of this context
a4e73390 350 virtual void SetTransform( const wxGraphicsMatrix& matrix );
83576e68
SC
351
352 // gets the matrix of this context
a4e73390 353 virtual wxGraphicsMatrix GetTransform() const;
83576e68 354
bce28872 355 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
6fea499c
SC
356 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
357 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
cb3a0d42 358 virtual void PushState();
6fea499c
SC
359 virtual void PopState();
360
eb40c2d8
JS
361 // sets the font of this context
362 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) const;
363
6fea499c
SC
364 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
365 wxDouble *descent, wxDouble *externalLeading ) const;
366 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
d9485f89 367 virtual bool ShouldOffset() const;
fbd5416f 368 virtual void GetSize( wxDouble* width, wxDouble *height );
6fea499c 369
eb40c2d8
JS
370 Graphics* GetGraphics() const { return m_context; }
371
372protected:
373
374 wxDouble m_fontScaleRatio;
375
6fea499c 376private:
9f339b18
SC
377 void Init();
378 void SetDefaults();
379
0b7dce54
VZ
380 virtual void DoDrawText(const wxString& str, wxDouble x, wxDouble y)
381 { DoDrawFilledText(str, x, y, wxNullGraphicsBrush); }
382 virtual void DoDrawFilledText(const wxString& str, wxDouble x, wxDouble y,
383 const wxGraphicsBrush& backgroundBrush);
384
83576e68 385 Graphics* m_context;
b1bf7dc7 386 wxStack<GraphicsState> m_stateStack;
83576e68
SC
387 GraphicsState m_state1;
388 GraphicsState m_state2;
389
81e0b3d9
SC
390 wxDouble m_width;
391 wxDouble m_height;
392
83576e68
SC
393 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext)
394};
395
5aac6f3f 396class wxGDIPlusMeasuringContext : public wxGDIPlusContext
0c7bd159
SC
397{
398public:
81e0b3d9 399 wxGDIPlusMeasuringContext( wxGraphicsRenderer* renderer ) : wxGDIPlusContext( renderer , m_hdc = GetDC(NULL), 1000, 1000 )
0c7bd159
SC
400 {
401 }
402 wxGDIPlusMeasuringContext()
403 {
404 }
405
406 virtual ~wxGDIPlusMeasuringContext()
407 {
408 ReleaseDC( NULL, m_hdc );
409 }
410
411private:
7fa3cf6a 412 HDC m_hdc ;
0c7bd159
SC
413 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext)
414} ;
415
eb40c2d8
JS
416class wxGDIPlusPrintingContext : public wxGDIPlusContext
417{
418public:
419 wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer, const wxDC& dc );
420 virtual ~wxGDIPlusPrintingContext() { }
421protected:
422};
423
424//-----------------------------------------------------------------------------
425// wxGDIPlusRenderer declaration
426//-----------------------------------------------------------------------------
427
428class wxGDIPlusRenderer : public wxGraphicsRenderer
429{
430public :
431 wxGDIPlusRenderer()
432 {
433 m_loaded = -1;
434 m_gditoken = 0;
435 }
436
437 virtual ~wxGDIPlusRenderer()
438 {
439 if ( m_loaded == 1 )
440 {
441 Unload();
442 }
443 }
444
445 // Context
446
447 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
448
449 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
450
451#if wxUSE_PRINTING_ARCHITECTURE
452 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
453#endif
454
455#if wxUSE_ENH_METAFILE
456 virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc);
457#endif
458
459 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
460
461 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
462
463 virtual wxGraphicsContext * CreateContext( wxWindow* window );
464
465 virtual wxGraphicsContext * CreateMeasuringContext();
466
467 // Path
468
469 virtual wxGraphicsPath CreatePath();
470
471 // Matrix
472
473 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
474 wxDouble tx=0.0, wxDouble ty=0.0);
475
476
477 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
478
479 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
480
481 virtual wxGraphicsBrush
482 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
483 wxDouble x2, wxDouble y2,
484 const wxGraphicsGradientStops& stops);
485
486 virtual wxGraphicsBrush
487 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
488 wxDouble xc, wxDouble yc,
489 wxDouble radius,
490 const wxGraphicsGradientStops& stops);
491
492 // create a native bitmap representation
493 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
494
495 // stub: should not be called directly
496 virtual wxGraphicsFont CreateFont( const wxFont& WXUNUSED(font),
497 const wxColour& WXUNUSED(col) )
498 { wxFAIL; return wxNullGraphicsFont; }
499
500 // this is used to really create the font
501 wxGraphicsFont CreateGDIPlusFont( const wxGDIPlusContext* gc,
502 const wxFont &font,
503 const wxColour &col );
504
505 // create a graphics bitmap from a native bitmap
506 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
507
508 // create a subimage from a native image representation
509 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
510
511protected :
512 bool EnsureIsLoaded();
513 void Load();
514 void Unload();
515 friend class wxGDIPlusRendererModule;
516
517private :
518 int m_loaded;
519 ULONG_PTR m_gditoken;
520
521 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer)
522} ;
523
83576e68
SC
524//-----------------------------------------------------------------------------
525// wxGDIPlusPen implementation
526//-----------------------------------------------------------------------------
527
2c820406 528wxGDIPlusPenData::~wxGDIPlusPenData()
83576e68
SC
529{
530 delete m_pen;
531 delete m_penImage;
532 delete m_penBrush;
533}
534
2c820406 535void wxGDIPlusPenData::Init()
83576e68
SC
536{
537 m_pen = NULL ;
538 m_penImage = NULL;
539 m_penBrush = NULL;
540}
541
2c820406
SC
542wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
543: wxGraphicsObjectRefData(renderer)
cb3a0d42 544{
83576e68
SC
545 Init();
546 m_width = pen.GetWidth();
547 if (m_width <= 0.0)
548 m_width = 0.1;
549
f49c80c1 550 m_pen = new Pen(wxColourToColor(pen.GetColour()), m_width );
83576e68
SC
551
552 LineCap cap;
553 switch ( pen.GetCap() )
554 {
555 case wxCAP_ROUND :
556 cap = LineCapRound;
557 break;
558
559 case wxCAP_PROJECTING :
560 cap = LineCapSquare;
561 break;
562
563 case wxCAP_BUTT :
564 cap = LineCapFlat; // TODO verify
565 break;
566
567 default :
568 cap = LineCapFlat;
569 break;
570 }
571 m_pen->SetLineCap(cap,cap, DashCapFlat);
572
573 LineJoin join;
574 switch ( pen.GetJoin() )
575 {
576 case wxJOIN_BEVEL :
577 join = LineJoinBevel;
578 break;
579
580 case wxJOIN_MITER :
581 join = LineJoinMiter;
582 break;
583
584 case wxJOIN_ROUND :
585 join = LineJoinRound;
586 break;
587
588 default :
589 join = LineJoinMiter;
590 break;
591 }
592
593 m_pen->SetLineJoin(join);
594
595 m_pen->SetDashStyle(DashStyleSolid);
596
597 DashStyle dashStyle = DashStyleSolid;
598 switch ( pen.GetStyle() )
599 {
600 case wxSOLID :
601 break;
602
603 case wxDOT :
604 dashStyle = DashStyleDot;
605 break;
606
607 case wxLONG_DASH :
608 dashStyle = DashStyleDash; // TODO verify
609 break;
610
611 case wxSHORT_DASH :
612 dashStyle = DashStyleDash;
613 break;
614
615 case wxDOT_DASH :
616 dashStyle = DashStyleDashDot;
617 break;
618 case wxUSER_DASH :
619 {
620 dashStyle = DashStyleCustom;
621 wxDash *dashes;
622 int count = pen.GetDashes( &dashes );
623 if ((dashes != NULL) && (count > 0))
624 {
625 REAL *userLengths = new REAL[count];
626 for ( int i = 0; i < count; ++i )
627 {
628 userLengths[i] = dashes[i];
629 }
630 m_pen->SetDashPattern( userLengths, count);
631 delete[] userLengths;
632 }
633 }
634 break;
635 case wxSTIPPLE :
636 {
637 wxBitmap* bmp = pen.GetStipple();
638 if ( bmp && bmp->Ok() )
639 {
e38f5435
DS
640 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
641#if wxUSE_PALETTE
642 (HPALETTE)bmp->GetPalette()->GetHPALETTE()
643#else
644 NULL
645#endif
646 );
83576e68
SC
647 m_penBrush = new TextureBrush(m_penImage);
648 m_pen->SetBrush( m_penBrush );
649 }
650
651 }
652 break;
653 default :
654 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
655 {
656 HatchStyle style = HatchStyleHorizontal;
657 switch( pen.GetStyle() )
658 {
659 case wxBDIAGONAL_HATCH :
660 style = HatchStyleBackwardDiagonal;
661 break ;
662 case wxCROSSDIAG_HATCH :
663 style = HatchStyleDiagonalCross;
664 break ;
665 case wxFDIAGONAL_HATCH :
666 style = HatchStyleForwardDiagonal;
667 break ;
668 case wxCROSS_HATCH :
669 style = HatchStyleCross;
670 break ;
671 case wxHORIZONTAL_HATCH :
672 style = HatchStyleHorizontal;
673 break ;
674 case wxVERTICAL_HATCH :
675 style = HatchStyleVertical;
676 break ;
677
678 }
f49c80c1
VZ
679 m_penBrush = new HatchBrush
680 (
681 style,
682 wxColourToColor(pen.GetColour()),
683 Color::Transparent
684 );
83576e68
SC
685 m_pen->SetBrush( m_penBrush );
686 }
687 break;
cb3a0d42 688 }
83576e68
SC
689 if ( dashStyle != DashStyleSolid )
690 m_pen->SetDashStyle(dashStyle);
691}
692
83576e68
SC
693//-----------------------------------------------------------------------------
694// wxGDIPlusBrush implementation
695//-----------------------------------------------------------------------------
696
2c820406
SC
697wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer )
698: wxGraphicsObjectRefData(renderer)
83576e68
SC
699{
700 Init();
701}
702
2c820406
SC
703wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer , const wxBrush &brush )
704: wxGraphicsObjectRefData(renderer)
83576e68
SC
705{
706 Init();
707 if ( brush.GetStyle() == wxSOLID)
708 {
f49c80c1 709 m_brush = new SolidBrush(wxColourToColor( brush.GetColour()));
83576e68
SC
710 }
711 else if ( brush.IsHatch() )
712 {
713 HatchStyle style = HatchStyleHorizontal;
714 switch( brush.GetStyle() )
715 {
716 case wxBDIAGONAL_HATCH :
717 style = HatchStyleBackwardDiagonal;
718 break ;
719 case wxCROSSDIAG_HATCH :
720 style = HatchStyleDiagonalCross;
721 break ;
722 case wxFDIAGONAL_HATCH :
723 style = HatchStyleForwardDiagonal;
724 break ;
725 case wxCROSS_HATCH :
726 style = HatchStyleCross;
727 break ;
728 case wxHORIZONTAL_HATCH :
729 style = HatchStyleHorizontal;
730 break ;
731 case wxVERTICAL_HATCH :
732 style = HatchStyleVertical;
733 break ;
734
735 }
f49c80c1
VZ
736 m_brush = new HatchBrush
737 (
738 style,
739 wxColourToColor(brush.GetColour()),
740 Color::Transparent
741 );
83576e68 742 }
cb3a0d42 743 else
83576e68
SC
744 {
745 wxBitmap* bmp = brush.GetStipple();
746 if ( bmp && bmp->Ok() )
747 {
748 wxDELETE( m_brushImage );
e38f5435
DS
749 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
750#if wxUSE_PALETTE
751 (HPALETTE)bmp->GetPalette()->GetHPALETTE()
752#else
753 NULL
754#endif
755 );
83576e68
SC
756 m_brush = new TextureBrush(m_brushImage);
757 }
758 }
759}
760
2c820406 761wxGDIPlusBrushData::~wxGDIPlusBrushData()
83576e68
SC
762{
763 delete m_brush;
764 delete m_brushImage;
765 delete m_brushPath;
766};
767
2c820406 768void wxGDIPlusBrushData::Init()
83576e68
SC
769{
770 m_brush = NULL;
771 m_brushImage= NULL;
772 m_brushPath= NULL;
773}
774
4ee4c7b9
VZ
775template <typename T>
776void
777wxGDIPlusBrushData::SetGradientStops(T *brush,
778 const wxGraphicsGradientStops& stops)
779{
780 const unsigned numStops = stops.GetCount();
781 if ( numStops <= 2 )
782 {
783 // initial and final colours are set during the brush creation, nothing
784 // more to do
785 return;
786 }
787
788 wxVector<Color> colors(numStops);
789 wxVector<REAL> positions(numStops);
790
791 for ( unsigned i = 0; i < numStops; i++ )
792 {
793 wxGraphicsGradientStop stop = stops.Item(i);
794
795 colors[i] = wxColourToColor(stop.GetColour());
796 positions[i] = stop.GetPosition();
797 }
798
799 brush->SetInterpolationColors(&colors[0], &positions[0], numStops);
800}
801
802void
803wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
804 wxDouble x2, wxDouble y2,
805 const wxGraphicsGradientStops& stops)
83576e68 806{
4ee4c7b9
VZ
807 LinearGradientBrush * const
808 brush = new LinearGradientBrush(PointF(x1, y1) , PointF(x2, y2),
809 wxColourToColor(stops.GetStartColour()),
810 wxColourToColor(stops.GetEndColour()));
811 m_brush = brush;
812
813 SetGradientStops(brush, stops);
83576e68
SC
814}
815
4ee4c7b9
VZ
816void
817wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
818 wxDouble xc, wxDouble yc,
819 wxDouble radius,
820 const wxGraphicsGradientStops& stops)
83576e68 821{
83576e68 822 m_brushPath = new GraphicsPath();
4ee4c7b9
VZ
823 m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius),
824 (REAL)(2*radius), (REAL)(2*radius));
83576e68 825
4ee4c7b9
VZ
826 PathGradientBrush * const brush = new PathGradientBrush(m_brushPath);
827 m_brush = brush;
828 brush->SetCenterPoint(PointF(xo, yo));
829 brush->SetCenterColor(wxColourToColor(stops.GetStartColour()));
83576e68 830
4ee4c7b9 831 const Color col(wxColourToColor(stops.GetEndColour()));
83576e68 832 int count = 1;
4ee4c7b9
VZ
833 brush->SetSurroundColors(&col, &count);
834
835 SetGradientStops(brush, stops);
83576e68
SC
836}
837
83576e68
SC
838//-----------------------------------------------------------------------------
839// wxGDIPlusFont implementation
840//-----------------------------------------------------------------------------
841
eb40c2d8
JS
842wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer* renderer,
843 const wxGDIPlusContext* gc,
844 const wxFont &font,
845 const wxColour& col )
846 : wxGraphicsObjectRefData( renderer )
83576e68 847{
83576e68 848 wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI );
83576e68
SC
849 int style = FontStyleRegular;
850 if ( font.GetStyle() == wxFONTSTYLE_ITALIC )
851 style |= FontStyleItalic;
852 if ( font.GetUnderlined() )
853 style |= FontStyleUnderline;
854 if ( font.GetWeight() == wxFONTWEIGHT_BOLD )
855 style |= FontStyleBold;
eb40c2d8
JS
856
857 Graphics* context = gc->GetGraphics();
858
859 Unit fontUnit = context->GetPageUnit();
860 // if fontUnit is UnitDisplay, then specify UnitPixel, otherwise
861 // you'll get a "InvalidParameter" from GDI+
862 if ( fontUnit == UnitDisplay )
863 fontUnit = UnitPixel;
864
865 REAL points = font.GetPointSize();
866
867 // This scaling is needed when we use unit other than the
868 // default UnitPoint. It works for both display and printing.
869 REAL size = points * (100.0 / 72.0);
870
871 // NB: font unit should match context's unit. We can use UnitPixel,
872 // as that is what the print context should use.
873 m_font = new Font( s, size, style, fontUnit );
874
f49c80c1 875 m_textBrush = new SolidBrush(wxColourToColor(col));
83576e68 876}
6fea499c 877
2c820406 878wxGDIPlusFontData::~wxGDIPlusFontData()
83576e68
SC
879{
880 delete m_textBrush;
881 delete m_font;
882}
6fea499c 883
bce28872
SC
884// the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
885// premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
886// bytes as parameter, since there is no real copying of the data going in, only references are stored
887// m_helper has to be kept alive as well
888
889//-----------------------------------------------------------------------------
890// wxGDIPlusBitmapData implementation
891//-----------------------------------------------------------------------------
892
893wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap ) :
894 wxGraphicsObjectRefData( renderer ), m_bitmap( bitmap )
895{
896 m_helper = NULL;
897}
898
0b7dce54 899wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer,
bce28872
SC
900 const wxBitmap &bmp) : wxGraphicsObjectRefData( renderer )
901{
902 m_bitmap = NULL;
903 m_helper = NULL;
904
905 Bitmap* image = NULL;
906 if ( bmp.GetMask() )
907 {
e38f5435
DS
908 Bitmap interim((HBITMAP)bmp.GetHBITMAP(),
909#if wxUSE_PALETTE
910 (HPALETTE)bmp.GetPalette()->GetHPALETTE()
911#else
912 NULL
913#endif
914 );
bce28872
SC
915
916 size_t width = interim.GetWidth();
917 size_t height = interim.GetHeight();
918 Rect bounds(0,0,width,height);
919
920 image = new Bitmap(width,height,PixelFormat32bppPARGB) ;
921
922 Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL);
923 wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed);
924
925 BitmapData dataMask ;
926 interimMask.LockBits(&bounds,ImageLockModeRead,
927 interimMask.GetPixelFormat(),&dataMask);
928
929
930 BitmapData imageData ;
931 image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData);
932
933 BYTE maskPattern = 0 ;
934 BYTE maskByte = 0;
935 size_t maskIndex ;
936
937 for ( size_t y = 0 ; y < height ; ++y)
938 {
939 maskIndex = 0 ;
940 for( size_t x = 0 ; x < width; ++x)
941 {
942 if ( x % 8 == 0)
943 {
944 maskPattern = 0x80;
945 maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex);
946 maskIndex++;
947 }
948 else
949 maskPattern = maskPattern >> 1;
950
951 ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4);
952 if ( (maskByte & maskPattern) == 0 )
953 *dest = 0x00000000;
954 else
955 {
956 Color c ;
957 interim.GetPixel(x,y,&c) ;
958 *dest = (c.GetValue() | Color::AlphaMask);
959 }
960 }
961 }
962
963 image->UnlockBits(&imageData);
964
965 interimMask.UnlockBits(&dataMask);
966 interim.UnlockBits(&dataMask);
967 }
968 else
969 {
e38f5435
DS
970 image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),
971#if wxUSE_PALETTE
972 (HPALETTE)bmp.GetPalette()->GetHPALETTE()
973#else
974 NULL
975#endif
976 );
bce28872
SC
977 if ( bmp.HasAlpha() && GetPixelFormatSize(image->GetPixelFormat()) == 32 )
978 {
979 size_t width = image->GetWidth();
980 size_t height = image->GetHeight();
981 Rect bounds(0,0,width,height);
982 static BitmapData data ;
983
984 m_helper = image ;
985 image = NULL ;
986 m_helper->LockBits(&bounds, ImageLockModeRead,
987 m_helper->GetPixelFormat(),&data);
988
989 image = new Bitmap(data.Width, data.Height, data.Stride,
990 PixelFormat32bppPARGB , (BYTE*) data.Scan0);
991
992 m_helper->UnlockBits(&data);
993 }
994 }
995 if ( image )
996 m_bitmap = image;
997}
998
999wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1000{
1001 delete m_bitmap;
1002 delete m_helper;
1003}
1004
83576e68
SC
1005//-----------------------------------------------------------------------------
1006// wxGDIPlusPath implementation
1007//-----------------------------------------------------------------------------
6fea499c 1008
a4e73390 1009wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path ) : wxGraphicsPathData(renderer)
83576e68
SC
1010{
1011 if ( path )
1012 m_path = path;
1013 else
1014 m_path = new GraphicsPath();
1015}
1016
a4e73390 1017wxGDIPlusPathData::~wxGDIPlusPathData()
6fea499c
SC
1018{
1019 delete m_path;
1020}
1021
a4e73390 1022wxGraphicsObjectRefData* wxGDIPlusPathData::Clone() const
83576e68 1023{
a4e73390 1024 return new wxGDIPlusPathData( GetRenderer() , m_path->Clone());
83576e68
SC
1025}
1026
6fea499c
SC
1027//
1028// The Primitives
1029//
1030
a4e73390 1031void wxGDIPlusPathData::MoveToPoint( wxDouble x , wxDouble y )
6fea499c
SC
1032{
1033 m_path->StartFigure();
1034 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
1035}
1036
a4e73390 1037void wxGDIPlusPathData::AddLineToPoint( wxDouble x , wxDouble y )
6fea499c
SC
1038{
1039 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
1040}
1041
a4e73390 1042void wxGDIPlusPathData::CloseSubpath()
6fea499c
SC
1043{
1044 m_path->CloseFigure();
1045}
1046
a4e73390 1047void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
6fea499c
SC
1048{
1049 PointF c1(cx1,cy1);
1050 PointF c2(cx2,cy2);
1051 PointF end(x,y);
1052 PointF start;
1053 m_path->GetLastPoint(&start);
1054 m_path->AddBezier(start,c1,c2,end);
1055}
1056
1057// gets the last point of the current path, (0,0) if not yet set
a4e73390 1058void wxGDIPlusPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
6fea499c
SC
1059{
1060 PointF start;
1061 m_path->GetLastPoint(&start);
a4e73390
SC
1062 *x = start.X ;
1063 *y = start.Y ;
6fea499c
SC
1064}
1065
cb3a0d42 1066void wxGDIPlusPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
6fea499c
SC
1067{
1068 double sweepAngle = endAngle - startAngle ;
c5b8c66a 1069 if( fabs(sweepAngle) >= 2*M_PI)
6fea499c
SC
1070 {
1071 sweepAngle = 2 * M_PI;
1072 }
1073 else
1074 {
1075 if ( clockwise )
1076 {
1077 if( sweepAngle < 0 )
1078 sweepAngle += 2 * M_PI;
1079 }
1080 else
1081 {
1082 if( sweepAngle > 0 )
1083 sweepAngle -= 2 * M_PI;
1084
1085 }
1086 }
cb3a0d42 1087 m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle));
6fea499c
SC
1088}
1089
a4e73390 1090void wxGDIPlusPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
6fea499c
SC
1091{
1092 m_path->AddRectangle(RectF(x,y,w,h));
1093}
1094
a4e73390 1095void wxGDIPlusPathData::AddPath( const wxGraphicsPathData* path )
6fea499c 1096{
83576e68 1097 m_path->AddPath( (GraphicsPath*) path->GetNativePath(), FALSE);
6fea499c
SC
1098}
1099
6fea499c 1100
83576e68 1101// transforms each point of this path by the matrix
cb3a0d42 1102void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData* matrix )
9f339b18 1103{
83576e68 1104 m_path->Transform( (Matrix*) matrix->GetNativeMatrix() );
9f339b18 1105}
6fea499c 1106
83576e68 1107// gets the bounding box enclosing all points (possibly including control points)
a4e73390 1108void wxGDIPlusPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
e49c065d 1109{
83576e68
SC
1110 RectF bounds;
1111 m_path->GetBounds( &bounds, NULL, NULL) ;
1112 *x = bounds.X;
1113 *y = bounds.Y;
1114 *w = bounds.Width;
1115 *h = bounds.Height;
e49c065d
RD
1116}
1117
94a007ec 1118bool wxGDIPlusPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
6fea499c 1119{
83576e68
SC
1120 m_path->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
1121 return m_path->IsVisible( (FLOAT) x,(FLOAT) y) == TRUE ;
1122}
9f339b18 1123
83576e68 1124//-----------------------------------------------------------------------------
a4e73390 1125// wxGDIPlusMatrixData implementation
83576e68 1126//-----------------------------------------------------------------------------
9f339b18 1127
a4e73390
SC
1128wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix )
1129 : wxGraphicsMatrixData(renderer)
9f339b18 1130{
83576e68
SC
1131 if ( matrix )
1132 m_matrix = matrix ;
1133 else
1134 m_matrix = new Matrix();
6fea499c
SC
1135}
1136
cb3a0d42 1137wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
6fea499c 1138{
83576e68 1139 delete m_matrix;
6fea499c
SC
1140}
1141
cb3a0d42 1142wxGraphicsObjectRefData *wxGDIPlusMatrixData::Clone() const
6fea499c 1143{
a4e73390 1144 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix->Clone());
539e2795
SC
1145}
1146
83576e68 1147// concatenates the matrix
a4e73390 1148void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData *t )
539e2795 1149{
83576e68 1150 m_matrix->Multiply( (Matrix*) t->GetNativeMatrix());
539e2795 1151}
83576e68 1152
83576e68 1153// sets the matrix to the respective values
cb3a0d42 1154void wxGDIPlusMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
83576e68 1155 wxDouble tx, wxDouble ty)
6fea499c 1156{
83576e68 1157 m_matrix->SetElements(a,b,c,d,tx,ty);
6fea499c
SC
1158}
1159
248802d0
RD
1160// gets the component valuess of the matrix
1161void wxGDIPlusMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1162 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1163{
1164 REAL elements[6];
1165 m_matrix->GetElements(elements);
1166 if (a) *a = elements[0];
1167 if (b) *b = elements[1];
1168 if (c) *c = elements[2];
1169 if (d) *d = elements[3];
1170 if (tx) *tx= elements[4];
1171 if (ty) *ty= elements[5];
1172}
1173
83576e68 1174// makes this the inverse matrix
a4e73390 1175void wxGDIPlusMatrixData::Invert()
6fea499c 1176{
83576e68 1177 m_matrix->Invert();
6fea499c
SC
1178}
1179
83576e68 1180// returns true if the elements of the transformation matrix are equal ?
cb3a0d42 1181bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
6fea499c 1182{
83576e68 1183 return m_matrix->Equals((Matrix*) t->GetNativeMatrix())== TRUE ;
6fea499c
SC
1184}
1185
83576e68 1186// return true if this is the identity matrix
a4e73390 1187bool wxGDIPlusMatrixData::IsIdentity() const
6fea499c 1188{
83576e68 1189 return m_matrix->IsIdentity() == TRUE ;
6fea499c
SC
1190}
1191
83576e68
SC
1192//
1193// transformation
1194//
1195
1196// add the translation to this matrix
a4e73390 1197void wxGDIPlusMatrixData::Translate( wxDouble dx , wxDouble dy )
6fea499c 1198{
83576e68 1199 m_matrix->Translate(dx,dy);
6fea499c
SC
1200}
1201
83576e68 1202// add the scale to this matrix
a4e73390 1203void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale )
6fea499c 1204{
83576e68 1205 m_matrix->Scale(xScale,yScale);
6fea499c
SC
1206}
1207
83576e68 1208// add the rotation to this matrix (radians)
a4e73390 1209void wxGDIPlusMatrixData::Rotate( wxDouble angle )
6fea499c 1210{
6f32f3ce 1211 m_matrix->Rotate( RadToDeg(angle) );
6fea499c
SC
1212}
1213
83576e68
SC
1214//
1215// apply the transforms
1216//
1217
1218// applies that matrix to the point
a4e73390 1219void wxGDIPlusMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
6fea499c 1220{
83576e68
SC
1221 PointF pt(*x,*y);
1222 m_matrix->TransformPoints(&pt);
1223 *x = pt.X;
1224 *y = pt.Y;
6fea499c
SC
1225}
1226
83576e68 1227// applies the matrix except for translations
a4e73390 1228void wxGDIPlusMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
6fea499c 1229{
83576e68
SC
1230 PointF pt(*dx,*dy);
1231 m_matrix->TransformVectors(&pt);
1232 *dx = pt.X;
1233 *dy = pt.Y;
6fea499c
SC
1234}
1235
83576e68 1236// returns the native representation
a4e73390 1237void * wxGDIPlusMatrixData::GetNativeMatrix() const
6fea499c 1238{
83576e68
SC
1239 return m_matrix;
1240}
6fea499c 1241
83576e68
SC
1242//-----------------------------------------------------------------------------
1243// wxGDIPlusContext implementation
1244//-----------------------------------------------------------------------------
1245
1246IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext,wxGraphicsContext)
0c7bd159
SC
1247IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext,wxGDIPlusContext)
1248
1249class wxGDIPlusOffsetHelper
1250{
1251public :
1252 wxGDIPlusOffsetHelper( Graphics* gr , bool offset )
1253 {
1254 m_gr = gr;
1255 m_offset = offset;
1256 if ( m_offset )
1257 m_gr->TranslateTransform( 0.5, 0.5 );
1258 }
1259 ~wxGDIPlusOffsetHelper( )
1260 {
1261 if ( m_offset )
1262 m_gr->TranslateTransform( -0.5, -0.5 );
1263 }
1264public :
1265 Graphics* m_gr;
1266 bool m_offset;
1267} ;
83576e68 1268
7fa3cf6a 1269wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc, wxDouble width, wxDouble height )
83576e68
SC
1270 : wxGraphicsContext(renderer)
1271{
1272 Init();
7fa3cf6a 1273 m_context = new Graphics( hdc);
81e0b3d9
SC
1274 m_width = width;
1275 m_height = height;
83576e68 1276 SetDefaults();
6fea499c
SC
1277}
1278
eb40c2d8
JS
1279wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, const wxDC& dc )
1280 : wxGraphicsContext(renderer)
1281{
1282 Init();
1283
1284 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1285 HDC hdc = (HDC) msw->GetHDC();
1286
1287 m_context = new Graphics(hdc);
1288 wxSize sz = dc.GetSize();
1289 m_width = sz.x;
1290 m_height = sz.y;
1291
1292 SetDefaults();
1293}
1294
83576e68
SC
1295wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd )
1296 : wxGraphicsContext(renderer)
6fea499c 1297{
83576e68
SC
1298 Init();
1299 m_context = new Graphics( hwnd);
81e0b3d9
SC
1300 RECT rect = wxGetWindowRect(hwnd);
1301 m_width = rect.right - rect.left;
1302 m_height = rect.bottom - rect.top;
83576e68
SC
1303 SetDefaults();
1304}
6fea499c 1305
83576e68
SC
1306wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr )
1307 : wxGraphicsContext(renderer)
1308{
1309 Init();
1310 m_context = gr;
1311 SetDefaults();
1312}
6fea499c 1313
83576e68
SC
1314wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL)
1315{
1316 Init();
1317}
6fea499c 1318
83576e68
SC
1319void wxGDIPlusContext::Init()
1320{
1321 m_context = NULL;
1322 m_state1 = 0;
1323 m_state2= 0;
81e0b3d9
SC
1324 m_height = 0;
1325 m_width = 0;
eb40c2d8 1326 m_fontScaleRatio = 1.0;
83576e68 1327}
6fea499c 1328
83576e68
SC
1329void wxGDIPlusContext::SetDefaults()
1330{
0c7bd159
SC
1331 m_context->SetTextRenderingHint(TextRenderingHintSystemDefault);
1332 m_context->SetPixelOffsetMode(PixelOffsetModeHalf);
83576e68
SC
1333 m_context->SetSmoothingMode(SmoothingModeHighQuality);
1334 m_state1 = m_context->Save();
1335 m_state2 = m_context->Save();
1336}
6fea499c 1337
83576e68
SC
1338wxGDIPlusContext::~wxGDIPlusContext()
1339{
83576e68 1340 if ( m_context )
6fea499c 1341 {
83576e68
SC
1342 m_context->Restore( m_state2 );
1343 m_context->Restore( m_state1 );
1344 delete m_context;
6fea499c
SC
1345 }
1346}
1347
83576e68
SC
1348
1349void wxGDIPlusContext::Clip( const wxRegion &region )
6fea499c 1350{
93f72bae
SC
1351 Region rgn((HRGN)region.GetHRGN());
1352 m_context->SetClip(&rgn,CombineModeIntersect);
83576e68 1353}
6fea499c 1354
83576e68
SC
1355void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1356{
1357 m_context->SetClip(RectF(x,y,w,h),CombineModeIntersect);
1358}
cb3a0d42 1359
83576e68
SC
1360void wxGDIPlusContext::ResetClip()
1361{
1362 m_context->ResetClip();
1363}
6fea499c 1364
0c7bd159
SC
1365void wxGDIPlusContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
1366{
bf02a7f9
SC
1367 if (m_composition == wxCOMPOSITION_DEST)
1368 return;
1369
03647350
VZ
1370 if ( !m_pen.IsNull() )
1371 {
1372 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1373 Point *cpoints = new Point[n];
1374 for (size_t i = 0; i < n; i++)
1375 {
1376 cpoints[i].X = (int)(points[i].m_x );
1377 cpoints[i].Y = (int)(points[i].m_y );
1378
1379 } // for (size_t i = 0; i < n; i++)
1380 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1381 delete[] cpoints;
1382 }
0c7bd159
SC
1383}
1384
94a007ec 1385void wxGDIPlusContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode WXUNUSED(fillStyle) )
0c7bd159 1386{
bf02a7f9
SC
1387 if (m_composition == wxCOMPOSITION_DEST)
1388 return;
1389
0c7bd159 1390 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
03647350
VZ
1391 Point *cpoints = new Point[n];
1392 for (size_t i = 0; i < n; i++)
1393 {
1394 cpoints[i].X = (int)(points[i].m_x );
1395 cpoints[i].Y = (int)(points[i].m_y );
1396
1397 } // for (int i = 0; i < n; i++)
1398 if ( !m_brush.IsNull() )
1399 m_context->FillPolygon( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , cpoints , n ) ;
1400 if ( !m_pen.IsNull() )
1401 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1402 delete[] cpoints;
0c7bd159
SC
1403}
1404
a4e73390 1405void wxGDIPlusContext::StrokePath( const wxGraphicsPath& path )
83576e68 1406{
bf02a7f9
SC
1407 if (m_composition == wxCOMPOSITION_DEST)
1408 return;
1409
2c820406 1410 if ( !m_pen.IsNull() )
83576e68 1411 {
0c7bd159 1412 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
a4e73390 1413 m_context->DrawPath( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath*) path.GetNativePath() );
83576e68 1414 }
6fea499c
SC
1415}
1416
94a007ec 1417void wxGDIPlusContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
6fea499c 1418{
bf02a7f9
SC
1419 if (m_composition == wxCOMPOSITION_DEST)
1420 return;
1421
2c820406 1422 if ( !m_brush.IsNull() )
83576e68 1423 {
0c7bd159 1424 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
a4e73390 1425 ((GraphicsPath*) path.GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
cb3a0d42 1426 m_context->FillPath( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() ,
a4e73390 1427 (GraphicsPath*) path.GetNativePath());
83576e68
SC
1428 }
1429}
6fea499c 1430
bf02a7f9
SC
1431bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias)
1432{
1433 if (m_antialias == antialias)
1434 return true;
03647350 1435
bf02a7f9 1436 m_antialias = antialias;
03647350 1437
bf02a7f9
SC
1438 SmoothingMode antialiasMode;
1439 switch (antialias)
1440 {
1441 case wxANTIALIAS_DEFAULT:
1442 antialiasMode = SmoothingModeHighQuality;
1443 break;
1444 case wxANTIALIAS_NONE:
1445 antialiasMode = SmoothingModeNone;
1446 break;
1447 default:
1448 return false;
1449 }
1450 m_context->SetSmoothingMode(antialiasMode);
1451 return true;
1452}
1453
1454bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op)
1455{
1456 if ( m_composition == op )
1457 return true;
03647350 1458
bf02a7f9 1459 m_composition = op;
03647350 1460
bf02a7f9
SC
1461 if (m_composition == wxCOMPOSITION_DEST)
1462 return true;
1463
1464 CompositingMode cop;
1465 switch (op)
1466 {
1467 case wxCOMPOSITION_SOURCE:
1468 cop = CompositingModeSourceCopy;
1469 break;
1470 case wxCOMPOSITION_OVER:
1471 cop = CompositingModeSourceOver;
1472 break;
1473 default:
1474 return false;
1475 }
1476
1477 m_context->SetCompositingMode(cop);
1478 return true;
1479}
1480
c496d8fa 1481void wxGDIPlusContext::BeginLayer(wxDouble /* opacity */)
bf02a7f9
SC
1482{
1483 // TODO
1484}
1485
1486void wxGDIPlusContext::EndLayer()
1487{
1488 // TODO
03647350 1489}
bf02a7f9 1490
cb3a0d42 1491void wxGDIPlusContext::Rotate( wxDouble angle )
83576e68
SC
1492{
1493 m_context->RotateTransform( RadToDeg(angle) );
1494}
6fea499c 1495
cb3a0d42 1496void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
83576e68
SC
1497{
1498 m_context->TranslateTransform( dx , dy );
1499}
6fea499c 1500
83576e68
SC
1501void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
1502{
1503 m_context->ScaleTransform(xScale,yScale);
1504}
6fea499c 1505
83576e68
SC
1506void wxGDIPlusContext::PushState()
1507{
1508 GraphicsState state = m_context->Save();
bdba6fdc 1509 m_stateStack.push(state);
83576e68
SC
1510}
1511
cb3a0d42 1512void wxGDIPlusContext::PopState()
83576e68 1513{
bdba6fdc
VZ
1514 GraphicsState state = m_stateStack.top();
1515 m_stateStack.pop();
83576e68 1516 m_context->Restore(state);
6fea499c
SC
1517}
1518
bce28872 1519void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
6fea499c 1520{
bf02a7f9
SC
1521 if (m_composition == wxCOMPOSITION_DEST)
1522 return;
1523
bce28872
SC
1524 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bmp.GetRefData())->GetGDIPlusBitmap();
1525 if ( image )
cb3a0d42 1526 {
bce28872 1527 if( image->GetWidth() != (UINT) w || image->GetHeight() != (UINT) h )
fe31db81 1528 {
bce28872
SC
1529 Rect drawRect((REAL) x, (REAL)y, (REAL)w, (REAL)h);
1530 m_context->SetPixelOffsetMode( PixelOffsetModeNone );
1531 m_context->DrawImage(image, drawRect, 0 , 0 , image->GetWidth()-1, image->GetHeight()-1, UnitPixel ) ;
1532 m_context->SetPixelOffsetMode( PixelOffsetModeHalf );
fe31db81 1533 }
bce28872
SC
1534 else
1535 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
fe31db81 1536 }
bce28872 1537}
fe31db81 1538
bce28872
SC
1539void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1540{
1541 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1542 DrawBitmap(bitmap, x, y, w, h);
6fea499c
SC
1543}
1544
cb3a0d42 1545void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
6fea499c 1546{
bf02a7f9
SC
1547 if (m_composition == wxCOMPOSITION_DEST)
1548 return;
1549
0c7bd159
SC
1550 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1551 // find out by looking at the bitmap data whether there really was alpha in it
fe31db81
SC
1552 HICON hIcon = (HICON)icon.GetHICON();
1553 ICONINFO iconInfo ;
1554 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1555 if (!GetIconInfo(hIcon,&iconInfo))
1556 return;
1557
fe31db81
SC
1558 Bitmap interim(iconInfo.hbmColor,NULL);
1559
1560 Bitmap* image = NULL ;
1561
0c7bd159
SC
1562 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1563 // work correctly, asking IsAlphaPixelFormat at this point fails as well
fe31db81
SC
1564 if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 )
1565 {
1566 image = Bitmap::FromHICON(hIcon);
1567 }
1568 else
1569 {
1570 size_t width = interim.GetWidth();
1571 size_t height = interim.GetHeight();
1572 Rect bounds(0,0,width,height);
1573 BitmapData data ;
1574
1575 interim.LockBits(&bounds, ImageLockModeRead,
1576 interim.GetPixelFormat(),&data);
0b7dce54 1577
0c7bd159
SC
1578 bool hasAlpha = false;
1579 for ( size_t y = 0 ; y < height && !hasAlpha ; ++y)
1580 {
1581 for( size_t x = 0 ; x < width && !hasAlpha; ++x)
1582 {
1583 ARGB *dest = (ARGB*)((BYTE*)data.Scan0 + data.Stride*y + x*4);
1584 if ( ( *dest & Color::AlphaMask ) != 0 )
1585 hasAlpha = true;
1586 }
1587 }
1588
1589 if ( hasAlpha )
1590 {
cb3a0d42 1591 image = new Bitmap(data.Width, data.Height, data.Stride,
fe31db81 1592 PixelFormat32bppARGB , (BYTE*) data.Scan0);
0c7bd159
SC
1593 }
1594 else
1595 {
1596 image = Bitmap::FromHICON(hIcon);
1597 }
1598
fe31db81
SC
1599 interim.UnlockBits(&data);
1600 }
1601
6fea499c 1602 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
fe31db81 1603
6fea499c 1604 delete image ;
fe31db81
SC
1605 DeleteObject(iconInfo.hbmColor);
1606 DeleteObject(iconInfo.hbmMask);
6fea499c
SC
1607}
1608
eb40c2d8
JS
1609wxGraphicsFont wxGDIPlusContext::CreateFont( const wxFont &font,
1610 const wxColour &col ) const
1611{
1612 wxGDIPlusRenderer* renderer =
1613 static_cast<wxGDIPlusRenderer*>(GetRenderer());
1614 return renderer->CreateGDIPlusFont(this, font, col);
1615}
1616
0b7dce54
VZ
1617void wxGDIPlusContext::DoDrawFilledText(const wxString& str,
1618 wxDouble x, wxDouble y,
1619 const wxGraphicsBrush& brush)
6fea499c 1620{
bf02a7f9
SC
1621 if (m_composition == wxCOMPOSITION_DEST)
1622 return;
1623
0b7dce54
VZ
1624 wxCHECK_RET( !m_font.IsNull(),
1625 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1011fbeb
SC
1626
1627 if ( str.IsEmpty())
6fea499c
SC
1628 return ;
1629
091111d6 1630 wxGDIPlusFontData * const
0b7dce54 1631 fontData = (wxGDIPlusFontData *)m_font.GetRefData();
091111d6
VZ
1632 wxGDIPlusBrushData * const
1633 brushData = (wxGDIPlusBrushData *)brush.GetRefData();
0b7dce54
VZ
1634
1635 m_context->DrawString
1636 (
1637 str.wc_str(*wxConvUI), // string to draw, always Unicode
1638 -1, // length: string is NUL-terminated
1639 fontData->GetGDIPlusFont(),
1640 PointF(x, y),
1641 StringFormat::GenericTypographic(),
1642 brushData ? brushData->GetGDIPlusBrush()
1643 : fontData->GetGDIPlusBrush()
1644 );
6fea499c
SC
1645}
1646
1647void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1648 wxDouble *descent, wxDouble *externalLeading ) const
1649{
1011fbeb
SC
1650 wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1651
6fea499c
SC
1652 wxWCharBuffer s = str.wc_str( *wxConvUI );
1653 FontFamily ffamily ;
2c820406 1654 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
cb3a0d42 1655
83576e68 1656 f->GetFamily(&ffamily) ;
6fea499c 1657
eb40c2d8 1658 REAL factorY = m_fontScaleRatio;
6fea499c
SC
1659
1660 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
83576e68 1661 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c 1662 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
83576e68 1663 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c 1664 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
83576e68 1665 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c
SC
1666
1667 if ( height )
595fda6f 1668 *height = rHeight * factorY;
6fea499c 1669 if ( descent )
595fda6f 1670 *descent = rDescent * factorY;
6fea499c 1671 if ( externalLeading )
595fda6f 1672 *externalLeading = (rHeight - rAscent - rDescent) * factorY;
6fea499c 1673 // measuring empty strings is not guaranteed, so do it by hand
cb3a0d42 1674 if ( str.IsEmpty())
6fea499c
SC
1675 {
1676 if ( width )
1677 *width = 0 ;
1678 }
1679 else
1680 {
6fea499c 1681 RectF layoutRect(0,0, 100000.0f, 100000.0f);
0c7bd159 1682 StringFormat strFormat( StringFormat::GenericTypographic() );
0b7dce54 1683 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
0c7bd159
SC
1684
1685 RectF bounds ;
1686 m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ;
6fea499c 1687 if ( width )
0c7bd159 1688 *width = bounds.Width;
465642da
JS
1689 if ( height )
1690 *height = bounds.Height;
6fea499c
SC
1691 }
1692}
1693
cb3a0d42 1694void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
6fea499c
SC
1695{
1696 widths.Empty();
1697 widths.Add(0, text.length());
1698
1011fbeb
SC
1699 wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1700
6fea499c
SC
1701 if (text.empty())
1702 return;
1703
2c820406 1704 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
6fea499c
SC
1705 wxWCharBuffer ws = text.wc_str( *wxConvUI );
1706 size_t len = wcslen( ws ) ;
1707 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1708
1709 RectF layoutRect(0,0, 100000.0f, 100000.0f);
0c7bd159 1710 StringFormat strFormat( StringFormat::GenericTypographic() );
6fea499c 1711
c315587c 1712 size_t startPosition = 0;
918df9d2 1713 size_t remainder = len;
c315587c
SC
1714 const size_t maxSpan = 32;
1715 CharacterRange* ranges = new CharacterRange[maxSpan] ;
1716 Region* regions = new Region[maxSpan];
6fea499c 1717
918df9d2 1718 while( remainder > 0 )
6fea499c 1719 {
918df9d2 1720 size_t span = wxMin( maxSpan, remainder );
c315587c
SC
1721
1722 for( size_t i = 0 ; i < span ; ++i)
1723 {
1724 ranges[i].First = 0 ;
1725 ranges[i].Length = startPosition+i+1 ;
1726 }
1727 strFormat.SetMeasurableCharacterRanges(span,ranges);
1728 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1729 m_context->MeasureCharacterRanges(ws, -1 , f,layoutRect, &strFormat,span,regions) ;
1730
1731 RectF bbox ;
1732 for ( size_t i = 0 ; i < span ; ++i)
1733 {
1734 regions[i].GetBounds(&bbox,m_context);
1735 widths[startPosition+i] = bbox.Width;
1736 }
918df9d2 1737 remainder -= span;
c315587c 1738 startPosition += span;
6fea499c 1739 }
c315587c
SC
1740
1741 delete[] ranges;
1742 delete[] regions;
6fea499c
SC
1743}
1744
d9485f89 1745bool wxGDIPlusContext::ShouldOffset() const
0b7dce54 1746{
d9485f89
RD
1747 int penwidth = 0 ;
1748 if ( !m_pen.IsNull() )
1749 {
1750 penwidth = (int)((wxGDIPlusPenData*)m_pen.GetRefData())->GetWidth();
1751 if ( penwidth == 0 )
1752 penwidth = 1;
1753 }
1754 return ( penwidth % 2 ) == 1;
1755}
1756
cb3a0d42 1757void* wxGDIPlusContext::GetNativeContext()
6fea499c 1758{
cb3a0d42 1759 return m_context;
6fea499c
SC
1760}
1761
83576e68 1762// concatenates this transform with the current transform of this context
a4e73390 1763void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix& matrix )
539e2795 1764{
a4e73390 1765 m_context->MultiplyTransform((Matrix*) matrix.GetNativeMatrix());
83576e68
SC
1766}
1767
1768// sets the transform of this context
a4e73390 1769void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix& matrix )
83576e68 1770{
a4e73390 1771 m_context->SetTransform((Matrix*) matrix.GetNativeMatrix());
83576e68
SC
1772}
1773
1774// gets the matrix of this context
a4e73390 1775wxGraphicsMatrix wxGDIPlusContext::GetTransform() const
83576e68 1776{
a4e73390
SC
1777 wxGraphicsMatrix matrix = CreateMatrix();
1778 m_context->GetTransform((Matrix*) matrix.GetNativeMatrix());
1779 return matrix;
83576e68 1780}
fbd5416f
SC
1781
1782void wxGDIPlusContext::GetSize( wxDouble* width, wxDouble *height )
1783{
81e0b3d9
SC
1784 *width = m_width;
1785 *height = m_height;
fbd5416f 1786}
eb40c2d8 1787
7fa3cf6a 1788//-----------------------------------------------------------------------------
eb40c2d8 1789// wxGDIPlusPrintingContext implementation
7fa3cf6a
JS
1790//-----------------------------------------------------------------------------
1791
eb40c2d8
JS
1792wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer,
1793 const wxDC& dc )
1794 : wxGDIPlusContext(renderer, dc)
7fa3cf6a 1795{
eb40c2d8 1796 Graphics* context = GetGraphics();
7fa3cf6a 1797
eb40c2d8 1798 //m_context->SetPageUnit(UnitDocument);
7fa3cf6a 1799
eb40c2d8
JS
1800 // Setup page scale, based on DPI ratio.
1801 // Antecedent should be 100dpi when the default page unit
1802 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1803 // instead. Note that calling SetPageScale() does not have effect on
1804 // non-printing DCs (that is, any other than wxPrinterDC or
1805 // wxEnhMetaFileDC).
1806 REAL dpiRatio = 100.0 / context->GetDpiY();
1807 context->SetPageScale(dpiRatio);
7fa3cf6a 1808
eb40c2d8
JS
1809 // We use this modifier when measuring fonts. It is needed because the
1810 // page scale is modified above.
1811 m_fontScaleRatio = context->GetDpiY() / 72.0;
1812}
83576e68
SC
1813
1814//-----------------------------------------------------------------------------
1815// wxGDIPlusRenderer implementation
1816//-----------------------------------------------------------------------------
1817
1818IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer,wxGraphicsRenderer)
1819
1820static wxGDIPlusRenderer gs_GDIPlusRenderer;
1821
1822wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1823{
1824 return &gs_GDIPlusRenderer;
1825}
1826
bce82a75 1827bool wxGDIPlusRenderer::EnsureIsLoaded()
83576e68 1828{
bce82a75
VS
1829 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1830 // if we already tried and failed (we don't want to spend lot of time
1831 // returning NULL from wxGraphicsContext::Create(), which may be called
1832 // relatively frequently):
1833 if ( m_loaded == -1 )
83576e68
SC
1834 {
1835 Load();
1836 }
bce82a75
VS
1837
1838 return m_loaded == 1;
83576e68
SC
1839}
1840
bce82a75
VS
1841// call EnsureIsLoaded() and return returnOnFail value if it fails
1842#define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1843 if ( !EnsureIsLoaded() ) \
1844 return (returnOnFail)
1845
1846
83576e68
SC
1847void wxGDIPlusRenderer::Load()
1848{
1849 GdiplusStartupInput input;
1850 GdiplusStartupOutput output;
bce82a75
VS
1851 if ( GdiplusStartup(&m_gditoken,&input,&output) == Gdiplus::Ok )
1852 {
1853 wxLogTrace("gdiplus", "successfully initialized GDI+");
1854 m_loaded = 1;
1855 }
1856 else
1857 {
1858 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1859 m_loaded = 0;
1860 }
83576e68
SC
1861}
1862
1863void wxGDIPlusRenderer::Unload()
1864{
1865 if ( m_gditoken )
385addaf 1866 {
83576e68 1867 GdiplusShutdown(m_gditoken);
385addaf
VZ
1868 m_gditoken = NULL;
1869 }
bce82a75 1870 m_loaded = -1; // next Load() will try again
83576e68
SC
1871}
1872
1873wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxWindowDC& dc)
1874{
bce82a75 1875 ENSURE_LOADED_OR_RETURN(NULL);
eb40c2d8 1876 return new wxGDIPlusContext(this, dc);
83576e68
SC
1877}
1878
c929ad91 1879#if wxUSE_PRINTING_ARCHITECTURE
0b822969 1880wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxPrinterDC& dc)
8371a353
JS
1881{
1882 ENSURE_LOADED_OR_RETURN(NULL);
eb40c2d8
JS
1883 wxGDIPlusContext* context = new wxGDIPlusPrintingContext(this, dc);
1884 return context;
8371a353 1885}
c929ad91 1886#endif
8371a353 1887
c929ad91 1888#if wxUSE_ENH_METAFILE
8371a353 1889wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC& dc)
0b822969 1890{
bce82a75 1891 ENSURE_LOADED_OR_RETURN(NULL);
eb40c2d8
JS
1892 wxGDIPlusContext* context = new wxGDIPlusPrintingContext(this, dc);
1893 return context;
0b822969 1894}
c929ad91 1895#endif
0b822969 1896
773ccc31
SC
1897wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxMemoryDC& dc)
1898{
bce82a75 1899 ENSURE_LOADED_OR_RETURN(NULL);
eb40c2d8 1900 return new wxGDIPlusContext(this, dc);
773ccc31
SC
1901}
1902
091ef146
SC
1903wxGraphicsContext * wxGDIPlusRenderer::CreateMeasuringContext()
1904{
bce82a75 1905 ENSURE_LOADED_OR_RETURN(NULL);
0c7bd159 1906 return new wxGDIPlusMeasuringContext(this);
091ef146
SC
1907}
1908
83576e68
SC
1909wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeContext( void * context )
1910{
bce82a75 1911 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
1912 return new wxGDIPlusContext(this,(Graphics*) context);
1913}
1914
1915
1916wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window )
1917{
bce82a75 1918 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
1919 return new wxGDIPlusContext(this,(HWND) window);
1920}
1921
1922wxGraphicsContext * wxGDIPlusRenderer::CreateContext( wxWindow* window )
1923{
bce82a75 1924 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
1925 return new wxGDIPlusContext(this, (HWND) window->GetHWND() );
1926}
1927
1928// Path
1929
a4e73390 1930wxGraphicsPath wxGDIPlusRenderer::CreatePath()
83576e68 1931{
bce82a75 1932 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath);
a4e73390
SC
1933 wxGraphicsPath m;
1934 m.SetRefData( new wxGDIPlusPathData(this));
1935 return m;
83576e68
SC
1936}
1937
1938
1939// Matrix
1940
cb3a0d42 1941wxGraphicsMatrix wxGDIPlusRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
83576e68
SC
1942 wxDouble tx, wxDouble ty)
1943
1944{
bce82a75 1945 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix);
a4e73390
SC
1946 wxGraphicsMatrix m;
1947 wxGDIPlusMatrixData* data = new wxGDIPlusMatrixData( this );
1948 data->Set( a,b,c,d,tx,ty ) ;
1949 m.SetRefData(data);
83576e68 1950 return m;
539e2795
SC
1951}
1952
cb3a0d42 1953wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen)
6fea499c 1954{
bce82a75 1955 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen);
83576e68 1956 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
2c820406 1957 return wxNullGraphicsPen;
83576e68 1958 else
2c820406
SC
1959 {
1960 wxGraphicsPen p;
1961 p.SetRefData(new wxGDIPlusPenData( this, pen ));
1962 return p;
1963 }
6fea499c 1964}
7ba86d93 1965
cb3a0d42 1966wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush )
539e2795 1967{
bce82a75 1968 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
83576e68 1969 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
2c820406 1970 return wxNullGraphicsBrush;
83576e68 1971 else
2c820406
SC
1972 {
1973 wxGraphicsBrush p;
1974 p.SetRefData(new wxGDIPlusBrushData( this, brush ));
1975 return p;
1976 }
539e2795
SC
1977}
1978
4ee4c7b9
VZ
1979wxGraphicsBrush
1980wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
1981 wxDouble x2, wxDouble y2,
1982 const wxGraphicsGradientStops& stops)
539e2795 1983{
bce82a75 1984 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2c820406
SC
1985 wxGraphicsBrush p;
1986 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
4ee4c7b9 1987 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
2c820406
SC
1988 p.SetRefData(d);
1989 return p;
1990 }
539e2795 1991
4ee4c7b9
VZ
1992wxGraphicsBrush
1993wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
1994 wxDouble xc, wxDouble yc,
1995 wxDouble radius,
1996 const wxGraphicsGradientStops& stops)
83576e68 1997{
bce82a75 1998 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2c820406
SC
1999 wxGraphicsBrush p;
2000 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
4ee4c7b9 2001 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,stops);
2c820406
SC
2002 p.SetRefData(d);
2003 return p;
83576e68 2004}
539e2795 2005
eb40c2d8
JS
2006wxGraphicsFont
2007wxGDIPlusRenderer::CreateGDIPlusFont( const wxGDIPlusContext* gc,
2008 const wxFont &font,
2009 const wxColour &col )
83576e68 2010{
bce82a75 2011 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
83576e68 2012 if ( font.Ok() )
cb3a0d42 2013 {
2c820406 2014 wxGraphicsFont p;
eb40c2d8 2015 p.SetRefData(new wxGDIPlusFontData( this, gc, font, col ));
2c820406
SC
2016 return p;
2017 }
83576e68 2018 else
2c820406 2019 return wxNullGraphicsFont;
83576e68 2020}
7ba86d93 2021
bce28872
SC
2022wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmap( const wxBitmap &bitmap )
2023{
bce82a75 2024 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
bce28872
SC
2025 if ( bitmap.Ok() )
2026 {
2027 wxGraphicsBitmap p;
2028 p.SetRefData(new wxGDIPlusBitmapData( this , bitmap ));
2029 return p;
2030 }
2031 else
2032 return wxNullGraphicsBitmap;
2033}
2034
2986eb86
SC
2035wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap )
2036{
2037 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2038 if ( bitmap != NULL )
2039 {
2040 wxGraphicsBitmap p;
2041 p.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap*) bitmap ));
2042 return p;
2043 }
2044 else
2045 return wxNullGraphicsBitmap;
2046}
2047
bce28872
SC
2048wxGraphicsBitmap wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2049{
bce82a75 2050 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
bce28872
SC
2051 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bitmap.GetRefData())->GetGDIPlusBitmap();
2052 if ( image )
2053 {
2054 wxGraphicsBitmap p;
2055 p.SetRefData(new wxGDIPlusBitmapData( this , image->Clone( (REAL) x , (REAL) y , (REAL) w , (REAL) h , PixelFormat32bppPARGB) ));
2056 return p;
2057 }
2058 else
2059 return wxNullGraphicsBitmap;
2060}
2061
385addaf
VZ
2062// Shutdown GDI+ at app exit, before possible dll unload
2063class wxGDIPlusRendererModule : public wxModule
2064{
2065public:
2066 virtual bool OnInit() { return true; }
2067 virtual void OnExit() { gs_GDIPlusRenderer.Unload(); }
2068
2069private:
2070 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule)
2071};
2072
2073IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule)
2074
942d5e2d
VZ
2075// ----------------------------------------------------------------------------
2076// wxMSW-specific parts of wxGCDC
2077// ----------------------------------------------------------------------------
2078
2079WXHDC wxGCDC::AcquireHDC()
2080{
2081 wxGraphicsContext * const gc = GetGraphicsContext();
2082 if ( !gc )
2083 return NULL;
2084
2085 Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
2086 return g ? g->GetHDC() : NULL;
2087}
2088
2089void wxGCDC::ReleaseHDC(WXHDC hdc)
2090{
2091 if ( !hdc )
2092 return;
2093
2094 wxGraphicsContext * const gc = GetGraphicsContext();
2095 wxCHECK_RET( gc, "can't release HDC because there is no wxGraphicsContext" );
2096
2097 Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
2098 wxCHECK_RET( g, "can't release HDC because there is no Graphics" );
2099
2100 g->ReleaseHDC((HDC)hdc);
2101}
2102
7ba86d93 2103#endif // wxUSE_GRAPHICS_CONTEXT