forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsTableRowGroupFrame.cpp
1980 lines (1763 loc) · 75.8 KB
/
nsTableRowGroupFrame.cpp
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCOMPtr.h"
#include "nsTableRowGroupFrame.h"
#include "nsTableRowFrame.h"
#include "nsTableFrame.h"
#include "nsTableCellFrame.h"
#include "nsPresContext.h"
#include "nsStyleContext.h"
#include "nsStyleConsts.h"
#include "nsIContent.h"
#include "nsGkAtoms.h"
#include "nsIPresShell.h"
#include "nsCSSRendering.h"
#include "nsHTMLParts.h"
#include "nsCSSFrameConstructor.h"
#include "nsDisplayList.h"
#include "nsCellMap.h"//table cell navigation
#include <algorithm>
using namespace mozilla;
using namespace mozilla::layout;
nsTableRowGroupFrame::nsTableRowGroupFrame(nsStyleContext* aContext):
nsContainerFrame(aContext)
{
SetRepeatable(false);
}
nsTableRowGroupFrame::~nsTableRowGroupFrame()
{
}
void
nsTableRowGroupFrame::DestroyFrom(nsIFrame* aDestructRoot)
{
if (HasAnyStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN)) {
nsTableFrame::UnregisterPositionedTablePart(this, aDestructRoot);
}
nsContainerFrame::DestroyFrom(aDestructRoot);
}
NS_QUERYFRAME_HEAD(nsTableRowGroupFrame)
NS_QUERYFRAME_ENTRY(nsTableRowGroupFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
int32_t
nsTableRowGroupFrame::GetRowCount()
{
#ifdef DEBUG
for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) {
NS_ASSERTION(e.get()->StyleDisplay()->mDisplay ==
NS_STYLE_DISPLAY_TABLE_ROW,
"Unexpected display");
NS_ASSERTION(e.get()->GetType() == nsGkAtoms::tableRowFrame,
"Unexpected frame type");
}
#endif
return mFrames.GetLength();
}
int32_t nsTableRowGroupFrame::GetStartRowIndex()
{
int32_t result = -1;
if (mFrames.NotEmpty()) {
NS_ASSERTION(mFrames.FirstChild()->GetType() == nsGkAtoms::tableRowFrame,
"Unexpected frame type");
result = static_cast<nsTableRowFrame*>(mFrames.FirstChild())->GetRowIndex();
}
// if the row group doesn't have any children, get it the hard way
if (-1 == result) {
return GetTableFrame()->GetStartRowIndex(this);
}
return result;
}
void nsTableRowGroupFrame::AdjustRowIndices(int32_t aRowIndex,
int32_t anAdjustment)
{
for (nsIFrame* rowFrame : mFrames) {
if (NS_STYLE_DISPLAY_TABLE_ROW==rowFrame->StyleDisplay()->mDisplay) {
int32_t index = ((nsTableRowFrame*)rowFrame)->GetRowIndex();
if (index >= aRowIndex)
((nsTableRowFrame *)rowFrame)->SetRowIndex(index+anAdjustment);
}
}
}
nsresult
nsTableRowGroupFrame::InitRepeatedFrame(nsPresContext* aPresContext,
nsTableRowGroupFrame* aHeaderFooterFrame)
{
nsTableRowFrame* copyRowFrame = GetFirstRow();
nsTableRowFrame* originalRowFrame = aHeaderFooterFrame->GetFirstRow();
AddStateBits(NS_REPEATED_ROW_OR_ROWGROUP);
while (copyRowFrame && originalRowFrame) {
copyRowFrame->AddStateBits(NS_REPEATED_ROW_OR_ROWGROUP);
int rowIndex = originalRowFrame->GetRowIndex();
copyRowFrame->SetRowIndex(rowIndex);
// For each table cell frame set its column index
nsTableCellFrame* originalCellFrame = originalRowFrame->GetFirstCell();
nsTableCellFrame* copyCellFrame = copyRowFrame->GetFirstCell();
while (copyCellFrame && originalCellFrame) {
NS_ASSERTION(originalCellFrame->GetContent() == copyCellFrame->GetContent(),
"cell frames have different content");
int32_t colIndex;
originalCellFrame->GetColIndex(colIndex);
copyCellFrame->SetColIndex(colIndex);
// Move to the next cell frame
copyCellFrame = copyCellFrame->GetNextCell();
originalCellFrame = originalCellFrame->GetNextCell();
}
// Move to the next row frame
originalRowFrame = originalRowFrame->GetNextRow();
copyRowFrame = copyRowFrame->GetNextRow();
}
return NS_OK;
}
/**
* We need a custom display item for table row backgrounds. This is only used
* when the table row is the root of a stacking context (e.g., has 'opacity').
* Table row backgrounds can extend beyond the row frame bounds, when
* the row contains row-spanning cells.
*/
class nsDisplayTableRowGroupBackground : public nsDisplayTableItem {
public:
nsDisplayTableRowGroupBackground(nsDisplayListBuilder* aBuilder,
nsTableRowGroupFrame* aFrame) :
nsDisplayTableItem(aBuilder, aFrame) {
MOZ_COUNT_CTOR(nsDisplayTableRowGroupBackground);
}
#ifdef NS_BUILD_REFCNT_LOGGING
virtual ~nsDisplayTableRowGroupBackground() {
MOZ_COUNT_DTOR(nsDisplayTableRowGroupBackground);
}
#endif
virtual void Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx) override;
NS_DISPLAY_DECL_NAME("TableRowGroupBackground", TYPE_TABLE_ROW_GROUP_BACKGROUND)
};
void
nsDisplayTableRowGroupBackground::Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx)
{
auto rgFrame = static_cast<nsTableRowGroupFrame*>(mFrame);
TableBackgroundPainter painter(rgFrame->GetTableFrame(),
TableBackgroundPainter::eOrigin_TableRowGroup,
mFrame->PresContext(), *aCtx,
mVisibleRect, ToReferenceFrame(),
aBuilder->GetBackgroundPaintFlags());
DrawResult result = painter.PaintRowGroup(rgFrame);
nsDisplayTableItemGeometry::UpdateDrawResult(this, result);
}
// Handle the child-traversal part of DisplayGenericTablePart
static void
DisplayRows(nsDisplayListBuilder* aBuilder, nsFrame* aFrame,
const nsRect& aDirtyRect, const nsDisplayListSet& aLists)
{
nscoord overflowAbove;
nsTableRowGroupFrame* f = static_cast<nsTableRowGroupFrame*>(aFrame);
// Don't try to use the row cursor if we have to descend into placeholders;
// we might have rows containing placeholders, where the row's overflow
// area doesn't intersect the dirty rect but we need to descend into the row
// to see out of flows.
// Note that we really want to check ShouldDescendIntoFrame for all
// the rows in |f|, but that's exactly what we're trying to avoid, so we
// approximate it by checking it for |f|: if it's true for any row
// in |f| then it's true for |f| itself.
nsIFrame* kid = aBuilder->ShouldDescendIntoFrame(f) ?
nullptr : f->GetFirstRowContaining(aDirtyRect.y, &overflowAbove);
if (kid) {
// have a cursor, use it
while (kid) {
if (kid->GetRect().y - overflowAbove >= aDirtyRect.YMost() &&
kid->GetNormalRect().y - overflowAbove >= aDirtyRect.YMost())
break;
f->BuildDisplayListForChild(aBuilder, kid, aDirtyRect, aLists);
kid = kid->GetNextSibling();
}
return;
}
// No cursor. Traverse children the hard way and build a cursor while we're at it
nsTableRowGroupFrame::FrameCursorData* cursor = f->SetupRowCursor();
kid = f->GetFirstPrincipalChild();
while (kid) {
f->BuildDisplayListForChild(aBuilder, kid, aDirtyRect, aLists);
if (cursor) {
if (!cursor->AppendFrame(kid)) {
f->ClearRowCursor();
return;
}
}
kid = kid->GetNextSibling();
}
if (cursor) {
cursor->FinishBuildingCursor();
}
}
void
nsTableRowGroupFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
nsDisplayTableItem* item = nullptr;
if (IsVisibleInSelection(aBuilder)) {
bool isRoot = aBuilder->IsAtRootOfPseudoStackingContext();
if (isRoot) {
// This background is created regardless of whether this frame is
// visible or not. Visibility decisions are delegated to the
// table background painter.
item = new (aBuilder) nsDisplayTableRowGroupBackground(aBuilder, this);
aLists.BorderBackground()->AppendNewToTop(item);
}
}
nsTableFrame::DisplayGenericTablePart(aBuilder, this, aDirtyRect,
aLists, item, DisplayRows);
}
nsIFrame::LogicalSides
nsTableRowGroupFrame::GetLogicalSkipSides(const nsHTMLReflowState* aReflowState) const
{
if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
NS_STYLE_BOX_DECORATION_BREAK_CLONE)) {
return LogicalSides();
}
LogicalSides skip;
if (nullptr != GetPrevInFlow()) {
skip |= eLogicalSideBitsBStart;
}
if (nullptr != GetNextInFlow()) {
skip |= eLogicalSideBitsBEnd;
}
return skip;
}
// Position and size aKidFrame and update our reflow state.
void
nsTableRowGroupFrame::PlaceChild(nsPresContext* aPresContext,
nsRowGroupReflowState& aReflowState,
nsIFrame* aKidFrame,
WritingMode aWM,
const LogicalPoint& aKidPosition,
const nsSize& aContainerSize,
nsHTMLReflowMetrics& aDesiredSize,
const nsRect& aOriginalKidRect,
const nsRect& aOriginalKidVisualOverflow)
{
bool isFirstReflow = aKidFrame->HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
// Place and size the child
FinishReflowChild(aKidFrame, aPresContext, aDesiredSize, nullptr,
aWM, aKidPosition, aContainerSize, 0);
nsTableFrame::InvalidateTableFrame(aKidFrame, aOriginalKidRect,
aOriginalKidVisualOverflow, isFirstReflow);
// Adjust the running block-offset
aReflowState.bCoord += aDesiredSize.BSize(aWM);
// If our block-size is constrained then update the available bsize
if (NS_UNCONSTRAINEDSIZE != aReflowState.availSize.BSize(aWM)) {
aReflowState.availSize.BSize(aWM) -= aDesiredSize.BSize(aWM);
}
}
void
nsTableRowGroupFrame::InitChildReflowState(nsPresContext& aPresContext,
bool aBorderCollapse,
nsHTMLReflowState& aReflowState)
{
nsMargin collapseBorder;
nsMargin padding(0,0,0,0);
nsMargin* pCollapseBorder = nullptr;
if (aBorderCollapse) {
nsTableRowFrame *rowFrame = do_QueryFrame(aReflowState.frame);
if (rowFrame) {
WritingMode wm = GetWritingMode();
LogicalMargin border = rowFrame->GetBCBorderWidth(wm);
collapseBorder = border.GetPhysicalMargin(wm);
pCollapseBorder = &collapseBorder;
}
}
aReflowState.Init(&aPresContext, nullptr, pCollapseBorder, &padding);
}
static void
CacheRowBSizesForPrinting(nsPresContext* aPresContext,
nsTableRowFrame* aFirstRow,
WritingMode aWM)
{
for (nsTableRowFrame* row = aFirstRow; row; row = row->GetNextRow()) {
if (!row->GetPrevInFlow()) {
row->SetHasUnpaginatedBSize(true);
row->SetUnpaginatedBSize(aPresContext, row->BSize(aWM));
}
}
}
void
nsTableRowGroupFrame::ReflowChildren(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
nsRowGroupReflowState& aReflowState,
nsReflowStatus& aStatus,
bool* aPageBreakBeforeEnd)
{
if (aPageBreakBeforeEnd) {
*aPageBreakBeforeEnd = false;
}
WritingMode wm = aReflowState.reflowState.GetWritingMode();
nsTableFrame* tableFrame = GetTableFrame();
const bool borderCollapse = tableFrame->IsBorderCollapse();
// XXXldb Should we really be checking IsPaginated(),
// or should we *only* check available block-size?
// (Think about multi-column layout!)
bool isPaginated = aPresContext->IsPaginated() &&
NS_UNCONSTRAINEDSIZE != aReflowState.availSize.BSize(wm);
bool haveRow = false;
bool reflowAllKids = aReflowState.reflowState.ShouldReflowAllKids() ||
tableFrame->IsGeometryDirty();
// in vertical-rl mode, we always need the row bsizes in order to
// get the necessary containerSize for placing our kids
bool needToCalcRowBSizes = reflowAllKids || wm.IsVerticalRL();
nsSize containerSize =
aReflowState.reflowState.ComputedSizeAsContainerIfConstrained();
nsIFrame *prevKidFrame = nullptr;
for (nsIFrame* kidFrame = mFrames.FirstChild(); kidFrame;
prevKidFrame = kidFrame, kidFrame = kidFrame->GetNextSibling()) {
nsTableRowFrame *rowFrame = do_QueryFrame(kidFrame);
if (!rowFrame) {
// XXXldb nsCSSFrameConstructor needs to enforce this!
NS_NOTREACHED("yikes, a non-row child");
continue;
}
nscoord cellSpacingB = tableFrame->GetRowSpacing(rowFrame->GetRowIndex());
haveRow = true;
// Reflow the row frame
if (reflowAllKids ||
NS_SUBTREE_DIRTY(kidFrame) ||
(aReflowState.reflowState.mFlags.mSpecialBSizeReflow &&
(isPaginated ||
kidFrame->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)))) {
LogicalRect oldKidRect = kidFrame->GetLogicalRect(wm, containerSize);
nsRect oldKidVisualOverflow = kidFrame->GetVisualOverflowRect();
// XXXldb We used to only pass aDesiredSize.mFlags through for the
// incremental reflow codepath.
nsHTMLReflowMetrics desiredSize(aReflowState.reflowState,
aDesiredSize.mFlags);
desiredSize.ClearSize();
// Reflow the child into the available space, giving it as much bsize as
// it wants. We'll deal with splitting later after we've computed the row
// bsizes, taking into account cells with row spans...
LogicalSize kidAvailSize = aReflowState.availSize;
kidAvailSize.BSize(wm) = NS_UNCONSTRAINEDSIZE;
nsHTMLReflowState kidReflowState(aPresContext, aReflowState.reflowState,
kidFrame, kidAvailSize,
nullptr,
nsHTMLReflowState::CALLER_WILL_INIT);
InitChildReflowState(*aPresContext, borderCollapse, kidReflowState);
// This can indicate that columns were resized.
if (aReflowState.reflowState.IsIResize()) {
kidReflowState.SetIResize(true);
}
NS_ASSERTION(kidFrame == mFrames.FirstChild() || prevKidFrame,
"If we're not on the first frame, we should have a "
"previous sibling...");
// If prev row has nonzero YMost, then we can't be at the top of the page
if (prevKidFrame && prevKidFrame->GetNormalRect().YMost() > 0) {
kidReflowState.mFlags.mIsTopOfPage = false;
}
LogicalPoint kidPosition(wm, 0, aReflowState.bCoord);
ReflowChild(kidFrame, aPresContext, desiredSize, kidReflowState,
wm, kidPosition, containerSize, 0, aStatus);
kidReflowState.ApplyRelativePositioning(&kidPosition, containerSize);
// Place the child
PlaceChild(aPresContext, aReflowState, kidFrame,
wm, kidPosition, containerSize,
desiredSize, oldKidRect.GetPhysicalRect(wm, containerSize),
oldKidVisualOverflow);
aReflowState.bCoord += cellSpacingB;
if (!reflowAllKids) {
if (IsSimpleRowFrame(aReflowState.tableFrame, rowFrame)) {
// Inform the row of its new bsize.
rowFrame->DidResize();
// the overflow area may have changed inflate the overflow area
const nsStylePosition *stylePos = StylePosition();
nsStyleUnit unit = stylePos->BSize(wm).GetUnit();
if (aReflowState.tableFrame->IsAutoBSize(wm) &&
unit != eStyleUnit_Coord) {
// Because other cells in the row may need to be aligned
// differently, repaint the entire row
InvalidateFrame();
} else if (oldKidRect.BSize(wm) != desiredSize.BSize(wm)) {
needToCalcRowBSizes = true;
}
} else {
needToCalcRowBSizes = true;
}
}
if (isPaginated && aPageBreakBeforeEnd && !*aPageBreakBeforeEnd) {
nsTableRowFrame* nextRow = rowFrame->GetNextRow();
if (nextRow) {
*aPageBreakBeforeEnd = nsTableFrame::PageBreakAfter(kidFrame, nextRow);
}
}
} else {
SlideChild(aReflowState, kidFrame);
// Adjust the running b-offset so we know where the next row should be placed
nscoord bSize = kidFrame->BSize(wm) + cellSpacingB;
aReflowState.bCoord += bSize;
if (NS_UNCONSTRAINEDSIZE != aReflowState.availSize.BSize(wm)) {
aReflowState.availSize.BSize(wm) -= bSize;
}
}
ConsiderChildOverflow(aDesiredSize.mOverflowAreas, kidFrame);
}
if (haveRow) {
aReflowState.bCoord -= tableFrame->GetRowSpacing(GetStartRowIndex() +
GetRowCount());
}
// Return our desired rect
aDesiredSize.ISize(wm) = aReflowState.reflowState.AvailableISize();
aDesiredSize.BSize(wm) = aReflowState.bCoord;
if (aReflowState.reflowState.mFlags.mSpecialBSizeReflow) {
DidResizeRows(aDesiredSize);
if (isPaginated) {
CacheRowBSizesForPrinting(aPresContext, GetFirstRow(), wm);
}
}
else if (needToCalcRowBSizes) {
CalculateRowBSizes(aPresContext, aDesiredSize, aReflowState.reflowState);
if (!reflowAllKids) {
InvalidateFrame();
}
}
}
nsTableRowFrame*
nsTableRowGroupFrame::GetFirstRow()
{
for (nsIFrame* childFrame : mFrames) {
nsTableRowFrame *rowFrame = do_QueryFrame(childFrame);
if (rowFrame) {
return rowFrame;
}
}
return nullptr;
}
struct RowInfo {
RowInfo() { bSize = pctBSize = hasStyleBSize = hasPctBSize = isSpecial = 0; }
unsigned bSize; // content bsize or fixed bsize, excluding pct bsize
unsigned pctBSize:29; // pct bsize
unsigned hasStyleBSize:1;
unsigned hasPctBSize:1;
unsigned isSpecial:1; // there is no cell originating in the row with rowspan=1 and there are at
// least 2 cells spanning the row and there is no style bsize on the row
};
static void
UpdateBSizes(RowInfo& aRowInfo,
nscoord aAdditionalBSize,
nscoord& aTotal,
nscoord& aUnconstrainedTotal)
{
aRowInfo.bSize += aAdditionalBSize;
aTotal += aAdditionalBSize;
if (!aRowInfo.hasStyleBSize) {
aUnconstrainedTotal += aAdditionalBSize;
}
}
void
nsTableRowGroupFrame::DidResizeRows(nsHTMLReflowMetrics& aDesiredSize)
{
// Update the cells spanning rows with their new bsizes.
// This is the place where all of the cells in the row get set to the bsize
// of the row.
// Reset the overflow area.
aDesiredSize.mOverflowAreas.Clear();
for (nsTableRowFrame* rowFrame = GetFirstRow();
rowFrame; rowFrame = rowFrame->GetNextRow()) {
rowFrame->DidResize();
ConsiderChildOverflow(aDesiredSize.mOverflowAreas, rowFrame);
}
}
// This calculates the bsize of all the rows and takes into account
// style bsize on the row group, style bsizes on rows and cells, style bsizes on rowspans.
// Actual row bsizes will be adjusted later if the table has a style bsize.
// Even if rows don't change bsize, this method must be called to set the bsizes of each
// cell in the row to the bsize of its row.
void
nsTableRowGroupFrame::CalculateRowBSizes(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState)
{
nsTableFrame* tableFrame = GetTableFrame();
const bool isPaginated = aPresContext->IsPaginated();
int32_t numEffCols = tableFrame->GetEffectiveColCount();
int32_t startRowIndex = GetStartRowIndex();
// find the row corresponding to the row index we just found
nsTableRowFrame* startRowFrame = GetFirstRow();
if (!startRowFrame) {
return;
}
// The current row group block-size is the block-origin of the 1st row
// we are about to calculate a block-size for.
WritingMode wm = aReflowState.GetWritingMode();
nsSize containerSize; // actual value is unimportant as we're initially
// computing sizes, not physical positions
nscoord startRowGroupBSize =
startRowFrame->GetLogicalNormalPosition(wm, containerSize).B(wm);
int32_t numRows = GetRowCount() - (startRowFrame->GetRowIndex() - GetStartRowIndex());
// Collect the current bsize of each row.
if (numRows <= 0)
return;
nsTArray<RowInfo> rowInfo;
if (!rowInfo.AppendElements(numRows)) {
return;
}
bool hasRowSpanningCell = false;
nscoord bSizeOfRows = 0;
nscoord bSizeOfUnStyledRows = 0;
// Get the bsize of each row without considering rowspans. This will be the max of
// the largest desired bsize of each cell, the largest style bsize of each cell,
// the style bsize of the row.
nscoord pctBSizeBasis = GetBSizeBasis(aReflowState);
int32_t rowIndex; // the index in rowInfo, not among the rows in the row group
nsTableRowFrame* rowFrame;
for (rowFrame = startRowFrame, rowIndex = 0; rowFrame; rowFrame = rowFrame->GetNextRow(), rowIndex++) {
nscoord nonPctBSize = rowFrame->GetContentBSize();
if (isPaginated) {
nonPctBSize = std::max(nonPctBSize, rowFrame->BSize(wm));
}
if (!rowFrame->GetPrevInFlow()) {
if (rowFrame->HasPctBSize()) {
rowInfo[rowIndex].hasPctBSize = true;
rowInfo[rowIndex].pctBSize = rowFrame->GetInitialBSize(pctBSizeBasis);
}
rowInfo[rowIndex].hasStyleBSize = rowFrame->HasStyleBSize();
nonPctBSize = std::max(nonPctBSize, rowFrame->GetFixedBSize());
}
UpdateBSizes(rowInfo[rowIndex], nonPctBSize, bSizeOfRows, bSizeOfUnStyledRows);
if (!rowInfo[rowIndex].hasStyleBSize) {
if (isPaginated || tableFrame->HasMoreThanOneCell(rowIndex + startRowIndex)) {
rowInfo[rowIndex].isSpecial = true;
// iteratate the row's cell frames to see if any do not have rowspan > 1
nsTableCellFrame* cellFrame = rowFrame->GetFirstCell();
while (cellFrame) {
int32_t rowSpan = tableFrame->GetEffectiveRowSpan(rowIndex + startRowIndex, *cellFrame);
if (1 == rowSpan) {
rowInfo[rowIndex].isSpecial = false;
break;
}
cellFrame = cellFrame->GetNextCell();
}
}
}
// See if a cell spans into the row. If so we'll have to do the next step
if (!hasRowSpanningCell) {
if (tableFrame->RowIsSpannedInto(rowIndex + startRowIndex, numEffCols)) {
hasRowSpanningCell = true;
}
}
}
if (hasRowSpanningCell) {
// Get the bsize of cells with rowspans and allocate any extra space to the rows they span
// iteratate the child frames and process the row frames among them
for (rowFrame = startRowFrame, rowIndex = 0; rowFrame; rowFrame = rowFrame->GetNextRow(), rowIndex++) {
// See if the row has an originating cell with rowspan > 1. We cannot determine this for a row in a
// continued row group by calling RowHasSpanningCells, because the row's fif may not have any originating
// cells yet the row may have a continued cell which originates in it.
if (GetPrevInFlow() || tableFrame->RowHasSpanningCells(startRowIndex + rowIndex, numEffCols)) {
nsTableCellFrame* cellFrame = rowFrame->GetFirstCell();
// iteratate the row's cell frames
while (cellFrame) {
nscoord cellSpacingB = tableFrame->GetRowSpacing(startRowIndex + rowIndex);
int32_t rowSpan = tableFrame->GetEffectiveRowSpan(rowIndex + startRowIndex, *cellFrame);
if ((rowIndex + rowSpan) > numRows) {
// there might be rows pushed already to the nextInFlow
rowSpan = numRows - rowIndex;
}
if (rowSpan > 1) { // a cell with rowspan > 1, determine the bsize of the rows it spans
nscoord bsizeOfRowsSpanned = 0;
nscoord bsizeOfUnStyledRowsSpanned = 0;
nscoord numSpecialRowsSpanned = 0;
nscoord cellSpacingTotal = 0;
int32_t spanX;
for (spanX = 0; spanX < rowSpan; spanX++) {
bsizeOfRowsSpanned += rowInfo[rowIndex + spanX].bSize;
if (!rowInfo[rowIndex + spanX].hasStyleBSize) {
bsizeOfUnStyledRowsSpanned += rowInfo[rowIndex + spanX].bSize;
}
if (0 != spanX) {
cellSpacingTotal += cellSpacingB;
}
if (rowInfo[rowIndex + spanX].isSpecial) {
numSpecialRowsSpanned++;
}
}
nscoord bsizeOfAreaSpanned = bsizeOfRowsSpanned + cellSpacingTotal;
// get the bsize of the cell
LogicalSize cellFrameSize = cellFrame->GetLogicalSize(wm);
LogicalSize cellDesSize = cellFrame->GetDesiredSize();
rowFrame->CalculateCellActualBSize(cellFrame, cellDesSize.BSize(wm), wm);
cellFrameSize.BSize(wm) = cellDesSize.BSize(wm);
if (cellFrame->HasVerticalAlignBaseline()) {
// to ensure that a spanning cell with a long descender doesn't
// collide with the next row, we need to take into account the shift
// that will be done to align the cell on the baseline of the row.
cellFrameSize.BSize(wm) += rowFrame->GetMaxCellAscent() -
cellFrame->GetCellBaseline();
}
if (bsizeOfAreaSpanned < cellFrameSize.BSize(wm)) {
// the cell's bsize is larger than the available space of the rows it
// spans so distribute the excess bsize to the rows affected
nscoord extra = cellFrameSize.BSize(wm) - bsizeOfAreaSpanned;
nscoord extraUsed = 0;
if (0 == numSpecialRowsSpanned) {
//NS_ASSERTION(bsizeOfRowsSpanned > 0, "invalid row span situation");
bool haveUnStyledRowsSpanned = (bsizeOfUnStyledRowsSpanned > 0);
nscoord divisor = (haveUnStyledRowsSpanned)
? bsizeOfUnStyledRowsSpanned : bsizeOfRowsSpanned;
if (divisor > 0) {
for (spanX = rowSpan - 1; spanX >= 0; spanX--) {
if (!haveUnStyledRowsSpanned || !rowInfo[rowIndex + spanX].hasStyleBSize) {
// The amount of additional space each row gets is proportional to its bsize
float percent = ((float)rowInfo[rowIndex + spanX].bSize) / ((float)divisor);
// give rows their percentage, except for the first row which gets the remainder
nscoord extraForRow = (0 == spanX) ? extra - extraUsed
: NSToCoordRound(((float)(extra)) * percent);
extraForRow = std::min(extraForRow, extra - extraUsed);
// update the row bsize
UpdateBSizes(rowInfo[rowIndex + spanX], extraForRow, bSizeOfRows, bSizeOfUnStyledRows);
extraUsed += extraForRow;
if (extraUsed >= extra) {
NS_ASSERTION((extraUsed == extra), "invalid row bsize calculation");
break;
}
}
}
}
else {
// put everything in the last row
UpdateBSizes(rowInfo[rowIndex + rowSpan - 1], extra, bSizeOfRows, bSizeOfUnStyledRows);
}
}
else {
// give the extra to the special rows
nscoord numSpecialRowsAllocated = 0;
for (spanX = rowSpan - 1; spanX >= 0; spanX--) {
if (rowInfo[rowIndex + spanX].isSpecial) {
// The amount of additional space each degenerate row gets is proportional to the number of them
float percent = 1.0f / ((float)numSpecialRowsSpanned);
// give rows their percentage, except for the first row which gets the remainder
nscoord extraForRow = (numSpecialRowsSpanned - 1 == numSpecialRowsAllocated)
? extra - extraUsed
: NSToCoordRound(((float)(extra)) * percent);
extraForRow = std::min(extraForRow, extra - extraUsed);
// update the row bsize
UpdateBSizes(rowInfo[rowIndex + spanX], extraForRow, bSizeOfRows, bSizeOfUnStyledRows);
extraUsed += extraForRow;
if (extraUsed >= extra) {
NS_ASSERTION((extraUsed == extra), "invalid row bsize calculation");
break;
}
}
}
}
}
} // if (rowSpan > 1)
cellFrame = cellFrame->GetNextCell();
} // while (cellFrame)
} // if (tableFrame->RowHasSpanningCells(startRowIndex + rowIndex) {
} // while (rowFrame)
}
// pct bsize rows have already got their content bsizes.
// Give them their pct bsizes up to pctBSizeBasis
nscoord extra = pctBSizeBasis - bSizeOfRows;
for (rowFrame = startRowFrame, rowIndex = 0; rowFrame && (extra > 0);
rowFrame = rowFrame->GetNextRow(), rowIndex++) {
RowInfo& rInfo = rowInfo[rowIndex];
if (rInfo.hasPctBSize) {
nscoord rowExtra = (rInfo.pctBSize > rInfo.bSize)
? rInfo.pctBSize - rInfo.bSize: 0;
rowExtra = std::min(rowExtra, extra);
UpdateBSizes(rInfo, rowExtra, bSizeOfRows, bSizeOfUnStyledRows);
extra -= rowExtra;
}
}
bool styleBSizeAllocation = false;
nscoord rowGroupBSize = startRowGroupBSize + bSizeOfRows +
tableFrame->GetRowSpacing(0, numRows-1);
// if we have a style bsize, allocate the extra bsize to unconstrained rows
if ((aReflowState.ComputedBSize() > rowGroupBSize) &&
(NS_UNCONSTRAINEDSIZE != aReflowState.ComputedBSize())) {
nscoord extraComputedBSize = aReflowState.ComputedBSize() - rowGroupBSize;
nscoord extraUsed = 0;
bool haveUnStyledRows = (bSizeOfUnStyledRows > 0);
nscoord divisor = (haveUnStyledRows)
? bSizeOfUnStyledRows : bSizeOfRows;
if (divisor > 0) {
styleBSizeAllocation = true;
for (rowIndex = 0; rowIndex < numRows; rowIndex++) {
if (!haveUnStyledRows || !rowInfo[rowIndex].hasStyleBSize) {
// The amount of additional space each row gets is based on the
// percentage of space it occupies
float percent = ((float)rowInfo[rowIndex].bSize) / ((float)divisor);
// give rows their percentage, except for the last row which gets the remainder
nscoord extraForRow = (numRows - 1 == rowIndex)
? extraComputedBSize - extraUsed
: NSToCoordRound(((float)extraComputedBSize) * percent);
extraForRow = std::min(extraForRow, extraComputedBSize - extraUsed);
// update the row bsize
UpdateBSizes(rowInfo[rowIndex], extraForRow, bSizeOfRows, bSizeOfUnStyledRows);
extraUsed += extraForRow;
if (extraUsed >= extraComputedBSize) {
NS_ASSERTION((extraUsed == extraComputedBSize), "invalid row bsize calculation");
break;
}
}
}
}
rowGroupBSize = aReflowState.ComputedBSize();
}
if (wm.IsVertical()) {
// we need the correct containerSize below for block positioning in
// vertical-rl writing mode
containerSize.width = rowGroupBSize;
}
nscoord bOrigin = startRowGroupBSize;
// update the rows with their (potentially) new bsizes
for (rowFrame = startRowFrame, rowIndex = 0; rowFrame;
rowFrame = rowFrame->GetNextRow(), rowIndex++) {
nsRect rowBounds = rowFrame->GetRect();
LogicalSize rowBoundsSize(wm, rowBounds.Size());
nsRect rowVisualOverflow = rowFrame->GetVisualOverflowRect();
nscoord deltaB =
bOrigin - rowFrame->GetLogicalNormalPosition(wm, containerSize).B(wm);
nscoord rowBSize = (rowInfo[rowIndex].bSize > 0) ? rowInfo[rowIndex].bSize : 0;
if (deltaB != 0 || (rowBSize != rowBoundsSize.BSize(wm))) {
// Resize/move the row to its final size and position
if (deltaB != 0) {
rowFrame->InvalidateFrameSubtree();
}
rowFrame->MovePositionBy(wm, LogicalPoint(wm, 0, deltaB));
rowFrame->SetSize(LogicalSize(wm, rowBoundsSize.ISize(wm), rowBSize));
nsTableFrame::InvalidateTableFrame(rowFrame, rowBounds, rowVisualOverflow,
false);
if (deltaB != 0) {
nsTableFrame::RePositionViews(rowFrame);
// XXXbz we don't need to update our overflow area?
}
}
bOrigin += rowBSize + tableFrame->GetRowSpacing(startRowIndex + rowIndex);
}
if (isPaginated && styleBSizeAllocation) {
// since the row group has a style bsize, cache the row bsizes,
// so next in flows can honor them
CacheRowBSizesForPrinting(aPresContext, GetFirstRow(), wm);
}
DidResizeRows(aDesiredSize);
aDesiredSize.BSize(wm) = rowGroupBSize; // Adjust our desired size
}
nscoord
nsTableRowGroupFrame::CollapseRowGroupIfNecessary(nscoord aBTotalOffset,
nscoord aISize,
WritingMode aWM)
{
nsTableFrame* tableFrame = GetTableFrame();
nsSize containerSize = tableFrame->GetSize();
const nsStyleVisibility* groupVis = StyleVisibility();
bool collapseGroup = (NS_STYLE_VISIBILITY_COLLAPSE == groupVis->mVisible);
if (collapseGroup) {
tableFrame->SetNeedToCollapse(true);
}
nsOverflowAreas overflow;
nsTableRowFrame* rowFrame = GetFirstRow();
bool didCollapse = false;
nscoord bGroupOffset = 0;
while (rowFrame) {
bGroupOffset += rowFrame->CollapseRowIfNecessary(bGroupOffset,
aISize, collapseGroup,
didCollapse);
ConsiderChildOverflow(overflow, rowFrame);
rowFrame = rowFrame->GetNextRow();
}
LogicalRect groupRect = GetLogicalRect(aWM, containerSize);
nsRect oldGroupRect = GetRect();
nsRect oldGroupVisualOverflow = GetVisualOverflowRect();
groupRect.BSize(aWM) -= bGroupOffset;
if (didCollapse) {
// add back the cellspacing between rowgroups
groupRect.BSize(aWM) += tableFrame->GetRowSpacing(GetStartRowIndex() +
GetRowCount());
}
groupRect.BStart(aWM) -= aBTotalOffset;
groupRect.ISize(aWM) = aISize;
if (aBTotalOffset != 0) {
InvalidateFrameSubtree();
}
SetRect(aWM, groupRect, containerSize);
overflow.UnionAllWith(nsRect(0, 0, groupRect.Width(aWM),
groupRect.Height(aWM)));
FinishAndStoreOverflow(overflow, groupRect.Size(aWM).GetPhysicalSize(aWM));
nsTableFrame::RePositionViews(this);
nsTableFrame::InvalidateTableFrame(this, oldGroupRect, oldGroupVisualOverflow,
false);
return bGroupOffset;
}
// Move a child that was skipped during a reflow.
void
nsTableRowGroupFrame::SlideChild(nsRowGroupReflowState& aReflowState,
nsIFrame* aKidFrame)
{
// Move the frame if we need to.
WritingMode wm = aReflowState.reflowState.GetWritingMode();
const nsSize containerSize =
aReflowState.reflowState.ComputedSizeAsContainerIfConstrained();
LogicalPoint oldPosition =
aKidFrame->GetLogicalNormalPosition(wm, containerSize);
LogicalPoint newPosition = oldPosition;
newPosition.B(wm) = aReflowState.bCoord;
if (oldPosition.B(wm) != newPosition.B(wm)) {
aKidFrame->InvalidateFrameSubtree();
aReflowState.reflowState.ApplyRelativePositioning(&newPosition,
containerSize);
aKidFrame->SetPosition(wm, newPosition, containerSize);
nsTableFrame::RePositionViews(aKidFrame);
aKidFrame->InvalidateFrameSubtree();
}
}
// Create a continuing frame, add it to the child list, and then push it
// and the frames that follow
void
nsTableRowGroupFrame::CreateContinuingRowFrame(nsPresContext& aPresContext,
nsIFrame& aRowFrame,
nsIFrame** aContRowFrame)
{
// XXX what is the row index?
if (!aContRowFrame) {NS_ASSERTION(false, "bad call"); return;}
// create the continuing frame which will create continuing cell frames
*aContRowFrame = aPresContext.PresShell()->FrameConstructor()->
CreateContinuingFrame(&aPresContext, &aRowFrame, this);
// Add the continuing row frame to the child list
mFrames.InsertFrame(nullptr, &aRowFrame, *aContRowFrame);
// Push the continuing row frame and the frames that follow
PushChildren(*aContRowFrame, &aRowFrame);
}
// Reflow the cells with rowspan > 1 which originate between aFirstRow
// and end on or after aLastRow. aFirstTruncatedRow is the highest row on the
// page that contains a cell which cannot split on this page
void
nsTableRowGroupFrame::SplitSpanningCells(nsPresContext& aPresContext,
const nsHTMLReflowState& aReflowState,
nsTableFrame& aTable,
nsTableRowFrame& aFirstRow,
nsTableRowFrame& aLastRow,
bool aFirstRowIsTopOfPage,
nscoord aSpanningRowBEnd,
nsTableRowFrame*& aContRow,
nsTableRowFrame*& aFirstTruncatedRow,
nscoord& aDesiredBSize)
{
NS_ASSERTION(aSpanningRowBEnd >= 0, "Can't split negative bsizes");
aFirstTruncatedRow = nullptr;
aDesiredBSize = 0;
const bool borderCollapse = aTable.IsBorderCollapse();
int32_t lastRowIndex = aLastRow.GetRowIndex();
bool wasLast = false;
bool haveRowSpan = false;
// Iterate the rows between aFirstRow and aLastRow
for (nsTableRowFrame* row = &aFirstRow; !wasLast; row = row->GetNextRow()) {
wasLast = (row == &aLastRow);
int32_t rowIndex = row->GetRowIndex();
nsPoint rowPos = row->GetNormalPosition();
// Iterate the cells looking for those that have rowspan > 1
for (nsTableCellFrame* cell = row->GetFirstCell(); cell; cell = cell->GetNextCell()) {
int32_t rowSpan = aTable.GetEffectiveRowSpan(rowIndex, *cell);
// Only reflow rowspan > 1 cells which span aLastRow. Those which don't span aLastRow
// were reflowed correctly during the unconstrained bsize reflow.
if ((rowSpan > 1) && (rowIndex + rowSpan > lastRowIndex)) {
haveRowSpan = true;
nsReflowStatus status;
// Ask the row to reflow the cell to the bsize of all the rows it spans up through aLastRow
// cellAvailBSize is the space between the row group start and the end of the page
nscoord cellAvailBSize = aSpanningRowBEnd - rowPos.y;
NS_ASSERTION(cellAvailBSize >= 0, "No space for cell?");
bool isTopOfPage = (row == &aFirstRow) && aFirstRowIsTopOfPage;
nsRect rowRect = row->GetNormalRect();
nsSize rowAvailSize(aReflowState.AvailableWidth(),
std::max(aReflowState.AvailableHeight() - rowRect.y,
0));
// don't let the available height exceed what
// CalculateRowBSizes set for it
rowAvailSize.height = std::min(rowAvailSize.height, rowRect.height);
nsHTMLReflowState rowReflowState(&aPresContext, aReflowState, row,
LogicalSize(row->GetWritingMode(),
rowAvailSize),
nullptr,
nsHTMLReflowState::CALLER_WILL_INIT);
InitChildReflowState(aPresContext, borderCollapse, rowReflowState);
rowReflowState.mFlags.mIsTopOfPage = isTopOfPage; // set top of page
nscoord cellBSize = row->ReflowCellFrame(&aPresContext, rowReflowState,
isTopOfPage, cell,
cellAvailBSize, status);
aDesiredBSize = std::max(aDesiredBSize, rowPos.y + cellBSize);
if (NS_FRAME_IS_COMPLETE(status)) {
if (cellBSize > cellAvailBSize) {
aFirstTruncatedRow = row;
if ((row != &aFirstRow) || !aFirstRowIsTopOfPage) {
// return now, since we will be getting another reflow after either (1) row is
// moved to the next page or (2) the row group is moved to the next page
return;
}
}
}