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