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