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