1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_DOMRECT_H_
8 #define MOZILLA_DOMRECT_H_
10 #include "js/StructuredClone.h"
13 #include "nsWrapperCache.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "mozilla/Attributes.h"
16 #include "mozilla/dom/BindingDeclarations.h"
17 #include "mozilla/ErrorResult.h"
18 #include "mozilla/FloatingPoint.h"
21 class nsIGlobalObject
;
28 class DOMRectReadOnly
: public nsISupports
, public nsWrapperCache
{
30 virtual ~DOMRectReadOnly() = default;
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly
)
36 explicit DOMRectReadOnly(nsISupports
* aParent
, double aX
= 0, double aY
= 0,
37 double aWidth
= 0, double aHeight
= 0)
38 : mParent(aParent
), mX(aX
), mY(aY
), mWidth(aWidth
), mHeight(aHeight
) {}
40 nsISupports
* GetParentObject() const {
45 virtual JSObject
* WrapObject(JSContext
* aCx
,
46 JS::Handle
<JSObject
*> aGivenProto
) override
;
48 static already_AddRefed
<DOMRectReadOnly
> FromRect(const GlobalObject
& aGlobal
,
49 const DOMRectInit
& aInit
);
51 static already_AddRefed
<DOMRectReadOnly
> Constructor(
52 const GlobalObject
& aGlobal
, double aX
, double aY
, double aWidth
,
55 double X() const { return mX
; }
56 double Y() const { return mY
; }
57 double Width() const { return mWidth
; }
58 double Height() const { return mHeight
; }
61 double x
= X(), w
= Width();
62 return NaNSafeMin(x
, x
+ w
);
65 double y
= Y(), h
= Height();
66 return NaNSafeMin(y
, y
+ h
);
68 double Right() const {
69 double x
= X(), w
= Width();
70 return NaNSafeMax(x
, x
+ w
);
72 double Bottom() const {
73 double y
= Y(), h
= Height();
74 return NaNSafeMax(y
, y
+ h
);
77 bool WriteStructuredClone(JSContext
* aCx
,
78 JSStructuredCloneWriter
* aWriter
) const;
80 static already_AddRefed
<DOMRectReadOnly
> ReadStructuredClone(
81 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
82 JSStructuredCloneReader
* aReader
);
85 // Shared implementation of ReadStructuredClone for DOMRect and
87 bool ReadStructuredClone(JSStructuredCloneReader
* aReader
);
89 nsCOMPtr
<nsISupports
> mParent
;
90 double mX
, mY
, mWidth
, mHeight
;
93 class DOMRect final
: public DOMRectReadOnly
{
95 explicit DOMRect(nsISupports
* aParent
, double aX
= 0, double aY
= 0,
96 double aWidth
= 0, double aHeight
= 0)
97 : DOMRectReadOnly(aParent
, aX
, aY
, aWidth
, aHeight
) {}
99 NS_INLINE_DECL_REFCOUNTING_INHERITED(DOMRect
, DOMRectReadOnly
)
101 static already_AddRefed
<DOMRect
> FromRect(const GlobalObject
& aGlobal
,
102 const DOMRectInit
& aInit
);
104 static already_AddRefed
<DOMRect
> Constructor(const GlobalObject
& aGlobal
,
105 double aX
, double aY
,
106 double aWidth
, double aHeight
);
108 virtual JSObject
* WrapObject(JSContext
* aCx
,
109 JS::Handle
<JSObject
*> aGivenProto
) override
;
111 static already_AddRefed
<DOMRect
> ReadStructuredClone(
112 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
113 JSStructuredCloneReader
* aReader
);
114 using DOMRectReadOnly::ReadStructuredClone
;
116 void SetRect(float aX
, float aY
, float aWidth
, float aHeight
) {
122 void SetLayoutRect(const nsRect
& aLayoutRect
);
124 void SetX(double aX
) { mX
= aX
; }
125 void SetY(double aY
) { mY
= aY
; }
126 void SetWidth(double aWidth
) { mWidth
= aWidth
; }
127 void SetHeight(double aHeight
) { mHeight
= aHeight
; }
129 static DOMRect
* FromSupports(nsISupports
* aSupports
) {
130 return static_cast<DOMRect
*>(aSupports
);
134 ~DOMRect() = default;
137 class DOMRectList final
: public nsISupports
, public nsWrapperCache
{
138 ~DOMRectList() = default;
141 explicit DOMRectList(nsISupports
* aParent
) : mParent(aParent
) {}
143 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
144 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList
)
146 virtual JSObject
* WrapObject(JSContext
* cx
,
147 JS::Handle
<JSObject
*> aGivenProto
) override
;
149 nsISupports
* GetParentObject() { return mParent
; }
151 void Append(DOMRect
* aElement
) { mArray
.AppendElement(aElement
); }
153 uint32_t Length() { return mArray
.Length(); }
154 DOMRect
* Item(uint32_t aIndex
) { return mArray
.SafeElementAt(aIndex
); }
155 DOMRect
* IndexedGetter(uint32_t aIndex
, bool& aFound
) {
156 aFound
= aIndex
< mArray
.Length();
160 return mArray
[aIndex
];
164 nsTArray
<RefPtr
<DOMRect
> > mArray
;
165 nsCOMPtr
<nsISupports
> mParent
;
169 } // namespace mozilla
171 #endif /*MOZILLA_DOMRECT_H_*/