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_
14 #include "js/TypeDecls.h"
15 #include "mozilla/AlreadyAddRefed.h"
16 #include "mozilla/Assertions.h"
17 #include "mozilla/FloatingPoint.h"
18 #include "mozilla/RefPtr.h"
20 #include "nsCycleCollectionParticipant.h"
21 #include "nsISupports.h"
23 #include "nsWrapperCache.h"
26 class nsIGlobalObject
;
28 struct JSStructuredCloneReader
;
29 struct JSStructuredCloneWriter
;
32 namespace mozilla::dom
{
37 class DOMRectReadOnly
: public nsISupports
, public nsWrapperCache
{
39 virtual ~DOMRectReadOnly() = default;
42 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
43 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DOMRectReadOnly
)
45 explicit DOMRectReadOnly(nsISupports
* aParent
, double aX
= 0, double aY
= 0,
46 double aWidth
= 0, double aHeight
= 0)
47 : mParent(aParent
), mX(aX
), mY(aY
), mWidth(aWidth
), mHeight(aHeight
) {}
49 nsISupports
* GetParentObject() const {
54 virtual JSObject
* WrapObject(JSContext
* aCx
,
55 JS::Handle
<JSObject
*> aGivenProto
) override
;
57 static already_AddRefed
<DOMRectReadOnly
> FromRect(const GlobalObject
& aGlobal
,
58 const DOMRectInit
& aInit
);
60 static already_AddRefed
<DOMRectReadOnly
> Constructor(
61 const GlobalObject
& aGlobal
, double aX
, double aY
, double aWidth
,
64 double X() const { return mX
; }
65 double Y() const { return mY
; }
66 double Width() const { return mWidth
; }
67 double Height() const { return mHeight
; }
70 double x
= X(), w
= Width();
71 return NaNSafeMin(x
, x
+ w
);
74 double y
= Y(), h
= Height();
75 return NaNSafeMin(y
, y
+ h
);
77 double Right() const {
78 double x
= X(), w
= Width();
79 return NaNSafeMax(x
, x
+ w
);
81 double Bottom() const {
82 double y
= Y(), h
= Height();
83 return NaNSafeMax(y
, y
+ h
);
86 bool WriteStructuredClone(JSContext
* aCx
,
87 JSStructuredCloneWriter
* aWriter
) const;
89 static already_AddRefed
<DOMRectReadOnly
> ReadStructuredClone(
90 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
91 JSStructuredCloneReader
* aReader
);
94 // Shared implementation of ReadStructuredClone for DOMRect and
96 bool ReadStructuredClone(JSStructuredCloneReader
* aReader
);
98 nsCOMPtr
<nsISupports
> mParent
;
99 double mX
, mY
, mWidth
, mHeight
;
102 class DOMRect final
: public DOMRectReadOnly
{
104 explicit DOMRect(nsISupports
* aParent
, double aX
= 0, double aY
= 0,
105 double aWidth
= 0, double aHeight
= 0)
106 : DOMRectReadOnly(aParent
, aX
, aY
, aWidth
, aHeight
) {}
108 NS_INLINE_DECL_REFCOUNTING_INHERITED(DOMRect
, DOMRectReadOnly
)
110 static already_AddRefed
<DOMRect
> FromRect(const GlobalObject
& aGlobal
,
111 const DOMRectInit
& aInit
);
113 static already_AddRefed
<DOMRect
> Constructor(const GlobalObject
& aGlobal
,
114 double aX
, double aY
,
115 double aWidth
, double aHeight
);
117 virtual JSObject
* WrapObject(JSContext
* aCx
,
118 JS::Handle
<JSObject
*> aGivenProto
) override
;
120 static already_AddRefed
<DOMRect
> ReadStructuredClone(
121 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
122 JSStructuredCloneReader
* aReader
);
123 using DOMRectReadOnly::ReadStructuredClone
;
125 void SetRect(float aX
, float aY
, float aWidth
, float aHeight
) {
131 void SetLayoutRect(const nsRect
& aLayoutRect
);
133 void SetX(double aX
) { mX
= aX
; }
134 void SetY(double aY
) { mY
= aY
; }
135 void SetWidth(double aWidth
) { mWidth
= aWidth
; }
136 void SetHeight(double aHeight
) { mHeight
= aHeight
; }
138 static DOMRect
* FromSupports(nsISupports
* aSupports
) {
139 return static_cast<DOMRect
*>(aSupports
);
143 ~DOMRect() = default;
146 class DOMRectList final
: public nsISupports
, public nsWrapperCache
{
147 ~DOMRectList() = default;
150 explicit DOMRectList(nsISupports
* aParent
) : mParent(aParent
) {}
152 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
153 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DOMRectList
)
155 virtual JSObject
* WrapObject(JSContext
* cx
,
156 JS::Handle
<JSObject
*> aGivenProto
) override
;
158 nsISupports
* GetParentObject() { return mParent
; }
160 void Append(DOMRect
* aElement
) { mArray
.AppendElement(aElement
); }
162 uint32_t Length() { return mArray
.Length(); }
163 DOMRect
* Item(uint32_t aIndex
) { return mArray
.SafeElementAt(aIndex
); }
164 DOMRect
* IndexedGetter(uint32_t aIndex
, bool& aFound
) {
165 aFound
= aIndex
< mArray
.Length();
169 return mArray
[aIndex
];
173 nsTArray
<RefPtr
<DOMRect
> > mArray
;
174 nsCOMPtr
<nsISupports
> mParent
;
177 } // namespace mozilla::dom
179 #endif /*MOZILLA_DOMRECT_H_*/