Backed out changeset 1d9301697aa0 (bug 1887752) for causing failures on browser_all_f...
[gecko.git] / layout / forms / nsNumberControlFrame.cpp
blob518ba91e241f3e7498ca75cb14bf60595dacdc33
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsNumberControlFrame.h"
9 #include "mozilla/BasicEvents.h"
10 #include "mozilla/FloatingPoint.h"
11 #include "mozilla/PresShell.h"
12 #include "HTMLInputElement.h"
13 #include "nsGkAtoms.h"
14 #include "nsNameSpaceManager.h"
15 #include "nsStyleConsts.h"
16 #include "nsContentUtils.h"
17 #include "nsContentCreatorFunctions.h"
18 #include "nsCSSPseudoElements.h"
19 #include "nsLayoutUtils.h"
21 #ifdef ACCESSIBILITY
22 # include "mozilla/a11y/AccTypes.h"
23 #endif
25 using namespace mozilla;
26 using namespace mozilla::dom;
28 nsIFrame* NS_NewNumberControlFrame(PresShell* aPresShell,
29 ComputedStyle* aStyle) {
30 return new (aPresShell)
31 nsNumberControlFrame(aStyle, aPresShell->GetPresContext());
34 NS_IMPL_FRAMEARENA_HELPERS(nsNumberControlFrame)
36 NS_QUERYFRAME_HEAD(nsNumberControlFrame)
37 NS_QUERYFRAME_ENTRY(nsNumberControlFrame)
38 NS_QUERYFRAME_TAIL_INHERITING(nsTextControlFrame)
40 nsNumberControlFrame::nsNumberControlFrame(ComputedStyle* aStyle,
41 nsPresContext* aPresContext)
42 : nsTextControlFrame(aStyle, aPresContext, kClassID) {}
44 void nsNumberControlFrame::Destroy(DestroyContext& aContext) {
45 aContext.AddAnonymousContent(mSpinBox.forget());
46 nsTextControlFrame::Destroy(aContext);
49 nsresult nsNumberControlFrame::CreateAnonymousContent(
50 nsTArray<ContentInfo>& aElements) {
51 // We create an anonymous tree for our input element that is structured as
52 // follows:
54 // input
55 // div - placeholder
56 // div - preview div
57 // div - editor root
58 // div - spin box wrapping up/down arrow buttons
59 // div - spin up (up arrow button)
60 // div - spin down (down arrow button)
62 // If you change this, be careful to change the order of stuff returned in
63 // AppendAnonymousContentTo.
65 nsTextControlFrame::CreateAnonymousContent(aElements);
67 #if defined(MOZ_WIDGET_ANDROID)
68 // We don't want spin buttons on Android
69 return NS_OK;
70 #else
71 // The author has elected to hide the spinner by setting this
72 // -moz-appearance. We will reframe if it changes.
73 if (StyleDisplay()->EffectiveAppearance() == StyleAppearance::Textfield) {
74 return NS_OK;
77 // Create the ::-moz-number-spin-box pseudo-element:
78 mSpinBox = MakeAnonElement(PseudoStyleType::mozNumberSpinBox);
80 // Create the ::-moz-number-spin-up pseudo-element:
81 mSpinUp = MakeAnonElement(PseudoStyleType::mozNumberSpinUp, mSpinBox);
83 // Create the ::-moz-number-spin-down pseudo-element:
84 mSpinDown = MakeAnonElement(PseudoStyleType::mozNumberSpinDown, mSpinBox);
86 aElements.AppendElement(mSpinBox);
88 return NS_OK;
89 #endif
92 /* static */
93 nsNumberControlFrame* nsNumberControlFrame::GetNumberControlFrameForSpinButton(
94 nsIFrame* aFrame) {
95 // If aFrame is a spin button for an <input type=number> then we expect the
96 // frame of the NAC root parent to be that input's frame. We have to check for
97 // this via the content tree because we don't know whether extra frames will
98 // be wrapped around any of the elements between aFrame and the
99 // nsNumberControlFrame that we're looking for (e.g. flex wrappers).
100 nsIContent* content = aFrame->GetContent();
101 auto* nacHost = content->GetClosestNativeAnonymousSubtreeRootParentOrHost();
102 if (!nacHost) {
103 return nullptr;
105 auto* input = HTMLInputElement::FromNode(nacHost);
106 if (!input || input->ControlType() != FormControlType::InputNumber) {
107 return nullptr;
109 return do_QueryFrame(input->GetPrimaryFrame());
112 int32_t nsNumberControlFrame::GetSpinButtonForPointerEvent(
113 WidgetGUIEvent* aEvent) const {
114 MOZ_ASSERT(aEvent->mClass == eMouseEventClass, "Unexpected event type");
116 if (!mSpinBox) {
117 // we don't have a spinner
118 return eSpinButtonNone;
120 if (aEvent->mOriginalTarget == mSpinUp) {
121 return eSpinButtonUp;
123 if (aEvent->mOriginalTarget == mSpinDown) {
124 return eSpinButtonDown;
126 if (aEvent->mOriginalTarget == mSpinBox) {
127 // In the case that the up/down buttons are hidden (display:none) we use
128 // just the spin box element, spinning up if the pointer is over the top
129 // half of the element, or down if it's over the bottom half. This is
130 // important to handle since this is the state things are in for the
131 // default UA style sheet. See the comment in forms.css for why.
132 LayoutDeviceIntPoint absPoint = aEvent->mRefPoint;
133 nsPoint point = nsLayoutUtils::GetEventCoordinatesRelativeTo(
134 aEvent, absPoint, RelativeTo{mSpinBox->GetPrimaryFrame()});
135 if (point != nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE)) {
136 if (point.y < mSpinBox->GetPrimaryFrame()->GetSize().height / 2) {
137 return eSpinButtonUp;
139 return eSpinButtonDown;
142 return eSpinButtonNone;
145 void nsNumberControlFrame::SpinnerStateChanged() const {
146 if (mSpinUp) {
147 nsIFrame* spinUpFrame = mSpinUp->GetPrimaryFrame();
148 if (spinUpFrame && spinUpFrame->IsThemed()) {
149 spinUpFrame->InvalidateFrame();
152 if (mSpinDown) {
153 nsIFrame* spinDownFrame = mSpinDown->GetPrimaryFrame();
154 if (spinDownFrame && spinDownFrame->IsThemed()) {
155 spinDownFrame->InvalidateFrame();
160 bool nsNumberControlFrame::SpinnerUpButtonIsDepressed() const {
161 return HTMLInputElement::FromNode(mContent)
162 ->NumberSpinnerUpButtonIsDepressed();
165 bool nsNumberControlFrame::SpinnerDownButtonIsDepressed() const {
166 return HTMLInputElement::FromNode(mContent)
167 ->NumberSpinnerDownButtonIsDepressed();
170 void nsNumberControlFrame::AppendAnonymousContentTo(
171 nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
172 nsTextControlFrame::AppendAnonymousContentTo(aElements, aFilter);
173 if (mSpinBox) {
174 aElements.AppendElement(mSpinBox);
178 #ifdef ACCESSIBILITY
179 a11y::AccType nsNumberControlFrame::AccessibleType() {
180 return a11y::eHTMLSpinnerType;
182 #endif