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