forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsListControlFrame.cpp
2551 lines (2221 loc) · 81.1 KB
/
nsListControlFrame.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/. */
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsUnicharUtils.h"
#include "nsListControlFrame.h"
#include "nsCheckboxRadioFrame.h" // for COMPARE macro
#include "nsGkAtoms.h"
#include "nsComboboxControlFrame.h"
#include "nsIPresShell.h"
#include "nsIXULRuntime.h"
#include "nsFontMetrics.h"
#include "nsIScrollableFrame.h"
#include "nsCSSRendering.h"
#include "nsIDOMEventListener.h"
#include "nsLayoutUtils.h"
#include "nsDisplayList.h"
#include "nsContentUtils.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/HTMLOptGroupElement.h"
#include "mozilla/dom/HTMLOptionsCollection.h"
#include "mozilla/dom/HTMLSelectElement.h"
#include "mozilla/dom/MouseEvent.h"
#include "mozilla/dom/MouseEventBinding.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/EventStates.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/Preferences.h"
#include "mozilla/TextEvents.h"
#include <algorithm>
using namespace mozilla;
// Constants
const uint32_t kMaxDropDownRows = 20; // This matches the setting for 4.x browsers
const int32_t kNothingSelected = -1;
// Static members
nsListControlFrame * nsListControlFrame::mFocused = nullptr;
nsString * nsListControlFrame::sIncrementalString = nullptr;
// Using for incremental typing navigation
#define INCREMENTAL_SEARCH_KEYPRESS_TIME 1000
// XXX, [email protected], there are 4 definitions for the same purpose:
// nsMenuPopupFrame.h, nsListControlFrame.cpp, listbox.xml, tree.xml
// need to find a good place to put them together.
// if someone changes one, please also change the other.
DOMTimeStamp nsListControlFrame::gLastKeyTime = 0;
/******************************************************************************
* nsListEventListener
* This class is responsible for propagating events to the nsListControlFrame.
* Frames are not refcounted so they can't be used as event listeners.
*****************************************************************************/
class nsListEventListener final : public nsIDOMEventListener
{
public:
explicit nsListEventListener(nsListControlFrame *aFrame)
: mFrame(aFrame) { }
void SetFrame(nsListControlFrame *aFrame) { mFrame = aFrame; }
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMEVENTLISTENER
private:
~nsListEventListener() {}
nsListControlFrame *mFrame;
};
//---------------------------------------------------------
nsContainerFrame*
NS_NewListControlFrame(nsIPresShell* aPresShell, ComputedStyle* aStyle)
{
nsListControlFrame* it =
new (aPresShell) nsListControlFrame(aStyle);
it->AddStateBits(NS_FRAME_INDEPENDENT_SELECTION);
return it;
}
NS_IMPL_FRAMEARENA_HELPERS(nsListControlFrame)
//---------------------------------------------------------
nsListControlFrame::nsListControlFrame(ComputedStyle* aStyle)
: nsHTMLScrollFrame(aStyle, kClassID, false)
, mView(nullptr)
, mMightNeedSecondPass(false)
, mHasPendingInterruptAtStartOfReflow(false)
, mDropdownCanGrow(false)
, mForceSelection(false)
, mLastDropdownComputedBSize(NS_UNCONSTRAINEDSIZE)
{
mComboboxFrame = nullptr;
mChangesSinceDragStart = false;
mButtonDown = false;
mIsAllContentHere = false;
mIsAllFramesHere = false;
mHasBeenInitialized = false;
mNeedToReset = true;
mPostChildrenLoadedReset = false;
mControlSelectMode = false;
}
//---------------------------------------------------------
nsListControlFrame::~nsListControlFrame()
{
mComboboxFrame = nullptr;
}
static bool ShouldFireDropDownEvent() {
// We don't need to fire the event to SelectContentHelper when content-select
// is enabled.
if (nsLayoutUtils::IsContentSelectEnabled()) {
return false;
}
return (XRE_IsContentProcess() &&
Preferences::GetBool("browser.tabs.remote.desktopbehavior", false)) ||
Preferences::GetBool("dom.select_popup_in_parent.enabled", false);
}
// for Bug 47302 (remove this comment later)
void
nsListControlFrame::DestroyFrom(nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData)
{
// get the receiver interface from the browser button's content node
ENSURE_TRUE(mContent);
// Clear the frame pointer on our event listener, just in case the
// event listener can outlive the frame.
mEventListener->SetFrame(nullptr);
mContent->RemoveSystemEventListener(NS_LITERAL_STRING("keydown"),
mEventListener, false);
mContent->RemoveSystemEventListener(NS_LITERAL_STRING("keypress"),
mEventListener, false);
mContent->RemoveSystemEventListener(NS_LITERAL_STRING("mousedown"),
mEventListener, false);
mContent->RemoveSystemEventListener(NS_LITERAL_STRING("mouseup"),
mEventListener, false);
mContent->RemoveSystemEventListener(NS_LITERAL_STRING("mousemove"),
mEventListener, false);
if (ShouldFireDropDownEvent()) {
nsContentUtils::AddScriptRunner(
new AsyncEventDispatcher(mContent,
NS_LITERAL_STRING("mozhidedropdown"), true,
true));
}
nsCheckboxRadioFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
nsHTMLScrollFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
}
void
nsListControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsDisplayListSet& aLists)
{
// We allow visibility:hidden <select>s to contain visible options.
// Don't allow painting of list controls when painting is suppressed.
// XXX why do we need this here? we should never reach this. Maybe
// because these can have widgets? Hmm
if (aBuilder->IsBackgroundOnly())
return;
DO_GLOBAL_REFLOW_COUNT_DSP("nsListControlFrame");
if (IsInDropDownMode()) {
NS_ASSERTION(NS_GET_A(mLastDropdownBackstopColor) == 255,
"need an opaque backstop color");
// XXX Because we have an opaque widget and we get called to paint with
// this frame as the root of a stacking context we need make sure to draw
// some opaque color over the whole widget. (Bug 511323)
aLists.BorderBackground()->AppendToBottom(
MakeDisplayItem<nsDisplaySolidColor>(aBuilder,
this, nsRect(aBuilder->ToReferenceFrame(this), GetSize()),
mLastDropdownBackstopColor));
}
nsHTMLScrollFrame::BuildDisplayList(aBuilder, aLists);
}
/**
* This is called by the SelectsAreaFrame, which is the same
* as the frame returned by GetOptionsContainer. It's the frame which is
* scrolled by us.
* @param aPt the offset of this frame, relative to the rendering reference
* frame
*/
void nsListControlFrame::PaintFocus(DrawTarget* aDrawTarget, nsPoint aPt)
{
if (mFocused != this) return;
nsPresContext* presContext = PresContext();
nsIFrame* containerFrame = GetOptionsContainer();
if (!containerFrame) return;
nsIFrame* childframe = nullptr;
nsCOMPtr<nsIContent> focusedContent = GetCurrentOption();
if (focusedContent) {
childframe = focusedContent->GetPrimaryFrame();
}
nsRect fRect;
if (childframe) {
// get the child rect
fRect = childframe->GetRect();
// get it into our coordinates
fRect.MoveBy(childframe->GetParent()->GetOffsetTo(this));
} else {
float inflation = nsLayoutUtils::FontSizeInflationFor(this);
fRect.x = fRect.y = 0;
if (GetWritingMode().IsVertical()) {
fRect.width = GetScrollPortRect().width;
fRect.height = CalcFallbackRowBSize(inflation);
} else {
fRect.width = CalcFallbackRowBSize(inflation);
fRect.height = GetScrollPortRect().height;
}
fRect.MoveBy(containerFrame->GetOffsetTo(this));
}
fRect += aPt;
bool lastItemIsSelected = false;
HTMLOptionElement* domOpt = HTMLOptionElement::FromNodeOrNull(focusedContent);
if (domOpt) {
lastItemIsSelected = domOpt->Selected();
}
// set up back stop colors and then ask L&F service for the real colors
nscolor color =
LookAndFeel::GetColor(lastItemIsSelected ?
LookAndFeel::eColorID_WidgetSelectForeground :
LookAndFeel::eColorID_WidgetSelectBackground);
nsCSSRendering::PaintFocus(presContext, aDrawTarget, fRect, color);
}
void
nsListControlFrame::InvalidateFocus()
{
if (mFocused != this)
return;
nsIFrame* containerFrame = GetOptionsContainer();
if (containerFrame) {
containerFrame->InvalidateFrame();
}
}
NS_QUERYFRAME_HEAD(nsListControlFrame)
NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
NS_QUERYFRAME_ENTRY(nsIListControlFrame)
NS_QUERYFRAME_ENTRY(nsISelectControlFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsHTMLScrollFrame)
#ifdef ACCESSIBILITY
a11y::AccType
nsListControlFrame::AccessibleType()
{
return a11y::eHTMLSelectListType;
}
#endif
static nscoord
GetMaxOptionBSize(nsIFrame* aContainer, WritingMode aWM)
{
nscoord result = 0;
for (nsIFrame* option : aContainer->PrincipalChildList()) {
nscoord optionBSize;
if (HTMLOptGroupElement::FromNode(option->GetContent())) {
// An optgroup; drill through any scroll frame and recurse. |frame| might
// be null here though if |option| is an anonymous leaf frame of some sort.
auto frame = option->GetContentInsertionFrame();
optionBSize = frame ? GetMaxOptionBSize(frame, aWM) : 0;
} else {
// an option
optionBSize = option->BSize(aWM);
}
if (result < optionBSize)
result = optionBSize;
}
return result;
}
//-----------------------------------------------------------------
// Main Reflow for ListBox/Dropdown
//-----------------------------------------------------------------
nscoord
nsListControlFrame::CalcBSizeOfARow()
{
// Calculate the block size in our writing mode of a single row in the
// listbox or dropdown list by using the tallest thing in the subtree,
// since there may be option groups in addition to option elements,
// either of which may be visible or invisible, may use different
// fonts, etc.
int32_t blockSizeOfARow = GetMaxOptionBSize(GetOptionsContainer(),
GetWritingMode());
// Check to see if we have zero items (and optimize by checking
// blockSizeOfARow first)
if (blockSizeOfARow == 0 && GetNumberOfOptions() == 0) {
float inflation = nsLayoutUtils::FontSizeInflationFor(this);
blockSizeOfARow = CalcFallbackRowBSize(inflation);
}
return blockSizeOfARow;
}
nscoord
nsListControlFrame::GetPrefISize(gfxContext *aRenderingContext)
{
nscoord result;
DISPLAY_PREF_WIDTH(this, result);
// Always add scrollbar inline sizes to the pref-inline-size of the
// scrolled content. Combobox frames depend on this happening in the
// dropdown, and standalone listboxes are overflow:scroll so they need
// it too.
WritingMode wm = GetWritingMode();
result = GetScrolledFrame()->GetPrefISize(aRenderingContext);
LogicalMargin scrollbarSize(wm, GetDesiredScrollbarSizes(PresContext(),
aRenderingContext));
result = NSCoordSaturatingAdd(result, scrollbarSize.IStartEnd(wm));
return result;
}
nscoord
nsListControlFrame::GetMinISize(gfxContext *aRenderingContext)
{
nscoord result;
DISPLAY_MIN_WIDTH(this, result);
// Always add scrollbar inline sizes to the min-inline-size of the
// scrolled content. Combobox frames depend on this happening in the
// dropdown, and standalone listboxes are overflow:scroll so they need
// it too.
WritingMode wm = GetWritingMode();
result = GetScrolledFrame()->GetMinISize(aRenderingContext);
LogicalMargin scrollbarSize(wm, GetDesiredScrollbarSizes(PresContext(),
aRenderingContext));
result += scrollbarSize.IStartEnd(wm);
return result;
}
void
nsListControlFrame::Reflow(nsPresContext* aPresContext,
ReflowOutput& aDesiredSize,
const ReflowInput& aReflowInput,
nsReflowStatus& aStatus)
{
MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
NS_WARNING_ASSERTION(aReflowInput.ComputedISize() != NS_UNCONSTRAINEDSIZE,
"Must have a computed inline size");
SchedulePaint();
mHasPendingInterruptAtStartOfReflow = aPresContext->HasPendingInterrupt();
// If all the content and frames are here
// then initialize it before reflow
if (mIsAllContentHere && !mHasBeenInitialized) {
if (false == mIsAllFramesHere) {
CheckIfAllFramesHere();
}
if (mIsAllFramesHere && !mHasBeenInitialized) {
mHasBeenInitialized = true;
}
}
if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
nsCheckboxRadioFrame::RegUnRegAccessKey(this, true);
}
if (IsInDropDownMode()) {
ReflowAsDropdown(aPresContext, aDesiredSize, aReflowInput, aStatus);
return;
}
MarkInReflow();
/*
* Due to the fact that our intrinsic block size depends on the block
* sizes of our kids, we end up having to do two-pass reflow, in
* general -- the first pass to find the intrinsic block size and a
* second pass to reflow the scrollframe at that block size (which
* will size the scrollbars correctly, etc).
*
* Naturally, we want to avoid doing the second reflow as much as
* possible.
* We can skip it in the following cases (in all of which the first
* reflow is already happening at the right block size):
*
* - We're reflowing with a constrained computed block size -- just
* use that block size.
* - We're not dirty and have no dirty kids and shouldn't be reflowing
* all kids. In this case, our cached max block size of a child is
* not going to change.
* - We do our first reflow using our cached max block size of a
* child, then compute the new max block size and it's the same as
* the old one.
*/
bool autoBSize = (aReflowInput.ComputedBSize() == NS_UNCONSTRAINEDSIZE);
mMightNeedSecondPass = autoBSize &&
(NS_SUBTREE_DIRTY(this) || aReflowInput.ShouldReflowAllKids());
ReflowInput state(aReflowInput);
int32_t length = GetNumberOfRows();
nscoord oldBSizeOfARow = BSizeOfARow();
if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW) && autoBSize) {
// When not doing an initial reflow, and when the block size is
// auto, start off with our computed block size set to what we'd
// expect our block size to be.
nscoord computedBSize = CalcIntrinsicBSize(oldBSizeOfARow, length);
computedBSize = state.ApplyMinMaxBSize(computedBSize);
state.SetComputedBSize(computedBSize);
}
nsHTMLScrollFrame::Reflow(aPresContext, aDesiredSize, state, aStatus);
if (!mMightNeedSecondPass) {
NS_ASSERTION(!autoBSize || BSizeOfARow() == oldBSizeOfARow,
"How did our BSize of a row change if nothing was dirty?");
NS_ASSERTION(!autoBSize ||
!(GetStateBits() & NS_FRAME_FIRST_REFLOW),
"How do we not need a second pass during initial reflow at "
"auto BSize?");
NS_ASSERTION(!IsScrollbarUpdateSuppressed(),
"Shouldn't be suppressing if we don't need a second pass!");
if (!autoBSize) {
// Update our mNumDisplayRows based on our new row block size now
// that we know it. Note that if autoBSize and we landed in this
// code then we already set mNumDisplayRows in CalcIntrinsicBSize.
// Also note that we can't use BSizeOfARow() here because that
// just uses a cached value that we didn't compute.
nscoord rowBSize = CalcBSizeOfARow();
if (rowBSize == 0) {
// Just pick something
mNumDisplayRows = 1;
} else {
mNumDisplayRows = std::max(1, state.ComputedBSize() / rowBSize);
}
}
return;
}
mMightNeedSecondPass = false;
// Now see whether we need a second pass. If we do, our
// nsSelectsAreaFrame will have suppressed the scrollbar update.
if (!IsScrollbarUpdateSuppressed()) {
// All done. No need to do more reflow.
return;
}
SetSuppressScrollbarUpdate(false);
// Gotta reflow again.
// XXXbz We're just changing the block size here; do we need to dirty
// ourselves or anything like that? We might need to, per the letter
// of the reflow protocol, but things seem to work fine without it...
// Is that just an implementation detail of nsHTMLScrollFrame that
// we're depending on?
nsHTMLScrollFrame::DidReflow(aPresContext, &state);
// Now compute the block size we want to have
nscoord computedBSize = CalcIntrinsicBSize(BSizeOfARow(), length);
computedBSize = state.ApplyMinMaxBSize(computedBSize);
state.SetComputedBSize(computedBSize);
// XXXbz to make the ascent really correct, we should add our
// mComputedPadding.top to it (and subtract it from descent). Need that
// because nsGfxScrollFrame just adds in the border....
aStatus.Reset();
nsHTMLScrollFrame::Reflow(aPresContext, aDesiredSize, state, aStatus);
}
void
nsListControlFrame::ReflowAsDropdown(nsPresContext* aPresContext,
ReflowOutput& aDesiredSize,
const ReflowInput& aReflowInput,
nsReflowStatus& aStatus)
{
MOZ_ASSERT(aReflowInput.ComputedBSize() == NS_UNCONSTRAINEDSIZE,
"We should not have a computed block size here!");
mMightNeedSecondPass = NS_SUBTREE_DIRTY(this) ||
aReflowInput.ShouldReflowAllKids();
WritingMode wm = aReflowInput.GetWritingMode();
#ifdef DEBUG
nscoord oldBSizeOfARow = BSizeOfARow();
nscoord oldVisibleBSize = (GetStateBits() & NS_FRAME_FIRST_REFLOW) ?
NS_UNCONSTRAINEDSIZE : GetScrolledFrame()->BSize(wm);
#endif
ReflowInput state(aReflowInput);
if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
// When not doing an initial reflow, and when the block size is
// auto, start off with our computed block size set to what we'd
// expect our block size to be.
// Note: At this point, mLastDropdownComputedBSize can be
// NS_UNCONSTRAINEDSIZE in cases when last time we didn't have to
// constrain the block size. That's fine; just do the same thing as
// last time.
state.SetComputedBSize(mLastDropdownComputedBSize);
}
nsHTMLScrollFrame::Reflow(aPresContext, aDesiredSize, state, aStatus);
if (!mMightNeedSecondPass) {
NS_ASSERTION(oldVisibleBSize == GetScrolledFrame()->BSize(wm),
"How did our kid's BSize change if nothing was dirty?");
NS_ASSERTION(BSizeOfARow() == oldBSizeOfARow,
"How did our BSize of a row change if nothing was dirty?");
NS_ASSERTION(!IsScrollbarUpdateSuppressed(),
"Shouldn't be suppressing if we don't need a second pass!");
NS_ASSERTION(!(GetStateBits() & NS_FRAME_FIRST_REFLOW),
"How can we avoid a second pass during first reflow?");
return;
}
mMightNeedSecondPass = false;
// Now see whether we need a second pass. If we do, our nsSelectsAreaFrame
// will have suppressed the scrollbar update.
if (!IsScrollbarUpdateSuppressed()) {
// All done. No need to do more reflow.
NS_ASSERTION(!(GetStateBits() & NS_FRAME_FIRST_REFLOW),
"How can we avoid a second pass during first reflow?");
return;
}
SetSuppressScrollbarUpdate(false);
nscoord visibleBSize = GetScrolledFrame()->BSize(wm);
nscoord blockSizeOfARow = BSizeOfARow();
// Gotta reflow again.
// XXXbz We're just changing the block size here; do we need to dirty
// ourselves or anything like that? We might need to, per the letter
// of the reflow protocol, but things seem to work fine without it...
// Is that just an implementation detail of nsHTMLScrollFrame that
// we're depending on?
nsHTMLScrollFrame::DidReflow(aPresContext, &state);
// Now compute the block size we want to have.
// Note: no need to apply min/max constraints, since we have no such
// rules applied to the combobox dropdown.
mDropdownCanGrow = false;
if (visibleBSize <= 0 || blockSizeOfARow <= 0 || XRE_IsContentProcess()) {
// Looks like we have no options. Just size us to a single row
// block size.
state.SetComputedBSize(blockSizeOfARow);
mNumDisplayRows = 1;
} else {
nsComboboxControlFrame* combobox =
static_cast<nsComboboxControlFrame*>(mComboboxFrame);
LogicalPoint translation(wm);
nscoord before, after;
combobox->GetAvailableDropdownSpace(wm, &before, &after, &translation);
if (before <= 0 && after <= 0) {
state.SetComputedBSize(blockSizeOfARow);
mNumDisplayRows = 1;
mDropdownCanGrow = GetNumberOfRows() > 1;
} else {
nscoord bp = aReflowInput.ComputedLogicalBorderPadding().BStartEnd(wm);
nscoord availableBSize = std::max(before, after) - bp;
nscoord newBSize;
uint32_t rows;
if (visibleBSize <= availableBSize) {
// The dropdown fits in the available block size.
rows = GetNumberOfRows();
mNumDisplayRows = clamped<uint32_t>(rows, 1, kMaxDropDownRows);
if (mNumDisplayRows == rows) {
newBSize = visibleBSize; // use the exact block size
} else {
newBSize = mNumDisplayRows * blockSizeOfARow; // approximate
// The approximation here might actually be too big (bug 1208978);
// don't let it exceed the actual block-size of the list.
newBSize = std::min(newBSize, visibleBSize);
}
} else {
rows = availableBSize / blockSizeOfARow;
mNumDisplayRows = clamped<uint32_t>(rows, 1, kMaxDropDownRows);
newBSize = mNumDisplayRows * blockSizeOfARow; // approximate
}
state.SetComputedBSize(newBSize);
mDropdownCanGrow = visibleBSize - newBSize >= blockSizeOfARow &&
mNumDisplayRows != kMaxDropDownRows;
}
}
mLastDropdownComputedBSize = state.ComputedBSize();
aStatus.Reset();
nsHTMLScrollFrame::Reflow(aPresContext, aDesiredSize, state, aStatus);
}
ScrollbarStyles
nsListControlFrame::GetScrollbarStyles() const
{
// We can't express this in the style system yet; when we can, this can go away
// and GetScrollbarStyles can be devirtualized
int32_t style = IsInDropDownMode() ? NS_STYLE_OVERFLOW_AUTO
: NS_STYLE_OVERFLOW_SCROLL;
if (GetWritingMode().IsVertical()) {
return ScrollbarStyles(style, NS_STYLE_OVERFLOW_HIDDEN);
} else {
return ScrollbarStyles(NS_STYLE_OVERFLOW_HIDDEN, style);
}
}
bool
nsListControlFrame::ShouldPropagateComputedBSizeToScrolledContent() const
{
return !IsInDropDownMode();
}
//---------------------------------------------------------
nsContainerFrame*
nsListControlFrame::GetContentInsertionFrame() {
return GetOptionsContainer()->GetContentInsertionFrame();
}
//---------------------------------------------------------
bool
nsListControlFrame::ExtendedSelection(int32_t aStartIndex,
int32_t aEndIndex,
bool aClearAll)
{
return SetOptionsSelectedFromFrame(aStartIndex, aEndIndex,
true, aClearAll);
}
//---------------------------------------------------------
bool
nsListControlFrame::SingleSelection(int32_t aClickedIndex, bool aDoToggle)
{
if (mComboboxFrame) {
mComboboxFrame->UpdateRecentIndex(GetSelectedIndex());
}
bool wasChanged = false;
// Get Current selection
if (aDoToggle) {
wasChanged = ToggleOptionSelectedFromFrame(aClickedIndex);
} else {
wasChanged = SetOptionsSelectedFromFrame(aClickedIndex, aClickedIndex,
true, true);
}
AutoWeakFrame weakFrame(this);
ScrollToIndex(aClickedIndex);
if (!weakFrame.IsAlive()) {
return wasChanged;
}
#ifdef ACCESSIBILITY
bool isCurrentOptionChanged = mEndSelectionIndex != aClickedIndex;
#endif
mStartSelectionIndex = aClickedIndex;
mEndSelectionIndex = aClickedIndex;
InvalidateFocus();
#ifdef ACCESSIBILITY
if (isCurrentOptionChanged) {
FireMenuItemActiveEvent();
}
#endif
return wasChanged;
}
void
nsListControlFrame::InitSelectionRange(int32_t aClickedIndex)
{
//
// If nothing is selected, set the start selection depending on where
// the user clicked and what the initial selection is:
// - if the user clicked *before* selectedIndex, set the start index to
// the end of the first contiguous selection.
// - if the user clicked *after* the end of the first contiguous
// selection, set the start index to selectedIndex.
// - if the user clicked *within* the first contiguous selection, set the
// start index to selectedIndex.
// The last two rules, of course, boil down to the same thing: if the user
// clicked >= selectedIndex, return selectedIndex.
//
// This makes it so that shift click works properly when you first click
// in a multiple select.
//
int32_t selectedIndex = GetSelectedIndex();
if (selectedIndex >= 0) {
// Get the end of the contiguous selection
RefPtr<dom::HTMLOptionsCollection> options = GetOptions();
NS_ASSERTION(options, "Collection of options is null!");
uint32_t numOptions = options->Length();
// Push i to one past the last selected index in the group.
uint32_t i;
for (i = selectedIndex + 1; i < numOptions; i++) {
if (!options->ItemAsOption(i)->Selected()) {
break;
}
}
if (aClickedIndex < selectedIndex) {
// User clicked before selection, so start selection at end of
// contiguous selection
mStartSelectionIndex = i-1;
mEndSelectionIndex = selectedIndex;
} else {
// User clicked after selection, so start selection at start of
// contiguous selection
mStartSelectionIndex = selectedIndex;
mEndSelectionIndex = i-1;
}
}
}
static uint32_t
CountOptionsAndOptgroups(nsIFrame* aFrame)
{
uint32_t count = 0;
nsFrameList::Enumerator e(aFrame->PrincipalChildList());
for (; !e.AtEnd(); e.Next()) {
nsIFrame* child = e.get();
nsIContent* content = child->GetContent();
if (content) {
if (content->IsHTMLElement(nsGkAtoms::option)) {
++count;
} else {
RefPtr<HTMLOptGroupElement> optgroup =
HTMLOptGroupElement::FromNode(content);
if (optgroup) {
nsAutoString label;
optgroup->GetLabel(label);
if (label.Length() > 0) {
++count;
}
count += CountOptionsAndOptgroups(child);
}
}
}
}
return count;
}
uint32_t
nsListControlFrame::GetNumberOfRows()
{
return ::CountOptionsAndOptgroups(GetContentInsertionFrame());
}
//---------------------------------------------------------
bool
nsListControlFrame::PerformSelection(int32_t aClickedIndex,
bool aIsShift,
bool aIsControl)
{
bool wasChanged = false;
if (aClickedIndex == kNothingSelected && !mForceSelection) {
// Ignore kNothingSelected unless the selection is forced
} else if (GetMultiple()) {
if (aIsShift) {
// Make sure shift+click actually does something expected when
// the user has never clicked on the select
if (mStartSelectionIndex == kNothingSelected) {
InitSelectionRange(aClickedIndex);
}
// Get the range from beginning (low) to end (high)
// Shift *always* works, even if the current option is disabled
int32_t startIndex;
int32_t endIndex;
if (mStartSelectionIndex == kNothingSelected) {
startIndex = aClickedIndex;
endIndex = aClickedIndex;
} else if (mStartSelectionIndex <= aClickedIndex) {
startIndex = mStartSelectionIndex;
endIndex = aClickedIndex;
} else {
startIndex = aClickedIndex;
endIndex = mStartSelectionIndex;
}
// Clear only if control was not pressed
wasChanged = ExtendedSelection(startIndex, endIndex, !aIsControl);
AutoWeakFrame weakFrame(this);
ScrollToIndex(aClickedIndex);
if (!weakFrame.IsAlive()) {
return wasChanged;
}
if (mStartSelectionIndex == kNothingSelected) {
mStartSelectionIndex = aClickedIndex;
}
#ifdef ACCESSIBILITY
bool isCurrentOptionChanged = mEndSelectionIndex != aClickedIndex;
#endif
mEndSelectionIndex = aClickedIndex;
InvalidateFocus();
#ifdef ACCESSIBILITY
if (isCurrentOptionChanged) {
FireMenuItemActiveEvent();
}
#endif
} else if (aIsControl) {
wasChanged = SingleSelection(aClickedIndex, true); // might destroy us
} else {
wasChanged = SingleSelection(aClickedIndex, false); // might destroy us
}
} else {
wasChanged = SingleSelection(aClickedIndex, false); // might destroy us
}
return wasChanged;
}
//---------------------------------------------------------
bool
nsListControlFrame::HandleListSelection(dom::Event* aEvent,
int32_t aClickedIndex)
{
MouseEvent* mouseEvent = aEvent->AsMouseEvent();
bool isControl;
#ifdef XP_MACOSX
isControl = mouseEvent->MetaKey();
#else
isControl = mouseEvent->CtrlKey();
#endif
bool isShift = mouseEvent->ShiftKey();
return PerformSelection(aClickedIndex, isShift, isControl); // might destroy us
}
//---------------------------------------------------------
void
nsListControlFrame::CaptureMouseEvents(bool aGrabMouseEvents)
{
// Currently cocoa widgets use a native popup widget which tracks clicks synchronously,
// so we never want to do mouse capturing. Note that we only bail if the list
// is in drop-down mode, and the caller is requesting capture (we let release capture
// requests go through to ensure that we can release capture requested via other
// code paths, if any exist).
if (aGrabMouseEvents && IsInDropDownMode() && nsComboboxControlFrame::ToolkitHasNativePopup())
return;
if (aGrabMouseEvents) {
nsIPresShell::SetCapturingContent(mContent, CAPTURE_IGNOREALLOWED);
} else {
nsIContent* capturingContent = nsIPresShell::GetCapturingContent();
bool dropDownIsHidden = false;
if (IsInDropDownMode()) {
dropDownIsHidden = !mComboboxFrame->IsDroppedDown();
}
if (capturingContent == mContent || dropDownIsHidden) {
// only clear the capturing content if *we* are the ones doing the
// capturing (or if the dropdown is hidden, in which case NO-ONE should
// be capturing anything - it could be a scrollbar inside this listbox
// which is actually grabbing
// This shouldn't be necessary. We should simply ensure that events targeting
// scrollbars are never visible to DOM consumers.
nsIPresShell::SetCapturingContent(nullptr, 0);
}
}
}
//---------------------------------------------------------
nsresult
nsListControlFrame::HandleEvent(nsPresContext* aPresContext,
WidgetGUIEvent* aEvent,
nsEventStatus* aEventStatus)
{
NS_ENSURE_ARG_POINTER(aEventStatus);
/*const char * desc[] = {"eMouseMove",
"NS_MOUSE_LEFT_BUTTON_UP",
"NS_MOUSE_LEFT_BUTTON_DOWN",
"<NA>","<NA>","<NA>","<NA>","<NA>","<NA>","<NA>",
"NS_MOUSE_MIDDLE_BUTTON_UP",
"NS_MOUSE_MIDDLE_BUTTON_DOWN",
"<NA>","<NA>","<NA>","<NA>","<NA>","<NA>","<NA>","<NA>",
"NS_MOUSE_RIGHT_BUTTON_UP",
"NS_MOUSE_RIGHT_BUTTON_DOWN",
"eMouseOver",
"eMouseOut",
"NS_MOUSE_LEFT_DOUBLECLICK",
"NS_MOUSE_MIDDLE_DOUBLECLICK",
"NS_MOUSE_RIGHT_DOUBLECLICK",
"NS_MOUSE_LEFT_CLICK",
"NS_MOUSE_MIDDLE_CLICK",
"NS_MOUSE_RIGHT_CLICK"};
int inx = aEvent->mMessage - eMouseEventFirst;
if (inx >= 0 && inx <= (NS_MOUSE_RIGHT_CLICK - eMouseEventFirst)) {
printf("Mouse in ListFrame %s [%d]\n", desc[inx], aEvent->mMessage);
} else {
printf("Mouse in ListFrame <UNKNOWN> [%d]\n", aEvent->mMessage);
}*/
if (nsEventStatus_eConsumeNoDefault == *aEventStatus)
return NS_OK;
// disabled state affects how we're selected, but we don't want to go through
// nsHTMLScrollFrame if we're disabled.
if (IsContentDisabled()) {
return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
}
return nsHTMLScrollFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
}
//---------------------------------------------------------
void
nsListControlFrame::SetInitialChildList(ChildListID aListID,
nsFrameList& aChildList)
{
if (aListID == kPrincipalList) {
// First check to see if all the content has been added
mIsAllContentHere = mContent->IsDoneAddingChildren();
if (!mIsAllContentHere) {
mIsAllFramesHere = false;
mHasBeenInitialized = false;
}
}
nsHTMLScrollFrame::SetInitialChildList(aListID, aChildList);
// If all the content is here now check
// to see if all the frames have been created
/*if (mIsAllContentHere) {
// If all content and frames are here
// the reset/initialize
if (CheckIfAllFramesHere()) {
ResetList(aPresContext);
mHasBeenInitialized = true;
}
}*/
}
//---------------------------------------------------------
void
nsListControlFrame::Init(nsIContent* aContent,
nsContainerFrame* aParent,
nsIFrame* aPrevInFlow)
{
nsHTMLScrollFrame::Init(aContent, aParent, aPrevInFlow);
if (!nsLayoutUtils::IsContentSelectEnabled() &&
IsInDropDownMode()) {
// TODO(kuoe0) Remove the following code when content-select is enabled.
AddStateBits(NS_FRAME_IN_POPUP);
CreateView();
}
// we shouldn't have to unregister this listener because when
// our frame goes away all these content node go away as well
// because our frame is the only one who references them.
// we need to hook up our listeners before the editor is initialized
mEventListener = new nsListEventListener(this);
mContent->AddSystemEventListener(NS_LITERAL_STRING("keydown"),
mEventListener, false, false);
mContent->AddSystemEventListener(NS_LITERAL_STRING("keypress"),
mEventListener, false, false);
mContent->AddSystemEventListener(NS_LITERAL_STRING("mousedown"),
mEventListener, false, false);
mContent->AddSystemEventListener(NS_LITERAL_STRING("mouseup"),
mEventListener, false, false);
mContent->AddSystemEventListener(NS_LITERAL_STRING("mousemove"),
mEventListener, false, false);
mStartSelectionIndex = kNothingSelected;
mEndSelectionIndex = kNothingSelected;