Back to index

lightning-sunbird  0.9+nobinonly
nsStyleCoord.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 #ifndef nsStyleCoord_h___
00038 #define nsStyleCoord_h___
00039 
00040 #include "nscore.h"
00041 #include "nsCoord.h"
00042 #include "nsCRT.h"
00043 #include "nsStyleConsts.h"
00044 class nsString;
00045 
00046 enum nsStyleUnit {
00047   eStyleUnit_Null         = 0,      // (no value) value is not specified
00048   eStyleUnit_Normal       = 1,      // (no value)
00049   eStyleUnit_Auto         = 2,      // (no value)
00050   eStyleUnit_Percent      = 10,     // (float) 1.0 == 100%
00051   eStyleUnit_Factor       = 11,     // (float) a multiplier
00052   eStyleUnit_Coord        = 20,     // (nscoord) value is twips
00053   eStyleUnit_Integer      = 30,     // (int) value is simple integer
00054   eStyleUnit_Proportional = 31,     // (int) value has proportional meaning
00055   eStyleUnit_Enumerated   = 32,     // (int) value has enumerated meaning
00056   eStyleUnit_Chars        = 33      // (int) value is number of characters
00057 };
00058 
00059 typedef union {
00060   PRInt32     mInt;   // nscoord is a PRInt32 for now
00061   float       mFloat;
00062 } nsStyleUnion;
00063 
00072 class nsStyleCoord {
00073 public:
00074   nsStyleCoord(nsStyleUnit aUnit = eStyleUnit_Null);
00075   nsStyleCoord(nscoord aValue);
00076   nsStyleCoord(PRInt32 aValue, nsStyleUnit aUnit);
00077   nsStyleCoord(float aValue, nsStyleUnit aUnit);
00078   nsStyleCoord(const nsStyleCoord& aCopy);
00079 
00080   nsStyleCoord&  operator=(const nsStyleCoord& aCopy);
00081   PRBool         operator==(const nsStyleCoord& aOther) const;
00082   PRBool         operator!=(const nsStyleCoord& aOther) const;
00083 
00084   nsStyleUnit GetUnit(void) const { return mUnit; }
00085   nscoord     GetCoordValue(void) const;
00086   PRInt32     GetIntValue(void) const;
00087   float       GetPercentValue(void) const;
00088   float       GetFactorValue(void) const;
00089   void        GetUnionValue(nsStyleUnion& aValue) const;
00090 
00091   void  Reset(void);  // sets to null
00092   void  SetCoordValue(nscoord aValue);
00093   void  SetIntValue(PRInt32 aValue, nsStyleUnit aUnit);
00094   void  SetPercentValue(float aValue);
00095   void  SetFactorValue(float aValue);
00096   void  SetNormalValue(void);
00097   void  SetAutoValue(void);
00098   void  SetUnionValue(const nsStyleUnion& aValue, nsStyleUnit aUnit);
00099 
00100   void  AppendToString(nsString& aBuffer) const;
00101   void  ToString(nsString& aBuffer) const;
00102 
00103 public:
00104   nsStyleUnit   mUnit;
00105   nsStyleUnion  mValue;
00106 };
00107 
00108 
00114 class nsStyleSides {
00115 public:
00116   nsStyleSides(void);
00117 
00118 //  nsStyleSides&  operator=(const nsStyleSides& aCopy);  // use compiler's version
00119   PRBool         operator==(const nsStyleSides& aOther) const;
00120   PRBool         operator!=(const nsStyleSides& aOther) const;
00121 
00122   // aSide is always one of NS_SIDE_* defined in nsStyleConsts.h
00123 
00124   inline nsStyleUnit GetUnit(PRUint8 aSide) const;
00125   inline nsStyleUnit GetLeftUnit(void) const;
00126   inline nsStyleUnit GetTopUnit(void) const;
00127   inline nsStyleUnit GetRightUnit(void) const;
00128   inline nsStyleUnit GetBottomUnit(void) const;
00129 
00130   inline nsStyleCoord& Get(PRUint8 aSide, nsStyleCoord& aCoord) const;
00131   inline nsStyleCoord& GetLeft(nsStyleCoord& aCoord) const;
00132   inline nsStyleCoord& GetTop(nsStyleCoord& aCoord) const;
00133   inline nsStyleCoord& GetRight(nsStyleCoord& aCoord) const;
00134   inline nsStyleCoord& GetBottom(nsStyleCoord& aCoord) const;
00135 
00136   void  Reset(void);
00137 
00138   inline void Set(PRUint8 aSide, const nsStyleCoord& aCoord);
00139   inline void SetLeft(const nsStyleCoord& aCoord);
00140   inline void SetTop(const nsStyleCoord& aCoord);
00141   inline void SetRight(const nsStyleCoord& aCoord);
00142   inline void SetBottom(const nsStyleCoord& aCoord);
00143 
00144   void  AppendToString(nsString& aBuffer) const;
00145   void  ToString(nsString& aBuffer) const;
00146 
00147 protected:
00148   PRUint8       mUnits[4];
00149   nsStyleUnion  mValues[4];
00150 };
00151 
00152 // -------------------------
00153 // nsStyleCoord inlines
00154 //
00155 inline PRBool nsStyleCoord::operator!=(const nsStyleCoord& aOther) const
00156 {
00157   return PRBool(! ((*this) == aOther));
00158 }
00159 
00160 inline PRInt32 nsStyleCoord::GetCoordValue(void) const
00161 {
00162   NS_ASSERTION((mUnit == eStyleUnit_Coord), "not a coord value");
00163   if (mUnit == eStyleUnit_Coord) {
00164     return mValue.mInt;
00165   }
00166   return 0;
00167 }
00168 
00169 inline PRInt32 nsStyleCoord::GetIntValue(void) const
00170 {
00171   NS_ASSERTION((mUnit == eStyleUnit_Proportional) ||
00172                (mUnit == eStyleUnit_Enumerated) ||
00173                (mUnit == eStyleUnit_Chars) ||
00174                (mUnit == eStyleUnit_Integer), "not an int value");
00175   if ((mUnit == eStyleUnit_Proportional) ||
00176       (mUnit == eStyleUnit_Enumerated) ||
00177       (mUnit == eStyleUnit_Chars) ||
00178       (mUnit == eStyleUnit_Integer)) {
00179     return mValue.mInt;
00180   }
00181   return 0;
00182 }
00183 
00184 inline float nsStyleCoord::GetPercentValue(void) const
00185 {
00186   NS_ASSERTION(mUnit == eStyleUnit_Percent, "not a percent value");
00187   if (mUnit == eStyleUnit_Percent) {
00188     return mValue.mFloat;
00189   }
00190   return 0.0f;
00191 }
00192 
00193 inline float nsStyleCoord::GetFactorValue(void) const
00194 {
00195   NS_ASSERTION(mUnit == eStyleUnit_Factor, "not a factor value");
00196   if (mUnit == eStyleUnit_Factor) {
00197     return mValue.mFloat;
00198   }
00199   return 0.0f;
00200 }
00201 
00202 inline void nsStyleCoord::GetUnionValue(nsStyleUnion& aValue) const
00203 {
00204   memcpy(&aValue, &mValue, sizeof(nsStyleUnion));
00205 }
00206 
00207 // -------------------------
00208 // nsStyleSides inlines
00209 //
00210 inline PRBool nsStyleSides::operator!=(const nsStyleSides& aOther) const
00211 {
00212   return PRBool(! ((*this) == aOther));
00213 }
00214 
00215 inline nsStyleUnit nsStyleSides::GetUnit(PRUint8 aSide) const
00216 {
00217   return (nsStyleUnit)mUnits[aSide];
00218 }
00219 
00220 inline nsStyleUnit nsStyleSides::GetLeftUnit(void) const
00221 {
00222   return GetUnit(NS_SIDE_LEFT);
00223 }
00224 
00225 inline nsStyleUnit nsStyleSides::GetTopUnit(void) const
00226 {
00227   return GetUnit(NS_SIDE_TOP);
00228 }
00229 
00230 inline nsStyleUnit nsStyleSides::GetRightUnit(void) const
00231 {
00232   return GetUnit(NS_SIDE_RIGHT);
00233 }
00234 
00235 inline nsStyleUnit nsStyleSides::GetBottomUnit(void) const
00236 {
00237   return GetUnit(NS_SIDE_BOTTOM);
00238 }
00239 
00240 inline nsStyleCoord& nsStyleSides::Get(PRUint8 aSide, nsStyleCoord& aCoord) const
00241 {
00242   aCoord.SetUnionValue(mValues[aSide], (nsStyleUnit)mUnits[aSide]);
00243   return aCoord;
00244 }
00245 
00246 inline nsStyleCoord& nsStyleSides::GetLeft(nsStyleCoord& aCoord) const
00247 {
00248   return Get(NS_SIDE_LEFT, aCoord);
00249 }
00250 
00251 inline nsStyleCoord& nsStyleSides::GetTop(nsStyleCoord& aCoord) const
00252 {
00253   return Get(NS_SIDE_TOP, aCoord);
00254 }
00255 
00256 inline nsStyleCoord& nsStyleSides::GetRight(nsStyleCoord& aCoord) const
00257 {
00258   return Get(NS_SIDE_RIGHT, aCoord);
00259 }
00260 
00261 inline nsStyleCoord& nsStyleSides::GetBottom(nsStyleCoord& aCoord) const
00262 {
00263   return Get(NS_SIDE_BOTTOM, aCoord);
00264 }
00265 
00266 inline void nsStyleSides::Set(PRUint8 aSide, const nsStyleCoord& aCoord)
00267 {
00268   mUnits[aSide] = aCoord.GetUnit();
00269   aCoord.GetUnionValue(mValues[aSide]);
00270 }
00271 
00272 inline void nsStyleSides::SetLeft(const nsStyleCoord& aCoord)
00273 {
00274   Set(NS_SIDE_LEFT, aCoord);
00275 }
00276 
00277 inline void nsStyleSides::SetTop(const nsStyleCoord& aCoord)
00278 {
00279   Set(NS_SIDE_TOP, aCoord);
00280 }
00281 
00282 inline void nsStyleSides::SetRight(const nsStyleCoord& aCoord)
00283 {
00284   Set(NS_SIDE_RIGHT, aCoord);
00285 }
00286 
00287 inline void nsStyleSides::SetBottom(const nsStyleCoord& aCoord)
00288 {
00289   Set(NS_SIDE_BOTTOM, aCoord);
00290 }
00291 
00292 #endif /* nsStyleCoord_h___ */