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 : #include "mozilla/dom/DOMRect.h"
8 :
9 : #include "nsPresContext.h"
10 : #include "mozilla/dom/DOMRectListBinding.h"
11 : #include "mozilla/dom/DOMRectBinding.h"
12 :
13 : using namespace mozilla;
14 : using namespace mozilla::dom;
15 :
16 6 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMRectReadOnly, mParent)
17 16 : NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectReadOnly)
18 8 : NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectReadOnly)
19 31 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMRectReadOnly)
20 7 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
22 0 : NS_INTERFACE_MAP_END
23 :
24 : JSObject*
25 0 : DOMRectReadOnly::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
26 : {
27 0 : MOZ_ASSERT(mParent);
28 0 : return DOMRectReadOnlyBinding::Wrap(aCx, this, aGivenProto);
29 : }
30 :
31 : // -----------------------------------------------------------------------------
32 :
33 55 : NS_IMPL_ISUPPORTS_INHERITED(DOMRect, DOMRectReadOnly, nsIDOMClientRect)
34 :
35 : #define FORWARD_GETTER(_name) \
36 : NS_IMETHODIMP \
37 : DOMRect::Get ## _name(float* aResult) \
38 : { \
39 : *aResult = float(_name()); \
40 : return NS_OK; \
41 : }
42 :
43 0 : FORWARD_GETTER(Left)
44 0 : FORWARD_GETTER(Top)
45 0 : FORWARD_GETTER(Right)
46 0 : FORWARD_GETTER(Bottom)
47 0 : FORWARD_GETTER(Width)
48 0 : FORWARD_GETTER(Height)
49 :
50 : JSObject*
51 7 : DOMRect::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
52 : {
53 7 : MOZ_ASSERT(mParent);
54 7 : return DOMRectBinding::Wrap(aCx, this, aGivenProto);
55 : }
56 :
57 : already_AddRefed<DOMRect>
58 0 : DOMRect::Constructor(const GlobalObject& aGlobal, ErrorResult& aRV)
59 : {
60 : RefPtr<DOMRect> obj =
61 0 : new DOMRect(aGlobal.GetAsSupports(), 0.0, 0.0, 0.0, 0.0);
62 0 : return obj.forget();
63 : }
64 :
65 : already_AddRefed<DOMRect>
66 0 : DOMRect::Constructor(const GlobalObject& aGlobal, double aX, double aY,
67 : double aWidth, double aHeight, ErrorResult& aRV)
68 : {
69 : RefPtr<DOMRect> obj =
70 0 : new DOMRect(aGlobal.GetAsSupports(), aX, aY, aWidth, aHeight);
71 0 : return obj.forget();
72 : }
73 :
74 : // -----------------------------------------------------------------------------
75 :
76 3 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMRectList, mParent, mArray)
77 :
78 10 : NS_INTERFACE_TABLE_HEAD(DOMRectList)
79 10 : NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
80 9 : NS_INTERFACE_TABLE(DOMRectList, nsIDOMClientRectList)
81 9 : NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(DOMRectList)
82 0 : NS_INTERFACE_MAP_END
83 :
84 2 : NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectList)
85 1 : NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectList)
86 :
87 :
88 : NS_IMETHODIMP
89 0 : DOMRectList::GetLength(uint32_t* aLength)
90 : {
91 0 : *aLength = Length();
92 0 : return NS_OK;
93 : }
94 :
95 : NS_IMETHODIMP
96 0 : DOMRectList::Item(uint32_t aIndex, nsIDOMClientRect** aReturn)
97 : {
98 0 : NS_IF_ADDREF(*aReturn = Item(aIndex));
99 0 : return NS_OK;
100 : }
101 :
102 : JSObject*
103 1 : DOMRectList::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto)
104 : {
105 1 : return mozilla::dom::DOMRectListBinding::Wrap(cx, this, aGivenProto);
106 : }
107 :
108 : static double
109 32 : RoundFloat(double aValue)
110 : {
111 32 : return floor(aValue + 0.5);
112 : }
113 :
114 : void
115 8 : DOMRect::SetLayoutRect(const nsRect& aLayoutRect)
116 : {
117 8 : double scale = 65536.0;
118 : // Round to the nearest 1/scale units. We choose scale so it can be represented
119 : // exactly by machine floating point.
120 8 : double scaleInv = 1/scale;
121 8 : double t2pScaled = scale/nsPresContext::AppUnitsPerCSSPixel();
122 8 : double x = RoundFloat(aLayoutRect.x*t2pScaled)*scaleInv;
123 8 : double y = RoundFloat(aLayoutRect.y*t2pScaled)*scaleInv;
124 8 : SetRect(x, y, RoundFloat(aLayoutRect.XMost()*t2pScaled)*scaleInv - x,
125 16 : RoundFloat(aLayoutRect.YMost()*t2pScaled)*scaleInv - y);
126 8 : }
|