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