Back to index

lightning-sunbird  0.9+nobinonly
nsSVGAnimatedNumber.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> (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 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 "nsSVGAnimatedNumber.h"
00040 #include "nsTextFormatter.h"
00041 #include "prdtoa.h"
00042 #include "nsSVGValue.h"
00043 #include "nsISVGValueUtils.h"
00044 #include "nsDOMError.h"
00045 #include "nsContentUtils.h"
00046 
00047 
00049 // nsSVGAnimatedNumber
00050 
00051 class nsSVGAnimatedNumber : public nsIDOMSVGAnimatedNumber,
00052                             public nsSVGValue
00053 {
00054 protected:
00055   friend nsresult NS_NewSVGAnimatedNumber(nsIDOMSVGAnimatedNumber** result,
00056                                           float aBaseVal);
00057   nsSVGAnimatedNumber();
00058   ~nsSVGAnimatedNumber();
00059   void Init(float aBaseVal);
00060   
00061 public:
00062   // nsISupports interface:
00063   NS_DECL_ISUPPORTS
00064 
00065   // nsIDOMSVGAnimatedNumber interface:
00066   NS_DECL_NSIDOMSVGANIMATEDNUMBER
00067 
00068   // remainder of nsISVGValue interface:
00069   NS_IMETHOD SetValueString(const nsAString& aValue);
00070   NS_IMETHOD GetValueString(nsAString& aValue);
00071 
00072 protected:
00073   float mBaseVal;
00074 };
00075 
00076 
00077 
00078 //----------------------------------------------------------------------
00079 // Implementation
00080 
00081 nsSVGAnimatedNumber::nsSVGAnimatedNumber()
00082 {
00083 }
00084 
00085 nsSVGAnimatedNumber::~nsSVGAnimatedNumber()
00086 {
00087 }
00088 
00089 void
00090 nsSVGAnimatedNumber::Init(float aBaseVal)
00091 {
00092   mBaseVal = aBaseVal;
00093 }
00094 
00095 //----------------------------------------------------------------------
00096 // nsISupports methods:
00097 
00098 NS_IMPL_ADDREF(nsSVGAnimatedNumber)
00099 NS_IMPL_RELEASE(nsSVGAnimatedNumber)
00100 
00101 
00102 NS_INTERFACE_MAP_BEGIN(nsSVGAnimatedNumber)
00103   NS_INTERFACE_MAP_ENTRY(nsISVGValue)
00104   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGAnimatedNumber)
00105   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(SVGAnimatedNumber)
00106   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISVGValue)
00107 NS_INTERFACE_MAP_END
00108   
00109 //----------------------------------------------------------------------
00110 // nsISVGValue methods:
00111 
00112 NS_IMETHODIMP
00113 nsSVGAnimatedNumber::SetValueString(const nsAString& aValue)
00114 {
00115   nsresult rv = NS_OK;
00116   WillModify();
00117   
00118   char *str = ToNewCString(aValue);
00119 
00120   if (*str) {
00121     char *rest;
00122     double value = PR_strtod(str, &rest);
00123     if (rest && rest!=str) {
00124       if (*rest=='%') {
00125         mBaseVal = (float)(value/100.0);
00126         rest++;
00127       } else {
00128         mBaseVal = (float)(value);
00129       }
00130       // skip trailing spaces
00131       while (*rest && isspace(*rest))
00132         ++rest;
00133 
00134       // check to see if there is trailing stuff...
00135       if (*rest != '\0') {
00136         rv = NS_ERROR_FAILURE;
00137         NS_ERROR("trailing data in number value");
00138       }
00139     } else {
00140       rv = NS_ERROR_FAILURE;
00141       // no number
00142     }
00143   }
00144   nsMemory::Free(str);
00145   DidModify();
00146   return rv;
00147 }
00148 
00149 NS_IMETHODIMP
00150 nsSVGAnimatedNumber::GetValueString(nsAString& aValue)
00151 {
00152   aValue.Truncate();
00153 
00154   PRUnichar buf[24];
00155   nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(PRUnichar),
00156                             NS_LITERAL_STRING("%g").get(),
00157                             (double)mBaseVal);
00158   aValue.Append(buf);
00159   
00160   return NS_OK;
00161 }
00162 
00163 //----------------------------------------------------------------------
00164 // nsIDOMSVGAnimatedNumber methods:
00165 
00166 /* attribute nsIDOMSVGNumber baseVal; */
00167 NS_IMETHODIMP
00168 nsSVGAnimatedNumber::GetBaseVal(float *aBaseVal)
00169 {
00170   *aBaseVal = mBaseVal;
00171   return NS_OK;
00172 }
00173 
00174 /* attribute nsIDOMSVGNumber baseVal; */
00175 NS_IMETHODIMP
00176 nsSVGAnimatedNumber::SetBaseVal(float aBaseVal)
00177 {
00178   WillModify();
00179   mBaseVal = aBaseVal;
00180   DidModify();
00181   return NS_OK;
00182 }
00183 
00184 /* readonly attribute nsIDOMSVGNumber animVal; */
00185 NS_IMETHODIMP
00186 nsSVGAnimatedNumber::GetAnimVal(float *aAnimVal)
00187 {
00188   *aAnimVal = mBaseVal;
00189   return NS_OK;
00190 }
00191 
00193 // Exported creation functions
00194 
00195 nsresult
00196 NS_NewSVGAnimatedNumber(nsIDOMSVGAnimatedNumber** aResult,
00197                         float aBaseVal)
00198 {
00199   *aResult = nsnull;
00200   
00201   nsSVGAnimatedNumber* animatedNumber = new nsSVGAnimatedNumber();
00202   if (!animatedNumber) return NS_ERROR_OUT_OF_MEMORY;
00203   NS_ADDREF(animatedNumber);
00204 
00205   animatedNumber->Init(aBaseVal);
00206   
00207   *aResult = (nsIDOMSVGAnimatedNumber*) animatedNumber;
00208   
00209   return NS_OK;
00210 }
00211 
00212