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