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