323605c9253b14be682bd00121c4b5932ce2c4ee
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLAreaElement.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  * Copyright (C) 2004, 2005, 2006, 2009, 2011 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 "core/html/HTMLAreaElement.h"
24
25 #include "HTMLNames.h"
26 #include "core/html/HTMLImageElement.h"
27 #include "core/html/HTMLMapElement.h"
28 #include "core/rendering/HitTestResult.h"
29 #include "core/rendering/RenderImage.h"
30 #include "core/rendering/RenderView.h"
31 #include "platform/LengthFunctions.h"
32 #include "platform/graphics/Path.h"
33 #include "platform/transforms/AffineTransform.h"
34
35 using namespace std;
36
37 namespace WebCore {
38
39 using namespace HTMLNames;
40
41 inline HTMLAreaElement::HTMLAreaElement(Document& document)
42     : HTMLAnchorElement(areaTag, document)
43     , m_lastSize(-1, -1)
44     , m_shape(Unknown)
45 {
46     ScriptWrappable::init(this);
47 }
48
49 PassRefPtr<HTMLAreaElement> HTMLAreaElement::create(Document& document)
50 {
51     return adoptRef(new HTMLAreaElement(document));
52 }
53
54 void HTMLAreaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
55 {
56     if (name == shapeAttr) {
57         if (equalIgnoringCase(value, "default"))
58             m_shape = Default;
59         else if (equalIgnoringCase(value, "circle"))
60             m_shape = Circle;
61         else if (equalIgnoringCase(value, "poly"))
62             m_shape = Poly;
63         else if (equalIgnoringCase(value, "rect"))
64             m_shape = Rect;
65         invalidateCachedRegion();
66     } else if (name == coordsAttr) {
67         m_coords = parseHTMLAreaElementCoords(value.string());
68         invalidateCachedRegion();
69     } else if (name == altAttr || name == accesskeyAttr) {
70         // Do nothing.
71     } else
72         HTMLAnchorElement::parseAttribute(name, value);
73 }
74
75 void HTMLAreaElement::invalidateCachedRegion()
76 {
77     m_lastSize = LayoutSize(-1, -1);
78 }
79
80 bool HTMLAreaElement::mapMouseEvent(LayoutPoint location, const LayoutSize& size, HitTestResult& result)
81 {
82     if (m_lastSize != size) {
83         m_region = adoptPtr(new Path(getRegion(size)));
84         m_lastSize = size;
85     }
86
87     if (!m_region->contains(location))
88         return false;
89
90     result.setInnerNode(this);
91     result.setURLElement(this);
92     return true;
93 }
94
95 Path HTMLAreaElement::computePath(RenderObject* obj) const
96 {
97     if (!obj)
98         return Path();
99
100     // FIXME: This doesn't work correctly with transforms.
101     FloatPoint absPos = obj->localToAbsolute();
102
103     // Default should default to the size of the containing object.
104     LayoutSize size = m_lastSize;
105     if (m_shape == Default)
106         size = obj->absoluteClippedOverflowRect().size();
107
108     Path p = getRegion(size);
109     float zoomFactor = obj->style()->effectiveZoom();
110     if (zoomFactor != 1.0f) {
111         AffineTransform zoomTransform;
112         zoomTransform.scale(zoomFactor);
113         p.transform(zoomTransform);
114     }
115
116     p.translate(toFloatSize(absPos));
117     return p;
118 }
119
120 LayoutRect HTMLAreaElement::computeRect(RenderObject* obj) const
121 {
122     return enclosingLayoutRect(computePath(obj).boundingRect());
123 }
124
125 Path HTMLAreaElement::getRegion(const LayoutSize& size) const
126 {
127     if (m_coords.isEmpty() && m_shape != Default)
128         return Path();
129
130     LayoutUnit width = size.width();
131     LayoutUnit height = size.height();
132
133     // If element omits the shape attribute, select shape based on number of coordinates.
134     Shape shape = m_shape;
135     if (shape == Unknown) {
136         if (m_coords.size() == 3)
137             shape = Circle;
138         else if (m_coords.size() == 4)
139             shape = Rect;
140         else if (m_coords.size() >= 6)
141             shape = Poly;
142     }
143
144     Path path;
145     switch (shape) {
146         case Poly:
147             if (m_coords.size() >= 6) {
148                 int numPoints = m_coords.size() / 2;
149                 path.moveTo(FloatPoint(minimumValueForLength(m_coords[0], width).toFloat(), minimumValueForLength(m_coords[1], height).toFloat()));
150                 for (int i = 1; i < numPoints; ++i)
151                     path.addLineTo(FloatPoint(minimumValueForLength(m_coords[i * 2], width).toFloat(), minimumValueForLength(m_coords[i * 2 + 1], height).toFloat()));
152                 path.closeSubpath();
153             }
154             break;
155         case Circle:
156             if (m_coords.size() >= 3) {
157                 Length radius = m_coords[2];
158                 float r = min(minimumValueForLength(radius, width).toFloat(), minimumValueForLength(radius, height).toFloat());
159                 path.addEllipse(FloatRect(minimumValueForLength(m_coords[0], width).toFloat() - r, minimumValueForLength(m_coords[1], height).toFloat() - r, 2 * r, 2 * r));
160             }
161             break;
162         case Rect:
163             if (m_coords.size() >= 4) {
164                 float x0 = minimumValueForLength(m_coords[0], width).toFloat();
165                 float y0 = minimumValueForLength(m_coords[1], height).toFloat();
166                 float x1 = minimumValueForLength(m_coords[2], width).toFloat();
167                 float y1 = minimumValueForLength(m_coords[3], height).toFloat();
168                 path.addRect(FloatRect(x0, y0, x1 - x0, y1 - y0));
169             }
170             break;
171         case Default:
172             path.addRect(FloatRect(0, 0, width.toFloat(), height.toFloat()));
173             break;
174         case Unknown:
175             break;
176     }
177
178     return path;
179 }
180
181 HTMLImageElement* HTMLAreaElement::imageElement() const
182 {
183     Element* mapElement = parentElement();
184     while (mapElement && !isHTMLMapElement(*mapElement))
185         mapElement = mapElement->parentElement();
186
187     if (!mapElement)
188         return 0;
189
190     return toHTMLMapElement(*mapElement).imageElement();
191 }
192
193 bool HTMLAreaElement::isKeyboardFocusable() const
194 {
195     return isFocusable();
196 }
197
198 bool HTMLAreaElement::isMouseFocusable() const
199 {
200     return isFocusable();
201 }
202
203 bool HTMLAreaElement::rendererIsFocusable() const
204 {
205     HTMLImageElement* image = imageElement();
206     if (!image || !image->renderer() || image->renderer()->style()->visibility() != VISIBLE)
207         return false;
208
209     return supportsFocus() && Element::tabIndex() >= 0;
210 }
211
212 void HTMLAreaElement::setFocus(bool shouldBeFocused)
213 {
214     if (focused() == shouldBeFocused)
215         return;
216
217     HTMLAnchorElement::setFocus(shouldBeFocused);
218
219     HTMLImageElement* imageElement = this->imageElement();
220     if (!imageElement)
221         return;
222
223     RenderObject* renderer = imageElement->renderer();
224     if (!renderer || !renderer->isImage())
225         return;
226
227     toRenderImage(renderer)->areaElementFocusChanged(this);
228 }
229
230 void HTMLAreaElement::updateFocusAppearance(bool restorePreviousSelection)
231 {
232     if (!isFocusable())
233         return;
234
235     HTMLImageElement* imageElement = this->imageElement();
236     if (!imageElement)
237         return;
238
239     imageElement->updateFocusAppearance(restorePreviousSelection);
240 }
241
242 bool HTMLAreaElement::supportsFocus() const
243 {
244     // If the AREA element was a link, it should support focus.
245     // FIXME: This means that an AREA that is not a link cannot be made focusable through contenteditable or tabindex. Is it correct?
246     return isLink();
247 }
248
249 }