]> git.saurik.com Git - wxWidgets.git/blame - src/msw/graphics.cpp
Fix wxAffineMatrix2D::Translate() to multiply on the left.
[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
cdcc1eda
VZ
40#include "wx/stack.h"
41
623cf1fa 42#include "wx/private/graphics.h"
bdba6fdc 43#include "wx/msw/wrapgdip.h"
bce28872 44#include "wx/msw/dc.h"
c929ad91
VZ
45#if wxUSE_ENH_METAFILE
46 #include "wx/msw/enhmeta.h"
47#endif
7fa3cf6a 48#include "wx/dcgraph.h"
6fea499c 49
f49c80c1
VZ
50#include "wx/msw/private.h" // needs to be before #include <commdlg.h>
51
52#if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
53#include <commdlg.h>
54#endif
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
0a470e5e
VZ
284#if wxUSE_IMAGE
285 wxImage ConvertToImage() const;
286#endif // wxUSE_IMAGE
287
bce28872
SC
288private :
289 Bitmap* m_bitmap;
290 Bitmap* m_helper;
291};
292
eb4083ef 293class wxGDIPlusFontData : public wxGraphicsObjectRefData
83576e68
SC
294{
295public:
eb40c2d8 296 wxGDIPlusFontData( wxGraphicsRenderer* renderer,
eb40c2d8
JS
297 const wxFont &font,
298 const wxColour& col );
fa378d36
VZ
299 wxGDIPlusFontData(wxGraphicsRenderer* renderer,
300 const wxString& name,
301 REAL sizeInPixels,
302 int style,
303 const wxColour& col);
2c820406 304 ~wxGDIPlusFontData();
83576e68 305
83576e68
SC
306 virtual Brush* GetGDIPlusBrush() { return m_textBrush; }
307 virtual Font* GetGDIPlusFont() { return m_font; }
12493a5f 308
83576e68 309private :
12493a5f
VZ
310 // Common part of all ctors, flags here is a combination of values of
311 // FontStyle GDI+ enum.
312 void Init(const wxString& name,
313 REAL size,
314 int style,
fa378d36
VZ
315 const wxColour& col,
316 Unit fontUnit);
12493a5f 317
83576e68
SC
318 Brush* m_textBrush;
319 Font* m_font;
83576e68
SC
320};
321
eb4083ef 322class wxGDIPlusContext : public wxGraphicsContext
6fea499c 323{
6fea499c 324public:
eb40c2d8
JS
325 wxGDIPlusContext( wxGraphicsRenderer* renderer, const wxDC& dc );
326 wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc, wxDouble width, wxDouble height );
83576e68
SC
327 wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd );
328 wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr);
645f9bd3 329 wxGDIPlusContext(wxGraphicsRenderer* renderer);
9f339b18 330
6fea499c
SC
331 virtual ~wxGDIPlusContext();
332
333 virtual void Clip( const wxRegion &region );
539e2795
SC
334 // clips drawings to the rect
335 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
539e2795 336
cb3a0d42
VZ
337 // resets the clipping to original extent
338 virtual void ResetClip();
339
340 virtual void * GetNativeContext();
341
a4e73390 342 virtual void StrokePath( const wxGraphicsPath& p );
94a007ec 343 virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
6fea499c 344
89db201a
SC
345 virtual void DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
346
03647350 347 // stroke lines connecting each of the points
0c7bd159
SC
348 virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
349
350 // draws a polygon
94a007ec 351 virtual void DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
0c7bd159 352
bf02a7f9
SC
353 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
354
133cb28b
SC
355 virtual bool SetInterpolationQuality(wxInterpolationQuality interpolation);
356
bf02a7f9
SC
357 virtual bool SetCompositionMode(wxCompositionMode op);
358
359 virtual void BeginLayer(wxDouble opacity);
360
361 virtual void EndLayer();
03647350 362
cb3a0d42
VZ
363 virtual void Translate( wxDouble dx , wxDouble dy );
364 virtual void Scale( wxDouble xScale , wxDouble yScale );
365 virtual void Rotate( wxDouble angle );
6fea499c 366
83576e68 367 // concatenates this transform with the current transform of this context
a4e73390 368 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
83576e68
SC
369
370 // sets the transform of this context
a4e73390 371 virtual void SetTransform( const wxGraphicsMatrix& matrix );
83576e68
SC
372
373 // gets the matrix of this context
a4e73390 374 virtual wxGraphicsMatrix GetTransform() const;
83576e68 375
bce28872 376 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
6fea499c
SC
377 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
378 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
cb3a0d42 379 virtual void PushState();
6fea499c
SC
380 virtual void PopState();
381
6fea499c
SC
382 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
383 wxDouble *descent, wxDouble *externalLeading ) const;
384 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
d9485f89 385 virtual bool ShouldOffset() const;
fbd5416f 386 virtual void GetSize( wxDouble* width, wxDouble *height );
6fea499c 387
eb40c2d8
JS
388 Graphics* GetGraphics() const { return m_context; }
389
390protected:
391
392 wxDouble m_fontScaleRatio;
393
645f9bd3
VZ
394 // Used from ctors (including those in the derived classes) and takes
395 // ownership of the graphics pointer that must be non-NULL.
396 void Init(Graphics* graphics, int width, int height);
9f339b18 397
645f9bd3 398private:
0b7dce54
VZ
399 virtual void DoDrawText(const wxString& str, wxDouble x, wxDouble y)
400 { DoDrawFilledText(str, x, y, wxNullGraphicsBrush); }
401 virtual void DoDrawFilledText(const wxString& str, wxDouble x, wxDouble y,
402 const wxGraphicsBrush& backgroundBrush);
403
83576e68 404 Graphics* m_context;
b1bf7dc7 405 wxStack<GraphicsState> m_stateStack;
83576e68
SC
406 GraphicsState m_state1;
407 GraphicsState m_state2;
408
364197e8 409 wxDECLARE_NO_COPY_CLASS(wxGDIPlusContext);
83576e68
SC
410};
411
0a470e5e
VZ
412#if wxUSE_IMAGE
413
414class wxGDIPlusImageContext : public wxGDIPlusContext
415{
416public:
417 wxGDIPlusImageContext(wxGraphicsRenderer* renderer, wxImage& image) :
418 wxGDIPlusContext(renderer),
419 m_image(image),
420 m_bitmap(renderer, image)
421 {
422 Init
423 (
424 new Graphics(m_bitmap.GetGDIPlusBitmap()),
425 image.GetWidth(),
426 image.GetHeight()
427 );
428 }
429
430 virtual ~wxGDIPlusImageContext()
431 {
432 m_image = m_bitmap.ConvertToImage();
433 }
434
435private:
436 wxImage& m_image;
437 wxGDIPlusBitmapData m_bitmap;
438
439 wxDECLARE_NO_COPY_CLASS(wxGDIPlusImageContext);
440};
441
442#endif // wxUSE_IMAGE
443
5aac6f3f 444class wxGDIPlusMeasuringContext : public wxGDIPlusContext
0c7bd159
SC
445{
446public:
81e0b3d9 447 wxGDIPlusMeasuringContext( wxGraphicsRenderer* renderer ) : wxGDIPlusContext( renderer , m_hdc = GetDC(NULL), 1000, 1000 )
0c7bd159
SC
448 {
449 }
0c7bd159
SC
450
451 virtual ~wxGDIPlusMeasuringContext()
452 {
453 ReleaseDC( NULL, m_hdc );
454 }
455
456private:
7fa3cf6a 457 HDC m_hdc ;
0c7bd159
SC
458} ;
459
eb40c2d8
JS
460class wxGDIPlusPrintingContext : public wxGDIPlusContext
461{
462public:
463 wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer, const wxDC& dc );
464 virtual ~wxGDIPlusPrintingContext() { }
465protected:
466};
467
468//-----------------------------------------------------------------------------
469// wxGDIPlusRenderer declaration
470//-----------------------------------------------------------------------------
471
472class wxGDIPlusRenderer : public wxGraphicsRenderer
473{
474public :
475 wxGDIPlusRenderer()
476 {
477 m_loaded = -1;
478 m_gditoken = 0;
479 }
480
481 virtual ~wxGDIPlusRenderer()
482 {
483 if ( m_loaded == 1 )
484 {
485 Unload();
486 }
487 }
488
489 // Context
490
491 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
492
493 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
494
495#if wxUSE_PRINTING_ARCHITECTURE
496 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
497#endif
498
499#if wxUSE_ENH_METAFILE
500 virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc);
501#endif
502
503 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
504
505 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
506
507 virtual wxGraphicsContext * CreateContext( wxWindow* window );
508
0a470e5e
VZ
509#if wxUSE_IMAGE
510 virtual wxGraphicsContext * CreateContextFromImage(wxImage& image);
511#endif // wxUSE_IMAGE
512
eb40c2d8
JS
513 virtual wxGraphicsContext * CreateMeasuringContext();
514
515 // Path
516
517 virtual wxGraphicsPath CreatePath();
518
519 // Matrix
520
521 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
522 wxDouble tx=0.0, wxDouble ty=0.0);
523
524
525 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
526
527 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
528
529 virtual wxGraphicsBrush
530 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
531 wxDouble x2, wxDouble y2,
532 const wxGraphicsGradientStops& stops);
533
534 virtual wxGraphicsBrush
535 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
536 wxDouble xc, wxDouble yc,
537 wxDouble radius,
538 const wxGraphicsGradientStops& stops);
539
540 // create a native bitmap representation
541 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
0a470e5e
VZ
542#if wxUSE_IMAGE
543 virtual wxGraphicsBitmap CreateBitmapFromImage(const wxImage& image);
6e6f074b 544 virtual wxImage CreateImageFromBitmap(const wxGraphicsBitmap& bmp);
0a470e5e 545#endif // wxUSE_IMAGE
eb40c2d8 546
a95f35b0
VZ
547 virtual wxGraphicsFont CreateFont( const wxFont& font,
548 const wxColour& col);
eb40c2d8 549
fa378d36
VZ
550 virtual wxGraphicsFont CreateFont(double size,
551 const wxString& facename,
552 int flags = wxFONTFLAG_DEFAULT,
553 const wxColour& col = *wxBLACK);
554
eb40c2d8
JS
555 // create a graphics bitmap from a native bitmap
556 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
557
558 // create a subimage from a native image representation
559 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
560
561protected :
562 bool EnsureIsLoaded();
563 void Load();
564 void Unload();
565 friend class wxGDIPlusRendererModule;
566
567private :
568 int m_loaded;
569 ULONG_PTR m_gditoken;
570
571 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer)
572} ;
573
83576e68
SC
574//-----------------------------------------------------------------------------
575// wxGDIPlusPen implementation
576//-----------------------------------------------------------------------------
577
2c820406 578wxGDIPlusPenData::~wxGDIPlusPenData()
83576e68
SC
579{
580 delete m_pen;
581 delete m_penImage;
582 delete m_penBrush;
583}
584
2c820406 585void wxGDIPlusPenData::Init()
83576e68
SC
586{
587 m_pen = NULL ;
588 m_penImage = NULL;
589 m_penBrush = NULL;
590}
591
2c820406
SC
592wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
593: wxGraphicsObjectRefData(renderer)
cb3a0d42 594{
83576e68
SC
595 Init();
596 m_width = pen.GetWidth();
597 if (m_width <= 0.0)
598 m_width = 0.1;
599
f49c80c1 600 m_pen = new Pen(wxColourToColor(pen.GetColour()), m_width );
83576e68
SC
601
602 LineCap cap;
603 switch ( pen.GetCap() )
604 {
605 case wxCAP_ROUND :
606 cap = LineCapRound;
607 break;
608
609 case wxCAP_PROJECTING :
610 cap = LineCapSquare;
611 break;
612
613 case wxCAP_BUTT :
614 cap = LineCapFlat; // TODO verify
615 break;
616
617 default :
618 cap = LineCapFlat;
619 break;
620 }
621 m_pen->SetLineCap(cap,cap, DashCapFlat);
622
623 LineJoin join;
624 switch ( pen.GetJoin() )
625 {
626 case wxJOIN_BEVEL :
627 join = LineJoinBevel;
628 break;
629
630 case wxJOIN_MITER :
631 join = LineJoinMiter;
632 break;
633
634 case wxJOIN_ROUND :
635 join = LineJoinRound;
636 break;
637
638 default :
639 join = LineJoinMiter;
640 break;
641 }
642
643 m_pen->SetLineJoin(join);
644
645 m_pen->SetDashStyle(DashStyleSolid);
646
647 DashStyle dashStyle = DashStyleSolid;
648 switch ( pen.GetStyle() )
649 {
f6453568 650 case wxPENSTYLE_SOLID :
83576e68
SC
651 break;
652
f6453568 653 case wxPENSTYLE_DOT :
83576e68
SC
654 dashStyle = DashStyleDot;
655 break;
656
f6453568 657 case wxPENSTYLE_LONG_DASH :
83576e68
SC
658 dashStyle = DashStyleDash; // TODO verify
659 break;
660
f6453568 661 case wxPENSTYLE_SHORT_DASH :
83576e68
SC
662 dashStyle = DashStyleDash;
663 break;
664
f6453568 665 case wxPENSTYLE_DOT_DASH :
83576e68
SC
666 dashStyle = DashStyleDashDot;
667 break;
f6453568 668 case wxPENSTYLE_USER_DASH :
83576e68
SC
669 {
670 dashStyle = DashStyleCustom;
671 wxDash *dashes;
672 int count = pen.GetDashes( &dashes );
673 if ((dashes != NULL) && (count > 0))
674 {
675 REAL *userLengths = new REAL[count];
676 for ( int i = 0; i < count; ++i )
677 {
678 userLengths[i] = dashes[i];
679 }
680 m_pen->SetDashPattern( userLengths, count);
681 delete[] userLengths;
682 }
683 }
684 break;
f6453568 685 case wxPENSTYLE_STIPPLE :
83576e68
SC
686 {
687 wxBitmap* bmp = pen.GetStipple();
a1b806b9 688 if ( bmp && bmp->IsOk() )
83576e68 689 {
e38f5435
DS
690 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
691#if wxUSE_PALETTE
692 (HPALETTE)bmp->GetPalette()->GetHPALETTE()
693#else
694 NULL
695#endif
696 );
83576e68
SC
697 m_penBrush = new TextureBrush(m_penImage);
698 m_pen->SetBrush( m_penBrush );
699 }
700
701 }
702 break;
703 default :
f6453568
VZ
704 if ( pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH &&
705 pen.GetStyle() <= wxPENSTYLE_LAST_HATCH )
83576e68 706 {
f6453568 707 HatchStyle style;
83576e68
SC
708 switch( pen.GetStyle() )
709 {
f6453568 710 case wxPENSTYLE_BDIAGONAL_HATCH :
83576e68
SC
711 style = HatchStyleBackwardDiagonal;
712 break ;
f6453568 713 case wxPENSTYLE_CROSSDIAG_HATCH :
83576e68
SC
714 style = HatchStyleDiagonalCross;
715 break ;
f6453568 716 case wxPENSTYLE_FDIAGONAL_HATCH :
83576e68
SC
717 style = HatchStyleForwardDiagonal;
718 break ;
f6453568 719 case wxPENSTYLE_CROSS_HATCH :
83576e68
SC
720 style = HatchStyleCross;
721 break ;
f6453568 722 case wxPENSTYLE_HORIZONTAL_HATCH :
83576e68
SC
723 style = HatchStyleHorizontal;
724 break ;
f6453568 725 case wxPENSTYLE_VERTICAL_HATCH :
83576e68
SC
726 style = HatchStyleVertical;
727 break ;
f6453568
VZ
728 default:
729 style = HatchStyleHorizontal;
83576e68 730 }
f49c80c1
VZ
731 m_penBrush = new HatchBrush
732 (
733 style,
734 wxColourToColor(pen.GetColour()),
735 Color::Transparent
736 );
83576e68
SC
737 m_pen->SetBrush( m_penBrush );
738 }
739 break;
cb3a0d42 740 }
83576e68
SC
741 if ( dashStyle != DashStyleSolid )
742 m_pen->SetDashStyle(dashStyle);
743}
744
83576e68
SC
745//-----------------------------------------------------------------------------
746// wxGDIPlusBrush implementation
747//-----------------------------------------------------------------------------
748
2c820406
SC
749wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer )
750: wxGraphicsObjectRefData(renderer)
83576e68
SC
751{
752 Init();
753}
754
2c820406
SC
755wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer , const wxBrush &brush )
756: wxGraphicsObjectRefData(renderer)
83576e68
SC
757{
758 Init();
759 if ( brush.GetStyle() == wxSOLID)
760 {
f49c80c1 761 m_brush = new SolidBrush(wxColourToColor( brush.GetColour()));
83576e68
SC
762 }
763 else if ( brush.IsHatch() )
764 {
f6453568 765 HatchStyle style;
83576e68
SC
766 switch( brush.GetStyle() )
767 {
f6453568 768 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
83576e68
SC
769 style = HatchStyleBackwardDiagonal;
770 break ;
f6453568 771 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
83576e68
SC
772 style = HatchStyleDiagonalCross;
773 break ;
f6453568 774 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
83576e68
SC
775 style = HatchStyleForwardDiagonal;
776 break ;
f6453568 777 case wxBRUSHSTYLE_CROSS_HATCH :
83576e68
SC
778 style = HatchStyleCross;
779 break ;
f6453568 780 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
83576e68
SC
781 style = HatchStyleHorizontal;
782 break ;
f6453568 783 case wxBRUSHSTYLE_VERTICAL_HATCH :
83576e68
SC
784 style = HatchStyleVertical;
785 break ;
f6453568
VZ
786 default:
787 style = HatchStyleHorizontal;
83576e68 788 }
f49c80c1
VZ
789 m_brush = new HatchBrush
790 (
791 style,
792 wxColourToColor(brush.GetColour()),
793 Color::Transparent
794 );
83576e68 795 }
cb3a0d42 796 else
83576e68
SC
797 {
798 wxBitmap* bmp = brush.GetStipple();
a1b806b9 799 if ( bmp && bmp->IsOk() )
83576e68
SC
800 {
801 wxDELETE( m_brushImage );
e38f5435
DS
802 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
803#if wxUSE_PALETTE
804 (HPALETTE)bmp->GetPalette()->GetHPALETTE()
805#else
806 NULL
807#endif
808 );
83576e68
SC
809 m_brush = new TextureBrush(m_brushImage);
810 }
811 }
812}
813
2c820406 814wxGDIPlusBrushData::~wxGDIPlusBrushData()
83576e68
SC
815{
816 delete m_brush;
817 delete m_brushImage;
818 delete m_brushPath;
819};
820
2c820406 821void wxGDIPlusBrushData::Init()
83576e68
SC
822{
823 m_brush = NULL;
824 m_brushImage= NULL;
825 m_brushPath= NULL;
826}
827
4ee4c7b9
VZ
828template <typename T>
829void
830wxGDIPlusBrushData::SetGradientStops(T *brush,
831 const wxGraphicsGradientStops& stops)
832{
833 const unsigned numStops = stops.GetCount();
834 if ( numStops <= 2 )
835 {
836 // initial and final colours are set during the brush creation, nothing
837 // more to do
838 return;
839 }
840
841 wxVector<Color> colors(numStops);
842 wxVector<REAL> positions(numStops);
843
844 for ( unsigned i = 0; i < numStops; i++ )
845 {
846 wxGraphicsGradientStop stop = stops.Item(i);
847
848 colors[i] = wxColourToColor(stop.GetColour());
849 positions[i] = stop.GetPosition();
850 }
851
852 brush->SetInterpolationColors(&colors[0], &positions[0], numStops);
853}
854
855void
856wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
857 wxDouble x2, wxDouble y2,
858 const wxGraphicsGradientStops& stops)
83576e68 859{
4ee4c7b9
VZ
860 LinearGradientBrush * const
861 brush = new LinearGradientBrush(PointF(x1, y1) , PointF(x2, y2),
862 wxColourToColor(stops.GetStartColour()),
863 wxColourToColor(stops.GetEndColour()));
864 m_brush = brush;
865
866 SetGradientStops(brush, stops);
83576e68
SC
867}
868
4ee4c7b9
VZ
869void
870wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
871 wxDouble xc, wxDouble yc,
872 wxDouble radius,
873 const wxGraphicsGradientStops& stops)
83576e68 874{
83576e68 875 m_brushPath = new GraphicsPath();
4ee4c7b9
VZ
876 m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius),
877 (REAL)(2*radius), (REAL)(2*radius));
83576e68 878
4ee4c7b9
VZ
879 PathGradientBrush * const brush = new PathGradientBrush(m_brushPath);
880 m_brush = brush;
881 brush->SetCenterPoint(PointF(xo, yo));
882 brush->SetCenterColor(wxColourToColor(stops.GetStartColour()));
83576e68 883
4ee4c7b9 884 const Color col(wxColourToColor(stops.GetEndColour()));
83576e68 885 int count = 1;
4ee4c7b9
VZ
886 brush->SetSurroundColors(&col, &count);
887
888 SetGradientStops(brush, stops);
83576e68
SC
889}
890
83576e68
SC
891//-----------------------------------------------------------------------------
892// wxGDIPlusFont implementation
893//-----------------------------------------------------------------------------
894
12493a5f
VZ
895void
896wxGDIPlusFontData::Init(const wxString& name,
897 REAL size,
898 int style,
fa378d36
VZ
899 const wxColour& col,
900 Unit fontUnit)
12493a5f 901{
c9a828e7 902 m_font = new Font(name.wc_str(), size, style, fontUnit);
12493a5f
VZ
903
904 m_textBrush = new SolidBrush(wxColourToColor(col));
905}
906
eb40c2d8 907wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer* renderer,
eb40c2d8
JS
908 const wxFont &font,
909 const wxColour& col )
910 : wxGraphicsObjectRefData( renderer )
83576e68 911{
83576e68
SC
912 int style = FontStyleRegular;
913 if ( font.GetStyle() == wxFONTSTYLE_ITALIC )
914 style |= FontStyleItalic;
915 if ( font.GetUnderlined() )
916 style |= FontStyleUnderline;
917 if ( font.GetWeight() == wxFONTWEIGHT_BOLD )
918 style |= FontStyleBold;
eb40c2d8 919
fa378d36
VZ
920 Init(font.GetFaceName(), font.GetPointSize(), style, col, UnitPoint);
921}
922
923wxGDIPlusFontData::wxGDIPlusFontData(wxGraphicsRenderer* renderer,
924 const wxString& name,
925 REAL sizeInPixels,
926 int style,
927 const wxColour& col) :
928 wxGraphicsObjectRefData(renderer)
929{
930 Init(name, sizeInPixels, style, col, UnitPixel);
83576e68 931}
6fea499c 932
2c820406 933wxGDIPlusFontData::~wxGDIPlusFontData()
83576e68
SC
934{
935 delete m_textBrush;
936 delete m_font;
937}
6fea499c 938
bce28872
SC
939// the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
940// premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
941// bytes as parameter, since there is no real copying of the data going in, only references are stored
942// m_helper has to be kept alive as well
943
944//-----------------------------------------------------------------------------
945// wxGDIPlusBitmapData implementation
946//-----------------------------------------------------------------------------
947
948wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap ) :
949 wxGraphicsObjectRefData( renderer ), m_bitmap( bitmap )
950{
951 m_helper = NULL;
952}
953
0b7dce54 954wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer,
bce28872
SC
955 const wxBitmap &bmp) : wxGraphicsObjectRefData( renderer )
956{
957 m_bitmap = NULL;
958 m_helper = NULL;
959
960 Bitmap* image = NULL;
961 if ( bmp.GetMask() )
962 {
e38f5435
DS
963 Bitmap interim((HBITMAP)bmp.GetHBITMAP(),
964#if wxUSE_PALETTE
965 (HPALETTE)bmp.GetPalette()->GetHPALETTE()
966#else
967 NULL
968#endif
969 );
bce28872
SC
970
971 size_t width = interim.GetWidth();
972 size_t height = interim.GetHeight();
973 Rect bounds(0,0,width,height);
974
975 image = new Bitmap(width,height,PixelFormat32bppPARGB) ;
976
977 Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL);
978 wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed);
979
980 BitmapData dataMask ;
981 interimMask.LockBits(&bounds,ImageLockModeRead,
982 interimMask.GetPixelFormat(),&dataMask);
983
984
985 BitmapData imageData ;
986 image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData);
987
988 BYTE maskPattern = 0 ;
989 BYTE maskByte = 0;
990 size_t maskIndex ;
991
992 for ( size_t y = 0 ; y < height ; ++y)
993 {
994 maskIndex = 0 ;
995 for( size_t x = 0 ; x < width; ++x)
996 {
997 if ( x % 8 == 0)
998 {
999 maskPattern = 0x80;
1000 maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex);
1001 maskIndex++;
1002 }
1003 else
1004 maskPattern = maskPattern >> 1;
1005
1006 ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4);
1007 if ( (maskByte & maskPattern) == 0 )
1008 *dest = 0x00000000;
1009 else
1010 {
1011 Color c ;
1012 interim.GetPixel(x,y,&c) ;
1013 *dest = (c.GetValue() | Color::AlphaMask);
1014 }
1015 }
1016 }
1017
1018 image->UnlockBits(&imageData);
1019
1020 interimMask.UnlockBits(&dataMask);
1021 interim.UnlockBits(&dataMask);
1022 }
1023 else
1024 {
e38f5435
DS
1025 image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),
1026#if wxUSE_PALETTE
1027 (HPALETTE)bmp.GetPalette()->GetHPALETTE()
1028#else
1029 NULL
1030#endif
1031 );
bce28872
SC
1032 if ( bmp.HasAlpha() && GetPixelFormatSize(image->GetPixelFormat()) == 32 )
1033 {
1034 size_t width = image->GetWidth();
1035 size_t height = image->GetHeight();
1036 Rect bounds(0,0,width,height);
1037 static BitmapData data ;
1038
1039 m_helper = image ;
1040 image = NULL ;
1041 m_helper->LockBits(&bounds, ImageLockModeRead,
1042 m_helper->GetPixelFormat(),&data);
1043
1044 image = new Bitmap(data.Width, data.Height, data.Stride,
1045 PixelFormat32bppPARGB , (BYTE*) data.Scan0);
1046
1047 m_helper->UnlockBits(&data);
1048 }
1049 }
1050 if ( image )
1051 m_bitmap = image;
1052}
1053
0a470e5e
VZ
1054#if wxUSE_IMAGE
1055
1056wxImage wxGDIPlusBitmapData::ConvertToImage() const
1057{
1058 // We could use Bitmap::LockBits() and convert to wxImage directly but
1059 // passing by wxBitmap is easier. It would be nice to measure performance
1060 // of the two methods but for this the second one would need to be written
1061 // first...
1062 HBITMAP hbmp;
1063 if ( m_bitmap->GetHBITMAP(Color(0xffffffff), &hbmp) != Gdiplus::Ok )
1064 return wxNullImage;
1065
1066 wxBitmap bmp;
1067 bmp.SetWidth(m_bitmap->GetWidth());
1068 bmp.SetHeight(m_bitmap->GetHeight());
1069 bmp.SetHBITMAP(hbmp);
1070 bmp.SetDepth(IsAlphaPixelFormat(m_bitmap->GetPixelFormat()) ? 32 : 24);
1071 return bmp.ConvertToImage();
1072}
1073
1074#endif // wxUSE_IMAGE
1075
bce28872
SC
1076wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
1077{
1078 delete m_bitmap;
1079 delete m_helper;
1080}
1081
83576e68
SC
1082//-----------------------------------------------------------------------------
1083// wxGDIPlusPath implementation
1084//-----------------------------------------------------------------------------
6fea499c 1085
a4e73390 1086wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path ) : wxGraphicsPathData(renderer)
83576e68
SC
1087{
1088 if ( path )
1089 m_path = path;
1090 else
1091 m_path = new GraphicsPath();
1092}
1093
a4e73390 1094wxGDIPlusPathData::~wxGDIPlusPathData()
6fea499c
SC
1095{
1096 delete m_path;
1097}
1098
a4e73390 1099wxGraphicsObjectRefData* wxGDIPlusPathData::Clone() const
83576e68 1100{
a4e73390 1101 return new wxGDIPlusPathData( GetRenderer() , m_path->Clone());
83576e68
SC
1102}
1103
6fea499c
SC
1104//
1105// The Primitives
1106//
1107
a4e73390 1108void wxGDIPlusPathData::MoveToPoint( wxDouble x , wxDouble y )
6fea499c
SC
1109{
1110 m_path->StartFigure();
1111 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
1112}
1113
a4e73390 1114void wxGDIPlusPathData::AddLineToPoint( wxDouble x , wxDouble y )
6fea499c
SC
1115{
1116 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
1117}
1118
a4e73390 1119void wxGDIPlusPathData::CloseSubpath()
6fea499c
SC
1120{
1121 m_path->CloseFigure();
1122}
1123
a4e73390 1124void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
6fea499c
SC
1125{
1126 PointF c1(cx1,cy1);
1127 PointF c2(cx2,cy2);
1128 PointF end(x,y);
1129 PointF start;
1130 m_path->GetLastPoint(&start);
1131 m_path->AddBezier(start,c1,c2,end);
1132}
1133
1134// gets the last point of the current path, (0,0) if not yet set
a4e73390 1135void wxGDIPlusPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
6fea499c
SC
1136{
1137 PointF start;
1138 m_path->GetLastPoint(&start);
a4e73390
SC
1139 *x = start.X ;
1140 *y = start.Y ;
6fea499c
SC
1141}
1142
cb3a0d42 1143void wxGDIPlusPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
6fea499c
SC
1144{
1145 double sweepAngle = endAngle - startAngle ;
c5b8c66a 1146 if( fabs(sweepAngle) >= 2*M_PI)
6fea499c
SC
1147 {
1148 sweepAngle = 2 * M_PI;
1149 }
1150 else
1151 {
1152 if ( clockwise )
1153 {
1154 if( sweepAngle < 0 )
1155 sweepAngle += 2 * M_PI;
1156 }
1157 else
1158 {
1159 if( sweepAngle > 0 )
1160 sweepAngle -= 2 * M_PI;
1161
1162 }
1163 }
cb3a0d42 1164 m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle));
6fea499c
SC
1165}
1166
a4e73390 1167void wxGDIPlusPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
6fea499c
SC
1168{
1169 m_path->AddRectangle(RectF(x,y,w,h));
1170}
1171
a4e73390 1172void wxGDIPlusPathData::AddPath( const wxGraphicsPathData* path )
6fea499c 1173{
83576e68 1174 m_path->AddPath( (GraphicsPath*) path->GetNativePath(), FALSE);
6fea499c
SC
1175}
1176
6fea499c 1177
83576e68 1178// transforms each point of this path by the matrix
cb3a0d42 1179void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData* matrix )
9f339b18 1180{
83576e68 1181 m_path->Transform( (Matrix*) matrix->GetNativeMatrix() );
9f339b18 1182}
6fea499c 1183
83576e68 1184// gets the bounding box enclosing all points (possibly including control points)
a4e73390 1185void wxGDIPlusPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
e49c065d 1186{
83576e68
SC
1187 RectF bounds;
1188 m_path->GetBounds( &bounds, NULL, NULL) ;
1189 *x = bounds.X;
1190 *y = bounds.Y;
1191 *w = bounds.Width;
1192 *h = bounds.Height;
e49c065d
RD
1193}
1194
94a007ec 1195bool wxGDIPlusPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
6fea499c 1196{
83576e68
SC
1197 m_path->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
1198 return m_path->IsVisible( (FLOAT) x,(FLOAT) y) == TRUE ;
1199}
9f339b18 1200
83576e68 1201//-----------------------------------------------------------------------------
a4e73390 1202// wxGDIPlusMatrixData implementation
83576e68 1203//-----------------------------------------------------------------------------
9f339b18 1204
a4e73390
SC
1205wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix )
1206 : wxGraphicsMatrixData(renderer)
9f339b18 1207{
83576e68
SC
1208 if ( matrix )
1209 m_matrix = matrix ;
1210 else
1211 m_matrix = new Matrix();
6fea499c
SC
1212}
1213
cb3a0d42 1214wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
6fea499c 1215{
83576e68 1216 delete m_matrix;
6fea499c
SC
1217}
1218
cb3a0d42 1219wxGraphicsObjectRefData *wxGDIPlusMatrixData::Clone() const
6fea499c 1220{
a4e73390 1221 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix->Clone());
539e2795
SC
1222}
1223
83576e68 1224// concatenates the matrix
a4e73390 1225void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData *t )
539e2795 1226{
83576e68 1227 m_matrix->Multiply( (Matrix*) t->GetNativeMatrix());
539e2795 1228}
83576e68 1229
83576e68 1230// sets the matrix to the respective values
cb3a0d42 1231void wxGDIPlusMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
83576e68 1232 wxDouble tx, wxDouble ty)
6fea499c 1233{
83576e68 1234 m_matrix->SetElements(a,b,c,d,tx,ty);
6fea499c
SC
1235}
1236
248802d0
RD
1237// gets the component valuess of the matrix
1238void wxGDIPlusMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1239 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1240{
1241 REAL elements[6];
1242 m_matrix->GetElements(elements);
1243 if (a) *a = elements[0];
1244 if (b) *b = elements[1];
1245 if (c) *c = elements[2];
1246 if (d) *d = elements[3];
1247 if (tx) *tx= elements[4];
1248 if (ty) *ty= elements[5];
1249}
1250
83576e68 1251// makes this the inverse matrix
a4e73390 1252void wxGDIPlusMatrixData::Invert()
6fea499c 1253{
83576e68 1254 m_matrix->Invert();
6fea499c
SC
1255}
1256
83576e68 1257// returns true if the elements of the transformation matrix are equal ?
cb3a0d42 1258bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
6fea499c 1259{
83576e68 1260 return m_matrix->Equals((Matrix*) t->GetNativeMatrix())== TRUE ;
6fea499c
SC
1261}
1262
83576e68 1263// return true if this is the identity matrix
a4e73390 1264bool wxGDIPlusMatrixData::IsIdentity() const
6fea499c 1265{
83576e68 1266 return m_matrix->IsIdentity() == TRUE ;
6fea499c
SC
1267}
1268
83576e68
SC
1269//
1270// transformation
1271//
1272
1273// add the translation to this matrix
a4e73390 1274void wxGDIPlusMatrixData::Translate( wxDouble dx , wxDouble dy )
6fea499c 1275{
83576e68 1276 m_matrix->Translate(dx,dy);
6fea499c
SC
1277}
1278
83576e68 1279// add the scale to this matrix
a4e73390 1280void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale )
6fea499c 1281{
83576e68 1282 m_matrix->Scale(xScale,yScale);
6fea499c
SC
1283}
1284
83576e68 1285// add the rotation to this matrix (radians)
a4e73390 1286void wxGDIPlusMatrixData::Rotate( wxDouble angle )
6fea499c 1287{
6f32f3ce 1288 m_matrix->Rotate( RadToDeg(angle) );
6fea499c
SC
1289}
1290
83576e68
SC
1291//
1292// apply the transforms
1293//
1294
1295// applies that matrix to the point
a4e73390 1296void wxGDIPlusMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
6fea499c 1297{
83576e68
SC
1298 PointF pt(*x,*y);
1299 m_matrix->TransformPoints(&pt);
1300 *x = pt.X;
1301 *y = pt.Y;
6fea499c
SC
1302}
1303
83576e68 1304// applies the matrix except for translations
a4e73390 1305void wxGDIPlusMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
6fea499c 1306{
83576e68
SC
1307 PointF pt(*dx,*dy);
1308 m_matrix->TransformVectors(&pt);
1309 *dx = pt.X;
1310 *dy = pt.Y;
6fea499c
SC
1311}
1312
83576e68 1313// returns the native representation
a4e73390 1314void * wxGDIPlusMatrixData::GetNativeMatrix() const
6fea499c 1315{
83576e68
SC
1316 return m_matrix;
1317}
6fea499c 1318
83576e68
SC
1319//-----------------------------------------------------------------------------
1320// wxGDIPlusContext implementation
1321//-----------------------------------------------------------------------------
1322
0c7bd159
SC
1323class wxGDIPlusOffsetHelper
1324{
1325public :
1326 wxGDIPlusOffsetHelper( Graphics* gr , bool offset )
1327 {
1328 m_gr = gr;
1329 m_offset = offset;
1330 if ( m_offset )
1331 m_gr->TranslateTransform( 0.5, 0.5 );
1332 }
1333 ~wxGDIPlusOffsetHelper( )
1334 {
1335 if ( m_offset )
1336 m_gr->TranslateTransform( -0.5, -0.5 );
1337 }
1338public :
1339 Graphics* m_gr;
1340 bool m_offset;
1341} ;
83576e68 1342
7fa3cf6a 1343wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc, wxDouble width, wxDouble height )
83576e68
SC
1344 : wxGraphicsContext(renderer)
1345{
645f9bd3 1346 Init(new Graphics(hdc), width, height);
6fea499c
SC
1347}
1348
eb40c2d8
JS
1349wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, const wxDC& dc )
1350 : wxGraphicsContext(renderer)
1351{
eb40c2d8
JS
1352 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1353 HDC hdc = (HDC) msw->GetHDC();
eb40c2d8 1354 wxSize sz = dc.GetSize();
eb40c2d8 1355
645f9bd3 1356 Init(new Graphics(hdc), sz.x, sz.y);
eb40c2d8
JS
1357}
1358
83576e68
SC
1359wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd )
1360 : wxGraphicsContext(renderer)
6fea499c 1361{
81e0b3d9 1362 RECT rect = wxGetWindowRect(hwnd);
645f9bd3
VZ
1363 Init(new Graphics(hwnd), rect.right - rect.left, rect.bottom - rect.top);
1364 m_enableOffset = true;
83576e68 1365}
6fea499c 1366
83576e68
SC
1367wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr )
1368 : wxGraphicsContext(renderer)
1369{
645f9bd3 1370 Init(gr, 0, 0);
83576e68 1371}
6fea499c 1372
645f9bd3
VZ
1373wxGDIPlusContext::wxGDIPlusContext(wxGraphicsRenderer* renderer)
1374 : wxGraphicsContext(renderer)
83576e68 1375{
645f9bd3
VZ
1376 // Derived class must call Init() later but just set m_context to NULL for
1377 // safety to avoid crashing in our dtor if Init() ends up not being called.
1378 m_context = NULL;
83576e68 1379}
6fea499c 1380
645f9bd3 1381void wxGDIPlusContext::Init(Graphics* graphics, int width, int height)
83576e68 1382{
645f9bd3 1383 m_context = graphics;
83576e68 1384 m_state1 = 0;
645f9bd3
VZ
1385 m_state2 = 0;
1386 m_width = width;
1387 m_height = height;
eb40c2d8 1388 m_fontScaleRatio = 1.0;
6fea499c 1389
0c7bd159
SC
1390 m_context->SetTextRenderingHint(TextRenderingHintSystemDefault);
1391 m_context->SetPixelOffsetMode(PixelOffsetModeHalf);
83576e68
SC
1392 m_context->SetSmoothingMode(SmoothingModeHighQuality);
1393 m_state1 = m_context->Save();
1394 m_state2 = m_context->Save();
1395}
6fea499c 1396
83576e68
SC
1397wxGDIPlusContext::~wxGDIPlusContext()
1398{
83576e68 1399 if ( m_context )
6fea499c 1400 {
83576e68
SC
1401 m_context->Restore( m_state2 );
1402 m_context->Restore( m_state1 );
1403 delete m_context;
6fea499c
SC
1404 }
1405}
1406
83576e68
SC
1407
1408void wxGDIPlusContext::Clip( const wxRegion &region )
6fea499c 1409{
93f72bae
SC
1410 Region rgn((HRGN)region.GetHRGN());
1411 m_context->SetClip(&rgn,CombineModeIntersect);
83576e68 1412}
6fea499c 1413
83576e68
SC
1414void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1415{
1416 m_context->SetClip(RectF(x,y,w,h),CombineModeIntersect);
1417}
cb3a0d42 1418
83576e68
SC
1419void wxGDIPlusContext::ResetClip()
1420{
1421 m_context->ResetClip();
1422}
6fea499c 1423
89db201a
SC
1424void wxGDIPlusContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1425{
1426 if (m_composition == wxCOMPOSITION_DEST)
1427 return;
1428
1429 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1430 Brush *brush = m_brush.IsNull() ? NULL : ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush();
1431 Pen *pen = m_pen.IsNull() ? NULL : ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen();
1432
1433 if ( brush )
1434 {
1435 // the offset is used to fill only the inside of the rectangle and not paint underneath
1436 // its border which may influence a transparent Pen
1437 REAL offset = 0;
1438 if ( pen )
1439 offset = pen->GetWidth();
1440 m_context->FillRectangle( brush, (REAL)x + offset/2, (REAL)y + offset/2, (REAL)w - offset, (REAL)h - offset);
1441 }
1442
1443 if ( pen )
1444 {
1445 m_context->DrawRectangle( pen, (REAL)x, (REAL)y, (REAL)w, (REAL)h );
1446 }
1447}
1448
0c7bd159
SC
1449void wxGDIPlusContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
1450{
bf02a7f9
SC
1451 if (m_composition == wxCOMPOSITION_DEST)
1452 return;
1453
03647350
VZ
1454 if ( !m_pen.IsNull() )
1455 {
1456 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1457 Point *cpoints = new Point[n];
1458 for (size_t i = 0; i < n; i++)
1459 {
1460 cpoints[i].X = (int)(points[i].m_x );
1461 cpoints[i].Y = (int)(points[i].m_y );
1462
1463 } // for (size_t i = 0; i < n; i++)
1464 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1465 delete[] cpoints;
1466 }
0c7bd159
SC
1467}
1468
94a007ec 1469void wxGDIPlusContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode WXUNUSED(fillStyle) )
0c7bd159 1470{
bf02a7f9
SC
1471 if (m_composition == wxCOMPOSITION_DEST)
1472 return;
1473
0c7bd159 1474 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
03647350
VZ
1475 Point *cpoints = new Point[n];
1476 for (size_t i = 0; i < n; i++)
1477 {
1478 cpoints[i].X = (int)(points[i].m_x );
1479 cpoints[i].Y = (int)(points[i].m_y );
1480
1481 } // for (int i = 0; i < n; i++)
1482 if ( !m_brush.IsNull() )
1483 m_context->FillPolygon( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , cpoints , n ) ;
1484 if ( !m_pen.IsNull() )
1485 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1486 delete[] cpoints;
0c7bd159
SC
1487}
1488
a4e73390 1489void wxGDIPlusContext::StrokePath( const wxGraphicsPath& path )
83576e68 1490{
bf02a7f9
SC
1491 if (m_composition == wxCOMPOSITION_DEST)
1492 return;
1493
2c820406 1494 if ( !m_pen.IsNull() )
83576e68 1495 {
0c7bd159 1496 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
a4e73390 1497 m_context->DrawPath( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath*) path.GetNativePath() );
83576e68 1498 }
6fea499c
SC
1499}
1500
94a007ec 1501void wxGDIPlusContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
6fea499c 1502{
bf02a7f9
SC
1503 if (m_composition == wxCOMPOSITION_DEST)
1504 return;
1505
2c820406 1506 if ( !m_brush.IsNull() )
83576e68 1507 {
0c7bd159 1508 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
a4e73390 1509 ((GraphicsPath*) path.GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
cb3a0d42 1510 m_context->FillPath( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() ,
a4e73390 1511 (GraphicsPath*) path.GetNativePath());
83576e68
SC
1512 }
1513}
6fea499c 1514
bf02a7f9
SC
1515bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias)
1516{
1517 if (m_antialias == antialias)
1518 return true;
03647350 1519
bf02a7f9 1520 m_antialias = antialias;
03647350 1521
bf02a7f9
SC
1522 SmoothingMode antialiasMode;
1523 switch (antialias)
1524 {
1525 case wxANTIALIAS_DEFAULT:
1526 antialiasMode = SmoothingModeHighQuality;
1527 break;
1528 case wxANTIALIAS_NONE:
1529 antialiasMode = SmoothingModeNone;
1530 break;
1531 default:
1532 return false;
1533 }
1534 m_context->SetSmoothingMode(antialiasMode);
1535 return true;
1536}
1537
133cb28b
SC
1538bool wxGDIPlusContext::SetInterpolationQuality(wxInterpolationQuality WXUNUSED(interpolation))
1539{
1540 // placeholder
1541 return false;
1542}
1543
bf02a7f9
SC
1544bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op)
1545{
1546 if ( m_composition == op )
1547 return true;
03647350 1548
bf02a7f9 1549 m_composition = op;
03647350 1550
bf02a7f9
SC
1551 if (m_composition == wxCOMPOSITION_DEST)
1552 return true;
1553
1554 CompositingMode cop;
1555 switch (op)
1556 {
1557 case wxCOMPOSITION_SOURCE:
1558 cop = CompositingModeSourceCopy;
1559 break;
1560 case wxCOMPOSITION_OVER:
1561 cop = CompositingModeSourceOver;
1562 break;
1563 default:
1564 return false;
1565 }
1566
1567 m_context->SetCompositingMode(cop);
1568 return true;
1569}
1570
c496d8fa 1571void wxGDIPlusContext::BeginLayer(wxDouble /* opacity */)
bf02a7f9
SC
1572{
1573 // TODO
1574}
1575
1576void wxGDIPlusContext::EndLayer()
1577{
1578 // TODO
03647350 1579}
bf02a7f9 1580
cb3a0d42 1581void wxGDIPlusContext::Rotate( wxDouble angle )
83576e68
SC
1582{
1583 m_context->RotateTransform( RadToDeg(angle) );
1584}
6fea499c 1585
cb3a0d42 1586void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
83576e68
SC
1587{
1588 m_context->TranslateTransform( dx , dy );
1589}
6fea499c 1590
83576e68
SC
1591void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
1592{
1593 m_context->ScaleTransform(xScale,yScale);
1594}
6fea499c 1595
83576e68
SC
1596void wxGDIPlusContext::PushState()
1597{
1598 GraphicsState state = m_context->Save();
bdba6fdc 1599 m_stateStack.push(state);
83576e68
SC
1600}
1601
cb3a0d42 1602void wxGDIPlusContext::PopState()
83576e68 1603{
401889f0
VZ
1604 wxCHECK_RET( !m_stateStack.empty(), wxT("No state to pop") );
1605
bdba6fdc
VZ
1606 GraphicsState state = m_stateStack.top();
1607 m_stateStack.pop();
83576e68 1608 m_context->Restore(state);
6fea499c
SC
1609}
1610
bce28872 1611void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
6fea499c 1612{
bf02a7f9
SC
1613 if (m_composition == wxCOMPOSITION_DEST)
1614 return;
1615
bce28872
SC
1616 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bmp.GetRefData())->GetGDIPlusBitmap();
1617 if ( image )
cb3a0d42 1618 {
bce28872 1619 if( image->GetWidth() != (UINT) w || image->GetHeight() != (UINT) h )
fe31db81 1620 {
bce28872
SC
1621 Rect drawRect((REAL) x, (REAL)y, (REAL)w, (REAL)h);
1622 m_context->SetPixelOffsetMode( PixelOffsetModeNone );
42604e44 1623 m_context->DrawImage(image, drawRect, 0 , 0 , image->GetWidth(), image->GetHeight(), UnitPixel ) ;
bce28872 1624 m_context->SetPixelOffsetMode( PixelOffsetModeHalf );
fe31db81 1625 }
bce28872
SC
1626 else
1627 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
fe31db81 1628 }
bce28872 1629}
fe31db81 1630
bce28872
SC
1631void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1632{
1633 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1634 DrawBitmap(bitmap, x, y, w, h);
6fea499c
SC
1635}
1636
cb3a0d42 1637void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
6fea499c 1638{
bf02a7f9
SC
1639 if (m_composition == wxCOMPOSITION_DEST)
1640 return;
1641
0c7bd159
SC
1642 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1643 // find out by looking at the bitmap data whether there really was alpha in it
fe31db81
SC
1644 HICON hIcon = (HICON)icon.GetHICON();
1645 ICONINFO iconInfo ;
1646 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1647 if (!GetIconInfo(hIcon,&iconInfo))
1648 return;
1649
fe31db81
SC
1650 Bitmap interim(iconInfo.hbmColor,NULL);
1651
1652 Bitmap* image = NULL ;
1653
0c7bd159
SC
1654 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1655 // work correctly, asking IsAlphaPixelFormat at this point fails as well
fe31db81
SC
1656 if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 )
1657 {
1658 image = Bitmap::FromHICON(hIcon);
1659 }
1660 else
1661 {
1662 size_t width = interim.GetWidth();
1663 size_t height = interim.GetHeight();
1664 Rect bounds(0,0,width,height);
1665 BitmapData data ;
1666
1667 interim.LockBits(&bounds, ImageLockModeRead,
1668 interim.GetPixelFormat(),&data);
0b7dce54 1669
0c7bd159
SC
1670 bool hasAlpha = false;
1671 for ( size_t y = 0 ; y < height && !hasAlpha ; ++y)
1672 {
1673 for( size_t x = 0 ; x < width && !hasAlpha; ++x)
1674 {
1675 ARGB *dest = (ARGB*)((BYTE*)data.Scan0 + data.Stride*y + x*4);
1676 if ( ( *dest & Color::AlphaMask ) != 0 )
1677 hasAlpha = true;
1678 }
1679 }
1680
1681 if ( hasAlpha )
1682 {
cb3a0d42 1683 image = new Bitmap(data.Width, data.Height, data.Stride,
fe31db81 1684 PixelFormat32bppARGB , (BYTE*) data.Scan0);
0c7bd159
SC
1685 }
1686 else
1687 {
1688 image = Bitmap::FromHICON(hIcon);
1689 }
1690
fe31db81
SC
1691 interim.UnlockBits(&data);
1692 }
1693
6fea499c 1694 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
fe31db81 1695
6fea499c 1696 delete image ;
fe31db81
SC
1697 DeleteObject(iconInfo.hbmColor);
1698 DeleteObject(iconInfo.hbmMask);
6fea499c
SC
1699}
1700
0b7dce54
VZ
1701void wxGDIPlusContext::DoDrawFilledText(const wxString& str,
1702 wxDouble x, wxDouble y,
1703 const wxGraphicsBrush& brush)
6fea499c 1704{
bf02a7f9
SC
1705 if (m_composition == wxCOMPOSITION_DEST)
1706 return;
1707
0b7dce54
VZ
1708 wxCHECK_RET( !m_font.IsNull(),
1709 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1011fbeb
SC
1710
1711 if ( str.IsEmpty())
6fea499c
SC
1712 return ;
1713
091111d6 1714 wxGDIPlusFontData * const
0b7dce54 1715 fontData = (wxGDIPlusFontData *)m_font.GetRefData();
091111d6
VZ
1716 wxGDIPlusBrushData * const
1717 brushData = (wxGDIPlusBrushData *)brush.GetRefData();
0b7dce54
VZ
1718
1719 m_context->DrawString
1720 (
1721 str.wc_str(*wxConvUI), // string to draw, always Unicode
1722 -1, // length: string is NUL-terminated
1723 fontData->GetGDIPlusFont(),
1724 PointF(x, y),
1725 StringFormat::GenericTypographic(),
1726 brushData ? brushData->GetGDIPlusBrush()
1727 : fontData->GetGDIPlusBrush()
1728 );
6fea499c
SC
1729}
1730
1731void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1732 wxDouble *descent, wxDouble *externalLeading ) const
1733{
1011fbeb
SC
1734 wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1735
6fea499c
SC
1736 wxWCharBuffer s = str.wc_str( *wxConvUI );
1737 FontFamily ffamily ;
2c820406 1738 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
cb3a0d42 1739
83576e68 1740 f->GetFamily(&ffamily) ;
6fea499c 1741
eb40c2d8 1742 REAL factorY = m_fontScaleRatio;
6fea499c
SC
1743
1744 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
83576e68 1745 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c 1746 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
83576e68 1747 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c 1748 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
83576e68 1749 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c
SC
1750
1751 if ( height )
595fda6f 1752 *height = rHeight * factorY;
6fea499c 1753 if ( descent )
595fda6f 1754 *descent = rDescent * factorY;
6fea499c 1755 if ( externalLeading )
595fda6f 1756 *externalLeading = (rHeight - rAscent - rDescent) * factorY;
6fea499c 1757 // measuring empty strings is not guaranteed, so do it by hand
cb3a0d42 1758 if ( str.IsEmpty())
6fea499c
SC
1759 {
1760 if ( width )
1761 *width = 0 ;
1762 }
1763 else
1764 {
6fea499c 1765 RectF layoutRect(0,0, 100000.0f, 100000.0f);
0c7bd159 1766 StringFormat strFormat( StringFormat::GenericTypographic() );
0b7dce54 1767 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
0c7bd159
SC
1768
1769 RectF bounds ;
1770 m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ;
6fea499c 1771 if ( width )
0c7bd159 1772 *width = bounds.Width;
465642da
JS
1773 if ( height )
1774 *height = bounds.Height;
6fea499c
SC
1775 }
1776}
1777
cb3a0d42 1778void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
6fea499c
SC
1779{
1780 widths.Empty();
1781 widths.Add(0, text.length());
1782
1011fbeb
SC
1783 wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1784
6fea499c
SC
1785 if (text.empty())
1786 return;
1787
2c820406 1788 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
6fea499c
SC
1789 wxWCharBuffer ws = text.wc_str( *wxConvUI );
1790 size_t len = wcslen( ws ) ;
1791 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1792
1793 RectF layoutRect(0,0, 100000.0f, 100000.0f);
0c7bd159 1794 StringFormat strFormat( StringFormat::GenericTypographic() );
6fea499c 1795
c315587c 1796 size_t startPosition = 0;
918df9d2 1797 size_t remainder = len;
c315587c
SC
1798 const size_t maxSpan = 32;
1799 CharacterRange* ranges = new CharacterRange[maxSpan] ;
1800 Region* regions = new Region[maxSpan];
6fea499c 1801
918df9d2 1802 while( remainder > 0 )
6fea499c 1803 {
918df9d2 1804 size_t span = wxMin( maxSpan, remainder );
c315587c
SC
1805
1806 for( size_t i = 0 ; i < span ; ++i)
1807 {
1808 ranges[i].First = 0 ;
1809 ranges[i].Length = startPosition+i+1 ;
1810 }
1811 strFormat.SetMeasurableCharacterRanges(span,ranges);
1812 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1813 m_context->MeasureCharacterRanges(ws, -1 , f,layoutRect, &strFormat,span,regions) ;
1814
1815 RectF bbox ;
1816 for ( size_t i = 0 ; i < span ; ++i)
1817 {
1818 regions[i].GetBounds(&bbox,m_context);
1819 widths[startPosition+i] = bbox.Width;
1820 }
918df9d2 1821 remainder -= span;
c315587c 1822 startPosition += span;
6fea499c 1823 }
c315587c
SC
1824
1825 delete[] ranges;
1826 delete[] regions;
6fea499c
SC
1827}
1828
d9485f89 1829bool wxGDIPlusContext::ShouldOffset() const
0b7dce54 1830{
639e9c7d
SC
1831 if ( !m_enableOffset )
1832 return false;
1833
d9485f89
RD
1834 int penwidth = 0 ;
1835 if ( !m_pen.IsNull() )
1836 {
1837 penwidth = (int)((wxGDIPlusPenData*)m_pen.GetRefData())->GetWidth();
1838 if ( penwidth == 0 )
1839 penwidth = 1;
1840 }
1841 return ( penwidth % 2 ) == 1;
1842}
1843
cb3a0d42 1844void* wxGDIPlusContext::GetNativeContext()
6fea499c 1845{
cb3a0d42 1846 return m_context;
6fea499c
SC
1847}
1848
83576e68 1849// concatenates this transform with the current transform of this context
a4e73390 1850void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix& matrix )
539e2795 1851{
a4e73390 1852 m_context->MultiplyTransform((Matrix*) matrix.GetNativeMatrix());
83576e68
SC
1853}
1854
1855// sets the transform of this context
a4e73390 1856void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix& matrix )
83576e68 1857{
a4e73390 1858 m_context->SetTransform((Matrix*) matrix.GetNativeMatrix());
83576e68
SC
1859}
1860
1861// gets the matrix of this context
a4e73390 1862wxGraphicsMatrix wxGDIPlusContext::GetTransform() const
83576e68 1863{
a4e73390
SC
1864 wxGraphicsMatrix matrix = CreateMatrix();
1865 m_context->GetTransform((Matrix*) matrix.GetNativeMatrix());
1866 return matrix;
83576e68 1867}
fbd5416f
SC
1868
1869void wxGDIPlusContext::GetSize( wxDouble* width, wxDouble *height )
1870{
81e0b3d9
SC
1871 *width = m_width;
1872 *height = m_height;
fbd5416f 1873}
eb40c2d8 1874
7fa3cf6a 1875//-----------------------------------------------------------------------------
eb40c2d8 1876// wxGDIPlusPrintingContext implementation
7fa3cf6a
JS
1877//-----------------------------------------------------------------------------
1878
eb40c2d8
JS
1879wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer,
1880 const wxDC& dc )
1881 : wxGDIPlusContext(renderer, dc)
7fa3cf6a 1882{
eb40c2d8 1883 Graphics* context = GetGraphics();
7fa3cf6a 1884
eb40c2d8 1885 //m_context->SetPageUnit(UnitDocument);
7fa3cf6a 1886
eb40c2d8
JS
1887 // Setup page scale, based on DPI ratio.
1888 // Antecedent should be 100dpi when the default page unit
1889 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1890 // instead. Note that calling SetPageScale() does not have effect on
1891 // non-printing DCs (that is, any other than wxPrinterDC or
1892 // wxEnhMetaFileDC).
1893 REAL dpiRatio = 100.0 / context->GetDpiY();
1894 context->SetPageScale(dpiRatio);
7fa3cf6a 1895
eb40c2d8
JS
1896 // We use this modifier when measuring fonts. It is needed because the
1897 // page scale is modified above.
1898 m_fontScaleRatio = context->GetDpiY() / 72.0;
1899}
83576e68
SC
1900
1901//-----------------------------------------------------------------------------
1902// wxGDIPlusRenderer implementation
1903//-----------------------------------------------------------------------------
1904
1905IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer,wxGraphicsRenderer)
1906
1907static wxGDIPlusRenderer gs_GDIPlusRenderer;
1908
1909wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1910{
1911 return &gs_GDIPlusRenderer;
1912}
1913
bce82a75 1914bool wxGDIPlusRenderer::EnsureIsLoaded()
83576e68 1915{
bce82a75
VS
1916 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1917 // if we already tried and failed (we don't want to spend lot of time
1918 // returning NULL from wxGraphicsContext::Create(), which may be called
1919 // relatively frequently):
1920 if ( m_loaded == -1 )
83576e68
SC
1921 {
1922 Load();
1923 }
bce82a75
VS
1924
1925 return m_loaded == 1;
83576e68
SC
1926}
1927
bce82a75
VS
1928// call EnsureIsLoaded() and return returnOnFail value if it fails
1929#define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1930 if ( !EnsureIsLoaded() ) \
1931 return (returnOnFail)
1932
1933
83576e68
SC
1934void wxGDIPlusRenderer::Load()
1935{
1936 GdiplusStartupInput input;
1937 GdiplusStartupOutput output;
bce82a75
VS
1938 if ( GdiplusStartup(&m_gditoken,&input,&output) == Gdiplus::Ok )
1939 {
1940 wxLogTrace("gdiplus", "successfully initialized GDI+");
1941 m_loaded = 1;
1942 }
1943 else
1944 {
1945 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1946 m_loaded = 0;
1947 }
83576e68
SC
1948}
1949
1950void wxGDIPlusRenderer::Unload()
1951{
1952 if ( m_gditoken )
385addaf 1953 {
83576e68 1954 GdiplusShutdown(m_gditoken);
5d0152c2 1955 m_gditoken = 0;
385addaf 1956 }
bce82a75 1957 m_loaded = -1; // next Load() will try again
83576e68
SC
1958}
1959
1960wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxWindowDC& dc)
1961{
bce82a75 1962 ENSURE_LOADED_OR_RETURN(NULL);
639e9c7d
SC
1963 wxGDIPlusContext* context = new wxGDIPlusContext(this, dc);
1964 context->EnableOffset(true);
1965 return context;
83576e68
SC
1966}
1967
c929ad91 1968#if wxUSE_PRINTING_ARCHITECTURE
0b822969 1969wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxPrinterDC& dc)
8371a353
JS
1970{
1971 ENSURE_LOADED_OR_RETURN(NULL);
eb40c2d8
JS
1972 wxGDIPlusContext* context = new wxGDIPlusPrintingContext(this, dc);
1973 return context;
8371a353 1974}
c929ad91 1975#endif
8371a353 1976
c929ad91 1977#if wxUSE_ENH_METAFILE
8371a353 1978wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC& dc)
0b822969 1979{
bce82a75 1980 ENSURE_LOADED_OR_RETURN(NULL);
eb40c2d8
JS
1981 wxGDIPlusContext* context = new wxGDIPlusPrintingContext(this, dc);
1982 return context;
0b822969 1983}
c929ad91 1984#endif
0b822969 1985
773ccc31
SC
1986wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxMemoryDC& dc)
1987{
bce82a75 1988 ENSURE_LOADED_OR_RETURN(NULL);
639e9c7d
SC
1989 wxGDIPlusContext* context = new wxGDIPlusContext(this, dc);
1990 context->EnableOffset(true);
1991 return context;
773ccc31
SC
1992}
1993
0a470e5e
VZ
1994#if wxUSE_IMAGE
1995wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromImage(wxImage& image)
1996{
1997 ENSURE_LOADED_OR_RETURN(NULL);
1998 wxGDIPlusContext* context = new wxGDIPlusImageContext(this, image);
1999 context->EnableOffset(true);
2000 return context;
2001}
2002
2003#endif // wxUSE_IMAGE
2004
091ef146
SC
2005wxGraphicsContext * wxGDIPlusRenderer::CreateMeasuringContext()
2006{
bce82a75 2007 ENSURE_LOADED_OR_RETURN(NULL);
0c7bd159 2008 return new wxGDIPlusMeasuringContext(this);
091ef146
SC
2009}
2010
83576e68
SC
2011wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeContext( void * context )
2012{
bce82a75 2013 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
2014 return new wxGDIPlusContext(this,(Graphics*) context);
2015}
2016
2017
2018wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window )
2019{
bce82a75 2020 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
2021 return new wxGDIPlusContext(this,(HWND) window);
2022}
2023
2024wxGraphicsContext * wxGDIPlusRenderer::CreateContext( wxWindow* window )
2025{
bce82a75 2026 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
2027 return new wxGDIPlusContext(this, (HWND) window->GetHWND() );
2028}
2029
2030// Path
2031
a4e73390 2032wxGraphicsPath wxGDIPlusRenderer::CreatePath()
83576e68 2033{
bce82a75 2034 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath);
a4e73390
SC
2035 wxGraphicsPath m;
2036 m.SetRefData( new wxGDIPlusPathData(this));
2037 return m;
83576e68
SC
2038}
2039
2040
2041// Matrix
2042
cb3a0d42 2043wxGraphicsMatrix wxGDIPlusRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
83576e68
SC
2044 wxDouble tx, wxDouble ty)
2045
2046{
bce82a75 2047 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix);
a4e73390
SC
2048 wxGraphicsMatrix m;
2049 wxGDIPlusMatrixData* data = new wxGDIPlusMatrixData( this );
2050 data->Set( a,b,c,d,tx,ty ) ;
2051 m.SetRefData(data);
83576e68 2052 return m;
539e2795
SC
2053}
2054
cb3a0d42 2055wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen)
6fea499c 2056{
bce82a75 2057 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen);
a1b806b9 2058 if ( !pen.IsOk() || pen.GetStyle() == wxTRANSPARENT )
2c820406 2059 return wxNullGraphicsPen;
83576e68 2060 else
2c820406
SC
2061 {
2062 wxGraphicsPen p;
2063 p.SetRefData(new wxGDIPlusPenData( this, pen ));
2064 return p;
2065 }
6fea499c 2066}
7ba86d93 2067
cb3a0d42 2068wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush )
539e2795 2069{
bce82a75 2070 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
a1b806b9 2071 if ( !brush.IsOk() || brush.GetStyle() == wxTRANSPARENT )
2c820406 2072 return wxNullGraphicsBrush;
83576e68 2073 else
2c820406
SC
2074 {
2075 wxGraphicsBrush p;
2076 p.SetRefData(new wxGDIPlusBrushData( this, brush ));
2077 return p;
2078 }
539e2795
SC
2079}
2080
4ee4c7b9
VZ
2081wxGraphicsBrush
2082wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2083 wxDouble x2, wxDouble y2,
2084 const wxGraphicsGradientStops& stops)
539e2795 2085{
bce82a75 2086 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2c820406
SC
2087 wxGraphicsBrush p;
2088 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
4ee4c7b9 2089 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
2c820406
SC
2090 p.SetRefData(d);
2091 return p;
2092 }
539e2795 2093
4ee4c7b9
VZ
2094wxGraphicsBrush
2095wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2096 wxDouble xc, wxDouble yc,
2097 wxDouble radius,
2098 const wxGraphicsGradientStops& stops)
83576e68 2099{
bce82a75 2100 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2c820406
SC
2101 wxGraphicsBrush p;
2102 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
4ee4c7b9 2103 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,stops);
2c820406
SC
2104 p.SetRefData(d);
2105 return p;
83576e68 2106}
539e2795 2107
eb40c2d8 2108wxGraphicsFont
a95f35b0
VZ
2109wxGDIPlusRenderer::CreateFont( const wxFont &font,
2110 const wxColour &col )
83576e68 2111{
bce82a75 2112 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
a1b806b9 2113 if ( font.IsOk() )
cb3a0d42 2114 {
2c820406 2115 wxGraphicsFont p;
a95f35b0 2116 p.SetRefData(new wxGDIPlusFontData( this, font, col ));
2c820406
SC
2117 return p;
2118 }
83576e68 2119 else
2c820406 2120 return wxNullGraphicsFont;
83576e68 2121}
7ba86d93 2122
fa378d36
VZ
2123wxGraphicsFont
2124wxGDIPlusRenderer::CreateFont(double size,
2125 const wxString& facename,
2126 int flags,
2127 const wxColour& col)
2128{
2129 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
2130
2131 // Convert wxFont flags to GDI+ style:
2132 int style = FontStyleRegular;
2133 if ( flags & wxFONTFLAG_ITALIC )
2134 style |= FontStyleItalic;
2135 if ( flags & wxFONTFLAG_UNDERLINED )
2136 style |= FontStyleUnderline;
2137 if ( flags & wxFONTFLAG_BOLD )
2138 style |= FontStyleBold;
2139 if ( flags & wxFONTFLAG_STRIKETHROUGH )
2140 style |= FontStyleStrikeout;
2141
2142
2143 wxGraphicsFont f;
2144 f.SetRefData(new wxGDIPlusFontData(this, facename, size, style, col));
2145 return f;
2146}
2147
bce28872
SC
2148wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmap( const wxBitmap &bitmap )
2149{
bce82a75 2150 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
a1b806b9 2151 if ( bitmap.IsOk() )
bce28872
SC
2152 {
2153 wxGraphicsBitmap p;
2154 p.SetRefData(new wxGDIPlusBitmapData( this , bitmap ));
2155 return p;
2156 }
2157 else
2158 return wxNullGraphicsBitmap;
2159}
2160
0a470e5e
VZ
2161#if wxUSE_IMAGE
2162
2163wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmapFromImage(const wxImage& image)
2164{
2165 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2166 if ( image.IsOk() )
2167 {
2168 // Notice that we rely on conversion from wxImage to wxBitmap here but
2169 // we could probably do it more efficiently by converting from wxImage
2170 // to GDI+ Bitmap directly, i.e. copying wxImage pixels to the buffer
2171 // returned by Bitmap::LockBits(). However this would require writing
2172 // code specific for this task while like this we can reuse existing
2173 // code (see also wxGDIPlusBitmapData::ConvertToImage()).
2174 wxGraphicsBitmap gb;
2175 gb.SetRefData(new wxGDIPlusBitmapData(this, image));
2176 return gb;
2177 }
2178 else
2179 return wxNullGraphicsBitmap;
2180}
2181
6e6f074b
RD
2182
2183wxImage wxGDIPlusRenderer::CreateImageFromBitmap(const wxGraphicsBitmap& bmp)
2184{
2185 ENSURE_LOADED_OR_RETURN(wxNullImage);
2186 const wxGDIPlusBitmapData* const
2187 data = static_cast<wxGDIPlusBitmapData*>(bmp.GetGraphicsData());
2188
2189 return data ? data->ConvertToImage() : wxNullImage;
2190}
2191
0a470e5e
VZ
2192#endif // wxUSE_IMAGE
2193
6e6f074b 2194
2986eb86
SC
2195wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap )
2196{
2197 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2198 if ( bitmap != NULL )
2199 {
2200 wxGraphicsBitmap p;
2201 p.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap*) bitmap ));
2202 return p;
2203 }
2204 else
2205 return wxNullGraphicsBitmap;
2206}
2207
bce28872
SC
2208wxGraphicsBitmap wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2209{
bce82a75 2210 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
bce28872
SC
2211 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bitmap.GetRefData())->GetGDIPlusBitmap();
2212 if ( image )
2213 {
2214 wxGraphicsBitmap p;
2215 p.SetRefData(new wxGDIPlusBitmapData( this , image->Clone( (REAL) x , (REAL) y , (REAL) w , (REAL) h , PixelFormat32bppPARGB) ));
2216 return p;
2217 }
2218 else
2219 return wxNullGraphicsBitmap;
2220}
2221
385addaf
VZ
2222// Shutdown GDI+ at app exit, before possible dll unload
2223class wxGDIPlusRendererModule : public wxModule
2224{
2225public:
2226 virtual bool OnInit() { return true; }
2227 virtual void OnExit() { gs_GDIPlusRenderer.Unload(); }
2228
2229private:
2230 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule)
2231};
2232
2233IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule)
2234
942d5e2d
VZ
2235// ----------------------------------------------------------------------------
2236// wxMSW-specific parts of wxGCDC
2237// ----------------------------------------------------------------------------
2238
2239WXHDC wxGCDC::AcquireHDC()
2240{
2241 wxGraphicsContext * const gc = GetGraphicsContext();
2242 if ( !gc )
2243 return NULL;
2244
932d0768
RD
2245#if wxUSE_CAIRO
2246 // we can't get the HDC if it is not a GDI+ context
2247 wxGraphicsRenderer* r1 = gc->GetRenderer();
2248 wxGraphicsRenderer* r2 = wxGraphicsRenderer::GetCairoRenderer();
2249 if (r1 == r2)
2250 return NULL;
2251#endif
2252
942d5e2d
VZ
2253 Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
2254 return g ? g->GetHDC() : NULL;
2255}
2256
2257void wxGCDC::ReleaseHDC(WXHDC hdc)
2258{
2259 if ( !hdc )
2260 return;
2261
2262 wxGraphicsContext * const gc = GetGraphicsContext();
2263 wxCHECK_RET( gc, "can't release HDC because there is no wxGraphicsContext" );
2264
932d0768
RD
2265#if wxUSE_CAIRO
2266 // we can't get the HDC if it is not a GDI+ context
2267 wxGraphicsRenderer* r1 = gc->GetRenderer();
2268 wxGraphicsRenderer* r2 = wxGraphicsRenderer::GetCairoRenderer();
2269 if (r1 == r2)
2270 return;
2271#endif
2272
942d5e2d
VZ
2273 Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
2274 wxCHECK_RET( g, "can't release HDC because there is no Graphics" );
2275
2276 g->ReleaseHDC((HDC)hdc);
2277}
2278
7ba86d93 2279#endif // wxUSE_GRAPHICS_CONTEXT