2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsSelectsAreaFrame.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIDOMHTMLOptionElement.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsListControlFrame.h"
|
|
|
|
#include "nsDisplayList.h"
|
|
|
|
|
|
|
|
nsIFrame*
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_NewSelectsAreaFrame(nsIPresShell* aShell, nsStyleContext* aContext, uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsSelectsAreaFrame* it = new (aShell) nsSelectsAreaFrame(aContext);
|
|
|
|
|
|
|
|
if (it) {
|
2009-01-04 16:39:54 -08:00
|
|
|
// We need NS_BLOCK_FLOAT_MGR to ensure that the options inside the select
|
2007-03-22 10:30:00 -07:00
|
|
|
// aren't expanded by right floats outside the select.
|
2009-01-04 16:39:54 -08:00
|
|
|
it->SetFlags(aFlags | NS_BLOCK_FLOAT_MGR);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2009-09-12 09:49:24 -07:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsSelectsAreaFrame)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//---------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* This wrapper class lets us redirect mouse hits from the child frame of
|
|
|
|
* an option element to the element's own frame.
|
|
|
|
* REVIEW: This is what nsSelectsAreaFrame::GetFrameForPoint used to do
|
|
|
|
*/
|
|
|
|
class nsDisplayOptionEventGrabber : public nsDisplayWrapList {
|
|
|
|
public:
|
2010-08-13 03:01:13 -07:00
|
|
|
nsDisplayOptionEventGrabber(nsDisplayListBuilder* aBuilder,
|
|
|
|
nsIFrame* aFrame, nsDisplayItem* aItem)
|
|
|
|
: nsDisplayWrapList(aBuilder, aFrame, aItem) {}
|
|
|
|
nsDisplayOptionEventGrabber(nsDisplayListBuilder* aBuilder,
|
|
|
|
nsIFrame* aFrame, nsDisplayList* aList)
|
|
|
|
: nsDisplayWrapList(aBuilder, aFrame, aList) {}
|
2010-04-07 17:31:26 -07:00
|
|
|
virtual void HitTest(nsDisplayListBuilder* aBuilder, const nsRect& aRect,
|
|
|
|
HitTestState* aState, nsTArray<nsIFrame*> *aOutFrames);
|
2010-07-15 14:07:49 -07:00
|
|
|
NS_DISPLAY_DECL_NAME("OptionEventGrabber", TYPE_OPTION_EVENT_GRABBER)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
virtual nsDisplayWrapList* WrapWithClone(nsDisplayListBuilder* aBuilder,
|
|
|
|
nsDisplayItem* aItem);
|
|
|
|
};
|
|
|
|
|
2010-04-07 17:31:26 -07:00
|
|
|
void nsDisplayOptionEventGrabber::HitTest(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aRect, HitTestState* aState, nsTArray<nsIFrame*> *aOutFrames)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-07 17:31:26 -07:00
|
|
|
nsTArray<nsIFrame*> outFrames;
|
|
|
|
mList.HitTest(aBuilder, aRect, aState, &outFrames);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < outFrames.Length(); i++) {
|
2010-04-07 17:31:26 -07:00
|
|
|
nsIFrame* selectedFrame = outFrames.ElementAt(i);
|
2007-03-22 10:30:00 -07:00
|
|
|
while (selectedFrame &&
|
2011-08-08 08:14:33 -07:00
|
|
|
!(selectedFrame->GetContent() &&
|
|
|
|
selectedFrame->GetContent()->IsHTML(nsGkAtoms::option))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
selectedFrame = selectedFrame->GetParent();
|
|
|
|
}
|
|
|
|
if (selectedFrame) {
|
2010-04-07 17:31:26 -07:00
|
|
|
aOutFrames->AppendElement(selectedFrame);
|
|
|
|
} else {
|
|
|
|
// keep the original result, which could be this frame
|
|
|
|
aOutFrames->AppendElement(outFrames.ElementAt(i));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDisplayWrapList* nsDisplayOptionEventGrabber::WrapWithClone(
|
|
|
|
nsDisplayListBuilder* aBuilder, nsDisplayItem* aItem) {
|
2010-08-13 03:01:13 -07:00
|
|
|
return new (aBuilder)
|
|
|
|
nsDisplayOptionEventGrabber(aBuilder, aItem->GetUnderlyingFrame(), aItem);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class nsOptionEventGrabberWrapper : public nsDisplayWrapper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsOptionEventGrabberWrapper() {}
|
|
|
|
virtual nsDisplayItem* WrapList(nsDisplayListBuilder* aBuilder,
|
|
|
|
nsIFrame* aFrame, nsDisplayList* aList) {
|
|
|
|
// We can't specify the underlying frame here. We need this list to be
|
|
|
|
// exploded if sorted.
|
2012-07-30 07:20:58 -07:00
|
|
|
return new (aBuilder) nsDisplayOptionEventGrabber(aBuilder, nullptr, aList);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
virtual nsDisplayItem* WrapItem(nsDisplayListBuilder* aBuilder,
|
|
|
|
nsDisplayItem* aItem) {
|
2010-08-13 03:01:13 -07:00
|
|
|
return new (aBuilder) nsDisplayOptionEventGrabber(aBuilder, aItem->GetUnderlyingFrame(), aItem);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static nsListControlFrame* GetEnclosingListFrame(nsIFrame* aSelectsAreaFrame)
|
|
|
|
{
|
|
|
|
nsIFrame* frame = aSelectsAreaFrame->GetParent();
|
|
|
|
while (frame) {
|
|
|
|
if (frame->GetType() == nsGkAtoms::listControlFrame)
|
2007-07-08 00:08:04 -07:00
|
|
|
return static_cast<nsListControlFrame*>(frame);
|
2007-03-22 10:30:00 -07:00
|
|
|
frame = frame->GetParent();
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class nsDisplayListFocus : public nsDisplayItem {
|
|
|
|
public:
|
2010-08-13 03:01:13 -07:00
|
|
|
nsDisplayListFocus(nsDisplayListBuilder* aBuilder,
|
|
|
|
nsSelectsAreaFrame* aFrame) :
|
|
|
|
nsDisplayItem(aBuilder, aFrame) {
|
2007-03-22 10:30:00 -07:00
|
|
|
MOZ_COUNT_CTOR(nsDisplayListFocus);
|
|
|
|
}
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
virtual ~nsDisplayListFocus() {
|
|
|
|
MOZ_COUNT_DTOR(nsDisplayListFocus);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-10 04:24:18 -07:00
|
|
|
virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) {
|
|
|
|
*aSnap = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
// override bounds because the list item focus ring may extend outside
|
|
|
|
// the nsSelectsAreaFrame
|
|
|
|
nsListControlFrame* listFrame = GetEnclosingListFrame(GetUnderlyingFrame());
|
2011-10-21 10:45:32 -07:00
|
|
|
return listFrame->GetVisualOverflowRectRelativeToSelf() +
|
2012-09-16 03:32:59 -07:00
|
|
|
listFrame->GetOffsetToCrossDoc(ReferenceFrame());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-09-06 17:35:14 -07:00
|
|
|
virtual void Paint(nsDisplayListBuilder* aBuilder,
|
2011-04-07 18:04:40 -07:00
|
|
|
nsRenderingContext* aCtx) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsListControlFrame* listFrame = GetEnclosingListFrame(GetUnderlyingFrame());
|
|
|
|
// listFrame must be non-null or we wouldn't get called.
|
|
|
|
listFrame->PaintFocus(*aCtx, aBuilder->ToReferenceFrame(listFrame));
|
|
|
|
}
|
2010-07-15 14:07:49 -07:00
|
|
|
NS_DISPLAY_DECL_NAME("ListFocus", TYPE_LIST_FOCUS)
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-02-14 03:12:27 -08:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSelectsAreaFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
|
|
|
{
|
2013-02-14 03:12:27 -08:00
|
|
|
if (!aBuilder->IsForEventDelivery()) {
|
|
|
|
BuildDisplayListInternal(aBuilder, aDirtyRect, aLists);
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsDisplayListCollection set;
|
2013-02-14 03:12:27 -08:00
|
|
|
BuildDisplayListInternal(aBuilder, aDirtyRect, set);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsOptionEventGrabberWrapper wrapper;
|
2013-02-14 03:12:27 -08:00
|
|
|
wrapper.WrapLists(aBuilder, this, set, aLists);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-02-14 03:12:27 -08:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSelectsAreaFrame::BuildDisplayListInternal(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
|
|
|
{
|
2013-02-14 03:08:08 -08:00
|
|
|
nsBlockFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsListControlFrame* listFrame = GetEnclosingListFrame(this);
|
|
|
|
if (listFrame && listFrame->IsFocused()) {
|
|
|
|
// we can't just associate the display item with the list frame,
|
|
|
|
// because then the list's scrollframe won't clip it (the scrollframe
|
|
|
|
// only clips contained descendants).
|
2013-02-14 03:08:08 -08:00
|
|
|
aLists.Outlines()->AppendNewToTop(new (aBuilder)
|
2010-08-13 03:01:13 -07:00
|
|
|
nsDisplayListFocus(aBuilder, this));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSelectsAreaFrame::Reflow(nsPresContext* aPresContext,
|
|
|
|
nsHTMLReflowMetrics& aDesiredSize,
|
|
|
|
const nsHTMLReflowState& aReflowState,
|
|
|
|
nsReflowStatus& aStatus)
|
|
|
|
{
|
|
|
|
nsListControlFrame* list = GetEnclosingListFrame(this);
|
|
|
|
NS_ASSERTION(list,
|
|
|
|
"Must have an nsListControlFrame! Frame constructor is "
|
|
|
|
"broken");
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isInDropdownMode = list->IsInDropDownMode();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// See similar logic in nsListControlFrame::Reflow and
|
|
|
|
// nsListControlFrame::ReflowAsDropdown. We need to match it here.
|
|
|
|
nscoord oldHeight;
|
|
|
|
if (isInDropdownMode) {
|
|
|
|
// Store the height now in case it changes during
|
2008-12-29 07:07:38 -08:00
|
|
|
// nsBlockFrame::Reflow for some odd reason.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
|
|
|
|
oldHeight = GetSize().height;
|
|
|
|
} else {
|
|
|
|
oldHeight = NS_UNCONSTRAINEDSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-29 07:07:38 -08:00
|
|
|
nsresult rv = nsBlockFrame::Reflow(aPresContext, aDesiredSize,
|
2007-03-22 10:30:00 -07:00
|
|
|
aReflowState, aStatus);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-21 00:38:49 -08:00
|
|
|
// Check whether we need to suppress scrollbar updates. We want to do that if
|
2007-03-22 10:30:00 -07:00
|
|
|
// we're in a possible first pass and our height of a row has changed.
|
|
|
|
if (list->MightNeedSecondPass()) {
|
2012-01-24 17:21:29 -08:00
|
|
|
nscoord newHeightOfARow = list->CalcHeightOfARow();
|
2007-03-22 10:30:00 -07:00
|
|
|
// We'll need a second pass if our height of a row changed. For
|
|
|
|
// comboboxes, we'll also need it if our height changed. If we're going
|
|
|
|
// to do a second pass, suppress scrollbar updates for this pass.
|
|
|
|
if (newHeightOfARow != mHeightOfARow ||
|
|
|
|
(isInDropdownMode && (oldHeight != aDesiredSize.height ||
|
|
|
|
oldHeight != GetSize().height))) {
|
|
|
|
mHeightOfARow = newHeightOfARow;
|
2011-10-17 07:59:28 -07:00
|
|
|
list->SetSuppressScrollbarUpdate(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|