Line data Source code
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/. */
6 :
7 : #ifndef MOZILLA_DOMRECT_H_
8 : #define MOZILLA_DOMRECT_H_
9 :
10 : #include "nsTArray.h"
11 : #include "nsCOMPtr.h"
12 : #include "nsWrapperCache.h"
13 : #include "nsCycleCollectionParticipant.h"
14 : #include "mozilla/Attributes.h"
15 : #include "mozilla/dom/BindingDeclarations.h"
16 : #include "mozilla/ErrorResult.h"
17 : #include <algorithm>
18 :
19 : struct nsRect;
20 :
21 : namespace mozilla {
22 : namespace dom {
23 :
24 : class DOMRectReadOnly : public nsISupports
25 : , public nsWrapperCache
26 : {
27 : protected:
28 0 : virtual ~DOMRectReadOnly() {}
29 :
30 : public:
31 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
32 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly)
33 :
34 0 : explicit DOMRectReadOnly(nsISupports* aParent, double aX = 0, double aY = 0,
35 : double aWidth = 0, double aHeight = 0)
36 1 : : mParent(aParent)
37 : , mX(aX)
38 : , mY(aY)
39 : , mWidth(aWidth)
40 3 : , mHeight(aHeight)
41 : {
42 0 : }
43 :
44 1 : nsISupports* GetParentObject() const
45 : {
46 2 : MOZ_ASSERT(mParent);
47 2 : return mParent;
48 : }
49 :
50 : virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
51 :
52 : static already_AddRefed<DOMRectReadOnly>
53 : Constructor(const GlobalObject& aGlobal, double aX, double aY,
54 : double aWidth, double aHeight, ErrorResult& aRv);
55 :
56 : double X() const
57 : {
58 : return mX;
59 : }
60 : double Y() const
61 : {
62 : return mY;
63 : }
64 : double Width() const
65 : {
66 : return mWidth;
67 : }
68 : double Height() const
69 : {
70 : return mHeight;
71 : }
72 :
73 0 : double Left() const
74 : {
75 0 : double x = X(), w = Width();
76 0 : return std::min(x, x + w);
77 : }
78 0 : double Top() const
79 : {
80 0 : double y = Y(), h = Height();
81 0 : return std::min(y, y + h);
82 : }
83 0 : double Right() const
84 : {
85 0 : double x = X(), w = Width();
86 0 : return std::max(x, x + w);
87 : }
88 0 : double Bottom() const
89 : {
90 0 : double y = Y(), h = Height();
91 0 : return std::max(y, y + h);
92 : }
93 :
94 : protected:
95 : nsCOMPtr<nsISupports> mParent;
96 : double mX, mY, mWidth, mHeight;
97 : };
98 :
99 : class DOMRect final : public DOMRectReadOnly
100 : {
101 : public:
102 1 : explicit DOMRect(nsISupports* aParent, double aX = 0, double aY = 0,
103 : double aWidth = 0, double aHeight = 0)
104 0 : : DOMRectReadOnly(aParent, aX, aY, aWidth, aHeight)
105 : {
106 0 : }
107 :
108 0 : NS_INLINE_DECL_REFCOUNTING_INHERITED(DOMRect, DOMRectReadOnly)
109 :
110 : static already_AddRefed<DOMRect>
111 : Constructor(const GlobalObject& aGlobal, double aX, double aY,
112 : double aWidth, double aHeight, ErrorResult& aRv);
113 :
114 : virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
115 :
116 : void SetRect(float aX, float aY, float aWidth, float aHeight) {
117 1 : mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight;
118 : }
119 : void SetLayoutRect(const nsRect& aLayoutRect);
120 :
121 : void SetX(double aX)
122 : {
123 0 : mX = aX;
124 : }
125 : void SetY(double aY)
126 : {
127 0 : mY = aY;
128 : }
129 : void SetWidth(double aWidth)
130 : {
131 0 : mWidth = aWidth;
132 : }
133 : void SetHeight(double aHeight)
134 : {
135 0 : mHeight = aHeight;
136 : }
137 :
138 : static DOMRect* FromSupports(nsISupports* aSupports)
139 : {
140 : return static_cast<DOMRect*>(aSupports);
141 : }
142 :
143 : private:
144 0 : ~DOMRect() {}
145 : };
146 :
147 : class DOMRectList final : public nsISupports,
148 : public nsWrapperCache
149 : {
150 0 : ~DOMRectList() {}
151 :
152 : public:
153 0 : explicit DOMRectList(nsISupports *aParent) : mParent(aParent)
154 : {
155 0 : }
156 :
157 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
158 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList)
159 :
160 : virtual JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;
161 :
162 : nsISupports* GetParentObject()
163 : {
164 0 : return mParent;
165 : }
166 :
167 0 : void Append(DOMRect* aElement) { mArray.AppendElement(aElement); }
168 :
169 : uint32_t Length()
170 : {
171 0 : return mArray.Length();
172 : }
173 : DOMRect* Item(uint32_t aIndex)
174 : {
175 0 : return mArray.SafeElementAt(aIndex);
176 : }
177 0 : DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound)
178 : {
179 0 : aFound = aIndex < mArray.Length();
180 0 : if (!aFound) {
181 : return nullptr;
182 : }
183 0 : return mArray[aIndex];
184 : }
185 :
186 : protected:
187 : nsTArray<RefPtr<DOMRect> > mArray;
188 : nsCOMPtr<nsISupports> mParent;
189 : };
190 :
191 : } // namespace dom
192 : } // namespace mozilla
193 :
194 : #endif /*MOZILLA_DOMRECT_H_*/
|