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"
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"
26 class DOMRectReadOnly
: public nsISupports
27 , public nsWrapperCache
30 virtual ~DOMRectReadOnly() {}
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly
)
36 explicit DOMRectReadOnly(nsISupports
* aParent
)
41 nsISupports
* GetParentObject() const
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;
55 double x
= X(), w
= Width();
56 return std::min(x
, x
+ w
);
60 double y
= Y(), h
= Height();
61 return std::min(y
, y
+ h
);
65 double x
= X(), w
= Width();
66 return std::max(x
, x
+ w
);
70 double y
= Y(), h
= Height();
71 return std::max(y
, y
+ h
);
75 nsCOMPtr
<nsISupports
> mParent
;
78 class DOMRect MOZ_FINAL
: public DOMRectReadOnly
79 , public nsIDOMClientRect
82 explicit DOMRect(nsISupports
* aParent
, double aX
= 0, double aY
= 0,
83 double aWidth
= 0, double aHeight
= 0)
84 : DOMRectReadOnly(aParent
)
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
112 virtual double Y() const MOZ_OVERRIDE
116 virtual double Width() const MOZ_OVERRIDE
120 virtual double Height() const MOZ_OVERRIDE
133 void SetWidth(double aWidth
)
137 void SetHeight(double aHeight
)
143 double mX
, mY
, mWidth
, mHeight
;
149 class DOMRectList MOZ_FINAL
: public nsIDOMClientRectList
,
150 public nsWrapperCache
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()
171 void Append(DOMRect
* aElement
) { mArray
.AppendElement(aElement
); }
173 static DOMRectList
* FromSupports(nsISupports
* aSupports
)
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
),
187 return static_cast<DOMRectList
*>(aSupports
);
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();
204 return mArray
[aIndex
];
208 nsTArray
<nsRefPtr
<DOMRect
> > mArray
;
209 nsCOMPtr
<nsISupports
> mParent
;
216 #endif /*MOZILLA_DOMRECT_H_*/