/layout/svg/base/src/nsSVGCircleFrame.cpp

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ----- BEGIN LICENSE BLOCK -----
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License
00006  * Version 1.1 (the "License"); you may not use this file except in
00007  * compliance with the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla SVG project.
00016  *
00017  * The Initial Developer of the Original Code is 
00018  * Crocodile Clips Ltd..
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *    Alex Fritze <alex.fritze@crocodile-clips.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or 
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the NPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ----- END LICENSE BLOCK ----- */
00038 
00039 #include "nsSVGPathGeometryFrame.h"
00040 #include "nsIDOMSVGAnimatedLength.h"
00041 #include "nsIDOMSVGLength.h"
00042 #include "nsIDOMSVGPoint.h"
00043 #include "nsIDOMSVGCircleElement.h"
00044 #include "nsIDOMSVGElement.h"
00045 #include "nsIDOMSVGSVGElement.h"
00046 //#include "nsASVGPathBuilder.h"
00047 #include "nsISVGRendererPathBuilder.h"
00048 
00049 class nsSVGCircleFrame : public nsSVGPathGeometryFrame
00050 {
00051 protected:
00052   friend nsresult
00053   NS_NewSVGCircleFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame** aNewFrame);
00054 
00055   virtual ~nsSVGCircleFrame();
00056   virtual nsresult Init();
00057 
00058 public:
00059   // nsISVGValueObserver interface:
00060   NS_IMETHOD DidModifySVGObservable(nsISVGValue* observable);
00061 
00062   // nsISVGPathGeometrySource interface:
00063   NS_IMETHOD ConstructPath(nsISVGRendererPathBuilder *pathBuilder);
00064 
00065 private:
00066   nsCOMPtr<nsIDOMSVGLength> mCx;
00067   nsCOMPtr<nsIDOMSVGLength> mCy;
00068   nsCOMPtr<nsIDOMSVGLength> mR;
00069 };
00070 
00071 //----------------------------------------------------------------------
00072 // Implementation
00073 
00074 nsresult
00075 NS_NewSVGCircleFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame** aNewFrame)
00076 {
00077   *aNewFrame = nsnull;
00078   
00079   nsCOMPtr<nsIDOMSVGCircleElement> circle = do_QueryInterface(aContent);
00080   if (!circle) {
00081 #ifdef DEBUG
00082     printf("warning: trying to construct an SVGCircleFrame for a content element that doesn't support the right interfaces\n");
00083 #endif
00084     return NS_ERROR_FAILURE;
00085   }
00086   
00087   nsSVGCircleFrame* it = new (aPresShell) nsSVGCircleFrame;
00088   if (nsnull == it)
00089     return NS_ERROR_OUT_OF_MEMORY;
00090 
00091   *aNewFrame = it;
00092   return NS_OK;
00093 }
00094 
00095 nsSVGCircleFrame::~nsSVGCircleFrame()
00096 {
00097   nsCOMPtr<nsISVGValue> value;
00098   if (mCx && (value = do_QueryInterface(mCx)))
00099       value->RemoveObserver(this);
00100   if (mCy && (value = do_QueryInterface(mCy)))
00101       value->RemoveObserver(this);
00102   if (mR && (value = do_QueryInterface(mR)))
00103       value->RemoveObserver(this);
00104 }
00105 
00106 nsresult nsSVGCircleFrame::Init()
00107 {
00108   nsresult rv = nsSVGPathGeometryFrame::Init();
00109   if (NS_FAILED(rv)) return rv;
00110 
00111   
00112   nsCOMPtr<nsIDOMSVGCircleElement> circle = do_QueryInterface(mContent);
00113   NS_ASSERTION(circle,"wrong content element");
00114 
00115   {
00116     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00117     circle->GetCx(getter_AddRefs(length));
00118     length->GetBaseVal(getter_AddRefs(mCx));
00119     NS_ASSERTION(mCx, "no cx");
00120     if (!mCx) return NS_ERROR_FAILURE;
00121     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mCx);
00122     if (value)
00123       value->AddObserver(this);
00124   }
00125 
00126   {
00127     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00128     circle->GetCy(getter_AddRefs(length));
00129     length->GetBaseVal(getter_AddRefs(mCy));
00130     NS_ASSERTION(mCx, "no cy");
00131     if (!mCx) return NS_ERROR_FAILURE;
00132     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mCy);
00133     if (value)
00134       value->AddObserver(this);
00135   }
00136 
00137   {
00138     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00139     circle->GetR(getter_AddRefs(length));
00140     length->GetBaseVal(getter_AddRefs(mR));
00141     NS_ASSERTION(mCx, "no r");
00142     if (!mCx) return NS_ERROR_FAILURE;
00143     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mR);
00144     if (value)
00145       value->AddObserver(this);
00146   }
00147   
00148   return NS_OK;
00149 }  
00150 
00151 //----------------------------------------------------------------------
00152 // nsISVGValueObserver methods:
00153 
00154 NS_IMETHODIMP
00155 nsSVGCircleFrame::DidModifySVGObservable(nsISVGValue* observable)
00156 {
00157   nsCOMPtr<nsIDOMSVGLength> l = do_QueryInterface(observable);
00158   if (l && (mCx==l || mCy==l || mR==l)) {
00159     UpdateGraphic(nsISVGPathGeometrySource::UPDATEMASK_PATH);
00160     return NS_OK;
00161   }
00162   // else
00163   return nsSVGPathGeometryFrame::DidModifySVGObservable(observable);
00164 }
00165 
00166 
00167 //----------------------------------------------------------------------
00168 // nsISVGPathGeometrySource methods:
00169 
00170 /* void constructPath (in nsISVGRendererPathBuilder pathBuilder); */
00171 NS_IMETHODIMP nsSVGCircleFrame::ConstructPath(nsISVGRendererPathBuilder* pathBuilder)
00172 {
00173   float x,y,r;
00174 
00175   mCx->GetValue(&x);
00176   mCy->GetValue(&y);
00177   mR->GetValue(&r);
00178 
00179   // we should be able to build this as a single arc with startpoints
00180   // and endpoints infinitesimally separated. Let's use two arcs
00181   // though, so that the builder doesn't get confused:
00182   pathBuilder->Moveto(x, y-r);
00183   pathBuilder->Arcto(x-r, y  , r, r, 0.0, 0, 0);
00184   pathBuilder->Arcto(x  , y-r, r, r, 0.0, 1, 0);
00185   pathBuilder->ClosePath(&x,&y);
00186   
00187   return NS_OK;
00188 }

Generated on Wed Sep 10 22:25:23 2003 for Mozilla SVG Project Rendering Backend by doxygen1.3