Back to index

lightning-sunbird  0.9+nobinonly
nsSVGUseFrame.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 IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "nsIDOMSVGTransformable.h"
00038 #include "nsSVGDefsFrame.h"
00039 #include "nsIDOMSVGAnimatedLength.h"
00040 #include "nsSVGLength.h"
00041 #include "nsSVGGFrame.h"
00042 #include "nsIDOMSVGUseElement.h"
00043 #include "nsISVGValue.h"
00044 #include "nsIAnonymousContentCreator.h"
00045 #include "nsSVGMatrix.h"
00046 #include "nsLayoutAtoms.h"
00047 
00048 typedef nsSVGGFrame nsSVGUseFrameBase;
00049 
00050 class nsSVGUseFrame : public nsSVGUseFrameBase,
00051                       public nsIAnonymousContentCreator
00052 {
00053   friend nsresult
00054   NS_NewSVGUseFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame** aNewFrame);
00055 
00056 protected:
00057 
00058   NS_IMETHOD InitSVG();
00059 
00060    // nsISupports interface:
00061   NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr);
00062 private:
00063   NS_IMETHOD_(nsrefcnt) AddRef() { return NS_OK; }
00064   NS_IMETHOD_(nsrefcnt) Release() { return NS_OK; }  
00065 
00066 public:
00067 
00068   // nsISVGContainerFrame interface:
00069   already_AddRefed<nsIDOMSVGMatrix> GetCanvasTM();
00070 
00076   virtual nsIAtom* GetType() const;
00077 
00078 #ifdef DEBUG
00079   NS_IMETHOD GetFrameName(nsAString& aResult) const
00080   {
00081     return MakeFrameName(NS_LITERAL_STRING("SVGUse"), aResult);
00082   }
00083 #endif
00084 
00085   // nsIAnonymousContentCreator
00086   NS_IMETHOD CreateAnonymousContent(nsPresContext* aPresContext,
00087                                     nsISupportsArray& aAnonymousItems);
00088   NS_IMETHOD CreateFrameFor(nsPresContext *aPresContext,
00089                             nsIContent *aContent,
00090                             nsIFrame **aFrame);
00091 
00092 protected:
00093 
00094   nsCOMPtr<nsIDOMSVGLength> mX;
00095   nsCOMPtr<nsIDOMSVGLength> mY;
00096 };
00097 
00098 //----------------------------------------------------------------------
00099 // Implementation
00100 
00101 nsresult
00102 NS_NewSVGUseFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame** aNewFrame)
00103 {
00104   *aNewFrame = nsnull;
00105   
00106   nsCOMPtr<nsIDOMSVGTransformable> transformable = do_QueryInterface(aContent);
00107   if (!transformable) {
00108 #ifdef DEBUG
00109     printf("warning: trying to construct an SVGUseFrame for a content element that doesn't support the right interfaces\n");
00110 #endif
00111     return NS_ERROR_FAILURE;
00112   }
00113   
00114   nsSVGUseFrame* it = new (aPresShell) nsSVGUseFrame;
00115   if (it == nsnull)
00116     return NS_ERROR_OUT_OF_MEMORY;
00117 
00118   *aNewFrame = it;
00119 
00120   return NS_OK;
00121 }
00122 
00123 NS_IMETHODIMP
00124 nsSVGUseFrame::InitSVG()
00125 {
00126   nsresult rv = nsSVGUseFrameBase::InitSVG();
00127   if (NS_FAILED(rv)) return rv;
00128  
00129   nsCOMPtr<nsIDOMSVGUseElement> UseElement = do_QueryInterface(mContent);
00130 
00131   {
00132     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00133     UseElement->GetX(getter_AddRefs(length));
00134     length->GetAnimVal(getter_AddRefs(mX));
00135     NS_ASSERTION(mX, "no x");
00136     if (!mX) return NS_ERROR_FAILURE;
00137     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mX);
00138     if (value)
00139       value->AddObserver(this);  // nsISVGValueObserver
00140   }
00141 
00142   {
00143     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00144     UseElement->GetY(getter_AddRefs(length));
00145     length->GetAnimVal(getter_AddRefs(mY));
00146     NS_ASSERTION(mY, "no y");
00147     if (!mY) return NS_ERROR_FAILURE;
00148     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mY);
00149     if (value)
00150       value->AddObserver(this);
00151   }
00152 
00153   return NS_OK;
00154 }
00155 
00156 nsIAtom *
00157 nsSVGUseFrame::GetType() const
00158 {
00159   return nsLayoutAtoms::svgUseFrame;
00160 }
00161 
00162 //----------------------------------------------------------------------
00163 // nsISupports methods
00164 
00165 NS_INTERFACE_MAP_BEGIN(nsSVGUseFrame)
00166   NS_INTERFACE_MAP_ENTRY(nsIAnonymousContentCreator)
00167 NS_INTERFACE_MAP_END_INHERITING(nsSVGUseFrameBase)
00168 
00169 //----------------------------------------------------------------------
00170 // nsISVGContainerFrame methods:
00171 
00172 already_AddRefed<nsIDOMSVGMatrix>
00173 nsSVGUseFrame::GetCanvasTM()
00174 {
00175   if (!mPropagateTransform) {
00176     nsIDOMSVGMatrix *retval;
00177     NS_NewSVGMatrix(&retval);
00178     return retval;
00179   }
00180 
00181   nsCOMPtr<nsIDOMSVGMatrix> currentTM = nsSVGUseFrameBase::GetCanvasTM();
00182 
00183   // x and y:
00184   float x, y;
00185   mX->GetValue(&x);
00186   mY->GetValue(&y);
00187   nsCOMPtr<nsIDOMSVGMatrix> fini;
00188   currentTM->Translate(x, y, getter_AddRefs(fini));
00189 
00190   nsIDOMSVGMatrix *retval = fini.get();
00191   NS_IF_ADDREF(retval);
00192   return retval;
00193 }
00194 
00195 
00196 //----------------------------------------------------------------------
00197 // nsIAnonymousContentCreator methods:
00198 
00199 NS_IMETHODIMP
00200 nsSVGUseFrame::CreateAnonymousContent(nsPresContext* aPresContext,
00201                                       nsISupportsArray& aAnonymousItems)
00202 {
00203   nsCOMPtr<nsIAnonymousContentCreator> use = do_QueryInterface(mContent);
00204 
00205   if (use)
00206     return use->CreateAnonymousContent(aPresContext, aAnonymousItems);
00207   else
00208     return NS_ERROR_FAILURE;
00209 }
00210 
00211 NS_IMETHODIMP
00212 nsSVGUseFrame::CreateFrameFor(nsPresContext *aPresContext,
00213                               nsIContent *aContent,
00214                               nsIFrame **aFrame)
00215 {
00216   *aFrame = nsnull;
00217   return NS_ERROR_FAILURE;
00218 }