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