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