Back to index

lightning-sunbird  0.9+nobinonly
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 Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * 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 of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or 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 MPL, 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 #include "nsLayoutAtoms.h"
00049 
00050 class nsSVGCircleFrame : public nsSVGPathGeometryFrame
00051 {
00052 protected:
00053   friend nsresult
00054   NS_NewSVGCircleFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame** aNewFrame);
00055 
00056   virtual ~nsSVGCircleFrame();
00057   NS_IMETHOD InitSVG();
00058 
00059 public:
00065   virtual nsIAtom* GetType() const;
00066 
00067 #ifdef DEBUG
00068   NS_IMETHOD GetFrameName(nsAString& aResult) const
00069   {
00070     return MakeFrameName(NS_LITERAL_STRING("SVGCircle"), aResult);
00071   }
00072 #endif
00073 
00074   // nsISVGValueObserver interface:
00075   NS_IMETHOD DidModifySVGObservable(nsISVGValue* observable,
00076                                     nsISVGValue::modificationType aModType);
00077 
00078   // nsISVGPathGeometrySource interface:
00079   NS_IMETHOD ConstructPath(nsISVGRendererPathBuilder *pathBuilder);
00080 
00081 private:
00082   nsCOMPtr<nsIDOMSVGLength> mCx;
00083   nsCOMPtr<nsIDOMSVGLength> mCy;
00084   nsCOMPtr<nsIDOMSVGLength> mR;
00085 };
00086 
00087 //----------------------------------------------------------------------
00088 // Implementation
00089 
00090 nsresult
00091 NS_NewSVGCircleFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame** aNewFrame)
00092 {
00093   *aNewFrame = nsnull;
00094   
00095   nsCOMPtr<nsIDOMSVGCircleElement> circle = do_QueryInterface(aContent);
00096   if (!circle) {
00097 #ifdef DEBUG
00098     printf("warning: trying to construct an SVGCircleFrame for a content element that doesn't support the right interfaces\n");
00099 #endif
00100     return NS_ERROR_FAILURE;
00101   }
00102   
00103   nsSVGCircleFrame* it = new (aPresShell) nsSVGCircleFrame;
00104   if (nsnull == it)
00105     return NS_ERROR_OUT_OF_MEMORY;
00106 
00107   *aNewFrame = it;
00108   return NS_OK;
00109 }
00110 
00111 nsSVGCircleFrame::~nsSVGCircleFrame()
00112 {
00113   nsCOMPtr<nsISVGValue> value;
00114   if (mCx && (value = do_QueryInterface(mCx)))
00115       value->RemoveObserver(this);
00116   if (mCy && (value = do_QueryInterface(mCy)))
00117       value->RemoveObserver(this);
00118   if (mR && (value = do_QueryInterface(mR)))
00119       value->RemoveObserver(this);
00120 }
00121 
00122 NS_IMETHODIMP
00123 nsSVGCircleFrame::InitSVG()
00124 {
00125   nsresult rv = nsSVGPathGeometryFrame::InitSVG();
00126   if (NS_FAILED(rv)) return rv;
00127 
00128   
00129   nsCOMPtr<nsIDOMSVGCircleElement> circle = do_QueryInterface(mContent);
00130   NS_ASSERTION(circle,"wrong content element");
00131 
00132   {
00133     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00134     circle->GetCx(getter_AddRefs(length));
00135     length->GetAnimVal(getter_AddRefs(mCx));
00136     NS_ASSERTION(mCx, "no cx");
00137     if (!mCx) return NS_ERROR_FAILURE;
00138     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mCx);
00139     if (value)
00140       value->AddObserver(this);
00141   }
00142 
00143   {
00144     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00145     circle->GetCy(getter_AddRefs(length));
00146     length->GetAnimVal(getter_AddRefs(mCy));
00147     NS_ASSERTION(mCx, "no cy");
00148     if (!mCx) return NS_ERROR_FAILURE;
00149     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mCy);
00150     if (value)
00151       value->AddObserver(this);
00152   }
00153 
00154   {
00155     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00156     circle->GetR(getter_AddRefs(length));
00157     length->GetAnimVal(getter_AddRefs(mR));
00158     NS_ASSERTION(mCx, "no r");
00159     if (!mCx) return NS_ERROR_FAILURE;
00160     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mR);
00161     if (value)
00162       value->AddObserver(this);
00163   }
00164   
00165   return NS_OK;
00166 }  
00167 
00168 nsIAtom *
00169 nsSVGCircleFrame::GetType() const
00170 {
00171   return nsLayoutAtoms::svgCircleFrame;
00172 }
00173 
00174 //----------------------------------------------------------------------
00175 // nsISVGValueObserver methods:
00176 
00177 NS_IMETHODIMP
00178 nsSVGCircleFrame::DidModifySVGObservable(nsISVGValue* observable,
00179                                          nsISVGValue::modificationType aModType)
00180 {
00181   nsCOMPtr<nsIDOMSVGLength> l = do_QueryInterface(observable);
00182   if (l && (mCx==l || mCy==l || mR==l)) {
00183     UpdateGraphic(nsISVGPathGeometrySource::UPDATEMASK_PATH);
00184     return NS_OK;
00185   }
00186   // else
00187   return nsSVGPathGeometryFrame::DidModifySVGObservable(observable, aModType);
00188 }
00189 
00190 
00191 //----------------------------------------------------------------------
00192 // nsISVGPathGeometrySource methods:
00193 
00194 /* void constructPath (in nsISVGRendererPathBuilder pathBuilder); */
00195 NS_IMETHODIMP nsSVGCircleFrame::ConstructPath(nsISVGRendererPathBuilder* pathBuilder)
00196 {
00197   float x,y,r;
00198 
00199   mCx->GetValue(&x);
00200   mCy->GetValue(&y);
00201   mR->GetValue(&r);
00202 
00203   // we should be able to build this as a single arc with startpoints
00204   // and endpoints infinitesimally separated. Let's use two arcs
00205   // though, so that the builder doesn't get confused:
00206   pathBuilder->Moveto(x, y-r);
00207   pathBuilder->Arcto(x-r, y  , r, r, 0.0, 0, 0);
00208   pathBuilder->Arcto(x  , y-r, r, r, 0.0, 1, 0);
00209   pathBuilder->ClosePath(&x,&y);
00210   
00211   return NS_OK;
00212 }