]> git.saurik.com Git - wxWidgets.git/blame - src/msw/graphics.cpp
guard against nullptr
[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
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;
b1bf7dc7 388 wxStack<GraphicsState> m_stateStack;
83576e68
SC
389 GraphicsState m_state1;
390 GraphicsState m_state2;
391
392 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext)
393};
394
5aac6f3f 395class wxGDIPlusMeasuringContext : public wxGDIPlusContext
0c7bd159
SC
396{
397public:
81e0b3d9 398 wxGDIPlusMeasuringContext( wxGraphicsRenderer* renderer ) : wxGDIPlusContext( renderer , m_hdc = GetDC(NULL), 1000, 1000 )
0c7bd159
SC
399 {
400 }
401 wxGDIPlusMeasuringContext()
402 {
403 }
404
405 virtual ~wxGDIPlusMeasuringContext()
406 {
407 ReleaseDC( NULL, m_hdc );
408 }
409
410private:
7fa3cf6a 411 HDC m_hdc ;
0c7bd159
SC
412 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext)
413} ;
414
eb40c2d8
JS
415class wxGDIPlusPrintingContext : public wxGDIPlusContext
416{
417public:
418 wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer, const wxDC& dc );
419 virtual ~wxGDIPlusPrintingContext() { }
420protected:
421};
422
423//-----------------------------------------------------------------------------
424// wxGDIPlusRenderer declaration
425//-----------------------------------------------------------------------------
426
427class wxGDIPlusRenderer : public wxGraphicsRenderer
428{
429public :
430 wxGDIPlusRenderer()
431 {
432 m_loaded = -1;
433 m_gditoken = 0;
434 }
435
436 virtual ~wxGDIPlusRenderer()
437 {
438 if ( m_loaded == 1 )
439 {
440 Unload();
441 }
442 }
443
444 // Context
445
446 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
447
448 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
449
450#if wxUSE_PRINTING_ARCHITECTURE
451 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
452#endif
453
454#if wxUSE_ENH_METAFILE
455 virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc);
456#endif
457
458 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
459
460 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
461
462 virtual wxGraphicsContext * CreateContext( wxWindow* window );
463
464 virtual wxGraphicsContext * CreateMeasuringContext();
465
466 // Path
467
468 virtual wxGraphicsPath CreatePath();
469
470 // Matrix
471
472 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
473 wxDouble tx=0.0, wxDouble ty=0.0);
474
475
476 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
477
478 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
479
480 virtual wxGraphicsBrush
481 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
482 wxDouble x2, wxDouble y2,
483 const wxGraphicsGradientStops& stops);
484
485 virtual wxGraphicsBrush
486 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
487 wxDouble xc, wxDouble yc,
488 wxDouble radius,
489 const wxGraphicsGradientStops& stops);
490
491 // create a native bitmap representation
492 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
493
494 // stub: should not be called directly
495 virtual wxGraphicsFont CreateFont( const wxFont& WXUNUSED(font),
496 const wxColour& WXUNUSED(col) )
497 { wxFAIL; return wxNullGraphicsFont; }
498
499 // this is used to really create the font
500 wxGraphicsFont CreateGDIPlusFont( const wxGDIPlusContext* gc,
501 const wxFont &font,
502 const wxColour &col );
503
504 // create a graphics bitmap from a native bitmap
505 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
506
507 // create a subimage from a native image representation
508 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
509
510protected :
511 bool EnsureIsLoaded();
512 void Load();
513 void Unload();
514 friend class wxGDIPlusRendererModule;
515
516private :
517 int m_loaded;
518 ULONG_PTR m_gditoken;
519
520 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer)
521} ;
522
83576e68
SC
523//-----------------------------------------------------------------------------
524// wxGDIPlusPen implementation
525//-----------------------------------------------------------------------------
526
2c820406 527wxGDIPlusPenData::~wxGDIPlusPenData()
83576e68
SC
528{
529 delete m_pen;
530 delete m_penImage;
531 delete m_penBrush;
532}
533
2c820406 534void wxGDIPlusPenData::Init()
83576e68
SC
535{
536 m_pen = NULL ;
537 m_penImage = NULL;
538 m_penBrush = NULL;
539}
540
2c820406
SC
541wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
542: wxGraphicsObjectRefData(renderer)
cb3a0d42 543{
83576e68
SC
544 Init();
545 m_width = pen.GetWidth();
546 if (m_width <= 0.0)
547 m_width = 0.1;
548
f49c80c1 549 m_pen = new Pen(wxColourToColor(pen.GetColour()), m_width );
83576e68
SC
550
551 LineCap cap;
552 switch ( pen.GetCap() )
553 {
554 case wxCAP_ROUND :
555 cap = LineCapRound;
556 break;
557
558 case wxCAP_PROJECTING :
559 cap = LineCapSquare;
560 break;
561
562 case wxCAP_BUTT :
563 cap = LineCapFlat; // TODO verify
564 break;
565
566 default :
567 cap = LineCapFlat;
568 break;
569 }
570 m_pen->SetLineCap(cap,cap, DashCapFlat);
571
572 LineJoin join;
573 switch ( pen.GetJoin() )
574 {
575 case wxJOIN_BEVEL :
576 join = LineJoinBevel;
577 break;
578
579 case wxJOIN_MITER :
580 join = LineJoinMiter;
581 break;
582
583 case wxJOIN_ROUND :
584 join = LineJoinRound;
585 break;
586
587 default :
588 join = LineJoinMiter;
589 break;
590 }
591
592 m_pen->SetLineJoin(join);
593
594 m_pen->SetDashStyle(DashStyleSolid);
595
596 DashStyle dashStyle = DashStyleSolid;
597 switch ( pen.GetStyle() )
598 {
f6453568 599 case wxPENSTYLE_SOLID :
83576e68
SC
600 break;
601
f6453568 602 case wxPENSTYLE_DOT :
83576e68
SC
603 dashStyle = DashStyleDot;
604 break;
605
f6453568 606 case wxPENSTYLE_LONG_DASH :
83576e68
SC
607 dashStyle = DashStyleDash; // TODO verify
608 break;
609
f6453568 610 case wxPENSTYLE_SHORT_DASH :
83576e68
SC
611 dashStyle = DashStyleDash;
612 break;
613
f6453568 614 case wxPENSTYLE_DOT_DASH :
83576e68
SC
615 dashStyle = DashStyleDashDot;
616 break;
f6453568 617 case wxPENSTYLE_USER_DASH :
83576e68
SC
618 {
619 dashStyle = DashStyleCustom;
620 wxDash *dashes;
621 int count = pen.GetDashes( &dashes );
622 if ((dashes != NULL) && (count > 0))
623 {
624 REAL *userLengths = new REAL[count];
625 for ( int i = 0; i < count; ++i )
626 {
627 userLengths[i] = dashes[i];
628 }
629 m_pen->SetDashPattern( userLengths, count);
630 delete[] userLengths;
631 }
632 }
633 break;
f6453568 634 case wxPENSTYLE_STIPPLE :
83576e68
SC
635 {
636 wxBitmap* bmp = pen.GetStipple();
a1b806b9 637 if ( bmp && bmp->IsOk() )
83576e68 638 {
e38f5435
DS
639 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
640#if wxUSE_PALETTE
641 (HPALETTE)bmp->GetPalette()->GetHPALETTE()
642#else
643 NULL
644#endif
645 );
83576e68
SC
646 m_penBrush = new TextureBrush(m_penImage);
647 m_pen->SetBrush( m_penBrush );
648 }
649
650 }
651 break;
652 default :
f6453568
VZ
653 if ( pen.GetStyle() >= wxPENSTYLE_FIRST_HATCH &&
654 pen.GetStyle() <= wxPENSTYLE_LAST_HATCH )
83576e68 655 {
f6453568 656 HatchStyle style;
83576e68
SC
657 switch( pen.GetStyle() )
658 {
f6453568 659 case wxPENSTYLE_BDIAGONAL_HATCH :
83576e68
SC
660 style = HatchStyleBackwardDiagonal;
661 break ;
f6453568 662 case wxPENSTYLE_CROSSDIAG_HATCH :
83576e68
SC
663 style = HatchStyleDiagonalCross;
664 break ;
f6453568 665 case wxPENSTYLE_FDIAGONAL_HATCH :
83576e68
SC
666 style = HatchStyleForwardDiagonal;
667 break ;
f6453568 668 case wxPENSTYLE_CROSS_HATCH :
83576e68
SC
669 style = HatchStyleCross;
670 break ;
f6453568 671 case wxPENSTYLE_HORIZONTAL_HATCH :
83576e68
SC
672 style = HatchStyleHorizontal;
673 break ;
f6453568 674 case wxPENSTYLE_VERTICAL_HATCH :
83576e68
SC
675 style = HatchStyleVertical;
676 break ;
f6453568
VZ
677 default:
678 style = HatchStyleHorizontal;
83576e68 679 }
f49c80c1
VZ
680 m_penBrush = new HatchBrush
681 (
682 style,
683 wxColourToColor(pen.GetColour()),
684 Color::Transparent
685 );
83576e68
SC
686 m_pen->SetBrush( m_penBrush );
687 }
688 break;
cb3a0d42 689 }
83576e68
SC
690 if ( dashStyle != DashStyleSolid )
691 m_pen->SetDashStyle(dashStyle);
692}
693
83576e68
SC
694//-----------------------------------------------------------------------------
695// wxGDIPlusBrush implementation
696//-----------------------------------------------------------------------------
697
2c820406
SC
698wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer )
699: wxGraphicsObjectRefData(renderer)
83576e68
SC
700{
701 Init();
702}
703
2c820406
SC
704wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer , const wxBrush &brush )
705: wxGraphicsObjectRefData(renderer)
83576e68
SC
706{
707 Init();
708 if ( brush.GetStyle() == wxSOLID)
709 {
f49c80c1 710 m_brush = new SolidBrush(wxColourToColor( brush.GetColour()));
83576e68
SC
711 }
712 else if ( brush.IsHatch() )
713 {
f6453568 714 HatchStyle style;
83576e68
SC
715 switch( brush.GetStyle() )
716 {
f6453568 717 case wxBRUSHSTYLE_BDIAGONAL_HATCH :
83576e68
SC
718 style = HatchStyleBackwardDiagonal;
719 break ;
f6453568 720 case wxBRUSHSTYLE_CROSSDIAG_HATCH :
83576e68
SC
721 style = HatchStyleDiagonalCross;
722 break ;
f6453568 723 case wxBRUSHSTYLE_FDIAGONAL_HATCH :
83576e68
SC
724 style = HatchStyleForwardDiagonal;
725 break ;
f6453568 726 case wxBRUSHSTYLE_CROSS_HATCH :
83576e68
SC
727 style = HatchStyleCross;
728 break ;
f6453568 729 case wxBRUSHSTYLE_HORIZONTAL_HATCH :
83576e68
SC
730 style = HatchStyleHorizontal;
731 break ;
f6453568 732 case wxBRUSHSTYLE_VERTICAL_HATCH :
83576e68
SC
733 style = HatchStyleVertical;
734 break ;
f6453568
VZ
735 default:
736 style = HatchStyleHorizontal;
83576e68 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();
a1b806b9 748 if ( bmp && bmp->IsOk() )
83576e68
SC
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 1300 Init();
639e9c7d 1301 m_enableOffset = true;
83576e68 1302 m_context = new Graphics( hwnd);
81e0b3d9
SC
1303 RECT rect = wxGetWindowRect(hwnd);
1304 m_width = rect.right - rect.left;
1305 m_height = rect.bottom - rect.top;
83576e68
SC
1306 SetDefaults();
1307}
6fea499c 1308
83576e68
SC
1309wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr )
1310 : wxGraphicsContext(renderer)
1311{
1312 Init();
1313 m_context = gr;
1314 SetDefaults();
1315}
6fea499c 1316
83576e68
SC
1317wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL)
1318{
1319 Init();
1320}
6fea499c 1321
83576e68
SC
1322void wxGDIPlusContext::Init()
1323{
1324 m_context = NULL;
1325 m_state1 = 0;
1326 m_state2= 0;
81e0b3d9
SC
1327 m_height = 0;
1328 m_width = 0;
eb40c2d8 1329 m_fontScaleRatio = 1.0;
83576e68 1330}
6fea499c 1331
83576e68
SC
1332void wxGDIPlusContext::SetDefaults()
1333{
0c7bd159
SC
1334 m_context->SetTextRenderingHint(TextRenderingHintSystemDefault);
1335 m_context->SetPixelOffsetMode(PixelOffsetModeHalf);
83576e68
SC
1336 m_context->SetSmoothingMode(SmoothingModeHighQuality);
1337 m_state1 = m_context->Save();
1338 m_state2 = m_context->Save();
1339}
6fea499c 1340
83576e68
SC
1341wxGDIPlusContext::~wxGDIPlusContext()
1342{
83576e68 1343 if ( m_context )
6fea499c 1344 {
83576e68
SC
1345 m_context->Restore( m_state2 );
1346 m_context->Restore( m_state1 );
1347 delete m_context;
6fea499c
SC
1348 }
1349}
1350
83576e68
SC
1351
1352void wxGDIPlusContext::Clip( const wxRegion &region )
6fea499c 1353{
93f72bae
SC
1354 Region rgn((HRGN)region.GetHRGN());
1355 m_context->SetClip(&rgn,CombineModeIntersect);
83576e68 1356}
6fea499c 1357
83576e68
SC
1358void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1359{
1360 m_context->SetClip(RectF(x,y,w,h),CombineModeIntersect);
1361}
cb3a0d42 1362
83576e68
SC
1363void wxGDIPlusContext::ResetClip()
1364{
1365 m_context->ResetClip();
1366}
6fea499c 1367
89db201a
SC
1368void wxGDIPlusContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1369{
1370 if (m_composition == wxCOMPOSITION_DEST)
1371 return;
1372
1373 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1374 Brush *brush = m_brush.IsNull() ? NULL : ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush();
1375 Pen *pen = m_pen.IsNull() ? NULL : ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen();
1376
1377 if ( brush )
1378 {
1379 // the offset is used to fill only the inside of the rectangle and not paint underneath
1380 // its border which may influence a transparent Pen
1381 REAL offset = 0;
1382 if ( pen )
1383 offset = pen->GetWidth();
1384 m_context->FillRectangle( brush, (REAL)x + offset/2, (REAL)y + offset/2, (REAL)w - offset, (REAL)h - offset);
1385 }
1386
1387 if ( pen )
1388 {
1389 m_context->DrawRectangle( pen, (REAL)x, (REAL)y, (REAL)w, (REAL)h );
1390 }
1391}
1392
0c7bd159
SC
1393void wxGDIPlusContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
1394{
bf02a7f9
SC
1395 if (m_composition == wxCOMPOSITION_DEST)
1396 return;
1397
03647350
VZ
1398 if ( !m_pen.IsNull() )
1399 {
1400 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1401 Point *cpoints = new Point[n];
1402 for (size_t i = 0; i < n; i++)
1403 {
1404 cpoints[i].X = (int)(points[i].m_x );
1405 cpoints[i].Y = (int)(points[i].m_y );
1406
1407 } // for (size_t i = 0; i < n; i++)
1408 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1409 delete[] cpoints;
1410 }
0c7bd159
SC
1411}
1412
94a007ec 1413void wxGDIPlusContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode WXUNUSED(fillStyle) )
0c7bd159 1414{
bf02a7f9
SC
1415 if (m_composition == wxCOMPOSITION_DEST)
1416 return;
1417
0c7bd159 1418 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
03647350
VZ
1419 Point *cpoints = new Point[n];
1420 for (size_t i = 0; i < n; i++)
1421 {
1422 cpoints[i].X = (int)(points[i].m_x );
1423 cpoints[i].Y = (int)(points[i].m_y );
1424
1425 } // for (int i = 0; i < n; i++)
1426 if ( !m_brush.IsNull() )
1427 m_context->FillPolygon( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , cpoints , n ) ;
1428 if ( !m_pen.IsNull() )
1429 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1430 delete[] cpoints;
0c7bd159
SC
1431}
1432
a4e73390 1433void wxGDIPlusContext::StrokePath( const wxGraphicsPath& path )
83576e68 1434{
bf02a7f9
SC
1435 if (m_composition == wxCOMPOSITION_DEST)
1436 return;
1437
2c820406 1438 if ( !m_pen.IsNull() )
83576e68 1439 {
0c7bd159 1440 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
a4e73390 1441 m_context->DrawPath( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath*) path.GetNativePath() );
83576e68 1442 }
6fea499c
SC
1443}
1444
94a007ec 1445void wxGDIPlusContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
6fea499c 1446{
bf02a7f9
SC
1447 if (m_composition == wxCOMPOSITION_DEST)
1448 return;
1449
2c820406 1450 if ( !m_brush.IsNull() )
83576e68 1451 {
0c7bd159 1452 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
a4e73390 1453 ((GraphicsPath*) path.GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
cb3a0d42 1454 m_context->FillPath( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() ,
a4e73390 1455 (GraphicsPath*) path.GetNativePath());
83576e68
SC
1456 }
1457}
6fea499c 1458
bf02a7f9
SC
1459bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias)
1460{
1461 if (m_antialias == antialias)
1462 return true;
03647350 1463
bf02a7f9 1464 m_antialias = antialias;
03647350 1465
bf02a7f9
SC
1466 SmoothingMode antialiasMode;
1467 switch (antialias)
1468 {
1469 case wxANTIALIAS_DEFAULT:
1470 antialiasMode = SmoothingModeHighQuality;
1471 break;
1472 case wxANTIALIAS_NONE:
1473 antialiasMode = SmoothingModeNone;
1474 break;
1475 default:
1476 return false;
1477 }
1478 m_context->SetSmoothingMode(antialiasMode);
1479 return true;
1480}
1481
1482bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op)
1483{
1484 if ( m_composition == op )
1485 return true;
03647350 1486
bf02a7f9 1487 m_composition = op;
03647350 1488
bf02a7f9
SC
1489 if (m_composition == wxCOMPOSITION_DEST)
1490 return true;
1491
1492 CompositingMode cop;
1493 switch (op)
1494 {
1495 case wxCOMPOSITION_SOURCE:
1496 cop = CompositingModeSourceCopy;
1497 break;
1498 case wxCOMPOSITION_OVER:
1499 cop = CompositingModeSourceOver;
1500 break;
1501 default:
1502 return false;
1503 }
1504
1505 m_context->SetCompositingMode(cop);
1506 return true;
1507}
1508
c496d8fa 1509void wxGDIPlusContext::BeginLayer(wxDouble /* opacity */)
bf02a7f9
SC
1510{
1511 // TODO
1512}
1513
1514void wxGDIPlusContext::EndLayer()
1515{
1516 // TODO
03647350 1517}
bf02a7f9 1518
cb3a0d42 1519void wxGDIPlusContext::Rotate( wxDouble angle )
83576e68
SC
1520{
1521 m_context->RotateTransform( RadToDeg(angle) );
1522}
6fea499c 1523
cb3a0d42 1524void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
83576e68
SC
1525{
1526 m_context->TranslateTransform( dx , dy );
1527}
6fea499c 1528
83576e68
SC
1529void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
1530{
1531 m_context->ScaleTransform(xScale,yScale);
1532}
6fea499c 1533
83576e68
SC
1534void wxGDIPlusContext::PushState()
1535{
1536 GraphicsState state = m_context->Save();
bdba6fdc 1537 m_stateStack.push(state);
83576e68
SC
1538}
1539
cb3a0d42 1540void wxGDIPlusContext::PopState()
83576e68 1541{
bdba6fdc
VZ
1542 GraphicsState state = m_stateStack.top();
1543 m_stateStack.pop();
83576e68 1544 m_context->Restore(state);
6fea499c
SC
1545}
1546
bce28872 1547void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
6fea499c 1548{
bf02a7f9
SC
1549 if (m_composition == wxCOMPOSITION_DEST)
1550 return;
1551
bce28872
SC
1552 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bmp.GetRefData())->GetGDIPlusBitmap();
1553 if ( image )
cb3a0d42 1554 {
bce28872 1555 if( image->GetWidth() != (UINT) w || image->GetHeight() != (UINT) h )
fe31db81 1556 {
bce28872
SC
1557 Rect drawRect((REAL) x, (REAL)y, (REAL)w, (REAL)h);
1558 m_context->SetPixelOffsetMode( PixelOffsetModeNone );
42604e44 1559 m_context->DrawImage(image, drawRect, 0 , 0 , image->GetWidth(), image->GetHeight(), UnitPixel ) ;
bce28872 1560 m_context->SetPixelOffsetMode( PixelOffsetModeHalf );
fe31db81 1561 }
bce28872
SC
1562 else
1563 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
fe31db81 1564 }
bce28872 1565}
fe31db81 1566
bce28872
SC
1567void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1568{
1569 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1570 DrawBitmap(bitmap, x, y, w, h);
6fea499c
SC
1571}
1572
cb3a0d42 1573void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
6fea499c 1574{
bf02a7f9
SC
1575 if (m_composition == wxCOMPOSITION_DEST)
1576 return;
1577
0c7bd159
SC
1578 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1579 // find out by looking at the bitmap data whether there really was alpha in it
fe31db81
SC
1580 HICON hIcon = (HICON)icon.GetHICON();
1581 ICONINFO iconInfo ;
1582 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1583 if (!GetIconInfo(hIcon,&iconInfo))
1584 return;
1585
fe31db81
SC
1586 Bitmap interim(iconInfo.hbmColor,NULL);
1587
1588 Bitmap* image = NULL ;
1589
0c7bd159
SC
1590 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1591 // work correctly, asking IsAlphaPixelFormat at this point fails as well
fe31db81
SC
1592 if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 )
1593 {
1594 image = Bitmap::FromHICON(hIcon);
1595 }
1596 else
1597 {
1598 size_t width = interim.GetWidth();
1599 size_t height = interim.GetHeight();
1600 Rect bounds(0,0,width,height);
1601 BitmapData data ;
1602
1603 interim.LockBits(&bounds, ImageLockModeRead,
1604 interim.GetPixelFormat(),&data);
0b7dce54 1605
0c7bd159
SC
1606 bool hasAlpha = false;
1607 for ( size_t y = 0 ; y < height && !hasAlpha ; ++y)
1608 {
1609 for( size_t x = 0 ; x < width && !hasAlpha; ++x)
1610 {
1611 ARGB *dest = (ARGB*)((BYTE*)data.Scan0 + data.Stride*y + x*4);
1612 if ( ( *dest & Color::AlphaMask ) != 0 )
1613 hasAlpha = true;
1614 }
1615 }
1616
1617 if ( hasAlpha )
1618 {
cb3a0d42 1619 image = new Bitmap(data.Width, data.Height, data.Stride,
fe31db81 1620 PixelFormat32bppARGB , (BYTE*) data.Scan0);
0c7bd159
SC
1621 }
1622 else
1623 {
1624 image = Bitmap::FromHICON(hIcon);
1625 }
1626
fe31db81
SC
1627 interim.UnlockBits(&data);
1628 }
1629
6fea499c 1630 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
fe31db81 1631
6fea499c 1632 delete image ;
fe31db81
SC
1633 DeleteObject(iconInfo.hbmColor);
1634 DeleteObject(iconInfo.hbmMask);
6fea499c
SC
1635}
1636
eb40c2d8
JS
1637wxGraphicsFont wxGDIPlusContext::CreateFont( const wxFont &font,
1638 const wxColour &col ) const
1639{
1640 wxGDIPlusRenderer* renderer =
1641 static_cast<wxGDIPlusRenderer*>(GetRenderer());
1642 return renderer->CreateGDIPlusFont(this, font, col);
1643}
1644
0b7dce54
VZ
1645void wxGDIPlusContext::DoDrawFilledText(const wxString& str,
1646 wxDouble x, wxDouble y,
1647 const wxGraphicsBrush& brush)
6fea499c 1648{
bf02a7f9
SC
1649 if (m_composition == wxCOMPOSITION_DEST)
1650 return;
1651
0b7dce54
VZ
1652 wxCHECK_RET( !m_font.IsNull(),
1653 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1011fbeb
SC
1654
1655 if ( str.IsEmpty())
6fea499c
SC
1656 return ;
1657
091111d6 1658 wxGDIPlusFontData * const
0b7dce54 1659 fontData = (wxGDIPlusFontData *)m_font.GetRefData();
091111d6
VZ
1660 wxGDIPlusBrushData * const
1661 brushData = (wxGDIPlusBrushData *)brush.GetRefData();
0b7dce54
VZ
1662
1663 m_context->DrawString
1664 (
1665 str.wc_str(*wxConvUI), // string to draw, always Unicode
1666 -1, // length: string is NUL-terminated
1667 fontData->GetGDIPlusFont(),
1668 PointF(x, y),
1669 StringFormat::GenericTypographic(),
1670 brushData ? brushData->GetGDIPlusBrush()
1671 : fontData->GetGDIPlusBrush()
1672 );
6fea499c
SC
1673}
1674
1675void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1676 wxDouble *descent, wxDouble *externalLeading ) const
1677{
1011fbeb
SC
1678 wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1679
6fea499c
SC
1680 wxWCharBuffer s = str.wc_str( *wxConvUI );
1681 FontFamily ffamily ;
2c820406 1682 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
cb3a0d42 1683
83576e68 1684 f->GetFamily(&ffamily) ;
6fea499c 1685
eb40c2d8 1686 REAL factorY = m_fontScaleRatio;
6fea499c
SC
1687
1688 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
83576e68 1689 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c 1690 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
83576e68 1691 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c 1692 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
83576e68 1693 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
6fea499c
SC
1694
1695 if ( height )
595fda6f 1696 *height = rHeight * factorY;
6fea499c 1697 if ( descent )
595fda6f 1698 *descent = rDescent * factorY;
6fea499c 1699 if ( externalLeading )
595fda6f 1700 *externalLeading = (rHeight - rAscent - rDescent) * factorY;
6fea499c 1701 // measuring empty strings is not guaranteed, so do it by hand
cb3a0d42 1702 if ( str.IsEmpty())
6fea499c
SC
1703 {
1704 if ( width )
1705 *width = 0 ;
1706 }
1707 else
1708 {
6fea499c 1709 RectF layoutRect(0,0, 100000.0f, 100000.0f);
0c7bd159 1710 StringFormat strFormat( StringFormat::GenericTypographic() );
0b7dce54 1711 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
0c7bd159
SC
1712
1713 RectF bounds ;
1714 m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ;
6fea499c 1715 if ( width )
0c7bd159 1716 *width = bounds.Width;
465642da
JS
1717 if ( height )
1718 *height = bounds.Height;
6fea499c
SC
1719 }
1720}
1721
cb3a0d42 1722void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
6fea499c
SC
1723{
1724 widths.Empty();
1725 widths.Add(0, text.length());
1726
1011fbeb
SC
1727 wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1728
6fea499c
SC
1729 if (text.empty())
1730 return;
1731
2c820406 1732 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
6fea499c
SC
1733 wxWCharBuffer ws = text.wc_str( *wxConvUI );
1734 size_t len = wcslen( ws ) ;
1735 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1736
1737 RectF layoutRect(0,0, 100000.0f, 100000.0f);
0c7bd159 1738 StringFormat strFormat( StringFormat::GenericTypographic() );
6fea499c 1739
c315587c 1740 size_t startPosition = 0;
918df9d2 1741 size_t remainder = len;
c315587c
SC
1742 const size_t maxSpan = 32;
1743 CharacterRange* ranges = new CharacterRange[maxSpan] ;
1744 Region* regions = new Region[maxSpan];
6fea499c 1745
918df9d2 1746 while( remainder > 0 )
6fea499c 1747 {
918df9d2 1748 size_t span = wxMin( maxSpan, remainder );
c315587c
SC
1749
1750 for( size_t i = 0 ; i < span ; ++i)
1751 {
1752 ranges[i].First = 0 ;
1753 ranges[i].Length = startPosition+i+1 ;
1754 }
1755 strFormat.SetMeasurableCharacterRanges(span,ranges);
1756 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1757 m_context->MeasureCharacterRanges(ws, -1 , f,layoutRect, &strFormat,span,regions) ;
1758
1759 RectF bbox ;
1760 for ( size_t i = 0 ; i < span ; ++i)
1761 {
1762 regions[i].GetBounds(&bbox,m_context);
1763 widths[startPosition+i] = bbox.Width;
1764 }
918df9d2 1765 remainder -= span;
c315587c 1766 startPosition += span;
6fea499c 1767 }
c315587c
SC
1768
1769 delete[] ranges;
1770 delete[] regions;
6fea499c
SC
1771}
1772
d9485f89 1773bool wxGDIPlusContext::ShouldOffset() const
0b7dce54 1774{
639e9c7d
SC
1775 if ( !m_enableOffset )
1776 return false;
1777
d9485f89
RD
1778 int penwidth = 0 ;
1779 if ( !m_pen.IsNull() )
1780 {
1781 penwidth = (int)((wxGDIPlusPenData*)m_pen.GetRefData())->GetWidth();
1782 if ( penwidth == 0 )
1783 penwidth = 1;
1784 }
1785 return ( penwidth % 2 ) == 1;
1786}
1787
cb3a0d42 1788void* wxGDIPlusContext::GetNativeContext()
6fea499c 1789{
cb3a0d42 1790 return m_context;
6fea499c
SC
1791}
1792
83576e68 1793// concatenates this transform with the current transform of this context
a4e73390 1794void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix& matrix )
539e2795 1795{
a4e73390 1796 m_context->MultiplyTransform((Matrix*) matrix.GetNativeMatrix());
83576e68
SC
1797}
1798
1799// sets the transform of this context
a4e73390 1800void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix& matrix )
83576e68 1801{
a4e73390 1802 m_context->SetTransform((Matrix*) matrix.GetNativeMatrix());
83576e68
SC
1803}
1804
1805// gets the matrix of this context
a4e73390 1806wxGraphicsMatrix wxGDIPlusContext::GetTransform() const
83576e68 1807{
a4e73390
SC
1808 wxGraphicsMatrix matrix = CreateMatrix();
1809 m_context->GetTransform((Matrix*) matrix.GetNativeMatrix());
1810 return matrix;
83576e68 1811}
fbd5416f
SC
1812
1813void wxGDIPlusContext::GetSize( wxDouble* width, wxDouble *height )
1814{
81e0b3d9
SC
1815 *width = m_width;
1816 *height = m_height;
fbd5416f 1817}
eb40c2d8 1818
7fa3cf6a 1819//-----------------------------------------------------------------------------
eb40c2d8 1820// wxGDIPlusPrintingContext implementation
7fa3cf6a
JS
1821//-----------------------------------------------------------------------------
1822
eb40c2d8
JS
1823wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer,
1824 const wxDC& dc )
1825 : wxGDIPlusContext(renderer, dc)
7fa3cf6a 1826{
eb40c2d8 1827 Graphics* context = GetGraphics();
7fa3cf6a 1828
eb40c2d8 1829 //m_context->SetPageUnit(UnitDocument);
7fa3cf6a 1830
eb40c2d8
JS
1831 // Setup page scale, based on DPI ratio.
1832 // Antecedent should be 100dpi when the default page unit
1833 // (UnitDisplay) is used. Page unit UnitDocument would require 300dpi
1834 // instead. Note that calling SetPageScale() does not have effect on
1835 // non-printing DCs (that is, any other than wxPrinterDC or
1836 // wxEnhMetaFileDC).
1837 REAL dpiRatio = 100.0 / context->GetDpiY();
1838 context->SetPageScale(dpiRatio);
7fa3cf6a 1839
eb40c2d8
JS
1840 // We use this modifier when measuring fonts. It is needed because the
1841 // page scale is modified above.
1842 m_fontScaleRatio = context->GetDpiY() / 72.0;
1843}
83576e68
SC
1844
1845//-----------------------------------------------------------------------------
1846// wxGDIPlusRenderer implementation
1847//-----------------------------------------------------------------------------
1848
1849IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer,wxGraphicsRenderer)
1850
1851static wxGDIPlusRenderer gs_GDIPlusRenderer;
1852
1853wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1854{
1855 return &gs_GDIPlusRenderer;
1856}
1857
bce82a75 1858bool wxGDIPlusRenderer::EnsureIsLoaded()
83576e68 1859{
bce82a75
VS
1860 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1861 // if we already tried and failed (we don't want to spend lot of time
1862 // returning NULL from wxGraphicsContext::Create(), which may be called
1863 // relatively frequently):
1864 if ( m_loaded == -1 )
83576e68
SC
1865 {
1866 Load();
1867 }
bce82a75
VS
1868
1869 return m_loaded == 1;
83576e68
SC
1870}
1871
bce82a75
VS
1872// call EnsureIsLoaded() and return returnOnFail value if it fails
1873#define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1874 if ( !EnsureIsLoaded() ) \
1875 return (returnOnFail)
1876
1877
83576e68
SC
1878void wxGDIPlusRenderer::Load()
1879{
1880 GdiplusStartupInput input;
1881 GdiplusStartupOutput output;
bce82a75
VS
1882 if ( GdiplusStartup(&m_gditoken,&input,&output) == Gdiplus::Ok )
1883 {
1884 wxLogTrace("gdiplus", "successfully initialized GDI+");
1885 m_loaded = 1;
1886 }
1887 else
1888 {
1889 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1890 m_loaded = 0;
1891 }
83576e68
SC
1892}
1893
1894void wxGDIPlusRenderer::Unload()
1895{
1896 if ( m_gditoken )
385addaf 1897 {
83576e68 1898 GdiplusShutdown(m_gditoken);
385addaf
VZ
1899 m_gditoken = NULL;
1900 }
bce82a75 1901 m_loaded = -1; // next Load() will try again
83576e68
SC
1902}
1903
1904wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxWindowDC& dc)
1905{
bce82a75 1906 ENSURE_LOADED_OR_RETURN(NULL);
639e9c7d
SC
1907 wxGDIPlusContext* context = new wxGDIPlusContext(this, dc);
1908 context->EnableOffset(true);
1909 return context;
83576e68
SC
1910}
1911
c929ad91 1912#if wxUSE_PRINTING_ARCHITECTURE
0b822969 1913wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxPrinterDC& dc)
8371a353
JS
1914{
1915 ENSURE_LOADED_OR_RETURN(NULL);
eb40c2d8
JS
1916 wxGDIPlusContext* context = new wxGDIPlusPrintingContext(this, dc);
1917 return context;
8371a353 1918}
c929ad91 1919#endif
8371a353 1920
c929ad91 1921#if wxUSE_ENH_METAFILE
8371a353 1922wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC& dc)
0b822969 1923{
bce82a75 1924 ENSURE_LOADED_OR_RETURN(NULL);
eb40c2d8
JS
1925 wxGDIPlusContext* context = new wxGDIPlusPrintingContext(this, dc);
1926 return context;
0b822969 1927}
c929ad91 1928#endif
0b822969 1929
773ccc31
SC
1930wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxMemoryDC& dc)
1931{
bce82a75 1932 ENSURE_LOADED_OR_RETURN(NULL);
639e9c7d
SC
1933 wxGDIPlusContext* context = new wxGDIPlusContext(this, dc);
1934 context->EnableOffset(true);
1935 return context;
773ccc31
SC
1936}
1937
091ef146
SC
1938wxGraphicsContext * wxGDIPlusRenderer::CreateMeasuringContext()
1939{
bce82a75 1940 ENSURE_LOADED_OR_RETURN(NULL);
0c7bd159 1941 return new wxGDIPlusMeasuringContext(this);
091ef146
SC
1942}
1943
83576e68
SC
1944wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeContext( void * context )
1945{
bce82a75 1946 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
1947 return new wxGDIPlusContext(this,(Graphics*) context);
1948}
1949
1950
1951wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window )
1952{
bce82a75 1953 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
1954 return new wxGDIPlusContext(this,(HWND) window);
1955}
1956
1957wxGraphicsContext * wxGDIPlusRenderer::CreateContext( wxWindow* window )
1958{
bce82a75 1959 ENSURE_LOADED_OR_RETURN(NULL);
83576e68
SC
1960 return new wxGDIPlusContext(this, (HWND) window->GetHWND() );
1961}
1962
1963// Path
1964
a4e73390 1965wxGraphicsPath wxGDIPlusRenderer::CreatePath()
83576e68 1966{
bce82a75 1967 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath);
a4e73390
SC
1968 wxGraphicsPath m;
1969 m.SetRefData( new wxGDIPlusPathData(this));
1970 return m;
83576e68
SC
1971}
1972
1973
1974// Matrix
1975
cb3a0d42 1976wxGraphicsMatrix wxGDIPlusRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
83576e68
SC
1977 wxDouble tx, wxDouble ty)
1978
1979{
bce82a75 1980 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix);
a4e73390
SC
1981 wxGraphicsMatrix m;
1982 wxGDIPlusMatrixData* data = new wxGDIPlusMatrixData( this );
1983 data->Set( a,b,c,d,tx,ty ) ;
1984 m.SetRefData(data);
83576e68 1985 return m;
539e2795
SC
1986}
1987
cb3a0d42 1988wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen)
6fea499c 1989{
bce82a75 1990 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen);
a1b806b9 1991 if ( !pen.IsOk() || pen.GetStyle() == wxTRANSPARENT )
2c820406 1992 return wxNullGraphicsPen;
83576e68 1993 else
2c820406
SC
1994 {
1995 wxGraphicsPen p;
1996 p.SetRefData(new wxGDIPlusPenData( this, pen ));
1997 return p;
1998 }
6fea499c 1999}
7ba86d93 2000
cb3a0d42 2001wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush )
539e2795 2002{
bce82a75 2003 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
a1b806b9 2004 if ( !brush.IsOk() || brush.GetStyle() == wxTRANSPARENT )
2c820406 2005 return wxNullGraphicsBrush;
83576e68 2006 else
2c820406
SC
2007 {
2008 wxGraphicsBrush p;
2009 p.SetRefData(new wxGDIPlusBrushData( this, brush ));
2010 return p;
2011 }
539e2795
SC
2012}
2013
4ee4c7b9
VZ
2014wxGraphicsBrush
2015wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
2016 wxDouble x2, wxDouble y2,
2017 const wxGraphicsGradientStops& stops)
539e2795 2018{
bce82a75 2019 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2c820406
SC
2020 wxGraphicsBrush p;
2021 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
4ee4c7b9 2022 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
2c820406
SC
2023 p.SetRefData(d);
2024 return p;
2025 }
539e2795 2026
4ee4c7b9
VZ
2027wxGraphicsBrush
2028wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
2029 wxDouble xc, wxDouble yc,
2030 wxDouble radius,
2031 const wxGraphicsGradientStops& stops)
83576e68 2032{
bce82a75 2033 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
2c820406
SC
2034 wxGraphicsBrush p;
2035 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
4ee4c7b9 2036 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,stops);
2c820406
SC
2037 p.SetRefData(d);
2038 return p;
83576e68 2039}
539e2795 2040
eb40c2d8
JS
2041wxGraphicsFont
2042wxGDIPlusRenderer::CreateGDIPlusFont( const wxGDIPlusContext* gc,
2043 const wxFont &font,
2044 const wxColour &col )
83576e68 2045{
bce82a75 2046 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
a1b806b9 2047 if ( font.IsOk() )
cb3a0d42 2048 {
2c820406 2049 wxGraphicsFont p;
eb40c2d8 2050 p.SetRefData(new wxGDIPlusFontData( this, gc, font, col ));
2c820406
SC
2051 return p;
2052 }
83576e68 2053 else
2c820406 2054 return wxNullGraphicsFont;
83576e68 2055}
7ba86d93 2056
bce28872
SC
2057wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmap( const wxBitmap &bitmap )
2058{
bce82a75 2059 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
a1b806b9 2060 if ( bitmap.IsOk() )
bce28872
SC
2061 {
2062 wxGraphicsBitmap p;
2063 p.SetRefData(new wxGDIPlusBitmapData( this , bitmap ));
2064 return p;
2065 }
2066 else
2067 return wxNullGraphicsBitmap;
2068}
2069
2986eb86
SC
2070wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap )
2071{
2072 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
2073 if ( bitmap != NULL )
2074 {
2075 wxGraphicsBitmap p;
2076 p.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap*) bitmap ));
2077 return p;
2078 }
2079 else
2080 return wxNullGraphicsBitmap;
2081}
2082
bce28872
SC
2083wxGraphicsBitmap wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
2084{
bce82a75 2085 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
bce28872
SC
2086 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bitmap.GetRefData())->GetGDIPlusBitmap();
2087 if ( image )
2088 {
2089 wxGraphicsBitmap p;
2090 p.SetRefData(new wxGDIPlusBitmapData( this , image->Clone( (REAL) x , (REAL) y , (REAL) w , (REAL) h , PixelFormat32bppPARGB) ));
2091 return p;
2092 }
2093 else
2094 return wxNullGraphicsBitmap;
2095}
2096
385addaf
VZ
2097// Shutdown GDI+ at app exit, before possible dll unload
2098class wxGDIPlusRendererModule : public wxModule
2099{
2100public:
2101 virtual bool OnInit() { return true; }
2102 virtual void OnExit() { gs_GDIPlusRenderer.Unload(); }
2103
2104private:
2105 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule)
2106};
2107
2108IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule)
2109
942d5e2d
VZ
2110// ----------------------------------------------------------------------------
2111// wxMSW-specific parts of wxGCDC
2112// ----------------------------------------------------------------------------
2113
2114WXHDC wxGCDC::AcquireHDC()
2115{
2116 wxGraphicsContext * const gc = GetGraphicsContext();
2117 if ( !gc )
2118 return NULL;
2119
2120 Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
2121 return g ? g->GetHDC() : NULL;
2122}
2123
2124void wxGCDC::ReleaseHDC(WXHDC hdc)
2125{
2126 if ( !hdc )
2127 return;
2128
2129 wxGraphicsContext * const gc = GetGraphicsContext();
2130 wxCHECK_RET( gc, "can't release HDC because there is no wxGraphicsContext" );
2131
2132 Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
2133 wxCHECK_RET( g, "can't release HDC because there is no Graphics" );
2134
2135 g->ReleaseHDC((HDC)hdc);
2136}
2137
7ba86d93 2138#endif // wxUSE_GRAPHICS_CONTEXT