• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  *
4  * Portions are Copyright (C) 1998 Netscape Communications Corporation.
5  *
6  * Other contributors:
7  *   Robert O'Callahan <roc+@cs.cmu.edu>
8  *   David Baron <dbaron@fas.harvard.edu>
9  *   Christian Biesinger <cbiesinger@web.de>
10  *   Randall Jesup <rjesup@wgate.com>
11  *   Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
12  *   Josh Soref <timeless@mac.com>
13  *   Boris Zbarsky <bzbarsky@mit.edu>
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2.1 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
28  *
29  * Alternatively, the contents of this file may be used under the terms
30  * of either the Mozilla Public License Version 1.1, found at
31  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
32  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
33  * (the "GPL"), in which case the provisions of the MPL or the GPL are
34  * applicable instead of those above.  If you wish to allow use of your
35  * version of this file only under the terms of one of those two
36  * licenses (the MPL or the GPL) and not to allow others to use your
37  * version of this file under the LGPL, indicate your decision by
38  * deletingthe provisions above and replace them with the notice and
39  * other provisions required by the MPL or the GPL, as the case may be.
40  * If you do not delete the provisions above, a recipient may use your
41  * version of this file under any of the LGPL, the MPL or the GPL.
42  */
43 
44 #include "config.h"
45 
46 #include "RenderMarquee.h"
47 
48 #include "FrameView.h"
49 #include "HTMLMarqueeElement.h"
50 #include "HTMLNames.h"
51 #include "RenderLayer.h"
52 
53 using namespace std;
54 
55 namespace WebCore {
56 
57 using namespace HTMLNames;
58 
RenderMarquee(RenderLayer * l)59 RenderMarquee::RenderMarquee(RenderLayer* l)
60     : m_layer(l), m_currentLoop(0)
61     , m_totalLoops(0)
62     , m_timer(this, &RenderMarquee::timerFired)
63     , m_start(0), m_end(0), m_speed(0), m_reset(false)
64     , m_suspended(false), m_stopped(false), m_direction(MAUTO)
65 {
66 }
67 
marqueeSpeed() const68 int RenderMarquee::marqueeSpeed() const
69 {
70     int result = m_layer->renderer()->style()->marqueeSpeed();
71     Node* n = m_layer->renderer()->node();
72     if (n && n->hasTagName(marqueeTag)) {
73         HTMLMarqueeElement* marqueeElt = static_cast<HTMLMarqueeElement*>(n);
74         result = max(result, marqueeElt->minimumDelay());
75     }
76     return result;
77 }
78 
direction() const79 EMarqueeDirection RenderMarquee::direction() const
80 {
81     // FIXME: Support the CSS3 "auto" value for determining the direction of the marquee.
82     // For now just map MAUTO to MBACKWARD
83     EMarqueeDirection result = m_layer->renderer()->style()->marqueeDirection();
84     TextDirection dir = m_layer->renderer()->style()->direction();
85     if (result == MAUTO)
86         result = MBACKWARD;
87     if (result == MFORWARD)
88         result = (dir == LTR) ? MRIGHT : MLEFT;
89     if (result == MBACKWARD)
90         result = (dir == LTR) ? MLEFT : MRIGHT;
91 
92     // Now we have the real direction.  Next we check to see if the increment is negative.
93     // If so, then we reverse the direction.
94     Length increment = m_layer->renderer()->style()->marqueeIncrement();
95     if (increment.isNegative())
96         result = static_cast<EMarqueeDirection>(-result);
97 
98     return result;
99 }
100 
isHorizontal() const101 bool RenderMarquee::isHorizontal() const
102 {
103     return direction() == MLEFT || direction() == MRIGHT;
104 }
105 
computePosition(EMarqueeDirection dir,bool stopAtContentEdge)106 int RenderMarquee::computePosition(EMarqueeDirection dir, bool stopAtContentEdge)
107 {
108     RenderBox* box = m_layer->renderBox();
109     ASSERT(box);
110     RenderStyle* s = box->style();
111     if (isHorizontal()) {
112         bool ltr = s->direction() == LTR;
113         int clientWidth = box->clientWidth();
114         int contentWidth = ltr ? box->rightmostPosition(true, false) : box->leftmostPosition(true, false);
115         if (ltr)
116             contentWidth += (box->paddingRight() - box->borderLeft());
117         else {
118             contentWidth = box->width() - contentWidth;
119             contentWidth += (box->paddingLeft() - box->borderRight());
120         }
121         if (dir == MRIGHT) {
122             if (stopAtContentEdge)
123                 return max(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
124             else
125                 return ltr ? contentWidth : clientWidth;
126         }
127         else {
128             if (stopAtContentEdge)
129                 return min(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
130             else
131                 return ltr ? -clientWidth : -contentWidth;
132         }
133     }
134     else {
135         int contentHeight = box->lowestPosition(true, false) -
136                             box->borderTop() + box->paddingBottom();
137         int clientHeight = box->clientHeight();
138         if (dir == MUP) {
139             if (stopAtContentEdge)
140                  return min(contentHeight - clientHeight, 0);
141             else
142                 return -clientHeight;
143         }
144         else {
145             if (stopAtContentEdge)
146                 return max(contentHeight - clientHeight, 0);
147             else
148                 return contentHeight;
149         }
150     }
151 }
152 
start()153 void RenderMarquee::start()
154 {
155     if (m_timer.isActive() || m_layer->renderer()->style()->marqueeIncrement().isZero())
156         return;
157 
158     // We may end up propagating a scroll event. It is important that we suspend events until
159     // the end of the function since they could delete the layer, including the marquee.
160     FrameView* frameView = m_layer->renderer()->document()->view();
161     if (frameView)
162         frameView->pauseScheduledEvents();
163 
164     if (!m_suspended && !m_stopped) {
165         if (isHorizontal())
166             m_layer->scrollToOffset(m_start, 0, false, false);
167         else
168             m_layer->scrollToOffset(0, m_start, false, false);
169     }
170     else {
171         m_suspended = false;
172         m_stopped = false;
173     }
174 
175     m_timer.startRepeating(speed() * 0.001);
176 
177     if (frameView)
178         frameView->resumeScheduledEvents();
179 }
180 
suspend()181 void RenderMarquee::suspend()
182 {
183     m_timer.stop();
184     m_suspended = true;
185 }
186 
stop()187 void RenderMarquee::stop()
188 {
189     m_timer.stop();
190     m_stopped = true;
191 }
192 
updateMarqueePosition()193 void RenderMarquee::updateMarqueePosition()
194 {
195     bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
196     if (activate) {
197         EMarqueeBehavior behavior = m_layer->renderer()->style()->marqueeBehavior();
198         m_start = computePosition(direction(), behavior == MALTERNATE);
199         m_end = computePosition(reverseDirection(), behavior == MALTERNATE || behavior == MSLIDE);
200         if (!m_stopped)
201             start();
202     }
203 }
204 
updateMarqueeStyle()205 void RenderMarquee::updateMarqueeStyle()
206 {
207     RenderStyle* s = m_layer->renderer()->style();
208 
209     if (m_direction != s->marqueeDirection() || (m_totalLoops != s->marqueeLoopCount() && m_currentLoop >= m_totalLoops))
210         m_currentLoop = 0; // When direction changes or our loopCount is a smaller number than our current loop, reset our loop.
211 
212     m_totalLoops = s->marqueeLoopCount();
213     m_direction = s->marqueeDirection();
214 
215     if (m_layer->renderer()->isHTMLMarquee()) {
216         // Hack for WinIE.  In WinIE, a value of 0 or lower for the loop count for SLIDE means to only do
217         // one loop.
218         if (m_totalLoops <= 0 && s->marqueeBehavior() == MSLIDE)
219             m_totalLoops = 1;
220 
221         // Hack alert: Set the white-space value to nowrap for horizontal marquees with inline children, thus ensuring
222         // all the text ends up on one line by default.  Limit this hack to the <marquee> element to emulate
223         // WinIE's behavior.  Someone using CSS3 can use white-space: nowrap on their own to get this effect.
224         // Second hack alert: Set the text-align back to auto.  WinIE completely ignores text-align on the
225         // marquee element.
226         // FIXME: Bring these up with the CSS WG.
227         if (isHorizontal() && m_layer->renderer()->childrenInline()) {
228             s->setWhiteSpace(NOWRAP);
229             s->setTextAlign(TAAUTO);
230         }
231     }
232 
233     // Marquee height hack!! Make sure that, if it is a horizontal marquee, the height attribute is overridden
234     // if it is smaller than the font size. If it is a vertical marquee and height is not specified, we default
235     // to a marquee of 200px.
236     if (isHorizontal()) {
237         if (s->height().isFixed() && s->height().value() < s->fontSize())
238             s->setHeight(Length(s->fontSize(), Fixed));
239     } else if (s->height().isAuto())  //vertical marquee with no specified height
240         s->setHeight(Length(200, Fixed));
241 
242     if (speed() != marqueeSpeed()) {
243         m_speed = marqueeSpeed();
244         if (m_timer.isActive())
245             m_timer.startRepeating(speed() * 0.001);
246     }
247 
248     // Check the loop count to see if we should now stop.
249     bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
250     if (activate && !m_timer.isActive())
251         m_layer->renderer()->setNeedsLayout(true);
252     else if (!activate && m_timer.isActive())
253         m_timer.stop();
254 }
255 
timerFired(Timer<RenderMarquee> *)256 void RenderMarquee::timerFired(Timer<RenderMarquee>*)
257 {
258     if (m_layer->renderer()->needsLayout())
259         return;
260 
261     if (m_reset) {
262         m_reset = false;
263         if (isHorizontal())
264             m_layer->scrollToXOffset(m_start);
265         else
266             m_layer->scrollToYOffset(m_start);
267         return;
268     }
269 
270     RenderStyle* s = m_layer->renderer()->style();
271 
272     int endPoint = m_end;
273     int range = m_end - m_start;
274     int newPos;
275     if (range == 0)
276         newPos = m_end;
277     else {
278         bool addIncrement = direction() == MUP || direction() == MLEFT;
279         bool isReversed = s->marqueeBehavior() == MALTERNATE && m_currentLoop % 2;
280         if (isReversed) {
281             // We're going in the reverse direction.
282             endPoint = m_start;
283             range = -range;
284             addIncrement = !addIncrement;
285         }
286         bool positive = range > 0;
287         int clientSize = (isHorizontal() ? m_layer->renderBox()->clientWidth() : m_layer->renderBox()->clientHeight());
288         int increment = max(1, abs(m_layer->renderer()->style()->marqueeIncrement().calcValue(clientSize)));
289         int currentPos = (isHorizontal() ? m_layer->scrollXOffset() : m_layer->scrollYOffset());
290         newPos =  currentPos + (addIncrement ? increment : -increment);
291         if (positive)
292             newPos = min(newPos, endPoint);
293         else
294             newPos = max(newPos, endPoint);
295     }
296 
297     if (newPos == endPoint) {
298         m_currentLoop++;
299         if (m_totalLoops > 0 && m_currentLoop >= m_totalLoops)
300             m_timer.stop();
301         else if (s->marqueeBehavior() != MALTERNATE)
302             m_reset = true;
303     }
304 
305     if (isHorizontal())
306         m_layer->scrollToXOffset(newPos);
307     else
308         m_layer->scrollToYOffset(newPos);
309 }
310 
311 } // namespace WebCore
312