1 /* 2 Copyright (C) 2006 Apple Computer, Inc. 3 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org> 4 5 This file is part of the WebKit project 6 7 This library is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Library General Public 9 License as published by the Free Software Foundation; either 10 version 2 of the License, or (at your option) any later version. 11 12 This library is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Library General Public License for more details. 16 17 You should have received a copy of the GNU Library General Public License 18 along with this library; see the file COPYING.LIB. If not, write to 19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 Boston, MA 02110-1301, USA. 21 */ 22 23 #ifndef SVGDocumentExtensions_h 24 #define SVGDocumentExtensions_h 25 26 #if ENABLE(SVG) 27 #include <memory> 28 29 #include <wtf/HashSet.h> 30 #include <wtf/HashMap.h> 31 32 #include "StringHash.h" 33 #include "StringImpl.h" 34 #include "SVGAnimatedTemplate.h" 35 36 namespace WebCore { 37 38 class Document; 39 class EventListener; 40 class Node; 41 class String; 42 class SVGElementInstance; 43 class SVGStyledElement; 44 class SVGSVGElement; 45 46 class SVGDocumentExtensions { 47 public: 48 SVGDocumentExtensions(Document*); 49 ~SVGDocumentExtensions(); 50 51 void addTimeContainer(SVGSVGElement*); 52 void removeTimeContainer(SVGSVGElement*); 53 54 void startAnimations(); 55 void pauseAnimations(); 56 void unpauseAnimations(); 57 58 void reportWarning(const String&); 59 void reportError(const String&); 60 61 private: 62 Document* m_doc; // weak reference 63 HashSet<SVGSVGElement*> m_timeContainers; // For SVG 1.2 support this will need to be made more general. 64 HashMap<String, HashSet<SVGStyledElement*>*> m_pendingResources; 65 66 SVGDocumentExtensions(const SVGDocumentExtensions&); 67 SVGDocumentExtensions& operator=(const SVGDocumentExtensions&); 68 69 template<typename ValueType> baseValueMap()70 HashMap<const SVGElement*, HashMap<StringImpl*, ValueType>*>* baseValueMap() const 71 { 72 static HashMap<const SVGElement*, HashMap<StringImpl*, ValueType>*>* s_baseValueMap = new HashMap<const SVGElement*, HashMap<StringImpl*, ValueType>*>(); 73 return s_baseValueMap; 74 } 75 76 public: 77 // This HashMap contains a list of pending resources. Pending resources, are such 78 // which are referenced by any object in the SVG document, but do NOT exist yet. 79 // For instance, dynamically build gradients / patterns / clippers... 80 void addPendingResource(const AtomicString& id, SVGStyledElement*); 81 bool isPendingResource(const AtomicString& id) const; 82 std::auto_ptr<HashSet<SVGStyledElement*> > removePendingResource(const AtomicString& id); 83 84 // Used by the ANIMATED_PROPERTY_* macros 85 template<typename ValueType> baseValue(const SVGElement * element,const AtomicString & propertyName)86 ValueType baseValue(const SVGElement* element, const AtomicString& propertyName) const 87 { 88 HashMap<StringImpl*, ValueType>* propertyMap = baseValueMap<ValueType>()->get(element); 89 if (propertyMap) 90 return propertyMap->get(propertyName.impl()); 91 92 return SVGAnimatedTypeValue<ValueType>::null(); 93 } 94 95 template<typename ValueType> setBaseValue(const SVGElement * element,const AtomicString & propertyName,ValueType newValue)96 void setBaseValue(const SVGElement* element, const AtomicString& propertyName, ValueType newValue) 97 { 98 HashMap<StringImpl*, ValueType>* propertyMap = baseValueMap<ValueType>()->get(element); 99 if (!propertyMap) { 100 propertyMap = new HashMap<StringImpl*, ValueType>(); 101 baseValueMap<ValueType>()->set(element, propertyMap); 102 } 103 104 propertyMap->set(propertyName.impl(), newValue); 105 } 106 107 template<typename ValueType> removeBaseValue(const SVGElement * element,const AtomicString & propertyName)108 void removeBaseValue(const SVGElement* element, const AtomicString& propertyName) 109 { 110 HashMap<StringImpl*, ValueType>* propertyMap = baseValueMap<ValueType>()->get(element); 111 if (!propertyMap) 112 return; 113 114 propertyMap->remove(propertyName.impl()); 115 } 116 117 template<typename ValueType> hasBaseValue(const SVGElement * element,const AtomicString & propertyName)118 bool hasBaseValue(const SVGElement* element, const AtomicString& propertyName) const 119 { 120 HashMap<StringImpl*, ValueType>* propertyMap = baseValueMap<ValueType>()->get(element); 121 if (propertyMap) 122 return propertyMap->contains(propertyName.impl()); 123 124 return false; 125 } 126 }; 127 128 } 129 130 #endif // ENABLE(SVG) 131 #endif 132