Back to index

lightning-sunbird  0.9+nobinonly
nsSVGForeignObjectElement.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 "nsSVGGraphicElement.h"
00040 #include "nsSVGAtoms.h"
00041 #include "nsIDOMSVGForeignObjectElem.h"
00042 #include "nsSVGLength.h"
00043 #include "nsSVGAnimatedLength.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsISVGSVGElement.h"
00046 #include "nsSVGCoordCtxProvider.h"
00047 
00048 typedef nsSVGGraphicElement nsSVGForeignObjectElementBase;
00049 
00050 class nsSVGForeignObjectElement : public nsSVGForeignObjectElementBase,
00051                                   public nsIDOMSVGForeignObjectElement
00052 {
00053 protected:
00054   friend nsresult NS_NewSVGForeignObjectElement(nsIContent **aResult,
00055                                                 nsINodeInfo *aNodeInfo);
00056   nsSVGForeignObjectElement(nsINodeInfo *aNodeInfo);
00057   virtual ~nsSVGForeignObjectElement();
00058   nsresult Init();
00059 
00060 public:
00061   // interfaces:
00062   
00063   NS_DECL_ISUPPORTS_INHERITED
00064   NS_DECL_NSIDOMSVGFOREIGNOBJECTELEMENT
00065 
00066   // xxx I wish we could use virtual inheritance
00067   NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsSVGForeignObjectElementBase::)
00068   NS_FORWARD_NSIDOMELEMENT(nsSVGForeignObjectElementBase::)
00069   NS_FORWARD_NSIDOMSVGELEMENT(nsSVGForeignObjectElementBase::)
00070 
00071   // nsISVGContent specializations:
00072   virtual void ParentChainChanged();
00073 
00074   // nsIContent interface
00075   NS_IMETHODIMP_(PRBool) IsAttributeMapped(const nsIAtom* name) const;
00076 
00077 protected:
00078   
00079   nsCOMPtr<nsIDOMSVGAnimatedLength> mX;
00080   nsCOMPtr<nsIDOMSVGAnimatedLength> mY;
00081   nsCOMPtr<nsIDOMSVGAnimatedLength> mWidth;
00082   nsCOMPtr<nsIDOMSVGAnimatedLength> mHeight;
00083 };
00084 
00085 
00086 NS_IMPL_NS_NEW_SVG_ELEMENT(ForeignObject)
00087 
00088 
00089 //----------------------------------------------------------------------
00090 // nsISupports methods
00091 
00092 NS_IMPL_ADDREF_INHERITED(nsSVGForeignObjectElement,nsSVGForeignObjectElementBase)
00093 NS_IMPL_RELEASE_INHERITED(nsSVGForeignObjectElement,nsSVGForeignObjectElementBase)
00094 
00095 NS_INTERFACE_MAP_BEGIN(nsSVGForeignObjectElement)
00096   NS_INTERFACE_MAP_ENTRY(nsIDOMNode)
00097   NS_INTERFACE_MAP_ENTRY(nsIDOMElement)
00098   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGElement)
00099   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGForeignObjectElement)
00100   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(SVGForeignObjectElement)
00101 NS_INTERFACE_MAP_END_INHERITING(nsSVGForeignObjectElementBase)
00102 
00103 //----------------------------------------------------------------------
00104 // Implementation
00105 
00106 nsSVGForeignObjectElement::nsSVGForeignObjectElement(nsINodeInfo *aNodeInfo)
00107   : nsSVGForeignObjectElementBase(aNodeInfo)
00108 {
00109 
00110 }
00111 
00112 nsSVGForeignObjectElement::~nsSVGForeignObjectElement()
00113 {
00114 }
00115 
00116   
00117 nsresult
00118 nsSVGForeignObjectElement::Init()
00119 {
00120   nsresult rv = nsSVGForeignObjectElementBase::Init();
00121   NS_ENSURE_SUCCESS(rv,rv);
00122 
00123   // Create mapped properties:
00124   
00125 
00126   // DOM property: x ,  #IMPLIED attrib: x
00127   {
00128     nsCOMPtr<nsISVGLength> length;
00129     rv = NS_NewSVGLength(getter_AddRefs(length),
00130                          0.0f);
00131     NS_ENSURE_SUCCESS(rv,rv);
00132     rv = NS_NewSVGAnimatedLength(getter_AddRefs(mX), length);
00133     NS_ENSURE_SUCCESS(rv,rv);
00134     rv = AddMappedSVGValue(nsSVGAtoms::x, mX);
00135     NS_ENSURE_SUCCESS(rv,rv);
00136   }
00137 
00138   // DOM property: y ,  #IMPLIED attrib: y
00139   {
00140     nsCOMPtr<nsISVGLength> length;
00141     rv = NS_NewSVGLength(getter_AddRefs(length),
00142                          0.0f);
00143     NS_ENSURE_SUCCESS(rv,rv);
00144     rv = NS_NewSVGAnimatedLength(getter_AddRefs(mY), length);
00145     NS_ENSURE_SUCCESS(rv,rv);
00146     rv = AddMappedSVGValue(nsSVGAtoms::y, mY);
00147     NS_ENSURE_SUCCESS(rv,rv);
00148   }
00149 
00150   // DOM property: width ,  #REQUIRED  attrib: width
00151   // XXX: enforce requiredness
00152   {
00153     nsCOMPtr<nsISVGLength> length;
00154     rv = NS_NewSVGLength(getter_AddRefs(length),
00155                          100.0f);
00156     NS_ENSURE_SUCCESS(rv,rv);
00157     rv = NS_NewSVGAnimatedLength(getter_AddRefs(mWidth), length);
00158     NS_ENSURE_SUCCESS(rv,rv);
00159     rv = AddMappedSVGValue(nsSVGAtoms::width, mWidth);
00160     NS_ENSURE_SUCCESS(rv,rv);
00161   }
00162 
00163   // DOM property: height ,  #REQUIRED  attrib: height
00164   // XXX: enforce requiredness
00165   {
00166     nsCOMPtr<nsISVGLength> length;
00167     rv = NS_NewSVGLength(getter_AddRefs(length),
00168                          100.0f);
00169     NS_ENSURE_SUCCESS(rv,rv);
00170     rv = NS_NewSVGAnimatedLength(getter_AddRefs(mHeight), length);
00171     NS_ENSURE_SUCCESS(rv,rv);
00172     rv = AddMappedSVGValue(nsSVGAtoms::height, mHeight);
00173     NS_ENSURE_SUCCESS(rv,rv);
00174   }
00175 
00176   return rv;
00177 }
00178 
00179 //----------------------------------------------------------------------
00180 // nsIDOMNode methods
00181 
00182 
00183 NS_IMPL_DOM_CLONENODE_WITH_INIT(nsSVGForeignObjectElement)
00184 
00185 
00186 //----------------------------------------------------------------------
00187 // nsIDOMSVGForeignObjectElement methods:
00188 
00189 /* readonly attribute nsIDOMSVGAnimatedLength x; */
00190 NS_IMETHODIMP nsSVGForeignObjectElement::GetX(nsIDOMSVGAnimatedLength * *aX)
00191 {
00192   *aX = mX;
00193   NS_IF_ADDREF(*aX);
00194   return NS_OK;
00195 }
00196 
00197 /* readonly attribute nsIDOMSVGAnimatedLength y; */
00198 NS_IMETHODIMP nsSVGForeignObjectElement::GetY(nsIDOMSVGAnimatedLength * *aY)
00199 {
00200   *aY = mY;
00201   NS_IF_ADDREF(*aY);
00202   return NS_OK;
00203 }
00204 
00205 /* readonly attribute nsIDOMSVGAnimatedLength width; */
00206 NS_IMETHODIMP nsSVGForeignObjectElement::GetWidth(nsIDOMSVGAnimatedLength * *aWidth)
00207 {
00208   *aWidth = mWidth;
00209   NS_IF_ADDREF(*aWidth);
00210   return NS_OK;
00211 }
00212 
00213 /* readonly attribute nsIDOMSVGAnimatedLength height; */
00214 NS_IMETHODIMP nsSVGForeignObjectElement::GetHeight(nsIDOMSVGAnimatedLength * *aHeight)
00215 {
00216   *aHeight = mHeight;
00217   NS_IF_ADDREF(*aHeight);
00218   return NS_OK;
00219 }
00220 
00221 //----------------------------------------------------------------------
00222 // nsISVGContent methods
00223 
00224 void nsSVGForeignObjectElement::ParentChainChanged()
00225 {
00226   // set new context information on our length-properties:
00227   
00228   nsCOMPtr<nsIDOMSVGSVGElement> dom_elem;
00229   GetOwnerSVGElement(getter_AddRefs(dom_elem));
00230   if (!dom_elem) return;
00231 
00232   nsCOMPtr<nsSVGCoordCtxProvider> ctx = do_QueryInterface(dom_elem);
00233   NS_ASSERTION(ctx, "<svg> element missing interface");
00234 
00235   // x:
00236   {
00237     nsCOMPtr<nsIDOMSVGLength> dom_length;
00238     mX->GetAnimVal(getter_AddRefs(dom_length));
00239     nsCOMPtr<nsISVGLength> length = do_QueryInterface(dom_length);
00240     NS_ASSERTION(length, "svg length missing interface");
00241 
00242     length->SetContext(nsRefPtr<nsSVGCoordCtx>(ctx->GetContextX()));
00243   }
00244 
00245   // y:
00246   {
00247     nsCOMPtr<nsIDOMSVGLength> dom_length;
00248     mY->GetAnimVal(getter_AddRefs(dom_length));
00249     nsCOMPtr<nsISVGLength> length = do_QueryInterface(dom_length);
00250     NS_ASSERTION(length, "svg length missing interface");
00251     
00252     length->SetContext(nsRefPtr<nsSVGCoordCtx>(ctx->GetContextY()));
00253   }
00254 
00255   // width:
00256   {
00257     nsCOMPtr<nsIDOMSVGLength> dom_length;
00258     mWidth->GetAnimVal(getter_AddRefs(dom_length));
00259     nsCOMPtr<nsISVGLength> length = do_QueryInterface(dom_length);
00260     NS_ASSERTION(length, "svg length missing interface");
00261     
00262     length->SetContext(nsRefPtr<nsSVGCoordCtx>(ctx->GetContextX()));
00263   }
00264 
00265   // height:
00266   {
00267     nsCOMPtr<nsIDOMSVGLength> dom_length;
00268     mHeight->GetAnimVal(getter_AddRefs(dom_length));
00269     nsCOMPtr<nsISVGLength> length = do_QueryInterface(dom_length);
00270     NS_ASSERTION(length, "svg length missing interface");
00271     
00272     length->SetContext(nsRefPtr<nsSVGCoordCtx>(ctx->GetContextY()));
00273   }
00274 }  
00275 
00276 //----------------------------------------------------------------------
00277 // nsIContent methods
00278 
00279 NS_IMETHODIMP_(PRBool)
00280 nsSVGForeignObjectElement::IsAttributeMapped(const nsIAtom* name) const
00281 {
00282   static const MappedAttributeEntry* const map[] = {
00283     sViewportsMap,
00284   };
00285   
00286   return FindAttributeDependence(name, map, NS_ARRAY_LENGTH(map)) ||
00287     nsSVGForeignObjectElementBase::IsAttributeMapped(name);
00288 }