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: #ifndef MOZILLA_DOMRECT_H_ michael@0: #define MOZILLA_DOMRECT_H_ michael@0: michael@0: #include "nsIDOMClientRect.h" michael@0: #include "nsIDOMClientRectList.h" michael@0: #include "nsTArray.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsWrapperCache.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/dom/BindingDeclarations.h" michael@0: #include "mozilla/ErrorResult.h" michael@0: #include michael@0: michael@0: struct nsRect; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: class DOMRectReadOnly : public nsISupports michael@0: , public nsWrapperCache michael@0: { michael@0: public: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly) michael@0: michael@0: virtual ~DOMRectReadOnly() {} michael@0: michael@0: DOMRectReadOnly(nsISupports* aParent) michael@0: : mParent(aParent) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: nsISupports* GetParentObject() const michael@0: { michael@0: MOZ_ASSERT(mParent); michael@0: return mParent; michael@0: } michael@0: virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: virtual double X() const = 0; michael@0: virtual double Y() const = 0; michael@0: virtual double Width() const = 0; michael@0: virtual double Height() const = 0; michael@0: michael@0: double Left() const michael@0: { michael@0: double x = X(), w = Width(); michael@0: return std::min(x, x + w); michael@0: } michael@0: double Top() const michael@0: { michael@0: double y = Y(), h = Height(); michael@0: return std::min(y, y + h); michael@0: } michael@0: double Right() const michael@0: { michael@0: double x = X(), w = Width(); michael@0: return std::max(x, x + w); michael@0: } michael@0: double Bottom() const michael@0: { michael@0: double y = Y(), h = Height(); michael@0: return std::max(y, y + h); michael@0: } michael@0: michael@0: protected: michael@0: nsCOMPtr mParent; michael@0: }; michael@0: michael@0: class DOMRect MOZ_FINAL : public DOMRectReadOnly michael@0: , public nsIDOMClientRect michael@0: { michael@0: public: michael@0: DOMRect(nsISupports* aParent, double aX = 0, double aY = 0, michael@0: double aWidth = 0, double aHeight = 0) michael@0: : DOMRectReadOnly(aParent) michael@0: , mX(aX) michael@0: , mY(aY) michael@0: , mWidth(aWidth) michael@0: , mHeight(aHeight) michael@0: { michael@0: } michael@0: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_NSIDOMCLIENTRECT michael@0: michael@0: static already_AddRefed michael@0: Constructor(const GlobalObject& aGlobal, ErrorResult& aRV); michael@0: static already_AddRefed michael@0: Constructor(const GlobalObject& aGlobal, double aX, double aY, michael@0: double aWidth, double aHeight, ErrorResult& aRV); michael@0: michael@0: virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: void SetRect(float aX, float aY, float aWidth, float aHeight) { michael@0: mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight; michael@0: } michael@0: void SetLayoutRect(const nsRect& aLayoutRect); michael@0: michael@0: virtual double X() const MOZ_OVERRIDE michael@0: { michael@0: return mX; michael@0: } michael@0: virtual double Y() const MOZ_OVERRIDE michael@0: { michael@0: return mY; michael@0: } michael@0: virtual double Width() const MOZ_OVERRIDE michael@0: { michael@0: return mWidth; michael@0: } michael@0: virtual double Height() const MOZ_OVERRIDE michael@0: { michael@0: return mHeight; michael@0: } michael@0: michael@0: void SetX(double aX) michael@0: { michael@0: mX = aX; michael@0: } michael@0: void SetY(double aY) michael@0: { michael@0: mY = aY; michael@0: } michael@0: void SetWidth(double aWidth) michael@0: { michael@0: mWidth = aWidth; michael@0: } michael@0: void SetHeight(double aHeight) michael@0: { michael@0: mHeight = aHeight; michael@0: } michael@0: michael@0: protected: michael@0: double mX, mY, mWidth, mHeight; michael@0: }; michael@0: michael@0: class DOMRectList MOZ_FINAL : public nsIDOMClientRectList, michael@0: public nsWrapperCache michael@0: { michael@0: public: michael@0: DOMRectList(nsISupports *aParent) : mParent(aParent) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList) michael@0: michael@0: NS_DECL_NSIDOMCLIENTRECTLIST michael@0: michael@0: virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE; michael@0: michael@0: nsISupports* GetParentObject() michael@0: { michael@0: return mParent; michael@0: } michael@0: michael@0: void Append(DOMRect* aElement) { mArray.AppendElement(aElement); } michael@0: michael@0: static DOMRectList* FromSupports(nsISupports* aSupports) michael@0: { michael@0: #ifdef DEBUG michael@0: { michael@0: nsCOMPtr list_qi = do_QueryInterface(aSupports); michael@0: michael@0: // If this assertion fires the QI implementation for the object in michael@0: // question doesn't use the nsIDOMClientRectList pointer as the nsISupports michael@0: // pointer. That must be fixed, or we'll crash... michael@0: NS_ASSERTION(list_qi == static_cast(aSupports), michael@0: "Uh, fix QI!"); michael@0: } michael@0: #endif michael@0: michael@0: return static_cast(aSupports); michael@0: } michael@0: michael@0: uint32_t Length() michael@0: { michael@0: return mArray.Length(); michael@0: } michael@0: DOMRect* Item(uint32_t aIndex) michael@0: { michael@0: return mArray.SafeElementAt(aIndex); michael@0: } michael@0: DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound) michael@0: { michael@0: aFound = aIndex < mArray.Length(); michael@0: if (!aFound) { michael@0: return nullptr; michael@0: } michael@0: return mArray[aIndex]; michael@0: } michael@0: michael@0: protected: michael@0: nsTArray > mArray; michael@0: nsCOMPtr mParent; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif /*MOZILLA_DOMRECT_H_*/