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