2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-09-04 14:30:36 +04: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/. */
|
|
|
|
|
|
|
|
#include "nsNumberControlFrame.h"
|
|
|
|
|
2019-04-16 10:24:49 +03:00
|
|
|
#include "mozilla/BasicEvents.h"
|
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
#include "mozilla/PresShell.h"
|
2011-06-21 18:13:12 +04:00
|
|
|
#include "HTMLInputElement.h"
|
2013-09-04 14:30:36 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2018-07-25 16:03:45 +03:00
|
|
|
#include "nsStyleConsts.h"
|
2013-09-04 14:30:36 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsContentCreatorFunctions.h"
|
2016-02-17 23:37:00 +03:00
|
|
|
#include "nsCSSPseudoElements.h"
|
2021-04-26 19:55:32 +03:00
|
|
|
#include "nsLayoutUtils.h"
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2013-12-11 07:19:26 +04:00
|
|
|
#ifdef ACCESSIBILITY
|
|
|
|
# include "mozilla/a11y/AccTypes.h"
|
|
|
|
#endif
|
|
|
|
|
2013-09-04 14:30:36 +04:00
|
|
|
using namespace mozilla;
|
2011-06-21 18:13:12 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2019-04-16 10:24:49 +03:00
|
|
|
nsIFrame* NS_NewNumberControlFrame(PresShell* aPresShell,
|
2018-03-22 21:20:41 +03:00
|
|
|
ComputedStyle* aStyle) {
|
2019-02-05 19:45:54 +03:00
|
|
|
return new (aPresShell)
|
|
|
|
nsNumberControlFrame(aStyle, aPresShell->GetPresContext());
|
2013-09-04 14:30:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsNumberControlFrame)
|
|
|
|
|
|
|
|
NS_QUERYFRAME_HEAD(nsNumberControlFrame)
|
2018-12-07 23:00:18 +03:00
|
|
|
NS_QUERYFRAME_ENTRY(nsNumberControlFrame)
|
2020-01-14 22:01:05 +03:00
|
|
|
NS_QUERYFRAME_TAIL_INHERITING(nsTextControlFrame)
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2019-02-05 19:45:54 +03:00
|
|
|
nsNumberControlFrame::nsNumberControlFrame(ComputedStyle* aStyle,
|
|
|
|
nsPresContext* aPresContext)
|
2020-01-14 22:01:05 +03:00
|
|
|
: nsTextControlFrame(aStyle, aPresContext, kClassID) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-07 03:20:33 +03:00
|
|
|
void nsNumberControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
|
|
|
|
PostDestroyData& aPostDestroyData) {
|
2021-03-14 10:53:13 +03:00
|
|
|
aPostDestroyData.AddAnonymousContent(mSpinBox.forget());
|
2020-01-14 22:01:05 +03:00
|
|
|
nsTextControlFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
|
2013-09-04 14:30:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsNumberControlFrame::CreateAnonymousContent(
|
|
|
|
nsTArray<ContentInfo>& aElements) {
|
|
|
|
// We create an anonymous tree for our input element that is structured as
|
|
|
|
// follows:
|
|
|
|
//
|
|
|
|
// input
|
2021-04-26 19:55:32 +03:00
|
|
|
// div - placeholder
|
|
|
|
// div - preview div
|
|
|
|
// div - editor root
|
2021-03-14 10:53:13 +03:00
|
|
|
// div - spin box wrapping up/down arrow buttons
|
|
|
|
// div - spin up (up arrow button)
|
|
|
|
// div - spin down (down arrow button)
|
2013-09-04 14:30:36 +04:00
|
|
|
//
|
2021-04-26 19:55:32 +03:00
|
|
|
// If you change this, be careful to change the order of stuff returned in
|
|
|
|
// AppendAnonymousContentTo.
|
|
|
|
|
|
|
|
nsTextControlFrame::CreateAnonymousContent(aElements);
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2021-03-14 10:53:13 +03:00
|
|
|
// The author has elected to hide the spinner by setting this
|
|
|
|
// -moz-appearance. We will reframe if it changes.
|
|
|
|
if (StyleDisplay()->EffectiveAppearance() != StyleAppearance::Textfield) {
|
|
|
|
// Create the ::-moz-number-spin-box pseudo-element:
|
|
|
|
mSpinBox = MakeAnonElement(PseudoStyleType::mozNumberSpinBox);
|
2020-01-14 20:28:17 +03:00
|
|
|
|
2021-03-14 10:53:13 +03:00
|
|
|
// Create the ::-moz-number-spin-up pseudo-element:
|
|
|
|
mSpinUp = MakeAnonElement(PseudoStyleType::mozNumberSpinUp, mSpinBox);
|
2013-12-09 03:26:07 +04:00
|
|
|
|
2021-03-14 10:53:13 +03:00
|
|
|
// Create the ::-moz-number-spin-down pseudo-element:
|
|
|
|
mSpinDown = MakeAnonElement(PseudoStyleType::mozNumberSpinDown, mSpinBox);
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2021-03-14 10:53:13 +03:00
|
|
|
aElements.AppendElement(mSpinBox);
|
|
|
|
}
|
2013-09-04 14:30:36 +04:00
|
|
|
|
2018-07-24 21:11:55 +03:00
|
|
|
return NS_OK;
|
2013-09-04 14:30:36 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
nsNumberControlFrame* nsNumberControlFrame::GetNumberControlFrameForSpinButton(
|
|
|
|
nsIFrame* aFrame) {
|
2013-12-05 20:20:34 +04:00
|
|
|
// If aFrame is a spin button for an <input type=number> then we expect the
|
2021-03-17 00:53:20 +03:00
|
|
|
// frame of the NAC root parent to be that input's frame. We have to check for
|
|
|
|
// this via the content tree because we don't know whether extra frames will
|
|
|
|
// be wrapped around any of the elements between aFrame and the
|
|
|
|
// nsNumberControlFrame that we're looking for (e.g. flex wrappers).
|
2013-12-05 20:20:34 +04:00
|
|
|
nsIContent* content = aFrame->GetContent();
|
2021-03-17 00:53:20 +03:00
|
|
|
auto* nacHost = content->GetClosestNativeAnonymousSubtreeRootParent();
|
|
|
|
if (!nacHost) {
|
|
|
|
return nullptr;
|
2013-12-05 20:20:34 +04:00
|
|
|
}
|
2021-03-17 00:53:20 +03:00
|
|
|
auto* input = HTMLInputElement::FromNode(nacHost);
|
2021-06-08 12:43:59 +03:00
|
|
|
if (!input || input->ControlType() != FormControlType::InputNumber) {
|
2021-03-17 00:53:20 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return do_QueryFrame(input->GetPrimaryFrame());
|
2013-12-05 20:20:34 +04:00
|
|
|
}
|
|
|
|
|
2013-12-01 17:49:10 +04:00
|
|
|
int32_t nsNumberControlFrame::GetSpinButtonForPointerEvent(
|
|
|
|
WidgetGUIEvent* aEvent) const {
|
2014-08-04 09:28:50 +04:00
|
|
|
MOZ_ASSERT(aEvent->mClass == eMouseEventClass, "Unexpected event type");
|
2013-12-01 17:49:10 +04:00
|
|
|
|
2013-12-09 03:26:07 +04:00
|
|
|
if (!mSpinBox) {
|
|
|
|
// we don't have a spinner
|
|
|
|
return eSpinButtonNone;
|
|
|
|
}
|
2016-04-18 19:33:23 +03:00
|
|
|
if (aEvent->mOriginalTarget == mSpinUp) {
|
2013-12-01 17:49:10 +04:00
|
|
|
return eSpinButtonUp;
|
|
|
|
}
|
2016-04-18 19:33:23 +03:00
|
|
|
if (aEvent->mOriginalTarget == mSpinDown) {
|
2013-12-01 17:49:10 +04:00
|
|
|
return eSpinButtonDown;
|
|
|
|
}
|
2016-04-18 19:33:23 +03:00
|
|
|
if (aEvent->mOriginalTarget == mSpinBox) {
|
2013-12-05 20:20:34 +04:00
|
|
|
// In the case that the up/down buttons are hidden (display:none) we use
|
|
|
|
// just the spin box element, spinning up if the pointer is over the top
|
|
|
|
// half of the element, or down if it's over the bottom half. This is
|
|
|
|
// important to handle since this is the state things are in for the
|
|
|
|
// default UA style sheet. See the comment in forms.css for why.
|
2016-04-18 17:09:02 +03:00
|
|
|
LayoutDeviceIntPoint absPoint = aEvent->mRefPoint;
|
2013-12-05 20:20:34 +04:00
|
|
|
nsPoint point = nsLayoutUtils::GetEventCoordinatesRelativeTo(
|
2020-05-05 22:26:38 +03:00
|
|
|
aEvent, absPoint, RelativeTo{mSpinBox->GetPrimaryFrame()});
|
2013-12-05 20:20:34 +04:00
|
|
|
if (point != nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE)) {
|
|
|
|
if (point.y < mSpinBox->GetPrimaryFrame()->GetSize().height / 2) {
|
|
|
|
return eSpinButtonUp;
|
|
|
|
}
|
|
|
|
return eSpinButtonDown;
|
|
|
|
}
|
|
|
|
}
|
2013-12-01 17:49:10 +04:00
|
|
|
return eSpinButtonNone;
|
|
|
|
}
|
|
|
|
|
2013-12-05 20:20:34 +04:00
|
|
|
void nsNumberControlFrame::SpinnerStateChanged() const {
|
2023-01-26 17:55:22 +03:00
|
|
|
if (mSpinUp) {
|
|
|
|
nsIFrame* spinUpFrame = mSpinUp->GetPrimaryFrame();
|
|
|
|
if (spinUpFrame && spinUpFrame->IsThemed()) {
|
|
|
|
spinUpFrame->InvalidateFrame();
|
|
|
|
}
|
2013-12-05 20:20:34 +04:00
|
|
|
}
|
2023-01-26 17:55:22 +03:00
|
|
|
if (mSpinDown) {
|
|
|
|
nsIFrame* spinDownFrame = mSpinDown->GetPrimaryFrame();
|
|
|
|
if (spinDownFrame && spinDownFrame->IsThemed()) {
|
|
|
|
spinDownFrame->InvalidateFrame();
|
|
|
|
}
|
2013-12-05 20:20:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsNumberControlFrame::SpinnerUpButtonIsDepressed() const {
|
2018-03-22 00:39:04 +03:00
|
|
|
return HTMLInputElement::FromNode(mContent)
|
2013-12-05 20:20:34 +04:00
|
|
|
->NumberSpinnerUpButtonIsDepressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsNumberControlFrame::SpinnerDownButtonIsDepressed() const {
|
2018-03-22 00:39:04 +03:00
|
|
|
return HTMLInputElement::FromNode(mContent)
|
2013-12-05 20:20:34 +04:00
|
|
|
->NumberSpinnerDownButtonIsDepressed();
|
|
|
|
}
|
|
|
|
|
2014-07-16 22:41:57 +04:00
|
|
|
void nsNumberControlFrame::AppendAnonymousContentTo(
|
|
|
|
nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
|
2021-04-26 19:55:32 +03:00
|
|
|
nsTextControlFrame::AppendAnonymousContentTo(aElements, aFilter);
|
2021-03-14 10:53:13 +03:00
|
|
|
if (mSpinBox) {
|
|
|
|
aElements.AppendElement(mSpinBox);
|
2014-01-30 16:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 07:19:26 +04:00
|
|
|
#ifdef ACCESSIBILITY
|
|
|
|
a11y::AccType nsNumberControlFrame::AccessibleType() {
|
|
|
|
return a11y::eHTMLSpinnerType;
|
|
|
|
}
|
|
|
|
#endif
|