/*
 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef SVGUseElement_h
#define SVGUseElement_h

#if ENABLE(SVG)
#include "CachedSVGDocument.h"
#include "CachedSVGDocumentClient.h"
#include "SVGAnimatedBoolean.h"
#include "SVGAnimatedLength.h"
#include "SVGExternalResourcesRequired.h"
#include "SVGGraphicsElement.h"
#include "SVGURIReference.h"

namespace WebCore {

class CachedSVGDocument;
class SVGGElement;

class SVGUseElement : public SVGGraphicsElement, public SVGExternalResourcesRequired, public SVGURIReference, private CachedSVGDocumentClient {
    WTF_MAKE_FAST_ALLOCATED;

	BEGIN_DECLARE_ANIMATED_PROPERTIES(SVGUseElement)
        DECLARE_ANIMATED_LENGTH(X, x)
        DECLARE_ANIMATED_LENGTH(Y, y)
        DECLARE_ANIMATED_LENGTH(Width, width)
        DECLARE_ANIMATED_LENGTH(Height, height)
        DECLARE_ANIMATED_STRING(Href, href)
        DECLARE_ANIMATED_BOOLEAN(ExternalResourcesRequired, externalResourcesRequired)
    END_DECLARE_ANIMATED_PROPERTIES
public:
    static PassRefPtr<SVGUseElement> create(const QualifiedName&, Document*);
    virtual ~SVGUseElement();

    void invalidateShadowTree();

    RenderElement* rendererClipChild() const;

private:
    SVGUseElement(const QualifiedName&, Document*);

    virtual bool isValid() const;
    virtual bool supportsFocus() const { return true; }
    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
    virtual void removedFrom(ContainerNode*) OVERRIDE;
    virtual void buildPendingResource();
    virtual void parseAttribute(const Attribute&) OVERRIDE;
    virtual void svgAttributeChanged(const QualifiedName&);
    virtual void willAttachRenderers() OVERRIDE;
    virtual RenderElement* createElementRenderer(PassRefPtr<RenderStyle>, const RenderTreePosition&);
    virtual void toClipPath(Path&);
    virtual bool haveLoadedRequiredResources();
    virtual void finishParsingChildren();
    virtual bool selfHasRelativeLengths() const;
    virtual void setHaveFiredLoadEvent(bool) OVERRIDE;
    virtual bool haveFiredLoadEvent() const OVERRIDE;
    virtual Timer<SVGElement>* svgLoadEventTimer() OVERRIDE;
    virtual void notifyFinished(CachedResource*) OVERRIDE;

    Document* externalDocument() const;
    void updateExternalDocument();

    SVGElement* findTarget(String* targetID = 0) const;

    void cloneTarget(ContainerNode&, SVGElement& target) const;
    SVGElement* targetClone() const;

    void updateShadowTree();
    void expandUseElementsInShadowTree() const;
    void expandSymbolElementsInShadowTree() const;
    void transferEventListenersToShadowTree() const;
    void transferSizeAttributesToTargetClone(SVGElement&) const;

    void clearShadowTree();
    void invalidateDependentShadowTrees();

    bool m_haveFiredLoadEvent;
    bool m_shadowTreeNeedsUpdate;
    CachedResourceHandle<CachedSVGDocument> m_externalDocument;
    Timer<SVGElement> m_svgLoadEventTimer;
};

inline bool isSVGUseElement(const Node& node)
{
    return node.hasTagName(SVGNames::useTag);
}

inline bool isSVGUseElement(const Element& element)
{
    return element.hasTagName(SVGNames::useTag);
}

inline bool isSVGUseElement(const Node* node)
{
    return node->hasTagName(SVGNames::useTag);
}

inline bool isSVGUseElement(const Element* element)
{
    return element->hasTagName(SVGNames::useTag);
}

template <typename ArgType>
struct ElementTypeCastTraits<const SVGUseElement, ArgType> {
    static bool is(ArgType& node) { return isSVGUseElement(node); }
};

}

#endif
#endif
