WebCore/svg/SVGDocumentExtensions.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2     Copyright (C) 2006 Apple Computer, Inc.
       
     3                   2006 Nikolas Zimmermann <zimmermann@kde.org>
       
     4                   2007 Rob Buis <buis@kde.org>
       
     5 
       
     6     This file is part of the WebKit project
       
     7 
       
     8     This library is free software; you can redistribute it and/or
       
     9     modify it under the terms of the GNU Library General Public
       
    10     License as published by the Free Software Foundation; either
       
    11     version 2 of the License, or (at your option) any later version.
       
    12 
       
    13     This library is distributed in the hope that it will be useful,
       
    14     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16     Library General Public License for more details.
       
    17 
       
    18     You should have received a copy of the GNU Library General Public License
       
    19     along with this library; see the file COPYING.LIB.  If not, write to
       
    20     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    21     Boston, MA 02110-1301, USA.
       
    22 */
       
    23 
       
    24 #include "config.h"
       
    25 
       
    26 #if ENABLE(SVG)
       
    27 #include "SVGDocumentExtensions.h"
       
    28 
       
    29 #include "AtomicString.h"
       
    30 #include "Console.h"
       
    31 #include "DOMWindow.h"
       
    32 #include "Document.h"
       
    33 #include "EventListener.h"
       
    34 #include "Frame.h"
       
    35 #include "FrameLoader.h"
       
    36 #include "Page.h"
       
    37 #include "SVGSMILElement.h"
       
    38 #include "SVGSVGElement.h"
       
    39 #include "SMILTimeContainer.h"
       
    40 #include "ScriptableDocumentParser.h"
       
    41 #include "ScriptController.h"
       
    42 
       
    43 namespace WebCore {
       
    44 
       
    45 SVGDocumentExtensions::SVGDocumentExtensions(Document* doc)
       
    46     : m_doc(doc)
       
    47 {
       
    48 }
       
    49 
       
    50 SVGDocumentExtensions::~SVGDocumentExtensions()
       
    51 {
       
    52     deleteAllValues(m_pendingResources);
       
    53 }
       
    54 
       
    55 void SVGDocumentExtensions::addTimeContainer(SVGSVGElement* element)
       
    56 {
       
    57     m_timeContainers.add(element);
       
    58 }
       
    59 
       
    60 void SVGDocumentExtensions::removeTimeContainer(SVGSVGElement* element)
       
    61 {
       
    62     m_timeContainers.remove(element);
       
    63 }
       
    64 
       
    65 void SVGDocumentExtensions::addResource(const AtomicString& id, RenderSVGResourceContainer* resource)
       
    66 {
       
    67     ASSERT(resource);
       
    68 
       
    69     if (id.isEmpty())
       
    70         return;
       
    71 
       
    72     // Replaces resource if already present, to handle potential id changes
       
    73     m_resources.set(id, resource);
       
    74 }
       
    75 
       
    76 void SVGDocumentExtensions::removeResource(const AtomicString& id)
       
    77 {
       
    78     if (id.isEmpty() || !m_resources.contains(id))
       
    79         return;
       
    80 
       
    81     m_resources.remove(id);
       
    82 }
       
    83 
       
    84 RenderSVGResourceContainer* SVGDocumentExtensions::resourceById(const AtomicString& id) const
       
    85 {
       
    86     if (id.isEmpty())
       
    87         return 0;
       
    88 
       
    89     return m_resources.get(id);
       
    90 }
       
    91 
       
    92 void SVGDocumentExtensions::startAnimations()
       
    93 {
       
    94     // FIXME: Eventually every "Time Container" will need a way to latch on to some global timer
       
    95     // starting animations for a document will do this "latching"
       
    96 #if ENABLE(SVG_ANIMATION)    
       
    97     HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
       
    98     for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
       
    99         (*itr)->timeContainer()->begin();
       
   100 #endif
       
   101 }
       
   102     
       
   103 void SVGDocumentExtensions::pauseAnimations()
       
   104 {
       
   105     HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
       
   106     for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
       
   107         (*itr)->pauseAnimations();
       
   108 }
       
   109 
       
   110 void SVGDocumentExtensions::unpauseAnimations()
       
   111 {
       
   112     HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
       
   113     for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
       
   114         (*itr)->unpauseAnimations();
       
   115 }
       
   116 
       
   117 bool SVGDocumentExtensions::sampleAnimationAtTime(const String& elementId, SVGSMILElement* element, double time)
       
   118 {
       
   119 #if !ENABLE(SVG_ANIMATION)
       
   120     UNUSED_PARAM(elementId);
       
   121     UNUSED_PARAM(element);
       
   122     UNUSED_PARAM(time);
       
   123     return false;
       
   124 #else
       
   125     ASSERT(element);
       
   126     SMILTimeContainer* container = element->timeContainer();
       
   127     if (!container || container->isPaused())
       
   128         return false;
       
   129 
       
   130     container->sampleAnimationAtTime(elementId, time);
       
   131     return true;
       
   132 #endif
       
   133 }
       
   134 
       
   135 // FIXME: Callers should probably use ScriptController::eventHandlerLineNumber()
       
   136 static int parserLineNumber(Document* document)
       
   137 {
       
   138     ScriptableDocumentParser* parser = document->scriptableDocumentParser();
       
   139     if (!parser)
       
   140         return 1;
       
   141     return parser->lineNumber();
       
   142 }
       
   143 
       
   144 static void reportMessage(Document* document, MessageLevel level, const String& message)
       
   145 {
       
   146     if (Frame* frame = document->frame())
       
   147         frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, level, message, parserLineNumber(document), String());
       
   148 }
       
   149 
       
   150 void SVGDocumentExtensions::reportWarning(const String& message)
       
   151 {
       
   152     reportMessage(m_doc, WarningMessageLevel, "Warning: " + message);
       
   153 }
       
   154 
       
   155 void SVGDocumentExtensions::reportError(const String& message)
       
   156 {
       
   157     reportMessage(m_doc, ErrorMessageLevel, "Error: " + message);
       
   158 }
       
   159 
       
   160 void SVGDocumentExtensions::addPendingResource(const AtomicString& id, SVGStyledElement* obj)
       
   161 {
       
   162     ASSERT(obj);
       
   163 
       
   164     if (id.isEmpty())
       
   165         return;
       
   166 
       
   167     if (m_pendingResources.contains(id))
       
   168         m_pendingResources.get(id)->add(obj);
       
   169     else {
       
   170         HashSet<SVGStyledElement*>* set = new HashSet<SVGStyledElement*>;
       
   171         set->add(obj);
       
   172 
       
   173         m_pendingResources.add(id, set);
       
   174     }
       
   175 }
       
   176 
       
   177 bool SVGDocumentExtensions::isPendingResource(const AtomicString& id) const
       
   178 {
       
   179     if (id.isEmpty())
       
   180         return false;
       
   181 
       
   182     return m_pendingResources.contains(id);
       
   183 }
       
   184 
       
   185 PassOwnPtr<HashSet<SVGStyledElement*> > SVGDocumentExtensions::removePendingResource(const AtomicString& id)
       
   186 {
       
   187     ASSERT(m_pendingResources.contains(id));
       
   188 
       
   189     OwnPtr<HashSet<SVGStyledElement*> > set(m_pendingResources.get(id));
       
   190     m_pendingResources.remove(id);
       
   191     return set.release();
       
   192 }
       
   193 
       
   194 }
       
   195 
       
   196 #endif