summaryrefslogtreecommitdiffstats
path: root/khtml/rendering/render_table.h
blob: 979b92187fe9fc78cd0cef60c78eb01119707480 (plain)
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
 * This file is part of the DOM implementation for KDE.
 *
 * Copyright (C) 1997 Martin Jones ([email protected])
 *           (C) 1997 Torben Weis ([email protected])
 *           (C) 1998 Waldo Bastian ([email protected])
 *           (C) 1999-2003 Lars Knoll ([email protected])
 *           (C) 1999 Antti Koivisto ([email protected])
 *           (C) 2003 Apple Computer, Inc.
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */
#ifndef RENDER_TABLE_H
#define RENDER_TABLE_H

#include <qcolor.h>
#include <qptrvector.h>

#include "rendering/render_box.h"
#include "rendering/render_block.h"
#include "rendering/render_style.h"

#include "misc/khtmllayout.h"

namespace DOM {
    class DOMString;
}

namespace khtml {

class RenderTable;
class RenderTableSection;
class RenderTableRow;
class RenderTableCell;
class RenderTableCol;
class TableLayout;

class RenderTable : public RenderBlock
{
public:

    RenderTable(DOM::NodeImpl* node);
    ~RenderTable();

    virtual const char *renderName() const { return "RenderTable"; }

    virtual void setStyle(RenderStyle *style);

    virtual bool isTable() const { return true; }

    int getColumnPos(int col) const
        { return columnPos[col]; }

    int borderHSpacing() const { return hspacing; }
    int borderVSpacing() const { return vspacing; }

    bool collapseBorders() const { return style()->borderCollapse(); }
    int borderLeft() const;
    int borderRight() const;
    int borderTop() const;
    int borderBottom() const;
    int paddingLeft() const { return collapseBorders() ? 0 : RenderBlock::paddingLeft(); }
    int paddingRight() const { return collapseBorders() ? 0 : RenderBlock::paddingRight(); }
    int paddingTop() const { return collapseBorders() ? 0 : RenderBlock::paddingTop(); }
    int paddingBottom() const { return collapseBorders() ? 0 : RenderBlock::paddingBottom(); }

    const QColor &bgColor() const { return style()->backgroundColor(); }

    uint cellPadding() const { return padding; }
    void setCellPadding( uint p ) { padding = p; }

    // overrides
    virtual void addChild(RenderObject *child, RenderObject *beforeChild = 0);
    virtual void paint( PaintInfo&, int tx, int ty);
    virtual void paintBoxDecorations(PaintInfo&, int _tx, int _ty);
    virtual void layout();
    virtual void calcMinMaxWidth();
    virtual void close();

    virtual short lineHeight(bool b) const;
    virtual short baselinePosition(bool b) const;

    virtual void setCellWidths( );

    virtual void calcWidth();

    virtual FindSelectionResult checkSelectionPoint( int _x, int _y, int _tx, int _ty,
                                                     DOM::NodeImpl*& node, int & offset,
						     SelPointState & );

#ifdef ENABLE_DUMP
    virtual void dump(QTextStream &stream, const QString &ind) const;
#endif
    struct ColumnStruct {
	enum {
	    WidthUndefined = 0xffff
	};
	ColumnStruct() {
	    span = 1;
	    width = WidthUndefined;
	}
	ushort span;
	ushort width; // the calculated position of the column
    };

    QMemArray<int> columnPos;
    QMemArray<ColumnStruct> columns;

    void splitColumn( int pos, int firstSpan );
    void appendColumn( int span );
    int numEffCols() const { return columns.size(); }
    int spanOfEffCol( int effCol ) const { return columns[effCol].span; }
    int colToEffCol( int col ) const {
	int c = 0;
	int i = 0;
	while ( c < col && i < (int)columns.size() ) {
	    c += columns[i].span;
	    i++;
	}
	return i;
    }
    int effColToCol( int effCol ) const {
	int c = 0;
	for ( int i = 0; i < effCol; i++ )
	    c += columns[i].span;
	return c;
    }

    int bordersPaddingAndSpacing() const {
	return borderLeft() + borderRight() +
               (collapseBorders() ? 0 : (paddingLeft() + paddingRight() + (numEffCols()+1) * borderHSpacing()));
     }

    RenderTableCol *colElement( int col );

    void setNeedSectionRecalc() { needSectionRecalc = true; }

    virtual RenderObject* removeChildNode(RenderObject* child);

    RenderTableSection* sectionAbove(const RenderTableSection*, bool skipEmptySections = false) const;
    RenderTableSection* sectionBelow(const RenderTableSection*, bool skipEmptySections = false) const;

    RenderTableCell* cellAbove(const RenderTableCell* cell) const;
    RenderTableCell* cellBelow(const RenderTableCell* cell) const;
    RenderTableCell* cellLeft(const RenderTableCell* cell) const;
    RenderTableCell* cellRight(const RenderTableCell* cell) const;

    CollapsedBorderValue* currentBorderStyle() { return m_currentBorder; }

    RenderTableSection *firstBodySection() const { return firstBody; }
    RenderFlow*         caption() const { return tCaption; }

protected:

    void recalcSections();

    friend class AutoTableLayout;
    friend class FixedTableLayout;

    RenderFlow         *tCaption;
    RenderTableSection *head;
    RenderTableSection *foot;
    RenderTableSection *firstBody;

    TableLayout *tableLayout;

    CollapsedBorderValue* m_currentBorder;

    bool has_col_elems		: 1;
    uint needSectionRecalc	: 1;
    uint padding		: 22;

    ushort hspacing;
    ushort vspacing;

    friend class TableSectionIterator;
};

// -------------------------------------------------------------------------

class RenderTableSection : public RenderBox
{
public:
    RenderTableSection(DOM::NodeImpl* node);
    ~RenderTableSection();
    virtual void detach();

    virtual void setStyle(RenderStyle *style);

    virtual const char *renderName() const { return "RenderTableSection"; }

    // overrides
    virtual void addChild(RenderObject *child, RenderObject *beforeChild = 0);
    virtual bool isTableSection() const { return true; }

    virtual short lineHeight(bool) const { return 0; }
    virtual void position(InlineBox*, int, int, bool) {}

    virtual short width() const;

    virtual FindSelectionResult checkSelectionPoint( int _x, int _y, int _tx, int _ty,
                                                     DOM::NodeImpl*& node, int & offset,
						     SelPointState & );

#ifdef ENABLE_DUMP
    virtual void dump(QTextStream &stream, const QString &ind) const;
#endif

    void addCell( RenderTableCell *cell, RenderTableRow *row );

    void setCellWidths();
    void calcRowHeight();
    int layoutRows( int height );

    RenderTable *table() const { return static_cast<RenderTable *>(parent()); }

    typedef QMemArray<RenderTableCell *> Row;
    struct RowStruct {
        Row *row;
        RenderTableRow* rowRenderer;
        int baseLine;
        Length height;
        bool needFlex;
    };

    RenderTableCell *&cellAt( int row,  int col ) {
	return (*(grid[row].row))[col];
    }
    RenderTableCell *cellAt( int row,  int col ) const {
	return (*(grid[row].row))[col];
    }

    virtual int lowestPosition(bool includeOverflowInterior, bool includeSelf) const;
    virtual int rightmostPosition(bool includeOverflowInterior, bool includeSelf) const;
    virtual int leftmostPosition(bool includeOverflowInterior, bool includeSelf) const;
    virtual int highestPosition(bool includeOverflowInterior, bool includeSelf) const;

    int borderLeft() const { return table()->collapseBorders() ? 0 : RenderBox::borderLeft(); }
    int borderRight() const { return table()->collapseBorders() ? 0 : RenderBox::borderRight(); }
    int borderTop() const { return table()->collapseBorders() ? 0 : RenderBox::borderTop(); }
    int borderBottom() const { return table()->collapseBorders() ? 0 : RenderBox::borderBottom(); }

    virtual void paint( PaintInfo& i, int tx, int ty);

    int numRows() const { return grid.size(); }
    int getBaseline(int row) {return grid[row].baseLine;}

    void setNeedCellRecalc() {
        needCellRecalc = true;
        table()->setNeedSectionRecalc();
    }

    virtual RenderObject* removeChildNode(RenderObject* child);

    virtual bool canClear(RenderObject *child, PageBreakLevel level);
    void addSpaceAt(int pos, int dy);

    virtual bool nodeAtPoint(NodeInfo& info, int x, int y, int tx, int ty, HitTestAction action, bool inside);

    // this gets a cell grid data structure. changing the number of
    // columns is done by the table
    QMemArray<RowStruct> grid;
    QMemArray<int> rowPos;

    signed short cRow;
    ushort cCol;
    bool needCellRecalc;

    void recalcCells();
protected:
    void ensureRows( int numRows );
    void clearGrid();
    bool emptyRow(int rowNum);
    bool flexCellChildren(RenderObject* p) const;


    friend class TableSectionIterator;
};

// -------------------------------------------------------------------------

class RenderTableRow : public RenderContainer
{
public:
    RenderTableRow(DOM::NodeImpl* node);

    virtual void detach();

    virtual void setStyle( RenderStyle* );
    virtual const char *renderName() const { return "RenderTableRow"; }
    virtual bool isTableRow() const { return true; }
    virtual void addChild(RenderObject *child, RenderObject *beforeChild = 0);

    virtual short offsetWidth() const;
    virtual int offsetHeight() const;
    virtual int offsetLeft() const;
    virtual int offsetTop() const;

    virtual short lineHeight( bool ) const { return 0; }
    virtual void position(InlineBox*, int, int, bool) {}

    virtual bool nodeAtPoint(NodeInfo& info, int x, int y, int tx, int ty, HitTestAction action, bool inside);

    virtual void layout();

    virtual RenderObject* removeChildNode(RenderObject* child);

    // The only time rows get a layer is when they have transparency.
    virtual bool requiresLayer() const { return /* style()->opacity() < 1.0f; */ false ; }
    virtual void paint(PaintInfo& i, int tx, int ty);

    void paintRow( PaintInfo& i, int tx, int ty, int w, int h);

    RenderTable *table() const { return static_cast<RenderTable *>(parent()->parent()); }
    RenderTableSection *section() const { return static_cast<RenderTableSection *>(parent()); }
};

// -------------------------------------------------------------------------

class RenderTableCell : public RenderBlock
{
public:
    RenderTableCell(DOM::NodeImpl* node);

    virtual void layout();
    virtual void detach();

    virtual const char *renderName() const { return "RenderTableCell"; }
    virtual bool isTableCell() const { return true; }

    // ### FIX these two...
    long cellIndex() const { return 0; }
    void setCellIndex( long ) { }

    unsigned short colSpan() const { return cSpan; }
    void setColSpan( unsigned short c ) { cSpan = c; }

    unsigned short rowSpan() const { return rSpan; }
    void setRowSpan( unsigned short r ) { rSpan = r; }

    int col() const { return _col; }
    void setCol(int col) { _col = col; }
    int row() const { return _row; }
    void setRow(int r) { _row = r; }

    Length styleOrColWidth();

    // overrides
    virtual void calcMinMaxWidth();
    virtual void calcWidth();
    virtual void setWidth( int width );
    virtual void setStyle( RenderStyle *style );
    virtual bool requiresLayer() const;

    int borderLeft() const;
    int borderRight() const;
    int borderTop() const;
    int borderBottom() const;

    CollapsedBorderValue collapsedLeftBorder() const;
    CollapsedBorderValue collapsedRightBorder() const;
    CollapsedBorderValue collapsedTopBorder() const;
    CollapsedBorderValue collapsedBottomBorder() const;
    virtual void collectBorders(QValueList<CollapsedBorderValue>& borderStyles);

    virtual void updateFromElement();

    void setCellTopExtra(int p) { _topExtra = p; }
    void setCellBottomExtra(int p) { _bottomExtra = p; }
    int cellTopExtra() const { return _topExtra; }
    int cellBottomExtra() const { return _bottomExtra; }

    int pageTopAfter(int x) const;

    virtual void paint( PaintInfo& i, int tx, int ty);

    void paintCollapsedBorder(QPainter* p, int x, int y, int w, int h);
    void paintBackgroundsBehindCell(PaintInfo& i, int _tx, int _ty, RenderObject* backgroundObject);

    virtual void close();

    // lie position to outside observers
    virtual int yPos() const { return m_y + _topExtra; }

    virtual void repaintRectangle(int x, int y, int w, int h, Priority p=NormalPriority, bool f=false);
    virtual bool absolutePosition(int &xPos, int &yPos, bool f = false) const;

    virtual short baselinePosition( bool = false ) const;

    virtual bool nodeAtPoint(NodeInfo& info, int _x, int _y, int _tx, int _ty, HitTestAction hitTestAction, bool inside);

    RenderTable *table() const { return static_cast<RenderTable *>(parent()->parent()->parent()); }
    RenderTableSection *section() const { return static_cast<RenderTableSection *>(parent()->parent()); }

#ifdef ENABLE_DUMP
    virtual void dump(QTextStream &stream, const QString &ind) const;
#endif

    bool widthChanged() {
	bool retval = m_widthChanged;
	m_widthChanged = false;
	return retval;
    }

    int cellPercentageHeight() const
	{ return m_percentageHeight; }
    void setCellPercentageHeight(int h)
	{ m_percentageHeight = h; }
    bool hasFlexedAnonymous() const
        { return m_hasFlexedAnonymous; }
    void setHasFlexedAnonymous(bool b=true)
        { m_hasFlexedAnonymous = b; }

protected:
    virtual void paintBoxDecorations(PaintInfo& p, int _tx, int _ty);
    virtual int borderTopExtra() const { return _topExtra; }
    virtual int borderBottomExtra() const { return _bottomExtra; }

    short _row;
    short _col;
    ushort rSpan;
    ushort cSpan;
    int _topExtra;
    signed int _bottomExtra : 30;
    bool m_widthChanged : 1;
    bool m_hasFlexedAnonymous : 1;
    int m_percentageHeight;
};


// -------------------------------------------------------------------------

class RenderTableCol : public RenderContainer
{
public:
    RenderTableCol(DOM::NodeImpl* node);

    virtual const char *renderName() const { return "RenderTableCol"; }

    virtual bool isTableCol() const { return true; }

    virtual short lineHeight( bool ) const { return 0; }
    virtual void position(InlineBox*, int, int, bool) {}
    virtual void layout() {}
    virtual bool requiresLayer() const { return false; }

    virtual void updateFromElement();

#ifdef ENABLE_DUMP
    virtual void dump(QTextStream &stream, const QString& ind) const;
#endif

    int span() const { return m_span; }
    void setSpan( int s ) { m_span = s; }

private:
    int m_span;
};

// -------------------------------------------------------------------------

/** This class provides an iterator to iterate through the sections of a
 * render table in their visual order.
 *
 * In HTML, sections are specified in the order of THEAD, TFOOT, and TBODY.
 * Visually, TBODY sections appear between THEAD and TFOOT, which this iterator
 * takes into regard.
 * @author Leo Savernik
 * @internal
 * @since 3.2
 */
class TableSectionIterator {
public:

  /**
   * Initializes a new iterator
   * @param table table whose sections to iterate
   * @param fromEnd @p true, begin with last section, @p false, begin with
   *	first section.
   */
  TableSectionIterator(RenderTable *table, bool fromEnd = false);

  /**
   * Initializes a new iterator
   * @param start table section to start with.
   */
  TableSectionIterator(RenderTableSection *start) : sec(start) {}

  /**
   * Uninitialized iterator.
   */
  TableSectionIterator() {}

  /** Returns the current section, or @p 0 if the end has been reached.
   */
  RenderTableSection *operator *() const { return sec; }

  /** Moves to the next section in visual order. */
  TableSectionIterator &operator ++();

  /** Moves to the previous section in visual order. */
  TableSectionIterator &operator --();

private:
  RenderTableSection *sec;
};

}
#endif