1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* representation of length values in computed style data */
40 #include "nsStyleCoord.h"
44 nsStyleCoord::nsStyleCoord(nsStyleUnit aUnit
)
47 NS_ASSERTION(aUnit
< eStyleUnit_Percent
, "not a valueless unit");
48 if (aUnit
>= eStyleUnit_Percent
) {
49 mUnit
= eStyleUnit_Null
;
54 nsStyleCoord::nsStyleCoord(nscoord aValue
)
55 : mUnit(eStyleUnit_Coord
)
60 nsStyleCoord::nsStyleCoord(PRInt32 aValue
, nsStyleUnit aUnit
)
63 //if you want to pass in eStyleUnit_Coord, don't. instead, use the
64 //constructor just above this one... MMP
65 NS_ASSERTION((aUnit
== eStyleUnit_Enumerated
) ||
66 (aUnit
== eStyleUnit_Integer
), "not an int value");
67 if ((aUnit
== eStyleUnit_Enumerated
) ||
68 (aUnit
== eStyleUnit_Integer
)) {
72 mUnit
= eStyleUnit_Null
;
77 nsStyleCoord::nsStyleCoord(float aValue
, nsStyleUnit aUnit
)
80 NS_ASSERTION((aUnit
== eStyleUnit_Percent
) ||
81 (aUnit
== eStyleUnit_Factor
), "not a float value");
82 if ((aUnit
== eStyleUnit_Percent
) ||
83 (aUnit
== eStyleUnit_Factor
)) {
84 mValue
.mFloat
= aValue
;
87 mUnit
= eStyleUnit_Null
;
92 nsStyleCoord
& nsStyleCoord::operator=(const nsStyleCoord
& aCopy
)
95 if ((eStyleUnit_Percent
<= mUnit
) && (mUnit
< eStyleUnit_Coord
)) {
96 mValue
.mFloat
= aCopy
.mValue
.mFloat
;
99 mValue
.mInt
= aCopy
.mValue
.mInt
;
104 PRBool
nsStyleCoord::operator==(const nsStyleCoord
& aOther
) const
106 if (mUnit
== aOther
.mUnit
) {
107 if ((eStyleUnit_Percent
<= mUnit
) && (mUnit
< eStyleUnit_Coord
)) {
108 return PRBool(mValue
.mFloat
== aOther
.mValue
.mFloat
);
111 return PRBool(mValue
.mInt
== aOther
.mValue
.mInt
);
117 void nsStyleCoord::Reset(void)
119 mUnit
= eStyleUnit_Null
;
123 void nsStyleCoord::SetCoordValue(nscoord aValue
)
125 mUnit
= eStyleUnit_Coord
;
126 mValue
.mInt
= aValue
;
129 void nsStyleCoord::SetIntValue(PRInt32 aValue
, nsStyleUnit aUnit
)
131 NS_ASSERTION((aUnit
== eStyleUnit_Enumerated
) ||
132 (aUnit
== eStyleUnit_Integer
), "not an int value");
133 if ((aUnit
== eStyleUnit_Enumerated
) ||
134 (aUnit
== eStyleUnit_Integer
)) {
136 mValue
.mInt
= aValue
;
143 void nsStyleCoord::SetPercentValue(float aValue
)
145 mUnit
= eStyleUnit_Percent
;
146 mValue
.mFloat
= aValue
;
149 void nsStyleCoord::SetFactorValue(float aValue
)
151 mUnit
= eStyleUnit_Factor
;
152 mValue
.mFloat
= aValue
;
155 void nsStyleCoord::SetNormalValue(void)
157 mUnit
= eStyleUnit_Normal
;
161 void nsStyleCoord::SetAutoValue(void)
163 mUnit
= eStyleUnit_Auto
;
167 void nsStyleCoord::SetNoneValue(void)
169 mUnit
= eStyleUnit_None
;
173 void nsStyleCoord::AppendToString(nsString
& aBuffer
) const
175 if ((eStyleUnit_Percent
<= mUnit
) && (mUnit
< eStyleUnit_Coord
)) {
176 aBuffer
.AppendFloat(mValue
.mFloat
);
178 else if ((eStyleUnit_Coord
== mUnit
) ||
179 (eStyleUnit_Enumerated
== mUnit
) ||
180 (eStyleUnit_Integer
== mUnit
)) {
181 aBuffer
.AppendInt(mValue
.mInt
, 10);
182 aBuffer
.AppendLiteral("[0x");
183 aBuffer
.AppendInt(mValue
.mInt
, 16);
184 aBuffer
.Append(PRUnichar(']'));
188 case eStyleUnit_Null
: aBuffer
.AppendLiteral("Null"); break;
189 case eStyleUnit_Coord
: aBuffer
.AppendLiteral("tw"); break;
190 case eStyleUnit_Percent
: aBuffer
.AppendLiteral("%"); break;
191 case eStyleUnit_Factor
: aBuffer
.AppendLiteral("f"); break;
192 case eStyleUnit_Normal
: aBuffer
.AppendLiteral("Normal"); break;
193 case eStyleUnit_Auto
: aBuffer
.AppendLiteral("Auto"); break;
194 case eStyleUnit_None
: aBuffer
.AppendLiteral("None"); break;
195 case eStyleUnit_Enumerated
: aBuffer
.AppendLiteral("enum"); break;
196 case eStyleUnit_Integer
: aBuffer
.AppendLiteral("int"); break;
198 aBuffer
.Append(PRUnichar(' '));
201 void nsStyleCoord::ToString(nsString
& aBuffer
) const
204 AppendToString(aBuffer
);
210 nsStyleSides::nsStyleSides(void)
212 memset(this, 0x00, sizeof(nsStyleSides
));
215 #define COMPARE_SIDE(side) \
216 if ((eStyleUnit_Percent <= mUnits[side]) && \
217 (mUnits[side] < eStyleUnit_Coord)) { \
218 if (mValues[side].mFloat != aOther.mValues[side].mFloat) \
222 if (mValues[side].mInt != aOther.mValues[side].mInt) \
226 PRBool
nsStyleSides::operator==(const nsStyleSides
& aOther
) const
228 if ((mUnits
[NS_SIDE_LEFT
] == aOther
.mUnits
[NS_SIDE_LEFT
]) &&
229 (mUnits
[NS_SIDE_TOP
] == aOther
.mUnits
[NS_SIDE_TOP
]) &&
230 (mUnits
[NS_SIDE_RIGHT
] == aOther
.mUnits
[NS_SIDE_RIGHT
]) &&
231 (mUnits
[NS_SIDE_BOTTOM
] == aOther
.mUnits
[NS_SIDE_BOTTOM
])) {
232 COMPARE_SIDE(NS_SIDE_LEFT
);
233 COMPARE_SIDE(NS_SIDE_TOP
);
234 COMPARE_SIDE(NS_SIDE_RIGHT
);
235 COMPARE_SIDE(NS_SIDE_BOTTOM
);
241 void nsStyleSides::Reset(void)
243 memset(this, 0x00, sizeof(nsStyleSides
));
246 void nsStyleSides::AppendToString(nsString
& aBuffer
) const
248 aBuffer
.AppendLiteral("left: ");
249 GetLeft().AppendToString(aBuffer
);
251 aBuffer
.AppendLiteral("top: ");
252 GetTop().AppendToString(aBuffer
);
254 aBuffer
.AppendLiteral("right: ");
255 GetRight().AppendToString(aBuffer
);
257 aBuffer
.AppendLiteral("bottom: ");
258 GetBottom().AppendToString(aBuffer
);
261 void nsStyleSides::ToString(nsString
& aBuffer
) const
264 AppendToString(aBuffer
);