2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
5 * Portions are Copyright (C) 1998 Netscape Communications Corporation.
8 * Robert O'Callahan <roc+@cs.cmu.edu>
9 * David Baron <dbaron@fas.harvard.edu>
10 * Christian Biesinger <cbiesinger@web.de>
11 * Randall Jesup <rjesup@wgate.com>
12 * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
13 * Josh Soref <timeless@mac.com>
14 * Boris Zbarsky <bzbarsky@mit.edu>
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 * Alternatively, the contents of this file may be used under the terms
31 * of either the Mozilla Public License Version 1.1, found at
32 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
33 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
34 * (the "GPL"), in which case the provisions of the MPL or the GPL are
35 * applicable instead of those above. If you wish to allow use of your
36 * version of this file only under the terms of one of those two
37 * licenses (the MPL or the GPL) and not to allow others to use your
38 * version of this file under the LGPL, indicate your decision by
39 * deletingthe provisions above and replace them with the notice and
40 * other provisions required by the MPL or the GPL, as the case may be.
41 * If you do not delete the provisions above, a recipient may use your
42 * version of this file under any of the LGPL, the MPL or the GPL.
47 #include "RenderMarquee.h"
49 #include "FrameView.h"
50 #include "HTMLMarqueeElement.h"
51 #include "HTMLNames.h"
52 #include "RenderLayer.h"
58 using namespace HTMLNames;
60 RenderMarquee::RenderMarquee(RenderLayer* l)
61 : m_layer(l), m_currentLoop(0)
63 , m_timer(this, &RenderMarquee::timerFired)
64 , m_start(0), m_end(0), m_speed(0), m_reset(false)
65 , m_suspended(false), m_stopped(false), m_direction(MAUTO)
69 RenderMarquee::~RenderMarquee()
73 int RenderMarquee::marqueeSpeed() const
75 int result = m_layer->renderer()->style()->marqueeSpeed();
76 Node* n = m_layer->renderer()->node();
77 if (n && n->hasTagName(marqueeTag)) {
78 HTMLMarqueeElement* marqueeElt = static_cast<HTMLMarqueeElement*>(n);
79 result = max(result, marqueeElt->minimumDelay());
84 EMarqueeDirection RenderMarquee::direction() const
86 // FIXME: Support the CSS3 "auto" value for determining the direction of the marquee.
87 // For now just map MAUTO to MBACKWARD
88 EMarqueeDirection result = m_layer->renderer()->style()->marqueeDirection();
89 TextDirection dir = m_layer->renderer()->style()->direction();
92 if (result == MFORWARD)
93 result = (dir == LTR) ? MRIGHT : MLEFT;
94 if (result == MBACKWARD)
95 result = (dir == LTR) ? MLEFT : MRIGHT;
97 // Now we have the real direction. Next we check to see if the increment is negative.
98 // If so, then we reverse the direction.
99 Length increment = m_layer->renderer()->style()->marqueeIncrement();
100 if (increment.isNegative())
101 result = static_cast<EMarqueeDirection>(-result);
106 bool RenderMarquee::isHorizontal() const
108 return direction() == MLEFT || direction() == MRIGHT;
111 int RenderMarquee::computePosition(EMarqueeDirection dir, bool stopAtContentEdge)
113 RenderBox* box = m_layer->renderBox();
115 RenderStyle* s = box->style();
116 if (isHorizontal()) {
117 bool ltr = s->isLeftToRightDirection();
118 LayoutUnit clientWidth = box->clientWidth();
119 LayoutUnit contentWidth = ltr ? box->maxPreferredLogicalWidth() : box->minPreferredLogicalWidth();
121 contentWidth += (box->paddingRight() - box->borderLeft());
123 contentWidth = box->width() - contentWidth;
124 contentWidth += (box->paddingLeft() - box->borderRight());
127 if (stopAtContentEdge)
128 return max(ZERO_LAYOUT_UNIT, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
130 return ltr ? contentWidth : clientWidth;
133 if (stopAtContentEdge)
134 return min(ZERO_LAYOUT_UNIT, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
136 return ltr ? -clientWidth : -contentWidth;
140 int contentHeight = box->layoutOverflowRect().maxY() - box->borderTop() + box->paddingBottom();
141 int clientHeight = box->clientHeight();
143 if (stopAtContentEdge)
144 return min(contentHeight - clientHeight, 0);
146 return -clientHeight;
149 if (stopAtContentEdge)
150 return max(contentHeight - clientHeight, 0);
152 return contentHeight;
157 void RenderMarquee::start()
159 if (m_timer.isActive() || m_layer->renderer()->style()->marqueeIncrement().isZero())
162 // We may end up propagating a scroll event. It is important that we suspend events until
163 // the end of the function since they could delete the layer, including the marquee.
164 FrameView* frameView = m_layer->renderer()->document()->view();
166 frameView->pauseScheduledEvents();
168 if (!m_suspended && !m_stopped) {
170 m_layer->scrollToOffset(IntSize(m_start, 0));
172 m_layer->scrollToOffset(IntSize(0, m_start));
179 m_timer.startRepeating(speed() * 0.001);
182 frameView->resumeScheduledEvents();
185 void RenderMarquee::suspend()
191 void RenderMarquee::stop()
197 void RenderMarquee::updateMarqueePosition()
199 bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
201 EMarqueeBehavior behavior = m_layer->renderer()->style()->marqueeBehavior();
202 m_start = computePosition(direction(), behavior == MALTERNATE);
203 m_end = computePosition(reverseDirection(), behavior == MALTERNATE || behavior == MSLIDE);
209 void RenderMarquee::updateMarqueeStyle()
211 RenderStyle* s = m_layer->renderer()->style();
213 if (m_direction != s->marqueeDirection() || (m_totalLoops != s->marqueeLoopCount() && m_currentLoop >= m_totalLoops))
214 m_currentLoop = 0; // When direction changes or our loopCount is a smaller number than our current loop, reset our loop.
216 m_totalLoops = s->marqueeLoopCount();
217 m_direction = s->marqueeDirection();
219 if (m_layer->renderer()->isHTMLMarquee()) {
220 // Hack for WinIE. In WinIE, a value of 0 or lower for the loop count for SLIDE means to only do
222 if (m_totalLoops <= 0 && s->marqueeBehavior() == MSLIDE)
225 // Hack alert: Set the white-space value to nowrap for horizontal marquees with inline children, thus ensuring
226 // all the text ends up on one line by default. Limit this hack to the <marquee> element to emulate
227 // WinIE's behavior. Someone using CSS3 can use white-space: nowrap on their own to get this effect.
228 // Second hack alert: Set the text-align back to auto. WinIE completely ignores text-align on the
230 // FIXME: Bring these up with the CSS WG.
231 if (isHorizontal() && m_layer->renderer()->childrenInline()) {
232 s->setWhiteSpace(NOWRAP);
233 s->setTextAlign(TASTART);
237 // Marquee height hack!! Make sure that, if it is a horizontal marquee, the height attribute is overridden
238 // if it is smaller than the font size. If it is a vertical marquee and height is not specified, we default
239 // to a marquee of 200px.
240 if (isHorizontal()) {
241 if (s->height().isFixed() && s->height().value() < s->fontSize())
242 s->setHeight(Length(s->fontSize(), Fixed));
243 } else if (s->height().isAuto()) //vertical marquee with no specified height
244 s->setHeight(Length(200, Fixed));
246 if (speed() != marqueeSpeed()) {
247 m_speed = marqueeSpeed();
248 if (m_timer.isActive())
249 m_timer.startRepeating(speed() * 0.001);
252 // Check the loop count to see if we should now stop.
253 bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
254 if (activate && !m_timer.isActive())
255 m_layer->renderer()->setNeedsLayout(true);
256 else if (!activate && m_timer.isActive())
260 void RenderMarquee::timerFired(Timer<RenderMarquee>*)
262 if (m_layer->renderer()->needsLayout())
268 m_layer->scrollToXOffset(m_start);
270 m_layer->scrollToYOffset(m_start);
274 RenderStyle* s = m_layer->renderer()->style();
276 int endPoint = m_end;
277 int range = m_end - m_start;
282 bool addIncrement = direction() == MUP || direction() == MLEFT;
283 bool isReversed = s->marqueeBehavior() == MALTERNATE && m_currentLoop % 2;
285 // We're going in the reverse direction.
288 addIncrement = !addIncrement;
290 bool positive = range > 0;
291 int clientSize = (isHorizontal() ? m_layer->renderBox()->clientWidth() : m_layer->renderBox()->clientHeight());
292 int increment = abs(intValueForLength(m_layer->renderer()->style()->marqueeIncrement(), clientSize));
293 int currentPos = (isHorizontal() ? m_layer->scrollXOffset() : m_layer->scrollYOffset());
294 newPos = currentPos + (addIncrement ? increment : -increment);
296 newPos = min(newPos, endPoint);
298 newPos = max(newPos, endPoint);
301 if (newPos == endPoint) {
303 if (m_totalLoops > 0 && m_currentLoop >= m_totalLoops)
305 else if (s->marqueeBehavior() != MALTERNATE)
310 m_layer->scrollToXOffset(newPos);
312 m_layer->scrollToYOffset(newPos);
315 } // namespace WebCore