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/SVGRect.h"
8
9 #include "mozilla/dom/SVGRectBinding.h"
10 #include "mozilla/dom/SVGSVGElement.h"
11 #include "SVGAnimatedViewBox.h"
12 #include "nsWrapperCache.h"
13
14 using namespace mozilla::gfx;
15
16 namespace mozilla {
17 namespace dom {
18
19 //----------------------------------------------------------------------
20 // nsISupports methods:
21
NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(SVGRect,mParent)22 NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(SVGRect, mParent)
23
24 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(SVGRect, AddRef)
25 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(SVGRect, Release)
26
27 //----------------------------------------------------------------------
28 // implementation:
29
30 SVGRect::SVGRect(SVGSVGElement* aSVGElement)
31 : mVal(nullptr), mParent(aSVGElement), mType(RectType::CreatedValue) {
32 MOZ_ASSERT(mParent);
33 mRect = gfx::Rect(0, 0, 0, 0);
34 }
35
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)36 JSObject* SVGRect::WrapObject(JSContext* aCx,
37 JS::Handle<JSObject*> aGivenProto) {
38 MOZ_ASSERT(mParent);
39 return SVGRect_Binding::Wrap(aCx, this, aGivenProto);
40 }
41
X()42 float SVGRect::X() {
43 switch (mType) {
44 case RectType::AnimValue:
45 static_cast<SVGElement*>(mParent->AsElement())->FlushAnimations();
46 return mVal->GetAnimValue().x;
47 case RectType::BaseValue:
48 return mVal->GetBaseValue().x;
49 default:
50 return mRect.x;
51 }
52 }
53
Y()54 float SVGRect::Y() {
55 switch (mType) {
56 case RectType::AnimValue:
57 static_cast<SVGElement*>(mParent->AsElement())->FlushAnimations();
58 return mVal->GetAnimValue().y;
59 case RectType::BaseValue:
60 return mVal->GetBaseValue().y;
61 default:
62 return mRect.y;
63 }
64 }
65
Width()66 float SVGRect::Width() {
67 switch (mType) {
68 case RectType::AnimValue:
69 static_cast<SVGElement*>(mParent->AsElement())->FlushAnimations();
70 return mVal->GetAnimValue().width;
71 case RectType::BaseValue:
72 return mVal->GetBaseValue().width;
73 default:
74 return mRect.width;
75 }
76 }
77
Height()78 float SVGRect::Height() {
79 switch (mType) {
80 case RectType::AnimValue:
81 static_cast<SVGElement*>(mParent->AsElement())->FlushAnimations();
82 return mVal->GetAnimValue().height;
83 case RectType::BaseValue:
84 return mVal->GetBaseValue().height;
85 default:
86 return mRect.height;
87 }
88 }
89
SetX(float aX,ErrorResult & aRv)90 void SVGRect::SetX(float aX, ErrorResult& aRv) {
91 switch (mType) {
92 case RectType::AnimValue:
93 aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
94 return;
95 case RectType::BaseValue: {
96 SVGViewBox rect = mVal->GetBaseValue();
97 rect.x = aX;
98 mVal->SetBaseValue(rect, static_cast<SVGElement*>(mParent->AsElement()));
99 return;
100 }
101 default:
102 mRect.x = aX;
103 }
104 }
105
SetY(float aY,ErrorResult & aRv)106 void SVGRect::SetY(float aY, ErrorResult& aRv) {
107 switch (mType) {
108 case RectType::AnimValue:
109 aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
110 return;
111 case RectType::BaseValue: {
112 SVGViewBox rect = mVal->GetBaseValue();
113 rect.y = aY;
114 mVal->SetBaseValue(rect, static_cast<SVGElement*>(mParent->AsElement()));
115 return;
116 }
117 default:
118 mRect.y = aY;
119 }
120 }
121
SetWidth(float aWidth,ErrorResult & aRv)122 void SVGRect::SetWidth(float aWidth, ErrorResult& aRv) {
123 switch (mType) {
124 case RectType::AnimValue:
125 aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
126 return;
127 case RectType::BaseValue: {
128 SVGViewBox rect = mVal->GetBaseValue();
129 rect.width = aWidth;
130 mVal->SetBaseValue(rect, static_cast<SVGElement*>(mParent->AsElement()));
131 return;
132 }
133 default:
134 mRect.width = aWidth;
135 }
136 }
137
SetHeight(float aHeight,ErrorResult & aRv)138 void SVGRect::SetHeight(float aHeight, ErrorResult& aRv) {
139 switch (mType) {
140 case RectType::AnimValue:
141 aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
142 return;
143 case RectType::BaseValue: {
144 SVGViewBox rect = mVal->GetBaseValue();
145 rect.height = aHeight;
146 mVal->SetBaseValue(rect, static_cast<SVGElement*>(mParent->AsElement()));
147 return;
148 }
149 default:
150 mRect.height = aHeight;
151 }
152 }
153
154 } // namespace dom
155 } // namespace mozilla
156