Bumping manifests a=b2g-bump
[gecko.git] / dom / base / DOMRect.h
bloba3f8510f911f8868b465bfa0f485e2e7bc49b1e2
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 #ifndef MOZILLA_DOMRECT_H_
7 #define MOZILLA_DOMRECT_H_
9 #include "nsIDOMClientRect.h"
10 #include "nsIDOMClientRectList.h"
11 #include "nsTArray.h"
12 #include "nsCOMPtr.h"
13 #include "nsAutoPtr.h"
14 #include "nsWrapperCache.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/dom/BindingDeclarations.h"
18 #include "mozilla/ErrorResult.h"
19 #include <algorithm>
21 struct nsRect;
23 namespace mozilla {
24 namespace dom {
26 class DOMRectReadOnly : public nsISupports
27 , public nsWrapperCache
29 protected:
30 virtual ~DOMRectReadOnly() {}
32 public:
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly)
36 explicit DOMRectReadOnly(nsISupports* aParent)
37 : mParent(aParent)
41 nsISupports* GetParentObject() const
43 MOZ_ASSERT(mParent);
44 return mParent;
46 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
48 virtual double X() const = 0;
49 virtual double Y() const = 0;
50 virtual double Width() const = 0;
51 virtual double Height() const = 0;
53 double Left() const
55 double x = X(), w = Width();
56 return std::min(x, x + w);
58 double Top() const
60 double y = Y(), h = Height();
61 return std::min(y, y + h);
63 double Right() const
65 double x = X(), w = Width();
66 return std::max(x, x + w);
68 double Bottom() const
70 double y = Y(), h = Height();
71 return std::max(y, y + h);
74 protected:
75 nsCOMPtr<nsISupports> mParent;
78 class DOMRect MOZ_FINAL : public DOMRectReadOnly
79 , public nsIDOMClientRect
81 public:
82 explicit DOMRect(nsISupports* aParent, double aX = 0, double aY = 0,
83 double aWidth = 0, double aHeight = 0)
84 : DOMRectReadOnly(aParent)
85 , mX(aX)
86 , mY(aY)
87 , mWidth(aWidth)
88 , mHeight(aHeight)
92 NS_DECL_ISUPPORTS_INHERITED
93 NS_DECL_NSIDOMCLIENTRECT
95 static already_AddRefed<DOMRect>
96 Constructor(const GlobalObject& aGlobal, ErrorResult& aRV);
97 static already_AddRefed<DOMRect>
98 Constructor(const GlobalObject& aGlobal, double aX, double aY,
99 double aWidth, double aHeight, ErrorResult& aRV);
101 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
103 void SetRect(float aX, float aY, float aWidth, float aHeight) {
104 mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight;
106 void SetLayoutRect(const nsRect& aLayoutRect);
108 virtual double X() const MOZ_OVERRIDE
110 return mX;
112 virtual double Y() const MOZ_OVERRIDE
114 return mY;
116 virtual double Width() const MOZ_OVERRIDE
118 return mWidth;
120 virtual double Height() const MOZ_OVERRIDE
122 return mHeight;
125 void SetX(double aX)
127 mX = aX;
129 void SetY(double aY)
131 mY = aY;
133 void SetWidth(double aWidth)
135 mWidth = aWidth;
137 void SetHeight(double aHeight)
139 mHeight = aHeight;
142 protected:
143 double mX, mY, mWidth, mHeight;
145 private:
146 ~DOMRect() {};
149 class DOMRectList MOZ_FINAL : public nsIDOMClientRectList,
150 public nsWrapperCache
152 ~DOMRectList() {}
154 public:
155 explicit DOMRectList(nsISupports *aParent) : mParent(aParent)
159 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
160 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList)
162 NS_DECL_NSIDOMCLIENTRECTLIST
164 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
166 nsISupports* GetParentObject()
168 return mParent;
171 void Append(DOMRect* aElement) { mArray.AppendElement(aElement); }
173 static DOMRectList* FromSupports(nsISupports* aSupports)
175 #ifdef DEBUG
177 nsCOMPtr<nsIDOMClientRectList> list_qi = do_QueryInterface(aSupports);
179 // If this assertion fires the QI implementation for the object in
180 // question doesn't use the nsIDOMClientRectList pointer as the nsISupports
181 // pointer. That must be fixed, or we'll crash...
182 NS_ASSERTION(list_qi == static_cast<nsIDOMClientRectList*>(aSupports),
183 "Uh, fix QI!");
185 #endif
187 return static_cast<DOMRectList*>(aSupports);
190 uint32_t Length()
192 return mArray.Length();
194 DOMRect* Item(uint32_t aIndex)
196 return mArray.SafeElementAt(aIndex);
198 DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound)
200 aFound = aIndex < mArray.Length();
201 if (!aFound) {
202 return nullptr;
204 return mArray[aIndex];
207 protected:
208 nsTArray<nsRefPtr<DOMRect> > mArray;
209 nsCOMPtr<nsISupports> mParent;
216 #endif /*MOZILLA_DOMRECT_H_*/