Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / svg / SVGAnimatedPreserveAspectRatio.h
blobcc4721e42f63e86b7f85df8adc8479869a3bdb52
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/. */
7 #ifndef DOM_SVG_SVGANIMATEDPRESERVEASPECTRATIO_H_
8 #define DOM_SVG_SVGANIMATEDPRESERVEASPECTRATIO_H_
10 #include "nsCycleCollectionParticipant.h"
11 #include "nsError.h"
12 #include "mozilla/SVGPreserveAspectRatio.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/SMILAttr.h"
15 #include "mozilla/UniquePtr.h"
16 #include "mozilla/dom/SVGElement.h"
18 namespace mozilla {
20 class SMILValue;
22 namespace dom {
23 class DOMSVGAnimatedPreserveAspectRatio;
24 class SVGAnimationElement;
25 } // namespace dom
27 class SVGAnimatedPreserveAspectRatio final {
28 friend class AutoChangePreserveAspectRatioNotifier;
30 public:
31 void Init() {
32 mBaseVal.mAlign =
33 dom::SVGPreserveAspectRatio_Binding::SVG_PRESERVEASPECTRATIO_XMIDYMID;
34 mBaseVal.mMeetOrSlice =
35 dom::SVGPreserveAspectRatio_Binding::SVG_MEETORSLICE_MEET;
36 mAnimVal = mBaseVal;
37 mIsAnimated = false;
38 mIsBaseSet = false;
41 nsresult SetBaseValueString(const nsAString& aValue,
42 dom::SVGElement* aSVGElement, bool aDoSetAttr);
43 void GetBaseValueString(nsAString& aValue) const;
45 void SetBaseValue(const SVGPreserveAspectRatio& aValue,
46 dom::SVGElement* aSVGElement);
47 nsresult SetBaseAlign(uint16_t aAlign, dom::SVGElement* aSVGElement) {
48 if (aAlign < SVG_ALIGN_MIN_VALID || aAlign > SVG_ALIGN_MAX_VALID) {
49 return NS_ERROR_FAILURE;
51 SetBaseValue(SVGPreserveAspectRatio(static_cast<uint8_t>(aAlign),
52 mBaseVal.GetMeetOrSlice()),
53 aSVGElement);
54 return NS_OK;
56 nsresult SetBaseMeetOrSlice(uint16_t aMeetOrSlice,
57 dom::SVGElement* aSVGElement) {
58 if (aMeetOrSlice < SVG_MEETORSLICE_MIN_VALID ||
59 aMeetOrSlice > SVG_MEETORSLICE_MAX_VALID) {
60 return NS_ERROR_FAILURE;
62 SetBaseValue(SVGPreserveAspectRatio(mBaseVal.GetAlign(),
63 static_cast<uint8_t>(aMeetOrSlice)),
64 aSVGElement);
65 return NS_OK;
67 void SetAnimValue(uint64_t aPackedValue, dom::SVGElement* aSVGElement);
69 const SVGPreserveAspectRatio& GetBaseValue() const { return mBaseVal; }
70 const SVGPreserveAspectRatio& GetAnimValue() const { return mAnimVal; }
71 bool IsAnimated() const { return mIsAnimated; }
72 bool IsExplicitlySet() const { return mIsAnimated || mIsBaseSet; }
74 already_AddRefed<dom::DOMSVGAnimatedPreserveAspectRatio>
75 ToDOMAnimatedPreserveAspectRatio(dom::SVGElement* aSVGElement);
76 UniquePtr<SMILAttr> ToSMILAttr(dom::SVGElement* aSVGElement);
78 private:
79 SVGPreserveAspectRatio mAnimVal;
80 SVGPreserveAspectRatio mBaseVal;
81 bool mIsAnimated;
82 bool mIsBaseSet;
84 public:
85 struct SMILPreserveAspectRatio final : public SMILAttr {
86 public:
87 SMILPreserveAspectRatio(SVGAnimatedPreserveAspectRatio* aVal,
88 dom::SVGElement* aSVGElement)
89 : mVal(aVal), mSVGElement(aSVGElement) {}
91 // These will stay alive because a SMILAttr only lives as long
92 // as the Compositing step, and DOM elements don't get a chance to
93 // die during that.
94 SVGAnimatedPreserveAspectRatio* mVal;
95 dom::SVGElement* mSVGElement;
97 // SMILAttr methods
98 nsresult ValueFromString(const nsAString& aStr,
99 const dom::SVGAnimationElement* aSrcElement,
100 SMILValue& aValue,
101 bool& aPreventCachingOfSandwich) const override;
102 SMILValue GetBaseValue() const override;
103 void ClearAnimValue() override;
104 nsresult SetAnimValue(const SMILValue& aValue) override;
108 namespace dom {
109 class DOMSVGAnimatedPreserveAspectRatio final : public nsWrapperCache {
110 ~DOMSVGAnimatedPreserveAspectRatio();
112 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(
113 DOMSVGAnimatedPreserveAspectRatio)
114 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(
115 DOMSVGAnimatedPreserveAspectRatio)
117 DOMSVGAnimatedPreserveAspectRatio(SVGAnimatedPreserveAspectRatio* aVal,
118 dom::SVGElement* aSVGElement)
119 : mVal(aVal), mSVGElement(aSVGElement) {}
121 // WebIDL
122 dom::SVGElement* GetParentObject() const { return mSVGElement; }
123 virtual JSObject* WrapObject(JSContext* aCx,
124 JS::Handle<JSObject*> aGivenProto) override;
126 // These aren't weak refs because new objects are returned each time
127 already_AddRefed<DOMSVGPreserveAspectRatio> BaseVal();
128 already_AddRefed<DOMSVGPreserveAspectRatio> AnimVal();
130 protected:
131 // kept alive because it belongs to content:
132 SVGAnimatedPreserveAspectRatio* mVal;
133 RefPtr<dom::SVGElement> mSVGElement;
136 } // namespace dom
137 } // namespace mozilla
139 #endif // DOM_SVG_SVGANIMATEDPRESERVEASPECTRATIO_H_