Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / svg / SVGAnimatedEnumeration.h
blob11e6dfe9883df08b48de13e23916685f27bb4fce
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_SVGANIMATEDENUMERATION_H_
8 #define DOM_SVG_SVGANIMATEDENUMERATION_H_
10 #include "DOMSVGAnimatedEnumeration.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsError.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/SMILAttr.h"
15 #include "mozilla/UniquePtr.h"
16 #include "mozilla/dom/SVGElement.h"
18 class nsAtom;
20 namespace mozilla {
22 class SMILValue;
24 namespace dom {
25 class SVGAnimationElement;
26 } // namespace dom
28 using SVGEnumValue = uint8_t;
30 struct SVGEnumMapping {
31 nsStaticAtom* const mKey;
32 const SVGEnumValue mVal;
35 class SVGAnimatedEnumeration {
36 public:
37 friend class AutoChangeEnumNotifier;
38 using SVGElement = dom::SVGElement;
40 void Init(uint8_t aAttrEnum, uint16_t aValue) {
41 mAnimVal = mBaseVal = uint8_t(aValue);
42 mAttrEnum = aAttrEnum;
43 mIsAnimated = false;
44 mIsBaseSet = false;
47 // Returns whether aValue corresponded to a key in our mapping (in which case
48 // we actually set the base value) or not (in which case we did not).
49 bool SetBaseValueAtom(const nsAtom* aValue, SVGElement* aSVGElement);
50 nsAtom* GetBaseValueAtom(SVGElement* aSVGElement);
51 void SetBaseValue(uint16_t aValue, SVGElement* aSVGElement, ErrorResult& aRv);
52 uint16_t GetBaseValue() const { return mBaseVal; }
54 void SetAnimValue(uint16_t aValue, SVGElement* aSVGElement);
55 uint16_t GetAnimValue() const { return mAnimVal; }
56 bool IsExplicitlySet() const { return mIsAnimated || mIsBaseSet; }
58 already_AddRefed<dom::DOMSVGAnimatedEnumeration> ToDOMAnimatedEnum(
59 SVGElement* aSVGElement);
61 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement);
63 private:
64 SVGEnumValue mAnimVal;
65 SVGEnumValue mBaseVal;
66 uint8_t mAttrEnum; // element specified tracking for attribute
67 bool mIsAnimated;
68 bool mIsBaseSet;
70 const SVGEnumMapping* GetMapping(SVGElement* aSVGElement);
72 public:
73 // DOM wrapper class for the (DOM)SVGAnimatedEnumeration interface where the
74 // wrapped class is SVGAnimatedEnumeration.
75 struct DOMAnimatedEnum final : public dom::DOMSVGAnimatedEnumeration {
76 DOMAnimatedEnum(SVGAnimatedEnumeration* aVal, SVGElement* aSVGElement)
77 : dom::DOMSVGAnimatedEnumeration(aSVGElement), mVal(aVal) {}
78 virtual ~DOMAnimatedEnum();
80 SVGAnimatedEnumeration* mVal; // kept alive because it belongs to content
82 using dom::DOMSVGAnimatedEnumeration::SetBaseVal;
83 uint16_t BaseVal() override { return mVal->GetBaseValue(); }
84 void SetBaseVal(uint16_t aBaseVal, ErrorResult& aRv) override {
85 mVal->SetBaseValue(aBaseVal, mSVGElement, aRv);
87 uint16_t AnimVal() override {
88 // Script may have modified animation parameters or timeline -- DOM
89 // getters need to flush any resample requests to reflect these
90 // modifications.
91 mSVGElement->FlushAnimations();
92 return mVal->GetAnimValue();
96 struct SMILEnum : public SMILAttr {
97 public:
98 SMILEnum(SVGAnimatedEnumeration* aVal, SVGElement* aSVGElement)
99 : mVal(aVal), mSVGElement(aSVGElement) {}
101 // These will stay alive because a SMILAttr only lives as long
102 // as the Compositing step, and DOM elements don't get a chance to
103 // die during that.
104 SVGAnimatedEnumeration* mVal;
105 SVGElement* mSVGElement;
107 // SMILAttr methods
108 nsresult ValueFromString(const nsAString& aStr,
109 const dom::SVGAnimationElement* aSrcElement,
110 SMILValue& aValue,
111 bool& aPreventCachingOfSandwich) const override;
112 SMILValue GetBaseValue() const override;
113 void ClearAnimValue() override;
114 nsresult SetAnimValue(const SMILValue& aValue) override;
118 } // namespace mozilla
120 #endif // DOM_SVG_SVGANIMATEDENUMERATION_H_