Back to index

lightning-sunbird  0.9+nobinonly
nsSVGStopFrame.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  * Scooter Morris.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Scooter Morris <scootermorris@comcast.net>
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 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 "nsWeakReference.h"
00040 #include "nsIDOMSVGStopElement.h"
00041 #include "nsStyleContext.h"
00042 #include "nsContainerFrame.h"
00043 #include "nsLayoutAtoms.h"
00044 #include "nsIDOMSVGStopElement.h"
00045 #include "nsIDOMSVGAnimatedNumber.h"
00046 #include "nsISVGValueObserver.h"
00047 #include "nsISVGValueUtils.h"
00048 #include "nsISVGValue.h"
00049 
00050 // This is a very simple frame whose only purpose is to capture style change
00051 // events and propogate them to the parent.  Most of the heavy lifting is done
00052 // within the nsSVGGradientFrame, which is the parent for this frame
00053 
00054 typedef nsFrame  nsSVGStopFrameBase;
00055 
00056 class nsSVGStopFrame : public nsSVGStopFrameBase,
00057                        public nsISVGValueObserver,
00058                        public nsSupportsWeakReference
00059 {
00060   // nsISupports interface:
00061   NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr);
00062   NS_IMETHOD_(nsrefcnt) AddRef() { return NS_OK; }
00063   NS_IMETHOD_(nsrefcnt) Release() { return NS_OK; }
00064 
00070   virtual nsIAtom* GetType() const;
00071   virtual PRBool IsFrameOfType(PRUint32 aFlags) const;
00072 
00073 #ifdef DEBUG
00074   NS_IMETHOD GetFrameName(nsAString& aResult) const
00075   {
00076     return MakeFrameName(NS_LITERAL_STRING("SVGStop"), aResult);
00077   }
00078 #endif
00079 
00080   NS_IMETHOD Init(nsPresContext*  aPresContext,
00081                   nsIContent*      aContent,
00082                   nsIFrame*        aParent,
00083                   nsStyleContext*  aContext,
00084                   nsIFrame*        aPrevInFlow);
00085 
00086   // nsISVGValueObserver interface:
00087   NS_IMETHOD WillModifySVGObservable(nsISVGValue* observable, 
00088                                      nsISVGValue::modificationType aModType);
00089   NS_IMETHOD DidModifySVGObservable(nsISVGValue* observable, 
00090                                     nsISVGValue::modificationType aModType);
00091 
00092 protected:
00093   friend nsresult NS_NewSVGStopFrame(nsIPresShell* aPresShell, 
00094                                      nsIContent*   aContent, 
00095                                      nsIFrame*     aParentFrame, 
00096                                      nsIFrame**    aNewFrame);
00097   virtual ~nsSVGStopFrame();
00098 
00099 private:
00100   // nsIFrame interface:
00101   NS_IMETHOD DidSetStyleContext(nsPresContext* aPresContext);
00102 
00103   nsCOMPtr<nsIDOMSVGAnimatedNumber> mOffset;
00104 };
00105 
00106 //----------------------------------------------------------------------
00107 // nsISupports methods:
00108 
00109 NS_INTERFACE_MAP_BEGIN(nsSVGStopFrame)
00110   NS_INTERFACE_MAP_ENTRY(nsISVGValueObserver)
00111   NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
00112 NS_INTERFACE_MAP_END_INHERITING(nsSVGStopFrameBase)
00113 
00114 //----------------------------------------------------------------------
00115 // Implementation
00116 
00117 //----------------------------------------------------------------------
00118 // nsIFrame methods:
00119 
00120 NS_IMETHODIMP
00121 nsSVGStopFrame::DidSetStyleContext(nsPresContext* aPresContext)
00122 {
00123 #ifdef DEBUG_scooter
00124   printf("nsSVGStopFrame::DidSetStyleContext\n");
00125 #endif
00126   // Tell our parent
00127   if (mParent)
00128     mParent->DidSetStyleContext(aPresContext);
00129   return NS_OK;
00130 }
00131 
00132 nsIAtom *
00133 nsSVGStopFrame::GetType() const
00134 {
00135   return nsLayoutAtoms::svgStopFrame;
00136 }
00137 
00138 PRBool
00139 nsSVGStopFrame::IsFrameOfType(PRUint32 aFlags) const
00140 {
00141   return !(aFlags & ~nsIFrame::eSVG);
00142 }
00143 
00144 NS_IMETHODIMP
00145 nsSVGStopFrame::Init(nsPresContext*  aPresContext,
00146                      nsIContent*     aContent,
00147                      nsIFrame*       aParent,
00148                      nsStyleContext* aContext,
00149                      nsIFrame*       aPrevInFlow)
00150 {
00151   nsresult rv;
00152   rv = nsSVGStopFrameBase::Init(aPresContext, aContent, aParent,
00153                                 aContext, aPrevInFlow);
00154 
00155   nsCOMPtr<nsIDOMSVGStopElement> stop = do_QueryInterface(mContent);
00156   {
00157     stop->GetOffset(getter_AddRefs(mOffset));
00158     if (!mOffset)
00159       return NS_ERROR_FAILURE;
00160     NS_ADD_SVGVALUE_OBSERVER(mOffset);
00161   }
00162 
00163   return rv;
00164 }
00165 
00166 
00167 //----------------------------------------------------------------------
00168 // nsISVGValueObserver methods:
00169 NS_IMETHODIMP
00170 nsSVGStopFrame::WillModifySVGObservable(nsISVGValue* observable,
00171                                         nsISVGValue::modificationType aModType)
00172 {
00173   // Need to tell our parent gradients that something happened.
00174   // Calling {Begin,End}Update on an nsISVGValue, which
00175   // nsSVGGradientFrame implements, causes its observers (the
00176   // referencing graphics frames) to be notified.
00177   if (mParent) {
00178     nsISVGValue *value;
00179     if (NS_SUCCEEDED(mParent->QueryInterface(NS_GET_IID(nsISVGValue),
00180                                              (void **)&value)))
00181       value->BeginBatchUpdate();
00182   }
00183   return NS_OK;
00184 }
00185                                                                                 
00186 NS_IMETHODIMP
00187 nsSVGStopFrame::DidModifySVGObservable(nsISVGValue* observable, 
00188                                        nsISVGValue::modificationType aModType)
00189 {
00190   // Need to tell our parent gradients that something happened.
00191   // Calling {Begin,End}Update on an nsISVGValue, which
00192   // nsSVGGradientFrame implements, causes its observers (the
00193   // referencing graphics frames) to be notified.
00194   if (mParent) {
00195     nsISVGValue *value;
00196     if (NS_SUCCEEDED(mParent->QueryInterface(NS_GET_IID(nsISVGValue),
00197                                              (void **)&value)))
00198       value->EndBatchUpdate();
00199   }
00200   return NS_OK;
00201 }
00202 
00203 
00204 // -------------------------------------------------------------------------
00205 // Public functions
00206 // -------------------------------------------------------------------------
00207 
00208 nsresult NS_NewSVGStopFrame(nsIPresShell* aPresShell, 
00209                             nsIContent*   aContent, 
00210                             nsIFrame*     aParentFrame, 
00211                             nsIFrame**    aNewFrame)
00212 {
00213   *aNewFrame = nsnull;
00214   
00215 #ifdef DEBUG_scooter
00216   printf("NS_NewSVGStopFrame\n");
00217 #endif
00218 
00219   nsCOMPtr<nsIDOMSVGStopElement> grad = do_QueryInterface(aContent);
00220   NS_ASSERTION(grad, "NS_NewSVGStopFrame -- Content doesn't support nsIDOMSVGStopElement");
00221   if (!grad)
00222     return NS_ERROR_FAILURE;
00223 
00224   nsSVGStopFrame* it = new (aPresShell) nsSVGStopFrame;
00225   if (nsnull == it)
00226     return NS_ERROR_OUT_OF_MEMORY;
00227 
00228   *aNewFrame = it;
00229 
00230   return NS_OK;
00231 }
00232 
00233 nsSVGStopFrame::~nsSVGStopFrame()
00234 {
00235   if (mOffset)
00236     NS_REMOVE_SVGVALUE_OBSERVER(mOffset);
00237 }