forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsColumnSetFrame.cpp
1147 lines (991 loc) · 43.7 KB
/
nsColumnSetFrame.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/. */
/* rendering object for css3 multi-column layout */
#include "nsContainerFrame.h"
#include "nsIContent.h"
#include "nsIFrame.h"
#include "nsISupports.h"
#include "nsIAtom.h"
#include "nsPresContext.h"
#include "nsHTMLParts.h"
#include "nsGkAtoms.h"
#include "nsStyleConsts.h"
#include "nsCOMPtr.h"
#include "nsLayoutUtils.h"
#include "nsDisplayList.h"
#include "nsCSSRendering.h"
using namespace mozilla;
class nsColumnSetFrame : public nsContainerFrame {
public:
NS_DECL_FRAMEARENA_HELPERS
nsColumnSetFrame(nsStyleContext* aContext);
NS_IMETHOD SetInitialChildList(ChildListID aListID,
nsFrameList& aChildList);
NS_IMETHOD Reflow(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus);
NS_IMETHOD AppendFrames(ChildListID aListID,
nsFrameList& aFrameList);
NS_IMETHOD InsertFrames(ChildListID aListID,
nsIFrame* aPrevFrame,
nsFrameList& aFrameList);
NS_IMETHOD RemoveFrame(ChildListID aListID,
nsIFrame* aOldFrame);
virtual void DestroyFrom(nsIFrame* aDestructRoot);
virtual nscoord GetMinWidth(nsRenderingContext *aRenderingContext);
virtual nscoord GetPrefWidth(nsRenderingContext *aRenderingContext);
virtual nsIFrame* GetContentInsertionFrame() {
nsIFrame* frame = GetFirstPrincipalChild();
// if no children return nullptr
if (!frame)
return nullptr;
return frame->GetContentInsertionFrame();
}
virtual nsresult StealFrame(nsPresContext* aPresContext,
nsIFrame* aChild,
bool aForceNormal)
{ // nsColumnSetFrame keeps overflow containers in main child list
return nsContainerFrame::StealFrame(aPresContext, aChild, true);
}
NS_IMETHOD BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists);
virtual nsIAtom* GetType() const;
virtual void PaintColumnRule(nsRenderingContext* aCtx,
const nsRect& aDirtyRect,
const nsPoint& aPt);
#ifdef DEBUG
NS_IMETHOD GetFrameName(nsAString& aResult) const {
return MakeFrameName(NS_LITERAL_STRING("ColumnSet"), aResult);
}
#endif
protected:
nscoord mLastBalanceHeight;
nsReflowStatus mLastFrameStatus;
virtual int GetSkipSides() const;
/**
* These are the parameters that control the layout of columns.
*/
struct ReflowConfig {
int32_t mBalanceColCount;
nscoord mColWidth;
nscoord mExpectedWidthLeftOver;
nscoord mColGap;
nscoord mColMaxHeight;
};
/**
* Some data that is better calculated during reflow
*/
struct ColumnBalanceData {
// The maximum "content height" of any column
nscoord mMaxHeight;
// The sum of the "content heights" for all columns
nscoord mSumHeight;
// The "content height" of the last column
nscoord mLastHeight;
// The maximum "content height" of all columns that overflowed
// their available height
nscoord mMaxOverflowingHeight;
// Whether or not we should revert back to 'auto' setting for column-fill.
// This happens if we overflow our columns such that we no longer have
// enough room to keep balancing.
bool mShouldRevertToAuto;
void Reset() {
mMaxHeight = mSumHeight = mLastHeight = mMaxOverflowingHeight = 0;
mShouldRevertToAuto = false;
}
};
/**
* Similar to nsBlockFrame::DrainOverflowLines. Locate any columns not
* handled by our prev-in-flow, and any columns sitting on our own
* overflow list, and put them in our primary child list for reflowing.
*/
void DrainOverflowColumns();
/**
* The basic reflow strategy is to call this function repeatedly to
* obtain specific parameters that determine the layout of the
* columns. This function will compute those parameters from the CSS
* style. This function will also be responsible for implementing
* the state machine that controls column balancing.
*/
ReflowConfig ChooseColumnStrategy(const nsHTMLReflowState& aReflowState);
/**
* Reflow column children. Returns true iff the content that was reflowed
* fit into the mColMaxHeight.
*/
bool ReflowChildren(nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus,
const ReflowConfig& aConfig,
bool aLastColumnUnbounded,
nsCollapsingMargin* aCarriedOutBottomMargin,
ColumnBalanceData& aColData);
};
/**
* Tracking issues:
*
* XXX cursor movement around the top and bottom of colums seems to make the editor
* lose the caret.
*
* XXX should we support CSS columns applied to table elements?
*/
nsIFrame*
NS_NewColumnSetFrame(nsIPresShell* aPresShell, nsStyleContext* aContext, uint32_t aStateFlags)
{
nsColumnSetFrame* it = new (aPresShell) nsColumnSetFrame(aContext);
if (it) {
it->AddStateBits(aStateFlags | NS_BLOCK_MARGIN_ROOT);
}
return it;
}
NS_IMPL_FRAMEARENA_HELPERS(nsColumnSetFrame)
nsColumnSetFrame::nsColumnSetFrame(nsStyleContext* aContext)
: nsContainerFrame(aContext), mLastBalanceHeight(NS_INTRINSICSIZE),
mLastFrameStatus(NS_FRAME_COMPLETE)
{
}
void
nsColumnSetFrame::DestroyFrom(nsIFrame* aDestructRoot)
{
DestroyAbsoluteFrames(aDestructRoot);
nsContainerFrame::DestroyFrom(aDestructRoot);
}
nsIAtom*
nsColumnSetFrame::GetType() const
{
return nsGkAtoms::columnSetFrame;
}
static void
PaintColumnRule(nsIFrame* aFrame, nsRenderingContext* aCtx,
const nsRect& aDirtyRect, nsPoint aPt)
{
static_cast<nsColumnSetFrame*>(aFrame)->PaintColumnRule(aCtx, aDirtyRect, aPt);
}
void
nsColumnSetFrame::PaintColumnRule(nsRenderingContext* aCtx,
const nsRect& aDirtyRect,
const nsPoint& aPt)
{
nsIFrame* child = mFrames.FirstChild();
if (!child)
return; // no columns
nsIFrame* nextSibling = child->GetNextSibling();
if (!nextSibling)
return; // 1 column only - this means no gap to draw on
bool isRTL = GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
const nsStyleColumn* colStyle = GetStyleColumn();
uint8_t ruleStyle;
// Per spec, inset => ridge and outset => groove
if (colStyle->mColumnRuleStyle == NS_STYLE_BORDER_STYLE_INSET)
ruleStyle = NS_STYLE_BORDER_STYLE_RIDGE;
else if (colStyle->mColumnRuleStyle == NS_STYLE_BORDER_STYLE_OUTSET)
ruleStyle = NS_STYLE_BORDER_STYLE_GROOVE;
else
ruleStyle = colStyle->mColumnRuleStyle;
nsPresContext* presContext = PresContext();
nscoord ruleWidth = colStyle->GetComputedColumnRuleWidth();
if (!ruleWidth)
return;
nscolor ruleColor =
GetVisitedDependentColor(eCSSProperty__moz_column_rule_color);
// In order to re-use a large amount of code, we treat the column rule as a border.
// We create a new border style object and fill in all the details of the column rule as
// the left border. PaintBorder() does all the rendering for us, so we not
// only save an enormous amount of code but we'll support all the line styles that
// we support on borders!
nsStyleBorder border(presContext);
border.SetBorderWidth(NS_SIDE_LEFT, ruleWidth);
border.SetBorderStyle(NS_SIDE_LEFT, ruleStyle);
border.SetBorderColor(NS_SIDE_LEFT, ruleColor);
// Get our content rect as an absolute coordinate, not relative to
// our parent (which is what the X and Y normally is)
nsRect contentRect = GetContentRect() - GetRect().TopLeft() + aPt;
nsSize ruleSize(ruleWidth, contentRect.height);
while (nextSibling) {
// The frame tree goes RTL in RTL
nsIFrame* leftSibling = isRTL ? nextSibling : child;
nsIFrame* rightSibling = isRTL ? child : nextSibling;
// Each child frame's position coordinates is actually relative to this nsColumnSetFrame.
// linePt will be at the top-left edge to paint the line.
nsPoint edgeOfLeftSibling = leftSibling->GetRect().TopRight() + aPt;
nsPoint edgeOfRightSibling = rightSibling->GetRect().TopLeft() + aPt;
nsPoint linePt((edgeOfLeftSibling.x + edgeOfRightSibling.x - ruleWidth) / 2,
contentRect.y);
nsRect lineRect(linePt, ruleSize);
nsCSSRendering::PaintBorderWithStyleBorder(presContext, *aCtx, this,
aDirtyRect, lineRect, border, GetStyleContext(),
// Remember, we only have the "left" "border". Skip everything else
(1 << NS_SIDE_TOP | 1 << NS_SIDE_RIGHT | 1 << NS_SIDE_BOTTOM));
child = nextSibling;
nextSibling = nextSibling->GetNextSibling();
}
}
NS_IMETHODIMP
nsColumnSetFrame::SetInitialChildList(ChildListID aListID,
nsFrameList& aChildList)
{
NS_ASSERTION(aListID == kPrincipalList,
"Only default child list supported");
NS_ASSERTION(aChildList.OnlyChild(),
"initial child list must have exactly one child");
// Queue up the frames for the content frame
return nsContainerFrame::SetInitialChildList(kPrincipalList, aChildList);
}
static nscoord
GetAvailableContentWidth(const nsHTMLReflowState& aReflowState)
{
if (aReflowState.availableWidth == NS_INTRINSICSIZE) {
return NS_INTRINSICSIZE;
}
nscoord borderPaddingWidth =
aReflowState.mComputedBorderPadding.left +
aReflowState.mComputedBorderPadding.right;
return NS_MAX(0, aReflowState.availableWidth - borderPaddingWidth);
}
static nscoord
GetAvailableContentHeight(const nsHTMLReflowState& aReflowState)
{
if (aReflowState.availableHeight == NS_INTRINSICSIZE) {
return NS_INTRINSICSIZE;
}
nscoord borderPaddingHeight =
aReflowState.mComputedBorderPadding.top +
aReflowState.mComputedBorderPadding.bottom;
return NS_MAX(0, aReflowState.availableHeight - borderPaddingHeight);
}
static nscoord
GetColumnGap(nsColumnSetFrame* aFrame,
const nsStyleColumn* aColStyle)
{
if (eStyleUnit_Normal == aColStyle->mColumnGap.GetUnit())
return aFrame->GetStyleFont()->mFont.size;
if (eStyleUnit_Coord == aColStyle->mColumnGap.GetUnit()) {
nscoord colGap = aColStyle->mColumnGap.GetCoordValue();
NS_ASSERTION(colGap >= 0, "negative column gap");
return colGap;
}
NS_NOTREACHED("Unknown gap type");
return 0;
}
nsColumnSetFrame::ReflowConfig
nsColumnSetFrame::ChooseColumnStrategy(const nsHTMLReflowState& aReflowState)
{
const nsStyleColumn* colStyle = GetStyleColumn();
nscoord availContentWidth = GetAvailableContentWidth(aReflowState);
if (aReflowState.ComputedWidth() != NS_INTRINSICSIZE) {
availContentWidth = aReflowState.ComputedWidth();
}
nscoord colHeight = GetAvailableContentHeight(aReflowState);
if (aReflowState.ComputedHeight() != NS_INTRINSICSIZE) {
colHeight = aReflowState.ComputedHeight();
} else if (aReflowState.mComputedMaxHeight != NS_INTRINSICSIZE) {
colHeight = aReflowState.mComputedMaxHeight;
}
nscoord colGap = GetColumnGap(this, colStyle);
int32_t numColumns = colStyle->mColumnCount;
const uint32_t MAX_NESTED_COLUMN_BALANCING = 2;
uint32_t cnt = 1;
for (const nsHTMLReflowState* rs = aReflowState.parentReflowState; rs && cnt
< MAX_NESTED_COLUMN_BALANCING; rs = rs->parentReflowState) {
if (rs->mFlags.mIsColumnBalancing) {
++cnt;
}
}
if (cnt == MAX_NESTED_COLUMN_BALANCING) {
numColumns = 1;
}
nscoord colWidth;
if (colStyle->mColumnWidth.GetUnit() == eStyleUnit_Coord) {
colWidth = colStyle->mColumnWidth.GetCoordValue();
NS_ASSERTION(colWidth >= 0, "negative column width");
// Reduce column count if necessary to make columns fit in the
// available width. Compute max number of columns that fit in
// availContentWidth, satisfying colGap*(maxColumns - 1) +
// colWidth*maxColumns <= availContentWidth
if (availContentWidth != NS_INTRINSICSIZE && colGap + colWidth > 0
&& numColumns > 0) {
// This expression uses truncated rounding, which is what we
// want
int32_t maxColumns = (availContentWidth + colGap)/(colGap + colWidth);
numColumns = NS_MAX(1, NS_MIN(numColumns, maxColumns));
}
} else if (numColumns > 0 && availContentWidth != NS_INTRINSICSIZE) {
nscoord widthMinusGaps = availContentWidth - colGap*(numColumns - 1);
colWidth = widthMinusGaps/numColumns;
} else {
colWidth = NS_INTRINSICSIZE;
}
// Take care of the situation where there's only one column but it's
// still too wide
colWidth = NS_MAX(1, NS_MIN(colWidth, availContentWidth));
nscoord expectedWidthLeftOver = 0;
if (colWidth != NS_INTRINSICSIZE && availContentWidth != NS_INTRINSICSIZE) {
// distribute leftover space
// First, determine how many columns will be showing if the column
// count is auto
if (numColumns <= 0) {
// choose so that colGap*(nominalColumnCount - 1) +
// colWidth*nominalColumnCount is nearly availContentWidth
// make sure to round down
if (colGap + colWidth > 0) {
numColumns = (availContentWidth + colGap)/(colGap + colWidth);
}
if (numColumns <= 0) {
numColumns = 1;
}
}
// Compute extra space and divide it among the columns
nscoord extraSpace =
NS_MAX(0, availContentWidth - (colWidth*numColumns + colGap*(numColumns - 1)));
nscoord extraToColumns = extraSpace/numColumns;
colWidth += extraToColumns;
expectedWidthLeftOver = extraSpace - (extraToColumns*numColumns);
}
// If column-fill is set to 'balance', then we want to balance the columns.
if (colStyle->mColumnFill == NS_STYLE_COLUMN_FILL_BALANCE) {
// Balancing!
if (numColumns <= 0) {
// Hmm, auto column count, column width or available width is unknown,
// and balancing is required. Let's just use one column then.
numColumns = 1;
}
colHeight = NS_MIN(mLastBalanceHeight,
colHeight);
} else {
// This is the case when the column-fill property is set to 'auto'.
// No balancing, so don't limit the column count
numColumns = INT32_MAX;
}
#ifdef DEBUG_roc
printf("*** nsColumnSetFrame::ChooseColumnStrategy: numColumns=%d, colWidth=%d, expectedWidthLeftOver=%d, colHeight=%d, colGap=%d\n",
numColumns, colWidth, expectedWidthLeftOver, colHeight, colGap);
#endif
ReflowConfig config = { numColumns, colWidth, expectedWidthLeftOver, colGap, colHeight };
return config;
}
// XXX copied from nsBlockFrame, should this be moved to nsContainerFrame?
static void
PlaceFrameView(nsIFrame* aFrame)
{
if (aFrame->HasView())
nsContainerFrame::PositionFrameView(aFrame);
else
nsContainerFrame::PositionChildViews(aFrame);
}
static void MoveChildTo(nsIFrame* aParent, nsIFrame* aChild, nsPoint aOrigin) {
if (aChild->GetPosition() == aOrigin) {
return;
}
aChild->SetPosition(aOrigin);
PlaceFrameView(aChild);
}
nscoord
nsColumnSetFrame::GetMinWidth(nsRenderingContext *aRenderingContext) {
nscoord width = 0;
DISPLAY_MIN_WIDTH(this, width);
if (mFrames.FirstChild()) {
width = mFrames.FirstChild()->GetMinWidth(aRenderingContext);
}
const nsStyleColumn* colStyle = GetStyleColumn();
nscoord colWidth;
if (colStyle->mColumnWidth.GetUnit() == eStyleUnit_Coord) {
colWidth = colStyle->mColumnWidth.GetCoordValue();
// As available width reduces to zero, we reduce our number of columns
// to one, and don't enforce the column width, so just return the min
// of the child's min-width with any specified column width.
width = NS_MIN(width, colWidth);
} else {
NS_ASSERTION(colStyle->mColumnCount > 0,
"column-count and column-width can't both be auto");
// As available width reduces to zero, we still have mColumnCount columns,
// so multiply the child's min-width by the number of columns.
colWidth = width;
width *= colStyle->mColumnCount;
// The multiplication above can make 'width' negative (integer overflow),
// so use NS_MAX to protect against that.
width = NS_MAX(width, colWidth);
}
// XXX count forced column breaks here? Maybe we should return the child's
// min-width times the minimum number of columns.
return width;
}
nscoord
nsColumnSetFrame::GetPrefWidth(nsRenderingContext *aRenderingContext) {
// Our preferred width is our desired column width, if specified, otherwise
// the child's preferred width, times the number of columns, plus the width
// of any required column gaps
// XXX what about forced column breaks here?
nscoord result = 0;
DISPLAY_PREF_WIDTH(this, result);
const nsStyleColumn* colStyle = GetStyleColumn();
nscoord colGap = GetColumnGap(this, colStyle);
nscoord colWidth;
if (colStyle->mColumnWidth.GetUnit() == eStyleUnit_Coord) {
colWidth = colStyle->mColumnWidth.GetCoordValue();
} else if (mFrames.FirstChild()) {
colWidth = mFrames.FirstChild()->GetPrefWidth(aRenderingContext);
} else {
colWidth = 0;
}
int32_t numColumns = colStyle->mColumnCount;
if (numColumns <= 0) {
// if column-count is auto, assume one column
numColumns = 1;
}
nscoord width = colWidth*numColumns + colGap*(numColumns - 1);
// The multiplication above can make 'width' negative (integer overflow),
// so use NS_MAX to protect against that.
result = NS_MAX(width, colWidth);
return result;
}
bool
nsColumnSetFrame::ReflowChildren(nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus,
const ReflowConfig& aConfig,
bool aUnboundedLastColumn,
nsCollapsingMargin* aBottomMarginCarriedOut,
ColumnBalanceData& aColData)
{
aColData.Reset();
bool allFit = true;
bool RTL = GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
bool shrinkingHeightOnly = !NS_SUBTREE_DIRTY(this) &&
mLastBalanceHeight > aConfig.mColMaxHeight;
#ifdef DEBUG_roc
printf("*** Doing column reflow pass: mLastBalanceHeight=%d, mColMaxHeight=%d, RTL=%d\n, mBalanceColCount=%d, mColWidth=%d, mColGap=%d\n",
mLastBalanceHeight, aConfig.mColMaxHeight, RTL, aConfig.mBalanceColCount,
aConfig.mColWidth, aConfig.mColGap);
#endif
DrainOverflowColumns();
if (mLastBalanceHeight != aConfig.mColMaxHeight) {
mLastBalanceHeight = aConfig.mColMaxHeight;
// XXX Seems like this could fire if incremental reflow pushed the column set
// down so we reflow incrementally with a different available height.
// We need a way to do an incremental reflow and be sure availableHeight
// changes are taken account of! Right now I think block frames with absolute
// children might exit early.
//NS_ASSERTION(aKidReason != eReflowReason_Incremental,
// "incremental reflow should not have changed the balance height");
}
// get our border and padding
const nsMargin &borderPadding = aReflowState.mComputedBorderPadding;
nsRect contentRect(0, 0, 0, 0);
nsOverflowAreas overflowRects;
nsIFrame* child = mFrames.FirstChild();
nsPoint childOrigin = nsPoint(borderPadding.left, borderPadding.top);
// For RTL, figure out where the last column's left edge should be. Since the
// columns might not fill the frame exactly, we need to account for the
// slop. Otherwise we'll waste time moving the columns by some tiny
// amount unnecessarily.
if (RTL) {
nscoord availWidth = aReflowState.availableWidth;
if (aReflowState.ComputedWidth() != NS_INTRINSICSIZE) {
availWidth = aReflowState.ComputedWidth();
}
if (availWidth != NS_INTRINSICSIZE) {
childOrigin.x += availWidth - aConfig.mColWidth;
#ifdef DEBUG_roc
printf("*** childOrigin.x = %d\n", childOrigin.x);
#endif
}
}
int columnCount = 0;
int contentBottom = 0;
bool reflowNext = false;
while (child) {
// Try to skip reflowing the child. We can't skip if the child is dirty. We also can't
// skip if the next column is dirty, because the next column's first line(s)
// might be pullable back to this column. We can't skip if it's the last child
// because we need to obtain the bottom margin. We can't skip
// if this is the last column and we're supposed to assign unbounded
// height to it, because that could change the available height from
// the last time we reflowed it and we should try to pull all the
// content from its next sibling. (Note that it might be the last
// column, but not be the last child because the desired number of columns
// has changed.)
bool skipIncremental = !aReflowState.ShouldReflowAllKids()
&& !NS_SUBTREE_DIRTY(child)
&& child->GetNextSibling()
&& !(aUnboundedLastColumn && columnCount == aConfig.mBalanceColCount - 1)
&& !NS_SUBTREE_DIRTY(child->GetNextSibling());
// If we need to pull up content from the prev-in-flow then this is not just
// a height shrink. The prev in flow will have set the dirty bit.
// Check the overflow rect YMost instead of just the child's content height. The child
// may have overflowing content that cares about the available height boundary.
// (It may also have overflowing content that doesn't care about the available height
// boundary, but if so, too bad, this optimization is defeated.)
// We want scrollable overflow here since this is a calculation that
// affects layout.
bool skipResizeHeightShrink = shrinkingHeightOnly
&& child->GetScrollableOverflowRect().YMost() <= aConfig.mColMaxHeight;
nscoord childContentBottom = 0;
if (!reflowNext && (skipIncremental || skipResizeHeightShrink)) {
// This child does not need to be reflowed, but we may need to move it
MoveChildTo(this, child, childOrigin);
// If this is the last frame then make sure we get the right status
nsIFrame* kidNext = child->GetNextSibling();
if (kidNext) {
aStatus = (kidNext->GetStateBits() & NS_FRAME_IS_OVERFLOW_CONTAINER)
? NS_FRAME_OVERFLOW_INCOMPLETE
: NS_FRAME_NOT_COMPLETE;
} else {
aStatus = mLastFrameStatus;
}
childContentBottom = nsLayoutUtils::CalculateContentBottom(child);
#ifdef DEBUG_roc
printf("*** Skipping child #%d %p (incremental %d, resize height shrink %d): status = %d\n",
columnCount, (void*)child, skipIncremental, skipResizeHeightShrink, aStatus);
#endif
} else {
nsSize availSize(aConfig.mColWidth, aConfig.mColMaxHeight);
if (aUnboundedLastColumn && columnCount == aConfig.mBalanceColCount - 1) {
availSize.height = GetAvailableContentHeight(aReflowState);
}
if (reflowNext)
child->AddStateBits(NS_FRAME_IS_DIRTY);
nsHTMLReflowState kidReflowState(PresContext(), aReflowState, child,
availSize, availSize.width,
aReflowState.ComputedHeight());
kidReflowState.mFlags.mIsTopOfPage = true;
kidReflowState.mFlags.mTableIsSplittable = false;
kidReflowState.mFlags.mIsColumnBalancing = aConfig.mBalanceColCount < INT32_MAX;
#ifdef DEBUG_roc
printf("*** Reflowing child #%d %p: availHeight=%d\n",
columnCount, (void*)child,availSize.height);
#endif
// Note if the column's next in flow is not being changed by this incremental reflow.
// This may allow the current column to avoid trying to pull lines from the next column.
if (child->GetNextSibling() &&
!(GetStateBits() & NS_FRAME_IS_DIRTY) &&
!(child->GetNextSibling()->GetStateBits() & NS_FRAME_IS_DIRTY)) {
kidReflowState.mFlags.mNextInFlowUntouched = true;
}
nsHTMLReflowMetrics kidDesiredSize(aDesiredSize.mFlags);
// XXX it would be cool to consult the float manager for the
// previous block to figure out the region of floats from the
// previous column that extend into this column, and subtract
// that region from the new float manager. So you could stick a
// really big float in the first column and text in following
// columns would flow around it.
// Reflow the frame
ReflowChild(child, PresContext(), kidDesiredSize, kidReflowState,
childOrigin.x + kidReflowState.mComputedMargin.left,
childOrigin.y + kidReflowState.mComputedMargin.top,
0, aStatus);
reflowNext = (aStatus & NS_FRAME_REFLOW_NEXTINFLOW) != 0;
#ifdef DEBUG_roc
printf("*** Reflowed child #%d %p: status = %d, desiredSize=%d,%d CarriedOutBottomMargin=%d\n",
columnCount, (void*)child, aStatus, kidDesiredSize.width, kidDesiredSize.height,
kidDesiredSize.mCarriedOutBottomMargin.get());
#endif
NS_FRAME_TRACE_REFLOW_OUT("Column::Reflow", aStatus);
*aBottomMarginCarriedOut = kidDesiredSize.mCarriedOutBottomMargin;
FinishReflowChild(child, PresContext(), &kidReflowState,
kidDesiredSize, childOrigin.x, childOrigin.y, 0);
childContentBottom = nsLayoutUtils::CalculateContentBottom(child);
if (childContentBottom > aConfig.mColMaxHeight) {
allFit = false;
}
if (childContentBottom > availSize.height) {
aColData.mMaxOverflowingHeight = NS_MAX(childContentBottom,
aColData.mMaxOverflowingHeight);
}
}
contentRect.UnionRect(contentRect, child->GetRect());
ConsiderChildOverflow(overflowRects, child);
contentBottom = NS_MAX(contentBottom, childContentBottom);
aColData.mLastHeight = childContentBottom;
aColData.mSumHeight += childContentBottom;
// Build a continuation column if necessary
nsIFrame* kidNextInFlow = child->GetNextInFlow();
if (NS_FRAME_IS_FULLY_COMPLETE(aStatus) && !NS_FRAME_IS_TRUNCATED(aStatus)) {
NS_ASSERTION(!kidNextInFlow, "next in flow should have been deleted");
child = nullptr;
break;
} else {
++columnCount;
// Make sure that the column has a next-in-flow. If not, we must
// create one to hold the overflowing stuff, even if we're just
// going to put it on our overflow list and let *our*
// next in flow handle it.
if (!kidNextInFlow) {
NS_ASSERTION(aStatus & NS_FRAME_REFLOW_NEXTINFLOW,
"We have to create a continuation, but the block doesn't want us to reflow it?");
// We need to create a continuing column
nsresult rv = CreateNextInFlow(PresContext(), child, kidNextInFlow);
if (NS_FAILED(rv)) {
NS_NOTREACHED("Couldn't create continuation");
child = nullptr;
break;
}
}
// Make sure we reflow a next-in-flow when it switches between being
// normal or overflow container
if (NS_FRAME_OVERFLOW_IS_INCOMPLETE(aStatus)) {
if (!(kidNextInFlow->GetStateBits() & NS_FRAME_IS_OVERFLOW_CONTAINER)) {
aStatus |= NS_FRAME_REFLOW_NEXTINFLOW;
reflowNext = true;
kidNextInFlow->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
}
}
else if (kidNextInFlow->GetStateBits() & NS_FRAME_IS_OVERFLOW_CONTAINER) {
aStatus |= NS_FRAME_REFLOW_NEXTINFLOW;
reflowNext = true;
kidNextInFlow->RemoveStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
}
if ((contentBottom > aReflowState.mComputedMaxHeight ||
contentBottom > aReflowState.ComputedHeight()) &&
aConfig.mBalanceColCount < INT32_MAX) {
// We overflowed vertically, but have not exceeded the number
// of columns. If we're balancing, then we should try reverting
// to auto instead.
aColData.mShouldRevertToAuto = true;
}
if (columnCount >= aConfig.mBalanceColCount) {
if (contentBottom >= aReflowState.availableHeight) {
// No more columns allowed here. Stop.
aStatus |= NS_FRAME_REFLOW_NEXTINFLOW;
kidNextInFlow->AddStateBits(NS_FRAME_IS_DIRTY);
// Move any of our leftover columns to our overflow list. Our
// next-in-flow will eventually pick them up.
const nsFrameList& continuationColumns = mFrames.RemoveFramesAfter(child);
if (continuationColumns.NotEmpty()) {
SetOverflowFrames(PresContext(), continuationColumns);
}
child = nullptr;
break;
} else if (contentBottom > aReflowState.mComputedMaxHeight ||
contentBottom > aReflowState.ComputedHeight()) {
aColData.mShouldRevertToAuto = true;
} else {
// The number of columns required is too high.
allFit = false;
}
}
}
if (PresContext()->HasPendingInterrupt()) {
// Stop the loop now while |child| still points to the frame that bailed
// out. We could keep going here and condition a bunch of the code in
// this loop on whether there's an interrupt, or even just keep going and
// trying to reflow the blocks (even though we know they'll interrupt
// right after their first line), but stopping now is conceptually the
// simplest (and probably fastest) thing.
break;
}
// Advance to the next column
child = child->GetNextSibling();
if (child) {
if (!RTL) {
childOrigin.x += aConfig.mColWidth + aConfig.mColGap;
} else {
childOrigin.x -= aConfig.mColWidth + aConfig.mColGap;
}
#ifdef DEBUG_roc
printf("*** NEXT CHILD ORIGIN.x = %d\n", childOrigin.x);
#endif
}
}
if (PresContext()->CheckForInterrupt(this) &&
(GetStateBits() & NS_FRAME_IS_DIRTY)) {
// Mark all our kids starting with |child| dirty
// Note that this is a CheckForInterrupt call, not a HasPendingInterrupt,
// because we might have interrupted while reflowing |child|, and since
// we're about to add a dirty bit to |child| we need to make sure that
// |this| is scheduled to have dirty bits marked on it and its ancestors.
// Otherwise, when we go to mark dirty bits on |child|'s ancestors we'll
// bail out immediately, since it'll already have a dirty bit.
for (; child; child = child->GetNextSibling()) {
child->AddStateBits(NS_FRAME_IS_DIRTY);
}
}
aColData.mMaxHeight = contentBottom;
contentRect.height = NS_MAX(contentRect.height, contentBottom);
mLastFrameStatus = aStatus;
// contentRect included the borderPadding.left,borderPadding.top of the child rects
contentRect -= nsPoint(borderPadding.left, borderPadding.top);
nsSize contentSize = nsSize(contentRect.XMost(), contentRect.YMost());
// Apply computed and min/max values
if (aReflowState.ComputedHeight() != NS_INTRINSICSIZE) {
contentSize.height = aReflowState.ComputedHeight();
} else {
if (NS_UNCONSTRAINEDSIZE != aReflowState.mComputedMaxHeight) {
contentSize.height = NS_MIN(aReflowState.mComputedMaxHeight, contentSize.height);
}
if (NS_UNCONSTRAINEDSIZE != aReflowState.mComputedMinHeight) {
contentSize.height = NS_MAX(aReflowState.mComputedMinHeight, contentSize.height);
}
}
if (aReflowState.ComputedWidth() != NS_INTRINSICSIZE) {
contentSize.width = aReflowState.ComputedWidth();
} else {
if (NS_UNCONSTRAINEDSIZE != aReflowState.mComputedMaxWidth) {
contentSize.width = NS_MIN(aReflowState.mComputedMaxWidth, contentSize.width);
}
if (NS_UNCONSTRAINEDSIZE != aReflowState.mComputedMinWidth) {
contentSize.width = NS_MAX(aReflowState.mComputedMinWidth, contentSize.width);
}
}
aDesiredSize.height = borderPadding.top + contentSize.height +
borderPadding.bottom;
aDesiredSize.width = contentSize.width + borderPadding.left + borderPadding.right;
aDesiredSize.mOverflowAreas = overflowRects;
aDesiredSize.UnionOverflowAreasWithDesiredBounds();
#ifdef DEBUG_roc
printf("*** DONE PASS feasible=%d\n", allFit && NS_FRAME_IS_FULLY_COMPLETE(aStatus)
&& !NS_FRAME_IS_TRUNCATED(aStatus));
#endif
return allFit && NS_FRAME_IS_FULLY_COMPLETE(aStatus)
&& !NS_FRAME_IS_TRUNCATED(aStatus);
}
void
nsColumnSetFrame::DrainOverflowColumns()
{
// First grab the prev-in-flows overflows and reparent them to this
// frame.
nsColumnSetFrame* prev = static_cast<nsColumnSetFrame*>(GetPrevInFlow());
if (prev) {
nsAutoPtr<nsFrameList> overflows(prev->StealOverflowFrames());
if (overflows) {
nsContainerFrame::ReparentFrameViewList(PresContext(), *overflows,
prev, this);
mFrames.InsertFrames(this, nullptr, *overflows);
}
}
// Now pull back our own overflows and append them to our children.
// We don't need to reparent them since we're already their parent.
nsAutoPtr<nsFrameList> overflows(StealOverflowFrames());
if (overflows) {
// We're already the parent for these frames, so no need to set
// their parent again.
mFrames.AppendFrames(nullptr, *overflows);
}
}
NS_IMETHODIMP
nsColumnSetFrame::Reflow(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
{
// Don't support interruption in columns
nsPresContext::InterruptPreventer noInterrupts(aPresContext);
DO_GLOBAL_REFLOW_COUNT("nsColumnSetFrame");
DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
// Initialize OUT parameter
aStatus = NS_FRAME_COMPLETE;
// Our children depend on our height if we have a fixed height.
if (aReflowState.ComputedHeight() != NS_AUTOHEIGHT) {
NS_ASSERTION(aReflowState.ComputedHeight() != NS_INTRINSICSIZE,
"Unexpected mComputedHeight");
AddStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT);
}
else {
RemoveStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT);
}
//------------ Handle Incremental Reflow -----------------
ReflowConfig config = ChooseColumnStrategy(aReflowState);
bool isBalancing = config.mBalanceColCount < INT32_MAX;
// If balancing, then we allow the last column to grow to unbounded
// height during the first reflow. This gives us a way to estimate
// what the average column height should be, because we can measure
// the heights of all the columns and sum them up. But don't do this
// if we have a next in flow because we don't want to suck all its
// content back here and then have to push it out again!
nsIFrame* nextInFlow = GetNextInFlow();
bool unboundedLastColumn = isBalancing && !nextInFlow;
nsCollapsingMargin carriedOutBottomMargin;
ColumnBalanceData colData;
colData.mShouldRevertToAuto = false;
// This loop exists in order to try balancing initially. If the balancing
// overflows, then we want to revert to column-fill: auto.
// Our loop invariant is: colData.mShouldRevertToAuto is true if and only
// if we've reflowed our children, and during the most recent reflow of
// children, we were balancing and we overflowed in the block direction.
do {
if (colData.mShouldRevertToAuto) {
config = ChooseColumnStrategy(aReflowState);
isBalancing = false;
config.mBalanceColCount = INT32_MAX;
}
bool feasible = ReflowChildren(aDesiredSize, aReflowState,
aStatus, config, unboundedLastColumn, &carriedOutBottomMargin, colData);
if (isBalancing && !aPresContext->HasPendingInterrupt()) {
nscoord availableContentHeight = GetAvailableContentHeight(aReflowState);
// Termination of the algorithm below is guaranteed because
// knownFeasibleHeight - knownInfeasibleHeight decreases in every
// iteration.
nscoord knownFeasibleHeight = NS_INTRINSICSIZE;
nscoord knownInfeasibleHeight = 0;
// We set this flag when we detect that we may contain a frame
// that can break anywhere (thus foiling the linear decrease-by-one
// search)
bool maybeContinuousBreakingDetected = false;
while (!aPresContext->HasPendingInterrupt()) {
nscoord lastKnownFeasibleHeight = knownFeasibleHeight;
// Record what we learned from the last reflow
if (feasible) {
// maxHeight is feasible. Also, mLastBalanceHeight is feasible.
knownFeasibleHeight = NS_MIN(knownFeasibleHeight, colData.mMaxHeight);
knownFeasibleHeight = NS_MIN(knownFeasibleHeight, mLastBalanceHeight);
// Furthermore, no height less than the height of the last
// column can ever be feasible. (We might be able to reduce the
// height of a non-last column by moving content to a later column,
// but we can't do that with the last column.)
if (mFrames.GetLength() == config.mBalanceColCount) {
knownInfeasibleHeight = NS_MAX(knownInfeasibleHeight,
colData.mLastHeight - 1);
}
} else {
knownInfeasibleHeight = NS_MAX(knownInfeasibleHeight, mLastBalanceHeight);
// If a column didn't fit in its available height, then its current
// height must be the minimum height for unbreakable content in
// the column, and therefore no smaller height can be feasible.
knownInfeasibleHeight = NS_MAX(knownInfeasibleHeight,
colData.mMaxOverflowingHeight - 1);
if (unboundedLastColumn) {
// The last column is unbounded, so all content got reflowed, so the
// mColMaxHeight is feasible.
knownFeasibleHeight = NS_MIN(knownFeasibleHeight,
colData.mMaxHeight);
}
}
#ifdef DEBUG_roc
printf("*** nsColumnSetFrame::Reflow balancing knownInfeasible=%d knownFeasible=%d\n",
knownInfeasibleHeight, knownFeasibleHeight);
#endif
if (knownInfeasibleHeight >= knownFeasibleHeight - 1) {
// knownFeasibleHeight is where we want to be
break;
}
if (knownInfeasibleHeight >= availableContentHeight) {
break;