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