Backed out changeset 58dbd2146e24 (bug 944961) for bustage.
[gecko.git] / content / base / src / DOMRect.h
blobae3db528683cbb64e579ce9a9be89a6e0aa3b8a7
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"
18 struct nsRect;
20 namespace mozilla {
21 namespace dom {
23 class DOMRect MOZ_FINAL : public nsIDOMClientRect
24 , public nsWrapperCache
26 public:
27 DOMRect(nsISupports* aParent)
28 : mParent(aParent), mX(0.0), mY(0.0), mWidth(0.0), mHeight(0.0)
30 SetIsDOMBinding();
32 virtual ~DOMRect() {}
34 void SetRect(float aX, float aY, float aWidth, float aHeight) {
35 mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight;
37 void SetLayoutRect(const nsRect& aLayoutRect);
40 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
41 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRect)
42 NS_DECL_NSIDOMCLIENTRECT
45 nsISupports* GetParentObject() const
47 MOZ_ASSERT(mParent);
48 return mParent;
50 virtual JSObject* WrapObject(JSContext* aCx,
51 JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
54 float Left() const
56 return mX;
59 float Top() const
61 return mY;
64 float Right() const
66 return mX + mWidth;
69 float Bottom() const
71 return mY + mHeight;
74 float Width() const
76 return mWidth;
79 float Height() const
81 return mHeight;
84 protected:
85 nsCOMPtr<nsISupports> mParent;
86 float mX, mY, mWidth, mHeight;
89 class DOMRectList MOZ_FINAL : public nsIDOMClientRectList,
90 public nsWrapperCache
92 public:
93 DOMRectList(nsISupports *aParent) : mParent(aParent)
95 SetIsDOMBinding();
98 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
99 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList)
101 NS_DECL_NSIDOMCLIENTRECTLIST
103 virtual JSObject* WrapObject(JSContext *cx,
104 JS::Handle<JSObject*> scope) MOZ_OVERRIDE;
106 nsISupports* GetParentObject()
108 return mParent;
111 void Append(DOMRect* aElement) { mArray.AppendElement(aElement); }
113 static DOMRectList* FromSupports(nsISupports* aSupports)
115 #ifdef DEBUG
117 nsCOMPtr<nsIDOMClientRectList> list_qi = do_QueryInterface(aSupports);
119 // If this assertion fires the QI implementation for the object in
120 // question doesn't use the nsIDOMClientRectList pointer as the nsISupports
121 // pointer. That must be fixed, or we'll crash...
122 NS_ASSERTION(list_qi == static_cast<nsIDOMClientRectList*>(aSupports),
123 "Uh, fix QI!");
125 #endif
127 return static_cast<DOMRectList*>(aSupports);
130 uint32_t Length()
132 return mArray.Length();
134 DOMRect* Item(uint32_t aIndex)
136 return mArray.SafeElementAt(aIndex);
138 DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound)
140 aFound = aIndex < mArray.Length();
141 if (!aFound) {
142 return nullptr;
144 return mArray[aIndex];
147 protected:
148 virtual ~DOMRectList() {}
150 nsTArray< nsRefPtr<DOMRect> > mArray;
151 nsCOMPtr<nsISupports> mParent;
157 #endif /*MOZILLA_DOMRECT_H_*/