Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderMarquee.cpp
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
54 using namespace std;
55
56 namespace WebCore {
57
58 using namespace HTMLNames;
59
60 RenderMarquee::RenderMarquee(RenderLayer* l)
61     : m_layer(l), m_currentLoop(0)
62     , m_totalLoops(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)
66 {
67 }
68
69 RenderMarquee::~RenderMarquee()
70 {
71 }
72
73 int RenderMarquee::marqueeSpeed() const
74 {
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());
80     }
81     return result;
82 }
83
84 EMarqueeDirection RenderMarquee::direction() const
85 {
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();
90     if (result == MAUTO)
91         result = MBACKWARD;
92     if (result == MFORWARD)
93         result = (dir == LTR) ? MRIGHT : MLEFT;
94     if (result == MBACKWARD)
95         result = (dir == LTR) ? MLEFT : MRIGHT;
96     
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);
102     
103     return result;
104 }
105
106 bool RenderMarquee::isHorizontal() const
107 {
108     return direction() == MLEFT || direction() == MRIGHT;
109 }
110
111 int RenderMarquee::computePosition(EMarqueeDirection dir, bool stopAtContentEdge)
112 {
113     RenderBox* box = m_layer->renderBox();
114     ASSERT(box);
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();
120         if (ltr)
121             contentWidth += (box->paddingRight() - box->borderLeft());
122         else {
123             contentWidth = box->width() - contentWidth;
124             contentWidth += (box->paddingLeft() - box->borderRight());
125         }
126         if (dir == MRIGHT) {
127             if (stopAtContentEdge)
128                 return max(ZERO_LAYOUT_UNIT, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
129             else
130                 return ltr ? contentWidth : clientWidth;
131         }
132         else {
133             if (stopAtContentEdge)
134                 return min(ZERO_LAYOUT_UNIT, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
135             else
136                 return ltr ? -clientWidth : -contentWidth;
137         }
138     }
139     else {
140         int contentHeight = box->layoutOverflowRect().maxY() - box->borderTop() + box->paddingBottom();
141         int clientHeight = box->clientHeight();
142         if (dir == MUP) {
143             if (stopAtContentEdge)
144                  return min(contentHeight - clientHeight, 0);
145             else
146                 return -clientHeight;
147         }
148         else {
149             if (stopAtContentEdge)
150                 return max(contentHeight - clientHeight, 0);
151             else 
152                 return contentHeight;
153         }
154     }    
155 }
156
157 void RenderMarquee::start()
158 {
159     if (m_timer.isActive() || m_layer->renderer()->style()->marqueeIncrement().isZero())
160         return;
161
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();
165     if (frameView)
166         frameView->pauseScheduledEvents();
167
168     if (!m_suspended && !m_stopped) {
169         if (isHorizontal())
170             m_layer->scrollToOffset(IntSize(m_start, 0));
171         else
172             m_layer->scrollToOffset(IntSize(0, m_start));
173     }
174     else {
175         m_suspended = false;
176         m_stopped = false;
177     }
178
179     m_timer.startRepeating(speed() * 0.001);
180
181     if (frameView)
182         frameView->resumeScheduledEvents();
183 }
184
185 void RenderMarquee::suspend()
186 {
187     m_timer.stop();
188     m_suspended = true;
189 }
190
191 void RenderMarquee::stop()
192 {
193     m_timer.stop();
194     m_stopped = true;
195 }
196
197 void RenderMarquee::updateMarqueePosition()
198 {
199     bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
200     if (activate) {
201         EMarqueeBehavior behavior = m_layer->renderer()->style()->marqueeBehavior();
202         m_start = computePosition(direction(), behavior == MALTERNATE);
203         m_end = computePosition(reverseDirection(), behavior == MALTERNATE || behavior == MSLIDE);
204         if (!m_stopped)
205             start();
206     }
207 }
208
209 void RenderMarquee::updateMarqueeStyle()
210 {
211     RenderStyle* s = m_layer->renderer()->style();
212     
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.
215     
216     m_totalLoops = s->marqueeLoopCount();
217     m_direction = s->marqueeDirection();
218     
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
221         // one loop.
222         if (m_totalLoops <= 0 && s->marqueeBehavior() == MSLIDE)
223             m_totalLoops = 1;
224         
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
229         // marquee element.
230         // FIXME: Bring these up with the CSS WG.
231         if (isHorizontal() && m_layer->renderer()->childrenInline()) {
232             s->setWhiteSpace(NOWRAP);
233             s->setTextAlign(TASTART);
234         }
235     }
236     
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)); 
245    
246     if (speed() != marqueeSpeed()) {
247         m_speed = marqueeSpeed();
248         if (m_timer.isActive())
249             m_timer.startRepeating(speed() * 0.001);
250     }
251     
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())
257         m_timer.stop();
258 }
259
260 void RenderMarquee::timerFired(Timer<RenderMarquee>*)
261 {
262     if (m_layer->renderer()->needsLayout())
263         return;
264     
265     if (m_reset) {
266         m_reset = false;
267         if (isHorizontal())
268             m_layer->scrollToXOffset(m_start);
269         else
270             m_layer->scrollToYOffset(m_start);
271         return;
272     }
273     
274     RenderStyle* s = m_layer->renderer()->style();
275     
276     int endPoint = m_end;
277     int range = m_end - m_start;
278     int newPos;
279     if (range == 0)
280         newPos = m_end;
281     else {  
282         bool addIncrement = direction() == MUP || direction() == MLEFT;
283         bool isReversed = s->marqueeBehavior() == MALTERNATE && m_currentLoop % 2;
284         if (isReversed) {
285             // We're going in the reverse direction.
286             endPoint = m_start;
287             range = -range;
288             addIncrement = !addIncrement;
289         }
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);
295         if (positive)
296             newPos = min(newPos, endPoint);
297         else
298             newPos = max(newPos, endPoint);
299     }
300
301     if (newPos == endPoint) {
302         m_currentLoop++;
303         if (m_totalLoops > 0 && m_currentLoop >= m_totalLoops)
304             m_timer.stop();
305         else if (s->marqueeBehavior() != MALTERNATE)
306             m_reset = true;
307     }
308     
309     if (isHorizontal())
310         m_layer->scrollToXOffset(newPos);
311     else
312         m_layer->scrollToYOffset(newPos);
313 }
314
315 } // namespace WebCore