Bumping manifests a=b2g-bump
[gecko.git] / dom / base / DOMRect.cpp
blob92a2a12074679f0a6f94ec581acf197bcf113543
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)
21 NS_INTERFACE_MAP_END
23 JSObject*
24 DOMRectReadOnly::WrapObject(JSContext* aCx)
26 MOZ_ASSERT(mParent);
27 return DOMRectReadOnlyBinding::Wrap(aCx, this);
30 // -----------------------------------------------------------------------------
32 NS_IMPL_ISUPPORTS_INHERITED(DOMRect, DOMRectReadOnly, nsIDOMClientRect)
34 #define FORWARD_GETTER(_name) \
35 NS_IMETHODIMP \
36 DOMRect::Get ## _name(float* aResult) \
37 { \
38 *aResult = float(_name()); \
39 return NS_OK; \
42 FORWARD_GETTER(Left)
43 FORWARD_GETTER(Top)
44 FORWARD_GETTER(Right)
45 FORWARD_GETTER(Bottom)
46 FORWARD_GETTER(Width)
47 FORWARD_GETTER(Height)
49 JSObject*
50 DOMRect::WrapObject(JSContext* aCx)
52 MOZ_ASSERT(mParent);
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);
61 return obj.forget();
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);
70 return obj.forget();
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)
81 NS_INTERFACE_MAP_END
83 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectList)
84 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectList)
87 NS_IMETHODIMP
88 DOMRectList::GetLength(uint32_t* aLength)
90 *aLength = Length();
91 return NS_OK;
94 NS_IMETHODIMP
95 DOMRectList::Item(uint32_t aIndex, nsIDOMClientRect** aReturn)
97 NS_IF_ADDREF(*aReturn = Item(aIndex));
98 return NS_OK;
101 JSObject*
102 DOMRectList::WrapObject(JSContext *cx)
104 return mozilla::dom::DOMRectListBinding::Wrap(cx, this);
107 static double
108 RoundFloat(double aValue)
110 return floor(aValue + 0.5);
113 void
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);