forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsGfxScrollFrame.cpp
4259 lines (3725 loc) · 147 KB
/
nsGfxScrollFrame.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 to wrap rendering objects that should be scrollable */
#include "nsGfxScrollFrame.h"
#include "base/compiler_specific.h"
#include "nsCOMPtr.h"
#include "nsPresContext.h"
#include "nsView.h"
#include "nsIScrollable.h"
#include "nsContainerFrame.h"
#include "nsGkAtoms.h"
#include "nsINameSpaceManager.h"
#include "nsContentList.h"
#include "nsIDocumentInlines.h"
#include "nsFontMetrics.h"
#include "nsBoxLayoutState.h"
#include "nsINodeInfo.h"
#include "nsScrollbarFrame.h"
#include "nsIScrollbarMediator.h"
#include "nsITextControlFrame.h"
#include "nsIDOMHTMLTextAreaElement.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"
#include "nsAutoPtr.h"
#include "nsPresState.h"
#include "nsIHTMLDocument.h"
#include "nsEventDispatcher.h"
#include "nsContentUtils.h"
#include "nsLayoutUtils.h"
#include "nsBidiUtils.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/Preferences.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/dom/Element.h"
#include <stdint.h>
#include "mozilla/MathAlgorithms.h"
#include "FrameLayerBuilder.h"
#include "nsSMILKeySpline.h"
#include "nsSubDocumentFrame.h"
#include "nsSVGOuterSVGFrame.h"
#include "mozilla/Attributes.h"
#include "ScrollbarActivity.h"
#include "nsRefreshDriver.h"
#include "nsThemeConstants.h"
#include "nsSVGIntegrationUtils.h"
#include "nsIScrollPositionListener.h"
#include "StickyScrollContainer.h"
#include "nsIFrameInlines.h"
#include <algorithm>
#include <cstdlib> // for std::abs(int/long)
#include <cmath> // for std::abs(float/double)
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::layout;
//----------------------------------------------------------------------
//----------nsHTMLScrollFrame-------------------------------------------
nsIFrame*
NS_NewHTMLScrollFrame(nsIPresShell* aPresShell, nsStyleContext* aContext, bool aIsRoot)
{
return new (aPresShell) nsHTMLScrollFrame(aPresShell, aContext, aIsRoot);
}
NS_IMPL_FRAMEARENA_HELPERS(nsHTMLScrollFrame)
nsHTMLScrollFrame::nsHTMLScrollFrame(nsIPresShell* aShell, nsStyleContext* aContext, bool aIsRoot)
: nsContainerFrame(aContext),
mInner(ALLOW_THIS_IN_INITIALIZER_LIST(this), aIsRoot)
{
}
void
nsHTMLScrollFrame::ScrollbarActivityStarted() const
{
if (mInner.mScrollbarActivity) {
mInner.mScrollbarActivity->ActivityStarted();
}
}
void
nsHTMLScrollFrame::ScrollbarActivityStopped() const
{
if (mInner.mScrollbarActivity) {
mInner.mScrollbarActivity->ActivityStopped();
}
}
nsresult
nsHTMLScrollFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
{
return mInner.CreateAnonymousContent(aElements);
}
void
nsHTMLScrollFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
uint32_t aFilter)
{
mInner.AppendAnonymousContentTo(aElements, aFilter);
}
void
nsHTMLScrollFrame::DestroyFrom(nsIFrame* aDestructRoot)
{
DestroyAbsoluteFrames(aDestructRoot);
mInner.Destroy();
nsContainerFrame::DestroyFrom(aDestructRoot);
}
NS_IMETHODIMP
nsHTMLScrollFrame::SetInitialChildList(ChildListID aListID,
nsFrameList& aChildList)
{
nsresult rv = nsContainerFrame::SetInitialChildList(aListID, aChildList);
mInner.ReloadChildFrames();
return rv;
}
NS_IMETHODIMP
nsHTMLScrollFrame::AppendFrames(ChildListID aListID,
nsFrameList& aFrameList)
{
NS_ASSERTION(aListID == kPrincipalList, "Only main list supported");
mFrames.AppendFrames(nullptr, aFrameList);
mInner.ReloadChildFrames();
return NS_OK;
}
NS_IMETHODIMP
nsHTMLScrollFrame::InsertFrames(ChildListID aListID,
nsIFrame* aPrevFrame,
nsFrameList& aFrameList)
{
NS_ASSERTION(aListID == kPrincipalList, "Only main list supported");
NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == this,
"inserting after sibling frame with different parent");
mFrames.InsertFrames(nullptr, aPrevFrame, aFrameList);
mInner.ReloadChildFrames();
return NS_OK;
}
NS_IMETHODIMP
nsHTMLScrollFrame::RemoveFrame(ChildListID aListID,
nsIFrame* aOldFrame)
{
NS_ASSERTION(aListID == kPrincipalList, "Only main list supported");
mFrames.DestroyFrame(aOldFrame);
mInner.ReloadChildFrames();
return NS_OK;
}
nsSplittableType
nsHTMLScrollFrame::GetSplittableType() const
{
return NS_FRAME_NOT_SPLITTABLE;
}
nsIAtom*
nsHTMLScrollFrame::GetType() const
{
return nsGkAtoms::scrollFrame;
}
/**
HTML scrolling implementation
All other things being equal, we prefer layouts with fewer scrollbars showing.
*/
struct MOZ_STACK_CLASS ScrollReflowState {
const nsHTMLReflowState& mReflowState;
nsBoxLayoutState mBoxState;
ScrollbarStyles mStyles;
nsMargin mComputedBorder;
// === Filled in by ReflowScrolledFrame ===
nsOverflowAreas mContentsOverflowAreas;
bool mReflowedContentsWithHScrollbar;
bool mReflowedContentsWithVScrollbar;
// === Filled in when TryLayout succeeds ===
// The size of the inside-border area
nsSize mInsideBorderSize;
// Whether we decided to show the horizontal scrollbar
bool mShowHScrollbar;
// Whether we decided to show the vertical scrollbar
bool mShowVScrollbar;
ScrollReflowState(nsIScrollableFrame* aFrame,
const nsHTMLReflowState& aState) :
mReflowState(aState),
// mBoxState is just used for scrollbars so we don't need to
// worry about the reflow depth here
mBoxState(aState.frame->PresContext(), aState.rendContext, 0),
mStyles(aFrame->GetScrollbarStyles()) {
}
};
// XXXldb Can this go away?
static nsSize ComputeInsideBorderSize(ScrollReflowState* aState,
const nsSize& aDesiredInsideBorderSize)
{
// aDesiredInsideBorderSize is the frame size; i.e., it includes
// borders and padding (but the scrolled child doesn't have
// borders). The scrolled child has the same padding as us.
nscoord contentWidth = aState->mReflowState.ComputedWidth();
if (contentWidth == NS_UNCONSTRAINEDSIZE) {
contentWidth = aDesiredInsideBorderSize.width -
aState->mReflowState.mComputedPadding.LeftRight();
}
nscoord contentHeight = aState->mReflowState.ComputedHeight();
if (contentHeight == NS_UNCONSTRAINEDSIZE) {
contentHeight = aDesiredInsideBorderSize.height -
aState->mReflowState.mComputedPadding.TopBottom();
}
contentWidth = aState->mReflowState.ApplyMinMaxWidth(contentWidth);
contentHeight = aState->mReflowState.ApplyMinMaxHeight(contentHeight);
return nsSize(contentWidth + aState->mReflowState.mComputedPadding.LeftRight(),
contentHeight + aState->mReflowState.mComputedPadding.TopBottom());
}
static void
GetScrollbarMetrics(nsBoxLayoutState& aState, nsIFrame* aBox, nsSize* aMin,
nsSize* aPref, bool aVertical)
{
NS_ASSERTION(aState.GetRenderingContext(),
"Must have rendering context in layout state for size "
"computations");
if (aMin) {
*aMin = aBox->GetMinSize(aState);
nsBox::AddMargin(aBox, *aMin);
if (aMin->width < 0) {
aMin->width = 0;
}
if (aMin->height < 0) {
aMin->height = 0;
}
}
if (aPref) {
*aPref = aBox->GetPrefSize(aState);
nsBox::AddMargin(aBox, *aPref);
if (aPref->width < 0) {
aPref->width = 0;
}
if (aPref->height < 0) {
aPref->height = 0;
}
}
}
/**
* Assuming that we know the metrics for our wrapped frame and
* whether the horizontal and/or vertical scrollbars are present,
* compute the resulting layout and return true if the layout is
* consistent. If the layout is consistent then we fill in the
* computed fields of the ScrollReflowState.
*
* The layout is consistent when both scrollbars are showing if and only
* if they should be showing. A horizontal scrollbar should be showing if all
* following conditions are met:
* 1) the style is not HIDDEN
* 2) our inside-border height is at least the scrollbar height (i.e., the
* scrollbar fits vertically)
* 3) our scrollport width (the inside-border width minus the width allocated for a
* vertical scrollbar, if showing) is at least the scrollbar's min-width
* (i.e., the scrollbar fits horizontally)
* 4) the style is SCROLL, or the kid's overflow-area XMost is
* greater than the scrollport width
*
* @param aForce if true, then we just assume the layout is consistent.
*/
bool
nsHTMLScrollFrame::TryLayout(ScrollReflowState* aState,
nsHTMLReflowMetrics* aKidMetrics,
bool aAssumeHScroll, bool aAssumeVScroll,
bool aForce, nsresult* aResult)
{
*aResult = NS_OK;
if ((aState->mStyles.mVertical == NS_STYLE_OVERFLOW_HIDDEN && aAssumeVScroll) ||
(aState->mStyles.mHorizontal == NS_STYLE_OVERFLOW_HIDDEN && aAssumeHScroll)) {
NS_ASSERTION(!aForce, "Shouldn't be forcing a hidden scrollbar to show!");
return false;
}
if (aAssumeVScroll != aState->mReflowedContentsWithVScrollbar ||
(aAssumeHScroll != aState->mReflowedContentsWithHScrollbar &&
ScrolledContentDependsOnHeight(aState))) {
nsresult rv = ReflowScrolledFrame(aState, aAssumeHScroll, aAssumeVScroll,
aKidMetrics, false);
if (NS_FAILED(rv)) {
*aResult = rv;
return false;
}
}
nsSize vScrollbarMinSize(0, 0);
nsSize vScrollbarPrefSize(0, 0);
if (mInner.mVScrollbarBox) {
GetScrollbarMetrics(aState->mBoxState, mInner.mVScrollbarBox,
&vScrollbarMinSize,
aAssumeVScroll ? &vScrollbarPrefSize : nullptr, true);
}
nscoord vScrollbarDesiredWidth = aAssumeVScroll ? vScrollbarPrefSize.width : 0;
nscoord vScrollbarMinHeight = aAssumeVScroll ? vScrollbarMinSize.height : 0;
nsSize hScrollbarMinSize(0, 0);
nsSize hScrollbarPrefSize(0, 0);
if (mInner.mHScrollbarBox) {
GetScrollbarMetrics(aState->mBoxState, mInner.mHScrollbarBox,
&hScrollbarMinSize,
aAssumeHScroll ? &hScrollbarPrefSize : nullptr, false);
}
nscoord hScrollbarDesiredHeight = aAssumeHScroll ? hScrollbarPrefSize.height : 0;
nscoord hScrollbarMinWidth = aAssumeHScroll ? hScrollbarMinSize.width : 0;
// First, compute our inside-border size and scrollport size
// XXXldb Can we depend more on ComputeSize here?
nsSize desiredInsideBorderSize;
desiredInsideBorderSize.width = vScrollbarDesiredWidth +
std::max(aKidMetrics->width, hScrollbarMinWidth);
desiredInsideBorderSize.height = hScrollbarDesiredHeight +
std::max(aKidMetrics->height, vScrollbarMinHeight);
aState->mInsideBorderSize =
ComputeInsideBorderSize(aState, desiredInsideBorderSize);
nsSize scrollPortSize = nsSize(std::max(0, aState->mInsideBorderSize.width - vScrollbarDesiredWidth),
std::max(0, aState->mInsideBorderSize.height - hScrollbarDesiredHeight));
if (!aForce) {
nsRect scrolledRect =
mInner.GetScrolledRectInternal(aState->mContentsOverflowAreas.ScrollableOverflow(),
scrollPortSize);
nscoord oneDevPixel = aState->mBoxState.PresContext()->DevPixelsToAppUnits(1);
// If the style is HIDDEN then we already know that aAssumeHScroll is false
if (aState->mStyles.mHorizontal != NS_STYLE_OVERFLOW_HIDDEN) {
bool wantHScrollbar =
aState->mStyles.mHorizontal == NS_STYLE_OVERFLOW_SCROLL ||
scrolledRect.XMost() >= scrollPortSize.width + oneDevPixel ||
scrolledRect.x <= -oneDevPixel;
if (scrollPortSize.width < hScrollbarMinSize.width)
wantHScrollbar = false;
if (wantHScrollbar != aAssumeHScroll)
return false;
}
// If the style is HIDDEN then we already know that aAssumeVScroll is false
if (aState->mStyles.mVertical != NS_STYLE_OVERFLOW_HIDDEN) {
bool wantVScrollbar =
aState->mStyles.mVertical == NS_STYLE_OVERFLOW_SCROLL ||
scrolledRect.YMost() >= scrollPortSize.height + oneDevPixel ||
scrolledRect.y <= -oneDevPixel;
if (scrollPortSize.height < vScrollbarMinSize.height)
wantVScrollbar = false;
if (wantVScrollbar != aAssumeVScroll)
return false;
}
}
nscoord vScrollbarActualWidth = aState->mInsideBorderSize.width - scrollPortSize.width;
aState->mShowHScrollbar = aAssumeHScroll;
aState->mShowVScrollbar = aAssumeVScroll;
nsPoint scrollPortOrigin(aState->mComputedBorder.left,
aState->mComputedBorder.top);
if (!mInner.IsScrollbarOnRight()) {
scrollPortOrigin.x += vScrollbarActualWidth;
}
mInner.mScrollPort = nsRect(scrollPortOrigin, scrollPortSize);
return true;
}
bool
nsHTMLScrollFrame::ScrolledContentDependsOnHeight(ScrollReflowState* aState)
{
// Return true if ReflowScrolledFrame is going to do something different
// based on the presence of a horizontal scrollbar.
return (mInner.mScrolledFrame->GetStateBits() & NS_FRAME_CONTAINS_RELATIVE_HEIGHT) ||
aState->mReflowState.ComputedHeight() != NS_UNCONSTRAINEDSIZE ||
aState->mReflowState.mComputedMinHeight > 0 ||
aState->mReflowState.mComputedMaxHeight != NS_UNCONSTRAINEDSIZE;
}
nsresult
nsHTMLScrollFrame::ReflowScrolledFrame(ScrollReflowState* aState,
bool aAssumeHScroll,
bool aAssumeVScroll,
nsHTMLReflowMetrics* aMetrics,
bool aFirstPass)
{
// these could be NS_UNCONSTRAINEDSIZE ... std::min arithmetic should
// be OK
nscoord paddingLR = aState->mReflowState.mComputedPadding.LeftRight();
nscoord availWidth = aState->mReflowState.ComputedWidth() + paddingLR;
nscoord computedHeight = aState->mReflowState.ComputedHeight();
nscoord computedMinHeight = aState->mReflowState.mComputedMinHeight;
nscoord computedMaxHeight = aState->mReflowState.mComputedMaxHeight;
if (!ShouldPropagateComputedHeightToScrolledContent()) {
computedHeight = NS_UNCONSTRAINEDSIZE;
computedMinHeight = 0;
computedMaxHeight = NS_UNCONSTRAINEDSIZE;
}
if (aAssumeHScroll) {
nsSize hScrollbarPrefSize;
GetScrollbarMetrics(aState->mBoxState, mInner.mHScrollbarBox,
nullptr, &hScrollbarPrefSize, false);
if (computedHeight != NS_UNCONSTRAINEDSIZE)
computedHeight = std::max(0, computedHeight - hScrollbarPrefSize.height);
computedMinHeight = std::max(0, computedMinHeight - hScrollbarPrefSize.height);
if (computedMaxHeight != NS_UNCONSTRAINEDSIZE)
computedMaxHeight = std::max(0, computedMaxHeight - hScrollbarPrefSize.height);
}
if (aAssumeVScroll) {
nsSize vScrollbarPrefSize;
GetScrollbarMetrics(aState->mBoxState, mInner.mVScrollbarBox,
nullptr, &vScrollbarPrefSize, true);
availWidth = std::max(0, availWidth - vScrollbarPrefSize.width);
}
nsPresContext* presContext = PresContext();
// Pass false for aInit so we can pass in the correct padding.
nsHTMLReflowState kidReflowState(presContext, aState->mReflowState,
mInner.mScrolledFrame,
nsSize(availWidth, NS_UNCONSTRAINEDSIZE),
-1, -1, nsHTMLReflowState::CALLER_WILL_INIT);
kidReflowState.Init(presContext, -1, -1, nullptr,
&aState->mReflowState.mComputedPadding);
kidReflowState.mFlags.mAssumingHScrollbar = aAssumeHScroll;
kidReflowState.mFlags.mAssumingVScrollbar = aAssumeVScroll;
kidReflowState.SetComputedHeight(computedHeight);
kidReflowState.mComputedMinHeight = computedMinHeight;
kidReflowState.mComputedMaxHeight = computedMaxHeight;
// Temporarily set mHasHorizontalScrollbar/mHasVerticalScrollbar to
// reflect our assumptions while we reflow the child.
bool didHaveHorizontalScrollbar = mInner.mHasHorizontalScrollbar;
bool didHaveVerticalScrollbar = mInner.mHasVerticalScrollbar;
mInner.mHasHorizontalScrollbar = aAssumeHScroll;
mInner.mHasVerticalScrollbar = aAssumeVScroll;
nsReflowStatus status;
nsresult rv = ReflowChild(mInner.mScrolledFrame, presContext, *aMetrics,
kidReflowState, 0, 0,
NS_FRAME_NO_MOVE_FRAME, status);
mInner.mHasHorizontalScrollbar = didHaveHorizontalScrollbar;
mInner.mHasVerticalScrollbar = didHaveVerticalScrollbar;
// Don't resize or position the view (if any) because we're going to resize
// it to the correct size anyway in PlaceScrollArea. Allowing it to
// resize here would size it to the natural height of the frame,
// which will usually be different from the scrollport height;
// invalidating the difference will cause unnecessary repainting.
FinishReflowChild(mInner.mScrolledFrame, presContext,
&kidReflowState, *aMetrics, 0, 0,
NS_FRAME_NO_MOVE_FRAME | NS_FRAME_NO_SIZE_VIEW);
// XXX Some frames (e.g., nsObjectFrame, nsFrameFrame, nsTextFrame) don't bother
// setting their mOverflowArea. This is wrong because every frame should
// always set mOverflowArea. In fact nsObjectFrame and nsFrameFrame don't
// support the 'outline' property because of this. Rather than fix the world
// right now, just fix up the overflow area if necessary. Note that we don't
// check HasOverflowRect() because it could be set even though the
// overflow area doesn't include the frame bounds.
aMetrics->UnionOverflowAreasWithDesiredBounds();
aState->mContentsOverflowAreas = aMetrics->mOverflowAreas;
aState->mReflowedContentsWithHScrollbar = aAssumeHScroll;
aState->mReflowedContentsWithVScrollbar = aAssumeVScroll;
return rv;
}
bool
nsHTMLScrollFrame::GuessHScrollbarNeeded(const ScrollReflowState& aState)
{
if (aState.mStyles.mHorizontal != NS_STYLE_OVERFLOW_AUTO)
// no guessing required
return aState.mStyles.mHorizontal == NS_STYLE_OVERFLOW_SCROLL;
return mInner.mHasHorizontalScrollbar;
}
bool
nsHTMLScrollFrame::GuessVScrollbarNeeded(const ScrollReflowState& aState)
{
if (aState.mStyles.mVertical != NS_STYLE_OVERFLOW_AUTO)
// no guessing required
return aState.mStyles.mVertical == NS_STYLE_OVERFLOW_SCROLL;
// If we've had at least one non-initial reflow, then just assume
// the state of the vertical scrollbar will be what we determined
// last time.
if (mInner.mHadNonInitialReflow) {
return mInner.mHasVerticalScrollbar;
}
// If this is the initial reflow, guess false because usually
// we have very little content by then.
if (InInitialReflow())
return false;
if (mInner.mIsRoot) {
nsIFrame *f = mInner.mScrolledFrame->GetFirstPrincipalChild();
if (f && f->GetType() == nsGkAtoms::svgOuterSVGFrame &&
static_cast<nsSVGOuterSVGFrame*>(f)->VerticalScrollbarNotNeeded()) {
// Common SVG case - avoid a bad guess.
return false;
}
// Assume that there will be a scrollbar; it seems to me
// that 'most pages' do have a scrollbar, and anyway, it's cheaper
// to do an extra reflow for the pages that *don't* need a
// scrollbar (because on average they will have less content).
return true;
}
// For non-viewports, just guess that we don't need a scrollbar.
// XXX I wonder if statistically this is the right idea; I'm
// basically guessing that there are a lot of overflow:auto DIVs
// that get their intrinsic size and don't overflow
return false;
}
bool
nsHTMLScrollFrame::InInitialReflow() const
{
// We're in an initial reflow if NS_FRAME_FIRST_REFLOW is set, unless we're a
// root scrollframe. In that case we want to skip this clause altogether.
// The guess here is that there are lots of overflow:auto divs out there that
// end up auto-sizing so they don't overflow, and that the root basically
// always needs a scrollbar if it did last time we loaded this page (good
// assumption, because our initial reflow is no longer synchronous).
return !mInner.mIsRoot && (GetStateBits() & NS_FRAME_FIRST_REFLOW);
}
nsresult
nsHTMLScrollFrame::ReflowContents(ScrollReflowState* aState,
const nsHTMLReflowMetrics& aDesiredSize)
{
nsHTMLReflowMetrics kidDesiredSize(aDesiredSize.mFlags);
nsresult rv = ReflowScrolledFrame(aState, GuessHScrollbarNeeded(*aState),
GuessVScrollbarNeeded(*aState), &kidDesiredSize, true);
NS_ENSURE_SUCCESS(rv, rv);
// There's an important special case ... if the child appears to fit
// in the inside-border rect (but overflows the scrollport), we
// should try laying it out without a vertical scrollbar. It will
// usually fit because making the available-width wider will not
// normally make the child taller. (The only situation I can think
// of is when you have a line containing %-width inline replaced
// elements whose percentages sum to more than 100%, so increasing
// the available width makes the line break where it was fitting
// before.) If we don't treat this case specially, then we will
// decide that showing scrollbars is OK because the content
// overflows when we're showing scrollbars and we won't try to
// remove the vertical scrollbar.
// Detecting when we enter this special case is important for when
// people design layouts that exactly fit the container "most of the
// time".
// XXX Is this check really sufficient to catch all the incremental cases
// where the ideal case doesn't have a scrollbar?
if ((aState->mReflowedContentsWithHScrollbar || aState->mReflowedContentsWithVScrollbar) &&
aState->mStyles.mVertical != NS_STYLE_OVERFLOW_SCROLL &&
aState->mStyles.mHorizontal != NS_STYLE_OVERFLOW_SCROLL) {
nsSize insideBorderSize =
ComputeInsideBorderSize(aState,
nsSize(kidDesiredSize.width, kidDesiredSize.height));
nsRect scrolledRect =
mInner.GetScrolledRectInternal(kidDesiredSize.ScrollableOverflow(),
insideBorderSize);
if (nsRect(nsPoint(0, 0), insideBorderSize).Contains(scrolledRect)) {
// Let's pretend we had no scrollbars coming in here
rv = ReflowScrolledFrame(aState, false, false,
&kidDesiredSize, false);
NS_ENSURE_SUCCESS(rv, rv);
}
}
// Try vertical scrollbar settings that leave the vertical scrollbar unchanged.
// Do this first because changing the vertical scrollbar setting is expensive,
// forcing a reflow always.
// Try leaving the horizontal scrollbar unchanged first. This will be more
// efficient.
if (TryLayout(aState, &kidDesiredSize, aState->mReflowedContentsWithHScrollbar,
aState->mReflowedContentsWithVScrollbar, false, &rv))
return NS_OK;
if (TryLayout(aState, &kidDesiredSize, !aState->mReflowedContentsWithHScrollbar,
aState->mReflowedContentsWithVScrollbar, false, &rv))
return NS_OK;
// OK, now try toggling the vertical scrollbar. The performance advantage
// of trying the status-quo horizontal scrollbar state
// does not exist here (we'll have to reflow due to the vertical scrollbar
// change), so always try no horizontal scrollbar first.
bool newVScrollbarState = !aState->mReflowedContentsWithVScrollbar;
if (TryLayout(aState, &kidDesiredSize, false, newVScrollbarState, false, &rv))
return NS_OK;
if (TryLayout(aState, &kidDesiredSize, true, newVScrollbarState, false, &rv))
return NS_OK;
// OK, we're out of ideas. Try again enabling whatever scrollbars we can
// enable and force the layout to stick even if it's inconsistent.
// This just happens sometimes.
TryLayout(aState, &kidDesiredSize,
aState->mStyles.mHorizontal != NS_STYLE_OVERFLOW_HIDDEN,
aState->mStyles.mVertical != NS_STYLE_OVERFLOW_HIDDEN,
true, &rv);
return rv;
}
void
nsHTMLScrollFrame::PlaceScrollArea(const ScrollReflowState& aState,
const nsPoint& aScrollPosition)
{
nsIFrame *scrolledFrame = mInner.mScrolledFrame;
// Set the x,y of the scrolled frame to the correct value
scrolledFrame->SetPosition(mInner.mScrollPort.TopLeft() - aScrollPosition);
nsRect scrolledArea;
// Preserve the width or height of empty rects
nsSize portSize = mInner.mScrollPort.Size();
nsRect scrolledRect =
mInner.GetScrolledRectInternal(aState.mContentsOverflowAreas.ScrollableOverflow(),
portSize);
scrolledArea.UnionRectEdges(scrolledRect,
nsRect(nsPoint(0,0), portSize));
// Store the new overflow area. Note that this changes where an outline
// of the scrolled frame would be painted, but scrolled frames can't have
// outlines (the outline would go on this scrollframe instead).
// Using FinishAndStoreOverflow is needed so the overflow rect
// gets set correctly. It also messes with the overflow rect in the
// -moz-hidden-unscrollable case, but scrolled frames can't have
// 'overflow' either.
// This needs to happen before SyncFrameViewAfterReflow so
// HasOverflowRect() will return the correct value.
nsOverflowAreas overflow(scrolledArea, scrolledArea);
scrolledFrame->FinishAndStoreOverflow(overflow,
scrolledFrame->GetSize());
// Note that making the view *exactly* the size of the scrolled area
// is critical, since the view scrolling code uses the size of the
// scrolled view to clamp scroll requests.
// Normally the scrolledFrame won't have a view but in some cases it
// might create its own.
nsContainerFrame::SyncFrameViewAfterReflow(scrolledFrame->PresContext(),
scrolledFrame,
scrolledFrame->GetView(),
scrolledArea,
0);
}
nscoord
nsHTMLScrollFrame::GetIntrinsicVScrollbarWidth(nsRenderingContext *aRenderingContext)
{
ScrollbarStyles ss = GetScrollbarStyles();
if (ss.mVertical != NS_STYLE_OVERFLOW_SCROLL || !mInner.mVScrollbarBox)
return 0;
// Don't need to worry about reflow depth here since it's
// just for scrollbars
nsBoxLayoutState bls(PresContext(), aRenderingContext, 0);
nsSize vScrollbarPrefSize(0, 0);
GetScrollbarMetrics(bls, mInner.mVScrollbarBox,
nullptr, &vScrollbarPrefSize, true);
return vScrollbarPrefSize.width;
}
/* virtual */ nscoord
nsHTMLScrollFrame::GetMinWidth(nsRenderingContext *aRenderingContext)
{
nscoord result = mInner.mScrolledFrame->GetMinWidth(aRenderingContext);
DISPLAY_MIN_WIDTH(this, result);
return result + GetIntrinsicVScrollbarWidth(aRenderingContext);
}
/* virtual */ nscoord
nsHTMLScrollFrame::GetPrefWidth(nsRenderingContext *aRenderingContext)
{
nscoord result = mInner.mScrolledFrame->GetPrefWidth(aRenderingContext);
DISPLAY_PREF_WIDTH(this, result);
return NSCoordSaturatingAdd(result, GetIntrinsicVScrollbarWidth(aRenderingContext));
}
NS_IMETHODIMP
nsHTMLScrollFrame::GetPadding(nsMargin& aMargin)
{
// Our padding hangs out on the inside of the scrollframe, but XUL doesn't
// reaize that. If we're stuck inside a XUL box, we need to claim no
// padding.
// @see also nsXULScrollFrame::GetPadding.
aMargin.SizeTo(0,0,0,0);
return NS_OK;
}
bool
nsHTMLScrollFrame::IsCollapsed()
{
// We're never collapsed in the box sense.
return false;
}
// Return the <browser> if the scrollframe is for the root frame directly
// inside a <browser>.
static nsIContent*
GetBrowserRoot(nsIContent* aContent)
{
if (aContent) {
nsIDocument* doc = aContent->GetCurrentDoc();
nsPIDOMWindow* win = doc->GetWindow();
if (win) {
nsCOMPtr<nsIContent> frameContent =
do_QueryInterface(win->GetFrameElementInternal());
if (frameContent &&
frameContent->NodeInfo()->Equals(nsGkAtoms::browser, kNameSpaceID_XUL))
return frameContent;
}
}
return nullptr;
}
NS_IMETHODIMP
nsHTMLScrollFrame::Reflow(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
{
DO_GLOBAL_REFLOW_COUNT("nsHTMLScrollFrame");
DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
mInner.HandleScrollbarStyleSwitching();
ScrollReflowState state(this, aReflowState);
// sanity check: ensure that if we have no scrollbar, we treat it
// as hidden.
if (!mInner.mVScrollbarBox || mInner.mNeverHasVerticalScrollbar)
state.mStyles.mVertical = NS_STYLE_OVERFLOW_HIDDEN;
if (!mInner.mHScrollbarBox || mInner.mNeverHasHorizontalScrollbar)
state.mStyles.mHorizontal = NS_STYLE_OVERFLOW_HIDDEN;
//------------ Handle Incremental Reflow -----------------
bool reflowHScrollbar = true;
bool reflowVScrollbar = true;
bool reflowScrollCorner = true;
if (!aReflowState.ShouldReflowAllKids()) {
#define NEEDS_REFLOW(frame_) \
((frame_) && NS_SUBTREE_DIRTY(frame_))
reflowHScrollbar = NEEDS_REFLOW(mInner.mHScrollbarBox);
reflowVScrollbar = NEEDS_REFLOW(mInner.mVScrollbarBox);
reflowScrollCorner = NEEDS_REFLOW(mInner.mScrollCornerBox) ||
NEEDS_REFLOW(mInner.mResizerBox);
#undef NEEDS_REFLOW
}
if (mInner.mIsRoot) {
mInner.mCollapsedResizer = true;
nsIContent* browserRoot = GetBrowserRoot(mContent);
if (browserRoot) {
bool showResizer = browserRoot->HasAttr(kNameSpaceID_None, nsGkAtoms::showresizer);
reflowScrollCorner = showResizer == mInner.mCollapsedResizer;
mInner.mCollapsedResizer = !showResizer;
}
}
nsRect oldScrollAreaBounds = mInner.mScrollPort;
nsRect oldScrolledAreaBounds =
mInner.mScrolledFrame->GetScrollableOverflowRectRelativeToParent();
nsPoint oldScrollPosition = mInner.GetScrollPosition();
state.mComputedBorder = aReflowState.mComputedBorderPadding -
aReflowState.mComputedPadding;
nsresult rv = ReflowContents(&state, aDesiredSize);
if (NS_FAILED(rv))
return rv;
// Restore the old scroll position, for now, even if that's not valid anymore
// because we changed size. We'll fix it up in a post-reflow callback, because
// our current size may only be temporary (e.g. we're compute XUL desired sizes).
PlaceScrollArea(state, oldScrollPosition);
if (!mInner.mPostedReflowCallback) {
// Make sure we'll try scrolling to restored position
PresContext()->PresShell()->PostReflowCallback(&mInner);
mInner.mPostedReflowCallback = true;
}
bool didHaveHScrollbar = mInner.mHasHorizontalScrollbar;
bool didHaveVScrollbar = mInner.mHasVerticalScrollbar;
mInner.mHasHorizontalScrollbar = state.mShowHScrollbar;
mInner.mHasVerticalScrollbar = state.mShowVScrollbar;
nsRect newScrollAreaBounds = mInner.mScrollPort;
nsRect newScrolledAreaBounds =
mInner.mScrolledFrame->GetScrollableOverflowRectRelativeToParent();
if (mInner.mSkippedScrollbarLayout ||
reflowHScrollbar || reflowVScrollbar || reflowScrollCorner ||
(GetStateBits() & NS_FRAME_IS_DIRTY) ||
didHaveHScrollbar != state.mShowHScrollbar ||
didHaveVScrollbar != state.mShowVScrollbar ||
!oldScrollAreaBounds.IsEqualEdges(newScrollAreaBounds) ||
!oldScrolledAreaBounds.IsEqualEdges(newScrolledAreaBounds)) {
if (!mInner.mSupppressScrollbarUpdate) {
mInner.mSkippedScrollbarLayout = false;
mInner.SetScrollbarVisibility(mInner.mHScrollbarBox, state.mShowHScrollbar);
mInner.SetScrollbarVisibility(mInner.mVScrollbarBox, state.mShowVScrollbar);
// place and reflow scrollbars
nsRect insideBorderArea =
nsRect(nsPoint(state.mComputedBorder.left, state.mComputedBorder.top),
state.mInsideBorderSize);
mInner.LayoutScrollbars(state.mBoxState, insideBorderArea,
oldScrollAreaBounds);
} else {
mInner.mSkippedScrollbarLayout = true;
}
}
aDesiredSize.width = state.mInsideBorderSize.width +
state.mComputedBorder.LeftRight();
aDesiredSize.height = state.mInsideBorderSize.height +
state.mComputedBorder.TopBottom();
aDesiredSize.SetOverflowAreasToDesiredBounds();
if (mInner.IsIgnoringViewportClipping()) {
aDesiredSize.mOverflowAreas.UnionWith(
state.mContentsOverflowAreas + mInner.mScrolledFrame->GetPosition());
}
mInner.UpdateSticky();
FinishReflowWithAbsoluteFrames(aPresContext, aDesiredSize, aReflowState, aStatus);
if (!InInitialReflow() && !mInner.mHadNonInitialReflow) {
mInner.mHadNonInitialReflow = true;
}
if (mInner.mIsRoot && !oldScrolledAreaBounds.IsEqualEdges(newScrolledAreaBounds)) {
mInner.PostScrolledAreaEvent();
}
aStatus = NS_FRAME_COMPLETE;
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
mInner.PostOverflowEvent();
return rv;
}
////////////////////////////////////////////////////////////////////////////////
#ifdef DEBUG
NS_IMETHODIMP
nsHTMLScrollFrame::GetFrameName(nsAString& aResult) const
{
return MakeFrameName(NS_LITERAL_STRING("HTMLScroll"), aResult);
}
#endif
#ifdef ACCESSIBILITY
a11y::AccType
nsHTMLScrollFrame::AccessibleType()
{
// Create an accessible regardless of focusable state because the state can be
// changed during frame life cycle without any notifications to accessibility.
if (mContent->IsRootOfNativeAnonymousSubtree() ||
GetScrollbarStyles() ==
ScrollbarStyles(NS_STYLE_OVERFLOW_HIDDEN, NS_STYLE_OVERFLOW_HIDDEN) ) {
return a11y::eNoType;
}
return a11y::eHyperTextType;
}
#endif
NS_QUERYFRAME_HEAD(nsHTMLScrollFrame)
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
NS_QUERYFRAME_ENTRY(nsIScrollbarOwner)
NS_QUERYFRAME_ENTRY(nsIScrollableFrame)
NS_QUERYFRAME_ENTRY(nsIStatefulFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
//----------nsXULScrollFrame-------------------------------------------
nsIFrame*
NS_NewXULScrollFrame(nsIPresShell* aPresShell, nsStyleContext* aContext,
bool aIsRoot, bool aClipAllDescendants)
{
return new (aPresShell) nsXULScrollFrame(aPresShell, aContext, aIsRoot,
aClipAllDescendants);
}
NS_IMPL_FRAMEARENA_HELPERS(nsXULScrollFrame)
nsXULScrollFrame::nsXULScrollFrame(nsIPresShell* aShell, nsStyleContext* aContext,
bool aIsRoot, bool aClipAllDescendants)
: nsBoxFrame(aShell, aContext, aIsRoot),
mInner(ALLOW_THIS_IN_INITIALIZER_LIST(this), aIsRoot)
{
SetLayoutManager(nullptr);
mInner.mClipAllDescendants = aClipAllDescendants;
}
void
nsXULScrollFrame::ScrollbarActivityStarted() const
{
if (mInner.mScrollbarActivity) {
mInner.mScrollbarActivity->ActivityStarted();
}
}
void
nsXULScrollFrame::ScrollbarActivityStopped() const
{
if (mInner.mScrollbarActivity) {
mInner.mScrollbarActivity->ActivityStopped();
}
}
nsMargin
nsGfxScrollFrameInner::GetDesiredScrollbarSizes(nsBoxLayoutState* aState)
{
NS_ASSERTION(aState && aState->GetRenderingContext(),
"Must have rendering context in layout state for size "
"computations");
nsMargin result(0, 0, 0, 0);
if (mVScrollbarBox) {
nsSize size = mVScrollbarBox->GetPrefSize(*aState);
nsBox::AddMargin(mVScrollbarBox, size);
if (IsScrollbarOnRight())
result.left = size.width;
else
result.right = size.width;
}
if (mHScrollbarBox) {
nsSize size = mHScrollbarBox->GetPrefSize(*aState);
nsBox::AddMargin(mHScrollbarBox, size);
// We don't currently support any scripts that would require a scrollbar
// at the top. (Are there any?)
result.bottom = size.height;
}
return result;
}
nscoord
nsGfxScrollFrameInner::GetNondisappearingScrollbarWidth(nsBoxLayoutState* aState)
{
NS_ASSERTION(aState && aState->GetRenderingContext(),
"Must have rendering context in layout state for size "
"computations");
if (LookAndFeel::GetInt(LookAndFeel::eIntID_UseOverlayScrollbars) != 0) {
// We're using overlay scrollbars, so we need to get the width that
// non-disappearing scrollbars would have.
nsITheme* theme = aState->PresContext()->GetTheme();
if (theme &&
theme->ThemeSupportsWidget(aState->PresContext(),
mVScrollbarBox,
NS_THEME_SCROLLBAR_NON_DISAPPEARING)) {
nsIntSize size;
nsRenderingContext* rendContext = aState->GetRenderingContext();
if (rendContext) {
bool canOverride = true;
theme->GetMinimumWidgetSize(rendContext,
mVScrollbarBox,
NS_THEME_SCROLLBAR_NON_DISAPPEARING,
&size,
&canOverride);
if (size.width) {
return aState->PresContext()->DevPixelsToAppUnits(size.width);
}
}
}
}
return GetDesiredScrollbarSizes(aState).LeftRight();
}
void