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