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