initial import
[vuplus_webkit] / Source / WebCore / dom / ContainerNode.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #ifndef ContainerNode_h
25 #define ContainerNode_h
26
27 #include "Node.h"
28
29 namespace WebCore {
30
31 class FloatPoint;
32     
33 typedef void (*NodeCallback)(Node*, unsigned);
34
35 namespace Private { 
36     template<class GenericNode, class GenericNodeContainer>
37     void addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer* container);
38 };
39
40 class ContainerNode : public Node {
41 public:
42     virtual ~ContainerNode();
43
44     Node* firstChild() const { return m_firstChild; }
45     Node* lastChild() const { return m_lastChild; }
46
47     bool insertBefore(PassRefPtr<Node> newChild, Node* refChild, ExceptionCode&, bool shouldLazyAttach = false);
48     bool replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionCode&, bool shouldLazyAttach = false);
49     bool removeChild(Node* child, ExceptionCode&);
50     bool appendChild(PassRefPtr<Node> newChild, ExceptionCode&, bool shouldLazyAttach = false);
51
52     // These methods are only used during parsing.
53     // They don't send DOM mutation events or handle reparenting.
54     // However, arbitrary code may be run by beforeload handlers.
55     void parserAddChild(PassRefPtr<Node>);
56     void parserRemoveChild(Node*);
57     void parserInsertBefore(PassRefPtr<Node> newChild, Node* refChild);
58
59     bool hasChildNodes() const { return m_firstChild; }
60     virtual void attach();
61     virtual void detach();
62     virtual void willRemove();
63     virtual LayoutRect getRect() const;
64     virtual void setFocus(bool = true);
65     virtual void setActive(bool active = true, bool pause = false);
66     virtual void setHovered(bool = true);
67     unsigned childNodeCount() const;
68     Node* childNode(unsigned index) const;
69
70     virtual void insertedIntoDocument();
71     virtual void removedFromDocument();
72     virtual void insertedIntoTree(bool deep);
73     virtual void removedFromTree(bool deep);
74     virtual void childrenChanged(bool createdByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
75
76     // FIXME: It's not good to have two functions with such similar names, especially public functions.
77     // How do removeChildren and removeAllChildren differ?
78     void removeChildren();
79     void removeAllChildren();
80     void takeAllChildrenFrom(ContainerNode*);
81
82     void cloneChildNodes(ContainerNode* clone);
83     
84     bool dispatchBeforeLoadEvent(const String& sourceURL);
85
86     virtual void scheduleSetNeedsStyleRecalc(StyleChangeType = FullStyleChange);
87
88     static void queuePostAttachCallback(NodeCallback, Node*, unsigned = 0);
89     static bool postAttachCallbacksAreSuspended();
90     
91 protected:
92     ContainerNode(Document*, ConstructionType = CreateContainer);
93
94     void suspendPostAttachCallbacks();
95     void resumePostAttachCallbacks();
96
97     template<class GenericNode, class GenericNodeContainer>
98     friend void appendChildToContainer(GenericNode* child, GenericNodeContainer* container);
99
100     template<class GenericNode, class GenericNodeContainer>
101     friend void Private::addChildNodesToDeletionQueue(GenericNode*& head, GenericNode*& tail, GenericNodeContainer* container);
102
103     void setFirstChild(Node* child) { m_firstChild = child; }
104     void setLastChild(Node* child) { m_lastChild = child; }
105
106 private:
107     void removeBetween(Node* previousChild, Node* nextChild, Node* oldChild);
108     void insertBeforeCommon(Node* nextChild, Node* oldChild);
109
110     static void dispatchPostAttachCallbacks();
111
112     bool getUpperLeftCorner(FloatPoint&) const;
113     bool getLowerRightCorner(FloatPoint&) const;
114
115     Node* m_firstChild;
116     Node* m_lastChild;
117 };
118
119 inline ContainerNode* toContainerNode(Node* node)
120 {
121     ASSERT(!node || node->isContainerNode());
122     return static_cast<ContainerNode*>(node);
123 }
124
125 inline const ContainerNode* toContainerNode(const Node* node)
126 {
127     ASSERT(!node || node->isContainerNode());
128     return static_cast<const ContainerNode*>(node);
129 }
130
131 // This will catch anyone doing an unnecessary cast.
132 void toContainerNode(const ContainerNode*);
133
134 inline ContainerNode::ContainerNode(Document* document, ConstructionType type)
135     : Node(document, type)
136     , m_firstChild(0)
137     , m_lastChild(0)
138 {
139 }
140
141 inline unsigned Node::childNodeCount() const
142 {
143     if (!isContainerNode())
144         return 0;
145     return toContainerNode(this)->childNodeCount();
146 }
147
148 inline Node* Node::childNode(unsigned index) const
149 {
150     if (!isContainerNode())
151         return 0;
152     return toContainerNode(this)->childNode(index);
153 }
154
155 inline Node* Node::firstChild() const
156 {
157     if (!isContainerNode())
158         return 0;
159     return toContainerNode(this)->firstChild();
160 }
161
162 inline Node* Node::lastChild() const
163 {
164     if (!isContainerNode())
165         return 0;
166     return toContainerNode(this)->lastChild();
167 }
168
169 } // namespace WebCore
170
171 #endif // ContainerNode_h