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