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