forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsContainerFrame.cpp
2009 lines (1785 loc) · 73.8 KB
/
nsContainerFrame.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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/* base class #1 for rendering objects that have child lists */
#include "nsContainerFrame.h"
#include "mozilla/ComputedStyle.h"
#include "mozilla/PresShell.h"
#include "mozilla/dom/HTMLSummaryElement.h"
#include "nsAbsoluteContainingBlock.h"
#include "nsAttrValue.h"
#include "nsAttrValueInlines.h"
#include "nsFlexContainerFrame.h"
#include "mozilla/dom/Document.h"
#include "nsPresContext.h"
#include "nsRect.h"
#include "nsPoint.h"
#include "nsStyleConsts.h"
#include "nsView.h"
#include "nsCOMPtr.h"
#include "nsGkAtoms.h"
#include "nsViewManager.h"
#include "nsIWidget.h"
#include "nsCSSRendering.h"
#include "nsError.h"
#include "nsDisplayList.h"
#include "nsIBaseWindow.h"
#include "nsBoxLayoutState.h"
#include "nsCSSFrameConstructor.h"
#include "nsBlockFrame.h"
#include "nsBulletFrame.h"
#include "nsPlaceholderFrame.h"
#include "mozilla/AutoRestore.h"
#include "nsIFrameInlines.h"
#include "nsPrintfCString.h"
#include <algorithm>
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::layout;
nsContainerFrame::~nsContainerFrame() {}
NS_QUERYFRAME_HEAD(nsContainerFrame)
NS_QUERYFRAME_ENTRY(nsContainerFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsSplittableFrame)
void nsContainerFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
nsIFrame* aPrevInFlow) {
nsSplittableFrame::Init(aContent, aParent, aPrevInFlow);
if (aPrevInFlow) {
// Make sure we copy bits from our prev-in-flow that will affect
// us. A continuation for a container frame needs to know if it
// has a child with a view so that we'll properly reposition it.
if (aPrevInFlow->GetStateBits() & NS_FRAME_HAS_CHILD_WITH_VIEW)
AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
}
}
void nsContainerFrame::SetInitialChildList(ChildListID aListID,
nsFrameList& aChildList) {
#ifdef DEBUG
nsFrame::VerifyDirtyBitSet(aChildList);
for (nsIFrame* f : aChildList) {
MOZ_ASSERT(f->GetParent() == this, "Unexpected parent");
}
#endif
if (aListID == kPrincipalList) {
MOZ_ASSERT(mFrames.IsEmpty(),
"unexpected second call to SetInitialChildList");
mFrames.SetFrames(aChildList);
} else if (aListID == kBackdropList) {
MOZ_ASSERT(StyleDisplay()->mTopLayer != NS_STYLE_TOP_LAYER_NONE,
"Only top layer frames should have backdrop");
MOZ_ASSERT(GetStateBits() & NS_FRAME_OUT_OF_FLOW,
"Top layer frames should be out-of-flow");
MOZ_ASSERT(!GetProperty(BackdropProperty()),
"We shouldn't have setup backdrop frame list before");
#ifdef DEBUG
{
nsIFrame* placeholder = aChildList.FirstChild();
MOZ_ASSERT(aChildList.OnlyChild(), "Should have only one backdrop");
MOZ_ASSERT(placeholder->IsPlaceholderFrame(),
"The frame to be stored should be a placeholder");
MOZ_ASSERT(static_cast<nsPlaceholderFrame*>(placeholder)
->GetOutOfFlowFrame()
->IsBackdropFrame(),
"The placeholder should points to a backdrop frame");
}
#endif
nsFrameList* list = new (PresShell()) nsFrameList(aChildList);
SetProperty(BackdropProperty(), list);
} else {
MOZ_ASSERT_UNREACHABLE("Unexpected child list");
}
}
void nsContainerFrame::AppendFrames(ChildListID aListID,
nsFrameList& aFrameList) {
MOZ_ASSERT(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
"unexpected child list");
if (MOZ_UNLIKELY(aFrameList.IsEmpty())) {
return;
}
DrainSelfOverflowList(); // ensure the last frame is in mFrames
mFrames.AppendFrames(this, aFrameList);
if (aListID != kNoReflowPrincipalList) {
PresShell()->FrameNeedsReflow(this, nsIPresShell::eTreeChange,
NS_FRAME_HAS_DIRTY_CHILDREN);
}
}
void nsContainerFrame::InsertFrames(ChildListID aListID, nsIFrame* aPrevFrame,
nsFrameList& aFrameList) {
MOZ_ASSERT(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
"unexpected child list");
NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == this,
"inserting after sibling frame with different parent");
if (MOZ_UNLIKELY(aFrameList.IsEmpty())) {
return;
}
DrainSelfOverflowList(); // ensure aPrevFrame is in mFrames
mFrames.InsertFrames(this, aPrevFrame, aFrameList);
if (aListID != kNoReflowPrincipalList) {
PresShell()->FrameNeedsReflow(this, nsIPresShell::eTreeChange,
NS_FRAME_HAS_DIRTY_CHILDREN);
}
}
void nsContainerFrame::RemoveFrame(ChildListID aListID, nsIFrame* aOldFrame) {
MOZ_ASSERT(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
"unexpected child list");
AutoTArray<nsIFrame*, 10> continuations;
{
nsIFrame* continuation = aOldFrame;
while (continuation) {
continuations.AppendElement(continuation);
continuation = continuation->GetNextContinuation();
}
}
nsIPresShell* shell = PresShell();
nsContainerFrame* lastParent = nullptr;
// Loop and destroy aOldFrame and all of its continuations.
//
// Request a reflow on the parent frames involved unless we were explicitly
// told not to (kNoReflowPrincipalList).
const bool generateReflowCommand = (kNoReflowPrincipalList != aListID);
for (nsIFrame* continuation : Reversed(continuations)) {
nsContainerFrame* parent = continuation->GetParent();
// Please note that 'parent' may not actually be where 'continuation' lives.
// We really MUST use StealFrame() and nothing else here.
// @see nsInlineFrame::StealFrame for details.
parent->StealFrame(continuation);
continuation->Destroy();
if (generateReflowCommand && parent != lastParent) {
shell->FrameNeedsReflow(parent, nsIPresShell::eTreeChange,
NS_FRAME_HAS_DIRTY_CHILDREN);
lastParent = parent;
}
}
}
void nsContainerFrame::DestroyAbsoluteFrames(
nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData) {
if (IsAbsoluteContainer()) {
GetAbsoluteContainingBlock()->DestroyFrames(this, aDestructRoot,
aPostDestroyData);
MarkAsNotAbsoluteContainingBlock();
}
}
void nsContainerFrame::SafelyDestroyFrameListProp(
nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData,
nsIPresShell* aPresShell, FrameListPropertyDescriptor aProp) {
// Note that the last frame can be removed through another route and thus
// delete the property -- that's why we fetch the property again before
// removing each frame rather than fetching it once and iterating the list.
while (nsFrameList* frameList = GetProperty(aProp)) {
nsIFrame* frame = frameList->RemoveFirstChild();
if (MOZ_LIKELY(frame)) {
frame->DestroyFrom(aDestructRoot, aPostDestroyData);
} else {
RemoveProperty(aProp);
frameList->Delete(aPresShell);
return;
}
}
}
void nsContainerFrame::DestroyFrom(nsIFrame* aDestructRoot,
PostDestroyData& aPostDestroyData) {
// Prevent event dispatch during destruction.
if (HasView()) {
GetView()->SetFrame(nullptr);
}
DestroyAbsoluteFrames(aDestructRoot, aPostDestroyData);
// Destroy frames on the principal child list.
mFrames.DestroyFramesFrom(aDestructRoot, aPostDestroyData);
// If we have any IB split siblings, clear their references to us.
if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
// Delete previous sibling's reference to me.
nsIFrame* prevSib = GetProperty(nsIFrame::IBSplitPrevSibling());
if (prevSib) {
NS_WARNING_ASSERTION(
this == prevSib->GetProperty(nsIFrame::IBSplitSibling()),
"IB sibling chain is inconsistent");
prevSib->DeleteProperty(nsIFrame::IBSplitSibling());
}
// Delete next sibling's reference to me.
nsIFrame* nextSib = GetProperty(nsIFrame::IBSplitSibling());
if (nextSib) {
NS_WARNING_ASSERTION(
this == nextSib->GetProperty(nsIFrame::IBSplitPrevSibling()),
"IB sibling chain is inconsistent");
nextSib->DeleteProperty(nsIFrame::IBSplitPrevSibling());
}
#ifdef DEBUG
// This is just so we can assert it's not set in nsFrame::DestroyFrom.
RemoveStateBits(NS_FRAME_PART_OF_IBSPLIT);
#endif
}
if (MOZ_UNLIKELY(!mProperties.IsEmpty())) {
using T = mozilla::FrameProperties::UntypedDescriptor;
bool hasO = false, hasOC = false, hasEOC = false, hasBackdrop = false;
mProperties.ForEach([&](const T& aProp, void*) {
if (aProp == OverflowProperty()) {
hasO = true;
} else if (aProp == OverflowContainersProperty()) {
hasOC = true;
} else if (aProp == ExcessOverflowContainersProperty()) {
hasEOC = true;
} else if (aProp == BackdropProperty()) {
hasBackdrop = true;
}
return true;
});
// Destroy frames on the auxiliary frame lists and delete the lists.
nsPresContext* pc = PresContext();
mozilla::PresShell* presShell = pc->PresShell();
if (hasO) {
SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
OverflowProperty());
}
MOZ_ASSERT(
IsFrameOfType(eCanContainOverflowContainers) || !(hasOC || hasEOC),
"this type of frame shouldn't have overflow containers");
if (hasOC) {
SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
OverflowContainersProperty());
}
if (hasEOC) {
SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
ExcessOverflowContainersProperty());
}
MOZ_ASSERT(!GetProperty(BackdropProperty()) ||
StyleDisplay()->mTopLayer != NS_STYLE_TOP_LAYER_NONE,
"only top layer frame may have backdrop");
if (hasBackdrop) {
SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
BackdropProperty());
}
}
nsSplittableFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
}
/////////////////////////////////////////////////////////////////////////////
// Child frame enumeration
const nsFrameList& nsContainerFrame::GetChildList(ChildListID aListID) const {
// We only know about the principal child list, the overflow lists,
// and the backdrop list.
switch (aListID) {
case kPrincipalList:
return mFrames;
case kOverflowList: {
nsFrameList* list = GetOverflowFrames();
return list ? *list : nsFrameList::EmptyList();
}
case kOverflowContainersList: {
nsFrameList* list = GetPropTableFrames(OverflowContainersProperty());
return list ? *list : nsFrameList::EmptyList();
}
case kExcessOverflowContainersList: {
nsFrameList* list =
GetPropTableFrames(ExcessOverflowContainersProperty());
return list ? *list : nsFrameList::EmptyList();
}
case kBackdropList: {
nsFrameList* list = GetPropTableFrames(BackdropProperty());
return list ? *list : nsFrameList::EmptyList();
}
default:
return nsSplittableFrame::GetChildList(aListID);
}
}
void nsContainerFrame::GetChildLists(nsTArray<ChildList>* aLists) const {
mFrames.AppendIfNonempty(aLists, kPrincipalList);
using T = mozilla::FrameProperties::UntypedDescriptor;
mProperties.ForEach([this, aLists](const T& aProp, void* aValue) {
typedef const nsFrameList* L;
if (aProp == OverflowProperty()) {
L(aValue)->AppendIfNonempty(aLists, kOverflowList);
} else if (aProp == OverflowContainersProperty()) {
MOZ_ASSERT(IsFrameOfType(nsIFrame::eCanContainOverflowContainers),
"found unexpected OverflowContainersProperty");
Unused << this; // silence clang -Wunused-lambda-capture in opt builds
L(aValue)->AppendIfNonempty(aLists, kOverflowContainersList);
} else if (aProp == ExcessOverflowContainersProperty()) {
MOZ_ASSERT(IsFrameOfType(nsIFrame::eCanContainOverflowContainers),
"found unexpected ExcessOverflowContainersProperty");
Unused << this; // silence clang -Wunused-lambda-capture in opt builds
L(aValue)->AppendIfNonempty(aLists, kExcessOverflowContainersList);
} else if (aProp == BackdropProperty()) {
L(aValue)->AppendIfNonempty(aLists, kBackdropList);
}
return true;
});
nsSplittableFrame::GetChildLists(aLists);
}
/////////////////////////////////////////////////////////////////////////////
// Painting/Events
void nsContainerFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsDisplayListSet& aLists) {
DisplayBorderBackgroundOutline(aBuilder, aLists);
BuildDisplayListForNonBlockChildren(aBuilder, aLists);
}
void nsContainerFrame::BuildDisplayListForNonBlockChildren(
nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists,
uint32_t aFlags) {
nsIFrame* kid = mFrames.FirstChild();
// Put each child's background directly onto the content list
nsDisplayListSet set(aLists, aLists.Content());
// The children should be in content order
while (kid) {
BuildDisplayListForChild(aBuilder, kid, set, aFlags);
kid = kid->GetNextSibling();
}
}
/* virtual */
void nsContainerFrame::ChildIsDirty(nsIFrame* aChild) {
NS_ASSERTION(NS_SUBTREE_DIRTY(aChild), "child isn't actually dirty");
AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
}
nsIFrame::FrameSearchResult nsContainerFrame::PeekOffsetNoAmount(
bool aForward, int32_t* aOffset) {
NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
// Don't allow the caret to stay in an empty (leaf) container frame.
return CONTINUE_EMPTY;
}
nsIFrame::FrameSearchResult nsContainerFrame::PeekOffsetCharacter(
bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
// Don't allow the caret to stay in an empty (leaf) container frame.
return CONTINUE_EMPTY;
}
/////////////////////////////////////////////////////////////////////////////
// Helper member functions
/**
* Position the view associated with |aKidFrame|, if there is one. A
* container frame should call this method after positioning a frame,
* but before |Reflow|.
*/
void nsContainerFrame::PositionFrameView(nsIFrame* aKidFrame) {
nsIFrame* parentFrame = aKidFrame->GetParent();
if (!aKidFrame->HasView() || !parentFrame) return;
nsView* view = aKidFrame->GetView();
nsViewManager* vm = view->GetViewManager();
nsPoint pt;
nsView* ancestorView = parentFrame->GetClosestView(&pt);
if (ancestorView != view->GetParent()) {
NS_ASSERTION(ancestorView == view->GetParent()->GetParent(),
"Allowed only one anonymous view between frames");
// parentFrame is responsible for positioning aKidFrame's view
// explicitly
return;
}
pt += aKidFrame->GetPosition();
vm->MoveViewTo(view, pt.x, pt.y);
}
nsresult nsContainerFrame::ReparentFrameView(nsIFrame* aChildFrame,
nsIFrame* aOldParentFrame,
nsIFrame* aNewParentFrame) {
MOZ_ASSERT(aChildFrame, "null child frame pointer");
MOZ_ASSERT(aOldParentFrame, "null old parent frame pointer");
MOZ_ASSERT(aNewParentFrame, "null new parent frame pointer");
MOZ_ASSERT(aOldParentFrame != aNewParentFrame,
"same old and new parent frame");
// See if either the old parent frame or the new parent frame have a view
while (!aOldParentFrame->HasView() && !aNewParentFrame->HasView()) {
// Walk up both the old parent frame and the new parent frame nodes
// stopping when we either find a common parent or views for one
// or both of the frames.
//
// This works well in the common case where we push/pull and the old parent
// frame and the new parent frame are part of the same flow. They will
// typically be the same distance (height wise) from the
aOldParentFrame = aOldParentFrame->GetParent();
aNewParentFrame = aNewParentFrame->GetParent();
// We should never walk all the way to the root frame without finding
// a view
NS_ASSERTION(aOldParentFrame && aNewParentFrame, "didn't find view");
// See if we reached a common ancestor
if (aOldParentFrame == aNewParentFrame) {
break;
}
}
// See if we found a common parent frame
if (aOldParentFrame == aNewParentFrame) {
// We found a common parent and there are no views between the old parent
// and the common parent or the new parent frame and the common parent.
// Because neither the old parent frame nor the new parent frame have views,
// then any child views don't need reparenting
return NS_OK;
}
// We found views for one or both of the ancestor frames before we
// found a common ancestor.
nsView* oldParentView = aOldParentFrame->GetClosestView();
nsView* newParentView = aNewParentFrame->GetClosestView();
// See if the old parent frame and the new parent frame are in the
// same view sub-hierarchy. If they are then we don't have to do
// anything
if (oldParentView != newParentView) {
// They're not so we need to reparent any child views
aChildFrame->ReparentFrameViewTo(oldParentView->GetViewManager(),
newParentView, oldParentView);
}
return NS_OK;
}
nsresult nsContainerFrame::ReparentFrameViewList(
const nsFrameList& aChildFrameList, nsIFrame* aOldParentFrame,
nsIFrame* aNewParentFrame) {
MOZ_ASSERT(aChildFrameList.NotEmpty(), "empty child frame list");
MOZ_ASSERT(aOldParentFrame, "null old parent frame pointer");
MOZ_ASSERT(aNewParentFrame, "null new parent frame pointer");
MOZ_ASSERT(aOldParentFrame != aNewParentFrame,
"same old and new parent frame");
// See if either the old parent frame or the new parent frame have a view
while (!aOldParentFrame->HasView() && !aNewParentFrame->HasView()) {
// Walk up both the old parent frame and the new parent frame nodes
// stopping when we either find a common parent or views for one
// or both of the frames.
//
// This works well in the common case where we push/pull and the old parent
// frame and the new parent frame are part of the same flow. They will
// typically be the same distance (height wise) from the
aOldParentFrame = aOldParentFrame->GetParent();
aNewParentFrame = aNewParentFrame->GetParent();
// We should never walk all the way to the root frame without finding
// a view
NS_ASSERTION(aOldParentFrame && aNewParentFrame, "didn't find view");
// See if we reached a common ancestor
if (aOldParentFrame == aNewParentFrame) {
break;
}
}
// See if we found a common parent frame
if (aOldParentFrame == aNewParentFrame) {
// We found a common parent and there are no views between the old parent
// and the common parent or the new parent frame and the common parent.
// Because neither the old parent frame nor the new parent frame have views,
// then any child views don't need reparenting
return NS_OK;
}
// We found views for one or both of the ancestor frames before we
// found a common ancestor.
nsView* oldParentView = aOldParentFrame->GetClosestView();
nsView* newParentView = aNewParentFrame->GetClosestView();
// See if the old parent frame and the new parent frame are in the
// same view sub-hierarchy. If they are then we don't have to do
// anything
if (oldParentView != newParentView) {
nsViewManager* viewManager = oldParentView->GetViewManager();
// They're not so we need to reparent any child views
for (nsFrameList::Enumerator e(aChildFrameList); !e.AtEnd(); e.Next()) {
e.get()->ReparentFrameViewTo(viewManager, newParentView, oldParentView);
}
}
return NS_OK;
}
static nsIWidget* GetPresContextContainerWidget(nsPresContext* aPresContext) {
nsCOMPtr<nsISupports> container = aPresContext->Document()->GetContainer();
nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(container);
if (!baseWindow) return nullptr;
nsCOMPtr<nsIWidget> mainWidget;
baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
return mainWidget;
}
static bool IsTopLevelWidget(nsIWidget* aWidget) {
nsWindowType windowType = aWidget->WindowType();
return windowType == eWindowType_toplevel ||
windowType == eWindowType_dialog || windowType == eWindowType_popup ||
windowType == eWindowType_sheet;
}
void nsContainerFrame::SyncWindowProperties(nsPresContext* aPresContext,
nsIFrame* aFrame, nsView* aView,
gfxContext* aRC, uint32_t aFlags) {
#ifdef MOZ_XUL
if (!aView || !nsCSSRendering::IsCanvasFrame(aFrame) || !aView->HasWidget())
return;
nsCOMPtr<nsIWidget> windowWidget =
GetPresContextContainerWidget(aPresContext);
if (!windowWidget || !IsTopLevelWidget(windowWidget)) return;
nsViewManager* vm = aView->GetViewManager();
nsView* rootView = vm->GetRootView();
if (aView != rootView) return;
Element* rootElement = aPresContext->Document()->GetRootElement();
if (!rootElement || !rootElement->IsXULElement()) {
// Scrollframes use native widgets which don't work well with
// translucent windows, at least in Windows XP. So if the document
// has a root scrollrame it's useless to try to make it transparent,
// we'll just get something broken.
// nsCSSFrameConstructor::ConstructRootFrame constructs root
// scrollframes whenever the root element is not a XUL element, so
// we test for that here. We can't just call
// presShell->GetRootScrollFrame() since that might not have
// been constructed yet.
// We can change this to allow translucent toplevel HTML documents
// (e.g. to do something like Dashboard widgets), once we
// have broad support for translucent scrolled documents, but be
// careful because apparently some Firefox extensions expect
// openDialog("something.html") to produce an opaque window
// even if the HTML doesn't have a background-color set.
return;
}
nsIFrame* rootFrame =
aPresContext->PresShell()->FrameConstructor()->GetRootElementStyleFrame();
if (!rootFrame) return;
if (aFlags & SET_ASYNC) {
aView->SetNeedsWindowPropertiesSync();
return;
}
RefPtr<nsPresContext> kungFuDeathGrip(aPresContext);
AutoWeakFrame weak(rootFrame);
nsTransparencyMode mode =
nsLayoutUtils::GetFrameTransparency(aFrame, rootFrame);
int32_t shadow = rootFrame->StyleUIReset()->mWindowShadow;
nsCOMPtr<nsIWidget> viewWidget = aView->GetWidget();
viewWidget->SetTransparencyMode(mode);
windowWidget->SetWindowShadowStyle(shadow);
if (!aRC) return;
if (!weak.IsAlive()) {
return;
}
nsBoxLayoutState aState(aPresContext, aRC);
nsSize minSize = rootFrame->GetXULMinSize(aState);
nsSize maxSize = rootFrame->GetXULMaxSize(aState);
SetSizeConstraints(aPresContext, windowWidget, minSize, maxSize);
#endif
}
void nsContainerFrame::SetSizeConstraints(nsPresContext* aPresContext,
nsIWidget* aWidget,
const nsSize& aMinSize,
const nsSize& aMaxSize) {
LayoutDeviceIntSize devMinSize(
aPresContext->AppUnitsToDevPixels(aMinSize.width),
aPresContext->AppUnitsToDevPixels(aMinSize.height));
LayoutDeviceIntSize devMaxSize(
aMaxSize.width == NS_INTRINSICSIZE
? NS_MAXSIZE
: aPresContext->AppUnitsToDevPixels(aMaxSize.width),
aMaxSize.height == NS_INTRINSICSIZE
? NS_MAXSIZE
: aPresContext->AppUnitsToDevPixels(aMaxSize.height));
// MinSize has a priority over MaxSize
if (devMinSize.width > devMaxSize.width) devMaxSize.width = devMinSize.width;
if (devMinSize.height > devMaxSize.height)
devMaxSize.height = devMinSize.height;
widget::SizeConstraints constraints(devMinSize, devMaxSize);
// The sizes are in inner window sizes, so convert them into outer window
// sizes. Use a size of (200, 200) as only the difference between the inner
// and outer size is needed.
LayoutDeviceIntSize windowSize =
aWidget->ClientToWindowSize(LayoutDeviceIntSize(200, 200));
if (constraints.mMinSize.width)
constraints.mMinSize.width += windowSize.width - 200;
if (constraints.mMinSize.height)
constraints.mMinSize.height += windowSize.height - 200;
if (constraints.mMaxSize.width != NS_MAXSIZE)
constraints.mMaxSize.width += windowSize.width - 200;
if (constraints.mMaxSize.height != NS_MAXSIZE)
constraints.mMaxSize.height += windowSize.height - 200;
aWidget->SetSizeConstraints(constraints);
}
void nsContainerFrame::SyncFrameViewAfterReflow(
nsPresContext* aPresContext, nsIFrame* aFrame, nsView* aView,
const nsRect& aVisualOverflowArea, uint32_t aFlags) {
if (!aView) {
return;
}
// Make sure the view is sized and positioned correctly
if (0 == (aFlags & NS_FRAME_NO_MOVE_VIEW)) {
PositionFrameView(aFrame);
}
if (0 == (aFlags & NS_FRAME_NO_SIZE_VIEW)) {
nsViewManager* vm = aView->GetViewManager();
vm->ResizeView(aView, aVisualOverflowArea, true);
}
}
static nscoord GetCoord(const LengthPercentage& aCoord, nscoord aIfNotCoord) {
if (aCoord.ConvertsToLength()) {
return aCoord.ToLength();
}
return aIfNotCoord;
}
static nscoord GetCoord(const LengthPercentageOrAuto& aCoord,
nscoord aIfNotCoord) {
if (aCoord.IsAuto()) {
return aIfNotCoord;
}
return GetCoord(aCoord.AsLengthPercentage(), aIfNotCoord);
}
void nsContainerFrame::DoInlineIntrinsicISize(
gfxContext* aRenderingContext, InlineIntrinsicISizeData* aData,
nsLayoutUtils::IntrinsicISizeType aType) {
if (GetPrevInFlow()) return; // Already added.
MOZ_ASSERT(
aType == nsLayoutUtils::MIN_ISIZE || aType == nsLayoutUtils::PREF_ISIZE,
"bad type");
WritingMode wm = GetWritingMode();
mozilla::Side startSide = wm.PhysicalSideForInlineAxis(eLogicalEdgeStart);
mozilla::Side endSide = wm.PhysicalSideForInlineAxis(eLogicalEdgeEnd);
const nsStylePadding* stylePadding = StylePadding();
const nsStyleBorder* styleBorder = StyleBorder();
const nsStyleMargin* styleMargin = StyleMargin();
// This goes at the beginning no matter how things are broken and how
// messy the bidi situations are, since per CSS2.1 section 8.6
// (implemented in bug 328168), the startSide border is always on the
// first line.
// This frame is a first-in-flow, but it might have a previous bidi
// continuation, in which case that continuation should handle the startSide
// border.
// For box-decoration-break:clone we setup clonePBM = startPBM + endPBM and
// add that to each line. For box-decoration-break:slice clonePBM is zero.
nscoord clonePBM = 0; // PBM = PaddingBorderMargin
const bool sliceBreak =
styleBorder->mBoxDecorationBreak == StyleBoxDecorationBreak::Slice;
if (!GetPrevContinuation()) {
nscoord startPBM =
// clamp negative calc() to 0
std::max(GetCoord(stylePadding->mPadding.Get(startSide), 0), 0) +
styleBorder->GetComputedBorderWidth(startSide) +
GetCoord(styleMargin->mMargin.Get(startSide), 0);
if (MOZ_LIKELY(sliceBreak)) {
aData->mCurrentLine += startPBM;
} else {
clonePBM = startPBM;
}
}
nscoord endPBM =
// clamp negative calc() to 0
std::max(GetCoord(stylePadding->mPadding.Get(endSide), 0), 0) +
styleBorder->GetComputedBorderWidth(endSide) +
GetCoord(styleMargin->mMargin.Get(endSide), 0);
if (MOZ_UNLIKELY(!sliceBreak)) {
clonePBM += endPBM;
}
const nsLineList_iterator* savedLine = aData->mLine;
nsIFrame* const savedLineContainer = aData->LineContainer();
nsContainerFrame* lastInFlow;
for (nsContainerFrame* nif = this; nif;
nif = static_cast<nsContainerFrame*>(nif->GetNextInFlow())) {
if (aData->mCurrentLine == 0) {
aData->mCurrentLine = clonePBM;
}
for (nsIFrame* kid : nif->mFrames) {
if (aType == nsLayoutUtils::MIN_ISIZE)
kid->AddInlineMinISize(aRenderingContext,
static_cast<InlineMinISizeData*>(aData));
else
kid->AddInlinePrefISize(aRenderingContext,
static_cast<InlinePrefISizeData*>(aData));
}
// After we advance to our next-in-flow, the stored line and line container
// may no longer be correct. Just forget them.
aData->mLine = nullptr;
aData->SetLineContainer(nullptr);
lastInFlow = nif;
}
aData->mLine = savedLine;
aData->SetLineContainer(savedLineContainer);
// This goes at the end no matter how things are broken and how
// messy the bidi situations are, since per CSS2.1 section 8.6
// (implemented in bug 328168), the endSide border is always on the
// last line.
// We reached the last-in-flow, but it might have a next bidi
// continuation, in which case that continuation should handle
// the endSide border.
if (MOZ_LIKELY(!lastInFlow->GetNextContinuation() && sliceBreak)) {
aData->mCurrentLine += endPBM;
}
}
/* virtual */
LogicalSize nsContainerFrame::ComputeAutoSize(
gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
nscoord aAvailableISize, const LogicalSize& aMargin,
const LogicalSize& aBorder, const LogicalSize& aPadding,
ComputeSizeFlags aFlags) {
LogicalSize result(aWM, 0xdeadbeef, NS_UNCONSTRAINEDSIZE);
nscoord availBased = aAvailableISize - aMargin.ISize(aWM) -
aBorder.ISize(aWM) - aPadding.ISize(aWM);
// replaced elements always shrink-wrap
if ((aFlags & ComputeSizeFlags::eShrinkWrap) || IsFrameOfType(eReplaced)) {
// Only bother computing our 'auto' ISize if the result will be used.
// It'll be used under two scenarios:
// - If our ISize property is itself 'auto'.
// - If we're using flex-basis in place of our ISize property (i.e. we're a
// flex item with our inline axis being the main axis), AND we have
// flex-basis:content.
const nsStylePosition* pos = StylePosition();
if (pos->ISize(aWM).IsAuto() ||
(pos->mFlexBasis.IsContent() && IsFlexItem() &&
nsFlexContainerFrame::IsItemInlineAxisMainAxis(this))) {
result.ISize(aWM) =
ShrinkWidthToFit(aRenderingContext, availBased, aFlags);
}
} else {
result.ISize(aWM) = availBased;
}
if (IsTableCaption()) {
// If we're a container for font size inflation, then shrink
// wrapping inside of us should not apply font size inflation.
AutoMaybeDisableFontInflation an(this);
WritingMode tableWM = GetParent()->GetWritingMode();
uint8_t captionSide = StyleTableBorder()->mCaptionSide;
if (aWM.IsOrthogonalTo(tableWM)) {
if (captionSide == NS_STYLE_CAPTION_SIDE_TOP ||
captionSide == NS_STYLE_CAPTION_SIDE_TOP_OUTSIDE ||
captionSide == NS_STYLE_CAPTION_SIDE_BOTTOM ||
captionSide == NS_STYLE_CAPTION_SIDE_BOTTOM_OUTSIDE) {
// For an orthogonal caption on a block-dir side of the table,
// shrink-wrap to min-isize.
result.ISize(aWM) = GetMinISize(aRenderingContext);
} else {
// An orthogonal caption on an inline-dir side of the table
// is constrained to the containing block.
nscoord pref = GetPrefISize(aRenderingContext);
if (pref > aCBSize.ISize(aWM)) {
pref = aCBSize.ISize(aWM);
}
if (pref < result.ISize(aWM)) {
result.ISize(aWM) = pref;
}
}
} else {
if (captionSide == NS_STYLE_CAPTION_SIDE_LEFT ||
captionSide == NS_STYLE_CAPTION_SIDE_RIGHT) {
result.ISize(aWM) = GetMinISize(aRenderingContext);
} else if (captionSide == NS_STYLE_CAPTION_SIDE_TOP ||
captionSide == NS_STYLE_CAPTION_SIDE_BOTTOM) {
// The outer frame constrains our available isize to the isize of
// the table. Grow if our min-isize is bigger than that, but not
// larger than the containing block isize. (It would really be nice
// to transmit that information another way, so we could grow up to
// the table's available isize, but that's harder.)
nscoord min = GetMinISize(aRenderingContext);
if (min > aCBSize.ISize(aWM)) {
min = aCBSize.ISize(aWM);
}
if (min > result.ISize(aWM)) {
result.ISize(aWM) = min;
}
}
}
}
return result;
}
void nsContainerFrame::ReflowChild(
nsIFrame* aKidFrame, nsPresContext* aPresContext,
ReflowOutput& aDesiredSize, const ReflowInput& aReflowInput,
const WritingMode& aWM, const LogicalPoint& aPos,
const nsSize& aContainerSize, uint32_t aFlags, nsReflowStatus& aStatus,
nsOverflowContinuationTracker* aTracker) {
MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow state");
if (aWM.IsVerticalRL() || (!aWM.IsVertical() && !aWM.IsBidiLTR())) {
NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
"ReflowChild with unconstrained container width!");
}
MOZ_ASSERT(aDesiredSize.VisualOverflow() == nsRect(0, 0, 0, 0) &&
aDesiredSize.ScrollableOverflow() == nsRect(0, 0, 0, 0),
"please reset the overflow areas before calling ReflowChild");
// Position the child frame and its view if requested.
if (NS_FRAME_NO_MOVE_FRAME != (aFlags & NS_FRAME_NO_MOVE_FRAME)) {
aKidFrame->SetPosition(aWM, aPos, aContainerSize);
}
if (0 == (aFlags & NS_FRAME_NO_MOVE_VIEW)) {
PositionFrameView(aKidFrame);
PositionChildViews(aKidFrame);
}
// Reflow the child frame
aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
// If the child frame is complete, delete any next-in-flows,
// but only if the NO_DELETE_NEXT_IN_FLOW flag isn't set.
if (!aStatus.IsInlineBreakBefore() && aStatus.IsFullyComplete() &&
!(aFlags & NS_FRAME_NO_DELETE_NEXT_IN_FLOW_CHILD)) {
nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow();
if (kidNextInFlow) {
// Remove all of the childs next-in-flows. Make sure that we ask
// the right parent to do the removal (it's possible that the
// parent is not this because we are executing pullup code)
nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
}
}
}
// XXX temporary: hold on to a copy of the old physical version of
// ReflowChild so that we can convert callers incrementally.
void nsContainerFrame::ReflowChild(nsIFrame* aKidFrame,
nsPresContext* aPresContext,
ReflowOutput& aDesiredSize,
const ReflowInput& aReflowInput, nscoord aX,
nscoord aY, uint32_t aFlags,
nsReflowStatus& aStatus,
nsOverflowContinuationTracker* aTracker) {
MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow state");
// Position the child frame and its view if requested.
if (NS_FRAME_NO_MOVE_FRAME != (aFlags & NS_FRAME_NO_MOVE_FRAME)) {
aKidFrame->SetPosition(nsPoint(aX, aY));
}
if (0 == (aFlags & NS_FRAME_NO_MOVE_VIEW)) {
PositionFrameView(aKidFrame);
PositionChildViews(aKidFrame);
}
// Reflow the child frame
aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
// If the child frame is complete, delete any next-in-flows,
// but only if the NO_DELETE_NEXT_IN_FLOW flag isn't set.
if (aStatus.IsFullyComplete() &&
!(aFlags & NS_FRAME_NO_DELETE_NEXT_IN_FLOW_CHILD)) {
nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow();
if (kidNextInFlow) {
// Remove all of the childs next-in-flows. Make sure that we ask
// the right parent to do the removal (it's possible that the
// parent is not this because we are executing pullup code)
nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
}
}
}
/**
* Position the views of |aFrame|'s descendants. A container frame
* should call this method if it moves a frame after |Reflow|.
*/
void nsContainerFrame::PositionChildViews(nsIFrame* aFrame) {
if (!(aFrame->GetStateBits() & NS_FRAME_HAS_CHILD_WITH_VIEW)) {
return;
}
// Recursively walk aFrame's child frames.
// Process the additional child lists, but skip the popup list as the
// view for popups is managed by the parent. Currently only nsMenuFrame
// and nsPopupSetFrame have a popupList and during layout will adjust the
// view manually to position the popup.
ChildListIterator lists(aFrame);
for (; !lists.IsDone(); lists.Next()) {
if (lists.CurrentID() == kPopupList) {
continue;
}
nsFrameList::Enumerator childFrames(lists.CurrentList());
for (; !childFrames.AtEnd(); childFrames.Next()) {
// Position the frame's view (if it has one) otherwise recursively
// process its children
nsIFrame* childFrame = childFrames.get();
if (childFrame->HasView()) {
PositionFrameView(childFrame);
} else {
PositionChildViews(childFrame);
}
}
}
}
/**
* The second half of frame reflow. Does the following:
* - sets the frame's bounds
* - sizes and positions (if requested) the frame's view. If the frame's final
* position differs from the current position and the frame itself does not
* have a view, then any child frames with views are positioned so they stay
* in sync
* - sets the view's visibility, opacity, content transparency, and clip
* - invoked the DidReflow() function
*
* Flags:
* NS_FRAME_NO_MOVE_FRAME - don't move the frame. aX and aY are ignored in this
* case. Also implies NS_FRAME_NO_MOVE_VIEW
* NS_FRAME_NO_MOVE_VIEW - don't position the frame's view. Set this if you
* don't want to automatically sync the frame and view
* NS_FRAME_NO_SIZE_VIEW - don't size the frame's view
*/
/**