2 * Copyright (C) 2006 Rob Buis <buis@kde.org>
3 * (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2008 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "CSSCursorImageValue.h"
25 #include "DocLoader.h"
27 #include "PlatformString.h"
28 #include "RenderStyle.h"
29 #include <wtf/MathExtras.h>
30 #include <wtf/UnusedParam.h>
33 #include "SVGCursorElement.h"
34 #include "SVGURIReference.h"
40 static inline bool isSVGCursorIdentifier(const String& url)
42 KURL kurl(ParsedURLString, url);
43 return kurl.hasFragmentIdentifier();
46 static inline SVGCursorElement* resourceReferencedByCursorElement(const String& fragmentId, Document* document)
48 Element* element = document->getElementById(SVGURIReference::getTarget(fragmentId));
49 if (element && element->hasTagName(SVGNames::cursorTag))
50 return static_cast<SVGCursorElement*>(element);
56 CSSCursorImageValue::CSSCursorImageValue(const String& url, const IntPoint& hotspot)
62 CSSCursorImageValue::~CSSCursorImageValue()
65 const String& url = getStringValue();
66 if (!isSVGCursorIdentifier(url))
69 HashSet<SVGElement*>::const_iterator it = m_referencedElements.begin();
70 HashSet<SVGElement*>::const_iterator end = m_referencedElements.end();
72 for (; it != end; ++it) {
73 SVGElement* referencedElement = *it;
74 referencedElement->setCursorImageValue(0);
75 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, referencedElement->document()))
76 cursorElement->removeClient(referencedElement);
81 bool CSSCursorImageValue::updateIfSVGCursorIsUsed(Element* element)
84 UNUSED_PARAM(element);
86 if (!element || !element->isSVGElement())
89 const String& url = getStringValue();
90 if (!isSVGCursorIdentifier(url))
93 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, element->document())) {
94 float x = roundf(cursorElement->x().value(0));
95 m_hotspot.setX(static_cast<int>(x));
97 float y = roundf(cursorElement->y().value(0));
98 m_hotspot.setY(static_cast<int>(y));
100 if (cachedImageURL() != element->document()->completeURL(cursorElement->href()))
103 SVGElement* svgElement = static_cast<SVGElement*>(element);
104 m_referencedElements.add(svgElement);
105 svgElement->setCursorImageValue(this);
106 cursorElement->addClient(svgElement);
114 StyleCachedImage* CSSCursorImageValue::cachedImage(DocLoader* loader)
116 String url = getStringValue();
119 if (isSVGCursorIdentifier(url) && loader && loader->doc()) {
120 if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, loader->doc()))
121 url = cursorElement->href();
125 return CSSImageValue::cachedImage(loader, url);
129 void CSSCursorImageValue::removeReferencedElement(SVGElement* element)
131 m_referencedElements.remove(element);
135 } // namespace WebCore