Back to index

lightning-sunbird  0.9+nobinonly
nsStyleCoord.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 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 #include "nsStyleCoord.h"
00039 #include "nsString.h"
00040 #include "nsCRT.h"
00041 
00042 nsStyleCoord::nsStyleCoord(nsStyleUnit aUnit)
00043   : mUnit(aUnit)
00044 {
00045   NS_ASSERTION(aUnit < eStyleUnit_Percent, "not a valueless unit");
00046   if (aUnit >= eStyleUnit_Percent) {
00047     mUnit = eStyleUnit_Null;
00048   }
00049   mValue.mInt = 0;
00050 }
00051 
00052 nsStyleCoord::nsStyleCoord(nscoord aValue)
00053   : mUnit(eStyleUnit_Coord)
00054 {
00055   mValue.mInt = aValue;
00056 }
00057 
00058 nsStyleCoord::nsStyleCoord(PRInt32 aValue, nsStyleUnit aUnit)
00059   : mUnit(aUnit)
00060 {
00061   //if you want to pass in eStyleUnit_Coord, don't. instead, use the
00062   //constructor just above this one... MMP
00063   NS_ASSERTION((aUnit == eStyleUnit_Proportional) ||
00064                (aUnit == eStyleUnit_Enumerated) ||
00065                (aUnit == eStyleUnit_Integer), "not an int value");
00066   if ((aUnit == eStyleUnit_Proportional) ||
00067       (aUnit == eStyleUnit_Enumerated) ||
00068       (aUnit == eStyleUnit_Integer)) {
00069     mValue.mInt = aValue;
00070   }
00071   else {
00072     mUnit = eStyleUnit_Null;
00073     mValue.mInt = 0;
00074   }
00075 }
00076 
00077 nsStyleCoord::nsStyleCoord(float aValue, nsStyleUnit aUnit)
00078   : mUnit(aUnit)
00079 {
00080   NS_ASSERTION((aUnit == eStyleUnit_Percent) ||
00081                (aUnit == eStyleUnit_Factor), "not a float value");
00082   if ((aUnit == eStyleUnit_Percent) ||
00083       (aUnit == eStyleUnit_Factor)) {
00084     mValue.mFloat = aValue;
00085   }
00086   else {
00087     mUnit = eStyleUnit_Null;
00088     mValue.mInt = 0;
00089   }
00090 }
00091 
00092 nsStyleCoord::nsStyleCoord(const nsStyleCoord& aCopy)
00093   : mUnit(aCopy.mUnit)
00094 {
00095   if ((eStyleUnit_Percent <= mUnit) && (mUnit < eStyleUnit_Coord)) {
00096     mValue.mFloat = aCopy.mValue.mFloat;
00097   }
00098   else {
00099     mValue.mInt = aCopy.mValue.mInt;
00100   }
00101 }
00102 
00103 nsStyleCoord& nsStyleCoord::operator=(const nsStyleCoord& aCopy)
00104 {
00105   mUnit = aCopy.mUnit;
00106   if ((eStyleUnit_Percent <= mUnit) && (mUnit < eStyleUnit_Coord)) {
00107     mValue.mFloat = aCopy.mValue.mFloat;
00108   }
00109   else {
00110     mValue.mInt = aCopy.mValue.mInt;
00111   }
00112   return *this;
00113 }
00114 
00115 PRBool nsStyleCoord::operator==(const nsStyleCoord& aOther) const
00116 {
00117   if (mUnit == aOther.mUnit) {
00118     if ((eStyleUnit_Percent <= mUnit) && (mUnit < eStyleUnit_Coord)) {
00119       return PRBool(mValue.mFloat == aOther.mValue.mFloat);
00120     }
00121     else {
00122       return PRBool(mValue.mInt == aOther.mValue.mInt);
00123     }
00124   }
00125   return PR_FALSE;
00126 }
00127 
00128 void nsStyleCoord::Reset(void)
00129 {
00130   mUnit = eStyleUnit_Null;
00131   mValue.mInt = 0;
00132 }
00133 
00134 void nsStyleCoord::SetCoordValue(nscoord aValue)
00135 {
00136   mUnit = eStyleUnit_Coord;
00137   mValue.mInt = aValue;
00138 }
00139 
00140 void nsStyleCoord::SetIntValue(PRInt32 aValue, nsStyleUnit aUnit)
00141 {
00142   NS_ASSERTION((aUnit == eStyleUnit_Proportional) ||
00143                (aUnit == eStyleUnit_Enumerated) ||
00144                (aUnit == eStyleUnit_Chars) ||
00145                (aUnit == eStyleUnit_Integer), "not an int value");
00146   if ((aUnit == eStyleUnit_Proportional) ||
00147       (aUnit == eStyleUnit_Enumerated) ||
00148       (aUnit == eStyleUnit_Chars) ||
00149       (aUnit == eStyleUnit_Integer)) {
00150     mUnit = aUnit;
00151     mValue.mInt = aValue;
00152   }
00153   else {
00154     Reset();
00155   }
00156 }
00157 
00158 void nsStyleCoord::SetPercentValue(float aValue)
00159 {
00160   mUnit = eStyleUnit_Percent;
00161   mValue.mFloat = aValue;
00162 }
00163 
00164 void nsStyleCoord::SetFactorValue(float aValue)
00165 {
00166   mUnit = eStyleUnit_Factor;
00167   mValue.mFloat = aValue;
00168 }
00169 
00170 void nsStyleCoord::SetNormalValue(void)
00171 {
00172   mUnit = eStyleUnit_Normal;
00173   mValue.mInt = 0;
00174 }
00175 
00176 void nsStyleCoord::SetAutoValue(void)
00177 {
00178   mUnit = eStyleUnit_Auto;
00179   mValue.mInt = 0;
00180 }
00181 
00182 void nsStyleCoord::SetUnionValue(const nsStyleUnion& aValue, nsStyleUnit aUnit)
00183 {
00184   mUnit = aUnit;
00185 #if PR_BYTES_PER_INT == PR_BYTES_PER_FLOAT
00186   mValue.mInt = aValue.mInt;
00187 #else
00188   memcpy(&mValue, &aValue, sizeof(nsStyleUnion));
00189 #endif
00190 }
00191 
00192 void nsStyleCoord::AppendToString(nsString& aBuffer) const
00193 {
00194   if ((eStyleUnit_Percent <= mUnit) && (mUnit < eStyleUnit_Coord)) {
00195     aBuffer.AppendFloat(mValue.mFloat);
00196   }
00197   else if ((eStyleUnit_Coord == mUnit) || 
00198            (eStyleUnit_Proportional == mUnit) ||
00199            (eStyleUnit_Enumerated == mUnit) ||
00200            (eStyleUnit_Integer == mUnit)) {
00201     aBuffer.AppendInt(mValue.mInt, 10);
00202     aBuffer.AppendLiteral("[0x");
00203     aBuffer.AppendInt(mValue.mInt, 16);
00204     aBuffer.Append(PRUnichar(']'));
00205   }
00206 
00207   switch (mUnit) {
00208     case eStyleUnit_Null:         aBuffer.AppendLiteral("Null");     break;
00209     case eStyleUnit_Coord:        aBuffer.AppendLiteral("tw");       break;
00210     case eStyleUnit_Percent:      aBuffer.AppendLiteral("%");        break;
00211     case eStyleUnit_Factor:       aBuffer.AppendLiteral("f");        break;
00212     case eStyleUnit_Normal:       aBuffer.AppendLiteral("Normal");   break;
00213     case eStyleUnit_Auto:         aBuffer.AppendLiteral("Auto");     break;
00214     case eStyleUnit_Proportional: aBuffer.AppendLiteral("*");        break;
00215     case eStyleUnit_Enumerated:   aBuffer.AppendLiteral("enum");     break;
00216     case eStyleUnit_Integer:      aBuffer.AppendLiteral("int");      break;
00217     case eStyleUnit_Chars:        aBuffer.AppendLiteral("chars");    break;
00218   }
00219   aBuffer.Append(PRUnichar(' '));
00220 }
00221 
00222 void nsStyleCoord::ToString(nsString& aBuffer) const
00223 {
00224   aBuffer.Truncate();
00225   AppendToString(aBuffer);
00226 }
00227 
00228 
00229 
00230 
00231 nsStyleSides::nsStyleSides(void)
00232 {
00233   memset(this, 0x00, sizeof(nsStyleSides));
00234 }
00235 
00236 #define COMPARE_SIDE(side)                                                    \
00237   if ((eStyleUnit_Percent <= mUnits[side]) &&                                 \
00238       (mUnits[side] < eStyleUnit_Coord)) {                                    \
00239     if (mValues[side].mFloat != aOther.mValues[side].mFloat)                  \
00240       return PR_FALSE;                                                        \
00241   }                                                                           \
00242   else {                                                                      \
00243     if (mValues[side].mInt != aOther.mValues[side].mInt)                      \
00244       return PR_FALSE;                                                        \
00245   }
00246 
00247 PRBool nsStyleSides::operator==(const nsStyleSides& aOther) const
00248 {
00249   if ((mUnits[NS_SIDE_LEFT] == aOther.mUnits[NS_SIDE_LEFT]) && 
00250       (mUnits[NS_SIDE_TOP] == aOther.mUnits[NS_SIDE_TOP]) &&
00251       (mUnits[NS_SIDE_RIGHT] == aOther.mUnits[NS_SIDE_RIGHT]) &&
00252       (mUnits[NS_SIDE_BOTTOM] == aOther.mUnits[NS_SIDE_BOTTOM])) {
00253     COMPARE_SIDE(NS_SIDE_LEFT);
00254     COMPARE_SIDE(NS_SIDE_TOP);
00255     COMPARE_SIDE(NS_SIDE_RIGHT);
00256     COMPARE_SIDE(NS_SIDE_BOTTOM);
00257     return PR_TRUE;
00258   }
00259   return PR_FALSE;
00260 }
00261 
00262 void nsStyleSides::Reset(void)
00263 {
00264   memset(this, 0x00, sizeof(nsStyleSides));
00265 }
00266 
00267 void nsStyleSides::AppendToString(nsString& aBuffer) const
00268 {
00269   nsStyleCoord  temp;
00270 
00271   GetLeft(temp);
00272   aBuffer.AppendLiteral("left: ");
00273   temp.AppendToString(aBuffer);
00274 
00275   GetTop(temp);
00276   aBuffer.AppendLiteral("top: ");
00277   temp.AppendToString(aBuffer);
00278 
00279   GetRight(temp);
00280   aBuffer.AppendLiteral("right: ");
00281   temp.AppendToString(aBuffer);
00282 
00283   GetBottom(temp);
00284   aBuffer.AppendLiteral("bottom: ");
00285   temp.AppendToString(aBuffer);
00286 }
00287 
00288 void nsStyleSides::ToString(nsString& aBuffer) const
00289 {
00290   aBuffer.Truncate();
00291   AppendToString(aBuffer);
00292 }
00293