]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/graphcmn.cpp
fixed typo in last commit: use YLOG2DEV for y coordinates, not XLOG2DEV
[wxWidgets.git] / src / common / graphcmn.cpp
index 9de68440ef1d6680264f004ad47668c0a058f191..da3504a4b28722ec3c9ee4038a2d5e34b83751e0 100644 (file)
@@ -48,19 +48,84 @@ static inline double DegToRad(double deg)
 
 //-----------------------------------------------------------------------------
 
+//-----------------------------------------------------------------------------
+// wxGraphicsObject
+//-----------------------------------------------------------------------------
+
 IMPLEMENT_DYNAMIC_CLASS(wxGraphicsObject, wxObject)
 
-IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer, wxObject)
+wxGraphicsObjectRefData::wxGraphicsObjectRefData( wxGraphicsRenderer* renderer )
+{
+    m_renderer = renderer;
+}
+wxGraphicsObjectRefData::wxGraphicsObjectRefData( const wxGraphicsObjectRefData* data )
+{
+    m_renderer = data->m_renderer;
+}
+wxGraphicsRenderer* wxGraphicsObjectRefData::GetRenderer() const 
+{ 
+    return m_renderer ; 
+}
 
-IMPLEMENT_ABSTRACT_CLASS(wxGraphicsMatrix, wxGraphicsObject)
+wxGraphicsObjectRefData* wxGraphicsObjectRefData::Clone() const 
+{
+    return new wxGraphicsObjectRefData(this);
+}
 
-IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPath, wxGraphicsObject)
+wxGraphicsObject::wxGraphicsObject() 
+{
+}
+
+wxGraphicsObject::wxGraphicsObject( wxGraphicsRenderer* renderer ) 
+{
+    SetRefData( new wxGraphicsObjectRefData(renderer));
+}
+
+wxGraphicsObject::~wxGraphicsObject() 
+{
+}
+
+bool wxGraphicsObject::IsNull() const 
+{ 
+    return m_refData == NULL; 
+}
+
+wxGraphicsRenderer* wxGraphicsObject::GetRenderer() const 
+{ 
+    return ( IsNull() ? NULL : GetGraphicsData()->GetRenderer() ); 
+}
+
+wxGraphicsObjectRefData* wxGraphicsObject::GetGraphicsData() const 
+{ 
+    return (wxGraphicsObjectRefData*) m_refData; 
+}
+
+wxObjectRefData* wxGraphicsObject::CreateRefData() const
+{
+    wxLogDebug(wxT("A Null Object cannot be changed"));
+    return NULL;
+}
 
-IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPen, wxGraphicsObject)
+wxObjectRefData* wxGraphicsObject::CloneRefData(const wxObjectRefData* data) const
+{
+    const wxGraphicsObjectRefData* ptr = (const wxGraphicsObjectRefData*) data;
+    return ptr->Clone();
+}
 
-IMPLEMENT_ABSTRACT_CLASS(wxGraphicsBrush, wxGraphicsObject)
+//-----------------------------------------------------------------------------
+// pens etc.
+//-----------------------------------------------------------------------------
 
-IMPLEMENT_ABSTRACT_CLASS(wxGraphicsFont, wxGraphicsObject)
+IMPLEMENT_DYNAMIC_CLASS(wxGraphicsPen, wxGraphicsObject)
+IMPLEMENT_DYNAMIC_CLASS(wxGraphicsBrush, wxGraphicsObject)
+IMPLEMENT_DYNAMIC_CLASS(wxGraphicsFont, wxGraphicsObject)
+WXDLLIMPEXP_DATA_CORE(wxGraphicsPen) wxNullGraphicsPen;
+WXDLLIMPEXP_DATA_CORE(wxGraphicsBrush) wxNullGraphicsBrush;
+WXDLLIMPEXP_DATA_CORE(wxGraphicsFont) wxNullGraphicsFont;
+
+IMPLEMENT_ABSTRACT_CLASS(wxGraphicsRenderer, wxObject)
+IMPLEMENT_ABSTRACT_CLASS(wxGraphicsMatrix, wxGraphicsObject)
+IMPLEMENT_ABSTRACT_CLASS(wxGraphicsPath, wxGraphicsObject)
 
 wxPoint2DDouble wxGraphicsPath::GetCurrentPoint()
 {
@@ -130,6 +195,38 @@ void wxGraphicsPath::AddCircle( wxDouble x, wxDouble y, wxDouble r )
     CloseSubpath();
 }
 
+void wxGraphicsPath::AddEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
+{
+    wxDouble rw = w/2;
+    wxDouble rh = h/2;
+    wxDouble xc = x + rw;
+    wxDouble yc = y + rh;
+    wxGraphicsMatrix* m = GetRenderer()->CreateMatrix();
+    m->Translate(xc,yc);
+    m->Scale(rw/rh,1.0);
+    wxGraphicsPath* p = GetRenderer()->CreatePath();
+    p->AddCircle(0,0,rh);
+    p->Transform(m);
+    AddPath(p);
+    delete p;
+    delete m;
+}
+
+void wxGraphicsPath::AddRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
+{
+    if ( radius == 0 )
+        AddRectangle(x,y,w,h);
+    else
+    {
+        MoveToPoint( x + w, y + h / 2);
+        AddArcToPoint(x + w, y + h, x + w / 2, y + h, radius);
+        AddArcToPoint(x, y + h, x, y + h / 2, radius);
+        AddArcToPoint(x, y , x + w / 2, y, radius);
+        AddArcToPoint(x + w, y, x + w, y + h / 2, radius);
+        CloseSubpath();
+    }
+}
+
 // 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)
 void wxGraphicsPath::AddArcToPoint( wxDouble x1, wxDouble y1 , wxDouble x2, wxDouble y2, wxDouble r )
 {   
@@ -176,66 +273,52 @@ IMPLEMENT_ABSTRACT_CLASS(wxGraphicsContext, wxObject)
 
 wxGraphicsContext::wxGraphicsContext(wxGraphicsRenderer* renderer) : wxGraphicsObject(renderer) 
 {
-    m_pen = NULL;
-    m_releasePen = false;
-    m_brush = NULL;
-    m_releaseBrush = false;
-    m_font = NULL;
-    m_releaseFont = false;
 }
 
 wxGraphicsContext::~wxGraphicsContext() 
 {
-    wxASSERT_MSG( m_pen == NULL , wxT("No pen should be selected during destruction") );
-    wxASSERT_MSG( m_brush == NULL , wxT("No pen should be selected during destruction") );
 }
 
 // sets the pen
-void wxGraphicsContext::SetPen( wxGraphicsPen* pen , bool release ) 
+void wxGraphicsContext::SetPen( const wxGraphicsPen& pen ) 
 {
-    if ( m_releasePen )
-        delete m_pen;
     m_pen = pen;
-    m_releasePen = release;
 }
 
 void wxGraphicsContext::SetPen( const wxPen& pen )
 {
     if ( pen.GetStyle() == wxTRANSPARENT )
-        SetPen( NULL );
+        SetPen( wxNullGraphicsPen );
     else
         SetPen( CreatePen( pen ) );
 }
     
 // sets the brush for filling
-void wxGraphicsContext::SetBrush( wxGraphicsBrush* brush , bool release ) 
+void wxGraphicsContext::SetBrush( const wxGraphicsBrush& brush ) 
 {
-    if ( m_releaseBrush )
-        delete m_brush;
     m_brush = brush;
-    m_releaseBrush = release;
 }
 
 void wxGraphicsContext::SetBrush( const wxBrush& brush )
 {
     if ( brush.GetStyle() == wxTRANSPARENT )
-        SetBrush( NULL );
+        SetBrush( wxNullGraphicsBrush );
     else
         SetBrush( CreateBrush( brush ) );
 }
 
 // sets the brush for filling
-void wxGraphicsContext::SetFont( wxGraphicsFont* font , bool release ) 
+void wxGraphicsContext::SetFont( const wxGraphicsFont& font ) 
 {
-    if ( m_releaseFont )
-        delete m_font;
     m_font = font;
-    m_releaseFont = release;
 }
 
 void wxGraphicsContext::SetFont( const wxFont& font, const wxColour& colour )
 {
-    SetFont( CreateFont( font, colour ) );
+    if ( font.Ok() )
+        SetFont( CreateFont( font, colour ) );
+    else
+        SetFont( wxNullGraphicsFont );
 }
 
 void wxGraphicsContext::DrawPath( const wxGraphicsPath *path, int fillStyle )
@@ -273,46 +356,16 @@ void wxGraphicsContext::DrawRectangle( wxDouble x, wxDouble y, wxDouble w, wxDou
 void wxGraphicsContext::DrawEllipse( wxDouble x, wxDouble y, wxDouble w, wxDouble h)
 {
     wxGraphicsPath* path = CreatePath();
-    if ( w == h )
-    {
-        path->AddCircle( x+w/2,y+w/2,w/2);
-        DrawPath(path);
-    }
-    else
-    {
-        PushState();
-        Translate(x+w/2,y+h/2);
-        wxDouble factor = ((wxDouble) w) / h;
-        Scale( factor , 1.0);
-        path->AddCircle(0,0,h/2);
-        DrawPath(path);
-        PopState();
-    }
+    path->AddEllipse(x,y,w,h);
+    DrawPath(path);
     delete path;
 }
 
 void wxGraphicsContext::DrawRoundedRectangle( wxDouble x, wxDouble y, wxDouble w, wxDouble h, wxDouble radius)
 {
     wxGraphicsPath* path = CreatePath();
-    if ( radius == 0)
-    {
-        path->AddRectangle( x , y , w , h );
-        DrawPath( path );
-    }
-    else
-    {
-        PushState();
-        Translate( x , y );
-
-        path->MoveToPoint(w, h / 2);
-        path->AddArcToPoint(w, h, w / 2, h, radius);
-        path->AddArcToPoint(0, h, 0, h / 2, radius);
-        path->AddArcToPoint(0, 0, w / 2, 0, radius);
-        path->AddArcToPoint(w, 0, w, h / 2, radius);
-        path->CloseSubpath();
-        DrawPath( path );
-        PopState();
-    }
+    path->AddRoundedRectangle(x,y,w,h,radius);
+    DrawPath(path);
     delete path;
 }
 
@@ -363,18 +416,18 @@ wxGraphicsPath * wxGraphicsContext::CreatePath()
     return GetRenderer()->CreatePath();
 }
 
-wxGraphicsPen* wxGraphicsContext::CreatePen(const wxPen& pen)
+wxGraphicsPen wxGraphicsContext::CreatePen(const wxPen& pen)
 {
     return GetRenderer()->CreatePen(pen);
 }
 
-wxGraphicsBrush* wxGraphicsContext::CreateBrush(const wxBrush& brush )
+wxGraphicsBrush wxGraphicsContext::CreateBrush(const wxBrush& brush )
 {
     return GetRenderer()->CreateBrush(brush);
 }
 
 // sets the brush to a linear gradient, starting at (x1,y1) with color c1 to (x2,y2) with color c2
-wxGraphicsBrush* wxGraphicsContext::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, 
+wxGraphicsBrush wxGraphicsContext::CreateLinearGradientBrush( wxDouble x1, wxDouble y1, wxDouble x2, wxDouble y2, 
                                                    const wxColour&c1, const wxColour&c2)
 {
     return GetRenderer()->CreateLinearGradientBrush(x1,y1,x2,y2,c1,c2);
@@ -382,14 +435,14 @@ wxGraphicsBrush* wxGraphicsContext::CreateLinearGradientBrush( wxDouble x1, wxDo
 
 // 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* wxGraphicsContext::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
+wxGraphicsBrush wxGraphicsContext::CreateRadialGradientBrush( wxDouble xo, wxDouble yo, wxDouble xc, wxDouble yc, wxDouble radius,
                                                    const wxColour &oColor, const wxColour &cColor)
 {
     return GetRenderer()->CreateRadialGradientBrush(xo,yo,xc,yc,radius,oColor,cColor);
 }
 
 // sets the font
-wxGraphicsFont* wxGraphicsContext::CreateFont( const wxFont &font , const wxColour &col )
+wxGraphicsFont wxGraphicsContext::CreateFont( const wxFont &font , const wxColour &col )
 {
     return GetRenderer()->CreateFont(font,col);
 }