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