michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/DOMRect.h" michael@0: michael@0: #include "nsPresContext.h" michael@0: #include "mozilla/dom/DOMRectListBinding.h" michael@0: #include "mozilla/dom/DOMRectBinding.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DOMRectReadOnly, mParent) michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectReadOnly) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectReadOnly) michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMRectReadOnly) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: JSObject* michael@0: DOMRectReadOnly::WrapObject(JSContext* aCx) michael@0: { michael@0: MOZ_ASSERT(mParent); michael@0: return DOMRectReadOnlyBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED(DOMRect, DOMRectReadOnly, nsIDOMClientRect) michael@0: michael@0: #define FORWARD_GETTER(_name) \ michael@0: NS_IMETHODIMP \ michael@0: DOMRect::Get ## _name(float* aResult) \ michael@0: { \ michael@0: *aResult = float(_name()); \ michael@0: return NS_OK; \ michael@0: } michael@0: michael@0: FORWARD_GETTER(Left) michael@0: FORWARD_GETTER(Top) michael@0: FORWARD_GETTER(Right) michael@0: FORWARD_GETTER(Bottom) michael@0: FORWARD_GETTER(Width) michael@0: FORWARD_GETTER(Height) michael@0: michael@0: JSObject* michael@0: DOMRect::WrapObject(JSContext* aCx) michael@0: { michael@0: MOZ_ASSERT(mParent); michael@0: return DOMRectBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DOMRect::Constructor(const GlobalObject& aGlobal, ErrorResult& aRV) michael@0: { michael@0: nsRefPtr obj = michael@0: new DOMRect(aGlobal.GetAsSupports(), 0.0, 0.0, 0.0, 0.0); michael@0: return obj.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DOMRect::Constructor(const GlobalObject& aGlobal, double aX, double aY, michael@0: double aWidth, double aHeight, ErrorResult& aRV) michael@0: { michael@0: nsRefPtr obj = michael@0: new DOMRect(aGlobal.GetAsSupports(), aX, aY, aWidth, aHeight); michael@0: return obj.forget(); michael@0: } michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(DOMRectList, mParent, mArray) michael@0: michael@0: NS_INTERFACE_TABLE_HEAD(DOMRectList) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_TABLE(DOMRectList, nsIDOMClientRectList) michael@0: NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(DOMRectList) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectList) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectList) michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: DOMRectList::GetLength(uint32_t* aLength) michael@0: { michael@0: *aLength = Length(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: DOMRectList::Item(uint32_t aIndex, nsIDOMClientRect** aReturn) michael@0: { michael@0: NS_IF_ADDREF(*aReturn = Item(aIndex)); michael@0: return NS_OK; michael@0: } michael@0: michael@0: JSObject* michael@0: DOMRectList::WrapObject(JSContext *cx) michael@0: { michael@0: return mozilla::dom::DOMRectListBinding::Wrap(cx, this); michael@0: } michael@0: michael@0: static double michael@0: RoundFloat(double aValue) michael@0: { michael@0: return floor(aValue + 0.5); michael@0: } michael@0: michael@0: void michael@0: DOMRect::SetLayoutRect(const nsRect& aLayoutRect) michael@0: { michael@0: double scale = 65536.0; michael@0: // Round to the nearest 1/scale units. We choose scale so it can be represented michael@0: // exactly by machine floating point. michael@0: double scaleInv = 1/scale; michael@0: double t2pScaled = scale/nsPresContext::AppUnitsPerCSSPixel(); michael@0: double x = RoundFloat(aLayoutRect.x*t2pScaled)*scaleInv; michael@0: double y = RoundFloat(aLayoutRect.y*t2pScaled)*scaleInv; michael@0: SetRect(x, y, RoundFloat(aLayoutRect.XMost()*t2pScaled)*scaleInv - x, michael@0: RoundFloat(aLayoutRect.YMost()*t2pScaled)*scaleInv - y); michael@0: }