1 /*
2 * Copyright (C) 2007 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "LayoutState.h"
28
29 #include "RenderArena.h"
30 #include "RenderInline.h"
31 #include "RenderLayer.h"
32 #include "RenderView.h"
33
34 namespace WebCore {
35
LayoutState(LayoutState * prev,RenderBox * renderer,const IntSize & offset)36 LayoutState::LayoutState(LayoutState* prev, RenderBox* renderer, const IntSize& offset)
37 : m_next(prev)
38 #ifndef NDEBUG
39 , m_renderer(renderer)
40 #endif
41 {
42 ASSERT(m_next);
43
44 bool fixed = renderer->isPositioned() && renderer->style()->position() == FixedPosition;
45 if (fixed) {
46 // FIXME: This doesn't work correctly with transforms.
47 FloatPoint fixedOffset = renderer->view()->localToAbsolute(FloatPoint(), true);
48 m_offset = IntSize(fixedOffset.x(), fixedOffset.y()) + offset;
49 } else
50 m_offset = prev->m_offset + offset;
51
52 if (renderer->isRelPositioned()) {
53 if (renderer->hasLayer())
54 m_offset += renderer->layer()->relativePositionOffset();
55 } else if (renderer->isPositioned() && !fixed) {
56 if (RenderObject* container = renderer->container()) {
57 if (container->isRelPositioned() && container->isRenderInline())
58 m_offset += toRenderInline(container)->relativePositionedInlineOffset(renderer);
59 }
60 }
61
62 m_clipped = !fixed && prev->m_clipped;
63 if (m_clipped)
64 m_clipRect = prev->m_clipRect;
65 if (renderer->hasOverflowClip()) {
66 int x = m_offset.width();
67 int y = m_offset.height();
68 RenderLayer* layer = renderer->layer();
69 IntRect clipRect(x, y, layer->width(), layer->height());
70 clipRect.move(renderer->view()->layoutDelta());
71 if (m_clipped)
72 m_clipRect.intersect(clipRect);
73 else {
74 m_clipRect = clipRect;
75 m_clipped = true;
76 }
77 layer->subtractScrolledContentOffset(x, y);
78 m_offset = IntSize(x, y);
79 }
80
81 m_layoutDelta = m_next->m_layoutDelta;
82
83 // FIXME: <http://bugs.webkit.org/show_bug.cgi?id=13443> Apply control clip if present.
84 }
85
LayoutState(RenderObject * root)86 LayoutState::LayoutState(RenderObject* root)
87 : m_clipped(false)
88 , m_next(0)
89 #ifndef NDEBUG
90 , m_renderer(root)
91 #endif
92 {
93 RenderObject* container = root->container();
94 FloatPoint absContentPoint = container->localToAbsolute(FloatPoint(), false, true);
95 m_offset = IntSize(absContentPoint.x(), absContentPoint.y());
96 }
97
98 #ifndef NDEBUG
99 static bool inLayoutStateDestroy;
100 #endif
101
destroy(RenderArena * renderArena)102 void LayoutState::destroy(RenderArena* renderArena)
103 {
104 #ifndef NDEBUG
105 inLayoutStateDestroy = true;
106 #endif
107 delete this;
108 #ifndef NDEBUG
109 inLayoutStateDestroy = false;
110 #endif
111 renderArena->free(*(size_t*)this, this);
112 }
113
operator new(size_t sz,RenderArena * renderArena)114 void* LayoutState::operator new(size_t sz, RenderArena* renderArena) throw()
115 {
116 return renderArena->allocate(sz);
117 }
118
operator delete(void * ptr,size_t sz)119 void LayoutState::operator delete(void* ptr, size_t sz)
120 {
121 ASSERT(inLayoutStateDestroy);
122 *(size_t*)ptr = sz;
123 }
124
125 } // namespace WebCore
126