]> git.saurik.com Git - wxWidgets.git/blob - src/msw/graphics.cpp
Correct measurement for owner drawn menu items with only unchecked bitmap.
[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/private/graphics.h"
41 #include "wx/msw/wrapgdip.h"
42 #include "wx/msw/dc.h"
43 #if wxUSE_ENH_METAFILE
44 #include "wx/msw/enhmeta.h"
45 #endif
46 #include "wx/dcgraph.h"
47
48 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
49
50 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
51 #include <commdlg.h>
52 #endif
53
54 #include "wx/stack.h"
55
56 WX_DECLARE_STACK(GraphicsState, GraphicsStates);
57
58 namespace
59 {
60
61 //-----------------------------------------------------------------------------
62 // constants
63 //-----------------------------------------------------------------------------
64
65 const double RAD2DEG = 180.0 / M_PI;
66
67 //-----------------------------------------------------------------------------
68 // Local functions
69 //-----------------------------------------------------------------------------
70
71 inline double dmin(double a, double b) { return a < b ? a : b; }
72 inline double dmax(double a, double b) { return a > b ? a : b; }
73
74 inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
75 inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; }
76
77 // translate a wxColour to a Color
78 inline Color wxColourToColor(const wxColour& col)
79 {
80 return Color(col.Alpha(), col.Red(), col.Green(), col.Blue());
81 }
82
83 } // anonymous namespace
84
85 //-----------------------------------------------------------------------------
86 // device context implementation
87 //
88 // more and more of the dc functionality should be implemented by calling
89 // the appropricate wxGDIPlusContext, but we will have to do that step by step
90 // also coordinate conversions should be moved to native matrix ops
91 //-----------------------------------------------------------------------------
92
93 // we always stock two context states, one at entry, to be able to preserve the
94 // state we were called with, the other one after changing to HI Graphics orientation
95 // (this one is used for getting back clippings etc)
96
97 //-----------------------------------------------------------------------------
98 // wxGraphicsPath implementation
99 //-----------------------------------------------------------------------------
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 private :
285 Bitmap* m_bitmap;
286 Bitmap* m_helper;
287 };
288
289 class wxGDIPlusFontData : public wxGraphicsObjectRefData
290 {
291 public:
292 wxGDIPlusFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
293 ~wxGDIPlusFontData();
294
295 virtual Brush* GetGDIPlusBrush() { return m_textBrush; }
296 virtual Font* GetGDIPlusFont() { return m_font; }
297 private :
298 Brush* m_textBrush;
299 Font* m_font;
300 };
301
302 class wxGDIPlusContext : public wxGraphicsContext
303 {
304 public:
305 wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc , wxDouble width, wxDouble height );
306 wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd );
307 wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr);
308 wxGDIPlusContext();
309
310 virtual ~wxGDIPlusContext();
311
312 virtual void Clip( const wxRegion &region );
313 // clips drawings to the rect
314 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
315
316 // resets the clipping to original extent
317 virtual void ResetClip();
318
319 virtual void * GetNativeContext();
320
321 virtual void StrokePath( const wxGraphicsPath& p );
322 virtual void FillPath( const wxGraphicsPath& p , wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
323
324 // stroke lines connecting each of the points
325 virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
326
327 // draws a polygon
328 virtual void DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode fillStyle = wxODDEVEN_RULE );
329
330 virtual bool SetAntialiasMode(wxAntialiasMode antialias);
331
332 virtual bool SetCompositionMode(wxCompositionMode op);
333
334 virtual void BeginLayer(wxDouble opacity);
335
336 virtual void EndLayer();
337
338 virtual void Translate( wxDouble dx , wxDouble dy );
339 virtual void Scale( wxDouble xScale , wxDouble yScale );
340 virtual void Rotate( wxDouble angle );
341
342 // concatenates this transform with the current transform of this context
343 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
344
345 // sets the transform of this context
346 virtual void SetTransform( const wxGraphicsMatrix& matrix );
347
348 // gets the matrix of this context
349 virtual wxGraphicsMatrix GetTransform() const;
350
351 virtual void DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
352 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
353 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
354 virtual void PushState();
355 virtual void PopState();
356
357 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
358 wxDouble *descent, wxDouble *externalLeading ) const;
359 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
360 virtual bool ShouldOffset() const;
361 virtual void GetSize( wxDouble* width, wxDouble *height );
362
363 private:
364 void Init();
365 void SetDefaults();
366
367 virtual void DoDrawText(const wxString& str, wxDouble x, wxDouble y)
368 { DoDrawFilledText(str, x, y, wxNullGraphicsBrush); }
369 virtual void DoDrawFilledText(const wxString& str, wxDouble x, wxDouble y,
370 const wxGraphicsBrush& backgroundBrush);
371
372 Graphics* m_context;
373 GraphicsStates m_stateStack;
374 GraphicsState m_state1;
375 GraphicsState m_state2;
376
377 wxDouble m_width;
378 wxDouble m_height;
379
380 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext)
381 };
382
383 class wxGDIPlusMeasuringContext : public wxGDIPlusContext
384 {
385 public:
386 wxGDIPlusMeasuringContext( wxGraphicsRenderer* renderer ) : wxGDIPlusContext( renderer , m_hdc = GetDC(NULL), 1000, 1000 )
387 {
388 }
389 wxGDIPlusMeasuringContext()
390 {
391 }
392
393 virtual ~wxGDIPlusMeasuringContext()
394 {
395 ReleaseDC( NULL, m_hdc );
396 }
397
398 private:
399 HDC m_hdc ;
400 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext)
401 } ;
402
403 //-----------------------------------------------------------------------------
404 // wxGDIPlusPen implementation
405 //-----------------------------------------------------------------------------
406
407 wxGDIPlusPenData::~wxGDIPlusPenData()
408 {
409 delete m_pen;
410 delete m_penImage;
411 delete m_penBrush;
412 }
413
414 void wxGDIPlusPenData::Init()
415 {
416 m_pen = NULL ;
417 m_penImage = NULL;
418 m_penBrush = NULL;
419 }
420
421 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
422 : wxGraphicsObjectRefData(renderer)
423 {
424 Init();
425 m_width = pen.GetWidth();
426 if (m_width <= 0.0)
427 m_width = 0.1;
428
429 m_pen = new Pen(wxColourToColor(pen.GetColour()), m_width );
430
431 LineCap cap;
432 switch ( pen.GetCap() )
433 {
434 case wxCAP_ROUND :
435 cap = LineCapRound;
436 break;
437
438 case wxCAP_PROJECTING :
439 cap = LineCapSquare;
440 break;
441
442 case wxCAP_BUTT :
443 cap = LineCapFlat; // TODO verify
444 break;
445
446 default :
447 cap = LineCapFlat;
448 break;
449 }
450 m_pen->SetLineCap(cap,cap, DashCapFlat);
451
452 LineJoin join;
453 switch ( pen.GetJoin() )
454 {
455 case wxJOIN_BEVEL :
456 join = LineJoinBevel;
457 break;
458
459 case wxJOIN_MITER :
460 join = LineJoinMiter;
461 break;
462
463 case wxJOIN_ROUND :
464 join = LineJoinRound;
465 break;
466
467 default :
468 join = LineJoinMiter;
469 break;
470 }
471
472 m_pen->SetLineJoin(join);
473
474 m_pen->SetDashStyle(DashStyleSolid);
475
476 DashStyle dashStyle = DashStyleSolid;
477 switch ( pen.GetStyle() )
478 {
479 case wxSOLID :
480 break;
481
482 case wxDOT :
483 dashStyle = DashStyleDot;
484 break;
485
486 case wxLONG_DASH :
487 dashStyle = DashStyleDash; // TODO verify
488 break;
489
490 case wxSHORT_DASH :
491 dashStyle = DashStyleDash;
492 break;
493
494 case wxDOT_DASH :
495 dashStyle = DashStyleDashDot;
496 break;
497 case wxUSER_DASH :
498 {
499 dashStyle = DashStyleCustom;
500 wxDash *dashes;
501 int count = pen.GetDashes( &dashes );
502 if ((dashes != NULL) && (count > 0))
503 {
504 REAL *userLengths = new REAL[count];
505 for ( int i = 0; i < count; ++i )
506 {
507 userLengths[i] = dashes[i];
508 }
509 m_pen->SetDashPattern( userLengths, count);
510 delete[] userLengths;
511 }
512 }
513 break;
514 case wxSTIPPLE :
515 {
516 wxBitmap* bmp = pen.GetStipple();
517 if ( bmp && bmp->Ok() )
518 {
519 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
520 #if wxUSE_PALETTE
521 (HPALETTE)bmp->GetPalette()->GetHPALETTE()
522 #else
523 NULL
524 #endif
525 );
526 m_penBrush = new TextureBrush(m_penImage);
527 m_pen->SetBrush( m_penBrush );
528 }
529
530 }
531 break;
532 default :
533 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
534 {
535 HatchStyle style = HatchStyleHorizontal;
536 switch( pen.GetStyle() )
537 {
538 case wxBDIAGONAL_HATCH :
539 style = HatchStyleBackwardDiagonal;
540 break ;
541 case wxCROSSDIAG_HATCH :
542 style = HatchStyleDiagonalCross;
543 break ;
544 case wxFDIAGONAL_HATCH :
545 style = HatchStyleForwardDiagonal;
546 break ;
547 case wxCROSS_HATCH :
548 style = HatchStyleCross;
549 break ;
550 case wxHORIZONTAL_HATCH :
551 style = HatchStyleHorizontal;
552 break ;
553 case wxVERTICAL_HATCH :
554 style = HatchStyleVertical;
555 break ;
556
557 }
558 m_penBrush = new HatchBrush
559 (
560 style,
561 wxColourToColor(pen.GetColour()),
562 Color::Transparent
563 );
564 m_pen->SetBrush( m_penBrush );
565 }
566 break;
567 }
568 if ( dashStyle != DashStyleSolid )
569 m_pen->SetDashStyle(dashStyle);
570 }
571
572 //-----------------------------------------------------------------------------
573 // wxGDIPlusBrush implementation
574 //-----------------------------------------------------------------------------
575
576 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer )
577 : wxGraphicsObjectRefData(renderer)
578 {
579 Init();
580 }
581
582 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer , const wxBrush &brush )
583 : wxGraphicsObjectRefData(renderer)
584 {
585 Init();
586 if ( brush.GetStyle() == wxSOLID)
587 {
588 m_brush = new SolidBrush(wxColourToColor( brush.GetColour()));
589 }
590 else if ( brush.IsHatch() )
591 {
592 HatchStyle style = HatchStyleHorizontal;
593 switch( brush.GetStyle() )
594 {
595 case wxBDIAGONAL_HATCH :
596 style = HatchStyleBackwardDiagonal;
597 break ;
598 case wxCROSSDIAG_HATCH :
599 style = HatchStyleDiagonalCross;
600 break ;
601 case wxFDIAGONAL_HATCH :
602 style = HatchStyleForwardDiagonal;
603 break ;
604 case wxCROSS_HATCH :
605 style = HatchStyleCross;
606 break ;
607 case wxHORIZONTAL_HATCH :
608 style = HatchStyleHorizontal;
609 break ;
610 case wxVERTICAL_HATCH :
611 style = HatchStyleVertical;
612 break ;
613
614 }
615 m_brush = new HatchBrush
616 (
617 style,
618 wxColourToColor(brush.GetColour()),
619 Color::Transparent
620 );
621 }
622 else
623 {
624 wxBitmap* bmp = brush.GetStipple();
625 if ( bmp && bmp->Ok() )
626 {
627 wxDELETE( m_brushImage );
628 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
629 #if wxUSE_PALETTE
630 (HPALETTE)bmp->GetPalette()->GetHPALETTE()
631 #else
632 NULL
633 #endif
634 );
635 m_brush = new TextureBrush(m_brushImage);
636 }
637 }
638 }
639
640 wxGDIPlusBrushData::~wxGDIPlusBrushData()
641 {
642 delete m_brush;
643 delete m_brushImage;
644 delete m_brushPath;
645 };
646
647 void wxGDIPlusBrushData::Init()
648 {
649 m_brush = NULL;
650 m_brushImage= NULL;
651 m_brushPath= NULL;
652 }
653
654 template <typename T>
655 void
656 wxGDIPlusBrushData::SetGradientStops(T *brush,
657 const wxGraphicsGradientStops& stops)
658 {
659 const unsigned numStops = stops.GetCount();
660 if ( numStops <= 2 )
661 {
662 // initial and final colours are set during the brush creation, nothing
663 // more to do
664 return;
665 }
666
667 wxVector<Color> colors(numStops);
668 wxVector<REAL> positions(numStops);
669
670 for ( unsigned i = 0; i < numStops; i++ )
671 {
672 wxGraphicsGradientStop stop = stops.Item(i);
673
674 colors[i] = wxColourToColor(stop.GetColour());
675 positions[i] = stop.GetPosition();
676 }
677
678 brush->SetInterpolationColors(&colors[0], &positions[0], numStops);
679 }
680
681 void
682 wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
683 wxDouble x2, wxDouble y2,
684 const wxGraphicsGradientStops& stops)
685 {
686 LinearGradientBrush * const
687 brush = new LinearGradientBrush(PointF(x1, y1) , PointF(x2, y2),
688 wxColourToColor(stops.GetStartColour()),
689 wxColourToColor(stops.GetEndColour()));
690 m_brush = brush;
691
692 SetGradientStops(brush, stops);
693 }
694
695 void
696 wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
697 wxDouble xc, wxDouble yc,
698 wxDouble radius,
699 const wxGraphicsGradientStops& stops)
700 {
701 m_brushPath = new GraphicsPath();
702 m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius),
703 (REAL)(2*radius), (REAL)(2*radius));
704
705 PathGradientBrush * const brush = new PathGradientBrush(m_brushPath);
706 m_brush = brush;
707 brush->SetCenterPoint(PointF(xo, yo));
708 brush->SetCenterColor(wxColourToColor(stops.GetStartColour()));
709
710 const Color col(wxColourToColor(stops.GetEndColour()));
711 int count = 1;
712 brush->SetSurroundColors(&col, &count);
713
714 SetGradientStops(brush, stops);
715 }
716
717 //-----------------------------------------------------------------------------
718 // wxGDIPlusFont implementation
719 //-----------------------------------------------------------------------------
720
721 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer* renderer, const wxFont &font,
722 const wxColour& col ) : wxGraphicsObjectRefData( renderer )
723 {
724 m_textBrush = NULL;
725 m_font = NULL;
726
727 wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI );
728 int size = font.GetPointSize();
729 int style = FontStyleRegular;
730 if ( font.GetStyle() == wxFONTSTYLE_ITALIC )
731 style |= FontStyleItalic;
732 if ( font.GetUnderlined() )
733 style |= FontStyleUnderline;
734 if ( font.GetWeight() == wxFONTWEIGHT_BOLD )
735 style |= FontStyleBold;
736 m_font = new Font( s , size , style );
737 m_textBrush = new SolidBrush(wxColourToColor(col));
738 }
739
740 wxGDIPlusFontData::~wxGDIPlusFontData()
741 {
742 delete m_textBrush;
743 delete m_font;
744 }
745
746 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
747 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
748 // bytes as parameter, since there is no real copying of the data going in, only references are stored
749 // m_helper has to be kept alive as well
750
751 //-----------------------------------------------------------------------------
752 // wxGDIPlusBitmapData implementation
753 //-----------------------------------------------------------------------------
754
755 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap ) :
756 wxGraphicsObjectRefData( renderer ), m_bitmap( bitmap )
757 {
758 m_helper = NULL;
759 }
760
761 wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer,
762 const wxBitmap &bmp) : wxGraphicsObjectRefData( renderer )
763 {
764 m_bitmap = NULL;
765 m_helper = NULL;
766
767 Bitmap* image = NULL;
768 if ( bmp.GetMask() )
769 {
770 Bitmap interim((HBITMAP)bmp.GetHBITMAP(),
771 #if wxUSE_PALETTE
772 (HPALETTE)bmp.GetPalette()->GetHPALETTE()
773 #else
774 NULL
775 #endif
776 );
777
778 size_t width = interim.GetWidth();
779 size_t height = interim.GetHeight();
780 Rect bounds(0,0,width,height);
781
782 image = new Bitmap(width,height,PixelFormat32bppPARGB) ;
783
784 Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL);
785 wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed);
786
787 BitmapData dataMask ;
788 interimMask.LockBits(&bounds,ImageLockModeRead,
789 interimMask.GetPixelFormat(),&dataMask);
790
791
792 BitmapData imageData ;
793 image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData);
794
795 BYTE maskPattern = 0 ;
796 BYTE maskByte = 0;
797 size_t maskIndex ;
798
799 for ( size_t y = 0 ; y < height ; ++y)
800 {
801 maskIndex = 0 ;
802 for( size_t x = 0 ; x < width; ++x)
803 {
804 if ( x % 8 == 0)
805 {
806 maskPattern = 0x80;
807 maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex);
808 maskIndex++;
809 }
810 else
811 maskPattern = maskPattern >> 1;
812
813 ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4);
814 if ( (maskByte & maskPattern) == 0 )
815 *dest = 0x00000000;
816 else
817 {
818 Color c ;
819 interim.GetPixel(x,y,&c) ;
820 *dest = (c.GetValue() | Color::AlphaMask);
821 }
822 }
823 }
824
825 image->UnlockBits(&imageData);
826
827 interimMask.UnlockBits(&dataMask);
828 interim.UnlockBits(&dataMask);
829 }
830 else
831 {
832 image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),
833 #if wxUSE_PALETTE
834 (HPALETTE)bmp.GetPalette()->GetHPALETTE()
835 #else
836 NULL
837 #endif
838 );
839 if ( bmp.HasAlpha() && GetPixelFormatSize(image->GetPixelFormat()) == 32 )
840 {
841 size_t width = image->GetWidth();
842 size_t height = image->GetHeight();
843 Rect bounds(0,0,width,height);
844 static BitmapData data ;
845
846 m_helper = image ;
847 image = NULL ;
848 m_helper->LockBits(&bounds, ImageLockModeRead,
849 m_helper->GetPixelFormat(),&data);
850
851 image = new Bitmap(data.Width, data.Height, data.Stride,
852 PixelFormat32bppPARGB , (BYTE*) data.Scan0);
853
854 m_helper->UnlockBits(&data);
855 }
856 }
857 if ( image )
858 m_bitmap = image;
859 }
860
861 wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
862 {
863 delete m_bitmap;
864 delete m_helper;
865 }
866
867 //-----------------------------------------------------------------------------
868 // wxGDIPlusPath implementation
869 //-----------------------------------------------------------------------------
870
871 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path ) : wxGraphicsPathData(renderer)
872 {
873 if ( path )
874 m_path = path;
875 else
876 m_path = new GraphicsPath();
877 }
878
879 wxGDIPlusPathData::~wxGDIPlusPathData()
880 {
881 delete m_path;
882 }
883
884 wxGraphicsObjectRefData* wxGDIPlusPathData::Clone() const
885 {
886 return new wxGDIPlusPathData( GetRenderer() , m_path->Clone());
887 }
888
889 //
890 // The Primitives
891 //
892
893 void wxGDIPlusPathData::MoveToPoint( wxDouble x , wxDouble y )
894 {
895 m_path->StartFigure();
896 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
897 }
898
899 void wxGDIPlusPathData::AddLineToPoint( wxDouble x , wxDouble y )
900 {
901 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
902 }
903
904 void wxGDIPlusPathData::CloseSubpath()
905 {
906 m_path->CloseFigure();
907 }
908
909 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
910 {
911 PointF c1(cx1,cy1);
912 PointF c2(cx2,cy2);
913 PointF end(x,y);
914 PointF start;
915 m_path->GetLastPoint(&start);
916 m_path->AddBezier(start,c1,c2,end);
917 }
918
919 // gets the last point of the current path, (0,0) if not yet set
920 void wxGDIPlusPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
921 {
922 PointF start;
923 m_path->GetLastPoint(&start);
924 *x = start.X ;
925 *y = start.Y ;
926 }
927
928 void wxGDIPlusPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
929 {
930 double sweepAngle = endAngle - startAngle ;
931 if( fabs(sweepAngle) >= 2*M_PI)
932 {
933 sweepAngle = 2 * M_PI;
934 }
935 else
936 {
937 if ( clockwise )
938 {
939 if( sweepAngle < 0 )
940 sweepAngle += 2 * M_PI;
941 }
942 else
943 {
944 if( sweepAngle > 0 )
945 sweepAngle -= 2 * M_PI;
946
947 }
948 }
949 m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle));
950 }
951
952 void wxGDIPlusPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
953 {
954 m_path->AddRectangle(RectF(x,y,w,h));
955 }
956
957 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData* path )
958 {
959 m_path->AddPath( (GraphicsPath*) path->GetNativePath(), FALSE);
960 }
961
962
963 // transforms each point of this path by the matrix
964 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData* matrix )
965 {
966 m_path->Transform( (Matrix*) matrix->GetNativeMatrix() );
967 }
968
969 // gets the bounding box enclosing all points (possibly including control points)
970 void wxGDIPlusPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
971 {
972 RectF bounds;
973 m_path->GetBounds( &bounds, NULL, NULL) ;
974 *x = bounds.X;
975 *y = bounds.Y;
976 *w = bounds.Width;
977 *h = bounds.Height;
978 }
979
980 bool wxGDIPlusPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
981 {
982 m_path->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
983 return m_path->IsVisible( (FLOAT) x,(FLOAT) y) == TRUE ;
984 }
985
986 //-----------------------------------------------------------------------------
987 // wxGDIPlusMatrixData implementation
988 //-----------------------------------------------------------------------------
989
990 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix )
991 : wxGraphicsMatrixData(renderer)
992 {
993 if ( matrix )
994 m_matrix = matrix ;
995 else
996 m_matrix = new Matrix();
997 }
998
999 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
1000 {
1001 delete m_matrix;
1002 }
1003
1004 wxGraphicsObjectRefData *wxGDIPlusMatrixData::Clone() const
1005 {
1006 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix->Clone());
1007 }
1008
1009 // concatenates the matrix
1010 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData *t )
1011 {
1012 m_matrix->Multiply( (Matrix*) t->GetNativeMatrix());
1013 }
1014
1015 // sets the matrix to the respective values
1016 void wxGDIPlusMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1017 wxDouble tx, wxDouble ty)
1018 {
1019 m_matrix->SetElements(a,b,c,d,tx,ty);
1020 }
1021
1022 // gets the component valuess of the matrix
1023 void wxGDIPlusMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
1024 wxDouble* d, wxDouble* tx, wxDouble* ty) const
1025 {
1026 REAL elements[6];
1027 m_matrix->GetElements(elements);
1028 if (a) *a = elements[0];
1029 if (b) *b = elements[1];
1030 if (c) *c = elements[2];
1031 if (d) *d = elements[3];
1032 if (tx) *tx= elements[4];
1033 if (ty) *ty= elements[5];
1034 }
1035
1036 // makes this the inverse matrix
1037 void wxGDIPlusMatrixData::Invert()
1038 {
1039 m_matrix->Invert();
1040 }
1041
1042 // returns true if the elements of the transformation matrix are equal ?
1043 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
1044 {
1045 return m_matrix->Equals((Matrix*) t->GetNativeMatrix())== TRUE ;
1046 }
1047
1048 // return true if this is the identity matrix
1049 bool wxGDIPlusMatrixData::IsIdentity() const
1050 {
1051 return m_matrix->IsIdentity() == TRUE ;
1052 }
1053
1054 //
1055 // transformation
1056 //
1057
1058 // add the translation to this matrix
1059 void wxGDIPlusMatrixData::Translate( wxDouble dx , wxDouble dy )
1060 {
1061 m_matrix->Translate(dx,dy);
1062 }
1063
1064 // add the scale to this matrix
1065 void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale )
1066 {
1067 m_matrix->Scale(xScale,yScale);
1068 }
1069
1070 // add the rotation to this matrix (radians)
1071 void wxGDIPlusMatrixData::Rotate( wxDouble angle )
1072 {
1073 m_matrix->Rotate( RadToDeg(angle) );
1074 }
1075
1076 //
1077 // apply the transforms
1078 //
1079
1080 // applies that matrix to the point
1081 void wxGDIPlusMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
1082 {
1083 PointF pt(*x,*y);
1084 m_matrix->TransformPoints(&pt);
1085 *x = pt.X;
1086 *y = pt.Y;
1087 }
1088
1089 // applies the matrix except for translations
1090 void wxGDIPlusMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
1091 {
1092 PointF pt(*dx,*dy);
1093 m_matrix->TransformVectors(&pt);
1094 *dx = pt.X;
1095 *dy = pt.Y;
1096 }
1097
1098 // returns the native representation
1099 void * wxGDIPlusMatrixData::GetNativeMatrix() const
1100 {
1101 return m_matrix;
1102 }
1103
1104 //-----------------------------------------------------------------------------
1105 // wxGDIPlusContext implementation
1106 //-----------------------------------------------------------------------------
1107
1108 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext,wxGraphicsContext)
1109 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext,wxGDIPlusContext)
1110
1111 class wxGDIPlusOffsetHelper
1112 {
1113 public :
1114 wxGDIPlusOffsetHelper( Graphics* gr , bool offset )
1115 {
1116 m_gr = gr;
1117 m_offset = offset;
1118 if ( m_offset )
1119 m_gr->TranslateTransform( 0.5, 0.5 );
1120 }
1121 ~wxGDIPlusOffsetHelper( )
1122 {
1123 if ( m_offset )
1124 m_gr->TranslateTransform( -0.5, -0.5 );
1125 }
1126 public :
1127 Graphics* m_gr;
1128 bool m_offset;
1129 } ;
1130
1131 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc, wxDouble width, wxDouble height )
1132 : wxGraphicsContext(renderer)
1133 {
1134 Init();
1135 m_context = new Graphics( hdc);
1136 m_width = width;
1137 m_height = height;
1138 SetDefaults();
1139 }
1140
1141 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd )
1142 : wxGraphicsContext(renderer)
1143 {
1144 Init();
1145 m_context = new Graphics( hwnd);
1146 RECT rect = wxGetWindowRect(hwnd);
1147 m_width = rect.right - rect.left;
1148 m_height = rect.bottom - rect.top;
1149 SetDefaults();
1150 }
1151
1152 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr )
1153 : wxGraphicsContext(renderer)
1154 {
1155 Init();
1156 m_context = gr;
1157 SetDefaults();
1158 }
1159
1160 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL)
1161 {
1162 Init();
1163 }
1164
1165 void wxGDIPlusContext::Init()
1166 {
1167 m_context = NULL;
1168 m_state1 = 0;
1169 m_state2= 0;
1170 m_height = 0;
1171 m_width = 0;
1172 }
1173
1174 void wxGDIPlusContext::SetDefaults()
1175 {
1176 m_context->SetTextRenderingHint(TextRenderingHintSystemDefault);
1177 m_context->SetPixelOffsetMode(PixelOffsetModeHalf);
1178 m_context->SetSmoothingMode(SmoothingModeHighQuality);
1179 m_state1 = m_context->Save();
1180 m_state2 = m_context->Save();
1181
1182 // Setup page scale, based on DPI ratio.
1183 // Antecedent should be 100dpi when the default page unit (UnitDisplay)
1184 // is used. Page unit UnitDocument would require 300dpi instead.
1185 // Note that calling SetPageScale() does not have effect on non-printing
1186 // DCs (that is, any other than wxPrinterDC or wxEnhMetaFileDC).
1187 REAL dpiRatio = 100.0 / m_context->GetDpiY();
1188 m_context->SetPageScale(dpiRatio);
1189 }
1190
1191 wxGDIPlusContext::~wxGDIPlusContext()
1192 {
1193 if ( m_context )
1194 {
1195 m_context->Restore( m_state2 );
1196 m_context->Restore( m_state1 );
1197 delete m_context;
1198 }
1199 }
1200
1201
1202 void wxGDIPlusContext::Clip( const wxRegion &region )
1203 {
1204 Region rgn((HRGN)region.GetHRGN());
1205 m_context->SetClip(&rgn,CombineModeIntersect);
1206 }
1207
1208 void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1209 {
1210 m_context->SetClip(RectF(x,y,w,h),CombineModeIntersect);
1211 }
1212
1213 void wxGDIPlusContext::ResetClip()
1214 {
1215 m_context->ResetClip();
1216 }
1217
1218 void wxGDIPlusContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
1219 {
1220 if (m_composition == wxCOMPOSITION_DEST)
1221 return;
1222
1223 if ( !m_pen.IsNull() )
1224 {
1225 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1226 Point *cpoints = new Point[n];
1227 for (size_t i = 0; i < n; i++)
1228 {
1229 cpoints[i].X = (int)(points[i].m_x );
1230 cpoints[i].Y = (int)(points[i].m_y );
1231
1232 } // for (size_t i = 0; i < n; i++)
1233 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1234 delete[] cpoints;
1235 }
1236 }
1237
1238 void wxGDIPlusContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode WXUNUSED(fillStyle) )
1239 {
1240 if (m_composition == wxCOMPOSITION_DEST)
1241 return;
1242
1243 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1244 Point *cpoints = new Point[n];
1245 for (size_t i = 0; i < n; i++)
1246 {
1247 cpoints[i].X = (int)(points[i].m_x );
1248 cpoints[i].Y = (int)(points[i].m_y );
1249
1250 } // for (int i = 0; i < n; i++)
1251 if ( !m_brush.IsNull() )
1252 m_context->FillPolygon( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , cpoints , n ) ;
1253 if ( !m_pen.IsNull() )
1254 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1255 delete[] cpoints;
1256 }
1257
1258 void wxGDIPlusContext::StrokePath( const wxGraphicsPath& path )
1259 {
1260 if (m_composition == wxCOMPOSITION_DEST)
1261 return;
1262
1263 if ( !m_pen.IsNull() )
1264 {
1265 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1266 m_context->DrawPath( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath*) path.GetNativePath() );
1267 }
1268 }
1269
1270 void wxGDIPlusContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
1271 {
1272 if (m_composition == wxCOMPOSITION_DEST)
1273 return;
1274
1275 if ( !m_brush.IsNull() )
1276 {
1277 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1278 ((GraphicsPath*) path.GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
1279 m_context->FillPath( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() ,
1280 (GraphicsPath*) path.GetNativePath());
1281 }
1282 }
1283
1284 bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias)
1285 {
1286 if (m_antialias == antialias)
1287 return true;
1288
1289 m_antialias = antialias;
1290
1291 SmoothingMode antialiasMode;
1292 switch (antialias)
1293 {
1294 case wxANTIALIAS_DEFAULT:
1295 antialiasMode = SmoothingModeHighQuality;
1296 break;
1297 case wxANTIALIAS_NONE:
1298 antialiasMode = SmoothingModeNone;
1299 break;
1300 default:
1301 return false;
1302 }
1303 m_context->SetSmoothingMode(antialiasMode);
1304 return true;
1305 }
1306
1307 bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op)
1308 {
1309 if ( m_composition == op )
1310 return true;
1311
1312 m_composition = op;
1313
1314 if (m_composition == wxCOMPOSITION_DEST)
1315 return true;
1316
1317 CompositingMode cop;
1318 switch (op)
1319 {
1320 case wxCOMPOSITION_SOURCE:
1321 cop = CompositingModeSourceCopy;
1322 break;
1323 case wxCOMPOSITION_OVER:
1324 cop = CompositingModeSourceOver;
1325 break;
1326 default:
1327 return false;
1328 }
1329
1330 m_context->SetCompositingMode(cop);
1331 return true;
1332 }
1333
1334 void wxGDIPlusContext::BeginLayer(wxDouble /* opacity */)
1335 {
1336 // TODO
1337 }
1338
1339 void wxGDIPlusContext::EndLayer()
1340 {
1341 // TODO
1342 }
1343
1344 void wxGDIPlusContext::Rotate( wxDouble angle )
1345 {
1346 m_context->RotateTransform( RadToDeg(angle) );
1347 }
1348
1349 void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
1350 {
1351 m_context->TranslateTransform( dx , dy );
1352 }
1353
1354 void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
1355 {
1356 m_context->ScaleTransform(xScale,yScale);
1357 }
1358
1359 void wxGDIPlusContext::PushState()
1360 {
1361 GraphicsState state = m_context->Save();
1362 m_stateStack.push(state);
1363 }
1364
1365 void wxGDIPlusContext::PopState()
1366 {
1367 GraphicsState state = m_stateStack.top();
1368 m_stateStack.pop();
1369 m_context->Restore(state);
1370 }
1371
1372 void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1373 {
1374 if (m_composition == wxCOMPOSITION_DEST)
1375 return;
1376
1377 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bmp.GetRefData())->GetGDIPlusBitmap();
1378 if ( image )
1379 {
1380 if( image->GetWidth() != (UINT) w || image->GetHeight() != (UINT) h )
1381 {
1382 Rect drawRect((REAL) x, (REAL)y, (REAL)w, (REAL)h);
1383 m_context->SetPixelOffsetMode( PixelOffsetModeNone );
1384 m_context->DrawImage(image, drawRect, 0 , 0 , image->GetWidth()-1, image->GetHeight()-1, UnitPixel ) ;
1385 m_context->SetPixelOffsetMode( PixelOffsetModeHalf );
1386 }
1387 else
1388 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1389 }
1390 }
1391
1392 void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1393 {
1394 wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
1395 DrawBitmap(bitmap, x, y, w, h);
1396 }
1397
1398 void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1399 {
1400 if (m_composition == wxCOMPOSITION_DEST)
1401 return;
1402
1403 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1404 // find out by looking at the bitmap data whether there really was alpha in it
1405 HICON hIcon = (HICON)icon.GetHICON();
1406 ICONINFO iconInfo ;
1407 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1408 if (!GetIconInfo(hIcon,&iconInfo))
1409 return;
1410
1411 Bitmap interim(iconInfo.hbmColor,NULL);
1412
1413 Bitmap* image = NULL ;
1414
1415 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1416 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1417 if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 )
1418 {
1419 image = Bitmap::FromHICON(hIcon);
1420 }
1421 else
1422 {
1423 size_t width = interim.GetWidth();
1424 size_t height = interim.GetHeight();
1425 Rect bounds(0,0,width,height);
1426 BitmapData data ;
1427
1428 interim.LockBits(&bounds, ImageLockModeRead,
1429 interim.GetPixelFormat(),&data);
1430
1431 bool hasAlpha = false;
1432 for ( size_t y = 0 ; y < height && !hasAlpha ; ++y)
1433 {
1434 for( size_t x = 0 ; x < width && !hasAlpha; ++x)
1435 {
1436 ARGB *dest = (ARGB*)((BYTE*)data.Scan0 + data.Stride*y + x*4);
1437 if ( ( *dest & Color::AlphaMask ) != 0 )
1438 hasAlpha = true;
1439 }
1440 }
1441
1442 if ( hasAlpha )
1443 {
1444 image = new Bitmap(data.Width, data.Height, data.Stride,
1445 PixelFormat32bppARGB , (BYTE*) data.Scan0);
1446 }
1447 else
1448 {
1449 image = Bitmap::FromHICON(hIcon);
1450 }
1451
1452 interim.UnlockBits(&data);
1453 }
1454
1455 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1456
1457 delete image ;
1458 DeleteObject(iconInfo.hbmColor);
1459 DeleteObject(iconInfo.hbmMask);
1460 }
1461
1462 void wxGDIPlusContext::DoDrawFilledText(const wxString& str,
1463 wxDouble x, wxDouble y,
1464 const wxGraphicsBrush& brush)
1465 {
1466 if (m_composition == wxCOMPOSITION_DEST)
1467 return;
1468
1469 wxCHECK_RET( !m_font.IsNull(),
1470 wxT("wxGDIPlusContext::DrawText - no valid font set") );
1471
1472 if ( str.IsEmpty())
1473 return ;
1474
1475 wxGDIPlusFontData * const
1476 fontData = (wxGDIPlusFontData *)m_font.GetRefData();
1477 wxGDIPlusBrushData * const
1478 brushData = (wxGDIPlusBrushData *)brush.GetRefData();
1479
1480 m_context->DrawString
1481 (
1482 str.wc_str(*wxConvUI), // string to draw, always Unicode
1483 -1, // length: string is NUL-terminated
1484 fontData->GetGDIPlusFont(),
1485 PointF(x, y),
1486 StringFormat::GenericTypographic(),
1487 brushData ? brushData->GetGDIPlusBrush()
1488 : fontData->GetGDIPlusBrush()
1489 );
1490 }
1491
1492 void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1493 wxDouble *descent, wxDouble *externalLeading ) const
1494 {
1495 wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetTextExtent - no valid font set") );
1496
1497 wxWCharBuffer s = str.wc_str( *wxConvUI );
1498 FontFamily ffamily ;
1499 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
1500
1501 f->GetFamily(&ffamily) ;
1502
1503 REAL factorY = m_context->GetDpiY() / 72.0 ;
1504
1505 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
1506 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1507 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
1508 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1509 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
1510 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1511
1512 if ( height )
1513 *height = rHeight * factorY;
1514 if ( descent )
1515 *descent = rDescent * factorY;
1516 if ( externalLeading )
1517 *externalLeading = (rHeight - rAscent - rDescent) * factorY;
1518 // measuring empty strings is not guaranteed, so do it by hand
1519 if ( str.IsEmpty())
1520 {
1521 if ( width )
1522 *width = 0 ;
1523 }
1524 else
1525 {
1526 RectF layoutRect(0,0, 100000.0f, 100000.0f);
1527 StringFormat strFormat( StringFormat::GenericTypographic() );
1528 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1529
1530 RectF bounds ;
1531 m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ;
1532 if ( width )
1533 *width = bounds.Width;
1534 if ( height )
1535 *height = bounds.Height;
1536 }
1537 }
1538
1539 void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1540 {
1541 widths.Empty();
1542 widths.Add(0, text.length());
1543
1544 wxCHECK_RET( !m_font.IsNull(), wxT("wxGDIPlusContext::GetPartialTextExtents - no valid font set") );
1545
1546 if (text.empty())
1547 return;
1548
1549 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
1550 wxWCharBuffer ws = text.wc_str( *wxConvUI );
1551 size_t len = wcslen( ws ) ;
1552 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1553
1554 RectF layoutRect(0,0, 100000.0f, 100000.0f);
1555 StringFormat strFormat( StringFormat::GenericTypographic() );
1556
1557 size_t startPosition = 0;
1558 size_t remainder = len;
1559 const size_t maxSpan = 32;
1560 CharacterRange* ranges = new CharacterRange[maxSpan] ;
1561 Region* regions = new Region[maxSpan];
1562
1563 while( remainder > 0 )
1564 {
1565 size_t span = wxMin( maxSpan, remainder );
1566
1567 for( size_t i = 0 ; i < span ; ++i)
1568 {
1569 ranges[i].First = 0 ;
1570 ranges[i].Length = startPosition+i+1 ;
1571 }
1572 strFormat.SetMeasurableCharacterRanges(span,ranges);
1573 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1574 m_context->MeasureCharacterRanges(ws, -1 , f,layoutRect, &strFormat,span,regions) ;
1575
1576 RectF bbox ;
1577 for ( size_t i = 0 ; i < span ; ++i)
1578 {
1579 regions[i].GetBounds(&bbox,m_context);
1580 widths[startPosition+i] = bbox.Width;
1581 }
1582 remainder -= span;
1583 startPosition += span;
1584 }
1585
1586 delete[] ranges;
1587 delete[] regions;
1588 }
1589
1590 bool wxGDIPlusContext::ShouldOffset() const
1591 {
1592 int penwidth = 0 ;
1593 if ( !m_pen.IsNull() )
1594 {
1595 penwidth = (int)((wxGDIPlusPenData*)m_pen.GetRefData())->GetWidth();
1596 if ( penwidth == 0 )
1597 penwidth = 1;
1598 }
1599 return ( penwidth % 2 ) == 1;
1600 }
1601
1602 void* wxGDIPlusContext::GetNativeContext()
1603 {
1604 return m_context;
1605 }
1606
1607 // concatenates this transform with the current transform of this context
1608 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1609 {
1610 m_context->MultiplyTransform((Matrix*) matrix.GetNativeMatrix());
1611 }
1612
1613 // sets the transform of this context
1614 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix& matrix )
1615 {
1616 m_context->SetTransform((Matrix*) matrix.GetNativeMatrix());
1617 }
1618
1619 // gets the matrix of this context
1620 wxGraphicsMatrix wxGDIPlusContext::GetTransform() const
1621 {
1622 wxGraphicsMatrix matrix = CreateMatrix();
1623 m_context->GetTransform((Matrix*) matrix.GetNativeMatrix());
1624 return matrix;
1625 }
1626
1627 void wxGDIPlusContext::GetSize( wxDouble* width, wxDouble *height )
1628 {
1629 *width = m_width;
1630 *height = m_height;
1631 }
1632 //-----------------------------------------------------------------------------
1633 // wxGDIPlusRenderer declaration
1634 //-----------------------------------------------------------------------------
1635
1636 class wxGDIPlusRenderer : public wxGraphicsRenderer
1637 {
1638 public :
1639 wxGDIPlusRenderer()
1640 {
1641 m_loaded = -1;
1642 m_gditoken = 0;
1643 }
1644
1645 virtual ~wxGDIPlusRenderer()
1646 {
1647 if ( m_loaded == 1 )
1648 {
1649 Unload();
1650 }
1651 }
1652
1653 // Context
1654
1655 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1656
1657 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1658
1659 #if wxUSE_PRINTING_ARCHITECTURE
1660 virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
1661 #endif
1662
1663 #if wxUSE_ENH_METAFILE
1664 virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc);
1665 #endif
1666
1667 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1668
1669 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1670
1671 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1672
1673 virtual wxGraphicsContext * CreateMeasuringContext();
1674
1675 // Path
1676
1677 virtual wxGraphicsPath CreatePath();
1678
1679 // Matrix
1680
1681 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1682 wxDouble tx=0.0, wxDouble ty=0.0);
1683
1684
1685 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1686
1687 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1688
1689 virtual wxGraphicsBrush
1690 CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
1691 wxDouble x2, wxDouble y2,
1692 const wxGraphicsGradientStops& stops);
1693
1694 virtual wxGraphicsBrush
1695 CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
1696 wxDouble xc, wxDouble yc,
1697 wxDouble radius,
1698 const wxGraphicsGradientStops& stops);
1699 // sets the font
1700 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1701
1702 // create a native bitmap representation
1703 virtual wxGraphicsBitmap CreateBitmap( const wxBitmap &bitmap );
1704
1705 // create a graphics bitmap from a native bitmap
1706 virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
1707
1708 // create a subimage from a native image representation
1709 virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
1710
1711 protected :
1712 bool EnsureIsLoaded();
1713 void Load();
1714 void Unload();
1715 friend class wxGDIPlusRendererModule;
1716
1717 private :
1718 int m_loaded;
1719 ULONG_PTR m_gditoken;
1720
1721 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer)
1722 } ;
1723
1724 //-----------------------------------------------------------------------------
1725 // wxGDIPlusRenderer implementation
1726 //-----------------------------------------------------------------------------
1727
1728 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer,wxGraphicsRenderer)
1729
1730 static wxGDIPlusRenderer gs_GDIPlusRenderer;
1731
1732 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1733 {
1734 return &gs_GDIPlusRenderer;
1735 }
1736
1737 bool wxGDIPlusRenderer::EnsureIsLoaded()
1738 {
1739 // load gdiplus.dll if not yet loaded, but don't bother doing it again
1740 // if we already tried and failed (we don't want to spend lot of time
1741 // returning NULL from wxGraphicsContext::Create(), which may be called
1742 // relatively frequently):
1743 if ( m_loaded == -1 )
1744 {
1745 Load();
1746 }
1747
1748 return m_loaded == 1;
1749 }
1750
1751 // call EnsureIsLoaded() and return returnOnFail value if it fails
1752 #define ENSURE_LOADED_OR_RETURN(returnOnFail) \
1753 if ( !EnsureIsLoaded() ) \
1754 return (returnOnFail)
1755
1756
1757 void wxGDIPlusRenderer::Load()
1758 {
1759 GdiplusStartupInput input;
1760 GdiplusStartupOutput output;
1761 if ( GdiplusStartup(&m_gditoken,&input,&output) == Gdiplus::Ok )
1762 {
1763 wxLogTrace("gdiplus", "successfully initialized GDI+");
1764 m_loaded = 1;
1765 }
1766 else
1767 {
1768 wxLogTrace("gdiplus", "failed to initialize GDI+, missing gdiplus.dll?");
1769 m_loaded = 0;
1770 }
1771 }
1772
1773 void wxGDIPlusRenderer::Unload()
1774 {
1775 if ( m_gditoken )
1776 {
1777 GdiplusShutdown(m_gditoken);
1778 m_gditoken = NULL;
1779 }
1780 m_loaded = -1; // next Load() will try again
1781 }
1782
1783 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxWindowDC& dc)
1784 {
1785 ENSURE_LOADED_OR_RETURN(NULL);
1786 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1787 wxSize sz = dc.GetSize();
1788 return new wxGDIPlusContext(this,(HDC) msw->GetHDC(), sz.x, sz.y);
1789 }
1790
1791 #if wxUSE_PRINTING_ARCHITECTURE
1792 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxPrinterDC& dc)
1793 {
1794 ENSURE_LOADED_OR_RETURN(NULL);
1795 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1796 wxSize sz = dc.GetSize();
1797 return new wxGDIPlusContext(this,(HDC) msw->GetHDC(), sz.x, sz.y);
1798 }
1799 #endif
1800
1801 #if wxUSE_ENH_METAFILE
1802 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC& dc)
1803 {
1804 ENSURE_LOADED_OR_RETURN(NULL);
1805 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1806 wxSize sz = dc.GetSize();
1807 return new wxGDIPlusContext(this,(HDC) msw->GetHDC(), sz.x, sz.y);
1808 }
1809 #endif
1810
1811 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxMemoryDC& dc)
1812 {
1813 ENSURE_LOADED_OR_RETURN(NULL);
1814 wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
1815 wxSize sz = dc.GetSize();
1816 return new wxGDIPlusContext(this,(HDC) msw->GetHDC(), sz.x, sz.y);
1817 }
1818
1819 wxGraphicsContext * wxGDIPlusRenderer::CreateMeasuringContext()
1820 {
1821 ENSURE_LOADED_OR_RETURN(NULL);
1822 return new wxGDIPlusMeasuringContext(this);
1823 }
1824
1825 wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeContext( void * context )
1826 {
1827 ENSURE_LOADED_OR_RETURN(NULL);
1828 return new wxGDIPlusContext(this,(Graphics*) context);
1829 }
1830
1831
1832 wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window )
1833 {
1834 ENSURE_LOADED_OR_RETURN(NULL);
1835 return new wxGDIPlusContext(this,(HWND) window);
1836 }
1837
1838 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( wxWindow* window )
1839 {
1840 ENSURE_LOADED_OR_RETURN(NULL);
1841 return new wxGDIPlusContext(this, (HWND) window->GetHWND() );
1842 }
1843
1844 // Path
1845
1846 wxGraphicsPath wxGDIPlusRenderer::CreatePath()
1847 {
1848 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPath);
1849 wxGraphicsPath m;
1850 m.SetRefData( new wxGDIPlusPathData(this));
1851 return m;
1852 }
1853
1854
1855 // Matrix
1856
1857 wxGraphicsMatrix wxGDIPlusRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1858 wxDouble tx, wxDouble ty)
1859
1860 {
1861 ENSURE_LOADED_OR_RETURN(wxNullGraphicsMatrix);
1862 wxGraphicsMatrix m;
1863 wxGDIPlusMatrixData* data = new wxGDIPlusMatrixData( this );
1864 data->Set( a,b,c,d,tx,ty ) ;
1865 m.SetRefData(data);
1866 return m;
1867 }
1868
1869 wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen)
1870 {
1871 ENSURE_LOADED_OR_RETURN(wxNullGraphicsPen);
1872 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1873 return wxNullGraphicsPen;
1874 else
1875 {
1876 wxGraphicsPen p;
1877 p.SetRefData(new wxGDIPlusPenData( this, pen ));
1878 return p;
1879 }
1880 }
1881
1882 wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush )
1883 {
1884 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
1885 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1886 return wxNullGraphicsBrush;
1887 else
1888 {
1889 wxGraphicsBrush p;
1890 p.SetRefData(new wxGDIPlusBrushData( this, brush ));
1891 return p;
1892 }
1893 }
1894
1895 wxGraphicsBrush
1896 wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
1897 wxDouble x2, wxDouble y2,
1898 const wxGraphicsGradientStops& stops)
1899 {
1900 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
1901 wxGraphicsBrush p;
1902 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
1903 d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
1904 p.SetRefData(d);
1905 return p;
1906 }
1907
1908 wxGraphicsBrush
1909 wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
1910 wxDouble xc, wxDouble yc,
1911 wxDouble radius,
1912 const wxGraphicsGradientStops& stops)
1913 {
1914 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
1915 wxGraphicsBrush p;
1916 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
1917 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,stops);
1918 p.SetRefData(d);
1919 return p;
1920 }
1921
1922 // sets the font
1923 wxGraphicsFont wxGDIPlusRenderer::CreateFont( const wxFont &font , const wxColour &col )
1924 {
1925 ENSURE_LOADED_OR_RETURN(wxNullGraphicsFont);
1926 if ( font.Ok() )
1927 {
1928 wxGraphicsFont p;
1929 p.SetRefData(new wxGDIPlusFontData( this , font, col ));
1930 return p;
1931 }
1932 else
1933 return wxNullGraphicsFont;
1934 }
1935
1936 wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmap( const wxBitmap &bitmap )
1937 {
1938 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
1939 if ( bitmap.Ok() )
1940 {
1941 wxGraphicsBitmap p;
1942 p.SetRefData(new wxGDIPlusBitmapData( this , bitmap ));
1943 return p;
1944 }
1945 else
1946 return wxNullGraphicsBitmap;
1947 }
1948
1949 wxGraphicsBitmap wxGDIPlusRenderer::CreateBitmapFromNativeBitmap( void *bitmap )
1950 {
1951 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
1952 if ( bitmap != NULL )
1953 {
1954 wxGraphicsBitmap p;
1955 p.SetRefData(new wxGDIPlusBitmapData( this , (Bitmap*) bitmap ));
1956 return p;
1957 }
1958 else
1959 return wxNullGraphicsBitmap;
1960 }
1961
1962 wxGraphicsBitmap wxGDIPlusRenderer::CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1963 {
1964 ENSURE_LOADED_OR_RETURN(wxNullGraphicsBitmap);
1965 Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bitmap.GetRefData())->GetGDIPlusBitmap();
1966 if ( image )
1967 {
1968 wxGraphicsBitmap p;
1969 p.SetRefData(new wxGDIPlusBitmapData( this , image->Clone( (REAL) x , (REAL) y , (REAL) w , (REAL) h , PixelFormat32bppPARGB) ));
1970 return p;
1971 }
1972 else
1973 return wxNullGraphicsBitmap;
1974 }
1975
1976 // Shutdown GDI+ at app exit, before possible dll unload
1977 class wxGDIPlusRendererModule : public wxModule
1978 {
1979 public:
1980 virtual bool OnInit() { return true; }
1981 virtual void OnExit() { gs_GDIPlusRenderer.Unload(); }
1982
1983 private:
1984 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule)
1985 };
1986
1987 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule)
1988
1989 // ----------------------------------------------------------------------------
1990 // wxMSW-specific parts of wxGCDC
1991 // ----------------------------------------------------------------------------
1992
1993 WXHDC wxGCDC::AcquireHDC()
1994 {
1995 wxGraphicsContext * const gc = GetGraphicsContext();
1996 if ( !gc )
1997 return NULL;
1998
1999 Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
2000 return g ? g->GetHDC() : NULL;
2001 }
2002
2003 void wxGCDC::ReleaseHDC(WXHDC hdc)
2004 {
2005 if ( !hdc )
2006 return;
2007
2008 wxGraphicsContext * const gc = GetGraphicsContext();
2009 wxCHECK_RET( gc, "can't release HDC because there is no wxGraphicsContext" );
2010
2011 Graphics * const g = static_cast<Graphics *>(gc->GetNativeContext());
2012 wxCHECK_RET( g, "can't release HDC because there is no Graphics" );
2013
2014 g->ReleaseHDC((HDC)hdc);
2015 }
2016
2017 #endif // wxUSE_GRAPHICS_CONTEXT