1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ScrollingStateStickyNode.h"
28
29#if ENABLE(THREADED_SCROLLING) || USE(COORDINATED_GRAPHICS)
30
31#include "GraphicsLayer.h"
32#include "ScrollingStateTree.h"
33#include "TextStream.h"
34#include <wtf/OwnPtr.h>
35
36namespace WebCore {
37
38PassOwnPtr<ScrollingStateStickyNode> ScrollingStateStickyNode::create(ScrollingStateTree* stateTree, ScrollingNodeID nodeID)
39{
40    return adoptPtr(new ScrollingStateStickyNode(stateTree, nodeID));
41}
42
43ScrollingStateStickyNode::ScrollingStateStickyNode(ScrollingStateTree* tree, ScrollingNodeID nodeID)
44    : ScrollingStateNode(tree, nodeID)
45{
46}
47
48ScrollingStateStickyNode::ScrollingStateStickyNode(const ScrollingStateStickyNode& node)
49    : ScrollingStateNode(node)
50    , m_constraints(StickyPositionViewportConstraints(node.viewportConstraints()))
51{
52}
53
54ScrollingStateStickyNode::~ScrollingStateStickyNode()
55{
56}
57
58PassOwnPtr<ScrollingStateNode> ScrollingStateStickyNode::clone()
59{
60    return adoptPtr(new ScrollingStateStickyNode(*this));
61}
62
63void ScrollingStateStickyNode::updateConstraints(const StickyPositionViewportConstraints& constraints)
64{
65    if (m_constraints == constraints)
66        return;
67
68    m_constraints = constraints;
69    setPropertyChanged(ViewportConstraints);
70    m_scrollingStateTree->setHasChangedProperties(true);
71}
72
73void ScrollingStateStickyNode::syncLayerPositionForViewportRect(const LayoutRect& viewportRect)
74{
75    FloatPoint position = m_constraints.layerPositionForViewportRect(viewportRect);
76    graphicsLayer()->syncPosition(position);
77}
78
79void ScrollingStateStickyNode::dumpProperties(TextStream& ts, int indent) const
80{
81    ts << "(" << "Sticky node" << "\n";
82
83    if (m_constraints.anchorEdges()) {
84        writeIndent(ts, indent + 1);
85        ts << "(anchor edges: ";
86        if (m_constraints.hasAnchorEdge(ViewportConstraints::AnchorEdgeLeft))
87            ts << "AnchorEdgeLeft ";
88        if (m_constraints.hasAnchorEdge(ViewportConstraints::AnchorEdgeRight))
89            ts << "AnchorEdgeRight ";
90        if (m_constraints.hasAnchorEdge(ViewportConstraints::AnchorEdgeTop))
91            ts << "AnchorEdgeTop ";
92        if (m_constraints.hasAnchorEdge(ViewportConstraints::AnchorEdgeBottom))
93            ts << "AnchorEdgeBottom";
94        ts << ")\n";
95    }
96
97    if (m_constraints.hasAnchorEdge(ViewportConstraints::AnchorEdgeLeft)) {
98        writeIndent(ts, indent + 1);
99        ts << "(left offset " << m_constraints.leftOffset() << ")\n";
100    }
101    if (m_constraints.hasAnchorEdge(ViewportConstraints::AnchorEdgeRight)) {
102        writeIndent(ts, indent + 1);
103        ts << "(right offset " << m_constraints.rightOffset() << ")\n";
104    }
105    if (m_constraints.hasAnchorEdge(ViewportConstraints::AnchorEdgeTop)) {
106        writeIndent(ts, indent + 1);
107        ts << "(top offset " << m_constraints.topOffset() << ")\n";
108    }
109    if (m_constraints.hasAnchorEdge(ViewportConstraints::AnchorEdgeBottom)) {
110        writeIndent(ts, indent + 1);
111        ts << "(bottom offset " << m_constraints.bottomOffset() << ")\n";
112    }
113
114    writeIndent(ts, indent + 1);
115    FloatRect r = m_constraints.absoluteContainingBlockRect();
116    ts << "(containing block rect " << r.x() << ", " << r.y() << " " << r.width() << " x " << r.height() << ")\n";
117
118    writeIndent(ts, indent + 1);
119    r = m_constraints.absoluteStickyBoxRect();
120    ts << "(sticky box rect " << r.x() << " " << r.y() << " " << r.width() << " " << r.height() << ")\n";
121
122    writeIndent(ts, indent + 1);
123    r = m_constraints.absoluteStickyBoxRect();
124    ts << "(sticky box rect " << r.x() << " " << r.y() << " " << r.width() << " " << r.height() << ")\n";
125
126    writeIndent(ts, indent + 1);
127    ts << "(sticky offset at last layout " << m_constraints.stickyOffsetAtLastLayout().width() << " " << m_constraints.stickyOffsetAtLastLayout().height() << ")\n";
128
129    writeIndent(ts, indent + 1);
130    ts << "(layer position at last layout " << m_constraints.layerPositionAtLastLayout().x() << " " << m_constraints.layerPositionAtLastLayout().y() << ")\n";
131}
132
133} // namespace WebCore
134
135#endif // ENABLE(THREADED_SCROLLING) || USE(COORDINATED_GRAPHICS)
136