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