michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include "nsSelectsAreaFrame.h" michael@0: #include "nsIContent.h" michael@0: #include "nsListControlFrame.h" michael@0: #include "nsDisplayList.h" michael@0: michael@0: nsIFrame* michael@0: NS_NewSelectsAreaFrame(nsIPresShell* aShell, nsStyleContext* aContext, nsFrameState aFlags) michael@0: { michael@0: nsSelectsAreaFrame* it = new (aShell) nsSelectsAreaFrame(aContext); michael@0: michael@0: // We need NS_BLOCK_FLOAT_MGR to ensure that the options inside the select michael@0: // aren't expanded by right floats outside the select. michael@0: it->SetFlags(aFlags | NS_BLOCK_FLOAT_MGR); michael@0: michael@0: return it; michael@0: } michael@0: michael@0: NS_IMPL_FRAMEARENA_HELPERS(nsSelectsAreaFrame) michael@0: michael@0: //--------------------------------------------------------- michael@0: /** michael@0: * This wrapper class lets us redirect mouse hits from the child frame of michael@0: * an option element to the element's own frame. michael@0: * REVIEW: This is what nsSelectsAreaFrame::GetFrameForPoint used to do michael@0: */ michael@0: class nsDisplayOptionEventGrabber : public nsDisplayWrapList { michael@0: public: michael@0: nsDisplayOptionEventGrabber(nsDisplayListBuilder* aBuilder, michael@0: nsIFrame* aFrame, nsDisplayItem* aItem) michael@0: : nsDisplayWrapList(aBuilder, aFrame, aItem) {} michael@0: nsDisplayOptionEventGrabber(nsDisplayListBuilder* aBuilder, michael@0: nsIFrame* aFrame, nsDisplayList* aList) michael@0: : nsDisplayWrapList(aBuilder, aFrame, aList) {} michael@0: virtual void HitTest(nsDisplayListBuilder* aBuilder, const nsRect& aRect, michael@0: HitTestState* aState, nsTArray *aOutFrames); michael@0: NS_DISPLAY_DECL_NAME("OptionEventGrabber", TYPE_OPTION_EVENT_GRABBER) michael@0: }; michael@0: michael@0: void nsDisplayOptionEventGrabber::HitTest(nsDisplayListBuilder* aBuilder, michael@0: const nsRect& aRect, HitTestState* aState, nsTArray *aOutFrames) michael@0: { michael@0: nsTArray outFrames; michael@0: mList.HitTest(aBuilder, aRect, aState, &outFrames); michael@0: michael@0: for (uint32_t i = 0; i < outFrames.Length(); i++) { michael@0: nsIFrame* selectedFrame = outFrames.ElementAt(i); michael@0: while (selectedFrame && michael@0: !(selectedFrame->GetContent() && michael@0: selectedFrame->GetContent()->IsHTML(nsGkAtoms::option))) { michael@0: selectedFrame = selectedFrame->GetParent(); michael@0: } michael@0: if (selectedFrame) { michael@0: aOutFrames->AppendElement(selectedFrame); michael@0: } else { michael@0: // keep the original result, which could be this frame michael@0: aOutFrames->AppendElement(outFrames.ElementAt(i)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: class nsOptionEventGrabberWrapper : public nsDisplayWrapper michael@0: { michael@0: public: michael@0: nsOptionEventGrabberWrapper() {} michael@0: virtual nsDisplayItem* WrapList(nsDisplayListBuilder* aBuilder, michael@0: nsIFrame* aFrame, nsDisplayList* aList) { michael@0: return new (aBuilder) nsDisplayOptionEventGrabber(aBuilder, aFrame, aList); michael@0: } michael@0: virtual nsDisplayItem* WrapItem(nsDisplayListBuilder* aBuilder, michael@0: nsDisplayItem* aItem) { michael@0: return new (aBuilder) nsDisplayOptionEventGrabber(aBuilder, aItem->Frame(), aItem); michael@0: } michael@0: }; michael@0: michael@0: static nsListControlFrame* GetEnclosingListFrame(nsIFrame* aSelectsAreaFrame) michael@0: { michael@0: nsIFrame* frame = aSelectsAreaFrame->GetParent(); michael@0: while (frame) { michael@0: if (frame->GetType() == nsGkAtoms::listControlFrame) michael@0: return static_cast(frame); michael@0: frame = frame->GetParent(); michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: class nsDisplayListFocus : public nsDisplayItem { michael@0: public: michael@0: nsDisplayListFocus(nsDisplayListBuilder* aBuilder, michael@0: nsSelectsAreaFrame* aFrame) : michael@0: nsDisplayItem(aBuilder, aFrame) { michael@0: MOZ_COUNT_CTOR(nsDisplayListFocus); michael@0: } michael@0: #ifdef NS_BUILD_REFCNT_LOGGING michael@0: virtual ~nsDisplayListFocus() { michael@0: MOZ_COUNT_DTOR(nsDisplayListFocus); michael@0: } michael@0: #endif michael@0: michael@0: virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) { michael@0: *aSnap = false; michael@0: // override bounds because the list item focus ring may extend outside michael@0: // the nsSelectsAreaFrame michael@0: nsListControlFrame* listFrame = GetEnclosingListFrame(Frame()); michael@0: return listFrame->GetVisualOverflowRectRelativeToSelf() + michael@0: listFrame->GetOffsetToCrossDoc(ReferenceFrame()); michael@0: } michael@0: virtual void Paint(nsDisplayListBuilder* aBuilder, michael@0: nsRenderingContext* aCtx) { michael@0: nsListControlFrame* listFrame = GetEnclosingListFrame(Frame()); michael@0: // listFrame must be non-null or we wouldn't get called. michael@0: listFrame->PaintFocus(*aCtx, aBuilder->ToReferenceFrame(listFrame)); michael@0: } michael@0: NS_DISPLAY_DECL_NAME("ListFocus", TYPE_LIST_FOCUS) michael@0: }; michael@0: michael@0: void michael@0: nsSelectsAreaFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, michael@0: const nsRect& aDirtyRect, michael@0: const nsDisplayListSet& aLists) michael@0: { michael@0: if (!aBuilder->IsForEventDelivery()) { michael@0: BuildDisplayListInternal(aBuilder, aDirtyRect, aLists); michael@0: return; michael@0: } michael@0: michael@0: nsDisplayListCollection set; michael@0: BuildDisplayListInternal(aBuilder, aDirtyRect, set); michael@0: michael@0: nsOptionEventGrabberWrapper wrapper; michael@0: wrapper.WrapLists(aBuilder, this, set, aLists); michael@0: } michael@0: michael@0: void michael@0: nsSelectsAreaFrame::BuildDisplayListInternal(nsDisplayListBuilder* aBuilder, michael@0: const nsRect& aDirtyRect, michael@0: const nsDisplayListSet& aLists) michael@0: { michael@0: nsBlockFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists); michael@0: michael@0: nsListControlFrame* listFrame = GetEnclosingListFrame(this); michael@0: if (listFrame && listFrame->IsFocused()) { michael@0: // we can't just associate the display item with the list frame, michael@0: // because then the list's scrollframe won't clip it (the scrollframe michael@0: // only clips contained descendants). michael@0: aLists.Outlines()->AppendNewToTop(new (aBuilder) michael@0: nsDisplayListFocus(aBuilder, this)); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: nsSelectsAreaFrame::Reflow(nsPresContext* aPresContext, michael@0: nsHTMLReflowMetrics& aDesiredSize, michael@0: const nsHTMLReflowState& aReflowState, michael@0: nsReflowStatus& aStatus) michael@0: { michael@0: nsListControlFrame* list = GetEnclosingListFrame(this); michael@0: NS_ASSERTION(list, michael@0: "Must have an nsListControlFrame! Frame constructor is " michael@0: "broken"); michael@0: michael@0: bool isInDropdownMode = list->IsInDropDownMode(); michael@0: michael@0: // See similar logic in nsListControlFrame::Reflow and michael@0: // nsListControlFrame::ReflowAsDropdown. We need to match it here. michael@0: nscoord oldHeight; michael@0: if (isInDropdownMode) { michael@0: // Store the height now in case it changes during michael@0: // nsBlockFrame::Reflow for some odd reason. michael@0: if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) { michael@0: oldHeight = GetSize().height; michael@0: } else { michael@0: oldHeight = NS_UNCONSTRAINEDSIZE; michael@0: } michael@0: } michael@0: michael@0: nsresult rv = nsBlockFrame::Reflow(aPresContext, aDesiredSize, michael@0: aReflowState, aStatus); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Check whether we need to suppress scrollbar updates. We want to do that if michael@0: // we're in a possible first pass and our height of a row has changed. michael@0: if (list->MightNeedSecondPass()) { michael@0: nscoord newHeightOfARow = list->CalcHeightOfARow(); michael@0: // We'll need a second pass if our height of a row changed. For michael@0: // comboboxes, we'll also need it if our height changed. If we're going michael@0: // to do a second pass, suppress scrollbar updates for this pass. michael@0: if (newHeightOfARow != mHeightOfARow || michael@0: (isInDropdownMode && (oldHeight != aDesiredSize.Height() || michael@0: oldHeight != GetSize().height))) { michael@0: mHeightOfARow = newHeightOfARow; michael@0: list->SetSuppressScrollbarUpdate(true); michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: }