1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/DOMRect.h"
8 #include "nsPresContext.h"
9 #include "mozilla/dom/DOMRectListBinding.h"
10 #include "mozilla/dom/DOMRectBinding.h"
12 using namespace mozilla
;
13 using namespace mozilla::dom
;
15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMRectReadOnly
, mParent
)
16 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectReadOnly
)
17 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectReadOnly
)
18 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMRectReadOnly
)
19 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
20 NS_INTERFACE_MAP_ENTRY(nsISupports
)
24 DOMRectReadOnly::WrapObject(JSContext
* aCx
)
27 return DOMRectReadOnlyBinding::Wrap(aCx
, this);
30 // -----------------------------------------------------------------------------
32 NS_IMPL_ISUPPORTS_INHERITED(DOMRect
, DOMRectReadOnly
, nsIDOMClientRect
)
34 #define FORWARD_GETTER(_name) \
36 DOMRect::Get ## _name(float* aResult) \
38 *aResult = float(_name()); \
45 FORWARD_GETTER(Bottom
)
47 FORWARD_GETTER(Height
)
50 DOMRect::WrapObject(JSContext
* aCx
)
53 return DOMRectBinding::Wrap(aCx
, this);
56 already_AddRefed
<DOMRect
>
57 DOMRect::Constructor(const GlobalObject
& aGlobal
, ErrorResult
& aRV
)
59 nsRefPtr
<DOMRect
> obj
=
60 new DOMRect(aGlobal
.GetAsSupports(), 0.0, 0.0, 0.0, 0.0);
64 already_AddRefed
<DOMRect
>
65 DOMRect::Constructor(const GlobalObject
& aGlobal
, double aX
, double aY
,
66 double aWidth
, double aHeight
, ErrorResult
& aRV
)
68 nsRefPtr
<DOMRect
> obj
=
69 new DOMRect(aGlobal
.GetAsSupports(), aX
, aY
, aWidth
, aHeight
);
73 // -----------------------------------------------------------------------------
75 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMRectList
, mParent
, mArray
)
77 NS_INTERFACE_TABLE_HEAD(DOMRectList
)
78 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
79 NS_INTERFACE_TABLE(DOMRectList
, nsIDOMClientRectList
)
80 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(DOMRectList
)
83 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectList
)
84 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectList
)
88 DOMRectList::GetLength(uint32_t* aLength
)
95 DOMRectList::Item(uint32_t aIndex
, nsIDOMClientRect
** aReturn
)
97 NS_IF_ADDREF(*aReturn
= Item(aIndex
));
102 DOMRectList::WrapObject(JSContext
*cx
)
104 return mozilla::dom::DOMRectListBinding::Wrap(cx
, this);
108 RoundFloat(double aValue
)
110 return floor(aValue
+ 0.5);
114 DOMRect::SetLayoutRect(const nsRect
& aLayoutRect
)
116 double scale
= 65536.0;
117 // Round to the nearest 1/scale units. We choose scale so it can be represented
118 // exactly by machine floating point.
119 double scaleInv
= 1/scale
;
120 double t2pScaled
= scale
/nsPresContext::AppUnitsPerCSSPixel();
121 double x
= RoundFloat(aLayoutRect
.x
*t2pScaled
)*scaleInv
;
122 double y
= RoundFloat(aLayoutRect
.y
*t2pScaled
)*scaleInv
;
123 SetRect(x
, y
, RoundFloat(aLayoutRect
.XMost()*t2pScaled
)*scaleInv
- x
,
124 RoundFloat(aLayoutRect
.YMost()*t2pScaled
)*scaleInv
- y
);