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