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