1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2007, 2010 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
23#include "config.h"
24#include "HTMLMarqueeElement.h"
25
26#include "Attribute.h"
27#include "CSSPropertyNames.h"
28#include "CSSValueKeywords.h"
29#include "ExceptionCode.h"
30#include "HTMLNames.h"
31#include "RenderLayer.h"
32#include "RenderMarquee.h"
33
34namespace WebCore {
35
36using namespace HTMLNames;
37
38inline HTMLMarqueeElement::HTMLMarqueeElement(const QualifiedName& tagName, Document* document)
39    : HTMLElement(tagName, document)
40    , ActiveDOMObject(document)
41{
42    ASSERT(hasTagName(marqueeTag));
43}
44
45PassRefPtr<HTMLMarqueeElement> HTMLMarqueeElement::create(const QualifiedName& tagName, Document* document)
46{
47    RefPtr<HTMLMarqueeElement> marqueeElement(adoptRef(new HTMLMarqueeElement(tagName, document)));
48    marqueeElement->suspendIfNeeded();
49    return marqueeElement.release();
50}
51
52int HTMLMarqueeElement::minimumDelay() const
53{
54    if (fastGetAttribute(truespeedAttr).isEmpty()) {
55        // WinIE uses 60ms as the minimum delay by default.
56        return 60;
57    }
58    return 0;
59}
60
61bool HTMLMarqueeElement::isPresentationAttribute(const QualifiedName& name) const
62{
63    if (name == widthAttr || name == heightAttr || name == bgcolorAttr || name == vspaceAttr || name == hspaceAttr || name == scrollamountAttr || name == scrolldelayAttr || name == loopAttr || name == behaviorAttr || name == directionAttr)
64        return true;
65    return HTMLElement::isPresentationAttribute(name);
66}
67
68void HTMLMarqueeElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
69{
70    if (name == widthAttr) {
71        if (!value.isEmpty())
72            addHTMLLengthToStyle(style, CSSPropertyWidth, value);
73    } else if (name == heightAttr) {
74        if (!value.isEmpty())
75            addHTMLLengthToStyle(style, CSSPropertyHeight, value);
76    } else if (name == bgcolorAttr) {
77        if (!value.isEmpty())
78            addHTMLColorToStyle(style, CSSPropertyBackgroundColor, value);
79    } else if (name == vspaceAttr) {
80        if (!value.isEmpty()) {
81            addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
82            addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
83        }
84    } else if (name == hspaceAttr) {
85        if (!value.isEmpty()) {
86            addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
87            addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
88        }
89    } else if (name == scrollamountAttr) {
90        if (!value.isEmpty())
91            addHTMLLengthToStyle(style, CSSPropertyWebkitMarqueeIncrement, value);
92    } else if (name == scrolldelayAttr) {
93        if (!value.isEmpty())
94            addHTMLLengthToStyle(style, CSSPropertyWebkitMarqueeSpeed, value);
95    } else if (name == loopAttr) {
96        if (!value.isEmpty()) {
97            if (value == "-1" || equalIgnoringCase(value, "infinite"))
98                addPropertyToPresentationAttributeStyle(style, CSSPropertyWebkitMarqueeRepetition, CSSValueInfinite);
99            else
100                addHTMLLengthToStyle(style, CSSPropertyWebkitMarqueeRepetition, value);
101        }
102    } else if (name == behaviorAttr) {
103        if (!value.isEmpty())
104            addPropertyToPresentationAttributeStyle(style, CSSPropertyWebkitMarqueeStyle, value);
105    } else if (name == directionAttr) {
106        if (!value.isEmpty())
107            addPropertyToPresentationAttributeStyle(style, CSSPropertyWebkitMarqueeDirection, value);
108    } else
109        HTMLElement::collectStyleForPresentationAttribute(name, value, style);
110}
111
112void HTMLMarqueeElement::start()
113{
114    if (RenderMarquee* marqueeRenderer = renderMarquee())
115        marqueeRenderer->start();
116}
117
118void HTMLMarqueeElement::stop()
119{
120    if (RenderMarquee* marqueeRenderer = renderMarquee())
121        marqueeRenderer->stop();
122}
123
124int HTMLMarqueeElement::scrollAmount() const
125{
126    bool ok;
127    int scrollAmount = fastGetAttribute(scrollamountAttr).toInt(&ok);
128    return ok && scrollAmount >= 0 ? scrollAmount : RenderStyle::initialMarqueeIncrement().intValue();
129}
130
131void HTMLMarqueeElement::setScrollAmount(int scrollAmount, ExceptionCode& ec)
132{
133    if (scrollAmount < 0)
134        ec = INDEX_SIZE_ERR;
135    else
136        setIntegralAttribute(scrollamountAttr, scrollAmount);
137}
138
139int HTMLMarqueeElement::scrollDelay() const
140{
141    bool ok;
142    int scrollDelay = fastGetAttribute(scrolldelayAttr).toInt(&ok);
143    return ok && scrollDelay >= 0 ? scrollDelay : RenderStyle::initialMarqueeSpeed();
144}
145
146void HTMLMarqueeElement::setScrollDelay(int scrollDelay, ExceptionCode& ec)
147{
148    if (scrollDelay < 0)
149        ec = INDEX_SIZE_ERR;
150    else
151        setIntegralAttribute(scrolldelayAttr, scrollDelay);
152}
153
154int HTMLMarqueeElement::loop() const
155{
156    bool ok;
157    int loopValue = fastGetAttribute(loopAttr).toInt(&ok);
158    return ok && loopValue > 0 ? loopValue : -1;
159}
160
161void HTMLMarqueeElement::setLoop(int loop, ExceptionCode& ec)
162{
163    if (loop <= 0 && loop != -1)
164        ec = INDEX_SIZE_ERR;
165    else
166        setIntegralAttribute(loopAttr, loop);
167}
168
169bool HTMLMarqueeElement::canSuspend() const
170{
171    return true;
172}
173
174void HTMLMarqueeElement::suspend(ReasonForSuspension)
175{
176    if (RenderMarquee* marqueeRenderer = renderMarquee())
177        marqueeRenderer->suspend();
178}
179
180void HTMLMarqueeElement::resume()
181{
182    if (RenderMarquee* marqueeRenderer = renderMarquee())
183        marqueeRenderer->updateMarqueePosition();
184}
185
186RenderMarquee* HTMLMarqueeElement::renderMarquee() const
187{
188    if (renderer() && renderer()->hasLayer())
189        return renderBoxModelObject()->layer()->marquee();
190    return 0;
191}
192
193} // namespace WebCore
194