]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/graphics.cpp
Disable deprecation and other warnings in MFC sample.
[wxWidgets.git] / src / msw / graphics.cpp
index f1cab063cd7fb3319775b0ae2612d3596d5bc77b..87c5d8602fa9565856c1747ec42f1d919271c37d 100644 (file)
@@ -40,6 +40,9 @@
 #include "wx/private/graphics.h"
 #include "wx/msw/wrapgdip.h"
 #include "wx/msw/dc.h"
+#if wxUSE_ENH_METAFILE
+    #include "wx/msw/enhmeta.h"
+#endif
 #include "wx/dcgraph.h"
 
 #include "wx/msw/private.h" // needs to be before #include <commdlg.h>
@@ -246,16 +249,24 @@ public:
     wxGDIPlusBrushData( wxGraphicsRenderer* renderer, const wxBrush &brush );
     ~wxGDIPlusBrushData ();
 
-    void CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
-        const wxColour&c1, const wxColour&c2 );
-    void CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
-        const wxColour &oColor, const wxColour &cColor );
+    void CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
+                                   wxDouble x2, wxDouble y2,
+                                   const wxGraphicsGradientStops& stops);
+    void CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
+                                   wxDouble xc, wxDouble yc,
+                                   wxDouble radius,
+                                   const wxGraphicsGradientStops& stops);
+
     virtual Brush* GetGDIPlusBrush() { return m_brush; }
 
 protected:
     virtual void Init();
 
-private :
+private:
+    // common part of Create{Linear,Radial}GradientBrush()
+    template <typename T>
+    void SetGradientStops(T *brush, const wxGraphicsGradientStops& stops);
+
     Brush* m_brush;
     Image* m_brushImage;
     GraphicsPath* m_brushPath;
@@ -505,7 +516,13 @@ wxGDIPlusPenData::wxGDIPlusPenData( wxGraphicsRenderer* renderer, const wxPen &p
             wxBitmap* bmp = pen.GetStipple();
             if ( bmp && bmp->Ok() )
             {
-                m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
+                m_penImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
+#if wxUSE_PALETTE
+                    (HPALETTE)bmp->GetPalette()->GetHPALETTE()
+#else
+                    NULL
+#endif
+                );
                 m_penBrush = new TextureBrush(m_penImage);
                 m_pen->SetBrush( m_penBrush );
             }
@@ -608,7 +625,13 @@ wxGDIPlusBrushData::wxGDIPlusBrushData( wxGraphicsRenderer* renderer , const wxB
         if ( bmp && bmp->Ok() )
         {
             wxDELETE( m_brushImage );
-            m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),(HPALETTE)bmp->GetPalette()->GetHPALETTE());
+            m_brushImage = Bitmap::FromHBITMAP((HBITMAP)bmp->GetHBITMAP(),
+#if wxUSE_PALETTE
+                (HPALETTE)bmp->GetPalette()->GetHPALETTE()
+#else
+                NULL
+#endif
+            );
             m_brush = new TextureBrush(m_brushImage);
         }
     }
@@ -628,28 +651,67 @@ void wxGDIPlusBrushData::Init()
     m_brushPath= NULL;
 }
 
-void wxGDIPlusBrushData::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, const wxColour&c1, const wxColour&c2)
+template <typename T>
+void
+wxGDIPlusBrushData::SetGradientStops(T *brush,
+        const wxGraphicsGradientStops& stops)
+{
+    const unsigned numStops = stops.GetCount();
+    if ( numStops <= 2 )
+    {
+        // initial and final colours are set during the brush creation, nothing
+        // more to do
+        return;
+    }
+
+    wxVector<Color> colors(numStops);
+    wxVector<REAL> positions(numStops);
+
+    for ( unsigned i = 0; i < numStops; i++ )
+    {
+        wxGraphicsGradientStop stop = stops.Item(i);
+
+        colors[i] = wxColourToColor(stop.GetColour());
+        positions[i] = stop.GetPosition();
+    }
+
+    brush->SetInterpolationColors(&colors[0], &positions[0], numStops);
+}
+
+void
+wxGDIPlusBrushData::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
+                                              wxDouble x2, wxDouble y2,
+                                              const wxGraphicsGradientStops& stops)
 {
-    m_brush = new LinearGradientBrush( PointF( x1,y1) , PointF( x2,y2),
-        Color( c1.Alpha(), c1.Red(),c1.Green() , c1.Blue() ),
-        Color( c2.Alpha(), c2.Red(),c2.Green() , c2.Blue() ));
+    LinearGradientBrush * const
+        brush = new LinearGradientBrush(PointF(x1, y1) , PointF(x2, y2),
+                                        wxColourToColor(stops.GetStartColour()),
+                                        wxColourToColor(stops.GetEndColour()));
+    m_brush =  brush;
+
+    SetGradientStops(brush, stops);
 }
 
-void wxGDIPlusBrushData::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
-                                               const wxColour &oColor, const wxColour &cColor)
+void
+wxGDIPlusBrushData::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
+                                              wxDouble xc, wxDouble yc,
+                                              wxDouble radius,
+                                              const wxGraphicsGradientStops& stops)
 {
-    // Create a path that consists of a single circle.
     m_brushPath = new GraphicsPath();
-    m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius), (REAL)(2*radius), (REAL)(2*radius));
+    m_brushPath->AddEllipse( (REAL)(xc-radius), (REAL)(yc-radius),
+                             (REAL)(2*radius), (REAL)(2*radius));
 
-    PathGradientBrush *b = new PathGradientBrush(m_brushPath);
-    m_brush = b;
-    b->SetCenterPoint( PointF(xo,yo));
-    b->SetCenterColor(Color( oColor.Alpha(), oColor.Red(),oColor.Green() , oColor.Blue() ));
+    PathGradientBrush * const brush = new PathGradientBrush(m_brushPath);
+    m_brush = brush;
+    brush->SetCenterPoint(PointF(xo, yo));
+    brush->SetCenterColor(wxColourToColor(stops.GetStartColour()));
 
-    Color colors[] = {Color( cColor.Alpha(), cColor.Red(),cColor.Green() , cColor.Blue() )};
+    const Color col(wxColourToColor(stops.GetEndColour()));
     int count = 1;
-    b->SetSurroundColors(colors, &count);
+    brush->SetSurroundColors(&col, &count);
+
+    SetGradientStops(brush, stops);
 }
 
 //-----------------------------------------------------------------------------
@@ -705,7 +767,13 @@ wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer,
     Bitmap* image = NULL;
     if ( bmp.GetMask() )
     {
-        Bitmap interim((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE()) ;
+        Bitmap interim((HBITMAP)bmp.GetHBITMAP(),
+#if wxUSE_PALETTE
+            (HPALETTE)bmp.GetPalette()->GetHPALETTE()
+#else
+            NULL
+#endif
+        );
 
         size_t width = interim.GetWidth();
         size_t height = interim.GetHeight();
@@ -761,7 +829,13 @@ wxGDIPlusBitmapData::wxGDIPlusBitmapData( wxGraphicsRenderer* renderer,
     }
     else
     {
-        image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),(HPALETTE)bmp.GetPalette()->GetHPALETTE());
+        image = Bitmap::FromHBITMAP((HBITMAP)bmp.GetHBITMAP(),
+#if wxUSE_PALETTE
+            (HPALETTE)bmp.GetPalette()->GetHPALETTE()
+#else
+            NULL
+#endif
+        );
         if ( bmp.HasAlpha() && GetPixelFormatSize(image->GetPixelFormat()) == 32 )
         {
             size_t width = image->GetWidth();
@@ -996,7 +1070,7 @@ void wxGDIPlusMatrixData::Scale( wxDouble xScale , wxDouble yScale )
 // add the rotation to this matrix (radians)
 void wxGDIPlusMatrixData::Rotate( wxDouble angle )
 {
-    m_matrix->Rotate( angle );
+    m_matrix->Rotate( RadToDeg(angle) );
 }
 
 //
@@ -1104,6 +1178,14 @@ void wxGDIPlusContext::SetDefaults()
     m_context->SetSmoothingMode(SmoothingModeHighQuality);
     m_state1 = m_context->Save();
     m_state2 = m_context->Save();
+
+    // Setup page scale, based on DPI ratio.
+    // Antecedent should be 100dpi when the default page unit (UnitDisplay)
+    // is used. Page unit UnitDocument would require 300dpi instead.
+    // Note that calling SetPageScale() does not have effect on non-printing
+    // DCs (that is, any other than wxPrinterDC or wxEnhMetaFileDC).
+    REAL dpiRatio = 100.0 / m_context->GetDpiY();
+    m_context->SetPageScale(dpiRatio);
 }
 
 wxGDIPlusContext::~wxGDIPlusContext()
@@ -1449,6 +1531,8 @@ void wxGDIPlusContext::GetTextExtent( const wxString &str, wxDouble *width, wxDo
         m_context->MeasureString((const wchar_t *) s , wcslen(s) , f, layoutRect, &strFormat, &bounds ) ;
         if ( width )
             *width = bounds.Width;
+        if ( height )
+            *height = bounds.Height;
     }
 }
 
@@ -1572,7 +1656,13 @@ public :
 
     virtual wxGraphicsContext * CreateContext( const wxMemoryDC& dc);
 
+#if wxUSE_PRINTING_ARCHITECTURE
     virtual wxGraphicsContext * CreateContext( const wxPrinterDC& dc);
+#endif
+
+#if wxUSE_ENH_METAFILE
+    virtual wxGraphicsContext * CreateContext( const wxEnhMetaFileDC& dc);
+#endif
 
     virtual wxGraphicsContext * CreateContextFromNativeContext( void * context );
 
@@ -1596,15 +1686,16 @@ public :
 
     virtual wxGraphicsBrush CreateBrush(const wxBrush& brush ) ;
 
-    // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
-    virtual wxGraphicsBrush CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
-        const wxColour&c1, const wxColour&c2) ;
-
-    // sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
-    // with radius r and color cColor
-    virtual wxGraphicsBrush CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
-        const wxColour &oColor, const wxColour &cColor) ;
+    virtual wxGraphicsBrush
+    CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
+                              wxDouble x2, wxDouble y2,
+                              const wxGraphicsGradientStops& stops);
 
+    virtual wxGraphicsBrush
+    CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
+                              wxDouble xc, wxDouble yc,
+                              wxDouble radius,
+                              const wxGraphicsGradientStops& stops);
     // sets the font
     virtual wxGraphicsFont CreateFont( const wxFont &font , const wxColour &col = *wxBLACK ) ;
 
@@ -1613,7 +1704,7 @@ public :
 
     // create a graphics bitmap from a native bitmap
     virtual wxGraphicsBitmap CreateBitmapFromNativeBitmap( void* bitmap );
-    
+
     // create a subimage from a native image representation
     virtual wxGraphicsBitmap CreateSubBitmap( const wxGraphicsBitmap &bitmap, wxDouble x, wxDouble y, wxDouble w, wxDouble h  );
 
@@ -1697,6 +1788,7 @@ wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxWindowDC& dc)
     return new wxGDIPlusContext(this,(HDC) msw->GetHDC(), sz.x, sz.y);
 }
 
+#if wxUSE_PRINTING_ARCHITECTURE
 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxPrinterDC& dc)
 {
     ENSURE_LOADED_OR_RETURN(NULL);
@@ -1704,6 +1796,17 @@ wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxPrinterDC& dc)
     wxSize sz = dc.GetSize();
     return new wxGDIPlusContext(this,(HDC) msw->GetHDC(), sz.x, sz.y);
 }
+#endif
+
+#if wxUSE_ENH_METAFILE
+wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxEnhMetaFileDC& dc)
+{
+    ENSURE_LOADED_OR_RETURN(NULL);
+    wxMSWDCImpl *msw = wxDynamicCast( dc.GetImpl() , wxMSWDCImpl );
+    wxSize sz = dc.GetSize();
+    return new wxGDIPlusContext(this,(HDC) msw->GetHDC(), sz.x, sz.y);
+}
+#endif
 
 wxGraphicsContext * wxGDIPlusRenderer::CreateContext( const wxMemoryDC& dc)
 {
@@ -1789,27 +1892,29 @@ wxGraphicsBrush wxGDIPlusRenderer::CreateBrush(const wxBrush& brush )
     }
 }
 
-// sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
-wxGraphicsBrush wxGDIPlusRenderer::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2,
-                                                                      const wxColour&c1, const wxColour&c2)
+wxGraphicsBrush
+wxGDIPlusRenderer::CreateLinearGradientBrush(wxDouble x1, wxDouble y1,
+                                             wxDouble x2, wxDouble y2,
+                                             const wxGraphicsGradientStops& stops)
 {
     ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
     wxGraphicsBrush p;
     wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
-    d->CreateLinearGradientBrush(x1, y1, x2, y2, c1, c2);
+    d->CreateLinearGradientBrush(x1, y1, x2, y2, stops);
     p.SetRefData(d);
     return p;
  }
 
-// sets the brush to a radial gradient originating at (xo,yc) with color oColor and ends on a circle around (xc,yc)
-// with radius r and color cColor
-wxGraphicsBrush wxGDIPlusRenderer::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
-                                                                      const wxColour &oColor, const wxColour &cColor)
+wxGraphicsBrush
+wxGDIPlusRenderer::CreateRadialGradientBrush(wxDouble xo, wxDouble yo,
+                                             wxDouble xc, wxDouble yc,
+                                             wxDouble radius,
+                                             const wxGraphicsGradientStops& stops)
 {
     ENSURE_LOADED_OR_RETURN(wxNullGraphicsBrush);
     wxGraphicsBrush p;
     wxGDIPlusBrushData* d = new wxGDIPlusBrushData( this );
-    d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
+    d->CreateRadialGradientBrush(xo,yo,xc,yc,radius,stops);
     p.SetRefData(d);
     return p;
 }