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 #ifndef nsStyleCoord_h___
41 #define nsStyleCoord_h___
46 #include "nsStyleConsts.h"
50 eStyleUnit_Null
= 0, // (no value) value is not specified
51 eStyleUnit_Normal
= 1, // (no value)
52 eStyleUnit_Auto
= 2, // (no value)
53 eStyleUnit_None
= 3, // (no value)
54 eStyleUnit_Percent
= 10, // (float) 1.0 == 100%
55 eStyleUnit_Factor
= 11, // (float) a multiplier
56 eStyleUnit_Coord
= 20, // (nscoord) value is twips
57 eStyleUnit_Integer
= 30, // (int) value is simple integer
58 eStyleUnit_Enumerated
= 32 // (int) value has enumerated meaning
62 PRInt32 mInt
; // nscoord is a PRInt32 for now
67 * Class that hold a single size specification used by the style
68 * system. The size specification consists of two parts -- a number
69 * and a unit. The number is an integer, a floating point value, an
70 * nscoord, or undefined, and the unit is an nsStyleUnit. Checking
71 * the unit is a must before asking for the value in any particular
76 nsStyleCoord(nsStyleUnit aUnit
= eStyleUnit_Null
);
77 nsStyleCoord(nscoord aValue
);
78 nsStyleCoord(PRInt32 aValue
, nsStyleUnit aUnit
);
79 nsStyleCoord(float aValue
, nsStyleUnit aUnit
);
80 inline nsStyleCoord(const nsStyleCoord
& aCopy
);
81 inline nsStyleCoord(const nsStyleUnion
& aValue
, nsStyleUnit aUnit
);
83 nsStyleCoord
& operator=(const nsStyleCoord
& aCopy
);
84 PRBool
operator==(const nsStyleCoord
& aOther
) const;
85 PRBool
operator!=(const nsStyleCoord
& aOther
) const;
87 nsStyleUnit
GetUnit(void) const {
88 NS_ASSERTION(mUnit
!= eStyleUnit_Null
, "reading uninitialized value");
91 nscoord
GetCoordValue(void) const;
92 PRInt32
GetIntValue(void) const;
93 float GetPercentValue(void) const;
94 float GetFactorValue(void) const;
95 void GetUnionValue(nsStyleUnion
& aValue
) const;
97 void Reset(void); // sets to null
98 void SetCoordValue(nscoord aValue
);
99 void SetIntValue(PRInt32 aValue
, nsStyleUnit aUnit
);
100 void SetPercentValue(float aValue
);
101 void SetFactorValue(float aValue
);
102 void SetNormalValue(void);
103 void SetAutoValue(void);
104 void SetNoneValue(void);
107 void AppendToString(nsString
& aBuffer
) const;
108 void ToString(nsString
& aBuffer
) const;
118 * Class that represents a set of top/right/bottom/left nsStyleCoords.
119 * This is commonly used to hold the widths of the borders, margins,
120 * or paddings of a box.
126 // nsStyleSides& operator=(const nsStyleSides& aCopy); // use compiler's version
127 PRBool
operator==(const nsStyleSides
& aOther
) const;
128 PRBool
operator!=(const nsStyleSides
& aOther
) const;
130 // aSide is always one of NS_SIDE_* defined in nsStyleConsts.h
132 inline nsStyleUnit
GetUnit(PRUint8 aSide
) const;
133 inline nsStyleUnit
GetLeftUnit(void) const;
134 inline nsStyleUnit
GetTopUnit(void) const;
135 inline nsStyleUnit
GetRightUnit(void) const;
136 inline nsStyleUnit
GetBottomUnit(void) const;
138 inline nsStyleCoord
Get(PRUint8 aSide
) const;
139 inline nsStyleCoord
GetLeft() const;
140 inline nsStyleCoord
GetTop() const;
141 inline nsStyleCoord
GetRight() const;
142 inline nsStyleCoord
GetBottom() const;
146 inline void Set(PRUint8 aSide
, const nsStyleCoord
& aCoord
);
147 inline void SetLeft(const nsStyleCoord
& aCoord
);
148 inline void SetTop(const nsStyleCoord
& aCoord
);
149 inline void SetRight(const nsStyleCoord
& aCoord
);
150 inline void SetBottom(const nsStyleCoord
& aCoord
);
153 void AppendToString(nsString
& aBuffer
) const;
154 void ToString(nsString
& aBuffer
) const;
159 nsStyleUnion mValues
[4];
163 * Class that represents a set of top-left/top-right/bottom-left/bottom-right
164 * nsStyleCoord pairs. This is used to hold the dimensions of the
165 * corners of a box (for, e.g., border-radius and outline-radius).
167 class nsStyleCorners
{
169 nsStyleCorners(void);
171 // use compiler's version
172 //nsStyleCorners& operator=(const nsStyleCorners& aCopy);
173 PRBool
operator==(const nsStyleCorners
& aOther
) const;
174 PRBool
operator!=(const nsStyleCorners
& aOther
) const;
176 // aCorner is always one of NS_CORNER_* defined in nsStyleConsts.h
177 inline nsStyleUnit
GetUnit(PRUint8 aHalfCorner
) const;
179 inline nsStyleCoord
Get(PRUint8 aHalfCorner
) const;
183 inline void Set(PRUint8 aHalfCorner
, const nsStyleCoord
& aCoord
);
186 void AppendToString(nsString
& aBuffer
) const;
187 void ToString(nsString
& aBuffer
) const;
192 nsStyleUnion mValues
[8];
196 // -------------------------
197 // nsStyleCoord inlines
199 inline nsStyleCoord::nsStyleCoord(const nsStyleCoord
& aCopy
)
202 if ((eStyleUnit_Percent
<= mUnit
) && (mUnit
< eStyleUnit_Coord
)) {
203 mValue
.mFloat
= aCopy
.mValue
.mFloat
;
206 mValue
.mInt
= aCopy
.mValue
.mInt
;
210 inline nsStyleCoord::nsStyleCoord(const nsStyleUnion
& aValue
, nsStyleUnit aUnit
)
213 #if PR_BYTES_PER_INT == PR_BYTES_PER_FLOAT
214 mValue
.mInt
= aValue
.mInt
;
216 memcpy(&mValue
, &aValue
, sizeof(nsStyleUnion
));
220 inline PRBool
nsStyleCoord::operator!=(const nsStyleCoord
& aOther
) const
222 return !((*this) == aOther
);
225 inline PRInt32
nsStyleCoord::GetCoordValue(void) const
227 NS_ASSERTION((mUnit
== eStyleUnit_Coord
), "not a coord value");
228 if (mUnit
== eStyleUnit_Coord
) {
234 inline PRInt32
nsStyleCoord::GetIntValue(void) const
236 NS_ASSERTION((mUnit
== eStyleUnit_Enumerated
) ||
237 (mUnit
== eStyleUnit_Integer
), "not an int value");
238 if ((mUnit
== eStyleUnit_Enumerated
) ||
239 (mUnit
== eStyleUnit_Integer
)) {
245 inline float nsStyleCoord::GetPercentValue(void) const
247 NS_ASSERTION(mUnit
== eStyleUnit_Percent
, "not a percent value");
248 if (mUnit
== eStyleUnit_Percent
) {
249 return mValue
.mFloat
;
254 inline float nsStyleCoord::GetFactorValue(void) const
256 NS_ASSERTION(mUnit
== eStyleUnit_Factor
, "not a factor value");
257 if (mUnit
== eStyleUnit_Factor
) {
258 return mValue
.mFloat
;
263 inline void nsStyleCoord::GetUnionValue(nsStyleUnion
& aValue
) const
265 memcpy(&aValue
, &mValue
, sizeof(nsStyleUnion
));
268 // -------------------------
269 // nsStyleSides inlines
271 inline PRBool
nsStyleSides::operator!=(const nsStyleSides
& aOther
) const
273 return !((*this) == aOther
);
276 inline nsStyleUnit
nsStyleSides::GetUnit(PRUint8 aSide
) const
278 return (nsStyleUnit
)mUnits
[aSide
];
281 inline nsStyleUnit
nsStyleSides::GetLeftUnit(void) const
283 return GetUnit(NS_SIDE_LEFT
);
286 inline nsStyleUnit
nsStyleSides::GetTopUnit(void) const
288 return GetUnit(NS_SIDE_TOP
);
291 inline nsStyleUnit
nsStyleSides::GetRightUnit(void) const
293 return GetUnit(NS_SIDE_RIGHT
);
296 inline nsStyleUnit
nsStyleSides::GetBottomUnit(void) const
298 return GetUnit(NS_SIDE_BOTTOM
);
301 inline nsStyleCoord
nsStyleSides::Get(PRUint8 aSide
) const
303 return nsStyleCoord(mValues
[aSide
], nsStyleUnit(mUnits
[aSide
]));
306 inline nsStyleCoord
nsStyleSides::GetLeft() const
308 return Get(NS_SIDE_LEFT
);
311 inline nsStyleCoord
nsStyleSides::GetTop() const
313 return Get(NS_SIDE_TOP
);
316 inline nsStyleCoord
nsStyleSides::GetRight() const
318 return Get(NS_SIDE_RIGHT
);
321 inline nsStyleCoord
nsStyleSides::GetBottom() const
323 return Get(NS_SIDE_BOTTOM
);
326 inline void nsStyleSides::Set(PRUint8 aSide
, const nsStyleCoord
& aCoord
)
328 mUnits
[aSide
] = aCoord
.GetUnit();
329 aCoord
.GetUnionValue(mValues
[aSide
]);
332 inline void nsStyleSides::SetLeft(const nsStyleCoord
& aCoord
)
334 Set(NS_SIDE_LEFT
, aCoord
);
337 inline void nsStyleSides::SetTop(const nsStyleCoord
& aCoord
)
339 Set(NS_SIDE_TOP
, aCoord
);
342 inline void nsStyleSides::SetRight(const nsStyleCoord
& aCoord
)
344 Set(NS_SIDE_RIGHT
, aCoord
);
347 inline void nsStyleSides::SetBottom(const nsStyleCoord
& aCoord
)
349 Set(NS_SIDE_BOTTOM
, aCoord
);
352 // -------------------------
353 // nsStyleCorners inlines
355 inline PRBool
nsStyleCorners::operator!=(const nsStyleCorners
& aOther
) const
357 return !((*this) == aOther
);
360 inline nsStyleUnit
nsStyleCorners::GetUnit(PRUint8 aCorner
) const
362 return (nsStyleUnit
)mUnits
[aCorner
];
365 inline nsStyleCoord
nsStyleCorners::Get(PRUint8 aCorner
) const
367 return nsStyleCoord(mValues
[aCorner
], nsStyleUnit(mUnits
[aCorner
]));
370 inline void nsStyleCorners::Set(PRUint8 aCorner
, const nsStyleCoord
& aCoord
)
372 mUnits
[aCorner
] = aCoord
.GetUnit();
373 aCoord
.GetUnionValue(mValues
[aCorner
]);
376 #endif /* nsStyleCoord_h___ */