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);
106 void AppendToString(nsString
& aBuffer
) const;
107 void ToString(nsString
& aBuffer
) const;
116 * Class that represents a set of top/right/bottom/left nsStyleCoords.
117 * This is commonly used to hold the widths of the borders, margins,
118 * or paddings of a box.
124 // nsStyleSides& operator=(const nsStyleSides& aCopy); // use compiler's version
125 PRBool
operator==(const nsStyleSides
& aOther
) const;
126 PRBool
operator!=(const nsStyleSides
& aOther
) const;
128 // aSide is always one of NS_SIDE_* defined in nsStyleConsts.h
130 inline nsStyleUnit
GetUnit(PRUint8 aSide
) const;
131 inline nsStyleUnit
GetLeftUnit(void) const;
132 inline nsStyleUnit
GetTopUnit(void) const;
133 inline nsStyleUnit
GetRightUnit(void) const;
134 inline nsStyleUnit
GetBottomUnit(void) const;
136 inline nsStyleCoord
Get(PRUint8 aSide
) const;
137 inline nsStyleCoord
GetLeft() const;
138 inline nsStyleCoord
GetTop() const;
139 inline nsStyleCoord
GetRight() const;
140 inline nsStyleCoord
GetBottom() const;
144 inline void Set(PRUint8 aSide
, const nsStyleCoord
& aCoord
);
145 inline void SetLeft(const nsStyleCoord
& aCoord
);
146 inline void SetTop(const nsStyleCoord
& aCoord
);
147 inline void SetRight(const nsStyleCoord
& aCoord
);
148 inline void SetBottom(const nsStyleCoord
& aCoord
);
150 void AppendToString(nsString
& aBuffer
) const;
151 void ToString(nsString
& aBuffer
) const;
155 nsStyleUnion mValues
[4];
158 // -------------------------
159 // nsStyleCoord inlines
161 inline nsStyleCoord::nsStyleCoord(const nsStyleCoord
& aCopy
)
164 if ((eStyleUnit_Percent
<= mUnit
) && (mUnit
< eStyleUnit_Coord
)) {
165 mValue
.mFloat
= aCopy
.mValue
.mFloat
;
168 mValue
.mInt
= aCopy
.mValue
.mInt
;
172 inline nsStyleCoord::nsStyleCoord(const nsStyleUnion
& aValue
, nsStyleUnit aUnit
)
175 #if PR_BYTES_PER_INT == PR_BYTES_PER_FLOAT
176 mValue
.mInt
= aValue
.mInt
;
178 memcpy(&mValue
, &aValue
, sizeof(nsStyleUnion
));
182 inline PRBool
nsStyleCoord::operator!=(const nsStyleCoord
& aOther
) const
184 return PRBool(! ((*this) == aOther
));
187 inline PRInt32
nsStyleCoord::GetCoordValue(void) const
189 NS_ASSERTION((mUnit
== eStyleUnit_Coord
), "not a coord value");
190 if (mUnit
== eStyleUnit_Coord
) {
196 inline PRInt32
nsStyleCoord::GetIntValue(void) const
198 NS_ASSERTION((mUnit
== eStyleUnit_Enumerated
) ||
199 (mUnit
== eStyleUnit_Integer
), "not an int value");
200 if ((mUnit
== eStyleUnit_Enumerated
) ||
201 (mUnit
== eStyleUnit_Integer
)) {
207 inline float nsStyleCoord::GetPercentValue(void) const
209 NS_ASSERTION(mUnit
== eStyleUnit_Percent
, "not a percent value");
210 if (mUnit
== eStyleUnit_Percent
) {
211 return mValue
.mFloat
;
216 inline float nsStyleCoord::GetFactorValue(void) const
218 NS_ASSERTION(mUnit
== eStyleUnit_Factor
, "not a factor value");
219 if (mUnit
== eStyleUnit_Factor
) {
220 return mValue
.mFloat
;
225 inline void nsStyleCoord::GetUnionValue(nsStyleUnion
& aValue
) const
227 memcpy(&aValue
, &mValue
, sizeof(nsStyleUnion
));
230 // -------------------------
231 // nsStyleSides inlines
233 inline PRBool
nsStyleSides::operator!=(const nsStyleSides
& aOther
) const
235 return PRBool(! ((*this) == aOther
));
238 inline nsStyleUnit
nsStyleSides::GetUnit(PRUint8 aSide
) const
240 return (nsStyleUnit
)mUnits
[aSide
];
243 inline nsStyleUnit
nsStyleSides::GetLeftUnit(void) const
245 return GetUnit(NS_SIDE_LEFT
);
248 inline nsStyleUnit
nsStyleSides::GetTopUnit(void) const
250 return GetUnit(NS_SIDE_TOP
);
253 inline nsStyleUnit
nsStyleSides::GetRightUnit(void) const
255 return GetUnit(NS_SIDE_RIGHT
);
258 inline nsStyleUnit
nsStyleSides::GetBottomUnit(void) const
260 return GetUnit(NS_SIDE_BOTTOM
);
263 inline nsStyleCoord
nsStyleSides::Get(PRUint8 aSide
) const
265 return nsStyleCoord(mValues
[aSide
], nsStyleUnit(mUnits
[aSide
]));
268 inline nsStyleCoord
nsStyleSides::GetLeft() const
270 return Get(NS_SIDE_LEFT
);
273 inline nsStyleCoord
nsStyleSides::GetTop() const
275 return Get(NS_SIDE_TOP
);
278 inline nsStyleCoord
nsStyleSides::GetRight() const
280 return Get(NS_SIDE_RIGHT
);
283 inline nsStyleCoord
nsStyleSides::GetBottom() const
285 return Get(NS_SIDE_BOTTOM
);
288 inline void nsStyleSides::Set(PRUint8 aSide
, const nsStyleCoord
& aCoord
)
290 mUnits
[aSide
] = aCoord
.GetUnit();
291 aCoord
.GetUnionValue(mValues
[aSide
]);
294 inline void nsStyleSides::SetLeft(const nsStyleCoord
& aCoord
)
296 Set(NS_SIDE_LEFT
, aCoord
);
299 inline void nsStyleSides::SetTop(const nsStyleCoord
& aCoord
)
301 Set(NS_SIDE_TOP
, aCoord
);
304 inline void nsStyleSides::SetRight(const nsStyleCoord
& aCoord
)
306 Set(NS_SIDE_RIGHT
, aCoord
);
309 inline void nsStyleSides::SetBottom(const nsStyleCoord
& aCoord
)
311 Set(NS_SIDE_BOTTOM
, aCoord
);
314 #endif /* nsStyleCoord_h___ */