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