]> git.saurik.com Git - wxWidgets.git/blob - src/msw/graphics.cpp
cleanup
[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 #include "wx/dc.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
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/dc.h"
27 #include "wx/utils.h"
28 #include "wx/dialog.h"
29 #include "wx/app.h"
30 #include "wx/bitmap.h"
31 #include "wx/dcmemory.h"
32 #include "wx/log.h"
33 #include "wx/icon.h"
34 #include "wx/dcprint.h"
35 #include "wx/module.h"
36 #endif
37
38 #include "wx/graphics.h"
39 #include "wx/msw/wrapgdip.h"
40
41 #include "wx/stack.h"
42
43 WX_DECLARE_STACK(GraphicsState, GraphicsStates);
44
45 //-----------------------------------------------------------------------------
46 // constants
47 //-----------------------------------------------------------------------------
48
49 const double RAD2DEG = 180.0 / M_PI;
50
51 //-----------------------------------------------------------------------------
52 // Local functions
53 //-----------------------------------------------------------------------------
54
55 static inline double dmin(double a, double b) { return a < b ? a : b; }
56 static inline double dmax(double a, double b) { return a > b ? a : b; }
57
58 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
59 static inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; }
60
61 //-----------------------------------------------------------------------------
62 // device context implementation
63 //
64 // more and more of the dc functionality should be implemented by calling
65 // the appropricate wxGDIPlusContext, but we will have to do that step by step
66 // also coordinate conversions should be moved to native matrix ops
67 //-----------------------------------------------------------------------------
68
69 // we always stock two context states, one at entry, to be able to preserve the
70 // state we were called with, the other one after changing to HI Graphics orientation
71 // (this one is used for getting back clippings etc)
72
73 //-----------------------------------------------------------------------------
74 // wxGraphicsPath implementation
75 //-----------------------------------------------------------------------------
76
77 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
78
79 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
80 #include <commdlg.h>
81 #endif
82
83 class wxGDIPlusPathData : public wxGraphicsPathData
84 {
85 public :
86 wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path = NULL);
87 ~wxGDIPlusPathData();
88
89 virtual wxGraphicsObjectRefData *Clone() const;
90
91 //
92 // These are the path primitives from which everything else can be constructed
93 //
94
95 // begins a new subpath at (x,y)
96 virtual void MoveToPoint( wxDouble x, wxDouble y );
97
98 // adds a straight line from the current point to (x,y)
99 virtual void AddLineToPoint( wxDouble x, wxDouble y );
100
101 // adds a cubic Bezier curve from the current point, using two control points and an end point
102 virtual void AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y );
103
104
105 // adds an arc of a circle centering at (x,y) with radius (r) from startAngle to endAngle
106 virtual void AddArc( wxDouble x, wxDouble y, wxDouble r, wxDouble startAngle, wxDouble endAngle, bool clockwise ) ;
107
108 // gets the last point of the current path, (0,0) if not yet set
109 virtual void GetCurrentPoint( wxDouble* x, wxDouble* y) const;
110
111 // adds another path
112 virtual void AddPath( const wxGraphicsPathData* path );
113
114 // closes the current sub-path
115 virtual void CloseSubpath();
116
117 //
118 // These are convenience functions which - if not available natively will be assembled
119 // using the primitives from above
120 //
121
122 // appends a rectangle as a new closed subpath
123 virtual void AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h ) ;
124 /*
125
126 // appends an ellipsis as a new closed subpath fitting the passed rectangle
127 virtual void AddEllipsis( wxDouble x, wxDouble y, wxDouble w , wxDouble h ) ;
128
129 // 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)
130 virtual void AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r ) ;
131 */
132
133 // returns the native path
134 virtual void * GetNativePath() const { return m_path; }
135
136 // give the native path returned by GetNativePath() back (there might be some deallocations necessary)
137 virtual void UnGetNativePath(void * WXUNUSED(path)) const {}
138
139 // transforms each point of this path by the matrix
140 virtual void Transform( const wxGraphicsMatrixData* matrix ) ;
141
142 // gets the bounding box enclosing all points (possibly including control points)
143 virtual void GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const;
144
145 virtual bool Contains( wxDouble x, wxDouble y, int fillStyle = wxODDEVEN_RULE) const;
146
147 private :
148 GraphicsPath* m_path;
149 };
150
151 class wxGDIPlusMatrixData : public wxGraphicsMatrixData
152 {
153 public :
154 wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix = NULL) ;
155 virtual ~wxGDIPlusMatrixData() ;
156
157 virtual wxGraphicsObjectRefData* Clone() const ;
158
159 // concatenates the matrix
160 virtual void Concat( const wxGraphicsMatrixData *t );
161
162 // sets the matrix to the respective values
163 virtual void Set(wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
164 wxDouble tx=0.0, wxDouble ty=0.0);
165
166 // gets the component valuess of the matrix
167 virtual void Get(wxDouble* a=NULL, wxDouble* b=NULL, wxDouble* c=NULL,
168 wxDouble* d=NULL, wxDouble* tx=NULL, wxDouble* ty=NULL) const;
169
170 // makes this the inverse matrix
171 virtual void Invert();
172
173 // returns true if the elements of the transformation matrix are equal ?
174 virtual bool IsEqual( const wxGraphicsMatrixData* t) const ;
175
176 // return true if this is the identity matrix
177 virtual bool IsIdentity() const;
178
179 //
180 // transformation
181 //
182
183 // add the translation to this matrix
184 virtual void Translate( wxDouble dx , wxDouble dy );
185
186 // add the scale to this matrix
187 virtual void Scale( wxDouble xScale , wxDouble yScale );
188
189 // add the rotation to this matrix (radians)
190 virtual void Rotate( wxDouble angle );
191
192 //
193 // apply the transforms
194 //
195
196 // applies that matrix to the point
197 virtual void TransformPoint( wxDouble *x, wxDouble *y ) const;
198
199 // applies the matrix except for translations
200 virtual void TransformDistance( wxDouble *dx, wxDouble *dy ) const;
201
202 // returns the native representation
203 virtual void * GetNativeMatrix() const;
204 private:
205 Matrix* m_matrix ;
206 } ;
207
208 class wxGDIPlusPenData : public wxGraphicsObjectRefData
209 {
210 public:
211 wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen );
212 ~wxGDIPlusPenData();
213
214 void Init();
215
216 virtual wxDouble GetWidth() { return m_width; }
217 virtual Pen* GetGDIPlusPen() { return m_pen; }
218
219 protected :
220 Pen* m_pen;
221 Image* m_penImage;
222 Brush* m_penBrush;
223
224 wxDouble m_width;
225 };
226
227 class wxGDIPlusBrushData : public wxGraphicsObjectRefData
228 {
229 public:
230 wxGDIPlusBrushData( wxGraphicsRenderer* renderer );
231 wxGDIPlusBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
232 ~wxGDIPlusBrushData ();
233
234 void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
235 const wxColour&c1, const wxColour&c2 );
236 void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
237 const wxColour &oColor, const wxColour &cColor );
238 virtual Brush* GetGDIPlusBrush() { return m_brush; }
239
240 protected:
241 virtual void Init();
242
243 private :
244 Brush* m_brush;
245 Image* m_brushImage;
246 GraphicsPath* m_brushPath;
247 };
248
249 class wxGDIPlusFontData : public wxGraphicsObjectRefData
250 {
251 public:
252 wxGDIPlusFontData( wxGraphicsRenderer* renderer, const wxFont &font, const wxColour& col );
253 ~wxGDIPlusFontData();
254
255 virtual Brush* GetGDIPlusBrush() { return m_textBrush; }
256 virtual Font* GetGDIPlusFont() { return m_font; }
257 private :
258 Brush* m_textBrush;
259 Font* m_font;
260 };
261
262 class wxGDIPlusContext : public wxGraphicsContext
263 {
264 public:
265 wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc );
266 wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd );
267 wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr);
268 wxGDIPlusContext();
269
270 virtual ~wxGDIPlusContext();
271
272 virtual void Clip( const wxRegion &region );
273 // clips drawings to the rect
274 virtual void Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h );
275
276 // resets the clipping to original extent
277 virtual void ResetClip();
278
279 virtual void * GetNativeContext();
280
281 virtual void StrokePath( const wxGraphicsPath& p );
282 virtual void FillPath( const wxGraphicsPath& p , int fillStyle = wxODDEVEN_RULE );
283
284 // stroke lines connecting each of the points
285 virtual void StrokeLines( size_t n, const wxPoint2DDouble *points);
286
287 // draws a polygon
288 virtual void DrawLines( size_t n, const wxPoint2DDouble *points, int fillStyle = wxODDEVEN_RULE );
289
290 virtual void Translate( wxDouble dx , wxDouble dy );
291 virtual void Scale( wxDouble xScale , wxDouble yScale );
292 virtual void Rotate( wxDouble angle );
293
294 // concatenates this transform with the current transform of this context
295 virtual void ConcatTransform( const wxGraphicsMatrix& matrix );
296
297 // sets the transform of this context
298 virtual void SetTransform( const wxGraphicsMatrix& matrix );
299
300 // gets the matrix of this context
301 virtual wxGraphicsMatrix GetTransform() const;
302
303 virtual void DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
304 virtual void DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h );
305 virtual void PushState();
306 virtual void PopState();
307
308 virtual void DrawText( const wxString &str, wxDouble x, wxDouble y);
309 virtual void GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
310 wxDouble *descent, wxDouble *externalLeading ) const;
311 virtual void GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const;
312 virtual bool ShouldOffset() const;
313
314 private:
315 void Init();
316 void SetDefaults();
317
318 Graphics* m_context;
319 GraphicsStates m_stateStack;
320 GraphicsState m_state1;
321 GraphicsState m_state2;
322
323 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusContext)
324 };
325
326 class WXDLLIMPEXP_CORE wxGDIPlusMeasuringContext : public wxGDIPlusContext
327 {
328 public:
329 wxGDIPlusMeasuringContext( wxGraphicsRenderer* renderer ) : wxGDIPlusContext( renderer , m_hdc = GetDC(NULL) )
330 {
331 }
332 wxGDIPlusMeasuringContext()
333 {
334 }
335
336 virtual ~wxGDIPlusMeasuringContext()
337 {
338 ReleaseDC( NULL, m_hdc );
339 }
340
341 private:
342 HDC m_hdc ;
343 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusMeasuringContext)
344 } ;
345
346 //-----------------------------------------------------------------------------
347 // wxGDIPlusPen implementation
348 //-----------------------------------------------------------------------------
349
350 wxGDIPlusPenData::~wxGDIPlusPenData()
351 {
352 delete m_pen;
353 delete m_penImage;
354 delete m_penBrush;
355 }
356
357 void wxGDIPlusPenData::Init()
358 {
359 m_pen = NULL ;
360 m_penImage = NULL;
361 m_penBrush = NULL;
362 }
363
364 wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &pen )
365 : wxGraphicsObjectRefData(renderer)
366 {
367 Init();
368 m_width = pen.GetWidth();
369 if (m_width <= 0.0)
370 m_width = 0.1;
371
372 m_pen = new Pen(Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
373 pen.GetColour().Green() , pen.GetColour().Blue() ), m_width );
374
375 LineCap cap;
376 switch ( pen.GetCap() )
377 {
378 case wxCAP_ROUND :
379 cap = LineCapRound;
380 break;
381
382 case wxCAP_PROJECTING :
383 cap = LineCapSquare;
384 break;
385
386 case wxCAP_BUTT :
387 cap = LineCapFlat; // TODO verify
388 break;
389
390 default :
391 cap = LineCapFlat;
392 break;
393 }
394 m_pen->SetLineCap(cap,cap, DashCapFlat);
395
396 LineJoin join;
397 switch ( pen.GetJoin() )
398 {
399 case wxJOIN_BEVEL :
400 join = LineJoinBevel;
401 break;
402
403 case wxJOIN_MITER :
404 join = LineJoinMiter;
405 break;
406
407 case wxJOIN_ROUND :
408 join = LineJoinRound;
409 break;
410
411 default :
412 join = LineJoinMiter;
413 break;
414 }
415
416 m_pen->SetLineJoin(join);
417
418 m_pen->SetDashStyle(DashStyleSolid);
419
420 DashStyle dashStyle = DashStyleSolid;
421 switch ( pen.GetStyle() )
422 {
423 case wxSOLID :
424 break;
425
426 case wxDOT :
427 dashStyle = DashStyleDot;
428 break;
429
430 case wxLONG_DASH :
431 dashStyle = DashStyleDash; // TODO verify
432 break;
433
434 case wxSHORT_DASH :
435 dashStyle = DashStyleDash;
436 break;
437
438 case wxDOT_DASH :
439 dashStyle = DashStyleDashDot;
440 break;
441 case wxUSER_DASH :
442 {
443 dashStyle = DashStyleCustom;
444 wxDash *dashes;
445 int count = pen.GetDashes( &dashes );
446 if ((dashes != NULL) && (count > 0))
447 {
448 REAL *userLengths = new REAL[count];
449 for ( int i = 0; i < count; ++i )
450 {
451 userLengths[i] = dashes[i];
452 }
453 m_pen->SetDashPattern( userLengths, count);
454 delete[] userLengths;
455 }
456 }
457 break;
458 case wxSTIPPLE :
459 {
460 wxBitmap* bmp = pen.GetStipple();
461 if ( bmp && bmp->Ok() )
462 {
463 m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
464 m_penBrush = new TextureBrush(m_penImage);
465 m_pen->SetBrush( m_penBrush );
466 }
467
468 }
469 break;
470 default :
471 if ( pen.GetStyle() >= wxFIRST_HATCH && pen.GetStyle() <= wxLAST_HATCH )
472 {
473 HatchStyle style = HatchStyleHorizontal;
474 switch( pen.GetStyle() )
475 {
476 case wxBDIAGONAL_HATCH :
477 style = HatchStyleBackwardDiagonal;
478 break ;
479 case wxCROSSDIAG_HATCH :
480 style = HatchStyleDiagonalCross;
481 break ;
482 case wxFDIAGONAL_HATCH :
483 style = HatchStyleForwardDiagonal;
484 break ;
485 case wxCROSS_HATCH :
486 style = HatchStyleCross;
487 break ;
488 case wxHORIZONTAL_HATCH :
489 style = HatchStyleHorizontal;
490 break ;
491 case wxVERTICAL_HATCH :
492 style = HatchStyleVertical;
493 break ;
494
495 }
496 m_penBrush = new HatchBrush(style,Color( pen.GetColour().Alpha() , pen.GetColour().Red() ,
497 pen.GetColour().Green() , pen.GetColour().Blue() ), Color::Transparent );
498 m_pen->SetBrush( m_penBrush );
499 }
500 break;
501 }
502 if ( dashStyle != DashStyleSolid )
503 m_pen->SetDashStyle(dashStyle);
504 }
505
506 //-----------------------------------------------------------------------------
507 // wxGDIPlusBrush implementation
508 //-----------------------------------------------------------------------------
509
510 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer )
511 : wxGraphicsObjectRefData(renderer)
512 {
513 Init();
514 }
515
516 wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer , const wxBrush &brush )
517 : wxGraphicsObjectRefData(renderer)
518 {
519 Init();
520 if ( brush.GetStyle() == wxSOLID)
521 {
522 m_brush = new SolidBrush( Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
523 brush.GetColour().Green() , brush.GetColour().Blue() ) );
524 }
525 else if ( brush.IsHatch() )
526 {
527 HatchStyle style = HatchStyleHorizontal;
528 switch( brush.GetStyle() )
529 {
530 case wxBDIAGONAL_HATCH :
531 style = HatchStyleBackwardDiagonal;
532 break ;
533 case wxCROSSDIAG_HATCH :
534 style = HatchStyleDiagonalCross;
535 break ;
536 case wxFDIAGONAL_HATCH :
537 style = HatchStyleForwardDiagonal;
538 break ;
539 case wxCROSS_HATCH :
540 style = HatchStyleCross;
541 break ;
542 case wxHORIZONTAL_HATCH :
543 style = HatchStyleHorizontal;
544 break ;
545 case wxVERTICAL_HATCH :
546 style = HatchStyleVertical;
547 break ;
548
549 }
550 m_brush = new HatchBrush(style,Color( brush.GetColour().Alpha() , brush.GetColour().Red() ,
551 brush.GetColour().Green() , brush.GetColour().Blue() ), Color::Transparent );
552 }
553 else
554 {
555 wxBitmap* bmp = brush.GetStipple();
556 if ( bmp && bmp->Ok() )
557 {
558 wxDELETE( m_brushImage );
559 m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
560 m_brush = new TextureBrush(m_brushImage);
561 }
562 }
563 }
564
565 wxGDIPlusBrushData::~wxGDIPlusBrushData()
566 {
567 delete m_brush;
568 delete m_brushImage;
569 delete m_brushPath;
570 };
571
572 void wxGDIPlusBrushData::Init()
573 {
574 m_brush = NULL;
575 m_brushImage= NULL;
576 m_brushPath= NULL;
577 }
578
579 void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2)
580 {
581 m_brush = new LinearGradientBrush( PointF( x1,y1) , PointF( x2,y2),
582 Color( c1.Alpha(), c1.Red(),c1.Green() , c1.Blue() ),
583 Color( c2.Alpha(), c2.Red(),c2.Green() , c2.Blue() ));
584 }
585
586 void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
587 const wxColour &oColor, const wxColour &cColor)
588 {
589 // Create a path that consists of a single circle.
590 m_brushPath = new GraphicsPath();
591 m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius), (REAL)(2*radius), (REAL)(2*radius));
592
593 PathGradientBrush *b = new PathGradientBrush(m_brushPath);
594 m_brush = b;
595 b->SetCenterPoint( PointF(xo,yo));
596 b->SetCenterColor(Color( oColor.Alpha(), oColor.Red(),oColor.Green() , oColor.Blue() ));
597
598 Color colors[] = {Color( cColor.Alpha(), cColor.Red(),cColor.Green() , cColor.Blue() )};
599 int count = 1;
600 b->SetSurroundColors(colors, &count);
601 }
602
603 //-----------------------------------------------------------------------------
604 // wxGDIPlusFont implementation
605 //-----------------------------------------------------------------------------
606
607 wxGDIPlusFontData::wxGDIPlusFontData( wxGraphicsRenderer* renderer, const wxFont &font,
608 const wxColour& col ) : wxGraphicsObjectRefData( renderer )
609 {
610 m_textBrush = NULL;
611 m_font = NULL;
612
613 wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI );
614 int size = font.GetPointSize();
615 int style = FontStyleRegular;
616 if ( font.GetStyle() == wxFONTSTYLE_ITALIC )
617 style |= FontStyleItalic;
618 if ( font.GetUnderlined() )
619 style |= FontStyleUnderline;
620 if ( font.GetWeight() == wxFONTWEIGHT_BOLD )
621 style |= FontStyleBold;
622 m_font = new Font( s , size , style );
623 m_textBrush = new SolidBrush( Color( col.Alpha() , col.Red() ,
624 col.Green() , col.Blue() ));
625 }
626
627 wxGDIPlusFontData::~wxGDIPlusFontData()
628 {
629 delete m_textBrush;
630 delete m_font;
631 }
632
633 //-----------------------------------------------------------------------------
634 // wxGDIPlusPath implementation
635 //-----------------------------------------------------------------------------
636
637 wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path ) : wxGraphicsPathData(renderer)
638 {
639 if ( path )
640 m_path = path;
641 else
642 m_path = new GraphicsPath();
643 }
644
645 wxGDIPlusPathData::~wxGDIPlusPathData()
646 {
647 delete m_path;
648 }
649
650 wxGraphicsObjectRefData* wxGDIPlusPathData::Clone() const
651 {
652 return new wxGDIPlusPathData( GetRenderer() , m_path->Clone());
653 }
654
655 //
656 // The Primitives
657 //
658
659 void wxGDIPlusPathData::MoveToPoint( wxDouble x , wxDouble y )
660 {
661 m_path->StartFigure();
662 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
663 }
664
665 void wxGDIPlusPathData::AddLineToPoint( wxDouble x , wxDouble y )
666 {
667 m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
668 }
669
670 void wxGDIPlusPathData::CloseSubpath()
671 {
672 m_path->CloseFigure();
673 }
674
675 void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
676 {
677 PointF c1(cx1,cy1);
678 PointF c2(cx2,cy2);
679 PointF end(x,y);
680 PointF start;
681 m_path->GetLastPoint(&start);
682 m_path->AddBezier(start,c1,c2,end);
683 }
684
685 // gets the last point of the current path, (0,0) if not yet set
686 void wxGDIPlusPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
687 {
688 PointF start;
689 m_path->GetLastPoint(&start);
690 *x = start.X ;
691 *y = start.Y ;
692 }
693
694 void wxGDIPlusPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
695 {
696 double sweepAngle = endAngle - startAngle ;
697 if( fabs(sweepAngle) >= 2*M_PI)
698 {
699 sweepAngle = 2 * M_PI;
700 }
701 else
702 {
703 if ( clockwise )
704 {
705 if( sweepAngle < 0 )
706 sweepAngle += 2 * M_PI;
707 }
708 else
709 {
710 if( sweepAngle > 0 )
711 sweepAngle -= 2 * M_PI;
712
713 }
714 }
715 m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle));
716 }
717
718 void wxGDIPlusPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
719 {
720 m_path->AddRectangle(RectF(x,y,w,h));
721 }
722
723 void wxGDIPlusPathData::AddPath( const wxGraphicsPathData* path )
724 {
725 m_path->AddPath( (GraphicsPath*) path->GetNativePath(), FALSE);
726 }
727
728
729 // transforms each point of this path by the matrix
730 void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData* matrix )
731 {
732 m_path->Transform( (Matrix*) matrix->GetNativeMatrix() );
733 }
734
735 // gets the bounding box enclosing all points (possibly including control points)
736 void wxGDIPlusPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
737 {
738 RectF bounds;
739 m_path->GetBounds( &bounds, NULL, NULL) ;
740 *x = bounds.X;
741 *y = bounds.Y;
742 *w = bounds.Width;
743 *h = bounds.Height;
744 }
745
746 bool wxGDIPlusPathData::Contains( wxDouble x, wxDouble y, int fillStyle ) const
747 {
748 m_path->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
749 return m_path->IsVisible( (FLOAT) x,(FLOAT) y) == TRUE ;
750 }
751
752 //-----------------------------------------------------------------------------
753 // wxGDIPlusMatrixData implementation
754 //-----------------------------------------------------------------------------
755
756 wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix )
757 : wxGraphicsMatrixData(renderer)
758 {
759 if ( matrix )
760 m_matrix = matrix ;
761 else
762 m_matrix = new Matrix();
763 }
764
765 wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
766 {
767 delete m_matrix;
768 }
769
770 wxGraphicsObjectRefData *wxGDIPlusMatrixData::Clone() const
771 {
772 return new wxGDIPlusMatrixData( GetRenderer(), m_matrix->Clone());
773 }
774
775 // concatenates the matrix
776 void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData *t )
777 {
778 m_matrix->Multiply( (Matrix*) t->GetNativeMatrix());
779 }
780
781 // sets the matrix to the respective values
782 void wxGDIPlusMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
783 wxDouble tx, wxDouble ty)
784 {
785 m_matrix->SetElements(a,b,c,d,tx,ty);
786 }
787
788 // gets the component valuess of the matrix
789 void wxGDIPlusMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
790 wxDouble* d, wxDouble* tx, wxDouble* ty) const
791 {
792 REAL elements[6];
793 m_matrix->GetElements(elements);
794 if (a) *a = elements[0];
795 if (b) *b = elements[1];
796 if (c) *c = elements[2];
797 if (d) *d = elements[3];
798 if (tx) *tx= elements[4];
799 if (ty) *ty= elements[5];
800 }
801
802 // makes this the inverse matrix
803 void wxGDIPlusMatrixData::Invert()
804 {
805 m_matrix->Invert();
806 }
807
808 // returns true if the elements of the transformation matrix are equal ?
809 bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
810 {
811 return m_matrix->Equals((Matrix*) t->GetNativeMatrix())== TRUE ;
812 }
813
814 // return true if this is the identity matrix
815 bool wxGDIPlusMatrixData::IsIdentity() const
816 {
817 return m_matrix->IsIdentity() == TRUE ;
818 }
819
820 //
821 // transformation
822 //
823
824 // add the translation to this matrix
825 void wxGDIPlusMatrixData::Translate( wxDouble dx , wxDouble dy )
826 {
827 m_matrix->Translate(dx,dy);
828 }
829
830 // add the scale to this matrix
831 void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale )
832 {
833 m_matrix->Scale(xScale,yScale);
834 }
835
836 // add the rotation to this matrix (radians)
837 void wxGDIPlusMatrixData::Rotate( wxDouble angle )
838 {
839 m_matrix->Rotate( angle );
840 }
841
842 //
843 // apply the transforms
844 //
845
846 // applies that matrix to the point
847 void wxGDIPlusMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
848 {
849 PointF pt(*x,*y);
850 m_matrix->TransformPoints(&pt);
851 *x = pt.X;
852 *y = pt.Y;
853 }
854
855 // applies the matrix except for translations
856 void wxGDIPlusMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
857 {
858 PointF pt(*dx,*dy);
859 m_matrix->TransformVectors(&pt);
860 *dx = pt.X;
861 *dy = pt.Y;
862 }
863
864 // returns the native representation
865 void * wxGDIPlusMatrixData::GetNativeMatrix() const
866 {
867 return m_matrix;
868 }
869
870 //-----------------------------------------------------------------------------
871 // wxGDIPlusContext implementation
872 //-----------------------------------------------------------------------------
873
874 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext,wxGraphicsContext)
875 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext,wxGDIPlusContext)
876
877 class wxGDIPlusOffsetHelper
878 {
879 public :
880 wxGDIPlusOffsetHelper( Graphics* gr , bool offset )
881 {
882 m_gr = gr;
883 m_offset = offset;
884 if ( m_offset )
885 m_gr->TranslateTransform( 0.5, 0.5 );
886 }
887 ~wxGDIPlusOffsetHelper( )
888 {
889 if ( m_offset )
890 m_gr->TranslateTransform( -0.5, -0.5 );
891 }
892 public :
893 Graphics* m_gr;
894 bool m_offset;
895 } ;
896
897 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc )
898 : wxGraphicsContext(renderer)
899 {
900 Init();
901 m_context = new Graphics( hdc);
902 SetDefaults();
903 }
904
905 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd )
906 : wxGraphicsContext(renderer)
907 {
908 Init();
909 m_context = new Graphics( hwnd);
910 SetDefaults();
911 }
912
913 wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr )
914 : wxGraphicsContext(renderer)
915 {
916 Init();
917 m_context = gr;
918 SetDefaults();
919 }
920
921 wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL)
922 {
923 Init();
924 }
925
926 void wxGDIPlusContext::Init()
927 {
928 m_context = NULL;
929 m_state1 = 0;
930 m_state2= 0;
931 }
932
933 void wxGDIPlusContext::SetDefaults()
934 {
935 m_context->SetTextRenderingHint(TextRenderingHintSystemDefault);
936 m_context->SetPixelOffsetMode(PixelOffsetModeHalf);
937 m_context->SetSmoothingMode(SmoothingModeHighQuality);
938 m_state1 = m_context->Save();
939 m_state2 = m_context->Save();
940 }
941
942 wxGDIPlusContext::~wxGDIPlusContext()
943 {
944 if ( m_context )
945 {
946 m_context->Restore( m_state2 );
947 m_context->Restore( m_state1 );
948 delete m_context;
949 }
950 }
951
952
953 void wxGDIPlusContext::Clip( const wxRegion &region )
954 {
955 Region rgn((HRGN)region.GetHRGN());
956 m_context->SetClip(&rgn,CombineModeIntersect);
957 }
958
959 void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
960 {
961 m_context->SetClip(RectF(x,y,w,h),CombineModeIntersect);
962 }
963
964 void wxGDIPlusContext::ResetClip()
965 {
966 m_context->ResetClip();
967 }
968
969 void wxGDIPlusContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
970 {
971 if ( !m_pen.IsNull() )
972 {
973 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
974 Point *cpoints = new Point[n];
975 for (size_t i = 0; i < n; i++)
976 {
977 cpoints[i].X = (int)(points[i].m_x );
978 cpoints[i].Y = (int)(points[i].m_y );
979
980 } // for (size_t i = 0; i < n; i++)
981 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
982 delete[] cpoints;
983 }
984 }
985
986 void wxGDIPlusContext::DrawLines( size_t n, const wxPoint2DDouble *points, int WXUNUSED(fillStyle) )
987 {
988 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
989 Point *cpoints = new Point[n];
990 for (size_t i = 0; i < n; i++)
991 {
992 cpoints[i].X = (int)(points[i].m_x );
993 cpoints[i].Y = (int)(points[i].m_y );
994
995 } // for (int i = 0; i < n; i++)
996 if ( !m_brush.IsNull() )
997 m_context->FillPolygon( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , cpoints , n ) ;
998 if ( !m_pen.IsNull() )
999 m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
1000 delete[] cpoints;
1001 }
1002
1003 void wxGDIPlusContext::StrokePath( const wxGraphicsPath& path )
1004 {
1005 if ( !m_pen.IsNull() )
1006 {
1007 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1008 m_context->DrawPath( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath*) path.GetNativePath() );
1009 }
1010 }
1011
1012 void wxGDIPlusContext::FillPath( const wxGraphicsPath& path , int fillStyle )
1013 {
1014 if ( !m_brush.IsNull() )
1015 {
1016 wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
1017 ((GraphicsPath*) path.GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
1018 m_context->FillPath( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() ,
1019 (GraphicsPath*) path.GetNativePath());
1020 }
1021 }
1022
1023 void wxGDIPlusContext::Rotate( wxDouble angle )
1024 {
1025 m_context->RotateTransform( RadToDeg(angle) );
1026 }
1027
1028 void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
1029 {
1030 m_context->TranslateTransform( dx , dy );
1031 }
1032
1033 void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
1034 {
1035 m_context->ScaleTransform(xScale,yScale);
1036 }
1037
1038 void wxGDIPlusContext::PushState()
1039 {
1040 GraphicsState state = m_context->Save();
1041 m_stateStack.push(state);
1042 }
1043
1044 void wxGDIPlusContext::PopState()
1045 {
1046 GraphicsState state = m_stateStack.top();
1047 m_stateStack.pop();
1048 m_context->Restore(state);
1049 }
1050
1051 // the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
1052 // premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
1053 // bytes as parameter
1054
1055 void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1056 {
1057 Bitmap* image = NULL;
1058 Bitmap* helper = NULL;
1059 if ( bmp.GetMask() )
1060 {
1061 Bitmap interim((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE()) ;
1062
1063 size_t width = interim.GetWidth();
1064 size_t height = interim.GetHeight();
1065 Rect bounds(0,0,width,height);
1066
1067 image = new Bitmap(width,height,PixelFormat32bppPARGB) ;
1068
1069 Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL);
1070 wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed);
1071
1072 BitmapData dataMask ;
1073 interimMask.LockBits(&bounds,ImageLockModeRead,
1074 interimMask.GetPixelFormat(),&dataMask);
1075
1076
1077 BitmapData imageData ;
1078 image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData);
1079
1080 BYTE maskPattern = 0 ;
1081 BYTE maskByte = 0;
1082 size_t maskIndex ;
1083
1084 for ( size_t y = 0 ; y < height ; ++y)
1085 {
1086 maskIndex = 0 ;
1087 for( size_t x = 0 ; x < width; ++x)
1088 {
1089 if ( x % 8 == 0)
1090 {
1091 maskPattern = 0x80;
1092 maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex);
1093 maskIndex++;
1094 }
1095 else
1096 maskPattern = maskPattern >> 1;
1097
1098 ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4);
1099 if ( (maskByte & maskPattern) == 0 )
1100 *dest = 0x00000000;
1101 else
1102 {
1103 Color c ;
1104 interim.GetPixel(x,y,&c) ;
1105 *dest = (c.GetValue() | Color::AlphaMask);
1106 }
1107 }
1108 }
1109
1110 image->UnlockBits(&imageData);
1111
1112 interimMask.UnlockBits(&dataMask);
1113 interim.UnlockBits(&dataMask);
1114 }
1115 else
1116 {
1117 image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
1118 if ( bmp.HasAlpha() && GetPixelFormatSize(image->GetPixelFormat()) == 32 )
1119 {
1120 size_t width = image->GetWidth();
1121 size_t height = image->GetHeight();
1122 Rect bounds(0,0,width,height);
1123 BitmapData data ;
1124
1125 helper = image ;
1126 image = NULL ;
1127 helper->LockBits(&bounds, ImageLockModeRead,
1128 helper->GetPixelFormat(),&data);
1129
1130 image = new Bitmap(data.Width, data.Height, data.Stride,
1131 PixelFormat32bppPARGB , (BYTE*) data.Scan0);
1132
1133 helper->UnlockBits(&data);
1134 }
1135 }
1136 if ( image )
1137 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1138 delete image ;
1139 delete helper ;
1140 }
1141
1142 void wxGDIPlusContext::DrawIcon( const wxIcon &icon, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
1143 {
1144 // the built-in conversion fails when there is alpha in the HICON (eg XP style icons), we can only
1145 // find out by looking at the bitmap data whether there really was alpha in it
1146 HICON hIcon = (HICON)icon.GetHICON();
1147 ICONINFO iconInfo ;
1148 // IconInfo creates the bitmaps for color and mask, we must dispose of them after use
1149 if (!GetIconInfo(hIcon,&iconInfo))
1150 return;
1151
1152 Bitmap interim(iconInfo.hbmColor,NULL);
1153
1154 Bitmap* image = NULL ;
1155
1156 // if it's not 32 bit, it doesn't have an alpha channel, note that since the conversion doesn't
1157 // work correctly, asking IsAlphaPixelFormat at this point fails as well
1158 if( GetPixelFormatSize(interim.GetPixelFormat())!= 32 )
1159 {
1160 image = Bitmap::FromHICON(hIcon);
1161 }
1162 else
1163 {
1164 size_t width = interim.GetWidth();
1165 size_t height = interim.GetHeight();
1166 Rect bounds(0,0,width,height);
1167 BitmapData data ;
1168
1169 interim.LockBits(&bounds, ImageLockModeRead,
1170 interim.GetPixelFormat(),&data);
1171
1172 bool hasAlpha = false;
1173 for ( size_t y = 0 ; y < height && !hasAlpha ; ++y)
1174 {
1175 for( size_t x = 0 ; x < width && !hasAlpha; ++x)
1176 {
1177 ARGB *dest = (ARGB*)((BYTE*)data.Scan0 + data.Stride*y + x*4);
1178 if ( ( *dest & Color::AlphaMask ) != 0 )
1179 hasAlpha = true;
1180 }
1181 }
1182
1183 if ( hasAlpha )
1184 {
1185 image = new Bitmap(data.Width, data.Height, data.Stride,
1186 PixelFormat32bppARGB , (BYTE*) data.Scan0);
1187 }
1188 else
1189 {
1190 image = Bitmap::FromHICON(hIcon);
1191 }
1192
1193 interim.UnlockBits(&data);
1194 }
1195
1196 m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
1197
1198 delete image ;
1199 DeleteObject(iconInfo.hbmColor);
1200 DeleteObject(iconInfo.hbmMask);
1201 }
1202
1203 void wxGDIPlusContext::DrawText( const wxString &str, wxDouble x, wxDouble y )
1204 {
1205 if ( m_font.IsNull() || str.IsEmpty())
1206 return ;
1207
1208 wxWCharBuffer s = str.wc_str( *wxConvUI );
1209 m_context->DrawString( s , -1 , ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont() ,
1210 PointF( x , y ) , StringFormat::GenericTypographic() , ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusBrush() );
1211 }
1212
1213 void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDouble *height,
1214 wxDouble *descent, wxDouble *externalLeading ) const
1215 {
1216 wxWCharBuffer s = str.wc_str( *wxConvUI );
1217 FontFamily ffamily ;
1218 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
1219
1220 f->GetFamily(&ffamily) ;
1221
1222 REAL factorY = m_context->GetDpiY() / 72.0 ;
1223
1224 REAL rDescent = ffamily.GetCellDescent(FontStyleRegular) *
1225 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1226 REAL rAscent = ffamily.GetCellAscent(FontStyleRegular) *
1227 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1228 REAL rHeight = ffamily.GetLineSpacing(FontStyleRegular) *
1229 f->GetSize() / ffamily.GetEmHeight(FontStyleRegular);
1230
1231 if ( height )
1232 *height = rHeight * factorY;
1233 if ( descent )
1234 *descent = rDescent * factorY;
1235 if ( externalLeading )
1236 *externalLeading = (rHeight - rAscent - rDescent) * factorY;
1237 // measuring empty strings is not guaranteed, so do it by hand
1238 if ( str.IsEmpty())
1239 {
1240 if ( width )
1241 *width = 0 ;
1242 }
1243 else
1244 {
1245 RectF layoutRect(0,0, 100000.0f, 100000.0f);
1246 StringFormat strFormat( StringFormat::GenericTypographic() );
1247 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1248
1249 RectF bounds ;
1250 m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ;
1251 if ( width )
1252 *width = bounds.Width;
1253 }
1254 }
1255
1256 void wxGDIPlusContext::GetPartialTextExtents(const wxString& text, wxArrayDouble& widths) const
1257 {
1258 widths.Empty();
1259 widths.Add(0, text.length());
1260
1261 if (text.empty())
1262 return;
1263
1264 Font* f = ((wxGDIPlusFontData*)m_font.GetRefData())->GetGDIPlusFont();
1265 wxWCharBuffer ws = text.wc_str( *wxConvUI );
1266 size_t len = wcslen( ws ) ;
1267 wxASSERT_MSG(text.length() == len , wxT("GetPartialTextExtents not yet implemented for multichar situations"));
1268
1269 RectF layoutRect(0,0, 100000.0f, 100000.0f);
1270 StringFormat strFormat( StringFormat::GenericTypographic() );
1271
1272 CharacterRange* ranges = new CharacterRange[len] ;
1273 Region* regions = new Region[len];
1274 for( size_t i = 0 ; i < len ; ++i)
1275 {
1276 ranges[i].First = i ;
1277 ranges[i].Length = 1 ;
1278 }
1279 strFormat.SetMeasurableCharacterRanges(len,ranges);
1280 strFormat.SetFormatFlags( StringFormatFlagsMeasureTrailingSpaces | strFormat.GetFormatFlags() );
1281 m_context->MeasureCharacterRanges(ws, -1 , f,layoutRect, &strFormat,1,regions) ;
1282
1283 RectF bbox ;
1284 for ( size_t i = 0 ; i < len ; ++i)
1285 {
1286 regions[i].GetBounds(&bbox,m_context);
1287 widths[i] = bbox.GetRight()-bbox.GetLeft();
1288 }
1289 }
1290
1291 bool wxGDIPlusContext::ShouldOffset() const
1292 {
1293 int penwidth = 0 ;
1294 if ( !m_pen.IsNull() )
1295 {
1296 penwidth = (int)((wxGDIPlusPenData*)m_pen.GetRefData())->GetWidth();
1297 if ( penwidth == 0 )
1298 penwidth = 1;
1299 }
1300 return ( penwidth % 2 ) == 1;
1301 }
1302
1303 void* wxGDIPlusContext::GetNativeContext()
1304 {
1305 return m_context;
1306 }
1307
1308 // concatenates this transform with the current transform of this context
1309 void wxGDIPlusContext::ConcatTransform( const wxGraphicsMatrix& matrix )
1310 {
1311 m_context->MultiplyTransform((Matrix*) matrix.GetNativeMatrix());
1312 }
1313
1314 // sets the transform of this context
1315 void wxGDIPlusContext::SetTransform( const wxGraphicsMatrix& matrix )
1316 {
1317 m_context->SetTransform((Matrix*) matrix.GetNativeMatrix());
1318 }
1319
1320 // gets the matrix of this context
1321 wxGraphicsMatrix wxGDIPlusContext::GetTransform() const
1322 {
1323 wxGraphicsMatrix matrix = CreateMatrix();
1324 m_context->GetTransform((Matrix*) matrix.GetNativeMatrix());
1325 return matrix;
1326 }
1327 //-----------------------------------------------------------------------------
1328 // wxGDIPlusRenderer declaration
1329 //-----------------------------------------------------------------------------
1330
1331 class wxGDIPlusRenderer : public wxGraphicsRenderer
1332 {
1333 public :
1334 wxGDIPlusRenderer()
1335 {
1336 m_loaded = false;
1337 m_gditoken = 0;
1338 }
1339
1340 virtual ~wxGDIPlusRenderer()
1341 {
1342 if (m_loaded)
1343 {
1344 Unload();
1345 }
1346 }
1347
1348 // Context
1349
1350 virtual wxGraphicsContext * CreateContext( const wxWindowDC& dc);
1351
1352 virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
1353
1354 virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
1355
1356 virtual wxGraphicsContext * CreateContextFromNativeWindow( void * window );
1357
1358 virtual wxGraphicsContext * CreateContext( wxWindow* window );
1359
1360 virtual wxGraphicsContext * CreateMeasuringContext();
1361
1362 // Path
1363
1364 virtual wxGraphicsPath CreatePath();
1365
1366 // Matrix
1367
1368 virtual wxGraphicsMatrix CreateMatrix( wxDouble a=1.0, wxDouble b=0.0, wxDouble c=0.0, wxDouble d=1.0,
1369 wxDouble tx=0.0, wxDouble ty=0.0);
1370
1371
1372 virtual wxGraphicsPen CreatePen(const wxPen& pen) ;
1373
1374 virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
1375
1376 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1377 virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1378 const wxColour&c1, const wxColour&c2) ;
1379
1380 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1381 // with radius r and color cColor
1382 virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1383 const wxColour &oColor, const wxColour &cColor) ;
1384
1385 // sets the font
1386 virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
1387 protected :
1388 void EnsureIsLoaded();
1389 void Load();
1390 void Unload();
1391 friend class wxGDIPlusRendererModule;
1392
1393 private :
1394 bool m_loaded;
1395 ULONG_PTR m_gditoken;
1396
1397 DECLARE_DYNAMIC_CLASS_NO_COPY(wxGDIPlusRenderer)
1398 } ;
1399
1400 //-----------------------------------------------------------------------------
1401 // wxGDIPlusRenderer implementation
1402 //-----------------------------------------------------------------------------
1403
1404 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRenderer,wxGraphicsRenderer)
1405
1406 static wxGDIPlusRenderer gs_GDIPlusRenderer;
1407
1408 wxGraphicsRenderer* wxGraphicsRenderer::GetDefaultRenderer()
1409 {
1410 return &gs_GDIPlusRenderer;
1411 }
1412
1413 void wxGDIPlusRenderer::EnsureIsLoaded()
1414 {
1415 if (!m_loaded)
1416 {
1417 Load();
1418 }
1419 }
1420
1421 void wxGDIPlusRenderer::Load()
1422 {
1423 GdiplusStartupInput input;
1424 GdiplusStartupOutput output;
1425 GdiplusStartup(&m_gditoken,&input,&output);
1426 m_loaded = true;
1427 }
1428
1429 void wxGDIPlusRenderer::Unload()
1430 {
1431 if ( m_gditoken )
1432 {
1433 GdiplusShutdown(m_gditoken);
1434 m_gditoken = NULL;
1435 }
1436 m_loaded = false;
1437 }
1438
1439 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxWindowDC& dc)
1440 {
1441 EnsureIsLoaded();
1442 return new wxGDIPlusContext(this,(HDC) dc.GetHDC());
1443 }
1444
1445 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxMemoryDC& dc)
1446 {
1447 EnsureIsLoaded();
1448 return new wxGDIPlusContext(this,(HDC) dc.GetHDC());
1449 }
1450
1451 wxGraphicsContext * wxGDIPlusRenderer::CreateMeasuringContext()
1452 {
1453 EnsureIsLoaded();
1454 return new wxGDIPlusMeasuringContext(this);
1455 }
1456
1457 wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeContext( void * context )
1458 {
1459 EnsureIsLoaded();
1460 return new wxGDIPlusContext(this,(Graphics*) context);
1461 }
1462
1463
1464 wxGraphicsContext * wxGDIPlusRenderer::CreateContextFromNativeWindow( void * window )
1465 {
1466 EnsureIsLoaded();
1467 return new wxGDIPlusContext(this,(HWND) window);
1468 }
1469
1470 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( wxWindow* window )
1471 {
1472 EnsureIsLoaded();
1473 return new wxGDIPlusContext(this, (HWND) window->GetHWND() );
1474 }
1475
1476 // Path
1477
1478 wxGraphicsPath wxGDIPlusRenderer::CreatePath()
1479 {
1480 EnsureIsLoaded();
1481 wxGraphicsPath m;
1482 m.SetRefData( new wxGDIPlusPathData(this));
1483 return m;
1484 }
1485
1486
1487 // Matrix
1488
1489 wxGraphicsMatrix wxGDIPlusRenderer::CreateMatrix( wxDouble a, wxDouble b, wxDouble c, wxDouble d,
1490 wxDouble tx, wxDouble ty)
1491
1492 {
1493 EnsureIsLoaded();
1494 wxGraphicsMatrix m;
1495 wxGDIPlusMatrixData* data = new wxGDIPlusMatrixData( this );
1496 data->Set( a,b,c,d,tx,ty ) ;
1497 m.SetRefData(data);
1498 return m;
1499 }
1500
1501 wxGraphicsPen wxGDIPlusRenderer::CreatePen(const wxPen& pen)
1502 {
1503 EnsureIsLoaded();
1504 if ( !pen.Ok() || pen.GetStyle() == wxTRANSPARENT )
1505 return wxNullGraphicsPen;
1506 else
1507 {
1508 wxGraphicsPen p;
1509 p.SetRefData(new wxGDIPlusPenData( this, pen ));
1510 return p;
1511 }
1512 }
1513
1514 wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush )
1515 {
1516 EnsureIsLoaded();
1517 if ( !brush.Ok() || brush.GetStyle() == wxTRANSPARENT )
1518 return wxNullGraphicsBrush;
1519 else
1520 {
1521 wxGraphicsBrush p;
1522 p.SetRefData(new wxGDIPlusBrushData( this, brush ));
1523 return p;
1524 }
1525 }
1526
1527 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
1528 wxGraphicsBrush wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
1529 const wxColour&c1, const wxColour&c2)
1530 {
1531 EnsureIsLoaded();
1532 wxGraphicsBrush p;
1533 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
1534 d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
1535 p.SetRefData(d);
1536 return p;
1537 }
1538
1539 // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
1540 // with radius r and color cColor
1541 wxGraphicsBrush wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
1542 const wxColour &oColor, const wxColour &cColor)
1543 {
1544 EnsureIsLoaded();
1545 wxGraphicsBrush p;
1546 wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
1547 d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
1548 p.SetRefData(d);
1549 return p;
1550 }
1551
1552 // sets the font
1553 wxGraphicsFont wxGDIPlusRenderer::CreateFont( const wxFont &font , const wxColour &col )
1554 {
1555 EnsureIsLoaded();
1556 if ( font.Ok() )
1557 {
1558 wxGraphicsFont p;
1559 p.SetRefData(new wxGDIPlusFontData( this , font, col ));
1560 return p;
1561 }
1562 else
1563 return wxNullGraphicsFont;
1564 }
1565
1566 // Shutdown GDI+ at app exit, before possible dll unload
1567 class wxGDIPlusRendererModule : public wxModule
1568 {
1569 public:
1570 virtual bool OnInit() { return true; }
1571 virtual void OnExit() { gs_GDIPlusRenderer.Unload(); }
1572
1573 private:
1574 DECLARE_DYNAMIC_CLASS(wxGDIPlusRendererModule)
1575 };
1576
1577 IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusRendererModule, wxModule)
1578
1579 #endif // wxUSE_GRAPHICS_CONTEXT