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 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMRectReadOnly, mParent)
17 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectReadOnly)
18 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectReadOnly)
19 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMRectReadOnly)
20 0 : 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 : already_AddRefed<DOMRectReadOnly>
32 0 : DOMRectReadOnly::Constructor(const GlobalObject& aGlobal, double aX, double aY,
33 : double aWidth, double aHeight, ErrorResult& aRv)
34 : {
35 : RefPtr<DOMRectReadOnly> obj =
36 0 : new DOMRectReadOnly(aGlobal.GetAsSupports(), aX, aY, aWidth, aHeight);
37 0 : return obj.forget();
38 : }
39 :
40 : // -----------------------------------------------------------------------------
41 :
42 : JSObject*
43 1 : DOMRect::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
44 : {
45 0 : MOZ_ASSERT(mParent);
46 1 : return DOMRectBinding::Wrap(aCx, this, aGivenProto);
47 : }
48 :
49 : already_AddRefed<DOMRect>
50 0 : DOMRect::Constructor(const GlobalObject& aGlobal, double aX, double aY,
51 : double aWidth, double aHeight, ErrorResult& aRv)
52 : {
53 : RefPtr<DOMRect> obj =
54 0 : new DOMRect(aGlobal.GetAsSupports(), aX, aY, aWidth, aHeight);
55 0 : return obj.forget();
56 : }
57 :
58 : // -----------------------------------------------------------------------------
59 :
60 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMRectList, mParent, mArray)
61 :
62 0 : NS_INTERFACE_TABLE_HEAD(DOMRectList)
63 0 : NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
64 0 : NS_INTERFACE_TABLE0(DOMRectList)
65 0 : NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(DOMRectList)
66 0 : NS_INTERFACE_MAP_END
67 :
68 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectList)
69 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectList)
70 :
71 : JSObject*
72 0 : DOMRectList::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto)
73 : {
74 0 : return mozilla::dom::DOMRectListBinding::Wrap(cx, this, aGivenProto);
75 : }
76 :
77 : static double
78 4 : RoundFloat(double aValue)
79 : {
80 4 : return floor(aValue + 0.5);
81 : }
82 :
83 : void
84 1 : DOMRect::SetLayoutRect(const nsRect& aLayoutRect)
85 : {
86 1 : double scale = 65536.0;
87 : // Round to the nearest 1/scale units. We choose scale so it can be represented
88 : // exactly by machine floating point.
89 0 : double scaleInv = 1/scale;
90 0 : double t2pScaled = scale/nsPresContext::AppUnitsPerCSSPixel();
91 0 : double x = RoundFloat(aLayoutRect.x*t2pScaled)*scaleInv;
92 0 : double y = RoundFloat(aLayoutRect.y*t2pScaled)*scaleInv;
93 0 : SetRect(x, y, RoundFloat(aLayoutRect.XMost()*t2pScaled)*scaleInv - x,
94 0 : RoundFloat(aLayoutRect.YMost()*t2pScaled)*scaleInv - y);
95 : }
|