Back to index

lightning-sunbird  0.9+nobinonly
nsROCSSPrimitiveValue.h
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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsROCSSPrimitiveValue_h___
00039 #define nsROCSSPrimitiveValue_h___
00040 
00041 #include "nsIDOMCSSPrimitiveValue.h"
00042 #include "nsString.h"
00043 #include "nsCoord.h"
00044 #include "nsUnitConversion.h"
00045 #include "nsReadableUtils.h"
00046 #include "nsIURI.h"
00047 #include "nsIAtom.h"
00048 
00049 #include "nsCOMPtr.h"
00050 #include "nsDOMError.h"
00051 #include "nsDOMCSSRect.h"
00052 #include "nsDOMCSSRGBColor.h"
00053 
00054 class nsROCSSPrimitiveValue : public nsIDOMCSSPrimitiveValue
00055 {
00056 public:
00057   NS_DECL_ISUPPORTS
00058 
00059   // nsIDOMCSSPrimitiveValue
00060   NS_DECL_NSIDOMCSSPRIMITIVEVALUE
00061 
00062   // nsIDOMCSSValue
00063   NS_DECL_NSIDOMCSSVALUE
00064 
00065   // nsROCSSPrimitiveValue
00066   nsROCSSPrimitiveValue(float aP2T);
00067   virtual ~nsROCSSPrimitiveValue();
00068 
00069   void SetNumber(float aValue)
00070   {
00071     Reset();
00072     mValue.mFloat = aValue;
00073     mType = CSS_NUMBER;
00074   }
00075 
00076   void SetNumber(PRInt32 aValue)
00077   {
00078     Reset();
00079     mValue.mFloat = float(aValue);
00080     mType = CSS_NUMBER;
00081   }
00082 
00083   void SetNumber(PRUint32 aValue)
00084   {
00085     Reset();
00086     mValue.mFloat = float(aValue);
00087     mType = CSS_NUMBER;
00088   }
00089 
00090   void SetPercent(float aValue)
00091   {
00092     Reset();
00093     mValue.mFloat = aValue;
00094     mType = CSS_PERCENTAGE;
00095   }
00096 
00097   void SetTwips(nscoord aValue)
00098   {
00099     Reset();
00100     mValue.mTwips = aValue;
00101     mType = CSS_PX;
00102   }
00103 
00104   void SetTwips(float aValue)
00105   {
00106     Reset();
00107     mValue.mTwips = nscoord(aValue);
00108     mType = CSS_PX;
00109   }
00110 
00111   void SetIdent(nsIAtom* aAtom)
00112   {
00113     NS_PRECONDITION(aAtom, "Don't pass in a null atom");
00114     Reset();
00115     NS_ADDREF(mValue.mAtom = aAtom);
00116     mType = CSS_IDENT;
00117   }
00118 
00119   void SetIdent(const nsACString& aString)
00120   {
00121     Reset();
00122     mValue.mAtom = NS_NewAtom(aString);
00123     if (mValue.mAtom) {
00124       mType = CSS_IDENT;
00125     } else {
00126       // XXXcaa We should probably let the caller know we are out of memory
00127       mType = CSS_UNKNOWN;
00128     }
00129   }
00130 
00131   void SetString(const nsACString& aString)
00132   {
00133     Reset();
00134     mValue.mString = ToNewUnicode(aString);
00135     if (mValue.mString) {
00136       mType = CSS_STRING;
00137     } else {
00138       // XXXcaa We should probably let the caller know we are out of memory
00139       mType = CSS_UNKNOWN;
00140     }
00141   }
00142 
00143   void SetString(const nsAString& aString)
00144   {
00145     Reset();
00146     mValue.mString = ToNewUnicode(aString);
00147     if (mValue.mString) {
00148       mType = CSS_STRING;
00149     } else {
00150       // XXXcaa We should probably let the caller know we are out of memory
00151       mType = CSS_UNKNOWN;
00152     }
00153   }
00154 
00155   void SetURI(nsIURI *aURI)
00156   {
00157     Reset();
00158     mValue.mURI = aURI;
00159     NS_IF_ADDREF(mValue.mURI);
00160     mType = CSS_URI;
00161   }
00162 
00163   void SetColor(nsIDOMRGBColor* aColor)
00164   {
00165     NS_PRECONDITION(aColor, "Null RGBColor being set!");
00166     Reset();
00167     mValue.mColor = aColor;
00168     if (mValue.mColor) {
00169       NS_ADDREF(mValue.mColor);
00170       mType = CSS_RGBCOLOR;
00171     }
00172     else {
00173       mType = CSS_UNKNOWN;
00174     }
00175   }
00176 
00177   void SetRect(nsIDOMRect* aRect)
00178   {
00179     NS_PRECONDITION(aRect, "Null rect being set!");
00180     Reset();
00181     mValue.mRect = aRect;
00182     if (mValue.mRect) {
00183       NS_ADDREF(mValue.mRect);
00184       mType = CSS_RECT;
00185     }
00186     else {
00187       mType = CSS_UNKNOWN;
00188     }
00189   }
00190 
00191   void Reset(void)
00192   {
00193     switch (mType) {
00194       case CSS_IDENT:
00195         NS_ASSERTION(mValue.mAtom, "Null atom should never happen");
00196         NS_RELEASE(mValue.mAtom);
00197         break;
00198       case CSS_STRING:
00199         NS_ASSERTION(mValue.mString, "Null string should never happen");
00200         nsMemory::Free(mValue.mString);
00201         mValue.mString = nsnull;
00202         break;
00203       case CSS_URI:
00204         NS_IF_RELEASE(mValue.mURI);
00205         break;
00206       case CSS_RECT:
00207         NS_ASSERTION(mValue.mRect, "Null Rect should never happen");
00208         NS_RELEASE(mValue.mRect);
00209         break;
00210       case CSS_RGBCOLOR:
00211         NS_ASSERTION(mValue.mColor, "Null RGBColor should never happen");
00212         NS_RELEASE(mValue.mColor);
00213         break;
00214     }
00215   }
00216 
00217 private:
00218   void GetEscapedURI(nsIURI *aURI, PRUnichar **aReturn);
00219 
00220   PRUint16 mType;
00221 
00222   union {
00223     nscoord         mTwips;
00224     float           mFloat;
00225     nsIDOMRGBColor* mColor;
00226     nsIDOMRect*     mRect;
00227     PRUnichar*      mString;
00228     nsIURI*         mURI;
00229     nsIAtom*        mAtom;
00230   } mValue;
00231   
00232   float mT2P;
00233 };
00234 
00235 #endif /* nsROCSSPrimitiveValue_h___ */
00236