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: michael@0: #include "nsRangeFrame.h" michael@0: michael@0: #include "mozilla/EventStates.h" michael@0: #include "mozilla/TouchEvents.h" michael@0: michael@0: #include "nsContentCreatorFunctions.h" michael@0: #include "nsContentList.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCSSRendering.h" michael@0: #include "nsFormControlFrame.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsNameSpaceManager.h" michael@0: #include "nsINodeInfo.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "mozilla/dom/HTMLInputElement.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsNodeInfoManager.h" michael@0: #include "nsRenderingContext.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "nsStyleSet.h" michael@0: #include "nsThemeConstants.h" michael@0: michael@0: #ifdef ACCESSIBILITY michael@0: #include "nsAccessibilityService.h" michael@0: #endif michael@0: michael@0: #define LONG_SIDE_TO_SHORT_SIDE_RATIO 10 michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: nsIFrame* michael@0: NS_NewRangeFrame(nsIPresShell* aPresShell, nsStyleContext* aContext) michael@0: { michael@0: return new (aPresShell) nsRangeFrame(aContext); michael@0: } michael@0: michael@0: nsRangeFrame::nsRangeFrame(nsStyleContext* aContext) michael@0: : nsContainerFrame(aContext) michael@0: { michael@0: } michael@0: michael@0: nsRangeFrame::~nsRangeFrame() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_FRAMEARENA_HELPERS(nsRangeFrame) michael@0: michael@0: NS_QUERYFRAME_HEAD(nsRangeFrame) michael@0: NS_QUERYFRAME_ENTRY(nsRangeFrame) michael@0: NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator) michael@0: NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame) michael@0: michael@0: void michael@0: nsRangeFrame::Init(nsIContent* aContent, michael@0: nsIFrame* aParent, michael@0: nsIFrame* aPrevInFlow) michael@0: { michael@0: // B2G's AsyncPanZoomController::ReceiveInputEvent handles touch events michael@0: // without checking whether the out-of-process document that it controls michael@0: // will handle them, unless it has been told that the document might do so. michael@0: // This is for perf reasons, otherwise it has to wait for the event to be michael@0: // round-tripped to the other process and back, delaying panning, etc. michael@0: // We must call SetHasTouchEventListeners() in order to get APZC to wait michael@0: // until the event has been round-tripped and check whether it has been michael@0: // handled, otherwise B2G will end up panning the document when the user michael@0: // tries to drag our thumb. michael@0: // michael@0: nsIPresShell* presShell = PresContext()->GetPresShell(); michael@0: if (presShell) { michael@0: nsIDocument* document = presShell->GetDocument(); michael@0: if (document) { michael@0: nsPIDOMWindow* innerWin = document->GetInnerWindow(); michael@0: if (innerWin) { michael@0: innerWin->SetHasTouchEventListeners(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsStyleSet *styleSet = PresContext()->StyleSet(); michael@0: michael@0: mOuterFocusStyle = michael@0: styleSet->ProbePseudoElementStyle(aContent->AsElement(), michael@0: nsCSSPseudoElements::ePseudo_mozFocusOuter, michael@0: StyleContext()); michael@0: michael@0: return nsContainerFrame::Init(aContent, aParent, aPrevInFlow); michael@0: } michael@0: michael@0: void michael@0: nsRangeFrame::DestroyFrom(nsIFrame* aDestructRoot) michael@0: { michael@0: NS_ASSERTION(!GetPrevContinuation() && !GetNextContinuation(), michael@0: "nsRangeFrame should not have continuations; if it does we " michael@0: "need to call RegUnregAccessKey only for the first."); michael@0: nsFormControlFrame::RegUnRegAccessKey(static_cast(this), false); michael@0: nsContentUtils::DestroyAnonymousContent(&mTrackDiv); michael@0: nsContentUtils::DestroyAnonymousContent(&mProgressDiv); michael@0: nsContentUtils::DestroyAnonymousContent(&mThumbDiv); michael@0: nsContainerFrame::DestroyFrom(aDestructRoot); michael@0: } michael@0: michael@0: nsresult michael@0: nsRangeFrame::MakeAnonymousDiv(Element** aResult, michael@0: nsCSSPseudoElements::Type aPseudoType, michael@0: nsTArray& aElements) michael@0: { michael@0: nsCOMPtr doc = mContent->GetDocument(); michael@0: nsRefPtr resultElement = doc->CreateHTMLElement(nsGkAtoms::div); michael@0: michael@0: // Associate the pseudo-element with the anonymous child. michael@0: nsRefPtr newStyleContext = michael@0: PresContext()->StyleSet()->ResolvePseudoElementStyle(mContent->AsElement(), michael@0: aPseudoType, michael@0: StyleContext(), michael@0: resultElement); michael@0: michael@0: if (!aElements.AppendElement(ContentInfo(resultElement, newStyleContext))) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: resultElement.forget(aResult); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsRangeFrame::CreateAnonymousContent(nsTArray& aElements) michael@0: { michael@0: nsresult rv; michael@0: michael@0: // Create the ::-moz-range-track pseuto-element (a div): michael@0: rv = MakeAnonymousDiv(getter_AddRefs(mTrackDiv), michael@0: nsCSSPseudoElements::ePseudo_mozRangeTrack, michael@0: aElements); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Create the ::-moz-range-progress pseudo-element (a div): michael@0: rv = MakeAnonymousDiv(getter_AddRefs(mProgressDiv), michael@0: nsCSSPseudoElements::ePseudo_mozRangeProgress, michael@0: aElements); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Create the ::-moz-range-thumb pseudo-element (a div): michael@0: rv = MakeAnonymousDiv(getter_AddRefs(mThumbDiv), michael@0: nsCSSPseudoElements::ePseudo_mozRangeThumb, michael@0: aElements); michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: nsRangeFrame::AppendAnonymousContentTo(nsBaseContentList& aElements, michael@0: uint32_t aFilter) michael@0: { michael@0: aElements.MaybeAppendElement(mTrackDiv); michael@0: aElements.MaybeAppendElement(mProgressDiv); michael@0: aElements.MaybeAppendElement(mThumbDiv); michael@0: } michael@0: michael@0: class nsDisplayRangeFocusRing : public nsDisplayItem michael@0: { michael@0: public: michael@0: nsDisplayRangeFocusRing(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame) michael@0: : nsDisplayItem(aBuilder, aFrame) { michael@0: MOZ_COUNT_CTOR(nsDisplayRangeFocusRing); michael@0: } michael@0: #ifdef NS_BUILD_REFCNT_LOGGING michael@0: virtual ~nsDisplayRangeFocusRing() { michael@0: MOZ_COUNT_DTOR(nsDisplayRangeFocusRing); michael@0: } michael@0: #endif michael@0: michael@0: virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) MOZ_OVERRIDE; michael@0: virtual void Paint(nsDisplayListBuilder* aBuilder, nsRenderingContext* aCtx) MOZ_OVERRIDE; michael@0: NS_DISPLAY_DECL_NAME("RangeFocusRing", TYPE_OUTLINE) michael@0: }; michael@0: michael@0: nsRect michael@0: nsDisplayRangeFocusRing::GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) michael@0: { michael@0: *aSnap = false; michael@0: nsRect rect(ToReferenceFrame(), Frame()->GetSize()); michael@0: michael@0: // We want to paint as if specifying a border for ::-moz-focus-outer michael@0: // specifies an outline for our frame, so inflate by the border widths: michael@0: nsStyleContext* styleContext = michael@0: static_cast(mFrame)->mOuterFocusStyle; michael@0: MOZ_ASSERT(styleContext, "We only exist if mOuterFocusStyle is non-null"); michael@0: rect.Inflate(styleContext->StyleBorder()->GetComputedBorder()); michael@0: michael@0: return rect; michael@0: } michael@0: michael@0: void michael@0: nsDisplayRangeFocusRing::Paint(nsDisplayListBuilder* aBuilder, michael@0: nsRenderingContext* aCtx) michael@0: { michael@0: bool unused; michael@0: nsStyleContext* styleContext = michael@0: static_cast(mFrame)->mOuterFocusStyle; michael@0: MOZ_ASSERT(styleContext, "We only exist if mOuterFocusStyle is non-null"); michael@0: nsCSSRendering::PaintBorder(mFrame->PresContext(), *aCtx, mFrame, michael@0: mVisibleRect, GetBounds(aBuilder, &unused), michael@0: styleContext); michael@0: } michael@0: michael@0: void michael@0: nsRangeFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, michael@0: const nsRect& aDirtyRect, michael@0: const nsDisplayListSet& aLists) michael@0: { michael@0: if (IsThemed()) { michael@0: DisplayBorderBackgroundOutline(aBuilder, aLists); michael@0: // Only create items for the thumb. Specifically, we do not want michael@0: // the track to paint, since *our* background is used to paint michael@0: // the track, and we don't want the unthemed track painting over michael@0: // the top of the themed track. michael@0: // This logic is copied from michael@0: // nsContainerFrame::BuildDisplayListForNonBlockChildren as michael@0: // called by BuildDisplayListForInline. michael@0: nsIFrame* thumb = mThumbDiv->GetPrimaryFrame(); michael@0: if (thumb) { michael@0: nsDisplayListSet set(aLists, aLists.Content()); michael@0: BuildDisplayListForChild(aBuilder, thumb, aDirtyRect, set, DISPLAY_CHILD_INLINE); michael@0: } michael@0: } else { michael@0: BuildDisplayListForInline(aBuilder, aDirtyRect, aLists); michael@0: } michael@0: michael@0: // Draw a focus outline if appropriate: michael@0: michael@0: if (!aBuilder->IsForPainting() || michael@0: !IsVisibleForPainting(aBuilder)) { michael@0: // we don't want the focus ring item for hit-testing or if the item isn't michael@0: // in the area being [re]painted michael@0: return; michael@0: } michael@0: michael@0: EventStates eventStates = mContent->AsElement()->State(); michael@0: if (eventStates.HasState(NS_EVENT_STATE_DISABLED) || michael@0: !eventStates.HasState(NS_EVENT_STATE_FOCUSRING)) { michael@0: return; // can't have focus or doesn't match :-moz-focusring michael@0: } michael@0: michael@0: if (!mOuterFocusStyle || michael@0: !mOuterFocusStyle->StyleBorder()->HasBorder()) { michael@0: // no ::-moz-focus-outer specified border (how style specifies a focus ring michael@0: // for range) michael@0: return; michael@0: } michael@0: michael@0: const nsStyleDisplay *disp = StyleDisplay(); michael@0: if (IsThemed(disp) && michael@0: PresContext()->GetTheme()->ThemeDrawsFocusForWidget(disp->mAppearance)) { michael@0: return; // the native theme displays its own visual indication of focus michael@0: } michael@0: michael@0: aLists.Content()->AppendNewToTop( michael@0: new (aBuilder) nsDisplayRangeFocusRing(aBuilder, this)); michael@0: } michael@0: michael@0: nsresult michael@0: nsRangeFrame::Reflow(nsPresContext* aPresContext, michael@0: nsHTMLReflowMetrics& aDesiredSize, michael@0: const nsHTMLReflowState& aReflowState, michael@0: nsReflowStatus& aStatus) michael@0: { michael@0: DO_GLOBAL_REFLOW_COUNT("nsRangeFrame"); michael@0: DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus); michael@0: michael@0: NS_ASSERTION(mTrackDiv, "::-moz-range-track div must exist!"); michael@0: NS_ASSERTION(mProgressDiv, "::-moz-range-progress div must exist!"); michael@0: NS_ASSERTION(mThumbDiv, "::-moz-range-thumb div must exist!"); michael@0: NS_ASSERTION(!GetPrevContinuation() && !GetNextContinuation(), michael@0: "nsRangeFrame should not have continuations; if it does we " michael@0: "need to call RegUnregAccessKey only for the first."); michael@0: michael@0: if (mState & NS_FRAME_FIRST_REFLOW) { michael@0: nsFormControlFrame::RegUnRegAccessKey(this, true); michael@0: } michael@0: michael@0: nscoord computedHeight = aReflowState.ComputedHeight(); michael@0: if (computedHeight == NS_AUTOHEIGHT) { michael@0: computedHeight = 0; michael@0: } michael@0: aDesiredSize.Width() = aReflowState.ComputedWidth() + michael@0: aReflowState.ComputedPhysicalBorderPadding().LeftRight(); michael@0: aDesiredSize.Height() = computedHeight + michael@0: aReflowState.ComputedPhysicalBorderPadding().TopBottom(); michael@0: michael@0: nsresult rv = michael@0: ReflowAnonymousContent(aPresContext, aDesiredSize, aReflowState); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: aDesiredSize.SetOverflowAreasToDesiredBounds(); michael@0: michael@0: nsIFrame* trackFrame = mTrackDiv->GetPrimaryFrame(); michael@0: if (trackFrame) { michael@0: ConsiderChildOverflow(aDesiredSize.mOverflowAreas, trackFrame); michael@0: } michael@0: michael@0: nsIFrame* rangeProgressFrame = mProgressDiv->GetPrimaryFrame(); michael@0: if (rangeProgressFrame) { michael@0: ConsiderChildOverflow(aDesiredSize.mOverflowAreas, rangeProgressFrame); michael@0: } michael@0: michael@0: nsIFrame* thumbFrame = mThumbDiv->GetPrimaryFrame(); michael@0: if (thumbFrame) { michael@0: ConsiderChildOverflow(aDesiredSize.mOverflowAreas, thumbFrame); michael@0: } michael@0: michael@0: FinishAndStoreOverflow(&aDesiredSize); michael@0: michael@0: aStatus = NS_FRAME_COMPLETE; michael@0: michael@0: NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsRangeFrame::ReflowAnonymousContent(nsPresContext* aPresContext, michael@0: nsHTMLReflowMetrics& aDesiredSize, michael@0: const nsHTMLReflowState& aReflowState) michael@0: { michael@0: // The width/height of our content box, which is the available width/height michael@0: // for our anonymous content: michael@0: nscoord rangeFrameContentBoxWidth = aReflowState.ComputedWidth(); michael@0: nscoord rangeFrameContentBoxHeight = aReflowState.ComputedHeight(); michael@0: if (rangeFrameContentBoxHeight == NS_AUTOHEIGHT) { michael@0: rangeFrameContentBoxHeight = 0; michael@0: } michael@0: michael@0: nsIFrame* trackFrame = mTrackDiv->GetPrimaryFrame(); michael@0: michael@0: if (trackFrame) { // display:none? michael@0: michael@0: // Position the track: michael@0: // The idea here is that we allow content authors to style the width, michael@0: // height, border and padding of the track, but we ignore margin and michael@0: // positioning properties and do the positioning ourself to keep the center michael@0: // of the track's border box on the center of the nsRangeFrame's content michael@0: // box. michael@0: michael@0: nsHTMLReflowState trackReflowState(aPresContext, aReflowState, trackFrame, michael@0: nsSize(aReflowState.ComputedWidth(), michael@0: NS_UNCONSTRAINEDSIZE)); michael@0: michael@0: // Find the x/y position of the track frame such that it will be positioned michael@0: // as described above. These coordinates are with respect to the michael@0: // nsRangeFrame's border-box. michael@0: nscoord trackX = rangeFrameContentBoxWidth / 2; michael@0: nscoord trackY = rangeFrameContentBoxHeight / 2; michael@0: michael@0: // Account for the track's border and padding (we ignore its margin): michael@0: trackX -= trackReflowState.ComputedPhysicalBorderPadding().left + michael@0: trackReflowState.ComputedWidth() / 2; michael@0: trackY -= trackReflowState.ComputedPhysicalBorderPadding().top + michael@0: trackReflowState.ComputedHeight() / 2; michael@0: michael@0: // Make relative to our border box instead of our content box: michael@0: trackX += aReflowState.ComputedPhysicalBorderPadding().left; michael@0: trackY += aReflowState.ComputedPhysicalBorderPadding().top; michael@0: michael@0: nsReflowStatus frameStatus; michael@0: nsHTMLReflowMetrics trackDesiredSize(aReflowState); michael@0: nsresult rv = ReflowChild(trackFrame, aPresContext, trackDesiredSize, michael@0: trackReflowState, trackX, trackY, 0, frameStatus); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: MOZ_ASSERT(NS_FRAME_IS_FULLY_COMPLETE(frameStatus), michael@0: "We gave our child unconstrained height, so it should be complete"); michael@0: rv = FinishReflowChild(trackFrame, aPresContext, trackDesiredSize, michael@0: &trackReflowState, trackX, trackY, 0); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: nsIFrame* thumbFrame = mThumbDiv->GetPrimaryFrame(); michael@0: michael@0: if (thumbFrame) { // display:none? michael@0: nsHTMLReflowState thumbReflowState(aPresContext, aReflowState, thumbFrame, michael@0: nsSize(aReflowState.ComputedWidth(), michael@0: NS_UNCONSTRAINEDSIZE)); michael@0: michael@0: // Where we position the thumb depends on its size, so we first reflow michael@0: // the thumb at {0,0} to obtain its size, then position it afterwards. michael@0: michael@0: nsReflowStatus frameStatus; michael@0: nsHTMLReflowMetrics thumbDesiredSize(aReflowState); michael@0: nsresult rv = ReflowChild(thumbFrame, aPresContext, thumbDesiredSize, michael@0: thumbReflowState, 0, 0, 0, frameStatus); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: MOZ_ASSERT(NS_FRAME_IS_FULLY_COMPLETE(frameStatus), michael@0: "We gave our child unconstrained height, so it should be complete"); michael@0: rv = FinishReflowChild(thumbFrame, aPresContext, thumbDesiredSize, michael@0: &thumbReflowState, 0, 0, 0); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: DoUpdateThumbPosition(thumbFrame, nsSize(aDesiredSize.Width(), michael@0: aDesiredSize.Height())); michael@0: } michael@0: michael@0: nsIFrame* rangeProgressFrame = mProgressDiv->GetPrimaryFrame(); michael@0: michael@0: if (rangeProgressFrame) { // display:none? michael@0: nsHTMLReflowState progressReflowState(aPresContext, aReflowState, michael@0: rangeProgressFrame, michael@0: nsSize(aReflowState.ComputedWidth(), michael@0: NS_UNCONSTRAINEDSIZE)); michael@0: michael@0: // We first reflow the range-progress frame at {0,0} to obtain its michael@0: // unadjusted dimensions, then we adjust it to so that the appropriate edge michael@0: // ends at the thumb. michael@0: michael@0: nsReflowStatus frameStatus; michael@0: nsHTMLReflowMetrics progressDesiredSize(aReflowState); michael@0: nsresult rv = ReflowChild(rangeProgressFrame, aPresContext, michael@0: progressDesiredSize, progressReflowState, 0, 0, michael@0: 0, frameStatus); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: MOZ_ASSERT(NS_FRAME_IS_FULLY_COMPLETE(frameStatus), michael@0: "We gave our child unconstrained height, so it should be complete"); michael@0: rv = FinishReflowChild(rangeProgressFrame, aPresContext, michael@0: progressDesiredSize, &progressReflowState, 0, 0, 0); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: DoUpdateRangeProgressFrame(rangeProgressFrame, nsSize(aDesiredSize.Width(), michael@0: aDesiredSize.Height())); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: #ifdef ACCESSIBILITY michael@0: a11y::AccType michael@0: nsRangeFrame::AccessibleType() michael@0: { michael@0: return a11y::eHTMLRangeType; michael@0: } michael@0: #endif michael@0: michael@0: double michael@0: nsRangeFrame::GetValueAsFractionOfRange() michael@0: { michael@0: MOZ_ASSERT(mContent->IsHTML(nsGkAtoms::input), "bad cast"); michael@0: dom::HTMLInputElement* input = static_cast(mContent); michael@0: michael@0: MOZ_ASSERT(input->GetType() == NS_FORM_INPUT_RANGE); michael@0: michael@0: Decimal value = input->GetValueAsDecimal(); michael@0: Decimal minimum = input->GetMinimum(); michael@0: Decimal maximum = input->GetMaximum(); michael@0: michael@0: MOZ_ASSERT(value.isFinite() && minimum.isFinite() && maximum.isFinite(), michael@0: "type=range should have a default maximum/minimum"); michael@0: michael@0: if (maximum <= minimum) { michael@0: MOZ_ASSERT(value == minimum, "Unsanitized value"); michael@0: return 0.0; michael@0: } michael@0: michael@0: MOZ_ASSERT(value >= minimum && value <= maximum, "Unsanitized value"); michael@0: michael@0: return ((value - minimum) / (maximum - minimum)).toDouble(); michael@0: } michael@0: michael@0: Decimal michael@0: nsRangeFrame::GetValueAtEventPoint(WidgetGUIEvent* aEvent) michael@0: { michael@0: MOZ_ASSERT(aEvent->eventStructType == NS_MOUSE_EVENT || michael@0: aEvent->eventStructType == NS_TOUCH_EVENT, michael@0: "Unexpected event type - aEvent->refPoint may be meaningless"); michael@0: michael@0: MOZ_ASSERT(mContent->IsHTML(nsGkAtoms::input), "bad cast"); michael@0: dom::HTMLInputElement* input = static_cast(mContent); michael@0: michael@0: MOZ_ASSERT(input->GetType() == NS_FORM_INPUT_RANGE); michael@0: michael@0: Decimal minimum = input->GetMinimum(); michael@0: Decimal maximum = input->GetMaximum(); michael@0: MOZ_ASSERT(minimum.isFinite() && maximum.isFinite(), michael@0: "type=range should have a default maximum/minimum"); michael@0: if (maximum <= minimum) { michael@0: return minimum; michael@0: } michael@0: Decimal range = maximum - minimum; michael@0: michael@0: LayoutDeviceIntPoint absPoint; michael@0: if (aEvent->eventStructType == NS_TOUCH_EVENT) { michael@0: MOZ_ASSERT(aEvent->AsTouchEvent()->touches.Length() == 1, michael@0: "Unexpected number of touches"); michael@0: absPoint = LayoutDeviceIntPoint::FromUntyped( michael@0: aEvent->AsTouchEvent()->touches[0]->mRefPoint); michael@0: } else { michael@0: absPoint = aEvent->refPoint; michael@0: } michael@0: nsPoint point = michael@0: nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, michael@0: LayoutDeviceIntPoint::ToUntyped(absPoint), this); michael@0: michael@0: if (point == nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE)) { michael@0: // We don't want to change the current value for this error state. michael@0: return static_cast(mContent)->GetValueAsDecimal(); michael@0: } michael@0: michael@0: nsRect rangeContentRect = GetContentRectRelativeToSelf(); michael@0: nsSize thumbSize; michael@0: michael@0: if (IsThemed()) { michael@0: // We need to get the size of the thumb from the theme. michael@0: nsPresContext *presContext = PresContext(); michael@0: nsRefPtr tmpCtx = michael@0: presContext->PresShell()->CreateReferenceRenderingContext(); michael@0: bool notUsedCanOverride; michael@0: nsIntSize size; michael@0: presContext->GetTheme()-> michael@0: GetMinimumWidgetSize(tmpCtx.get(), this, NS_THEME_RANGE_THUMB, &size, michael@0: ¬UsedCanOverride); michael@0: thumbSize.width = presContext->DevPixelsToAppUnits(size.width); michael@0: thumbSize.height = presContext->DevPixelsToAppUnits(size.height); michael@0: MOZ_ASSERT(thumbSize.width > 0 && thumbSize.height > 0); michael@0: } else { michael@0: nsIFrame* thumbFrame = mThumbDiv->GetPrimaryFrame(); michael@0: if (thumbFrame) { // diplay:none? michael@0: thumbSize = thumbFrame->GetSize(); michael@0: } michael@0: } michael@0: michael@0: Decimal fraction; michael@0: if (IsHorizontal()) { michael@0: nscoord traversableDistance = rangeContentRect.width - thumbSize.width; michael@0: if (traversableDistance <= 0) { michael@0: return minimum; michael@0: } michael@0: nscoord posAtStart = rangeContentRect.x + thumbSize.width/2; michael@0: nscoord posAtEnd = posAtStart + traversableDistance; michael@0: nscoord posOfPoint = mozilla::clamped(point.x, posAtStart, posAtEnd); michael@0: fraction = Decimal(posOfPoint - posAtStart) / traversableDistance; michael@0: if (StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL) { michael@0: fraction = Decimal(1) - fraction; michael@0: } michael@0: } else { michael@0: nscoord traversableDistance = rangeContentRect.height - thumbSize.height; michael@0: if (traversableDistance <= 0) { michael@0: return minimum; michael@0: } michael@0: nscoord posAtStart = rangeContentRect.y + thumbSize.height/2; michael@0: nscoord posAtEnd = posAtStart + traversableDistance; michael@0: nscoord posOfPoint = mozilla::clamped(point.y, posAtStart, posAtEnd); michael@0: // For a vertical range, the top (posAtStart) is the highest value, so we michael@0: // subtract the fraction from 1.0 to get that polarity correct. michael@0: fraction = Decimal(1) - Decimal(posOfPoint - posAtStart) / traversableDistance; michael@0: } michael@0: michael@0: MOZ_ASSERT(fraction >= 0 && fraction <= 1); michael@0: return minimum + fraction * range; michael@0: } michael@0: michael@0: void michael@0: nsRangeFrame::UpdateForValueChange() michael@0: { michael@0: if (NS_SUBTREE_DIRTY(this)) { michael@0: return; // we're going to be updated when we reflow michael@0: } michael@0: nsIFrame* rangeProgressFrame = mProgressDiv->GetPrimaryFrame(); michael@0: nsIFrame* thumbFrame = mThumbDiv->GetPrimaryFrame(); michael@0: if (!rangeProgressFrame && !thumbFrame) { michael@0: return; // diplay:none? michael@0: } michael@0: if (rangeProgressFrame) { michael@0: DoUpdateRangeProgressFrame(rangeProgressFrame, GetSize()); michael@0: } michael@0: if (thumbFrame) { michael@0: DoUpdateThumbPosition(thumbFrame, GetSize()); michael@0: } michael@0: if (IsThemed()) { michael@0: // We don't know the exact dimensions or location of the thumb when native michael@0: // theming is applied, so we just repaint the entire range. michael@0: InvalidateFrame(); michael@0: } michael@0: michael@0: #ifdef ACCESSIBILITY michael@0: nsAccessibilityService* accService = nsIPresShell::AccService(); michael@0: if (accService) { michael@0: accService->RangeValueChanged(PresContext()->PresShell(), mContent); michael@0: } michael@0: #endif michael@0: michael@0: SchedulePaint(); michael@0: } michael@0: michael@0: void michael@0: nsRangeFrame::DoUpdateThumbPosition(nsIFrame* aThumbFrame, michael@0: const nsSize& aRangeSize) michael@0: { michael@0: MOZ_ASSERT(aThumbFrame); michael@0: michael@0: // The idea here is that we want to position the thumb so that the center michael@0: // of the thumb is on an imaginary line drawn from the middle of one edge michael@0: // of the range frame's content box to the middle of the opposite edge of michael@0: // its content box (the opposite edges being the left/right edge if the michael@0: // range is horizontal, or else the top/bottom edges if the range is michael@0: // vertical). How far along this line the center of the thumb is placed michael@0: // depends on the value of the range. michael@0: michael@0: nsMargin borderAndPadding = GetUsedBorderAndPadding(); michael@0: nsPoint newPosition(borderAndPadding.left, borderAndPadding.top); michael@0: michael@0: nsSize rangeContentBoxSize(aRangeSize); michael@0: rangeContentBoxSize.width -= borderAndPadding.LeftRight(); michael@0: rangeContentBoxSize.height -= borderAndPadding.TopBottom(); michael@0: michael@0: nsSize thumbSize = aThumbFrame->GetSize(); michael@0: double fraction = GetValueAsFractionOfRange(); michael@0: MOZ_ASSERT(fraction >= 0.0 && fraction <= 1.0); michael@0: michael@0: // We are called under Reflow, so we need to pass IsHorizontal a valid rect. michael@0: nsSize frameSizeOverride(aRangeSize.width, aRangeSize.height); michael@0: if (IsHorizontal(&frameSizeOverride)) { michael@0: if (thumbSize.width < rangeContentBoxSize.width) { michael@0: nscoord traversableDistance = michael@0: rangeContentBoxSize.width - thumbSize.width; michael@0: if (StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL) { michael@0: newPosition.x += NSToCoordRound((1.0 - fraction) * traversableDistance); michael@0: } else { michael@0: newPosition.x += NSToCoordRound(fraction * traversableDistance); michael@0: } michael@0: newPosition.y += (rangeContentBoxSize.height - thumbSize.height)/2; michael@0: } michael@0: } else { michael@0: if (thumbSize.height < rangeContentBoxSize.height) { michael@0: nscoord traversableDistance = michael@0: rangeContentBoxSize.height - thumbSize.height; michael@0: newPosition.x += (rangeContentBoxSize.width - thumbSize.width)/2; michael@0: newPosition.y += NSToCoordRound((1.0 - fraction) * traversableDistance); michael@0: } michael@0: } michael@0: aThumbFrame->SetPosition(newPosition); michael@0: } michael@0: michael@0: void michael@0: nsRangeFrame::DoUpdateRangeProgressFrame(nsIFrame* aRangeProgressFrame, michael@0: const nsSize& aRangeSize) michael@0: { michael@0: MOZ_ASSERT(aRangeProgressFrame); michael@0: michael@0: // The idea here is that we want to position the ::-moz-range-progress michael@0: // pseudo-element so that the center line running along its length is on the michael@0: // corresponding center line of the nsRangeFrame's content box. In the other michael@0: // dimension, we align the "start" edge of the ::-moz-range-progress michael@0: // pseudo-element's border-box with the corresponding edge of the michael@0: // nsRangeFrame's content box, and we size the progress element's border-box michael@0: // to have a length of GetValueAsFractionOfRange() times the nsRangeFrame's michael@0: // content-box size. michael@0: michael@0: nsMargin borderAndPadding = GetUsedBorderAndPadding(); michael@0: nsSize progSize = aRangeProgressFrame->GetSize(); michael@0: nsRect progRect(borderAndPadding.left, borderAndPadding.top, michael@0: progSize.width, progSize.height); michael@0: michael@0: nsSize rangeContentBoxSize(aRangeSize); michael@0: rangeContentBoxSize.width -= borderAndPadding.LeftRight(); michael@0: rangeContentBoxSize.height -= borderAndPadding.TopBottom(); michael@0: michael@0: double fraction = GetValueAsFractionOfRange(); michael@0: MOZ_ASSERT(fraction >= 0.0 && fraction <= 1.0); michael@0: michael@0: // We are called under Reflow, so we need to pass IsHorizontal a valid rect. michael@0: nsSize frameSizeOverride(aRangeSize.width, aRangeSize.height); michael@0: if (IsHorizontal(&frameSizeOverride)) { michael@0: nscoord progLength = NSToCoordRound(fraction * rangeContentBoxSize.width); michael@0: if (StyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL) { michael@0: progRect.x += rangeContentBoxSize.width - progLength; michael@0: } michael@0: progRect.y += (rangeContentBoxSize.height - progSize.height)/2; michael@0: progRect.width = progLength; michael@0: } else { michael@0: nscoord progLength = NSToCoordRound(fraction * rangeContentBoxSize.height); michael@0: progRect.x += (rangeContentBoxSize.width - progSize.width)/2; michael@0: progRect.y += rangeContentBoxSize.height - progLength; michael@0: progRect.height = progLength; michael@0: } michael@0: aRangeProgressFrame->SetRect(progRect); michael@0: } michael@0: michael@0: nsresult michael@0: nsRangeFrame::AttributeChanged(int32_t aNameSpaceID, michael@0: nsIAtom* aAttribute, michael@0: int32_t aModType) michael@0: { michael@0: NS_ASSERTION(mTrackDiv, "The track div must exist!"); michael@0: NS_ASSERTION(mThumbDiv, "The thumb div must exist!"); michael@0: michael@0: if (aNameSpaceID == kNameSpaceID_None) { michael@0: if (aAttribute == nsGkAtoms::value || michael@0: aAttribute == nsGkAtoms::min || michael@0: aAttribute == nsGkAtoms::max || michael@0: aAttribute == nsGkAtoms::step) { michael@0: // We want to update the position of the thumb, except in one special michael@0: // case: If the value attribute is being set, it is possible that we are michael@0: // in the middle of a type change away from type=range, under the michael@0: // SetAttr(..., nsGkAtoms::value, ...) call in HTMLInputElement:: michael@0: // HandleTypeChange. In that case the HTMLInputElement's type will michael@0: // already have changed, and if we call UpdateForValueChange() michael@0: // we'll fail the asserts under that call that check the type of our michael@0: // HTMLInputElement. Given that we're changing away from being a range michael@0: // and this frame will shortly be destroyed, there's no point in calling michael@0: // UpdateForValueChange() anyway. michael@0: MOZ_ASSERT(mContent->IsHTML(nsGkAtoms::input), "bad cast"); michael@0: bool typeIsRange = static_cast(mContent)->GetType() == michael@0: NS_FORM_INPUT_RANGE; michael@0: // If script changed the 's type before setting these attributes michael@0: // then we don't need to do anything since we are going to be reframed. michael@0: if (typeIsRange) { michael@0: UpdateForValueChange(); michael@0: } michael@0: } else if (aAttribute == nsGkAtoms::orient) { michael@0: PresContext()->PresShell()->FrameNeedsReflow(this, nsIPresShell::eResize, michael@0: NS_FRAME_IS_DIRTY); michael@0: } michael@0: } michael@0: michael@0: return nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType); michael@0: } michael@0: michael@0: nsSize michael@0: nsRangeFrame::ComputeAutoSize(nsRenderingContext *aRenderingContext, michael@0: nsSize aCBSize, nscoord aAvailableWidth, michael@0: nsSize aMargin, nsSize aBorder, michael@0: nsSize aPadding, bool aShrinkWrap) michael@0: { michael@0: nscoord oneEm = NSToCoordRound(StyleFont()->mFont.size * michael@0: nsLayoutUtils::FontSizeInflationFor(this)); // 1em michael@0: michael@0: // frameSizeOverride values just gets us to fall back to being horizontal michael@0: // (the actual values are irrelevant, as long as width > height): michael@0: nsSize frameSizeOverride(10,1); michael@0: bool isHorizontal = IsHorizontal(&frameSizeOverride); michael@0: michael@0: nsSize autoSize; michael@0: michael@0: // nsFrame::ComputeSize calls GetMinimumWidgetSize to prevent us from being michael@0: // given too small a size when we're natively themed. If we're themed, we set michael@0: // our "thickness" dimension to zero below and rely on that michael@0: // GetMinimumWidgetSize check to correct that dimension to the natural michael@0: // thickness of a slider in the current theme. michael@0: michael@0: if (isHorizontal) { michael@0: autoSize.width = LONG_SIDE_TO_SHORT_SIDE_RATIO * oneEm; michael@0: autoSize.height = IsThemed() ? 0 : oneEm; michael@0: } else { michael@0: autoSize.width = IsThemed() ? 0 : oneEm; michael@0: autoSize.height = LONG_SIDE_TO_SHORT_SIDE_RATIO * oneEm; michael@0: } michael@0: michael@0: return autoSize; michael@0: } michael@0: michael@0: nscoord michael@0: nsRangeFrame::GetMinWidth(nsRenderingContext *aRenderingContext) michael@0: { michael@0: // nsFrame::ComputeSize calls GetMinimumWidgetSize to prevent us from being michael@0: // given too small a size when we're natively themed. If we aren't native michael@0: // themed, we don't mind how small we're sized. michael@0: return nscoord(0); michael@0: } michael@0: michael@0: nscoord michael@0: nsRangeFrame::GetPrefWidth(nsRenderingContext *aRenderingContext) michael@0: { michael@0: // frameSizeOverride values just gets us to fall back to being horizontal: michael@0: nsSize frameSizeOverride(10,1); michael@0: bool isHorizontal = IsHorizontal(&frameSizeOverride); michael@0: michael@0: if (!isHorizontal && IsThemed()) { michael@0: // nsFrame::ComputeSize calls GetMinimumWidgetSize to prevent us from being michael@0: // given too small a size when we're natively themed. We return zero and michael@0: // depend on that correction to get our "natuaral" width when we're a michael@0: // vertical slider. michael@0: return 0; michael@0: } michael@0: michael@0: nscoord prefWidth = NSToCoordRound(StyleFont()->mFont.size * michael@0: nsLayoutUtils::FontSizeInflationFor(this)); // 1em michael@0: michael@0: if (isHorizontal) { michael@0: prefWidth *= LONG_SIDE_TO_SHORT_SIDE_RATIO; michael@0: } michael@0: michael@0: return prefWidth; michael@0: } michael@0: michael@0: bool michael@0: nsRangeFrame::IsHorizontal(const nsSize *aFrameSizeOverride) const michael@0: { michael@0: dom::HTMLInputElement* element = static_cast(mContent); michael@0: return !element->AttrValueIs(kNameSpaceID_None, nsGkAtoms::orient, michael@0: nsGkAtoms::vertical, eCaseMatters); michael@0: } michael@0: michael@0: double michael@0: nsRangeFrame::GetMin() const michael@0: { michael@0: return static_cast(mContent)->GetMinimum().toDouble(); michael@0: } michael@0: michael@0: double michael@0: nsRangeFrame::GetMax() const michael@0: { michael@0: return static_cast(mContent)->GetMaximum().toDouble(); michael@0: } michael@0: michael@0: double michael@0: nsRangeFrame::GetValue() const michael@0: { michael@0: return static_cast(mContent)->GetValueAsDecimal().toDouble(); michael@0: } michael@0: michael@0: nsIAtom* michael@0: nsRangeFrame::GetType() const michael@0: { michael@0: return nsGkAtoms::rangeFrame; michael@0: } michael@0: michael@0: #define STYLES_DISABLING_NATIVE_THEMING \ michael@0: NS_AUTHOR_SPECIFIED_BACKGROUND | \ michael@0: NS_AUTHOR_SPECIFIED_PADDING | \ michael@0: NS_AUTHOR_SPECIFIED_BORDER michael@0: michael@0: bool michael@0: nsRangeFrame::ShouldUseNativeStyle() const michael@0: { michael@0: return (StyleDisplay()->mAppearance == NS_THEME_RANGE) && michael@0: !PresContext()->HasAuthorSpecifiedRules(const_cast(this), michael@0: (NS_AUTHOR_SPECIFIED_BORDER | michael@0: NS_AUTHOR_SPECIFIED_BACKGROUND)) && michael@0: !PresContext()->HasAuthorSpecifiedRules(mTrackDiv->GetPrimaryFrame(), michael@0: STYLES_DISABLING_NATIVE_THEMING) && michael@0: !PresContext()->HasAuthorSpecifiedRules(mProgressDiv->GetPrimaryFrame(), michael@0: STYLES_DISABLING_NATIVE_THEMING) && michael@0: !PresContext()->HasAuthorSpecifiedRules(mThumbDiv->GetPrimaryFrame(), michael@0: STYLES_DISABLING_NATIVE_THEMING); michael@0: } michael@0: michael@0: Element* michael@0: nsRangeFrame::GetPseudoElement(nsCSSPseudoElements::Type aType) michael@0: { michael@0: if (aType == nsCSSPseudoElements::ePseudo_mozRangeTrack) { michael@0: return mTrackDiv; michael@0: } michael@0: michael@0: if (aType == nsCSSPseudoElements::ePseudo_mozRangeThumb) { michael@0: return mThumbDiv; michael@0: } michael@0: michael@0: if (aType == nsCSSPseudoElements::ePseudo_mozRangeProgress) { michael@0: return mProgressDiv; michael@0: } michael@0: michael@0: return nsContainerFrame::GetPseudoElement(aType); michael@0: } michael@0: michael@0: nsStyleContext* michael@0: nsRangeFrame::GetAdditionalStyleContext(int32_t aIndex) const michael@0: { michael@0: // We only implement this so that SetAdditionalStyleContext will be michael@0: // called if style changes that would change the -moz-focus-outer michael@0: // pseudo-element have occurred. michael@0: return aIndex == 0 ? mOuterFocusStyle : nullptr; michael@0: } michael@0: michael@0: void michael@0: nsRangeFrame::SetAdditionalStyleContext(int32_t aIndex, michael@0: nsStyleContext* aStyleContext) michael@0: { michael@0: MOZ_ASSERT(aIndex == 0, michael@0: "GetAdditionalStyleContext is handling other indexes?"); michael@0: michael@0: // The -moz-focus-outer pseudo-element's style has changed. michael@0: mOuterFocusStyle = aStyleContext; michael@0: }