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