2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #ifndef SVGPropertyTearOff_h
21 #define SVGPropertyTearOff_h
24 #include "SVGAnimatedProperty.h"
25 #include "SVGElement.h"
26 #include "SVGProperty.h"
30 template<typename PropertyType>
31 class SVGPropertyTearOff : public SVGProperty {
33 typedef SVGPropertyTearOff<PropertyType> Self;
35 // Used for child types (baseVal/animVal) of a SVGAnimated* property (for example: SVGAnimatedLength::baseVal()).
36 // Also used for list tear offs (for example: text.x.baseVal.getItem(0)).
37 static PassRefPtr<Self> create(SVGAnimatedProperty* animatedProperty, SVGPropertyRole role, PropertyType& value)
39 ASSERT(animatedProperty);
40 return adoptRef(new Self(animatedProperty, role, value));
43 // Used for non-animated POD types (for example: SVGSVGElement::createSVGLength()).
44 static PassRefPtr<Self> create(const PropertyType& initialValue)
46 return adoptRef(new Self(initialValue));
49 PropertyType& propertyReference() { return *m_value; }
50 SVGAnimatedProperty* animatedProperty() const { return m_animatedProperty.get(); }
52 // Used only by the list tear offs!
53 void setValue(PropertyType& value)
57 m_valueIsCopy = false;
61 void setAnimatedProperty(SVGAnimatedProperty* animatedProperty) { m_animatedProperty = animatedProperty; }
63 SVGElement* contextElement() const
65 if (!m_animatedProperty || m_valueIsCopy)
67 return m_animatedProperty->contextElement();
75 // Switch from a live value, to a non-live value.
76 // For example: <text x="50"/>
77 // var item = text.x.baseVal.getItem(0);
78 // text.setAttribute("x", "100");
79 // item.value still has to report '50' and it has to be possible to modify 'item'
80 // w/o changing the "new item" (with x=100) in the text element.
81 // Whenever the XML DOM modifies the "x" attribute, all existing wrappers are detached, using this function.
82 m_value = new PropertyType(*m_value);
84 m_animatedProperty = 0;
87 virtual void commitChange()
89 if (!m_animatedProperty || m_valueIsCopy)
91 m_animatedProperty->commitChange();
94 virtual SVGPropertyRole role() const { return m_role; }
96 void updateAnimVal(PropertyType* value)
98 ASSERT(!m_valueIsCopy);
99 ASSERT(m_role == AnimValRole);
104 SVGPropertyTearOff(SVGAnimatedProperty* animatedProperty, SVGPropertyRole role, PropertyType& value)
105 : m_animatedProperty(animatedProperty)
108 , m_valueIsCopy(false)
110 // Using operator & is completly fine, as SVGAnimatedProperty owns this reference,
111 // and we're guaranteed to live as long as SVGAnimatedProperty does.
114 SVGPropertyTearOff(const PropertyType& initialValue)
115 : m_animatedProperty(0)
116 , m_role(UndefinedRole)
117 , m_value(new PropertyType(initialValue))
118 , m_valueIsCopy(true)
122 virtual ~SVGPropertyTearOff()
128 RefPtr<SVGAnimatedProperty> m_animatedProperty;
129 SVGPropertyRole m_role;
130 PropertyType* m_value;
131 bool m_valueIsCopy : 1;
136 #endif // ENABLE(SVG)
137 #endif // SVGPropertyTearOff_h