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