tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / css / CSSCursorImageValue.cpp
1 /*
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.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23 #include "CSSCursorImageValue.h"
24
25 #include "CachedResourceLoader.h"
26 #include "TreeScope.h"
27 #include "PlatformString.h"
28 #include <wtf/MathExtras.h>
29 #include <wtf/UnusedParam.h>
30
31 #if ENABLE(SVG)
32 #include "SVGCursorElement.h"
33 #include "SVGLengthContext.h"
34 #include "SVGNames.h"
35 #include "SVGURIReference.h"
36 #endif
37
38 namespace WebCore {
39
40 #if ENABLE(SVG)
41 static inline bool isSVGCursorIdentifier(const String& url)
42 {
43     KURL kurl(ParsedURLString, url);
44     return kurl.hasFragmentIdentifier();
45 }
46
47 static inline SVGCursorElement* resourceReferencedByCursorElement(const String& url, Document* document)
48 {
49     Element* element = SVGURIReference::targetElementFromIRIString(url, document);
50     if (element && element->hasTagName(SVGNames::cursorTag))
51         return static_cast<SVGCursorElement*>(element);
52
53     return 0;
54 }
55 #endif
56
57 CSSCursorImageValue::CSSCursorImageValue(const String& url, const IntPoint& hotSpot)
58     : CSSImageValue(CursorImageClass, url)
59     , m_hotSpot(hotSpot)
60 {
61 }
62
63 CSSCursorImageValue::~CSSCursorImageValue()
64 {
65 #if ENABLE(SVG)
66     const String& url = getStringValue();
67     if (!isSVGCursorIdentifier(url))
68         return;
69
70     HashSet<SVGElement*>::const_iterator it = m_referencedElements.begin();
71     HashSet<SVGElement*>::const_iterator end = m_referencedElements.end();
72
73     for (; it != end; ++it) {
74         SVGElement* referencedElement = *it;
75         referencedElement->cursorImageValueRemoved();
76         if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, referencedElement->document()))
77             cursorElement->removeClient(referencedElement);
78     }
79 #endif
80 }
81
82 bool CSSCursorImageValue::updateIfSVGCursorIsUsed(Element* element)
83 {
84 #if !ENABLE(SVG)
85     UNUSED_PARAM(element);
86 #else
87     if (!element || !element->isSVGElement())
88         return false;
89
90     const String& url = getStringValue();
91     if (!isSVGCursorIdentifier(url))
92         return false;
93
94     if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, element->document())) {
95         // FIXME: This will override hot spot specified in CSS, which is probably incorrect.
96         SVGLengthContext lengthContext(0);
97         float x = roundf(cursorElement->x().value(lengthContext));
98         m_hotSpot.setX(static_cast<int>(x));
99
100         float y = roundf(cursorElement->y().value(lengthContext));
101         m_hotSpot.setY(static_cast<int>(y));
102
103         if (cachedImageURL() != element->document()->completeURL(cursorElement->href()))
104             clearCachedImage();
105
106         SVGElement* svgElement = static_cast<SVGElement*>(element);
107         m_referencedElements.add(svgElement);
108         svgElement->setCursorImageValue(this);
109         cursorElement->addClient(svgElement);
110         return true;
111     }
112 #endif
113
114     return false;
115 }
116
117 StyleCachedImage* CSSCursorImageValue::cachedImage(CachedResourceLoader* loader)
118 {
119     String url = getStringValue();
120
121 #if ENABLE(SVG)
122     if (isSVGCursorIdentifier(url) && loader && loader->document()) {
123         // FIXME: This will fail if the <cursor> element is in a shadow DOM (bug 59827)
124         if (SVGCursorElement* cursorElement = resourceReferencedByCursorElement(url, loader->document()))
125             url = cursorElement->href();
126     }
127 #endif
128
129     return CSSImageValue::cachedImage(loader, url);
130 }
131
132 #if ENABLE(SVG)
133 void CSSCursorImageValue::removeReferencedElement(SVGElement* element)
134 {
135     m_referencedElements.remove(element);
136 }
137 #endif
138
139 } // namespace WebCore