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.
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 "core/html/HTMLAreaElement.h"
25 #include "core/HTMLNames.h"
26 #include "core/dom/ElementTraversal.h"
27 #include "core/html/HTMLImageElement.h"
28 #include "core/html/HTMLMapElement.h"
29 #include "core/layout/HitTestResult.h"
30 #include "core/layout/LayoutImage.h"
31 #include "core/layout/LayoutView.h"
32 #include "platform/LengthFunctions.h"
33 #include "platform/graphics/Path.h"
34 #include "platform/transforms/AffineTransform.h"
38 using namespace HTMLNames
;
40 inline HTMLAreaElement::HTMLAreaElement(Document
& document
)
41 : HTMLAnchorElement(areaTag
, document
)
47 // An explicit empty destructor should be in HTMLAreaElement.cpp, because
48 // if an implicit destructor is used or an empty destructor is defined in
49 // HTMLAreaElement.h, when including HTMLAreaElement.h, msvc tries to expand
50 // the destructor and causes a compile error because of lack of blink::Path
52 HTMLAreaElement::~HTMLAreaElement()
56 DEFINE_NODE_FACTORY(HTMLAreaElement
)
58 void HTMLAreaElement::parseAttribute(const QualifiedName
& name
, const AtomicString
& value
)
60 if (name
== shapeAttr
) {
61 if (equalIgnoringCase(value
, "default"))
63 else if (equalIgnoringCase(value
, "circle"))
65 else if (equalIgnoringCase(value
, "poly"))
67 else if (equalIgnoringCase(value
, "rect"))
69 invalidateCachedRegion();
70 } else if (name
== coordsAttr
) {
71 m_coords
= parseHTMLAreaElementCoords(value
.string());
72 invalidateCachedRegion();
73 } else if (name
== altAttr
|| name
== accesskeyAttr
) {
76 HTMLAnchorElement::parseAttribute(name
, value
);
80 void HTMLAreaElement::invalidateCachedRegion()
82 m_lastSize
= LayoutSize(-1, -1);
85 bool HTMLAreaElement::pointInArea(LayoutPoint location
, const LayoutSize
& containerSize
)
87 if (m_lastSize
!= containerSize
) {
88 m_region
= adoptPtr(new Path(getRegion(containerSize
)));
89 m_lastSize
= containerSize
;
92 return m_region
->contains(FloatPoint(location
));
95 Path
HTMLAreaElement::computePath(LayoutObject
* obj
) const
100 // FIXME: This doesn't work correctly with transforms.
101 FloatPoint absPos
= obj
->localToAbsolute();
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();
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
);
116 p
.translate(toFloatSize(absPos
));
120 LayoutRect
HTMLAreaElement::computeRect(LayoutObject
* obj
) const
122 return enclosingLayoutRect(computePath(obj
).boundingRect());
125 Path
HTMLAreaElement::getRegion(const LayoutSize
& size
) const
127 if (m_coords
.isEmpty() && m_shape
!= Default
)
130 LayoutUnit width
= size
.width();
131 LayoutUnit height
= size
.height();
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)
138 else if (m_coords
.size() == 4)
140 else if (m_coords
.size() >= 6)
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()));
156 if (m_coords
.size() >= 3) {
157 Length radius
= m_coords
[2];
158 float r
= std::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
));
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
));
172 path
.addRect(FloatRect(0, 0, width
.toFloat(), height
.toFloat()));
181 HTMLImageElement
* HTMLAreaElement::imageElement() const
183 if (HTMLMapElement
* mapElement
= Traversal
<HTMLMapElement
>::firstAncestor(*this))
184 return mapElement
->imageElement();
188 bool HTMLAreaElement::isKeyboardFocusable() const
190 return isFocusable();
193 bool HTMLAreaElement::isMouseFocusable() const
195 return isFocusable();
198 bool HTMLAreaElement::layoutObjectIsFocusable() const
200 HTMLImageElement
* image
= imageElement();
201 if (!image
|| !image
->layoutObject() || image
->layoutObject()->style()->visibility() != VISIBLE
)
204 return supportsFocus() && Element::tabIndex() >= 0;
207 void HTMLAreaElement::setFocus(bool shouldBeFocused
)
209 if (focused() == shouldBeFocused
)
212 HTMLAnchorElement::setFocus(shouldBeFocused
);
214 HTMLImageElement
* imageElement
= this->imageElement();
218 LayoutObject
* layoutObject
= imageElement
->layoutObject();
219 if (!layoutObject
|| !layoutObject
->isImage())
222 toLayoutImage(layoutObject
)->areaElementFocusChanged(this);
225 void HTMLAreaElement::updateFocusAppearance(bool restorePreviousSelection
)
230 HTMLImageElement
* imageElement
= this->imageElement();
234 imageElement
->updateFocusAppearance(restorePreviousSelection
);