RenderObject.h revision 2fc2651226baac27029e38c9d6ef883fa32084db
1/*
2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3 *           (C) 2000 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2000 Dirk Mueller (mueller@kde.org)
5 *           (C) 2004 Allan Sandfeld Jensen (kde@carewolf.com)
6 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2009 Google Inc. All rights reserved.
8 *
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.
13 *
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.
18 *
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., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 *
24 */
25
26#ifndef RenderObject_h
27#define RenderObject_h
28
29#include "CachedResourceClient.h"
30#include "Document.h"
31#include "Element.h"
32#include "FloatQuad.h"
33#include "PaintPhase.h"
34#include "RenderObjectChildList.h"
35#include "RenderStyle.h"
36#include "TextAffinity.h"
37#include "TransformationMatrix.h"
38#include <wtf/UnusedParam.h>
39
40#if PLATFORM(CG) || PLATFORM(CAIRO) || PLATFORM(QT)
41#define HAVE_PATH_BASED_BORDER_RADIUS_DRAWING 1
42#endif
43
44namespace WebCore {
45
46class AffineTransform;
47class AnimationController;
48class HitTestResult;
49class InlineBox;
50class InlineFlowBox;
51class OverlapTestRequestClient;
52class Path;
53class Position;
54class RenderBoxModelObject;
55class RenderInline;
56class RenderBlock;
57class RenderFlow;
58class RenderLayer;
59class RenderTheme;
60class TransformState;
61class VisiblePosition;
62#if ENABLE(SVG)
63class RenderSVGResourceContainer;
64#endif
65
66struct PaintInfo;
67
68enum HitTestFilter {
69    HitTestAll,
70    HitTestSelf,
71    HitTestDescendants
72};
73
74enum HitTestAction {
75    HitTestBlockBackground,
76    HitTestChildBlockBackground,
77    HitTestChildBlockBackgrounds,
78    HitTestFloat,
79    HitTestForeground
80};
81
82// Sides used when drawing borders and outlines.  This is in RenderObject rather than RenderBoxModelObject since outlines can
83// be drawn by SVG around bounding boxes.
84enum BoxSide {
85    BSTop,
86    BSBottom,
87    BSLeft,
88    BSRight
89};
90
91const int caretWidth = 1;
92
93#if ENABLE(DASHBOARD_SUPPORT)
94struct DashboardRegionValue {
95    bool operator==(const DashboardRegionValue& o) const
96    {
97        return type == o.type && bounds == o.bounds && clip == o.clip && label == o.label;
98    }
99    bool operator!=(const DashboardRegionValue& o) const
100    {
101        return !(*this == o);
102    }
103
104    String label;
105    IntRect bounds;
106    IntRect clip;
107    int type;
108};
109#endif
110
111// Base class for all rendering tree objects.
112class RenderObject : public CachedResourceClient {
113    friend class RenderBlock;
114    friend class RenderBox;
115    friend class RenderLayer;
116    friend class RenderObjectChildList;
117    friend class RenderSVGContainer;
118public:
119    // Anonymous objects should pass the document as their node, and they will then automatically be
120    // marked as anonymous in the constructor.
121    RenderObject(Node*);
122    virtual ~RenderObject();
123
124    RenderTheme* theme() const;
125
126    virtual const char* renderName() const = 0;
127
128    RenderObject* parent() const { return m_parent; }
129    bool isDescendantOf(const RenderObject*) const;
130
131    RenderObject* previousSibling() const { return m_previous; }
132    RenderObject* nextSibling() const { return m_next; }
133
134    RenderObject* firstChild() const
135    {
136        if (const RenderObjectChildList* children = virtualChildren())
137            return children->firstChild();
138        return 0;
139    }
140    RenderObject* lastChild() const
141    {
142        if (const RenderObjectChildList* children = virtualChildren())
143            return children->lastChild();
144        return 0;
145    }
146    RenderObject* beforePseudoElementRenderer() const
147    {
148        if (const RenderObjectChildList* children = virtualChildren())
149            return children->beforePseudoElementRenderer(this);
150        return 0;
151    }
152    RenderObject* afterPseudoElementRenderer() const
153    {
154        if (const RenderObjectChildList* children = virtualChildren())
155            return children->afterPseudoElementRenderer(this);
156        return 0;
157    }
158    virtual RenderObjectChildList* virtualChildren() { return 0; }
159    virtual const RenderObjectChildList* virtualChildren() const { return 0; }
160
161    RenderObject* nextInPreOrder() const;
162    RenderObject* nextInPreOrder(RenderObject* stayWithin) const;
163    RenderObject* nextInPreOrderAfterChildren() const;
164    RenderObject* nextInPreOrderAfterChildren(RenderObject* stayWithin) const;
165    RenderObject* previousInPreOrder() const;
166    RenderObject* childAt(unsigned) const;
167
168    RenderObject* firstLeafChild() const;
169    RenderObject* lastLeafChild() const;
170
171    // The following six functions are used when the render tree hierarchy changes to make sure layers get
172    // properly added and removed.  Since containership can be implemented by any subclass, and since a hierarchy
173    // can contain a mixture of boxes and other object types, these functions need to be in the base class.
174    RenderLayer* enclosingLayer() const;
175    void addLayers(RenderLayer* parentLayer, RenderObject* newObject);
176    void removeLayers(RenderLayer* parentLayer);
177    void moveLayers(RenderLayer* oldParent, RenderLayer* newParent);
178    RenderLayer* findNextLayer(RenderLayer* parentLayer, RenderObject* startPoint, bool checkParent = true);
179
180    // Convenience function for getting to the nearest enclosing box of a RenderObject.
181    RenderBox* enclosingBox() const;
182    RenderBoxModelObject* enclosingBoxModelObject() const;
183
184    virtual bool isEmpty() const { return firstChild() == 0; }
185
186#ifndef NDEBUG
187    void setHasAXObject(bool flag) { m_hasAXObject = flag; }
188    bool hasAXObject() const { return m_hasAXObject; }
189    bool isSetNeedsLayoutForbidden() const { return m_setNeedsLayoutForbidden; }
190    void setNeedsLayoutIsForbidden(bool flag) { m_setNeedsLayoutForbidden = flag; }
191#endif
192
193    // Obtains the nearest enclosing block (including this block) that contributes a first-line style to our inline
194    // children.
195    virtual RenderBlock* firstLineBlock() const;
196
197    // Called when an object that was floating or positioned becomes a normal flow object
198    // again.  We have to make sure the render tree updates as needed to accommodate the new
199    // normal flow object.
200    void handleDynamicFloatPositionChange();
201
202    // RenderObject tree manipulation
203    //////////////////////////////////////////
204    virtual bool canHaveChildren() const { return virtualChildren(); }
205    virtual bool isChildAllowed(RenderObject*, RenderStyle*) const { return true; }
206    virtual void addChild(RenderObject* newChild, RenderObject* beforeChild = 0);
207    virtual void addChildIgnoringContinuation(RenderObject* newChild, RenderObject* beforeChild = 0) { return addChild(newChild, beforeChild); }
208    virtual void removeChild(RenderObject*);
209    virtual bool createsAnonymousWrapper() const { return false; }
210    //////////////////////////////////////////
211
212protected:
213    //////////////////////////////////////////
214    // Helper functions. Dangerous to use!
215    void setPreviousSibling(RenderObject* previous) { m_previous = previous; }
216    void setNextSibling(RenderObject* next) { m_next = next; }
217    void setParent(RenderObject* parent) { m_parent = parent; }
218    //////////////////////////////////////////
219private:
220    void addAbsoluteRectForLayer(IntRect& result);
221    void setLayerNeedsFullRepaint();
222
223public:
224#ifndef NDEBUG
225    void showTreeForThis() const;
226
227    void showRenderObject() const;
228    // We don't make printedCharacters an optional parameter so that
229    // showRenderObject can be called from gdb easily.
230    void showRenderObject(int printedCharacters) const;
231    void showRenderTreeAndMark(const RenderObject* markedObject1 = 0, const char* markedLabel1 = 0, const RenderObject* markedObject2 = 0, const char* markedLabel2 = 0, int depth = 0) const;
232#endif
233
234    static RenderObject* createObject(Node*, RenderStyle*);
235
236    // Overloaded new operator.  Derived classes must override operator new
237    // in order to allocate out of the RenderArena.
238    void* operator new(size_t, RenderArena*) throw();
239
240    // Overridden to prevent the normal delete from being called.
241    void operator delete(void*, size_t);
242
243private:
244    // The normal operator new is disallowed on all render objects.
245    void* operator new(size_t) throw();
246
247public:
248    RenderArena* renderArena() const { return document()->renderArena(); }
249
250    virtual bool isApplet() const { return false; }
251    virtual bool isBR() const { return false; }
252    virtual bool isBlockFlow() const { return false; }
253    virtual bool isBoxModelObject() const { return false; }
254    virtual bool isCounter() const { return false; }
255    virtual bool isDetails() const { return false; }
256    virtual bool isDetailsMarker() const { return false; }
257    virtual bool isEmbeddedObject() const { return false; }
258    virtual bool isFieldset() const { return false; }
259    virtual bool isFileUploadControl() const { return false; }
260    virtual bool isFrame() const { return false; }
261    virtual bool isFrameSet() const { return false; }
262    virtual bool isImage() const { return false; }
263    virtual bool isInlineBlockOrInlineTable() const { return false; }
264    virtual bool isListBox() const { return false; }
265    virtual bool isListItem() const { return false; }
266    virtual bool isListMarker() const { return false; }
267    virtual bool isMedia() const { return false; }
268    virtual bool isMenuList() const { return false; }
269#if ENABLE(METER_TAG)
270    virtual bool isMeter() const { return false; }
271#endif
272#if ENABLE(PROGRESS_TAG)
273    virtual bool isProgress() const { return false; }
274#endif
275    virtual bool isRenderBlock() const { return false; }
276    virtual bool isRenderButton() const { return false; }
277    virtual bool isRenderIFrame() const { return false; }
278    virtual bool isRenderImage() const { return false; }
279    virtual bool isRenderInline() const { return false; }
280    virtual bool isRenderPart() const { return false; }
281    virtual bool isRenderView() const { return false; }
282    virtual bool isReplica() const { return false; }
283
284    virtual bool isRuby() const { return false; }
285    virtual bool isRubyBase() const { return false; }
286    virtual bool isRubyRun() const { return false; }
287    virtual bool isRubyText() const { return false; }
288
289    virtual bool isSlider() const { return false; }
290    virtual bool isSummary() const { return false; }
291    virtual bool isTable() const { return false; }
292    virtual bool isTableCell() const { return false; }
293    virtual bool isTableCol() const { return false; }
294    virtual bool isTableRow() const { return false; }
295    virtual bool isTableSection() const { return false; }
296    virtual bool isTextControl() const { return false; }
297    virtual bool isTextArea() const { return false; }
298    virtual bool isTextField() const { return false; }
299    virtual bool isVideo() const { return false; }
300    virtual bool isWidget() const { return false; }
301    virtual bool isCanvas() const { return false; }
302#if ENABLE(FULLSCREEN_API)
303    virtual bool isRenderFullScreen() const { return false; }
304#endif
305
306    bool isRoot() const { return document()->documentElement() == m_node; }
307    bool isBody() const;
308    bool isHR() const;
309    bool isLegend() const;
310
311    bool isHTMLMarquee() const;
312
313    inline bool isBeforeContent() const;
314    inline bool isAfterContent() const;
315    static inline bool isBeforeContent(const RenderObject* obj) { return obj && obj->isBeforeContent(); }
316    static inline bool isAfterContent(const RenderObject* obj) { return obj && obj->isAfterContent(); }
317
318    bool childrenInline() const { return m_childrenInline; }
319    void setChildrenInline(bool b = true) { m_childrenInline = b; }
320    bool hasColumns() const { return m_hasColumns; }
321    void setHasColumns(bool b = true) { m_hasColumns = b; }
322    bool cellWidthChanged() const { return m_cellWidthChanged; }
323    void setCellWidthChanged(bool b = true) { m_cellWidthChanged = b; }
324
325    virtual bool requiresForcedStyleRecalcPropagation() const { return false; }
326
327#if ENABLE(MATHML)
328    virtual bool isRenderMathMLBlock() const { return false; }
329#endif // ENABLE(MATHML)
330
331#if ENABLE(SVG)
332    // FIXME: Until all SVG renders can be subclasses of RenderSVGModelObject we have
333    // to add SVG renderer methods to RenderObject with an ASSERT_NOT_REACHED() default implementation.
334    virtual bool isSVGRoot() const { return false; }
335    virtual bool isSVGContainer() const { return false; }
336    virtual bool isSVGViewportContainer() const { return false; }
337    virtual bool isSVGGradientStop() const { return false; }
338    virtual bool isSVGHiddenContainer() const { return false; }
339    virtual bool isSVGPath() const { return false; }
340    virtual bool isSVGText() const { return false; }
341    virtual bool isSVGTextPath() const { return false; }
342    virtual bool isSVGInline() const { return false; }
343    virtual bool isSVGInlineText() const { return false; }
344    virtual bool isSVGImage() const { return false; }
345    virtual bool isSVGForeignObject() const { return false; }
346    virtual bool isSVGResourceContainer() const { return false; }
347    virtual bool isSVGResourceFilter() const { return false; }
348    virtual bool isSVGResourceFilterPrimitive() const { return false; }
349    virtual bool isSVGShadowTreeRootContainer() const { return false; }
350
351    virtual RenderSVGResourceContainer* toRenderSVGResourceContainer();
352
353    // FIXME: Those belong into a SVG specific base-class for all renderers (see above)
354    // Unfortunately we don't have such a class yet, because it's not possible for all renderers
355    // to inherit from RenderSVGObject -> RenderObject (some need RenderBlock inheritance for instance)
356    virtual void setNeedsTransformUpdate() { }
357    virtual void setNeedsBoundariesUpdate();
358
359    // Per SVG 1.1 objectBoundingBox ignores clipping, masking, filter effects, opacity and stroke-width.
360    // This is used for all computation of objectBoundingBox relative units and by SVGLocateable::getBBox().
361    // NOTE: Markers are not specifically ignored here by SVG 1.1 spec, but we ignore them
362    // since stroke-width is ignored (and marker size can depend on stroke-width).
363    // objectBoundingBox is returned local coordinates.
364    // The name objectBoundingBox is taken from the SVG 1.1 spec.
365    virtual FloatRect objectBoundingBox() const;
366    virtual FloatRect strokeBoundingBox() const;
367
368    // Returns the smallest rectangle enclosing all of the painted content
369    // respecting clipping, masking, filters, opacity, stroke-width and markers
370    virtual FloatRect repaintRectInLocalCoordinates() const;
371
372    // This only returns the transform="" value from the element
373    // most callsites want localToParentTransform() instead.
374    virtual AffineTransform localTransform() const;
375
376    // Returns the full transform mapping from local coordinates to local coords for the parent SVG renderer
377    // This includes any viewport transforms and x/y offsets as well as the transform="" value off the element.
378    virtual const AffineTransform& localToParentTransform() const;
379
380    // SVG uses FloatPoint precise hit testing, and passes the point in parent
381    // coordinates instead of in repaint container coordinates.  Eventually the
382    // rest of the rendering tree will move to a similar model.
383    virtual bool nodeAtFloatPoint(const HitTestRequest&, HitTestResult&, const FloatPoint& pointInParent, HitTestAction);
384#endif
385
386    bool isAnonymous() const { return m_isAnonymous; }
387    void setIsAnonymous(bool b) { m_isAnonymous = b; }
388    bool isAnonymousBlock() const
389    {
390        return m_isAnonymous && style()->display() == BLOCK && style()->styleType() == NOPSEUDO && !isListMarker();
391    }
392    bool isAnonymousColumnsBlock() const { return style()->specifiesColumns() && isAnonymousBlock(); }
393    bool isAnonymousColumnSpanBlock() const { return style()->columnSpan() && isAnonymousBlock(); }
394    bool isElementContinuation() const { return node() && node()->renderer() != this; }
395    bool isInlineElementContinuation() const { return isElementContinuation() && isInline(); }
396    bool isBlockElementContinuation() const { return isElementContinuation() && !isInline(); }
397    virtual RenderBoxModelObject* virtualContinuation() const { return 0; }
398
399    bool isFloating() const { return m_floating; }
400    bool isPositioned() const { return m_positioned; } // absolute or fixed positioning
401    bool isRelPositioned() const { return m_relPositioned; } // relative positioning
402    bool isText() const  { return m_isText; }
403    bool isBox() const { return m_isBox; }
404    bool isInline() const { return m_inline; }  // inline object
405    bool isRunIn() const { return style()->display() == RUN_IN; } // run-in object
406    bool isDragging() const { return m_isDragging; }
407    bool isReplaced() const { return m_replaced; } // a "replaced" element (see CSS)
408
409    bool hasLayer() const { return m_hasLayer; }
410
411    bool hasBoxDecorations() const { return m_paintBackground; }
412    bool mustRepaintBackgroundOrBorder() const;
413    bool hasBackground() const { return style()->hasBackground(); }
414    bool needsLayout() const { return m_needsLayout || m_normalChildNeedsLayout || m_posChildNeedsLayout || m_needsPositionedMovementLayout; }
415    bool selfNeedsLayout() const { return m_needsLayout; }
416    bool needsPositionedMovementLayout() const { return m_needsPositionedMovementLayout; }
417    bool needsPositionedMovementLayoutOnly() const { return m_needsPositionedMovementLayout && !m_needsLayout && !m_normalChildNeedsLayout && !m_posChildNeedsLayout; }
418    bool posChildNeedsLayout() const { return m_posChildNeedsLayout; }
419    bool normalChildNeedsLayout() const { return m_normalChildNeedsLayout; }
420
421    bool preferredLogicalWidthsDirty() const { return m_preferredLogicalWidthsDirty; }
422
423    bool isSelectionBorder() const;
424
425    bool hasClip() const { return isPositioned() && style()->hasClip(); }
426    bool hasOverflowClip() const { return m_hasOverflowClip; }
427
428    bool hasTransform() const { return m_hasTransform; }
429    bool hasMask() const { return style() && style()->hasMask(); }
430
431    void drawLineForBoxSide(GraphicsContext*, int x1, int y1, int x2, int y2, BoxSide,
432                            Color, EBorderStyle, int adjbw1, int adjbw2);
433#if HAVE(PATH_BASED_BORDER_RADIUS_DRAWING)
434    void drawBoxSideFromPath(GraphicsContext*, IntRect, Path,
435                            float thickness, float drawThickness, BoxSide, const RenderStyle*,
436                            Color, EBorderStyle);
437#else
438    // FIXME: This function should be removed when all ports implement GraphicsContext::clipConvexPolygon()!!
439    // At that time, everyone can use RenderObject::drawBoxSideFromPath() instead. This should happen soon.
440    void drawArcForBoxSide(GraphicsContext*, int x, int y, float thickness, IntSize radius, int angleStart,
441                           int angleSpan, BoxSide, Color, EBorderStyle, bool firstCorner);
442#endif
443
444    IntRect borderInnerRect(const IntRect&, unsigned short topWidth, unsigned short bottomWidth,
445                            unsigned short leftWidth, unsigned short rightWidth) const;
446
447    // The pseudo element style can be cached or uncached.  Use the cached method if the pseudo element doesn't respect
448    // any pseudo classes (and therefore has no concept of changing state).
449    RenderStyle* getCachedPseudoStyle(PseudoId, RenderStyle* parentStyle = 0) const;
450    PassRefPtr<RenderStyle> getUncachedPseudoStyle(PseudoId, RenderStyle* parentStyle = 0, RenderStyle* ownStyle = 0) const;
451
452    virtual void updateDragState(bool dragOn);
453
454    RenderView* view() const;
455
456    // Returns true if this renderer is rooted, and optionally returns the hosting view (the root of the hierarchy).
457    bool isRooted(RenderView** = 0);
458
459    Node* node() const { return m_isAnonymous ? 0 : m_node; }
460
461    // Returns the styled node that caused the generation of this renderer.
462    // This is the same as node() except for renderers of :before and :after
463    // pseudo elements for which their parent node is returned.
464    Node* generatingNode() const { return m_node == document() ? 0 : m_node; }
465    void setNode(Node* node) { m_node = node; }
466
467    Document* document() const { return m_node->document(); }
468    Frame* frame() const { return document()->frame(); }
469
470    bool hasOutlineAnnotation() const;
471    bool hasOutline() const { return style()->hasOutline() || hasOutlineAnnotation(); }
472
473    // Returns the object containing this one. Can be different from parent for positioned elements.
474    // If repaintContainer and repaintContainerSkipped are not null, on return *repaintContainerSkipped
475    // is true if the renderer returned is an ancestor of repaintContainer.
476    RenderObject* container(RenderBoxModelObject* repaintContainer = 0, bool* repaintContainerSkipped = 0) const;
477
478    virtual RenderObject* hoverAncestor() const { return parent(); }
479
480    // IE Extension that can be called on any RenderObject.  See the implementation for the details.
481    RenderBoxModelObject* offsetParent() const;
482
483    void markContainingBlocksForLayout(bool scheduleRelayout = true, RenderObject* newRoot = 0);
484    void setNeedsLayout(bool b, bool markParents = true);
485    void setChildNeedsLayout(bool b, bool markParents = true);
486    void setNeedsPositionedMovementLayout();
487    void setPreferredLogicalWidthsDirty(bool, bool markParents = true);
488    void invalidateContainerPreferredLogicalWidths();
489
490    void setNeedsLayoutAndPrefWidthsRecalc()
491    {
492        setNeedsLayout(true);
493        setPreferredLogicalWidthsDirty(true);
494    }
495
496    void setPositioned(bool b = true)  { m_positioned = b;  }
497    void setRelPositioned(bool b = true) { m_relPositioned = b; }
498    void setFloating(bool b = true) { m_floating = b; }
499    void setInline(bool b = true) { m_inline = b; }
500    void setHasBoxDecorations(bool b = true) { m_paintBackground = b; }
501    void setIsText() { m_isText = true; }
502    void setIsBox() { m_isBox = true; }
503    void setReplaced(bool b = true) { m_replaced = b; }
504    void setHasOverflowClip(bool b = true) { m_hasOverflowClip = b; }
505    void setHasLayer(bool b = true) { m_hasLayer = b; }
506    void setHasTransform(bool b = true) { m_hasTransform = b; }
507    void setHasReflection(bool b = true) { m_hasReflection = b; }
508
509    void scheduleRelayout();
510
511    void updateFillImages(const FillLayer*, const FillLayer*);
512    void updateImage(StyleImage*, StyleImage*);
513
514    virtual void paint(PaintInfo&, int tx, int ty);
515
516    // Recursive function that computes the size and position of this object and all its descendants.
517    virtual void layout();
518
519    /* This function performs a layout only if one is needed. */
520    void layoutIfNeeded() { if (needsLayout()) layout(); }
521
522    // used for element state updates that cannot be fixed with a
523    // repaint and do not need a relayout
524    virtual void updateFromElement() { }
525
526#if ENABLE(DASHBOARD_SUPPORT)
527    virtual void addDashboardRegions(Vector<DashboardRegionValue>&);
528    void collectDashboardRegions(Vector<DashboardRegionValue>&);
529#endif
530
531    bool hitTest(const HitTestRequest&, HitTestResult&, const IntPoint&, int tx, int ty, HitTestFilter = HitTestAll);
532    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
533    virtual void updateHitTestResult(HitTestResult&, const IntPoint&);
534
535    VisiblePosition positionForCoordinates(int x, int y);
536    virtual VisiblePosition positionForPoint(const IntPoint&);
537    VisiblePosition createVisiblePosition(int offset, EAffinity);
538    VisiblePosition createVisiblePosition(const Position&);
539
540    virtual void dirtyLinesFromChangedChild(RenderObject*);
541
542    // Called to update a style that is allowed to trigger animations.
543    // FIXME: Right now this will typically be called only when updating happens from the DOM on explicit elements.
544    // We don't yet handle generated content animation such as first-letter or before/after (we'll worry about this later).
545    void setAnimatableStyle(PassRefPtr<RenderStyle>);
546
547    // Set the style of the object and update the state of the object accordingly.
548    virtual void setStyle(PassRefPtr<RenderStyle>);
549
550    // Updates only the local style ptr of the object.  Does not update the state of the object,
551    // and so only should be called when the style is known not to have changed (or from setStyle).
552    void setStyleInternal(PassRefPtr<RenderStyle>);
553
554    // returns the containing block level element for this element.
555    RenderBlock* containingBlock() const;
556
557    // Convert the given local point to absolute coordinates
558    // FIXME: Temporary. If useTransforms is true, take transforms into account. Eventually localToAbsolute() will always be transform-aware.
559    FloatPoint localToAbsolute(FloatPoint localPoint = FloatPoint(), bool fixed = false, bool useTransforms = false) const;
560    FloatPoint absoluteToLocal(FloatPoint, bool fixed = false, bool useTransforms = false) const;
561
562    // Convert a local quad to absolute coordinates, taking transforms into account.
563    FloatQuad localToAbsoluteQuad(const FloatQuad& quad, bool fixed = false) const
564    {
565        return localToContainerQuad(quad, 0, fixed);
566    }
567    // Convert a local quad into the coordinate system of container, taking transforms into account.
568    FloatQuad localToContainerQuad(const FloatQuad&, RenderBoxModelObject* repaintContainer, bool fixed = false) const;
569
570    // Return the offset from the container() renderer (excluding transforms). In multi-column layout,
571    // different offsets apply at different points, so return the offset that applies to the given point.
572    virtual IntSize offsetFromContainer(RenderObject*, const IntPoint&) const;
573    // Return the offset from an object up the container() chain. Asserts that none of the intermediate objects have transforms.
574    IntSize offsetFromAncestorContainer(RenderObject*) const;
575
576    virtual void absoluteRects(Vector<IntRect>&, int, int) { }
577    // FIXME: useTransforms should go away eventually
578    IntRect absoluteBoundingBoxRect(bool useTransforms = false);
579
580    // Build an array of quads in absolute coords for line boxes
581    virtual void absoluteQuads(Vector<FloatQuad>&) { }
582
583    void absoluteFocusRingQuads(Vector<FloatQuad>&);
584
585    // the rect that will be painted if this object is passed as the paintingRoot
586    IntRect paintingRootRect(IntRect& topLevelRect);
587
588    virtual int minPreferredLogicalWidth() const { return 0; }
589    virtual int maxPreferredLogicalWidth() const { return 0; }
590
591    RenderStyle* style() const { return m_style.get(); }
592    RenderStyle* firstLineStyle() const { return document()->usesFirstLineRules() ? firstLineStyleSlowCase() : style(); }
593    RenderStyle* style(bool firstLine) const { return firstLine ? firstLineStyle() : style(); }
594
595    // Used only by Element::pseudoStyleCacheIsInvalid to get a first line style based off of a
596    // given new style, without accessing the cache.
597    PassRefPtr<RenderStyle> uncachedFirstLineStyle(RenderStyle*) const;
598
599    // Anonymous blocks that are part of of a continuation chain will return their inline continuation's outline style instead.
600    // This is typically only relevant when repainting.
601    virtual RenderStyle* outlineStyleForRepaint() const { return style(); }
602
603    void getTextDecorationColors(int decorations, Color& underline, Color& overline,
604                                 Color& linethrough, bool quirksMode = false);
605
606    // Return the RenderBox in the container chain which is responsible for painting this object, or 0
607    // if painting is root-relative. This is the container that should be passed to the 'forRepaint'
608    // methods.
609    RenderBoxModelObject* containerForRepaint() const;
610    // Actually do the repaint of rect r for this object which has been computed in the coordinate space
611    // of repaintContainer. If repaintContainer is 0, repaint via the view.
612    void repaintUsingContainer(RenderBoxModelObject* repaintContainer, const IntRect& r, bool immediate = false);
613
614    // Repaint the entire object.  Called when, e.g., the color of a border changes, or when a border
615    // style changes.
616    void repaint(bool immediate = false);
617
618    // Repaint a specific subrectangle within a given object.  The rect |r| is in the object's coordinate space.
619    void repaintRectangle(const IntRect&, bool immediate = false);
620
621    // Repaint only if our old bounds and new bounds are different. The caller may pass in newBounds and newOutlineBox if they are known.
622    bool repaintAfterLayoutIfNeeded(RenderBoxModelObject* repaintContainer, const IntRect& oldBounds, const IntRect& oldOutlineBox, const IntRect* newBoundsPtr = 0, const IntRect* newOutlineBoxPtr = 0);
623
624    // Repaint only if the object moved.
625    virtual void repaintDuringLayoutIfMoved(const IntRect& rect);
626
627    // Called to repaint a block's floats.
628    virtual void repaintOverhangingFloats(bool paintAllDescendants = false);
629
630    bool checkForRepaintDuringLayout() const;
631
632    // Returns the rect that should be repainted whenever this object changes.  The rect is in the view's
633    // coordinate space.  This method deals with outlines and overflow.
634    IntRect absoluteClippedOverflowRect()
635    {
636        return clippedOverflowRectForRepaint(0);
637    }
638    virtual IntRect clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer);
639    virtual IntRect rectWithOutlineForRepaint(RenderBoxModelObject* repaintContainer, int outlineWidth);
640
641    // Given a rect in the object's coordinate space, compute a rect suitable for repainting
642    // that rect in view coordinates.
643    void computeAbsoluteRepaintRect(IntRect& r, bool fixed = false)
644    {
645        return computeRectForRepaint(0, r, fixed);
646    }
647    // Given a rect in the object's coordinate space, compute a rect suitable for repainting
648    // that rect in the coordinate space of repaintContainer.
649    virtual void computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect&, bool fixed = false);
650
651    // If multiple-column layout results in applying an offset to the given point, add the same
652    // offset to the given size.
653    virtual void adjustForColumns(IntSize&, const IntPoint&) const { }
654
655    virtual unsigned int length() const { return 1; }
656
657    bool isFloatingOrPositioned() const { return (isFloating() || isPositioned()); }
658
659    bool isTransparent() const { return style()->opacity() < 1.0f; }
660    float opacity() const { return style()->opacity(); }
661
662    bool hasReflection() const { return m_hasReflection; }
663
664    // Applied as a "slop" to dirty rect checks during the outline painting phase's dirty-rect checks.
665    int maximalOutlineSize(PaintPhase) const;
666
667    void setHasMarkupTruncation(bool b = true) { m_hasMarkupTruncation = b; }
668    bool hasMarkupTruncation() const { return m_hasMarkupTruncation; }
669
670    enum SelectionState {
671        SelectionNone, // The object is not selected.
672        SelectionStart, // The object either contains the start of a selection run or is the start of a run
673        SelectionInside, // The object is fully encompassed by a selection run
674        SelectionEnd, // The object either contains the end of a selection run or is the end of a run
675        SelectionBoth // The object contains an entire run or is the sole selected object in that run
676    };
677
678    // The current selection state for an object.  For blocks, the state refers to the state of the leaf
679    // descendants (as described above in the SelectionState enum declaration).
680    SelectionState selectionState() const { return static_cast<SelectionState>(m_selectionState);; }
681
682    // Sets the selection state for an object.
683    virtual void setSelectionState(SelectionState state) { m_selectionState = state; }
684
685    // A single rectangle that encompasses all of the selected objects within this object.  Used to determine the tightest
686    // possible bounding box for the selection.
687    IntRect selectionRect(bool clipToVisibleContent = true) { return selectionRectForRepaint(0, clipToVisibleContent); }
688    virtual IntRect selectionRectForRepaint(RenderBoxModelObject* /*repaintContainer*/, bool /*clipToVisibleContent*/ = true) { return IntRect(); }
689
690    // Whether or not an object can be part of the leaf elements of the selection.
691    virtual bool canBeSelectionLeaf() const { return false; }
692
693    // Whether or not a block has selected children.
694    bool hasSelectedChildren() const { return m_selectionState != SelectionNone; }
695
696    // Obtains the selection colors that should be used when painting a selection.
697    Color selectionBackgroundColor() const;
698    Color selectionForegroundColor() const;
699    Color selectionEmphasisMarkColor() const;
700
701    // Whether or not a given block needs to paint selection gaps.
702    virtual bool shouldPaintSelectionGaps() const { return false; }
703
704#if ENABLE(DRAG_SUPPORT)
705    Node* draggableNode(bool dhtmlOK, bool uaOK, int x, int y, bool& dhtmlWillDrag) const;
706#endif
707
708    /**
709     * Returns the local coordinates of the caret within this render object.
710     * @param caretOffset zero-based offset determining position within the render object.
711     * @param extraWidthToEndOfLine optional out arg to give extra width to end of line -
712     * useful for character range rect computations
713     */
714    virtual IntRect localCaretRect(InlineBox*, int caretOffset, int* extraWidthToEndOfLine = 0);
715
716    bool isMarginBeforeQuirk() const { return m_marginBeforeQuirk; }
717    bool isMarginAfterQuirk() const { return m_marginAfterQuirk; }
718    void setMarginBeforeQuirk(bool b = true) { m_marginBeforeQuirk = b; }
719    void setMarginAfterQuirk(bool b = true) { m_marginAfterQuirk = b; }
720
721    // When performing a global document tear-down, the renderer of the document is cleared.  We use this
722    // as a hook to detect the case of document destruction and don't waste time doing unnecessary work.
723    bool documentBeingDestroyed() const;
724
725    virtual void destroy();
726
727    // Virtual function helpers for CSS3 Flexible Box Layout
728    virtual bool isFlexibleBox() const { return false; }
729    virtual bool isFlexingChildren() const { return false; }
730    virtual bool isStretchingChildren() const { return false; }
731
732    virtual int caretMinOffset() const;
733    virtual int caretMaxOffset() const;
734    virtual unsigned caretMaxRenderedOffset() const;
735
736    virtual int previousOffset(int current) const;
737    virtual int previousOffsetForBackwardDeletion(int current) const;
738    virtual int nextOffset(int current) const;
739
740    virtual void imageChanged(CachedImage*, const IntRect* = 0);
741    virtual void imageChanged(WrappedImagePtr, const IntRect* = 0) { }
742    virtual bool willRenderImage(CachedImage*);
743
744    void selectionStartEnd(int& spos, int& epos) const;
745
746    bool hasOverrideSize() const { return m_hasOverrideSize; }
747    void setHasOverrideSize(bool b) { m_hasOverrideSize = b; }
748
749    void remove() { if (parent()) parent()->removeChild(this); }
750
751    AnimationController* animation() const;
752
753    bool visibleToHitTesting() const { return style()->visibility() == VISIBLE && style()->pointerEvents() != PE_NONE; }
754
755    // Map points and quads through elements, potentially via 3d transforms. You should never need to call these directly; use
756    // localToAbsolute/absoluteToLocal methods instead.
757    virtual void mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool useTransforms, bool fixed, TransformState&) const;
758    virtual void mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState&) const;
759
760    bool shouldUseTransformFromContainer(const RenderObject* container) const;
761    void getTransformFromContainer(const RenderObject* container, const IntSize& offsetInContainer, TransformationMatrix&) const;
762
763    virtual void addFocusRingRects(Vector<IntRect>&, int /*tx*/, int /*ty*/) { };
764
765    IntRect absoluteOutlineBounds() const
766    {
767        return outlineBoundsForRepaint(0);
768    }
769
770protected:
771    // Overrides should call the superclass at the end
772    virtual void styleWillChange(StyleDifference, const RenderStyle* newStyle);
773    // Overrides should call the superclass at the start
774    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
775
776    void paintFocusRing(GraphicsContext*, int tx, int ty, RenderStyle*);
777    void paintOutline(GraphicsContext*, int tx, int ty, int w, int h);
778    void addPDFURLRect(GraphicsContext*, const IntRect&);
779
780    virtual IntRect viewRect() const;
781
782    void adjustRectForOutlineAndShadow(IntRect&) const;
783
784    void arenaDelete(RenderArena*, void* objectBase);
785
786    virtual IntRect outlineBoundsForRepaint(RenderBoxModelObject* /*repaintContainer*/, IntPoint* /*cachedOffsetToRepaintContainer*/ = 0) const { return IntRect(); }
787
788    class LayoutRepainter {
789    public:
790        LayoutRepainter(RenderObject& object, bool checkForRepaint, const IntRect* oldBounds = 0)
791            : m_object(object)
792            , m_repaintContainer(0)
793            , m_checkForRepaint(checkForRepaint)
794        {
795            if (m_checkForRepaint) {
796                m_repaintContainer = m_object.containerForRepaint();
797                m_oldBounds = oldBounds ? *oldBounds : m_object.clippedOverflowRectForRepaint(m_repaintContainer);
798                m_oldOutlineBox = m_object.outlineBoundsForRepaint(m_repaintContainer);
799            }
800        }
801
802        // Return true if it repainted.
803        bool repaintAfterLayout()
804        {
805            return m_checkForRepaint ? m_object.repaintAfterLayoutIfNeeded(m_repaintContainer, m_oldBounds, m_oldOutlineBox) : false;
806        }
807
808        bool checkForRepaint() const { return m_checkForRepaint; }
809
810    private:
811        RenderObject& m_object;
812        RenderBoxModelObject* m_repaintContainer;
813        IntRect m_oldBounds;
814        IntRect m_oldOutlineBox;
815        bool m_checkForRepaint;
816    };
817
818private:
819    RenderStyle* firstLineStyleSlowCase() const;
820    StyleDifference adjustStyleDifference(StyleDifference, unsigned contextSensitiveProperties) const;
821
822    Color selectionColor(int colorProperty) const;
823
824    RefPtr<RenderStyle> m_style;
825
826    Node* m_node;
827
828    RenderObject* m_parent;
829    RenderObject* m_previous;
830    RenderObject* m_next;
831
832#ifndef NDEBUG
833    bool m_hasAXObject;
834    bool m_setNeedsLayoutForbidden : 1;
835#endif
836
837    // 32 bits have been used here. THERE ARE NO FREE BITS AVAILABLE.
838    bool m_needsLayout               : 1;
839    bool m_needsPositionedMovementLayout :1;
840    bool m_normalChildNeedsLayout    : 1;
841    bool m_posChildNeedsLayout       : 1;
842    bool m_preferredLogicalWidthsDirty           : 1;
843    bool m_floating                  : 1;
844
845    bool m_positioned                : 1;
846    bool m_relPositioned             : 1;
847    bool m_paintBackground           : 1; // if the box has something to paint in the
848                                          // background painting phase (background, border, etc)
849
850    bool m_isAnonymous               : 1;
851    bool m_isText                    : 1;
852    bool m_isBox                     : 1;
853    bool m_inline                    : 1;
854    bool m_replaced                  : 1;
855    bool m_isDragging                : 1;
856
857    bool m_hasLayer                  : 1;
858    bool m_hasOverflowClip           : 1; // Set in the case of overflow:auto/scroll/hidden
859    bool m_hasTransform              : 1;
860    bool m_hasReflection             : 1;
861
862    bool m_hasOverrideSize           : 1;
863
864public:
865    bool m_hasCounterNodeMap         : 1;
866    bool m_everHadLayout             : 1;
867
868private:
869    // These bitfields are moved here from subclasses to pack them together
870    // from RenderBlock
871    bool m_childrenInline : 1;
872    bool m_marginBeforeQuirk : 1;
873    bool m_marginAfterQuirk : 1;
874    bool m_hasMarkupTruncation : 1;
875    unsigned m_selectionState : 3; // SelectionState
876    bool m_hasColumns : 1;
877
878    // from RenderTableCell
879    bool m_cellWidthChanged : 1;
880
881private:
882    // Store state between styleWillChange and styleDidChange
883    static bool s_affectsParentBlock;
884};
885
886inline bool RenderObject::documentBeingDestroyed() const
887{
888    return !document()->renderer();
889}
890
891inline bool RenderObject::isBeforeContent() const
892{
893    if (style()->styleType() != BEFORE)
894        return false;
895    // Text nodes don't have their own styles, so ignore the style on a text node.
896    if (isText() && !isBR())
897        return false;
898    return true;
899}
900
901inline bool RenderObject::isAfterContent() const
902{
903    if (style()->styleType() != AFTER)
904        return false;
905    // Text nodes don't have their own styles, so ignore the style on a text node.
906    if (isText() && !isBR())
907        return false;
908    return true;
909}
910
911inline void RenderObject::setNeedsLayout(bool b, bool markParents)
912{
913    bool alreadyNeededLayout = m_needsLayout;
914    m_needsLayout = b;
915    if (b) {
916        ASSERT(!isSetNeedsLayoutForbidden());
917        if (!alreadyNeededLayout) {
918            if (markParents)
919                markContainingBlocksForLayout();
920            if (hasLayer())
921                setLayerNeedsFullRepaint();
922        }
923    } else {
924        m_everHadLayout = true;
925        m_posChildNeedsLayout = false;
926        m_normalChildNeedsLayout = false;
927        m_needsPositionedMovementLayout = false;
928    }
929}
930
931inline void RenderObject::setChildNeedsLayout(bool b, bool markParents)
932{
933    bool alreadyNeededLayout = m_normalChildNeedsLayout;
934    m_normalChildNeedsLayout = b;
935    if (b) {
936        ASSERT(!isSetNeedsLayoutForbidden());
937        if (!alreadyNeededLayout && markParents)
938            markContainingBlocksForLayout();
939    } else {
940        m_posChildNeedsLayout = false;
941        m_normalChildNeedsLayout = false;
942        m_needsPositionedMovementLayout = false;
943    }
944}
945
946inline void RenderObject::setNeedsPositionedMovementLayout()
947{
948    bool alreadyNeededLayout = needsLayout();
949    m_needsPositionedMovementLayout = true;
950    if (!alreadyNeededLayout) {
951        markContainingBlocksForLayout();
952        if (hasLayer())
953            setLayerNeedsFullRepaint();
954    }
955}
956
957inline bool objectIsRelayoutBoundary(const RenderObject *obj)
958{
959    // FIXME: In future it may be possible to broaden this condition in order to improve performance.
960    // Table cells are excluded because even when their CSS height is fixed, their height()
961    // may depend on their contents.
962    return obj->isTextControl()
963        || (obj->hasOverflowClip() && !obj->style()->width().isIntrinsicOrAuto() && !obj->style()->height().isIntrinsicOrAuto() && !obj->style()->height().isPercent() && !obj->isTableCell())
964#if ENABLE(SVG)
965           || obj->isSVGRoot()
966#endif
967           ;
968}
969
970inline void RenderObject::markContainingBlocksForLayout(bool scheduleRelayout, RenderObject* newRoot)
971{
972    ASSERT(!scheduleRelayout || !newRoot);
973
974    RenderObject* o = container();
975    RenderObject* last = this;
976
977    while (o) {
978        // Don't mark the outermost object of an unrooted subtree. That object will be
979        // marked when the subtree is added to the document.
980        RenderObject* container = o->container();
981        if (!container && !o->isRenderView())
982            return;
983        if (!last->isText() && (last->style()->position() == FixedPosition || last->style()->position() == AbsolutePosition)) {
984            if ((last->style()->top().isAuto() && last->style()->bottom().isAuto()) || last->style()->top().isStatic()) {
985                RenderObject* parent = last->parent();
986                if (!parent->normalChildNeedsLayout()) {
987                    parent->setChildNeedsLayout(true, false);
988                    if (parent != newRoot)
989                        parent->markContainingBlocksForLayout(scheduleRelayout, newRoot);
990                }
991            }
992            if (o->m_posChildNeedsLayout)
993                return;
994            o->m_posChildNeedsLayout = true;
995            ASSERT(!o->isSetNeedsLayoutForbidden());
996        } else {
997            if (o->m_normalChildNeedsLayout)
998                return;
999            o->m_normalChildNeedsLayout = true;
1000            ASSERT(!o->isSetNeedsLayoutForbidden());
1001        }
1002
1003        if (o == newRoot)
1004            return;
1005
1006        last = o;
1007        if (scheduleRelayout && objectIsRelayoutBoundary(last))
1008            break;
1009        o = container;
1010    }
1011
1012    if (scheduleRelayout)
1013        last->scheduleRelayout();
1014}
1015
1016inline void makeMatrixRenderable(TransformationMatrix& matrix, bool has3DRendering)
1017{
1018#if !ENABLE(3D_RENDERING)
1019    UNUSED_PARAM(has3DRendering);
1020    matrix.makeAffine();
1021#else
1022    if (!has3DRendering)
1023        matrix.makeAffine();
1024#endif
1025}
1026
1027inline int adjustForAbsoluteZoom(int value, RenderObject* renderer)
1028{
1029    return adjustForAbsoluteZoom(value, renderer->style());
1030}
1031
1032inline FloatPoint adjustFloatPointForAbsoluteZoom(const FloatPoint& point, RenderObject* renderer)
1033{
1034    // The result here is in floats, so we don't need the truncation hack from the integer version above.
1035    float zoomFactor = renderer->style()->effectiveZoom();
1036    if (zoomFactor == 1)
1037        return point;
1038    return FloatPoint(point.x() / zoomFactor, point.y() / zoomFactor);
1039}
1040
1041inline void adjustFloatQuadForAbsoluteZoom(FloatQuad& quad, RenderObject* renderer)
1042{
1043    quad.setP1(adjustFloatPointForAbsoluteZoom(quad.p1(), renderer));
1044    quad.setP2(adjustFloatPointForAbsoluteZoom(quad.p2(), renderer));
1045    quad.setP3(adjustFloatPointForAbsoluteZoom(quad.p3(), renderer));
1046    quad.setP4(adjustFloatPointForAbsoluteZoom(quad.p4(), renderer));
1047}
1048
1049inline void adjustFloatRectForAbsoluteZoom(FloatRect& rect, RenderObject* renderer)
1050{
1051    RenderStyle* style = renderer->style();
1052    rect.setX(adjustFloatForAbsoluteZoom(rect.x(), style));
1053    rect.setY(adjustFloatForAbsoluteZoom(rect.y(), style));
1054    rect.setWidth(adjustFloatForAbsoluteZoom(rect.width(), style));
1055    rect.setHeight(adjustFloatForAbsoluteZoom(rect.height(), style));
1056}
1057
1058} // namespace WebCore
1059
1060#ifndef NDEBUG
1061// Outside the WebCore namespace for ease of invocation from gdb.
1062void showTree(const WebCore::RenderObject*);
1063void showRenderTree(const WebCore::RenderObject* object1);
1064// We don't make object2 an optional parameter so that showRenderTree
1065// can be called from gdb easily.
1066void showRenderTree(const WebCore::RenderObject* object1, const WebCore::RenderObject* object2);
1067#endif
1068
1069#endif // RenderObject_h
1070