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