2 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "InlineBox.h"
23 #include "HitTestResult.h"
24 #include "InlineFlowBox.h"
25 #include "PaintInfo.h"
26 #include "RenderArena.h"
27 #include "RenderBlock.h"
28 #include "RootInlineBox.h"
39 static bool inInlineBoxDetach;
44 InlineBox::~InlineBox()
46 if (!m_hasBadParent && m_parent)
47 m_parent->setHasBadChildList();
52 void InlineBox::remove()
55 parent()->removeChild(this);
58 void InlineBox::destroy(RenderArena* renderArena)
61 inInlineBoxDetach = true;
65 inInlineBoxDetach = false;
68 // Recover the size left there for us by operator delete and free the memory.
69 renderArena->free(*(size_t *)this, this);
72 void* InlineBox::operator new(size_t sz, RenderArena* renderArena) throw()
74 return renderArena->allocate(sz);
77 void InlineBox::operator delete(void* ptr, size_t sz)
79 ASSERT(inInlineBoxDetach);
81 // Stash size where destroy can find it.
86 const char* InlineBox::boxName() const
91 void InlineBox::showTreeForThis() const
94 m_renderer->showTreeForThis();
97 void InlineBox::showLineTreeForThis() const
100 m_renderer->containingBlock()->showLineTreeAndMark(this, "*");
103 void InlineBox::showLineTreeAndMark(const InlineBox* markedBox1, const char* markedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const RenderObject* obj, int depth) const
105 int printedCharacters = 0;
106 if (this == markedBox1)
107 printedCharacters += fprintf(stderr, "%s", markedLabel1);
108 if (this == markedBox2)
109 printedCharacters += fprintf(stderr, "%s", markedLabel2);
110 if (renderer() == obj)
111 printedCharacters += fprintf(stderr, "*");
112 for (; printedCharacters < depth * 2; printedCharacters++)
115 showBox(printedCharacters);
118 void InlineBox::showBox(int printedCharacters) const
120 printedCharacters += fprintf(stderr, "%s\t%p", boxName(), this);
121 for (; printedCharacters < showTreeCharacterOffset; printedCharacters++)
123 fprintf(stderr, "\t%s %p\n", renderer() ? renderer()->renderName() : "No Renderer", renderer());
127 LayoutUnit InlineBox::logicalHeight() const
130 if (hasVirtualLogicalHeight())
131 return virtualLogicalHeight();
134 if (renderer()->isText())
135 return m_isText ? renderer()->style(m_firstLine)->fontMetrics().height() : 0;
136 if (renderer()->isBox() && parent())
137 return isHorizontal() ? toRenderBox(m_renderer)->height() : toRenderBox(m_renderer)->width();
139 ASSERT(isInlineFlowBox());
140 RenderBoxModelObject* flowObject = boxModelObject();
141 const FontMetrics& fontMetrics = renderer()->style(m_firstLine)->fontMetrics();
142 LayoutUnit result = fontMetrics.height();
144 result += flowObject->borderAndPaddingLogicalHeight();
148 int InlineBox::caretMinOffset() const
150 return m_renderer->caretMinOffset();
153 int InlineBox::caretMaxOffset() const
155 return m_renderer->caretMaxOffset();
158 unsigned InlineBox::caretMaxRenderedOffset() const
163 void InlineBox::dirtyLineBoxes()
166 for (InlineFlowBox* curr = parent(); curr && !curr->isDirty(); curr = curr->parent())
170 void InlineBox::deleteLine(RenderArena* arena)
172 if (!m_extracted && m_renderer->isBox())
173 toRenderBox(m_renderer)->setInlineBoxWrapper(0);
177 void InlineBox::extractLine()
180 if (m_renderer->isBox())
181 toRenderBox(m_renderer)->setInlineBoxWrapper(0);
184 void InlineBox::attachLine()
187 if (m_renderer->isBox())
188 toRenderBox(m_renderer)->setInlineBoxWrapper(this);
191 void InlineBox::adjustPosition(float dx, float dy)
193 m_topLeft.move(dx, dy);
195 if (m_renderer->isReplaced())
196 toRenderBox(m_renderer)->move(dx, dy);
199 void InlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit /* lineTop */, LayoutUnit /*lineBottom*/)
201 if (!paintInfo.shouldPaintWithinRoot(renderer()) || (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection))
204 LayoutPoint childPoint = paintOffset;
205 if (parent()->renderer()->style()->isFlippedBlocksWritingMode()) // Faster than calling containingBlock().
206 childPoint = renderer()->containingBlock()->flipForWritingMode(toRenderBox(renderer()), childPoint, RenderBox::ParentToChildFlippingAdjustment);
208 // Paint all phases of replaced elements atomically, as though the replaced element established its
209 // own stacking context. (See Appendix E.2, section 6.4 on inline block/table elements in the CSS2.1
211 bool preservePhase = paintInfo.phase == PaintPhaseSelection || paintInfo.phase == PaintPhaseTextClip;
212 PaintInfo info(paintInfo);
213 info.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
214 renderer()->paint(info, childPoint);
215 if (!preservePhase) {
216 info.phase = PaintPhaseChildBlockBackgrounds;
217 renderer()->paint(info, childPoint);
218 info.phase = PaintPhaseFloat;
219 renderer()->paint(info, childPoint);
220 info.phase = PaintPhaseForeground;
221 renderer()->paint(info, childPoint);
222 info.phase = PaintPhaseOutline;
223 renderer()->paint(info, childPoint);
227 bool InlineBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit /* lineTop */, LayoutUnit /*lineBottom*/)
229 // Hit test all phases of replaced elements atomically, as though the replaced element established its
230 // own stacking context. (See Appendix E.2, section 6.4 on inline block/table elements in the CSS2.1
232 return renderer()->hitTest(request, result, pointInContainer, accumulatedOffset);
235 const RootInlineBox* InlineBox::root() const
238 return m_parent->root();
239 ASSERT(isRootInlineBox());
240 return static_cast<const RootInlineBox*>(this);
243 RootInlineBox* InlineBox::root()
246 return m_parent->root();
247 ASSERT(isRootInlineBox());
248 return static_cast<RootInlineBox*>(this);
251 bool InlineBox::nextOnLineExists() const
253 if (!m_determinedIfNextOnLineExists) {
254 m_determinedIfNextOnLineExists = true;
257 m_nextOnLineExists = false;
258 else if (nextOnLine())
259 m_nextOnLineExists = true;
261 m_nextOnLineExists = parent()->nextOnLineExists();
263 return m_nextOnLineExists;
266 bool InlineBox::prevOnLineExists() const
268 if (!m_determinedIfPrevOnLineExists) {
269 m_determinedIfPrevOnLineExists = true;
272 m_prevOnLineExists = false;
273 else if (prevOnLine())
274 m_prevOnLineExists = true;
276 m_prevOnLineExists = parent()->prevOnLineExists();
278 return m_prevOnLineExists;
281 InlineBox* InlineBox::nextLeafChild() const
284 for (InlineBox* box = nextOnLine(); box && !leaf; box = box->nextOnLine())
285 leaf = box->isLeaf() ? box : toInlineFlowBox(box)->firstLeafChild();
286 if (!leaf && parent())
287 leaf = parent()->nextLeafChild();
291 InlineBox* InlineBox::prevLeafChild() const
294 for (InlineBox* box = prevOnLine(); box && !leaf; box = box->prevOnLine())
295 leaf = box->isLeaf() ? box : toInlineFlowBox(box)->lastLeafChild();
296 if (!leaf && parent())
297 leaf = parent()->prevLeafChild();
301 RenderObject::SelectionState InlineBox::selectionState()
303 return renderer()->selectionState();
306 bool InlineBox::canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth)
308 // Non-replaced elements can always accommodate an ellipsis.
309 if (!m_renderer || !m_renderer->isReplaced())
312 IntRect boxRect(left(), 0, m_logicalWidth, 10);
313 IntRect ellipsisRect(ltr ? blockEdge - ellipsisWidth : blockEdge, 0, ellipsisWidth, 10);
314 return !(boxRect.intersects(ellipsisRect));
317 float InlineBox::placeEllipsisBox(bool, float, float, float, bool&)
319 // Use -1 to mean "we didn't set the position."
323 void InlineBox::clearKnownToHaveNoOverflow()
325 m_knownToHaveNoOverflow = false;
326 if (parent() && parent()->knownToHaveNoOverflow())
327 parent()->clearKnownToHaveNoOverflow();
330 FloatPoint InlineBox::locationIncludingFlipping()
332 if (!renderer()->style()->isFlippedBlocksWritingMode())
333 return FloatPoint(x(), y());
334 RenderBlock* block = root()->block();
335 if (block->style()->isHorizontalWritingMode())
336 return FloatPoint(x(), block->height() - height() - y());
338 return FloatPoint(block->width() - width() - x(), y());
341 void InlineBox::flipForWritingMode(FloatRect& rect)
343 if (!renderer()->style()->isFlippedBlocksWritingMode())
345 root()->block()->flipForWritingMode(rect);
348 FloatPoint InlineBox::flipForWritingMode(const FloatPoint& point)
350 if (!renderer()->style()->isFlippedBlocksWritingMode())
352 return root()->block()->flipForWritingMode(point);
355 void InlineBox::flipForWritingMode(IntRect& rect)
357 if (!renderer()->style()->isFlippedBlocksWritingMode())
359 root()->block()->flipForWritingMode(rect);
362 IntPoint InlineBox::flipForWritingMode(const IntPoint& point)
364 if (!renderer()->style()->isFlippedBlocksWritingMode())
366 return root()->block()->flipForWritingMode(point);
369 } // namespace WebCore
373 void showTree(const WebCore::InlineBox* b)
376 b->showTreeForThis();
379 void showLineTree(const WebCore::InlineBox* b)
382 b->showLineTreeForThis();