Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / svg / SVGAnimatedViewBox.h
blob97c09b95627043fb692487d2376aa6cc9ff40c1e
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_SVGANIMATEDVIEWBOX_H_
8 #define DOM_SVG_SVGANIMATEDVIEWBOX_H_
10 #include "nsCycleCollectionParticipant.h"
11 #include "nsError.h"
12 #include "SVGAttrTearoffTable.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/SMILAttr.h"
15 #include "mozilla/UniquePtr.h"
16 #include "mozilla/dom/SVGAnimatedRect.h"
18 namespace mozilla {
20 class SMILValue;
22 namespace dom {
23 class SVGRect;
24 class SVGAnimationElement;
25 class SVGElement;
26 } // namespace dom
28 struct SVGViewBox {
29 float x, y;
30 float width, height;
31 bool none;
33 SVGViewBox() : x(0.0), y(0.0), width(0.0), height(0.0), none(true) {}
34 SVGViewBox(float aX, float aY, float aWidth, float aHeight)
35 : x(aX), y(aY), width(aWidth), height(aHeight), none(false) {}
36 bool operator==(const SVGViewBox& aOther) const;
37 SVGViewBox operator*(const float m) const {
38 return SVGViewBox(x * m, y * m, width * m, height * m);
41 static nsresult FromString(const nsAString& aStr, SVGViewBox* aViewBox);
44 class SVGAnimatedViewBox {
45 public:
46 friend class AutoChangeViewBoxNotifier;
47 using SVGElement = dom::SVGElement;
49 SVGAnimatedViewBox& operator=(const SVGAnimatedViewBox& aOther) {
50 mBaseVal = aOther.mBaseVal;
51 if (aOther.mAnimVal) {
52 mAnimVal = MakeUnique<SVGViewBox>(*aOther.mAnimVal);
54 mHasBaseVal = aOther.mHasBaseVal;
55 return *this;
58 void Init();
60 /**
61 * Returns true if the corresponding "viewBox" attribute defined a rectangle
62 * with finite values and nonnegative width/height.
63 * Returns false if the viewBox was set to an invalid
64 * string, or if any of the four rect values were too big to store in a
65 * float, or the width/height are negative.
67 bool HasRect() const;
69 /**
70 * Returns true if the corresponding "viewBox" attribute either defined a
71 * rectangle with finite values or the special "none" value.
73 bool IsExplicitlySet() const {
74 if (mAnimVal || mHasBaseVal) {
75 const SVGViewBox& rect = GetAnimValue();
76 return rect.none || (rect.width >= 0 && rect.height >= 0);
78 return false;
81 const SVGViewBox& GetBaseValue() const { return mBaseVal; }
82 void SetBaseValue(const SVGViewBox& aRect, SVGElement* aSVGElement);
83 const SVGViewBox& GetAnimValue() const {
84 return mAnimVal ? *mAnimVal : mBaseVal;
86 void SetAnimValue(const SVGViewBox& aRect, SVGElement* aSVGElement);
88 nsresult SetBaseValueString(const nsAString& aValue, SVGElement* aSVGElement,
89 bool aDoSetAttr);
90 void GetBaseValueString(nsAString& aValue) const;
92 already_AddRefed<dom::SVGAnimatedRect> ToSVGAnimatedRect(
93 SVGElement* aSVGElement);
95 already_AddRefed<dom::SVGRect> ToDOMBaseVal(SVGElement* aSVGElement);
97 already_AddRefed<dom::SVGRect> ToDOMAnimVal(SVGElement* aSVGElement);
99 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement);
101 private:
102 SVGViewBox mBaseVal;
103 UniquePtr<SVGViewBox> mAnimVal;
104 bool mHasBaseVal;
106 public:
107 struct SMILViewBox : public SMILAttr {
108 public:
109 SMILViewBox(SVGAnimatedViewBox* aVal, SVGElement* aSVGElement)
110 : mVal(aVal), mSVGElement(aSVGElement) {}
112 // These will stay alive because a SMILAttr only lives as long
113 // as the Compositing step, and DOM elements don't get a chance to
114 // die during that.
115 SVGAnimatedViewBox* mVal;
116 SVGElement* mSVGElement;
118 // SMILAttr methods
119 nsresult ValueFromString(const nsAString& aStr,
120 const dom::SVGAnimationElement* aSrcElement,
121 SMILValue& aValue,
122 bool& aPreventCachingOfSandwich) const override;
123 SMILValue GetBaseValue() const override;
124 void ClearAnimValue() override;
125 nsresult SetAnimValue(const SMILValue& aValue) override;
128 static SVGAttrTearoffTable<SVGAnimatedViewBox, dom::SVGAnimatedRect>
129 sSVGAnimatedRectTearoffTable;
132 } // namespace mozilla
134 #endif // DOM_SVG_SVGANIMATEDVIEWBOX_H_