|
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/. */ |
|
5 |
|
6 #ifndef MOZILLA_DOMRECT_H_ |
|
7 #define MOZILLA_DOMRECT_H_ |
|
8 |
|
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" |
|
17 #include "mozilla/dom/BindingDeclarations.h" |
|
18 #include "mozilla/ErrorResult.h" |
|
19 #include <algorithm> |
|
20 |
|
21 struct nsRect; |
|
22 |
|
23 namespace mozilla { |
|
24 namespace dom { |
|
25 |
|
26 class DOMRectReadOnly : public nsISupports |
|
27 , public nsWrapperCache |
|
28 { |
|
29 public: |
|
30 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
31 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly) |
|
32 |
|
33 virtual ~DOMRectReadOnly() {} |
|
34 |
|
35 DOMRectReadOnly(nsISupports* aParent) |
|
36 : mParent(aParent) |
|
37 { |
|
38 SetIsDOMBinding(); |
|
39 } |
|
40 |
|
41 nsISupports* GetParentObject() const |
|
42 { |
|
43 MOZ_ASSERT(mParent); |
|
44 return mParent; |
|
45 } |
|
46 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
47 |
|
48 virtual double X() const = 0; |
|
49 virtual double Y() const = 0; |
|
50 virtual double Width() const = 0; |
|
51 virtual double Height() const = 0; |
|
52 |
|
53 double Left() const |
|
54 { |
|
55 double x = X(), w = Width(); |
|
56 return std::min(x, x + w); |
|
57 } |
|
58 double Top() const |
|
59 { |
|
60 double y = Y(), h = Height(); |
|
61 return std::min(y, y + h); |
|
62 } |
|
63 double Right() const |
|
64 { |
|
65 double x = X(), w = Width(); |
|
66 return std::max(x, x + w); |
|
67 } |
|
68 double Bottom() const |
|
69 { |
|
70 double y = Y(), h = Height(); |
|
71 return std::max(y, y + h); |
|
72 } |
|
73 |
|
74 protected: |
|
75 nsCOMPtr<nsISupports> mParent; |
|
76 }; |
|
77 |
|
78 class DOMRect MOZ_FINAL : public DOMRectReadOnly |
|
79 , public nsIDOMClientRect |
|
80 { |
|
81 public: |
|
82 DOMRect(nsISupports* aParent, double aX = 0, double aY = 0, |
|
83 double aWidth = 0, double aHeight = 0) |
|
84 : DOMRectReadOnly(aParent) |
|
85 , mX(aX) |
|
86 , mY(aY) |
|
87 , mWidth(aWidth) |
|
88 , mHeight(aHeight) |
|
89 { |
|
90 } |
|
91 |
|
92 NS_DECL_ISUPPORTS_INHERITED |
|
93 NS_DECL_NSIDOMCLIENTRECT |
|
94 |
|
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); |
|
100 |
|
101 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
102 |
|
103 void SetRect(float aX, float aY, float aWidth, float aHeight) { |
|
104 mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight; |
|
105 } |
|
106 void SetLayoutRect(const nsRect& aLayoutRect); |
|
107 |
|
108 virtual double X() const MOZ_OVERRIDE |
|
109 { |
|
110 return mX; |
|
111 } |
|
112 virtual double Y() const MOZ_OVERRIDE |
|
113 { |
|
114 return mY; |
|
115 } |
|
116 virtual double Width() const MOZ_OVERRIDE |
|
117 { |
|
118 return mWidth; |
|
119 } |
|
120 virtual double Height() const MOZ_OVERRIDE |
|
121 { |
|
122 return mHeight; |
|
123 } |
|
124 |
|
125 void SetX(double aX) |
|
126 { |
|
127 mX = aX; |
|
128 } |
|
129 void SetY(double aY) |
|
130 { |
|
131 mY = aY; |
|
132 } |
|
133 void SetWidth(double aWidth) |
|
134 { |
|
135 mWidth = aWidth; |
|
136 } |
|
137 void SetHeight(double aHeight) |
|
138 { |
|
139 mHeight = aHeight; |
|
140 } |
|
141 |
|
142 protected: |
|
143 double mX, mY, mWidth, mHeight; |
|
144 }; |
|
145 |
|
146 class DOMRectList MOZ_FINAL : public nsIDOMClientRectList, |
|
147 public nsWrapperCache |
|
148 { |
|
149 public: |
|
150 DOMRectList(nsISupports *aParent) : mParent(aParent) |
|
151 { |
|
152 SetIsDOMBinding(); |
|
153 } |
|
154 |
|
155 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
156 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList) |
|
157 |
|
158 NS_DECL_NSIDOMCLIENTRECTLIST |
|
159 |
|
160 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE; |
|
161 |
|
162 nsISupports* GetParentObject() |
|
163 { |
|
164 return mParent; |
|
165 } |
|
166 |
|
167 void Append(DOMRect* aElement) { mArray.AppendElement(aElement); } |
|
168 |
|
169 static DOMRectList* FromSupports(nsISupports* aSupports) |
|
170 { |
|
171 #ifdef DEBUG |
|
172 { |
|
173 nsCOMPtr<nsIDOMClientRectList> list_qi = do_QueryInterface(aSupports); |
|
174 |
|
175 // If this assertion fires the QI implementation for the object in |
|
176 // question doesn't use the nsIDOMClientRectList pointer as the nsISupports |
|
177 // pointer. That must be fixed, or we'll crash... |
|
178 NS_ASSERTION(list_qi == static_cast<nsIDOMClientRectList*>(aSupports), |
|
179 "Uh, fix QI!"); |
|
180 } |
|
181 #endif |
|
182 |
|
183 return static_cast<DOMRectList*>(aSupports); |
|
184 } |
|
185 |
|
186 uint32_t Length() |
|
187 { |
|
188 return mArray.Length(); |
|
189 } |
|
190 DOMRect* Item(uint32_t aIndex) |
|
191 { |
|
192 return mArray.SafeElementAt(aIndex); |
|
193 } |
|
194 DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound) |
|
195 { |
|
196 aFound = aIndex < mArray.Length(); |
|
197 if (!aFound) { |
|
198 return nullptr; |
|
199 } |
|
200 return mArray[aIndex]; |
|
201 } |
|
202 |
|
203 protected: |
|
204 nsTArray<nsRefPtr<DOMRect> > mArray; |
|
205 nsCOMPtr<nsISupports> mParent; |
|
206 }; |
|
207 |
|
208 } |
|
209 } |
|
210 |
|
211 #endif /*MOZILLA_DOMRECT_H_*/ |