1/* 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/) 4 * 5 * Portions are Copyright (C) 1998 Netscape Communications Corporation. 6 * 7 * Other contributors: 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> 15 * 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. 20 * 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. 25 * 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 29 * 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. 43 */ 44 45#include "config.h" 46 47#include "RenderMarquee.h" 48 49#include "FrameView.h" 50#include "HTMLMarqueeElement.h" 51#include "HTMLNames.h" 52#include "RenderLayer.h" 53#include "RenderView.h" 54 55using namespace std; 56 57namespace WebCore { 58 59using namespace HTMLNames; 60 61RenderMarquee::RenderMarquee(RenderLayer* l) 62 : m_layer(l), m_currentLoop(0) 63 , m_totalLoops(0) 64 , m_timer(this, &RenderMarquee::timerFired) 65 , m_start(0), m_end(0), m_speed(0), m_reset(false) 66 , m_suspended(false), m_stopped(false), m_direction(MAUTO) 67{ 68} 69 70RenderMarquee::~RenderMarquee() 71{ 72} 73 74int RenderMarquee::marqueeSpeed() const 75{ 76 int result = m_layer->renderer()->style()->marqueeSpeed(); 77 Node* n = m_layer->renderer()->node(); 78 if (n && n->hasTagName(marqueeTag)) { 79 HTMLMarqueeElement* marqueeElt = static_cast<HTMLMarqueeElement*>(n); 80 result = max(result, marqueeElt->minimumDelay()); 81 } 82 return result; 83} 84 85EMarqueeDirection RenderMarquee::direction() const 86{ 87 // FIXME: Support the CSS3 "auto" value for determining the direction of the marquee. 88 // For now just map MAUTO to MBACKWARD 89 EMarqueeDirection result = m_layer->renderer()->style()->marqueeDirection(); 90 TextDirection dir = m_layer->renderer()->style()->direction(); 91 if (result == MAUTO) 92 result = MBACKWARD; 93 if (result == MFORWARD) 94 result = (dir == LTR) ? MRIGHT : MLEFT; 95 if (result == MBACKWARD) 96 result = (dir == LTR) ? MLEFT : MRIGHT; 97 98 // Now we have the real direction. Next we check to see if the increment is negative. 99 // If so, then we reverse the direction. 100 Length increment = m_layer->renderer()->style()->marqueeIncrement(); 101 if (increment.isNegative()) 102 result = static_cast<EMarqueeDirection>(-result); 103 104 return result; 105} 106 107bool RenderMarquee::isHorizontal() const 108{ 109 return direction() == MLEFT || direction() == MRIGHT; 110} 111 112int RenderMarquee::computePosition(EMarqueeDirection dir, bool stopAtContentEdge) 113{ 114 RenderBox* box = m_layer->renderBox(); 115 ASSERT(box); 116 RenderStyle* s = box->style(); 117 if (isHorizontal()) { 118 bool ltr = s->isLeftToRightDirection(); 119 LayoutUnit clientWidth = box->clientWidth(); 120 LayoutUnit contentWidth = ltr ? box->maxPreferredLogicalWidth() : box->minPreferredLogicalWidth(); 121 if (ltr) 122 contentWidth += (box->paddingRight() - box->borderLeft()); 123 else { 124 contentWidth = box->width() - contentWidth; 125 contentWidth += (box->paddingLeft() - box->borderRight()); 126 } 127 if (dir == MRIGHT) { 128 if (stopAtContentEdge) 129 return max<LayoutUnit>(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth)); 130 else 131 return ltr ? contentWidth : clientWidth; 132 } 133 else { 134 if (stopAtContentEdge) 135 return min<LayoutUnit>(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth)); 136 else 137 return ltr ? -clientWidth : -contentWidth; 138 } 139 } 140 else { 141 int contentHeight = box->layoutOverflowRect().maxY() - box->borderTop() + box->paddingBottom(); 142 int clientHeight = box->clientHeight(); 143 if (dir == MUP) { 144 if (stopAtContentEdge) 145 return min(contentHeight - clientHeight, 0); 146 else 147 return -clientHeight; 148 } 149 else { 150 if (stopAtContentEdge) 151 return max(contentHeight - clientHeight, 0); 152 else 153 return contentHeight; 154 } 155 } 156} 157 158void RenderMarquee::start() 159{ 160 if (m_timer.isActive() || m_layer->renderer()->style()->marqueeIncrement().isZero()) 161 return; 162 163 if (!m_suspended && !m_stopped) { 164 if (isHorizontal()) 165 m_layer->scrollToOffset(IntSize(m_start, 0)); 166 else 167 m_layer->scrollToOffset(IntSize(0, m_start)); 168 } 169 else { 170 m_suspended = false; 171 m_stopped = false; 172 } 173 174 m_timer.startRepeating(speed() * 0.001); 175} 176 177void RenderMarquee::suspend() 178{ 179 m_timer.stop(); 180 m_suspended = true; 181} 182 183void RenderMarquee::stop() 184{ 185 m_timer.stop(); 186 m_stopped = true; 187} 188 189void RenderMarquee::updateMarqueePosition() 190{ 191 bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops); 192 if (activate) { 193 EMarqueeBehavior behavior = m_layer->renderer()->style()->marqueeBehavior(); 194 m_start = computePosition(direction(), behavior == MALTERNATE); 195 m_end = computePosition(reverseDirection(), behavior == MALTERNATE || behavior == MSLIDE); 196 if (!m_stopped) 197 start(); 198 } 199} 200 201void RenderMarquee::updateMarqueeStyle() 202{ 203 RenderStyle* s = m_layer->renderer()->style(); 204 205 if (m_direction != s->marqueeDirection() || (m_totalLoops != s->marqueeLoopCount() && m_currentLoop >= m_totalLoops)) 206 m_currentLoop = 0; // When direction changes or our loopCount is a smaller number than our current loop, reset our loop. 207 208 m_totalLoops = s->marqueeLoopCount(); 209 m_direction = s->marqueeDirection(); 210 211 if (m_layer->renderer()->isHTMLMarquee()) { 212 // Hack for WinIE. In WinIE, a value of 0 or lower for the loop count for SLIDE means to only do 213 // one loop. 214 if (m_totalLoops <= 0 && s->marqueeBehavior() == MSLIDE) 215 m_totalLoops = 1; 216 217 // Hack alert: Set the white-space value to nowrap for horizontal marquees with inline children, thus ensuring 218 // all the text ends up on one line by default. Limit this hack to the <marquee> element to emulate 219 // WinIE's behavior. Someone using CSS3 can use white-space: nowrap on their own to get this effect. 220 // Second hack alert: Set the text-align back to auto. WinIE completely ignores text-align on the 221 // marquee element. 222 // FIXME: Bring these up with the CSS WG. 223 if (isHorizontal() && m_layer->renderer()->childrenInline()) { 224 s->setWhiteSpace(NOWRAP); 225 s->setTextAlign(TASTART); 226 } 227 } 228 229 // Legacy hack - multiple browsers default vertical marquees to 200px tall. 230 if (!isHorizontal() && s->height().isAuto()) 231 s->setHeight(Length(200, Fixed)); 232 233 if (speed() != marqueeSpeed()) { 234 m_speed = marqueeSpeed(); 235 if (m_timer.isActive()) 236 m_timer.startRepeating(speed() * 0.001); 237 } 238 239 // Check the loop count to see if we should now stop. 240 bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops); 241 if (activate && !m_timer.isActive()) 242 m_layer->renderer()->setNeedsLayout(true); 243 else if (!activate && m_timer.isActive()) 244 m_timer.stop(); 245} 246 247void RenderMarquee::timerFired(Timer<RenderMarquee>*) 248{ 249 if (m_layer->renderer()->view()->needsLayout()) 250 return; 251 252 if (m_reset) { 253 m_reset = false; 254 if (isHorizontal()) 255 m_layer->scrollToXOffset(m_start); 256 else 257 m_layer->scrollToYOffset(m_start); 258 return; 259 } 260 261 RenderStyle* s = m_layer->renderer()->style(); 262 263 int endPoint = m_end; 264 int range = m_end - m_start; 265 int newPos; 266 if (range == 0) 267 newPos = m_end; 268 else { 269 bool addIncrement = direction() == MUP || direction() == MLEFT; 270 bool isReversed = s->marqueeBehavior() == MALTERNATE && m_currentLoop % 2; 271 if (isReversed) { 272 // We're going in the reverse direction. 273 endPoint = m_start; 274 range = -range; 275 addIncrement = !addIncrement; 276 } 277 bool positive = range > 0; 278 int clientSize = (isHorizontal() ? m_layer->renderBox()->clientWidth() : m_layer->renderBox()->clientHeight()); 279 int increment = abs(intValueForLength(m_layer->renderer()->style()->marqueeIncrement(), clientSize)); 280 int currentPos = (isHorizontal() ? m_layer->scrollXOffset() : m_layer->scrollYOffset()); 281 newPos = currentPos + (addIncrement ? increment : -increment); 282 if (positive) 283 newPos = min(newPos, endPoint); 284 else 285 newPos = max(newPos, endPoint); 286 } 287 288 if (newPos == endPoint) { 289 m_currentLoop++; 290 if (m_totalLoops > 0 && m_currentLoop >= m_totalLoops) 291 m_timer.stop(); 292 else if (s->marqueeBehavior() != MALTERNATE) 293 m_reset = true; 294 } 295 296 if (isHorizontal()) 297 m_layer->scrollToXOffset(newPos); 298 else 299 m_layer->scrollToYOffset(newPos); 300} 301 302} // namespace WebCore 303