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 "ScrollingStateNode.h"
28
29#if ENABLE(THREADED_SCROLLING) || USE(COORDINATED_GRAPHICS)
30
31#include "ScrollingStateFixedNode.h"
32#include "ScrollingStateTree.h"
33#include "TextStream.h"
34
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39ScrollingStateNode::ScrollingStateNode(ScrollingStateTree* scrollingStateTree, ScrollingNodeID nodeID)
40    : m_scrollingStateTree(scrollingStateTree)
41    , m_nodeID(nodeID)
42    , m_changedProperties(0)
43    , m_parent(0)
44{
45}
46
47// This copy constructor is used for cloning nodes in the tree, and it doesn't make sense
48// to clone the relationship pointers, so don't copy that information from the original
49// node.
50ScrollingStateNode::ScrollingStateNode(const ScrollingStateNode& stateNode)
51    : m_scrollingStateTree(0)
52    , m_nodeID(stateNode.scrollingNodeID())
53    , m_changedProperties(stateNode.changedProperties())
54    , m_parent(0)
55{
56    // FIXME: why doesn't this set the GraphicsLayer?
57    setScrollPlatformLayer(stateNode.platformScrollLayer());
58}
59
60ScrollingStateNode::~ScrollingStateNode()
61{
62}
63
64PassOwnPtr<ScrollingStateNode> ScrollingStateNode::cloneAndReset()
65{
66    OwnPtr<ScrollingStateNode> clone = this->clone();
67
68    // Now that this node is cloned, reset our change properties.
69    resetChangedProperties();
70
71    cloneAndResetChildren(clone.get());
72    return clone.release();
73}
74
75void ScrollingStateNode::cloneAndResetChildren(ScrollingStateNode* clone)
76{
77    if (!m_children)
78        return;
79
80    size_t size = m_children->size();
81    for (size_t i = 0; i < size; ++i)
82        clone->appendChild(m_children->at(i)->cloneAndReset());
83}
84
85void ScrollingStateNode::appendChild(PassOwnPtr<ScrollingStateNode> childNode)
86{
87    childNode->setParent(this);
88
89    if (!m_children)
90        m_children = adoptPtr(new Vector<OwnPtr<ScrollingStateNode> >);
91
92    m_children->append(childNode);
93}
94
95void ScrollingStateNode::removeChild(ScrollingStateNode* node)
96{
97    if (!m_children)
98        return;
99
100    size_t index = m_children->find(node);
101
102    // The index will be notFound if the node to remove is a deeper-than-1-level descendant or
103    // if node is the root state node.
104    if (index != notFound) {
105        node->willBeRemovedFromStateTree();
106        m_children->remove(index);
107        return;
108    }
109
110    size_t size = m_children->size();
111    for (size_t i = 0; i < size; ++i)
112        m_children->at(i)->removeChild(node);
113}
114
115void ScrollingStateNode::willBeRemovedFromStateTree()
116{
117    ASSERT(m_scrollingStateTree);
118
119    m_scrollingStateTree->didRemoveNode(scrollingNodeID());
120
121    if (!m_children)
122        return;
123
124    size_t size = m_children->size();
125    for (size_t i = 0; i < size; ++i)
126        m_children->at(i)->willBeRemovedFromStateTree();
127}
128
129void ScrollingStateNode::writeIndent(TextStream& ts, int indent)
130{
131    for (int i = 0; i != indent; ++i)
132        ts << "  ";
133}
134
135void ScrollingStateNode::dump(TextStream& ts, int indent) const
136{
137    writeIndent(ts, indent);
138    dumpProperties(ts, indent);
139
140    if (m_children) {
141        writeIndent(ts, indent + 1);
142        size_t size = children()->size();
143        ts << "(children " << size << "\n";
144
145        for (size_t i = 0; i < size; i++)
146            m_children->at(i)->dump(ts, indent + 2);
147        writeIndent(ts, indent + 1);
148        ts << ")\n";
149    }
150
151    writeIndent(ts, indent);
152    ts << ")\n";
153}
154
155String ScrollingStateNode::scrollingStateTreeAsText() const
156{
157    TextStream ts;
158
159    dump(ts, 0);
160    return ts.release();
161}
162
163} // namespace WebCore
164
165#endif // ENABLE(THREADED_SCROLLING) || USE(COORDINATED_GRAPHICS)
166