2 * This file is part of the html renderer for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * (C) 2000 Dirk Mueller (mueller@kde.org)
7 * Copyright (C) 2004 Apple Computer, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 #include "rendering/render_object.h"
27 #include "rendering/render_table.h"
28 #include "rendering/render_text.h"
29 #include "rendering/render_line.h"
30 #include "rendering/render_list.h"
31 #include "rendering/render_canvas.h"
32 #include "xml/dom_elementimpl.h"
33 #include "xml/dom2_eventsimpl.h"
34 #include "xml/dom_docimpl.h"
35 #include "xml/dom_position.h"
36 #include "css/cssstyleselector.h"
37 #include "misc/htmlhashes.h"
40 #include "khtmlview.h"
41 #include "khtml_part.h"
42 #include "render_arena.h"
43 #include "render_inline.h"
44 #include "render_block.h"
45 #include "render_flexbox.h"
49 #include "KWQAccObjectCache.h"
54 using namespace khtml;
57 static void *baseOfRenderObjectBeingDeleted;
60 void* RenderObject::operator new(size_t sz, RenderArena* renderArena) throw()
62 return renderArena->allocate(sz);
65 void RenderObject::operator delete(void* ptr, size_t sz)
67 assert(baseOfRenderObjectBeingDeleted == ptr);
69 // Stash size where detach can find it.
73 RenderObject *RenderObject::createObject(DOM::NodeImpl* node, RenderStyle* style)
76 RenderArena* arena = node->getDocument()->renderArena();
77 switch(style->display())
82 o = new (arena) RenderInline(node);
85 o = new (arena) RenderBlock(node);
88 o = new (arena) RenderBlock(node);
91 o = new (arena) RenderListItem(node);
95 o = new (arena) RenderBlock(node);
99 //kdDebug( 6040 ) << "creating RenderTable" << endl;
100 o = new (arena) RenderTable(node);
102 case TABLE_ROW_GROUP:
103 case TABLE_HEADER_GROUP:
104 case TABLE_FOOTER_GROUP:
105 o = new (arena) RenderTableSection(node);
108 o = new (arena) RenderTableRow(node);
110 case TABLE_COLUMN_GROUP:
112 o = new (arena) RenderTableCol(node);
115 o = new (arena) RenderTableCell(node);
118 o = new (arena) RenderBlock(node);
122 o = new (arena) RenderFlexibleBox(node);
128 RenderObject::RenderObject(DOM::NodeImpl* node)
129 : CachedObjectClient(),
135 m_verticalPosition( PositionUndefined ),
136 m_needsLayout( false ),
137 m_normalChildNeedsLayout( false ),
138 m_posChildNeedsLayout( false ),
139 m_minMaxKnown( false ),
142 m_positioned( false ),
143 m_relPositioned( false ),
144 m_paintBackground( false ),
146 m_isAnonymous( node == node->getDocument() ),
147 m_recalcMinMax( false ),
152 m_mouseInside( false ),
153 m_isDragging( false ),
154 m_hasOverflowClip(false)
158 RenderObject::~RenderObject()
162 bool RenderObject::hasAncestor(const RenderObject *obj) const
164 for (const RenderObject *r = this; r; r = r->m_parent)
170 bool RenderObject::isRoot() const
172 return element() && element()->renderer() == this &&
173 element()->getDocument()->documentElement() == element();
176 bool RenderObject::isBody() const
178 return element() && element()->renderer() == this && element()->id() == ID_BODY;
181 bool RenderObject::isHR() const
183 return element() && element()->id() == ID_HR;
186 bool RenderObject::isHTMLMarquee() const
188 return element() && element()->renderer() == this && element()->id() == ID_MARQUEE;
191 bool RenderObject::canHaveChildren() const
196 RenderFlow* RenderObject::continuation() const
201 bool RenderObject::isInlineContinuation() const
206 void RenderObject::addChild(RenderObject* , RenderObject *)
211 RenderObject* RenderObject::removeChildNode(RenderObject* )
217 void RenderObject::removeChild(RenderObject* )
222 void RenderObject::appendChildNode(RenderObject*)
227 void RenderObject::insertChildNode(RenderObject*, RenderObject*)
232 RenderObject *RenderObject::nextRenderer() const
236 else if (nextSibling())
237 return nextSibling();
239 const RenderObject *r = this;
240 while (r && !r->nextSibling())
243 return r->nextSibling();
248 RenderObject *RenderObject::previousRenderer() const
250 if (previousSibling()) {
251 RenderObject *r = previousSibling();
252 while (r->lastChild())
264 bool RenderObject::isEditable() const
266 RenderText *textRenderer = 0;
268 textRenderer = static_cast<RenderText *>(const_cast<RenderObject *>(this));
271 return style()->visibility() == VISIBLE &&
272 element() && element()->isContentEditable() &&
273 ((isBlockFlow() && !firstChild()) ||
276 (textRenderer && textRenderer->firstTextBox()));
279 RenderObject *RenderObject::nextEditable() const
281 RenderObject *r = const_cast<RenderObject *>(this);
282 RenderObject *n = firstChild();
291 return r->nextEditable();
293 n = r->nextSibling();
303 return r->nextEditable();
308 n = r->nextSibling();
319 return r->nextEditable();
326 RenderObject *RenderObject::previousEditable() const
328 RenderObject *r = const_cast<RenderObject *>(this);
329 RenderObject *n = firstChild();
338 return r->previousEditable();
340 n = r->previousSibling();
350 return r->previousEditable();
355 n = r->previousSibling();
366 return r->previousEditable();
373 RenderObject *RenderObject::firstLeafChild() const
375 RenderObject *r = firstChild();
386 RenderObject *RenderObject::lastLeafChild() const
388 RenderObject *r = lastChild();
399 static void addLayers(RenderObject* obj, RenderLayer* parentLayer, RenderObject*& newObject,
400 RenderLayer*& beforeChild)
403 if (!beforeChild && newObject) {
404 // We need to figure out the layer that follows newObject. We only do
405 // this the first time we find a child layer, and then we update the
406 // pointer values for newObject and beforeChild used by everyone else.
407 beforeChild = newObject->parent()->findNextLayer(parentLayer, newObject);
410 parentLayer->addChild(obj->layer(), beforeChild);
414 for (RenderObject* curr = obj->firstChild(); curr; curr = curr->nextSibling())
415 addLayers(curr, parentLayer, newObject, beforeChild);
418 void RenderObject::addLayers(RenderLayer* parentLayer, RenderObject* newObject)
423 RenderObject* object = newObject;
424 RenderLayer* beforeChild = 0;
425 ::addLayers(this, parentLayer, object, beforeChild);
428 void RenderObject::removeLayers(RenderLayer* parentLayer)
434 parentLayer->removeChild(layer());
438 for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
439 curr->removeLayers(parentLayer);
442 void RenderObject::moveLayers(RenderLayer* oldParent, RenderLayer* newParent)
449 oldParent->removeChild(layer());
450 newParent->addChild(layer());
454 for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
455 curr->moveLayers(oldParent, newParent);
458 RenderLayer* RenderObject::findNextLayer(RenderLayer* parentLayer, RenderObject* startPoint,
461 // Error check the parent layer passed in. If it's null, we can't find anything.
465 // Step 1: Descend into our siblings trying to find the next layer. If we do find
466 // a layer, and if its parent layer matches our desired parent layer, then we have
468 for (RenderObject* curr = startPoint ? startPoint->nextSibling() : firstChild();
469 curr; curr = curr->nextSibling()) {
470 RenderLayer* nextLayer = curr->findNextLayer(parentLayer, 0, false);
472 if (nextLayer->parent() == parentLayer)
478 // Step 2: If our layer is the desired parent layer, then we're finished. We didn't
480 RenderLayer* ourLayer = layer();
481 if (parentLayer == ourLayer)
484 // Step 3: If we have a layer, then return that layer. It will be checked against
485 // the desired parent layer in the for loop above.
489 // Step 4: If |checkParent| is set, climb up to our parent and check its siblings that
490 // follow us to see if we can locate a layer.
491 if (checkParent && parent())
492 return parent()->findNextLayer(parentLayer, this, true);
497 RenderLayer* RenderObject::enclosingLayer()
499 RenderObject* curr = this;
501 RenderLayer *layer = curr->layer();
504 curr = curr->parent();
509 bool RenderObject::requiresLayer()
511 return isRoot() || isPositioned() || isRelPositioned() || style()->opacity() < 1.0f ||
515 RenderBlock* RenderObject::firstLineBlock() const
520 void RenderObject::updateFirstLetter()
523 int RenderObject::offsetLeft() const
526 if (!isPositioned()) {
527 if (isRelPositioned()) {
529 ((RenderBox*)this)->relativePositionOffset(x, y);
532 RenderObject* offsetPar = offsetParent();
533 RenderObject* curr = parent();
534 while (curr && curr != offsetPar) {
536 curr = curr->parent();
542 int RenderObject::offsetTop() const
545 if (!isPositioned()) {
546 if (isRelPositioned()) {
548 ((RenderBox*)this)->relativePositionOffset(x, y);
550 RenderObject* offsetPar = offsetParent();
551 RenderObject* curr = parent();
552 while (curr && curr != offsetPar) {
554 curr = curr->parent();
560 RenderObject* RenderObject::offsetParent() const
562 bool skipTables = isPositioned() || isRelPositioned();
563 RenderObject* curr = parent();
564 while (curr && !curr->isPositioned() && !curr->isRelPositioned() &&
566 if (!skipTables && (curr->isTableCell() || curr->isTable()))
568 curr = curr->parent();
573 // More IE extensions. clientWidth and clientHeight represent the interior of an object
574 // excluding border and scrollbar.
576 RenderObject::clientWidth() const
578 return width() - borderLeft() - borderRight() -
579 (includeScrollbarSize() ? layer()->verticalScrollbarWidth() : 0);
583 RenderObject::clientHeight() const
585 return height() - borderTop() - borderBottom() -
586 (includeScrollbarSize() ? layer()->horizontalScrollbarHeight() : 0);
589 // scrollWidth/scrollHeight will be the same as clientWidth/clientHeight unless the
590 // object has overflow:hidden/scroll/auto specified and also has overflow.
592 RenderObject::scrollWidth() const
594 return hasOverflowClip() ? layer()->scrollWidth() : overflowWidth();
598 RenderObject::scrollHeight() const
600 return hasOverflowClip() ? layer()->scrollHeight() : overflowHeight();
603 bool RenderObject::scroll(KWQScrollDirection direction, KWQScrollGranularity granularity, float multiplier)
605 RenderLayer *l = layer();
606 if (l != 0 && l->scroll(direction, granularity, multiplier)) {
609 RenderBlock *b = containingBlock();
610 if (b != 0 && !b->isCanvas()) {
611 return b->scroll(direction, granularity, multiplier);
617 RenderObject::hasStaticX() const
619 return (style()->left().isVariable() && style()->right().isVariable()) ||
620 style()->left().isStatic() ||
621 style()->right().isStatic();
625 RenderObject::hasStaticY() const
627 return (style()->top().isVariable() && style()->bottom().isVariable()) || style()->top().isStatic();
630 void RenderObject::markAllDescendantsWithFloatsForLayout(RenderObject*)
634 void RenderObject::setNeedsLayout(bool b, bool markParents)
636 bool alreadyNeededLayout = m_needsLayout;
639 if (!alreadyNeededLayout && markParents)
640 markContainingBlocksForLayout();
643 m_posChildNeedsLayout = false;
644 m_normalChildNeedsLayout = false;
648 void RenderObject::setChildNeedsLayout(bool b, bool markParents)
650 bool alreadyNeededLayout = m_normalChildNeedsLayout;
651 m_normalChildNeedsLayout = b;
653 if (!alreadyNeededLayout && markParents)
654 markContainingBlocksForLayout();
657 m_posChildNeedsLayout = false;
658 m_normalChildNeedsLayout = false;
662 void RenderObject::markContainingBlocksForLayout()
664 RenderObject *o = container();
665 RenderObject *last = this;
668 if (!last->isText() && (last->style()->position() == FIXED || last->style()->position() == ABSOLUTE)) {
669 if (o->m_posChildNeedsLayout)
671 o->m_posChildNeedsLayout = true;
674 if (o->m_normalChildNeedsLayout)
676 o->m_normalChildNeedsLayout = true;
683 last->scheduleRelayout();
686 RenderBlock* RenderObject::containingBlock() const
689 return static_cast<const RenderTableCell *>(this)->table();
691 return (RenderBlock*)this;
693 RenderObject *o = parent();
694 if (!isText() && m_style->position() == FIXED) {
695 while ( o && !o->isCanvas() )
698 else if (!isText() && m_style->position() == ABSOLUTE) {
699 while (o && (o->style()->position() == STATIC || (o->isInline() && !o->isReplaced()))
700 && !o->isRoot() && !o->isCanvas()) {
701 // For relpositioned inlines, we return the nearest enclosing block. We don't try
702 // to return the inline itself. This allows us to avoid having a positioned objects
703 // list in all RenderInlines and lets us return a strongly-typed RenderBlock* result
704 // from this method. The container() method can actually be used to obtain the
706 if (o->style()->position() == RELATIVE && o->isInline() && !o->isReplaced())
707 return o->containingBlock();
711 while (o && ((o->isInline() && !o->isReplaced()) || o->isTableRow() || o->isTableSection()
712 || o->isTableCol() || o->isFrameSet()))
716 if (!o || !o->isRenderBlock())
717 return 0; // Probably doesn't happen any more, but leave just in case. -dwh
719 return static_cast<RenderBlock*>(o);
722 int RenderObject::containingBlockWidth() const
725 return containingBlock()->contentWidth();
728 int RenderObject::containingBlockHeight() const
731 return containingBlock()->contentHeight();
734 bool RenderObject::sizesToMaxWidth() const
736 // Marquees in WinIE are like a mixture of blocks and inline-blocks. They size as though they're blocks,
737 // but they allow text to sit on the same line as the marquee.
738 if (isFloating() || (isCompact() && isInline()) ||
739 (isInlineBlockOrInlineTable() && !isHTMLMarquee()) ||
740 (element() && (element()->id() == ID_BUTTON || element()->id() == ID_LEGEND)))
743 // Children of a horizontal marquee do not fill the container by default.
744 // FIXME: Need to deal with MAUTO value properly. It could be vertical.
745 if (parent()->style()->overflow() == OMARQUEE) {
746 EMarqueeDirection dir = parent()->style()->marqueeDirection();
747 if (dir == MAUTO || dir == MFORWARD || dir == MBACKWARD || dir == MLEFT || dir == MRIGHT)
751 // Flexible horizontal boxes lay out children at their maxwidths. Also vertical boxes
752 // that don't stretch their kids lay out their children at their maxwidths.
753 if (parent()->isFlexibleBox() &&
754 (parent()->style()->boxOrient() == HORIZONTAL || parent()->style()->boxAlign() != BSTRETCH))
760 void RenderObject::drawBorder(QPainter *p, int x1, int y1, int x2, int y2,
761 BorderSide s, QColor c, const QColor& textcolor, EBorderStyle style,
762 int adjbw1, int adjbw2, bool invalidisInvert)
764 int width = (s==BSTop||s==BSBottom?y2-y1:x2-x1);
766 if(style == DOUBLE && width < 3)
772 p->setRasterOp(Qt::XorROP);
776 if(style == INSET || style == OUTSET || style == RIDGE || style ==
778 c.setRgb(238, 238, 238);
789 if(invalidisInvert && p->rasterOp() == Qt::XorROP)
790 p->setRasterOp(Qt::CopyROP);
794 p->setPen(QPen(c, width == 1 ? 0 : width, Qt::DotLine));
798 p->setPen(QPen(c, width == 1 ? 0 : width, Qt::DashLine));
805 p->drawLine(x1, (y1+y2)/2, x2, (y1+y2)/2);
809 p->drawLine((x1+x2)/2, y1, (x1+x2)/2, y2);
817 int third = (width+1)/3;
819 if (adjbw1 == 0 && adjbw2 == 0)
821 p->setPen(Qt::NoPen);
827 p->drawRect(x1, y1 , x2-x1, third);
828 p->drawRect(x1, y2-third, x2-x1, third);
831 p->drawRect(x1 , y1+1, third, y2-y1-1);
832 p->drawRect(x2-third, y1+1, third, y2-y1-1);
835 p->drawRect(x1 , y1+1, third, y2-y1-1);
836 p->drawRect(x2-third, y1+1, third, y2-y1-1);
843 if (adjbw1>0) adjbw1bigthird = adjbw1+1;
844 else adjbw1bigthird = adjbw1 - 1;
848 if (adjbw2>0) adjbw2bigthird = adjbw2 + 1;
849 else adjbw2bigthird = adjbw2 - 1;
855 drawBorder(p, x1+QMAX((-adjbw1*2+1)/3,0), y1 , x2-QMAX((-adjbw2*2+1)/3,0), y1 + third, s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
856 drawBorder(p, x1+QMAX(( adjbw1*2+1)/3,0), y2 - third, x2-QMAX(( adjbw2*2+1)/3,0), y2 , s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
859 drawBorder(p, x1 , y1+QMAX((-adjbw1*2+1)/3,0), x1+third, y2-QMAX((-adjbw2*2+1)/3,0), s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
860 drawBorder(p, x2 - third, y1+QMAX(( adjbw1*2+1)/3,0), x2 , y2-QMAX(( adjbw2*2+1)/3,0), s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
863 drawBorder(p, x1+QMAX(( adjbw1*2+1)/3,0), y1 , x2-QMAX(( adjbw2*2+1)/3,0), y1+third, s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
864 drawBorder(p, x1+QMAX((-adjbw1*2+1)/3,0), y2-third, x2-QMAX((-adjbw2*2+1)/3,0), y2 , s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
867 drawBorder(p, x1 , y1+QMAX(( adjbw1*2+1)/3,0), x1+third, y2-QMAX(( adjbw2*2+1)/3,0), s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
868 drawBorder(p, x2-third, y1+QMAX((-adjbw1*2+1)/3,0), x2 , y2-QMAX((-adjbw2*2+1)/3,0), s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
894 if (adjbw1>0) adjbw1bighalf=adjbw1+1;
895 else adjbw1bighalf=adjbw1-1;
898 if (adjbw2>0) adjbw2bighalf=adjbw2+1;
899 else adjbw2bighalf=adjbw2-1;
905 drawBorder(p, x1+QMAX(-adjbw1 ,0)/2, y1 , x2-QMAX(-adjbw2,0)/2, (y1+y2+1)/2, s, c, textcolor, s1, adjbw1bighalf, adjbw2bighalf);
906 drawBorder(p, x1+QMAX( adjbw1+1,0)/2, (y1+y2+1)/2, x2-QMAX( adjbw2+1,0)/2, y2 , s, c, textcolor, s2, adjbw1/2, adjbw2/2);
909 drawBorder(p, x1 , y1+QMAX(-adjbw1 ,0)/2, (x1+x2+1)/2, y2-QMAX(-adjbw2,0)/2, s, c, textcolor, s1, adjbw1bighalf, adjbw2bighalf);
910 drawBorder(p, (x1+x2+1)/2, y1+QMAX( adjbw1+1,0)/2, x2 , y2-QMAX( adjbw2+1,0)/2, s, c, textcolor, s2, adjbw1/2, adjbw2/2);
913 drawBorder(p, x1+QMAX( adjbw1 ,0)/2, y1 , x2-QMAX( adjbw2,0)/2, (y1+y2+1)/2, s, c, textcolor, s2, adjbw1bighalf, adjbw2bighalf);
914 drawBorder(p, x1+QMAX(-adjbw1+1,0)/2, (y1+y2+1)/2, x2-QMAX(-adjbw2+1,0)/2, y2 , s, c, textcolor, s1, adjbw1/2, adjbw2/2);
917 drawBorder(p, x1 , y1+QMAX( adjbw1 ,0)/2, (x1+x2+1)/2, y2-QMAX( adjbw2,0)/2, s, c, textcolor, s2, adjbw1bighalf, adjbw2bighalf);
918 drawBorder(p, (x1+x2+1)/2, y1+QMAX(-adjbw1+1,0)/2, x2 , y2-QMAX(-adjbw2+1,0)/2, s, c, textcolor, s1, adjbw1/2, adjbw2/2);
924 if(s == BSTop || s == BSLeft)
929 if(style == OUTSET && (s == BSBottom || s == BSRight))
934 p->setPen(Qt::NoPen);
938 if (adjbw1==0 && adjbw2 == 0)
940 p->drawRect(x1,y1,x2-x1,y2-y1);
946 x1+QMAX(-adjbw1,0), y1,
947 x1+QMAX( adjbw1,0), y2,
948 x2-QMAX( adjbw2,0), y2,
949 x2-QMAX(-adjbw2,0), y1);
953 x1+QMAX( adjbw1,0), y1,
954 x1+QMAX(-adjbw1,0), y2,
955 x2-QMAX(-adjbw2,0), y2,
956 x2-QMAX( adjbw2,0), y1);
960 x1, y1+QMAX(-adjbw1,0),
961 x1, y2-QMAX(-adjbw2,0),
962 x2, y2-QMAX( adjbw2,0),
963 x2, y1+QMAX( adjbw1,0));
967 x1, y1+QMAX( adjbw1,0),
968 x1, y2-QMAX( adjbw2,0),
969 x2, y2-QMAX(-adjbw2,0),
970 x2, y1+QMAX(-adjbw1,0));
973 p->drawConvexPolygon(quad);
977 if(invalidisInvert && p->rasterOp() == Qt::XorROP)
978 p->setRasterOp(Qt::CopyROP);
981 void RenderObject::paintBorder(QPainter *p, int _tx, int _ty, int w, int h, const RenderStyle* style, bool begin, bool end)
983 const QColor& tc = style->borderTopColor();
984 const QColor& bc = style->borderBottomColor();
985 const QColor& lc = style->borderLeftColor();
986 const QColor& rc = style->borderRightColor();
988 bool tt = style->borderTopIsTransparent();
989 bool bt = style->borderBottomIsTransparent();
990 bool rt = style->borderRightIsTransparent();
991 bool lt = style->borderLeftIsTransparent();
993 EBorderStyle ts = style->borderTopStyle();
994 EBorderStyle bs = style->borderBottomStyle();
995 EBorderStyle ls = style->borderLeftStyle();
996 EBorderStyle rs = style->borderRightStyle();
998 bool render_t = ts > BHIDDEN && !tt;
999 bool render_l = ls > BHIDDEN && begin && !lt;
1000 bool render_r = rs > BHIDDEN && end && !rt;
1001 bool render_b = bs > BHIDDEN && !bt;
1005 (tc == lc) && (tt == lt) &&
1007 (ls == DOTTED || ls == DASHED || ls == SOLID || ls == OUTSET);
1010 (tc == rc) && (tt == rt) &&
1012 (rs == DOTTED || rs == DASHED || rs == SOLID || rs == INSET);
1014 drawBorder(p, _tx, _ty, _tx + w, _ty + style->borderTopWidth(), BSTop, tc, style->color(), ts,
1015 ignore_left?0:style->borderLeftWidth(),
1016 ignore_right?0:style->borderRightWidth());
1021 (bc == lc) && (bt == lt) &&
1023 (ls == DOTTED || ls == DASHED || ls == SOLID || ls == OUTSET);
1026 (bc == rc) && (bt == rt) &&
1028 (rs == DOTTED || rs == DASHED || rs == SOLID || rs == INSET);
1030 drawBorder(p, _tx, _ty + h - style->borderBottomWidth(), _tx + w, _ty + h, BSBottom, bc, style->color(), bs,
1031 ignore_left?0:style->borderLeftWidth(),
1032 ignore_right?0:style->borderRightWidth());
1038 (tc == lc) && (tt == lt) &&
1040 (ts == DOTTED || ts == DASHED || ts == SOLID || ts == OUTSET);
1042 bool ignore_bottom =
1043 (bc == lc) && (bt == lt) &&
1045 (bs == DOTTED || bs == DASHED || bs == SOLID || bs == INSET);
1047 drawBorder(p, _tx, _ty, _tx + style->borderLeftWidth(), _ty + h, BSLeft, lc, style->color(), ls,
1048 ignore_top?0:style->borderTopWidth(),
1049 ignore_bottom?0:style->borderBottomWidth());
1055 (tc == rc) && (tt == rt) &&
1056 (rs >= DOTTED || rs == INSET) &&
1057 (ts == DOTTED || ts == DASHED || ts == SOLID || ts == OUTSET);
1059 bool ignore_bottom =
1060 (bc == rc) && (bt == rt) &&
1061 (rs >= DOTTED || rs == INSET) &&
1062 (bs == DOTTED || bs == DASHED || bs == SOLID || bs == INSET);
1064 drawBorder(p, _tx + w - style->borderRightWidth(), _ty, _tx + w, _ty + h, BSRight, rc, style->color(), rs,
1065 ignore_top?0:style->borderTopWidth(),
1066 ignore_bottom?0:style->borderBottomWidth());
1070 void RenderObject::absoluteRects(QValueList<QRect>& rects, int _tx, int _ty)
1072 // For blocks inside inlines, we go ahead and include margins so that we run right up to the
1073 // inline boxes above and below us (thus getting merged with them to form a single irregular
1075 if (continuation()) {
1076 rects.append(QRect(_tx, _ty - collapsedMarginTop(),
1077 width(), height()+collapsedMarginTop()+collapsedMarginBottom()));
1078 continuation()->absoluteRects(rects,
1079 _tx - xPos() + continuation()->containingBlock()->xPos(),
1080 _ty - yPos() + continuation()->containingBlock()->yPos());
1083 rects.append(QRect(_tx, _ty, width(), height()));
1086 QRect RenderObject::absoluteBoundingBoxRect()
1089 absolutePosition(x, y);
1090 QValueList<QRect> rects;
1091 absoluteRects(rects, x, y);
1093 QValueList<QRect>::ConstIterator it = rects.begin();
1095 while (++it != rects.end()) {
1096 result = result.unite(*it);
1101 void RenderObject::addAbsoluteRectForLayer(QRect& result)
1104 result = result.unite(absoluteBoundingBoxRect());
1106 for (RenderObject* current = firstChild(); current; current = current->nextSibling()) {
1107 current->addAbsoluteRectForLayer(result);
1111 QRect RenderObject::paintingRootRect(QRect& topLevelRect)
1113 QRect result = absoluteBoundingBoxRect();
1114 topLevelRect = result;
1115 for (RenderObject* current = firstChild(); current; current = current->nextSibling()) {
1116 current->addAbsoluteRectForLayer(result);
1122 void RenderObject::addFocusRingRects(QPainter *p, int _tx, int _ty)
1124 // For blocks inside inlines, we go ahead and include margins so that we run right up to the
1125 // inline boxes above and below us (thus getting merged with them to form a single irregular
1127 if (continuation()) {
1128 p->addFocusRingRect(_tx, _ty - collapsedMarginTop(), width(), height()+collapsedMarginTop()+collapsedMarginBottom());
1129 continuation()->addFocusRingRects(p,
1130 _tx - xPos() + continuation()->containingBlock()->xPos(),
1131 _ty - yPos() + continuation()->containingBlock()->yPos());
1134 p->addFocusRingRect(_tx, _ty, width(), height());
1138 void RenderObject::paintOutline(QPainter *p, int _tx, int _ty, int w, int h, const RenderStyle* style)
1140 int ow = style->outlineWidth();
1143 EBorderStyle os = style->outlineStyle();
1147 QColor oc = style->outlineColor();
1149 oc = style->color();
1151 int offset = style->outlineOffset();
1153 #ifdef APPLE_CHANGES
1154 if (style->outlineStyleIsAuto()) {
1155 p->initFocusRing(ow, offset, oc);
1156 addFocusRingRects(p, _tx, _ty);
1158 p->clearFocusRing();
1168 drawBorder(p, _tx-ow, _ty-ow, _tx, _ty+h+ow, BSLeft,
1169 QColor(oc), style->color(),
1172 drawBorder(p, _tx-ow, _ty-ow, _tx+w+ow, _ty, BSTop,
1173 QColor(oc), style->color(),
1176 drawBorder(p, _tx+w, _ty-ow, _tx+w+ow, _ty+h+ow, BSRight,
1177 QColor(oc), style->color(),
1180 drawBorder(p, _tx-ow, _ty+h, _tx+w+ow, _ty+h+ow, BSBottom,
1181 QColor(oc), style->color(),
1186 void RenderObject::paint(PaintInfo& i, int tx, int ty)
1190 void RenderObject::repaint(bool immediate)
1192 // Can't use canvas(), since we might be unrooted.
1193 RenderObject* o = this;
1194 while ( o->parent() ) o = o->parent();
1197 RenderCanvas* c = static_cast<RenderCanvas*>(o);
1198 if (c->printingMode())
1199 return; // Don't repaint if we're printing.
1200 c->repaintViewRectangle(getAbsoluteRepaintRect(), immediate);
1203 void RenderObject::repaintRectangle(const QRect& r, bool immediate)
1205 // Can't use canvas(), since we might be unrooted.
1206 RenderObject* o = this;
1207 while ( o->parent() ) o = o->parent();
1210 RenderCanvas* c = static_cast<RenderCanvas*>(o);
1211 if (c->printingMode())
1212 return; // Don't repaint if we're printing.
1214 computeAbsoluteRepaintRect(absRect);
1215 c->repaintViewRectangle(absRect, immediate);
1218 bool RenderObject::repaintAfterLayoutIfNeeded(const QRect& oldBounds, const QRect& oldFullBounds)
1220 QRect newBounds, newFullBounds;
1221 getAbsoluteRepaintRectIncludingFloats(newBounds, newFullBounds);
1222 if (newBounds != oldBounds || selfNeedsLayout()) {
1223 RenderCanvas* c = canvas();
1224 if (c->printingMode())
1225 return false; // Don't repaint if we're printing.
1226 c->repaintViewRectangle(oldFullBounds);
1227 if (newBounds != oldBounds)
1228 c->repaintViewRectangle(newFullBounds);
1234 void RenderObject::repaintDuringLayoutIfMoved(int x, int y)
1238 void RenderObject::repaintFloatingDescendants()
1242 bool RenderObject::checkForRepaintDuringLayout() const
1244 return !document()->view()->needsFullRepaint() && !layer();
1247 void RenderObject::repaintObjectsBeforeLayout()
1249 if (!needsLayout() || isText())
1252 bool blockWithInlineChildren = (isRenderBlock() && !isTable() && normalChildNeedsLayout() && childrenInline());
1253 if (selfNeedsLayout()) {
1255 if (blockWithInlineChildren)
1259 for (RenderObject* current = firstChild(); current; current = current->nextSibling()) {
1260 if (!current->isPositioned()) // RenderBlock subclass method handles walking the positioned objects.
1261 current->repaintObjectsBeforeLayout();
1265 QRect RenderObject::getAbsoluteRepaintRectWithOutline(int ow)
1267 QRect r(getAbsoluteRepaintRect());
1268 r.setRect(r.x()-ow, r.y()-ow, r.width()+ow*2, r.height()+ow*2);
1270 if (continuation() && !isInline())
1271 r.setRect(r.x(), r.y()-collapsedMarginTop(), r.width(), r.height()+collapsedMarginTop()+collapsedMarginBottom());
1273 if (isInlineFlow()) {
1274 for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling()) {
1275 if (!curr->isText()) {
1276 QRect childRect = curr->getAbsoluteRepaintRectWithOutline(ow);
1277 r = r.unite(childRect);
1285 QRect RenderObject::getAbsoluteRepaintRect()
1288 return parent()->getAbsoluteRepaintRect();
1292 void RenderObject::getAbsoluteRepaintRectIncludingFloats(QRect& bounds, QRect& fullBounds)
1294 bounds = fullBounds = getAbsoluteRepaintRect();
1297 void RenderObject::computeAbsoluteRepaintRect(QRect& r, bool f)
1300 return parent()->computeAbsoluteRepaintRect(r, f);
1303 void RenderObject::dirtyLinesFromChangedChild(RenderObject* child, bool adding)
1309 QString RenderObject::information() const
1312 QTextStream ts( &str, IO_WriteOnly );
1314 << "(" << (style() ? style()->refCount() : 0) << ")"
1315 << ": " << (void*)this << " ";
1316 if (isInline()) ts << "il ";
1317 if (childrenInline()) ts << "ci ";
1318 if (isFloating()) ts << "fl ";
1319 if (isAnonymous()) ts << "an ";
1320 if (isRelPositioned()) ts << "rp ";
1321 if (isPositioned()) ts << "ps ";
1322 if (needsLayout()) ts << "nl ";
1323 if (m_recalcMinMax) ts << "rmm ";
1324 if (mouseInside()) ts << "mi ";
1325 if (style() && style()->zIndex()) ts << "zI: " << style()->zIndex();
1326 if (element() && element()->active()) ts << "act ";
1327 if (element() && element()->hasAnchor()) ts << "anchor ";
1328 if (element() && element()->focused()) ts << "focus ";
1329 if (element()) ts << " <" << getTagName(element()->id()).string() << ">";
1330 ts << " (" << xPos() << "," << yPos() << "," << width() << "," << height() << ")"
1332 ( QString::fromLatin1(" [r=") +
1333 QString::number( static_cast<const RenderTableCell *>(this)->row() ) +
1334 QString::fromLatin1(" c=") +
1335 QString::number( static_cast<const RenderTableCell *>(this)->col() ) +
1336 QString::fromLatin1(" rs=") +
1337 QString::number( static_cast<const RenderTableCell *>(this)->rowSpan() ) +
1338 QString::fromLatin1(" cs=") +
1339 QString::number( static_cast<const RenderTableCell *>(this)->colSpan() ) +
1340 QString::fromLatin1("]") ) : QString::null );
1344 void RenderObject::printTree(int indent) const
1347 ind.fill(' ', indent);
1349 kdDebug() << ind << information() << endl;
1351 RenderObject *child = firstChild();
1354 child->printTree(indent+2);
1355 child = child->nextSibling();
1359 void RenderObject::dump(QTextStream *stream, QString ind) const
1361 if (isAnonymous()) { *stream << " anonymous"; }
1362 if (isFloating()) { *stream << " floating"; }
1363 if (isPositioned()) { *stream << " positioned"; }
1364 if (isRelPositioned()) { *stream << " relPositioned"; }
1365 if (isText()) { *stream << " text"; }
1366 if (isInline()) { *stream << " inline"; }
1367 if (isReplaced()) { *stream << " replaced"; }
1368 if (shouldPaintBackgroundOrBorder()) { *stream << " paintBackground"; }
1369 if (needsLayout()) { *stream << " needsLayout"; }
1370 if (minMaxKnown()) { *stream << " minMaxKnown"; }
1373 RenderObject *child = firstChild();
1376 *stream << ind << child->renderName() << ": ";
1377 child->dump(stream,ind+" ");
1378 child = child->nextSibling();
1383 bool RenderObject::shouldSelect() const
1385 const RenderObject* curr = this;
1386 DOM::NodeImpl *node = 0;
1387 bool forcedOn = false;
1390 if (curr->style()->userSelect() == SELECT_TEXT)
1392 if (!forcedOn && curr->style()->userSelect() == SELECT_NONE)
1396 node = curr->element();
1397 curr = curr->parent();
1400 // somewhere up the render tree there must be an element!
1403 return node->dispatchHTMLEvent(DOM::EventImpl::SELECTSTART_EVENT, true, true);
1406 QColor RenderObject::selectionColor(QPainter *p) const
1409 if (style()->userSelect() != SELECT_NONE) {
1410 RenderStyle* pseudoStyle = getPseudoStyle(RenderStyle::SELECTION);
1411 if (pseudoStyle && pseudoStyle->backgroundColor().isValid())
1412 color = pseudoStyle->backgroundColor();
1414 color = p->selectedTextBackgroundColor();
1420 DOM::NodeImpl* RenderObject::draggableNode(bool dhtmlOK, bool uaOK, int x, int y, bool& dhtmlWillDrag) const
1422 if (!dhtmlOK && !uaOK)
1425 const RenderObject* curr = this;
1427 DOM::NodeImpl *elt = curr->element();
1428 if (elt && elt->nodeType() == Node::TEXT_NODE) {
1429 // Since there's no way for the author to address the -khtml-user-drag style for a text node,
1430 // we use our own judgement.
1431 if (uaOK && canvas()->view()->part()->shouldDragAutoNode(curr->node(), x, y)) {
1432 dhtmlWillDrag = false;
1433 return curr->node();
1434 } else if (curr->shouldSelect()) {
1435 // In this case we have a click in the unselected portion of text. If this text is
1436 // selectable, we want to start the selection process instead of looking for a parent
1441 EUserDrag dragMode = curr->style()->userDrag();
1442 if (dhtmlOK && dragMode == DRAG_ELEMENT) {
1443 dhtmlWillDrag = true;
1444 return curr->node();
1445 } else if (uaOK && dragMode == DRAG_AUTO
1446 && canvas()->view()->part()->shouldDragAutoNode(curr->node(), x, y))
1448 dhtmlWillDrag = false;
1449 return curr->node();
1452 curr = curr->parent();
1457 void RenderObject::selectionStartEnd(int& spos, int& epos)
1459 canvas()->selectionStartEnd(spos, epos);
1462 RenderBlock* RenderObject::createAnonymousBlock()
1464 RenderStyle *newStyle = new (renderArena()) RenderStyle();
1465 newStyle->inheritFrom(m_style);
1466 newStyle->setDisplay(BLOCK);
1468 RenderBlock *newBox = new (renderArena()) RenderBlock(document() /* anonymous box */);
1469 newBox->setStyle(newStyle);
1473 void RenderObject::handleDynamicFloatPositionChange()
1475 // We have gone from not affecting the inline status of the parent flow to suddenly
1476 // having an impact. See if there is a mismatch between the parent flow's
1477 // childrenInline() state and our state.
1478 setInline(style()->isDisplayInlineType());
1479 if (isInline() != parent()->childrenInline()) {
1481 if (parent()->isRenderInline()) {
1482 // We have to split the parent flow.
1483 RenderInline* parentInline = static_cast<RenderInline*>(parent());
1484 RenderBlock* newBox = parentInline->createAnonymousBlock();
1486 RenderFlow* oldContinuation = parent()->continuation();
1487 parentInline->setContinuation(newBox);
1489 RenderObject* beforeChild = nextSibling();
1490 parent()->removeChildNode(this);
1491 parentInline->splitFlow(beforeChild, newBox, this, oldContinuation);
1493 else if (parent()->isRenderBlock())
1494 static_cast<RenderBlock*>(parent())->makeChildrenNonInline();
1497 // An anonymous block must be made to wrap this inline.
1498 RenderBlock* box = createAnonymousBlock();
1499 parent()->insertChildNode(box, this);
1500 box->appendChildNode(parent()->removeChildNode(this));
1505 void RenderObject::setStyle(RenderStyle *style)
1507 if (m_style == style)
1510 bool affectsParentBlock = false;
1511 RenderStyle::Diff d = RenderStyle::Equal;
1513 // If our z-index changes value or our visibility changes,
1514 // we need to dirty our stacking context's z-order list.
1517 if (m_style->visibility() != style->visibility() ||
1518 m_style->zIndex() != style->zIndex() ||
1519 m_style->hasAutoZIndex() != style->hasAutoZIndex())
1520 document()->setDashboardRegionsDirty(true);
1523 if ((m_style->hasAutoZIndex() != style->hasAutoZIndex() ||
1524 m_style->zIndex() != style->zIndex() ||
1525 m_style->visibility() != style->visibility()) && layer()) {
1526 layer()->stackingContext()->dirtyZOrderLists();
1527 if (m_style->hasAutoZIndex() != style->hasAutoZIndex() ||
1528 m_style->visibility() != style->visibility())
1529 layer()->dirtyZOrderLists();
1533 d = m_style->diff(style);
1535 // If we have no layer(), just treat a RepaintLayer hint as a normal Repaint.
1536 if (d == RenderStyle::RepaintLayer && !layer())
1537 d = RenderStyle::Repaint;
1539 // The background of the root element or the body element could propagate up to
1540 // the canvas. Just dirty the entire canvas when our style changes substantially.
1541 if (d >= RenderStyle::Repaint && element() &&
1542 (element()->id() == ID_HTML || element()->id() == ID_BODY))
1543 canvas()->repaint();
1544 else if (m_parent && !isText()) {
1545 // Do a repaint with the old style first, e.g., for example if we go from
1546 // having an outline to not having an outline.
1547 if (d == RenderStyle::RepaintLayer)
1548 layer()->repaintIncludingDescendants();
1549 else if (d == RenderStyle::Repaint)
1553 // When a layout hint happens, we go ahead and do a repaint of the layer, since the layer could
1554 // end up being destroyed.
1555 if (d == RenderStyle::Layout && layer() &&
1556 (m_style->position() != style->position() ||
1557 m_style->zIndex() != style->zIndex() ||
1558 m_style->hasAutoZIndex() != style->hasAutoZIndex() ||
1559 !(m_style->clip() == style->clip()) ||
1560 m_style->hasClip() != style->hasClip() ||
1561 m_style->opacity() != style->opacity()))
1562 layer()->repaintIncludingDescendants();
1564 // When a layout hint happens and an object's position style changes, we have to do a layout
1565 // to dirty the render tree using the old position value now.
1566 if (d == RenderStyle::Layout && m_parent && m_style->position() != style->position())
1567 setNeedsLayoutAndMinMaxRecalc();
1569 if (isFloating() && (m_style->floating() != style->floating()))
1570 // For changes in float styles, we need to conceivably remove ourselves
1571 // from the floating objects list.
1572 removeFromObjectLists();
1573 else if (isPositioned() && (style->position() != ABSOLUTE && style->position() != FIXED))
1574 // For changes in positioning styles, we need to conceivably remove ourselves
1575 // from the positioned objects list.
1576 removeFromObjectLists();
1578 affectsParentBlock = m_style && isFloatingOrPositioned() &&
1579 (!style->isFloating() && style->position() != ABSOLUTE && style->position() != FIXED)
1580 && parent() && (parent()->isBlockFlow() || parent()->isInlineFlow());
1582 // reset style flags
1584 m_positioned = false;
1585 m_relPositioned = false;
1586 m_paintBackground = false;
1587 m_hasOverflowClip = false;
1590 RenderStyle *oldStyle = m_style;
1593 updateBackgroundImages(oldStyle);
1599 oldStyle->deref(renderArena());
1601 setShouldPaintBackgroundOrBorder(m_style->hasBorder() || m_style->hasBackground());
1603 if (affectsParentBlock)
1604 handleDynamicFloatPositionChange();
1606 // No need to ever schedule repaints from a style change of a text run, since
1607 // we already did this for the parent of the text run.
1608 if (d == RenderStyle::Layout && m_parent)
1609 setNeedsLayoutAndMinMaxRecalc();
1610 else if (m_parent && !isText() && (d == RenderStyle::RepaintLayer || d == RenderStyle::Repaint))
1611 // Do a repaint with the new style now, e.g., for example if we go from
1612 // not having an outline to having an outline.
1616 void RenderObject::setStyleInternal(RenderStyle* st)
1621 m_style->deref(renderArena());
1627 void RenderObject::updateBackgroundImages(RenderStyle* oldStyle)
1629 // FIXME: This will be slow when a large number of images is used. Fix by using a dict.
1630 const BackgroundLayer* oldLayers = oldStyle ? oldStyle->backgroundLayers() : 0;
1631 const BackgroundLayer* newLayers = m_style ? m_style->backgroundLayers() : 0;
1632 for (const BackgroundLayer* currOld = oldLayers; currOld; currOld = currOld->next()) {
1633 if (currOld->backgroundImage() && (!newLayers || !newLayers->containsImage(currOld->backgroundImage())))
1634 currOld->backgroundImage()->deref(this);
1636 for (const BackgroundLayer* currNew = newLayers; currNew; currNew = currNew->next()) {
1637 if (currNew->backgroundImage() && (!oldLayers || !oldLayers->containsImage(currNew->backgroundImage())))
1638 currNew->backgroundImage()->ref(this);
1642 QRect RenderObject::viewRect() const
1644 return canvas()->viewRect();
1647 bool RenderObject::absolutePosition(int &xPos, int &yPos, bool f)
1649 RenderObject* o = parent();
1651 o->absolutePosition(xPos, yPos, f);
1652 if (o->hasOverflowClip())
1653 o->layer()->subtractScrollOffset(xPos, yPos);
1663 QRect RenderObject::caretRect(int, EAffinity, int *extraWidthToEndOfLine)
1665 if (extraWidthToEndOfLine)
1666 *extraWidthToEndOfLine = 0;
1671 int RenderObject::paddingTop() const
1674 Length padding = m_style->paddingTop();
1675 if (padding.isPercent())
1676 w = containingBlock()->contentWidth();
1677 w = padding.minWidth(w);
1678 if ( isTableCell() && padding.isVariable() )
1679 w = static_cast<const RenderTableCell *>(this)->table()->cellPadding();
1683 int RenderObject::paddingBottom() const
1686 Length padding = style()->paddingBottom();
1687 if (padding.isPercent())
1688 w = containingBlock()->contentWidth();
1689 w = padding.minWidth(w);
1690 if ( isTableCell() && padding.isVariable() )
1691 w = static_cast<const RenderTableCell *>(this)->table()->cellPadding();
1695 int RenderObject::paddingLeft() const
1698 Length padding = style()->paddingLeft();
1699 if (padding.isPercent())
1700 w = containingBlock()->contentWidth();
1701 w = padding.minWidth(w);
1702 if ( isTableCell() && padding.isVariable() )
1703 w = static_cast<const RenderTableCell *>(this)->table()->cellPadding();
1707 int RenderObject::paddingRight() const
1710 Length padding = style()->paddingRight();
1711 if (padding.isPercent())
1712 w = containingBlock()->contentWidth();
1713 w = padding.minWidth(w);
1714 if ( isTableCell() && padding.isVariable() )
1715 w = static_cast<const RenderTableCell *>(this)->table()->cellPadding();
1719 RenderCanvas* RenderObject::canvas() const
1721 return static_cast<RenderCanvas*>(document()->renderer());
1724 RenderObject *RenderObject::container() const
1726 // This method is extremely similar to containingBlock(), but with a few notable
1728 // (1) It can be used on orphaned subtrees, i.e., it can be called safely even when
1729 // the object is not part of the primary document subtree yet.
1730 // (2) For normal flow elements, it just returns the parent.
1731 // (3) For absolute positioned elements, it will return a relative positioned inline.
1732 // containingBlock() simply skips relpositioned inlines and lets an enclosing block handle
1733 // the layout of the positioned object. This does mean that calcAbsoluteHorizontal and
1734 // calcAbsoluteVertical have to use container().
1735 EPosition pos = m_style->position();
1736 RenderObject *o = 0;
1737 if (!isText() && pos == FIXED) {
1738 // container() can be called on an object that is not in the
1739 // tree yet. We don't call canvas() since it will assert if it
1740 // can't get back to the canvas. Instead we just walk as high up
1741 // as we can. If we're in the tree, we'll get the root. If we
1742 // aren't we'll get the root of our little subtree (most likely
1743 // we'll just return 0).
1745 while (o && o->parent()) o = o->parent();
1747 else if (!isText() && pos == ABSOLUTE) {
1748 // Same goes here. We technically just want our containing block, but
1749 // we may not have one if we're part of an uninstalled subtree. We'll
1750 // climb as high as we can though.
1752 while (o && o->style()->position() == STATIC && !o->isRoot() && !o->isCanvas())
1760 bool RenderObject::isSelectionBorder() const
1762 SelectionState st = selectionState();
1763 return st == SelectionStart || st == SelectionEnd || st == SelectionBoth;
1767 static void checkFloats(RenderObject* o, RenderObject* f)
1769 if (o->isRenderBlock()) {
1770 RenderBlock* b = static_cast<RenderBlock*>(o);
1771 if (b->containsFloat(f))
1775 for (RenderObject* c = o->firstChild(); c; c = c->nextSibling())
1780 void RenderObject::removeFromObjectLists()
1783 RenderBlock* outermostBlock = containingBlock();
1784 for (RenderBlock* p = outermostBlock; p && !p->isCanvas(); p = p->containingBlock()) {
1785 if (p->containsFloat(this))
1790 outermostBlock->markAllDescendantsWithFloatsForLayout(this);
1792 // Debugging code for float checking.
1793 checkFloats(canvas(), this);
1797 if (isPositioned()) {
1799 for (p = parent(); p; p = p->parent()) {
1800 if (p->isRenderBlock())
1801 static_cast<RenderBlock*>(p)->removePositionedObject(this);
1806 RenderArena* RenderObject::renderArena() const
1808 DOM::DocumentImpl* doc = document();
1809 return doc ? doc->renderArena() : 0;
1812 void RenderObject::remove()
1815 // Delete our accessibility object if we have one.
1816 KWQAccObjectCache* cache = document()->getExistingAccObjectCache();
1818 cache->detach(this);
1821 removeFromObjectLists();
1824 //have parent, take care of the tree integrity
1825 parent()->removeChild(this);
1828 void RenderObject::detach()
1832 // by default no refcounting
1833 arenaDelete(document()->renderArena(), this);
1836 void RenderObject::arenaDelete(RenderArena *arena, void *base)
1838 if (m_style->backgroundImage())
1839 m_style->backgroundImage()->deref(this);
1841 m_style->deref(arena);
1844 void *savedBase = baseOfRenderObjectBeingDeleted;
1845 baseOfRenderObjectBeingDeleted = base;
1849 baseOfRenderObjectBeingDeleted = savedBase;
1852 // Recover the size left there for us by operator delete and free the memory.
1853 arena->free(*(size_t *)base, base);
1856 Position RenderObject::positionForCoordinates(int x, int y, EAffinity *affinity)
1859 *affinity = UPSTREAM;
1861 return Position(element(), caretMinOffset());
1864 bool RenderObject::mouseInside() const
1866 if (!m_mouseInside && continuation())
1867 return continuation()->mouseInside();
1868 return m_mouseInside;
1871 bool RenderObject::isDragging() const
1873 return m_isDragging;
1876 void RenderObject::updateDragState(bool dragOn)
1878 bool valueChanged = (dragOn != m_isDragging);
1879 m_isDragging = dragOn;
1880 if (valueChanged && style()->affectedByDragRules())
1881 element()->setChanged();
1882 for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
1883 curr->updateDragState(dragOn);
1885 continuation()->updateDragState(dragOn);
1888 bool RenderObject::hitTest(NodeInfo& info, int x, int y, int tx, int ty, HitTestFilter hitTestFilter)
1890 bool inside = false;
1891 if (hitTestFilter != HitTestSelf) {
1892 // First test the foreground layer (lines and inlines).
1893 inside = nodeAtPoint(info, x, y, tx, ty, HitTestForeground);
1895 // Test floats next.
1897 inside = nodeAtPoint(info, x, y, tx, ty, HitTestFloat);
1899 // Finally test to see if the mouse is in the background (within a child block's background).
1901 inside = nodeAtPoint(info, x, y, tx, ty, HitTestChildBlockBackgrounds);
1904 // See if the mouse is inside us but not any of our descendants
1905 if (hitTestFilter != HitTestDescendants && !inside)
1906 inside = nodeAtPoint(info, x, y, tx, ty, HitTestBlockBackground);
1911 void RenderObject::setInnerNode(NodeInfo& info)
1913 if (!info.innerNode() && !isInline() && continuation()) {
1914 // We are in the margins of block elements that are part of a continuation. In
1915 // this case we're actually still inside the enclosing inline element that was
1916 // split. Go ahead and set our inner node accordingly.
1917 info.setInnerNode(continuation()->element());
1918 if (!info.innerNonSharedNode())
1919 info.setInnerNonSharedNode(continuation()->element());
1922 if (!info.innerNode() && element())
1923 info.setInnerNode(element());
1925 if(!info.innerNonSharedNode() && element())
1926 info.setInnerNonSharedNode(element());
1929 bool RenderObject::nodeAtPoint(NodeInfo& info, int _x, int _y, int _tx, int _ty,
1930 HitTestAction hitTestAction)
1935 short RenderObject::verticalPositionHint( bool firstLine ) const
1937 short vpos = m_verticalPosition;
1938 if ( m_verticalPosition == PositionUndefined || firstLine ) {
1939 vpos = getVerticalPosition( firstLine );
1941 m_verticalPosition = vpos;
1947 short RenderObject::getVerticalPosition( bool firstLine ) const
1952 // This method determines the vertical position for inline elements.
1954 EVerticalAlign va = style()->verticalAlign();
1957 } else if ( va == BOTTOM ) {
1958 vpos = PositionBottom;
1959 } else if ( va == LENGTH ) {
1960 vpos = -style()->verticalAlignLength().width( lineHeight( firstLine ) );
1962 bool checkParent = parent()->isInline() && !parent()->isInlineBlockOrInlineTable();
1963 vpos = checkParent ? parent()->verticalPositionHint( firstLine ) : 0;
1964 // don't allow elements nested inside text-top to have a different valignment.
1965 if ( va == BASELINE )
1968 // if ( vpos == PositionTop )
1971 const QFont &f = parent()->font( firstLine );
1972 int fontsize = f.pixelSize();
1975 vpos += fontsize/5 + 1;
1976 else if (va == SUPER)
1977 vpos -= fontsize/3 + 1;
1978 else if (va == TEXT_TOP)
1979 vpos += baselinePosition( firstLine ) - QFontMetrics(f).ascent();
1980 else if (va == MIDDLE)
1981 vpos += - (int)(QFontMetrics(f).xHeight()/2) - lineHeight( firstLine )/2 + baselinePosition( firstLine );
1982 else if (va == TEXT_BOTTOM) {
1983 vpos += QFontMetrics(f).descent();
1985 vpos -= fontMetrics(firstLine).descent();
1986 } else if ( va == BASELINE_MIDDLE )
1987 vpos += - lineHeight( firstLine )/2 + baselinePosition( firstLine );
1993 short RenderObject::lineHeight( bool firstLine, bool ) const
1995 RenderStyle* s = style(firstLine);
1997 Length lh = s->lineHeight();
1999 // its "unset", choose nice default
2001 return s->fontMetrics().lineSpacing();
2004 return lh.minWidth(s->font().pixelSize());
2010 short RenderObject::baselinePosition( bool firstLine, bool isRootLineBox ) const
2012 const QFontMetrics &fm = fontMetrics( firstLine );
2013 return fm.ascent() + ( lineHeight( firstLine, isRootLineBox ) - fm.height() ) / 2;
2016 void RenderObject::invalidateVerticalPositions()
2018 m_verticalPosition = PositionUndefined;
2019 RenderObject *child = firstChild();
2021 child->invalidateVerticalPositions();
2022 child = child->nextSibling();
2026 void RenderObject::recalcMinMaxWidths()
2028 KHTMLAssert( m_recalcMinMax );
2031 kdDebug( 6040 ) << renderName() << " recalcMinMaxWidths() this=" << this <<endl;
2035 updateFirstLetter();
2037 RenderObject *child = firstChild();
2039 // gcc sucks. if anybody knows a trick to get rid of the
2040 // warning without adding an extra (unneeded) initialisation,
2045 if ( ( m_minMaxKnown && child->m_recalcMinMax ) || !child->m_minMaxKnown ) {
2046 cmin = child->minWidth();
2047 cmax = child->maxWidth();
2050 if ( child->m_recalcMinMax )
2051 child->recalcMinMaxWidths();
2052 if ( !child->m_minMaxKnown )
2053 child->calcMinMaxWidth();
2054 if ( m_minMaxKnown && test && (cmin != child->minWidth() || cmax != child->maxWidth()) )
2055 m_minMaxKnown = false;
2056 child = child->nextSibling();
2059 // we need to recalculate, if the contains inline children, as the change could have
2060 // happened somewhere deep inside the child tree. Also do this for blocks or tables that
2061 // are inline (i.e., inline-block and inline-table).
2062 if ((!isInline() || isInlineBlockOrInlineTable()) && childrenInline())
2063 m_minMaxKnown = false;
2065 if ( !m_minMaxKnown )
2067 m_recalcMinMax = false;
2070 void RenderObject::scheduleRelayout()
2072 if (!isCanvas()) return;
2073 KHTMLView *view = static_cast<RenderCanvas *>(this)->view();
2075 view->scheduleRelayout();
2079 void RenderObject::removeLeftoverAnonymousBoxes()
2083 InlineBox* RenderObject::createInlineBox(bool, bool isRootLineBox, bool)
2085 KHTMLAssert(!isRootLineBox);
2086 return new (renderArena()) InlineBox(this);
2089 void RenderObject::dirtyLineBoxes(bool, bool)
2093 InlineBox* RenderObject::inlineBoxWrapper() const
2098 void RenderObject::setInlineBoxWrapper(InlineBox* b)
2102 void RenderObject::deleteLineBoxWrapper()
2106 RenderStyle* RenderObject::style(bool firstLine) const {
2107 RenderStyle *s = m_style;
2109 const RenderObject* obj = isText() ? parent() : this;
2110 if (obj->isBlockFlow()) {
2111 RenderBlock* firstLineBlock = obj->firstLineBlock();
2113 s = firstLineBlock->getPseudoStyle(RenderStyle::FIRST_LINE, style());
2115 else if (!obj->isAnonymous() && obj->isInlineFlow()) {
2116 RenderStyle* parentStyle = obj->parent()->style(true);
2117 if (parentStyle != obj->parent()->style()) {
2118 // A first-line style is in effect. We need to cache a first-line style
2120 style()->setHasPseudoStyle(RenderStyle::FIRST_LINE_INHERITED);
2121 s = obj->getPseudoStyle(RenderStyle::FIRST_LINE_INHERITED, parentStyle);
2128 RenderStyle* RenderObject::getPseudoStyle(RenderStyle::PseudoId pseudo, RenderStyle* parentStyle) const
2130 if (!style()->hasPseudoStyle(pseudo))
2134 parentStyle = style();
2136 RenderStyle* result = style()->getPseudoStyle(pseudo);
2137 if (result) return result;
2139 DOM::NodeImpl* node = element();
2141 node = element()->parentNode();
2142 if (!node) return 0;
2144 if (pseudo == RenderStyle::FIRST_LINE_INHERITED)
2145 result = document()->styleSelector()->styleForElement(static_cast<DOM::ElementImpl*>(node),
2146 parentStyle, false);
2148 result = document()->styleSelector()->pseudoStyleForElement(pseudo, static_cast<DOM::ElementImpl*>(node),
2151 style()->addPseudoStyle(result);
2155 void RenderObject::getTextDecorationColors(int decorations, QColor& underline, QColor& overline,
2156 QColor& linethrough, bool quirksMode)
2158 RenderObject* curr = this;
2160 int currDecs = curr->style()->textDecoration();
2162 if (currDecs & UNDERLINE) {
2163 decorations &= ~UNDERLINE;
2164 underline = curr->style()->color();
2166 if (currDecs & OVERLINE) {
2167 decorations &= ~OVERLINE;
2168 overline = curr->style()->color();
2170 if (currDecs & LINE_THROUGH) {
2171 decorations &= ~LINE_THROUGH;
2172 linethrough = curr->style()->color();
2175 curr = curr->parent();
2176 if (curr && curr->isRenderBlock() && curr->continuation())
2177 curr = curr->continuation();
2178 } while (curr && decorations && (!quirksMode || !curr->element() ||
2179 (curr->element()->id() != ID_A && curr->element()->id() != ID_FONT)));
2181 // If we bailed out, use the element we bailed out at (typically a <font> or <a> element).
2182 if (decorations && curr) {
2183 if (decorations & UNDERLINE)
2184 underline = curr->style()->color();
2185 if (decorations & OVERLINE)
2186 overline = curr->style()->color();
2187 if (decorations & LINE_THROUGH)
2188 linethrough = curr->style()->color();
2193 void RenderObject::updateWidgetPositions()
2195 for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
2196 curr->updateWidgetPositions();
2199 QValueList<DashboardRegionValue> RenderObject::computeDashboardRegions()
2201 QValueList<DashboardRegionValue> regions;
2202 collectDashboardRegions(regions);
2206 void RenderObject::addDashboardRegions (QValueList<DashboardRegionValue>& regions)
2208 // Convert the style regions to absolute coordinates.
2209 if (style()->visibility() != VISIBLE)
2212 QValueList<StyleDashboardRegion> styleRegions = style()->dashboardRegions();
2213 if (styleRegions.count() > 0) {
2214 uint i, count = styleRegions.count();
2215 for (i = 0; i < count; i++){
2216 StyleDashboardRegion styleRegion = styleRegions[i];
2221 DashboardRegionValue region;
2222 region.label = styleRegion.label;
2223 region.bounds = QRect (
2224 styleRegion.offset.left.value,
2225 styleRegion.offset.top.value,
2226 w - styleRegion.offset.left.value - styleRegion.offset.right.value,
2227 h - styleRegion.offset.top.value - styleRegion.offset.bottom.value);
2228 region.type = styleRegion.type;
2230 region.clip = region.bounds;
2231 computeAbsoluteRepaintRect(region.clip);
2232 if (region.clip.height() < 0) {
2233 region.clip.setHeight(0);
2234 region.clip.setWidth(0);
2238 absolutePosition (x, y);
2239 region.bounds.setX (x + styleRegion.offset.left.value);
2240 region.bounds.setY (y + styleRegion.offset.top.value);
2242 regions.append (region);
2247 void RenderObject::collectDashboardRegions (QValueList<DashboardRegionValue>& regions)
2249 // RenderTexts don't have their own style, they just use their parent's style,
2250 // so we don't want to include them.
2254 addDashboardRegions (regions);
2255 for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling()) {
2256 curr->collectDashboardRegions(regions);
2262 void RenderObject::collectBorders(QValueList<CollapsedBorderValue>& borderStyles)
2264 for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
2265 curr->collectBorders(borderStyles);
2268 bool RenderObject::avoidsFloats() const
2270 return isReplaced() || isTable() || hasOverflowClip() || isHR() || isFlexibleBox();
2273 bool RenderObject::usesLineWidth() const
2275 // 1. All auto-width objects that avoid floats should always use lineWidth
2276 // 2. For objects with a specified width, we match WinIE's behavior:
2277 // (a) tables use contentWidth
2278 // (b) <hr>s use lineWidth
2279 // (c) all other objects use lineWidth in quirks mode and contentWidth in strict mode.
2280 return (avoidsFloats() && (style()->width().isVariable() || isHR() || (style()->htmlHacks() && !isTable())));
2283 QChar RenderObject::backslashAsCurrencySymbol() const
2288 NodeImpl *node = element();
2291 DocumentImpl *document = node->getDocument();
2294 Decoder *decoder = document->decoder();
2297 const QTextCodec *codec = decoder->codec();
2300 return codec->backslashAsCurrencySymbol();
2304 void RenderObject::setPixmap(const QPixmap&, const QRect&, CachedImage *image)
2306 // Repaint when the background image finishes loading.
2307 // This is needed for RenderBox objects, and also for table objects that hold
2308 // backgrounds that are then respected by the table cells (which are RenderBox
2309 // subclasses). It would be even better to find a more elegant way of doing this that
2310 // would avoid putting this function and the CachedObjectClient base class into RenderObject.
2312 if (image && image->pixmap_size() == image->valid_rect().size() && parent()) {
2313 if (canvas() && element() && (element()->id() == ID_HTML || element()->id() == ID_BODY))
2314 canvas()->repaint(); // repaint the entire canvas since the background gets propagated up
2316 repaint(); // repaint object, which is a box or a container with boxes inside it
2320 int RenderObject::maximalOutlineSize(PaintAction p) const
2322 if (p != PaintActionOutline)
2324 return static_cast<RenderCanvas*>(document()->renderer())->maximalOutlineSize();
2327 long RenderObject::caretMinOffset() const
2332 long RenderObject::caretMaxOffset() const
2337 unsigned long RenderObject::caretMaxRenderedOffset() const
2342 InlineBox *RenderObject::inlineBox(long offset, EAffinity affinity)
2344 return inlineBoxWrapper();