+ m_textBrush = NULL;
+ m_font = NULL;
+
+ wxWCharBuffer s = font.GetFaceName().wc_str( *wxConvUI );
+ int size = font.GetPointSize();
+ int style = FontStyleRegular;
+ if ( font.GetStyle() == wxFONTSTYLE_ITALIC )
+ style |= FontStyleItalic;
+ if ( font.GetUnderlined() )
+ style |= FontStyleUnderline;
+ if ( font.GetWeight() == wxFONTWEIGHT_BOLD )
+ style |= FontStyleBold;
+ m_font = new Font( s , size , style );
+ m_textBrush = new SolidBrush( Color( col.Alpha() , col.Red() ,
+ col.Green() , col.Blue() ));
+}
+
+wxGDIPlusFontData::~wxGDIPlusFontData()
+{
+ delete m_textBrush;
+ delete m_font;
+}
+
+// the built-in conversions functions create non-premultiplied bitmaps, while GDIPlus needs them in the
+// premultiplied format, therefore in the failing cases we create a new bitmap using the non-premultiplied
+// bytes as parameter, since there is no real copying of the data going in, only references are stored
+// m_helper has to be kept alive as well
+
+//-----------------------------------------------------------------------------
+// wxGDIPlusBitmapData implementation
+//-----------------------------------------------------------------------------
+
+wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer, Bitmap* bitmap ) :
+ wxGraphicsObjectRefData( renderer ), m_bitmap( bitmap )
+{
+ m_helper = NULL;
+}
+
+wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer,
+ const wxBitmap &bmp) : wxGraphicsObjectRefData( renderer )
+{
+ m_bitmap = NULL;
+ m_helper = NULL;
+
+ Bitmap* image = NULL;
+ if ( bmp.GetMask() )
+ {
+ Bitmap interim((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE()) ;
+
+ size_t width = interim.GetWidth();
+ size_t height = interim.GetHeight();
+ Rect bounds(0,0,width,height);
+
+ image = new Bitmap(width,height,PixelFormat32bppPARGB) ;
+
+ Bitmap interimMask((HBITMAP)bmp.GetMask()->GetMaskBitmap(),NULL);
+ wxASSERT(interimMask.GetPixelFormat() == PixelFormat1bppIndexed);
+
+ BitmapData dataMask ;
+ interimMask.LockBits(&bounds,ImageLockModeRead,
+ interimMask.GetPixelFormat(),&dataMask);
+
+
+ BitmapData imageData ;
+ image->LockBits(&bounds,ImageLockModeWrite, PixelFormat32bppPARGB, &imageData);
+
+ BYTE maskPattern = 0 ;
+ BYTE maskByte = 0;
+ size_t maskIndex ;
+
+ for ( size_t y = 0 ; y < height ; ++y)
+ {
+ maskIndex = 0 ;
+ for( size_t x = 0 ; x < width; ++x)
+ {
+ if ( x % 8 == 0)
+ {
+ maskPattern = 0x80;
+ maskByte = *((BYTE*)dataMask.Scan0 + dataMask.Stride*y + maskIndex);
+ maskIndex++;
+ }
+ else
+ maskPattern = maskPattern >> 1;
+
+ ARGB *dest = (ARGB*)((BYTE*)imageData.Scan0 + imageData.Stride*y + x*4);
+ if ( (maskByte & maskPattern) == 0 )
+ *dest = 0x00000000;
+ else
+ {
+ Color c ;
+ interim.GetPixel(x,y,&c) ;
+ *dest = (c.GetValue() | Color::AlphaMask);
+ }
+ }
+ }
+
+ image->UnlockBits(&imageData);
+
+ interimMask.UnlockBits(&dataMask);
+ interim.UnlockBits(&dataMask);
+ }
+ else
+ {
+ image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
+ if ( bmp.HasAlpha() && GetPixelFormatSize(image->GetPixelFormat()) == 32 )
+ {
+ size_t width = image->GetWidth();
+ size_t height = image->GetHeight();
+ Rect bounds(0,0,width,height);
+ static BitmapData data ;
+
+ m_helper = image ;
+ image = NULL ;
+ m_helper->LockBits(&bounds, ImageLockModeRead,
+ m_helper->GetPixelFormat(),&data);
+
+ image = new Bitmap(data.Width, data.Height, data.Stride,
+ PixelFormat32bppPARGB , (BYTE*) data.Scan0);
+
+ m_helper->UnlockBits(&data);
+ }
+ }
+ if ( image )
+ m_bitmap = image;
+}
+
+wxGDIPlusBitmapData::~wxGDIPlusBitmapData()
+{
+ delete m_bitmap;
+ delete m_helper;
+}
+
+//-----------------------------------------------------------------------------
+// wxGDIPlusPath implementation
+//-----------------------------------------------------------------------------
+
+wxGDIPlusPathData::wxGDIPlusPathData(wxGraphicsRenderer* renderer, GraphicsPath* path ) : wxGraphicsPathData(renderer)
+{
+ if ( path )
+ m_path = path;
+ else
+ m_path = new GraphicsPath();
+}
+
+wxGDIPlusPathData::~wxGDIPlusPathData()
+{
+ delete m_path;
+}
+
+wxGraphicsObjectRefData* wxGDIPlusPathData::Clone() const
+{
+ return new wxGDIPlusPathData( GetRenderer() , m_path->Clone());
+}
+
+//
+// The Primitives
+//
+
+void wxGDIPlusPathData::MoveToPoint( wxDouble x , wxDouble y )
+{
+ m_path->StartFigure();
+ m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
+}
+
+void wxGDIPlusPathData::AddLineToPoint( wxDouble x , wxDouble y )
+{
+ m_path->AddLine((REAL) x,(REAL) y,(REAL) x,(REAL) y);
+}
+
+void wxGDIPlusPathData::CloseSubpath()
+{
+ m_path->CloseFigure();
+}
+
+void wxGDIPlusPathData::AddCurveToPoint( wxDouble cx1, wxDouble cy1, wxDouble cx2, wxDouble cy2, wxDouble x, wxDouble y )
+{
+ PointF c1(cx1,cy1);
+ PointF c2(cx2,cy2);
+ PointF end(x,y);
+ PointF start;
+ m_path->GetLastPoint(&start);
+ m_path->AddBezier(start,c1,c2,end);
+}
+
+// gets the last point of the current path, (0,0) if not yet set
+void wxGDIPlusPathData::GetCurrentPoint( wxDouble* x, wxDouble* y) const
+{
+ PointF start;
+ m_path->GetLastPoint(&start);
+ *x = start.X ;
+ *y = start.Y ;
+}
+
+void wxGDIPlusPathData::AddArc( wxDouble x, wxDouble y, wxDouble r, double startAngle, double endAngle, bool clockwise )
+{
+ double sweepAngle = endAngle - startAngle ;
+ if( fabs(sweepAngle) >= 2*M_PI)
+ {
+ sweepAngle = 2 * M_PI;
+ }
+ else
+ {
+ if ( clockwise )
+ {
+ if( sweepAngle < 0 )
+ sweepAngle += 2 * M_PI;
+ }
+ else
+ {
+ if( sweepAngle > 0 )
+ sweepAngle -= 2 * M_PI;
+
+ }
+ }
+ m_path->AddArc((REAL) (x-r),(REAL) (y-r),(REAL) (2*r),(REAL) (2*r),RadToDeg(startAngle),RadToDeg(sweepAngle));
+}
+
+void wxGDIPlusPathData::AddRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+ m_path->AddRectangle(RectF(x,y,w,h));
+}
+
+void wxGDIPlusPathData::AddPath( const wxGraphicsPathData* path )
+{
+ m_path->AddPath( (GraphicsPath*) path->GetNativePath(), FALSE);
+}
+
+
+// transforms each point of this path by the matrix
+void wxGDIPlusPathData::Transform( const wxGraphicsMatrixData* matrix )
+{
+ m_path->Transform( (Matrix*) matrix->GetNativeMatrix() );
+}
+
+// gets the bounding box enclosing all points (possibly including control points)
+void wxGDIPlusPathData::GetBox(wxDouble *x, wxDouble *y, wxDouble *w, wxDouble *h) const
+{
+ RectF bounds;
+ m_path->GetBounds( &bounds, NULL, NULL) ;
+ *x = bounds.X;
+ *y = bounds.Y;
+ *w = bounds.Width;
+ *h = bounds.Height;
+}
+
+bool wxGDIPlusPathData::Contains( wxDouble x, wxDouble y, wxPolygonFillMode fillStyle ) const
+{
+ m_path->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
+ return m_path->IsVisible( (FLOAT) x,(FLOAT) y) == TRUE ;
+}
+
+//-----------------------------------------------------------------------------
+// wxGDIPlusMatrixData implementation
+//-----------------------------------------------------------------------------
+
+wxGDIPlusMatrixData::wxGDIPlusMatrixData(wxGraphicsRenderer* renderer, Matrix* matrix )
+ : wxGraphicsMatrixData(renderer)
+{
+ if ( matrix )
+ m_matrix = matrix ;
+ else
+ m_matrix = new Matrix();
+}
+
+wxGDIPlusMatrixData::~wxGDIPlusMatrixData()
+{
+ delete m_matrix;
+}
+
+wxGraphicsObjectRefData *wxGDIPlusMatrixData::Clone() const
+{
+ return new wxGDIPlusMatrixData( GetRenderer(), m_matrix->Clone());
+}
+
+// concatenates the matrix
+void wxGDIPlusMatrixData::Concat( const wxGraphicsMatrixData *t )
+{
+ m_matrix->Multiply( (Matrix*) t->GetNativeMatrix());
+}
+
+// sets the matrix to the respective values
+void wxGDIPlusMatrixData::Set(wxDouble a, wxDouble b, wxDouble c, wxDouble d,
+ wxDouble tx, wxDouble ty)
+{
+ m_matrix->SetElements(a,b,c,d,tx,ty);
+}
+
+// gets the component valuess of the matrix
+void wxGDIPlusMatrixData::Get(wxDouble* a, wxDouble* b, wxDouble* c,
+ wxDouble* d, wxDouble* tx, wxDouble* ty) const
+{
+ REAL elements[6];
+ m_matrix->GetElements(elements);
+ if (a) *a = elements[0];
+ if (b) *b = elements[1];
+ if (c) *c = elements[2];
+ if (d) *d = elements[3];
+ if (tx) *tx= elements[4];
+ if (ty) *ty= elements[5];
+}
+
+// makes this the inverse matrix
+void wxGDIPlusMatrixData::Invert()
+{
+ m_matrix->Invert();
+}
+
+// returns true if the elements of the transformation matrix are equal ?
+bool wxGDIPlusMatrixData::IsEqual( const wxGraphicsMatrixData* t) const
+{
+ return m_matrix->Equals((Matrix*) t->GetNativeMatrix())== TRUE ;
+}
+
+// return true if this is the identity matrix
+bool wxGDIPlusMatrixData::IsIdentity() const
+{
+ return m_matrix->IsIdentity() == TRUE ;
+}
+
+//
+// transformation
+//
+
+// add the translation to this matrix
+void wxGDIPlusMatrixData::Translate( wxDouble dx , wxDouble dy )
+{
+ m_matrix->Translate(dx,dy);
+}
+
+// add the scale to this matrix
+void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale )
+{
+ m_matrix->Scale(xScale,yScale);
+}
+
+// add the rotation to this matrix (radians)
+void wxGDIPlusMatrixData::Rotate( wxDouble angle )
+{
+ m_matrix->Rotate( angle );
+}
+
+//
+// apply the transforms
+//
+
+// applies that matrix to the point
+void wxGDIPlusMatrixData::TransformPoint( wxDouble *x, wxDouble *y ) const
+{
+ PointF pt(*x,*y);
+ m_matrix->TransformPoints(&pt);
+ *x = pt.X;
+ *y = pt.Y;
+}
+
+// applies the matrix except for translations
+void wxGDIPlusMatrixData::TransformDistance( wxDouble *dx, wxDouble *dy ) const
+{
+ PointF pt(*dx,*dy);
+ m_matrix->TransformVectors(&pt);
+ *dx = pt.X;
+ *dy = pt.Y;
+}
+
+// returns the native representation
+void * wxGDIPlusMatrixData::GetNativeMatrix() const
+{
+ return m_matrix;
+}
+
+//-----------------------------------------------------------------------------
+// wxGDIPlusContext implementation
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusContext,wxGraphicsContext)
+IMPLEMENT_DYNAMIC_CLASS(wxGDIPlusMeasuringContext,wxGDIPlusContext)
+
+class wxGDIPlusOffsetHelper
+{
+public :
+ wxGDIPlusOffsetHelper( Graphics* gr , bool offset )
+ {
+ m_gr = gr;
+ m_offset = offset;
+ if ( m_offset )
+ m_gr->TranslateTransform( 0.5, 0.5 );
+ }
+ ~wxGDIPlusOffsetHelper( )
+ {
+ if ( m_offset )
+ m_gr->TranslateTransform( -0.5, -0.5 );
+ }
+public :
+ Graphics* m_gr;
+ bool m_offset;
+} ;
+
+wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HDC hdc, wxDouble width, wxDouble height )
+ : wxGraphicsContext(renderer)
+{
+ Init();
+ m_context = new Graphics( hdc);
+ m_width = width;
+ m_height = height;
+ SetDefaults();
+}
+
+wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, HWND hwnd )
+ : wxGraphicsContext(renderer)
+{
+ Init();
+ m_context = new Graphics( hwnd);
+ RECT rect = wxGetWindowRect(hwnd);
+ m_width = rect.right - rect.left;
+ m_height = rect.bottom - rect.top;
+ SetDefaults();
+}
+
+wxGDIPlusContext::wxGDIPlusContext( wxGraphicsRenderer* renderer, Graphics* gr )
+ : wxGraphicsContext(renderer)
+{
+ Init();
+ m_context = gr;
+ SetDefaults();
+}
+
+wxGDIPlusContext::wxGDIPlusContext() : wxGraphicsContext(NULL)
+{
+ Init();
+}
+
+void wxGDIPlusContext::Init()
+{
+ m_context = NULL;
+ m_state1 = 0;
+ m_state2= 0;
+ m_height = 0;
+ m_width = 0;
+}
+
+void wxGDIPlusContext::SetDefaults()
+{
+ m_context->SetTextRenderingHint(TextRenderingHintSystemDefault);
+ m_context->SetPixelOffsetMode(PixelOffsetModeHalf);
+ m_context->SetSmoothingMode(SmoothingModeHighQuality);
+ m_state1 = m_context->Save();
+ m_state2 = m_context->Save();
+}
+
+wxGDIPlusContext::~wxGDIPlusContext()
+{
+ if ( m_context )
+ {
+ m_context->Restore( m_state2 );
+ m_context->Restore( m_state1 );
+ delete m_context;
+ }
+}
+
+
+void wxGDIPlusContext::Clip( const wxRegion ®ion )
+{
+ Region rgn((HRGN)region.GetHRGN());
+ m_context->SetClip(&rgn,CombineModeIntersect);
+}
+
+void wxGDIPlusContext::Clip( wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+ m_context->SetClip(RectF(x,y,w,h),CombineModeIntersect);
+}
+
+void wxGDIPlusContext::ResetClip()
+{
+ m_context->ResetClip();
+}
+
+void wxGDIPlusContext::StrokeLines( size_t n, const wxPoint2DDouble *points)
+{
+ if (m_composition == wxCOMPOSITION_DEST)
+ return;
+
+ if ( !m_pen.IsNull() )
+ {
+ wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
+ Point *cpoints = new Point[n];
+ for (size_t i = 0; i < n; i++)
+ {
+ cpoints[i].X = (int)(points[i].m_x );
+ cpoints[i].Y = (int)(points[i].m_y );
+
+ } // for (size_t i = 0; i < n; i++)
+ m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
+ delete[] cpoints;
+ }
+}
+
+void wxGDIPlusContext::DrawLines( size_t n, const wxPoint2DDouble *points, wxPolygonFillMode WXUNUSED(fillStyle) )
+{
+ if (m_composition == wxCOMPOSITION_DEST)
+ return;
+
+ wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
+ Point *cpoints = new Point[n];
+ for (size_t i = 0; i < n; i++)
+ {
+ cpoints[i].X = (int)(points[i].m_x );
+ cpoints[i].Y = (int)(points[i].m_y );
+
+ } // for (int i = 0; i < n; i++)
+ if ( !m_brush.IsNull() )
+ m_context->FillPolygon( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() , cpoints , n ) ;
+ if ( !m_pen.IsNull() )
+ m_context->DrawLines( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , cpoints , n ) ;
+ delete[] cpoints;
+}
+
+void wxGDIPlusContext::StrokePath( const wxGraphicsPath& path )
+{
+ if (m_composition == wxCOMPOSITION_DEST)
+ return;
+
+ if ( !m_pen.IsNull() )
+ {
+ wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
+ m_context->DrawPath( ((wxGDIPlusPenData*)m_pen.GetGraphicsData())->GetGDIPlusPen() , (GraphicsPath*) path.GetNativePath() );
+ }
+}
+
+void wxGDIPlusContext::FillPath( const wxGraphicsPath& path , wxPolygonFillMode fillStyle )
+{
+ if (m_composition == wxCOMPOSITION_DEST)
+ return;
+
+ if ( !m_brush.IsNull() )
+ {
+ wxGDIPlusOffsetHelper helper( m_context , ShouldOffset() );
+ ((GraphicsPath*) path.GetNativePath())->SetFillMode( fillStyle == wxODDEVEN_RULE ? FillModeAlternate : FillModeWinding);
+ m_context->FillPath( ((wxGDIPlusBrushData*)m_brush.GetRefData())->GetGDIPlusBrush() ,
+ (GraphicsPath*) path.GetNativePath());
+ }
+}
+
+bool wxGDIPlusContext::SetAntialiasMode(wxAntialiasMode antialias)
+{
+ if (m_antialias == antialias)
+ return true;
+
+ m_antialias = antialias;
+
+ SmoothingMode antialiasMode;
+ switch (antialias)
+ {
+ case wxANTIALIAS_DEFAULT:
+ antialiasMode = SmoothingModeHighQuality;
+ break;
+ case wxANTIALIAS_NONE:
+ antialiasMode = SmoothingModeNone;
+ break;
+ default:
+ return false;
+ }
+ m_context->SetSmoothingMode(antialiasMode);
+ return true;
+}
+
+bool wxGDIPlusContext::SetCompositionMode(wxCompositionMode op)
+{
+ if ( m_composition == op )
+ return true;
+
+ m_composition = op;
+
+ if (m_composition == wxCOMPOSITION_DEST)
+ return true;
+
+ CompositingMode cop;
+ switch (op)
+ {
+ case wxCOMPOSITION_SOURCE:
+ cop = CompositingModeSourceCopy;
+ break;
+ case wxCOMPOSITION_OVER:
+ cop = CompositingModeSourceOver;
+ break;
+ default:
+ return false;
+ }
+
+ m_context->SetCompositingMode(cop);
+ return true;
+}
+
+void wxGDIPlusContext::BeginLayer(wxDouble /* opacity */)
+{
+ // TODO
+}
+
+void wxGDIPlusContext::EndLayer()
+{
+ // TODO
+}
+
+void wxGDIPlusContext::Rotate( wxDouble angle )
+{
+ m_context->RotateTransform( RadToDeg(angle) );
+}
+
+void wxGDIPlusContext::Translate( wxDouble dx , wxDouble dy )
+{
+ m_context->TranslateTransform( dx , dy );
+}
+
+void wxGDIPlusContext::Scale( wxDouble xScale , wxDouble yScale )
+{
+ m_context->ScaleTransform(xScale,yScale);
+}
+
+void wxGDIPlusContext::PushState()
+{
+ GraphicsState state = m_context->Save();
+ m_stateStack.push(state);
+}
+
+void wxGDIPlusContext::PopState()
+{
+ GraphicsState state = m_stateStack.top();
+ m_stateStack.pop();
+ m_context->Restore(state);
+}
+
+void wxGDIPlusContext::DrawBitmap( const wxGraphicsBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+ if (m_composition == wxCOMPOSITION_DEST)
+ return;
+
+ Bitmap* image = static_cast<wxGDIPlusBitmapData*>(bmp.GetRefData())->GetGDIPlusBitmap();
+ if ( image )
+ {
+ if( image->GetWidth() != (UINT) w || image->GetHeight() != (UINT) h )
+ {
+ Rect drawRect((REAL) x, (REAL)y, (REAL)w, (REAL)h);
+ m_context->SetPixelOffsetMode( PixelOffsetModeNone );
+ m_context->DrawImage(image, drawRect, 0 , 0 , image->GetWidth()-1, image->GetHeight()-1, UnitPixel ) ;
+ m_context->SetPixelOffsetMode( PixelOffsetModeHalf );
+ }
+ else
+ m_context->DrawImage(image,(REAL) x,(REAL) y,(REAL) w,(REAL) h) ;
+ }
+}
+
+void wxGDIPlusContext::DrawBitmap( const wxBitmap &bmp, wxDouble x, wxDouble y, wxDouble w, wxDouble h )
+{
+ wxGraphicsBitmap bitmap = GetRenderer()->CreateBitmap(bmp);
+ DrawBitmap(bitmap, x, y, w, h);