render_object.h 19.6 KB
Newer Older
1
/*
kocienda's avatar
kocienda committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * This file is part of the html renderer for KDE.
 *
 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
 *           (C) 2000 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Dirk Mueller (mueller@kde.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */
#ifndef render_object_h
#define render_object_h

#include <qcolor.h>
#include <qrect.h>
29
#include <assert.h>
kocienda's avatar
kocienda committed
30
31
32

#include "misc/khtmllayout.h"
#include "misc/loader_client.h"
33
34
#include "misc/helper.h"
#include "rendering/render_style.h"
kocienda's avatar
kocienda committed
35
36

class QPainter;
37
class QTextStream;
kocienda's avatar
kocienda committed
38
39
class CSSStyle;
class KHTMLView;
40
class RenderArena;
kocienda's avatar
kocienda committed
41

42
43
44
45
#ifndef NDEBUG
#define KHTMLAssert( x ) if( !(x) ) { \
    const RenderObject *o = this; while( o->parent() ) o = o->parent(); \
    o->printTree(); \
darin's avatar
darin committed
46
    qDebug(" this object = %p", this ); \
47
48
49
50
51
52
    assert( false ); \
}
#else
#define KHTMLAssert( x )
#endif

kocienda's avatar
kocienda committed
53
namespace DOM {
54
    class HTMLAreaElementImpl;
kocienda's avatar
kocienda committed
55
56
    class DOMString;
    class NodeImpl;
57
58
    class ElementImpl;
    class EventImpl;
kocienda's avatar
kocienda committed
59
60
61
};

namespace khtml {
62
    class RenderFlow;
kocienda's avatar
kocienda committed
63
64
65
66
    class RenderStyle;
    class RenderTable;
    class CachedObject;
    class RenderRoot;
67
68
    class RenderText;
    class RenderFrameSet;
69
    class RenderLayer;
kocienda's avatar
kocienda committed
70
71
72
73
74
75
76
77

/**
 * Base Class for all rendering tree objects.
 */
class RenderObject : public CachedObjectClient
{
public:

78
    RenderObject(DOM::NodeImpl* node);
kocienda's avatar
kocienda committed
79
80
81
82
83
84
85
86
87
88
    virtual ~RenderObject();

    RenderObject *parent() const { return m_parent; }

    RenderObject *previousSibling() const { return m_previous; }
    RenderObject *nextSibling() const { return m_next; }

    virtual RenderObject *firstChild() const { return 0; }
    virtual RenderObject *lastChild() const { return 0; }

89
90
91
92
93
    virtual RenderLayer* layer() const { return 0; }
    virtual RenderLayer* enclosingLayer() { return 0; }
    virtual void setHasChildLayers(bool hasLayers) { }
    virtual void positionChildLayers() { }
    
94
95
    virtual QRect getClipRect(int tx, int ty) { return QRect(0,0,0,0); }
    
kocienda's avatar
kocienda committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    // RenderObject tree manipulation
    //////////////////////////////////////////
    virtual void addChild(RenderObject *newChild, RenderObject *beforeChild = 0);
    virtual void removeChild(RenderObject *oldChild);

    // raw tree manipulation
    virtual RenderObject* removeChildNode(RenderObject* child);
    virtual void appendChildNode(RenderObject* child);
    virtual void insertChildNode(RenderObject* child, RenderObject* before);
    //////////////////////////////////////////

private:
    //////////////////////////////////////////
    // Helper functions. Dangerous to use!
    void setPreviousSibling(RenderObject *previous) { m_previous = previous; }
    void setNextSibling(RenderObject *next) { m_next = next; }
    void setParent(RenderObject *parent) { m_parent = parent; }
    //////////////////////////////////////////

public:
    virtual const char *renderName() const { return "RenderObject"; }
117
118
#ifndef NDEBUG
    QString information() const;
kocienda's avatar
kocienda committed
119
    virtual void printTree(int indent=0) const;
120
121
    virtual void dump(QTextStream *stream, QString ind = "") const;
#endif
kocienda's avatar
kocienda committed
122

123
    static RenderObject *createObject(DOM::NodeImpl* node, RenderStyle* style);
kocienda's avatar
kocienda committed
124

125
126
127
128
129
130
131
132
133
    // Overloaded new operator.  Derived classes must override operator new
    // in order to allocate out of the RenderArena.
    void* operator new(size_t sz, RenderArena* renderArena) throw();    

    // Overridden to prevent the normal delete from being called.
    void operator delete(void* ptr, size_t sz);
        
private:
    // The normal operator new is disallowed on all render objects.
134
    void* operator new(size_t sz) throw();
135
136
137
138
    
public:
    RenderArena* renderArena();
    
kocienda's avatar
kocienda committed
139
140
141
142
143
144
    // some helper functions...
    virtual bool childrenInline() const { return false; }
    virtual bool isRendered() const { return false; }
    virtual bool isFlow() const { return false; }

    virtual bool isListItem() const { return false; }
145
    virtual bool isListMarker() const { return false; }
kocienda's avatar
kocienda committed
146
147
148
149
150
151
152
153
154
155
    virtual bool isRoot() const { return false; }
    virtual bool isBR() const { return false; }
    virtual bool isHtml() const { return false; }
    virtual bool isTableCell() const { return false; }
    virtual bool isTableRow() const { return false; }
    virtual bool isTableSection() const { return false; }
    virtual bool isTable() const { return false; }
    virtual bool isWidget() const { return false; }
    virtual bool isBody() const { return false; }
    virtual bool isFormElement() const { return false; }
156
    virtual bool isTextArea() const { return false; }
157
    virtual bool isFrameSet() const { return false; }
darin's avatar
darin committed
158
    virtual bool isApplet() const { return false; }
kocienda's avatar
kocienda committed
159
160
161
162
163
164
165

    bool isAnonymousBox() const { return m_isAnonymous; }
    void setIsAnonymousBox(bool b) { m_isAnonymous = b; }

    bool isFloating() const { return m_floating; }
    bool isPositioned() const { return m_positioned; } // absolute or fixed positioning
    bool isRelPositioned() const { return m_relPositioned; } // relative positioning
166
    bool isText() const  { return m_isText; }
kocienda's avatar
kocienda committed
167
    bool isInline() const { return m_inline; }  // inline object
168
    bool mouseInside() const { return m_mouseInside; }
kocienda's avatar
kocienda committed
169
170
171
172
    bool isReplaced() const { return m_replaced; } // a "replaced" element (see CSS)
    bool hasSpecialObjects() const { return m_printSpecial; }
    bool layouted() const   { return m_layouted; }
    bool minMaxKnown() const{ return m_minMaxKnown; }
173
    bool overhangingContents() const { return m_overhangingContents; }
kocienda's avatar
kocienda committed
174
    bool hasFirstLine() const { return m_hasFirstLine; }
175
176
177
    bool isSelectionBorder() const { return m_isSelectionBorder; }
    bool recalcMinMax() const { return m_recalcMinMax; }

kocienda's avatar
kocienda committed
178
    RenderRoot* root() const;
179
180
181
182
    // don't even think about making this method virtual!
    DOM::NodeImpl* element() const { return m_node; }

   /**
kocienda's avatar
kocienda committed
183
184
185
186
187
     * returns the object containing this one. can be different from parent for
     * positioned elements
     */
    RenderObject *container() const;

188
    void setOverhangingContents(bool p=true);
hyatt's avatar
hyatt committed
189
    
190
191
    void setLayouted(bool b=true);
        
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    // hack to block inline layouts during parsing
    // evil, evil. I didn't do it. <tm>
    virtual void setBlockBidi() {}

    void setMinMaxKnown(bool b=true) {
	m_minMaxKnown = b;
	if ( !b ) {
	    RenderObject *o = this;
	    RenderObject *root = this;
	    while( o ) { // ### && !o->m_recalcMinMax ) {
		o->m_recalcMinMax = true;
		root = o;
		o = o->m_parent;
	    }
	}
    }
kocienda's avatar
kocienda committed
208
209
210
211
    void setPositioned(bool b=true)  { m_positioned = b;  }
    void setRelPositioned(bool b=true) { m_relPositioned = b; }
    void setFloating(bool b=true) { m_floating = b; }
    void setInline(bool b=true) { m_inline = b; }
212
    void setMouseInside(bool b=true) { m_mouseInside = b; }
kocienda's avatar
kocienda committed
213
214
215
    void setSpecialObjects(bool b=true) { m_printSpecial = b; }
    void setRenderText() { m_isText = true; }
    void setReplaced(bool b=true) { m_replaced = b; }
216
217
218
    void setIsSelectionBorder(bool b=true) { m_isSelectionBorder = b; }

    void scheduleRelayout();
kocienda's avatar
kocienda committed
219
220

    // for discussion of lineHeight see CSS2 spec
221
    virtual short lineHeight( bool firstLine ) const;
kocienda's avatar
kocienda committed
222
223
224
225
226
    // for the vertical-align property of inline elements
    // the difference between this objects baseline position and the lines baseline position.
    virtual short verticalPositionHint( bool firstLine ) const;
    // the offset of baseline from the top of the object.
    virtual short baselinePosition( bool firstLine ) const;
227

kocienda's avatar
kocienda committed
228
    /*
darin's avatar
darin committed
229
     * Print the object and its children, clipped by (x|y|w|h).
kocienda's avatar
kocienda committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
     * (tx|ty) is the calculated position of the parent
     */
    virtual void print( QPainter *p, int x, int y, int w, int h, int tx, int ty);

    virtual void printObject( QPainter */*p*/, int /*x*/, int /*y*/,
                        int /*w*/, int /*h*/, int /*tx*/, int /*ty*/) {}
    void printBorder(QPainter *p, int _tx, int _ty, int w, int h, const RenderStyle* style, bool begin=true, bool end=true);
    void printOutline(QPainter *p, int _tx, int _ty, int w, int h, const RenderStyle* style);


    /*
     * This function calculates the minimum & maximum width that the object
     * can be set to.
     *
     * when the Element calls setMinMaxKnown(true), calcMinMaxWidth() will
     * be no longer called.
     *
     * when a element has a fixed size, m_minWidth and m_maxWidth should be
     * set to the same value. This has the special meaning that m_width,
     * contains the actual value.
     *
251
     * assumes calcMinMaxWidth has already been called for all children.
kocienda's avatar
kocienda committed
252
253
254
     */
    virtual void calcMinMaxWidth() { }

255
256
257
258
259
    /*
     * Does the min max width recalculations after changes.
     */
    void recalcMinMaxWidths();

kocienda's avatar
kocienda committed
260
261
262
263
264
265
266
267
    /*
     * Calculates the actual width of the object (only for non inline
     * objects)
     */
    virtual void calcWidth() {}

    /*
     * This function should cause the Element to calculate its
darin's avatar
darin committed
268
     * width and height and the layout of its content
kocienda's avatar
kocienda committed
269
270
271
272
273
274
275
276
277
     *
     * when the Element calls setLayouted(true), layout() is no
     * longer called during relayouts, as long as there is no
     * style sheet change. When that occurs, isLayouted will be
     * set to false and the Element receives layout() calls
     * again.
     */
    virtual void layout() = 0;

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    // used for element state updates that can not be fixed with a
    // repaint and do not need a relayout
    virtual void updateFromElement() {};

    // The corresponding closing element has been parsed. ### remove me
    virtual void close() { }

    // does a query on the rendertree and finds the innernode
    // and overURL for the given position
    // if readonly == false, it will recalc hover styles accordingly
    class NodeInfo
    {
        friend class RenderImage;
        friend class RenderFlow;
        friend class RenderText;
        friend class RenderObject;
        friend class RenderFrameSet;
        friend class DOM::HTMLAreaElementImpl;
    public:
        NodeInfo(bool readonly, bool active)
cblu's avatar
cblu committed
298
            : m_innerNode(0), m_innerNonSharedNode(0), m_innerURLElement(0), m_readonly(readonly), m_active(active)
299
300
301
            { }

        DOM::NodeImpl* innerNode() const { return m_innerNode; }
cblu's avatar
cblu committed
302
        DOM::NodeImpl* innerNonSharedNode() const { return m_innerNonSharedNode; }
303
304
305
306
307
308
        DOM::NodeImpl* URLElement() const { return m_innerURLElement; }
        bool readonly() const { return m_readonly; }
        bool active() const { return m_active; }

    private:
        void setInnerNode(DOM::NodeImpl* n) { m_innerNode = n; }
cblu's avatar
cblu committed
309
        void setInnerNonSharedNode(DOM::NodeImpl* n) { m_innerNonSharedNode = n; }
310
311
312
        void setURLElement(DOM::NodeImpl* n) { m_innerURLElement = n; }

        DOM::NodeImpl* m_innerNode;
cblu's avatar
cblu committed
313
        DOM::NodeImpl* m_innerNonSharedNode;
314
315
316
317
318
319
320
321
        DOM::NodeImpl* m_innerURLElement;
        bool m_readonly;
        bool m_active;
    };

    virtual FindSelectionResult checkSelectionPoint( int _x, int _y, int _tx, int _ty,
                                                     DOM::NodeImpl*&, int & offset );
    virtual bool nodeAtPoint(NodeInfo& info, int x, int y, int tx, int ty);
kocienda's avatar
kocienda committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

    // set the style of the object.
    virtual void setStyle(RenderStyle *style);

    // returns the containing block level element for this element.
    RenderObject *containingBlock() const;

    // return just the width of the containing block
    virtual short containingBlockWidth() const;
    // return just the height of the containing block
    virtual int containingBlockHeight() const;

    // size of the content area (box size minus padding/border)
    virtual short contentWidth() const { return 0; }
    virtual int contentHeight() const { return 0; }

    // intrinsic extend of replaced elements. undefined otherwise
    virtual short intrinsicWidth() const { return 0; }
    virtual int intrinsicHeight() const { return 0; }

    // relative to parent node
    virtual void setPos( int /*xPos*/, int /*yPos*/ ) { }
    virtual void setWidth( int /*width*/ ) { }
    virtual void setHeight( int /*height*/ ) { }

    virtual int xPos() const { return 0; }
    virtual int yPos() const { return 0; }

    // calculate client position of box
    virtual bool absolutePosition(int &/*xPos*/, int &/*yPos*/, bool fixed = false);

    // width and height are without margins but include paddings and borders
    virtual short width() const { return 0; }
    virtual int height() const { return 0; }

357
358
    // IE extensions. Used to calculate offsetWidth/Height.  Overridden by inlines (render_flow) 
    // to return the remaining width on a given line (and the height of a single line). -dwh
359
360
361
    virtual short offsetWidth() const { return width(); }
    virtual int offsetHeight() const { return height(); }
    
362
363
364
365
366
    // IE exxtensions.  Also supported by Gecko.  We override in render flow to get the
    // left and top correct. -dwh
    virtual int offsetLeft() const;
    virtual int offsetTop() const;
    virtual RenderObject* offsetParent() const;
367
    
368
    // The following seven functions are used to implement collapsing margins.
369
370
371
372
373
    // All objects know their maximal positive and negative margins.  The
    // formula for computing a collapsed margin is |maxPosMargin|-|maxNegmargin|.
    // For a non-collapsing, e.g., a leaf element, this formula will simply return
    // the margin of the element.  Blocks override the maxTopMargin and maxBottomMargin
    // methods.
374
    virtual bool isSelfCollapsingBlock() const { return false; }
375
376
377
378
    virtual short collapsedMarginTop() const 
        { return maxTopMargin(true)-maxTopMargin(false); }
    virtual short collapsedMarginBottom() const 
        { return maxBottomMargin(true)-maxBottomMargin(false); }
379
380
    virtual bool isTopMarginQuirk() const { return false; }
    virtual bool isBottomMarginQuirk() const { return false; }
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
    virtual short maxTopMargin(bool positive) const {
        if (positive)
            if (marginTop() > 0)
                return marginTop();
            else
                return 0;
        else
            if (marginTop() < 0)
                return 0 - marginTop();
            else
                return 0;
    }
    virtual short maxBottomMargin(bool positive) const {
        if (positive)
            if (marginBottom() > 0)
                return marginBottom();
            else
                return 0;
        else
            if (marginBottom() < 0)
                return 0 - marginBottom();
            else
                return 0;
    }

kocienda's avatar
kocienda committed
406
407
408
409
410
    virtual short marginTop() const { return 0; }
    virtual short marginBottom() const { return 0; }
    virtual short marginLeft() const { return 0; }
    virtual short marginRight() const { return 0; }

411
412
413
414
415
416
417
    // Virtual since table cells override 
    virtual int paddingTop() const;
    virtual int paddingBottom() const;
    virtual int paddingLeft() const;
    virtual int paddingRight() const;
    virtual bool hasPadding() const { return style()->hasPadding(); }
    
418
419
420
421
    int borderTop() const { return style()->borderTopWidth(); }
    int borderBottom() const { return style()->borderBottomWidth(); }
    int borderLeft() const { return style()->borderLeftWidth(); }
    int borderRight() const { return style()->borderRightWidth(); }
kocienda's avatar
kocienda committed
422
423
424

    virtual short minWidth() const { return 0; }
    virtual short maxWidth() const { return 0; }
425
        
426
427
428
429
430
431
432
433
434
435
    RenderStyle* style() const { return m_style; }
    RenderStyle* style( bool firstLine ) const {
	RenderStyle *s = m_style;
	if( firstLine && hasFirstLine() ) {
	    RenderStyle *pseudoStyle  = style()->getPseudoStyle(RenderStyle::FIRST_LINE);
	    if ( pseudoStyle )
		s = pseudoStyle;
	}
	return s;
    }
kocienda's avatar
kocienda committed
436
437
438
439

    enum BorderSide {
        BSTop, BSBottom, BSLeft, BSRight
    };
440
441
    void drawBorder(QPainter *p, int x1, int y1, int x2, int y2, BorderSide s,
                    QColor c, const QColor& textcolor, EBorderStyle style,
kocienda's avatar
kocienda committed
442
443
444
445
                    int adjbw1, int adjbw2, bool invalidisInvert = false);

    virtual void setTable(RenderTable*) {};

446
    // force a complete repaint
kocienda's avatar
kocienda committed
447
448
449
450
451
452
    virtual void repaint() { if(m_parent) m_parent->repaint(); }
    virtual void repaintRectangle(int x, int y, int w, int h, bool f=false);

    virtual unsigned int length() const { return 1; }

    virtual bool isHidden() const { return isFloating() || isPositioned(); }
453

kocienda's avatar
kocienda committed
454
455
456
457
458
459
    // Special objects are objects that are neither really inline nor blocklevel
    bool isSpecial() const { return (isFloating() || isPositioned()); };
    virtual bool containsSpecial() { return false; }
    virtual bool hasOverhangingFloats() { return false; }

    // positioning of inline childs (bidi)
darin's avatar
darin committed
460
    virtual void position(int, int, int, int, int, bool, bool, int) {}
kocienda's avatar
kocienda committed
461
462
463
464
465
466
467
468
469
470
471
472
473

    enum SelectionState {
        SelectionNone,
        SelectionStart,
        SelectionInside,
        SelectionEnd,
        SelectionBoth
    };

    virtual SelectionState selectionState() const { return SelectionNone;}
    virtual void setSelectionState(SelectionState) {}

    virtual void cursorPos(int /*offset*/, int &/*_x*/, int &/*_y*/, int &/*height*/);
474

kocienda's avatar
kocienda committed
475
    virtual int lowestPosition() const {return 0;}
476

kocienda's avatar
kocienda committed
477
    virtual int rightmostPosition() const {return 0;}
478

kocienda's avatar
kocienda committed
479
    // recursively invalidate current layout
480
481
    // unused: void invalidateLayout();

kocienda's avatar
kocienda committed
482
483
484
    virtual void calcVerticalMargins() {}
    void removeFromSpecialObjects();

485
    virtual void detach(RenderArena* renderArena);
kocienda's avatar
kocienda committed
486

487
488
489
490
491
492
493
    const QFont &font(bool firstLine) const {
	return style( firstLine )->font();
    }

    const QFontMetrics &fontMetrics(bool firstLine) const {
	return style( firstLine )->fontMetrics();
    }
kocienda's avatar
kocienda committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
protected:
    virtual void selectionStartEnd(int& spos, int& epos);

    virtual void printBoxDecorations(QPainter* /*p*/, int /*_x*/, int /*_y*/,
                                     int /*_w*/, int /*_h*/, int /*_tx*/, int /*_ty*/) {}

    virtual QRect viewRect() const;
    void remove() {
        removeFromSpecialObjects();

        if ( parent() )
            //have parent, take care of the tree integrity
            parent()->removeChild(this);
    }

    void invalidateVerticalPositions();
    short getVerticalPosition( bool firstLine ) const;

darin's avatar
darin committed
512
513
    virtual void removeLeftoverAnonymousBoxes();
    
kocienda's avatar
kocienda committed
514
private:
515
516
    RenderStyle* m_style;
    DOM::NodeImpl* m_node;
kocienda's avatar
kocienda committed
517
518
519
    RenderObject *m_parent;
    RenderObject *m_previous;
    RenderObject *m_next;
520

kocienda's avatar
kocienda committed
521
522
    short m_verticalPosition;

523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
    bool m_layouted                  : 1;
    bool m_unused                   : 1;
    bool m_minMaxKnown               : 1;
    bool m_floating                  : 1;

    bool m_positioned                : 1;
    bool m_overhangingContents : 1;
    bool m_relPositioned             : 1;
    bool m_printSpecial              : 1; // if the box has something special to print (background, border, etc)

    bool m_isAnonymous               : 1;
    bool m_recalcMinMax 	     : 1;
    bool m_isText                    : 1;
    bool m_inline                    : 1;

    bool m_replaced                  : 1;
    bool m_mouseInside : 1;
    bool m_hasFirstLine              : 1;
    bool m_isSelectionBorder          : 1;
kocienda's avatar
kocienda committed
542

543
544
    // note: do not add unnecessary bitflags, we have 32 bit already!
    friend class RenderListItem;
kocienda's avatar
kocienda committed
545
546
547
548
549
550
551
552
553
554
555
556
557
    friend class RenderContainer;
    friend class RenderRoot;
};


enum VerticalPositionHint {
    PositionTop = -0x4000,
    PositionBottom = 0x4000,
    PositionUndefined = 0x3fff
};

}; //namespace
#endif