#include "config.h"
#include "RenderMarquee.h"
#include "FrameView.h"
#include "HTMLMarqueeElement.h"
#include "HTMLNames.h"
#include "RenderLayer.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
RenderMarquee::RenderMarquee(RenderLayer* l)
: m_layer(l), m_currentLoop(0)
, m_totalLoops(0)
, m_timer(this, &RenderMarquee::timerFired)
, m_start(0), m_end(0), m_speed(0), m_reset(false)
, m_suspended(false), m_stopped(false), m_direction(MAUTO)
{
}
int RenderMarquee::marqueeSpeed() const
{
int result = m_layer->renderer()->style()->marqueeSpeed();
Node* n = m_layer->renderer()->node();
if (n && n->hasTagName(marqueeTag)) {
HTMLMarqueeElement* marqueeElt = static_cast<HTMLMarqueeElement*>(n);
result = max(result, marqueeElt->minimumDelay());
}
return result;
}
EMarqueeDirection RenderMarquee::direction() const
{
EMarqueeDirection result = m_layer->renderer()->style()->marqueeDirection();
TextDirection dir = m_layer->renderer()->style()->direction();
if (result == MAUTO)
result = MBACKWARD;
if (result == MFORWARD)
result = (dir == LTR) ? MRIGHT : MLEFT;
if (result == MBACKWARD)
result = (dir == LTR) ? MLEFT : MRIGHT;
Length increment = m_layer->renderer()->style()->marqueeIncrement();
if (increment.isNegative())
result = static_cast<EMarqueeDirection>(-result);
return result;
}
bool RenderMarquee::isHorizontal() const
{
return direction() == MLEFT || direction() == MRIGHT;
}
int RenderMarquee::computePosition(EMarqueeDirection dir, bool stopAtContentEdge)
{
RenderBox* box = m_layer->renderBox();
ASSERT(box);
RenderStyle* s = box->style();
if (isHorizontal()) {
bool ltr = s->direction() == LTR;
int clientWidth = box->clientWidth();
int contentWidth = ltr ? box->rightmostPosition(true, false) : box->leftmostPosition(true, false);
if (ltr)
contentWidth += (box->paddingRight() - box->borderLeft());
else {
contentWidth = box->width() - contentWidth;
contentWidth += (box->paddingLeft() - box->borderRight());
}
if (dir == MRIGHT) {
if (stopAtContentEdge)
return max(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
else
return ltr ? contentWidth : clientWidth;
}
else {
if (stopAtContentEdge)
return min(0, ltr ? (contentWidth - clientWidth) : (clientWidth - contentWidth));
else
return ltr ? -clientWidth : -contentWidth;
}
}
else {
int contentHeight = box->lowestPosition(true, false) -
box->borderTop() + box->paddingBottom();
int clientHeight = box->clientHeight();
if (dir == MUP) {
if (stopAtContentEdge)
return min(contentHeight - clientHeight, 0);
else
return -clientHeight;
}
else {
if (stopAtContentEdge)
return max(contentHeight - clientHeight, 0);
else
return contentHeight;
}
}
}
void RenderMarquee::start()
{
if (m_timer.isActive() || m_layer->renderer()->style()->marqueeIncrement().isZero()
#if ENABLE(WCSS) && ENABLE(XHTMLMP)
|| (m_layer->renderer()->document()->isXHTMLMPDocument() && !m_layer->renderer()->style()->marqueeLoopCount())
#endif
)
return;
FrameView* frameView = m_layer->renderer()->document()->view();
if (frameView)
frameView->pauseScheduledEvents();
if (!m_suspended && !m_stopped) {
if (isHorizontal())
m_layer->scrollToOffset(m_start, 0, false, false);
else
m_layer->scrollToOffset(0, m_start, false, false);
}
else {
m_suspended = false;
m_stopped = false;
}
m_timer.startRepeating(speed() * 0.001);
if (frameView)
frameView->resumeScheduledEvents();
}
void RenderMarquee::suspend()
{
m_timer.stop();
m_suspended = true;
}
void RenderMarquee::stop()
{
m_timer.stop();
m_stopped = true;
}
void RenderMarquee::updateMarqueePosition()
{
bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
if (activate) {
EMarqueeBehavior behavior = m_layer->renderer()->style()->marqueeBehavior();
m_start = computePosition(direction(), behavior == MALTERNATE);
m_end = computePosition(reverseDirection(), behavior == MALTERNATE || behavior == MSLIDE);
if (!m_stopped)
start();
}
}
void RenderMarquee::updateMarqueeStyle()
{
RenderStyle* s = m_layer->renderer()->style();
if (m_direction != s->marqueeDirection() || (m_totalLoops != s->marqueeLoopCount() && m_currentLoop >= m_totalLoops))
m_currentLoop = 0;
m_totalLoops = s->marqueeLoopCount();
m_direction = s->marqueeDirection();
if (m_layer->renderer()->isHTMLMarquee()) {
if (m_totalLoops <= 0 && s->marqueeBehavior() == MSLIDE)
m_totalLoops = 1;
if (isHorizontal() && m_layer->renderer()->childrenInline()) {
s->setWhiteSpace(NOWRAP);
s->setTextAlign(TAAUTO);
}
}
if (isHorizontal()) {
if (s->height().isFixed() && s->height().value() < s->fontSize())
s->setHeight(Length(s->fontSize(), Fixed));
} else if (s->height().isAuto()) s->setHeight(Length(200, Fixed));
if (speed() != marqueeSpeed()) {
m_speed = marqueeSpeed();
if (m_timer.isActive())
m_timer.startRepeating(speed() * 0.001);
}
bool activate = (m_totalLoops <= 0 || m_currentLoop < m_totalLoops);
if (activate && !m_timer.isActive())
m_layer->renderer()->setNeedsLayout(true);
else if (!activate && m_timer.isActive())
m_timer.stop();
}
void RenderMarquee::timerFired(Timer<RenderMarquee>*)
{
if (m_layer->renderer()->needsLayout())
return;
if (m_reset) {
m_reset = false;
if (isHorizontal())
m_layer->scrollToXOffset(m_start);
else
m_layer->scrollToYOffset(m_start);
return;
}
RenderStyle* s = m_layer->renderer()->style();
int endPoint = m_end;
int range = m_end - m_start;
int newPos;
if (range == 0)
newPos = m_end;
else {
bool addIncrement = direction() == MUP || direction() == MLEFT;
bool isReversed = s->marqueeBehavior() == MALTERNATE && m_currentLoop % 2;
if (isReversed) {
endPoint = m_start;
range = -range;
addIncrement = !addIncrement;
}
bool positive = range > 0;
int clientSize = (isHorizontal() ? m_layer->renderBox()->clientWidth() : m_layer->renderBox()->clientHeight());
int increment = max(1, abs(m_layer->renderer()->style()->marqueeIncrement().calcValue(clientSize)));
int currentPos = (isHorizontal() ? m_layer->scrollXOffset() : m_layer->scrollYOffset());
newPos = currentPos + (addIncrement ? increment : -increment);
if (positive)
newPos = min(newPos, endPoint);
else
newPos = max(newPos, endPoint);
}
if (newPos == endPoint) {
m_currentLoop++;
if (m_totalLoops > 0 && m_currentLoop >= m_totalLoops)
m_timer.stop();
else if (s->marqueeBehavior() != MALTERNATE)
m_reset = true;
}
if (isHorizontal())
m_layer->scrollToXOffset(newPos);
else
m_layer->scrollToYOffset(newPos);
}
}