Back to index

lightning-sunbird  0.9+nobinonly
nsSVGEnum.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  * IBM Corporation
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Tim Rowley <tor@cs.brown.edu> (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 "nsSVGEnum.h"
00040 #include "nsSVGValue.h"
00041 #include "nsISVGValueUtils.h"
00042 #include "nsWeakReference.h"
00043 #include "nsIAtom.h"
00044 
00046 // nsSVGEnum class
00047 
00048 class nsSVGEnum : public nsISVGEnum,
00049                   public nsSVGValue
00050 {
00051 protected:
00052   friend nsresult NS_NewSVGEnum(nsISVGEnum** result,
00053                                 PRUint16 value,
00054                                 nsSVGEnumMapping *mapping);
00055     
00056   friend nsresult NS_NewSVGEnum(nsISVGEnum** result,
00057                                 const nsAString &value,
00058                                 nsSVGEnumMapping *mapping);
00059   
00060   nsSVGEnum(PRUint16 value, nsSVGEnumMapping *mapping);
00061   nsSVGEnum(nsSVGEnumMapping *mapping);
00062   virtual ~nsSVGEnum();
00063 
00064 public:
00065   // nsISupports interface:
00066   NS_DECL_ISUPPORTS
00067 
00068   // nsISVGEnum interface:
00069   NS_IMETHOD GetIntegerValue(PRUint16 &value);
00070   NS_IMETHOD SetIntegerValue(PRUint16 value);
00071   
00072   // nsISVGValue interface:
00073   NS_IMETHOD SetValueString(const nsAString& aValue);
00074   NS_IMETHOD GetValueString(nsAString& aValue);
00075   
00076 #ifdef DEBUG_scooter
00077   void Print_mapping();
00078 #endif
00079   
00080 protected:
00081   PRUint16 mValue;
00082   nsSVGEnumMapping *mMapping;
00083 };
00084 
00085 
00086 //----------------------------------------------------------------------
00087 // Implementation
00088 
00089 #ifdef DEBUG_scooter
00090 void nsSVGEnum::Print_mapping()
00091 {
00092   nsSVGEnumMapping *tmp = mMapping;
00093   nsAutoString aStr;
00094   printf("Print_mapping: mMapping = 0x%x\n", tmp);
00095   while (tmp->key) {
00096     (*tmp->key)->ToString(aStr);
00097     printf ("Print_mapping: %s (%d)\n", NS_ConvertUTF16toUTF8(aStr).get(), tmp->val);
00098     tmp++;
00099   }
00100 }
00101 #endif
00102 
00103 nsresult
00104 NS_NewSVGEnum(nsISVGEnum** result,
00105               PRUint16 value,
00106               nsSVGEnumMapping *mapping)
00107 {
00108   NS_ASSERTION(mapping, "no mapping");
00109   nsSVGEnum *pe = new nsSVGEnum(value, mapping);
00110   if (!pe) return NS_ERROR_OUT_OF_MEMORY;
00111   NS_ADDREF(pe);
00112   *result = pe;
00113   return NS_OK;
00114 }
00115 
00116 nsresult
00117 NS_NewSVGEnum(nsISVGEnum** result,
00118               const nsAString &value,
00119               nsSVGEnumMapping *mapping)
00120 {
00121   NS_ASSERTION(mapping, "no mapping");
00122   *result = nsnull;
00123   nsSVGEnum *pe = new nsSVGEnum(0, mapping);
00124   if (!pe) return NS_ERROR_OUT_OF_MEMORY;
00125   NS_ADDREF(pe);
00126   if (NS_FAILED(pe->SetValueString(value))) {
00127     NS_RELEASE(pe);
00128     return NS_ERROR_FAILURE;
00129   }
00130   *result = pe;
00131   return NS_OK;
00132 }  
00133 
00134 
00135 nsSVGEnum::nsSVGEnum(PRUint16 value,
00136                      nsSVGEnumMapping *mapping)
00137     : mValue(value), mMapping(mapping)
00138 {
00139 }
00140 
00141 nsSVGEnum::~nsSVGEnum()
00142 {
00143 }
00144 
00145 //----------------------------------------------------------------------
00146 // nsISupports methods:
00147 
00148 NS_IMPL_ADDREF(nsSVGEnum)
00149 NS_IMPL_RELEASE(nsSVGEnum)
00150 
00151 NS_INTERFACE_MAP_BEGIN(nsSVGEnum)
00152   NS_INTERFACE_MAP_ENTRY(nsISVGValue)
00153   NS_INTERFACE_MAP_ENTRY(nsISVGEnum)
00154   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISVGValue)
00155 NS_INTERFACE_MAP_END
00156 
00157 //----------------------------------------------------------------------
00158 // nsISVGValue methods:
00159 NS_IMETHODIMP
00160 nsSVGEnum::SetValueString(const nsAString& aValue)
00161 {
00162   nsCOMPtr<nsIAtom> valAtom = do_GetAtom(aValue);
00163 
00164   nsSVGEnumMapping *tmp = mMapping;
00165 
00166   while (tmp->key) {
00167     if (valAtom == *(tmp->key)) {
00168       WillModify();
00169       mValue = tmp->val;
00170       DidModify();
00171       return NS_OK;
00172     }
00173     tmp++;
00174   }
00175 
00176   // only a warning since authors may mistype attribute values
00177   NS_WARNING("unknown enumeration key");
00178   return NS_ERROR_FAILURE;
00179 }
00180 
00181 NS_IMETHODIMP
00182 nsSVGEnum::GetValueString(nsAString& aValue)
00183 {
00184   nsSVGEnumMapping *tmp = mMapping;
00185 
00186   while (tmp->key) {
00187     if (mValue == tmp->val) {
00188       (*tmp->key)->ToString(aValue);
00189       return NS_OK;
00190     }
00191     tmp++;
00192   }
00193   NS_ERROR("unknown enumeration value");
00194   return NS_ERROR_FAILURE;
00195 }
00196 
00197 //----------------------------------------------------------------------
00198 // nsISVGEnum methods:
00199 
00200 NS_IMETHODIMP
00201 nsSVGEnum::GetIntegerValue(PRUint16& aValue)
00202 {
00203   aValue = mValue;
00204   return NS_OK;
00205 }
00206 
00207 NS_IMETHODIMP
00208 nsSVGEnum::SetIntegerValue(PRUint16 aValue)
00209 {
00210   WillModify();
00211   mValue = aValue;
00212   DidModify();
00213   return NS_OK;
00214 }
00215 
00216