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/. */
6 
7 #include "nsNumberControlFrame.h"
8 
9 #include "mozilla/BasicEvents.h"
10 #include "mozilla/EventStates.h"
11 #include "mozilla/FloatingPoint.h"
12 #include "mozilla/PresShell.h"
13 #include "HTMLInputElement.h"
14 #include "nsGkAtoms.h"
15 #include "nsNameSpaceManager.h"
16 #include "nsStyleConsts.h"
17 #include "nsContentUtils.h"
18 #include "nsContentCreatorFunctions.h"
19 #include "nsCSSPseudoElements.h"
20 #include "nsLayoutUtils.h"
21 
22 #ifdef ACCESSIBILITY
23 #  include "mozilla/a11y/AccTypes.h"
24 #endif
25 
26 using namespace mozilla;
27 using namespace mozilla::dom;
28 
NS_NewNumberControlFrame(PresShell * aPresShell,ComputedStyle * aStyle)29 nsIFrame* NS_NewNumberControlFrame(PresShell* aPresShell,
30                                    ComputedStyle* aStyle) {
31   return new (aPresShell)
32       nsNumberControlFrame(aStyle, aPresShell->GetPresContext());
33 }
34 
35 NS_IMPL_FRAMEARENA_HELPERS(nsNumberControlFrame)
36 
NS_QUERYFRAME_HEAD(nsNumberControlFrame)37 NS_QUERYFRAME_HEAD(nsNumberControlFrame)
38   NS_QUERYFRAME_ENTRY(nsNumberControlFrame)
39 NS_QUERYFRAME_TAIL_INHERITING(nsTextControlFrame)
40 
41 nsNumberControlFrame::nsNumberControlFrame(ComputedStyle* aStyle,
42                                            nsPresContext* aPresContext)
43     : nsTextControlFrame(aStyle, aPresContext, kClassID) {}
44 
DestroyFrom(nsIFrame * aDestructRoot,PostDestroyData & aPostDestroyData)45 void nsNumberControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
46                                        PostDestroyData& aPostDestroyData) {
47   aPostDestroyData.AddAnonymousContent(mSpinBox.forget());
48   nsTextControlFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
49 }
50 
CreateAnonymousContent(nsTArray<ContentInfo> & aElements)51 nsresult nsNumberControlFrame::CreateAnonymousContent(
52     nsTArray<ContentInfo>& aElements) {
53   // We create an anonymous tree for our input element that is structured as
54   // follows:
55   //
56   // input
57   //   div    - placeholder
58   //   div    - preview div
59   //   div    - editor root
60   //   div    - spin box wrapping up/down arrow buttons
61   //     div  - spin up (up arrow button)
62   //     div  - spin down (down arrow button)
63   //
64   // If you change this, be careful to change the order of stuff returned in
65   // AppendAnonymousContentTo.
66 
67   nsTextControlFrame::CreateAnonymousContent(aElements);
68 
69   // The author has elected to hide the spinner by setting this
70   // -moz-appearance. We will reframe if it changes.
71   if (StyleDisplay()->EffectiveAppearance() != StyleAppearance::Textfield) {
72     // Create the ::-moz-number-spin-box pseudo-element:
73     mSpinBox = MakeAnonElement(PseudoStyleType::mozNumberSpinBox);
74 
75     // Create the ::-moz-number-spin-up pseudo-element:
76     mSpinUp = MakeAnonElement(PseudoStyleType::mozNumberSpinUp, mSpinBox);
77 
78     // Create the ::-moz-number-spin-down pseudo-element:
79     mSpinDown = MakeAnonElement(PseudoStyleType::mozNumberSpinDown, mSpinBox);
80 
81     aElements.AppendElement(mSpinBox);
82   }
83 
84   return NS_OK;
85 }
86 
87 /* static */
GetNumberControlFrameForSpinButton(nsIFrame * aFrame)88 nsNumberControlFrame* nsNumberControlFrame::GetNumberControlFrameForSpinButton(
89     nsIFrame* aFrame) {
90   // If aFrame is a spin button for an <input type=number> then we expect the
91   // frame of the NAC root parent to be that input's frame. We have to check for
92   // this via the content tree because we don't know whether extra frames will
93   // be wrapped around any of the elements between aFrame and the
94   // nsNumberControlFrame that we're looking for (e.g. flex wrappers).
95   nsIContent* content = aFrame->GetContent();
96   auto* nacHost = content->GetClosestNativeAnonymousSubtreeRootParent();
97   if (!nacHost) {
98     return nullptr;
99   }
100   auto* input = HTMLInputElement::FromNode(nacHost);
101   if (!input || input->ControlType() != FormControlType::InputNumber) {
102     return nullptr;
103   }
104   return do_QueryFrame(input->GetPrimaryFrame());
105 }
106 
GetSpinButtonForPointerEvent(WidgetGUIEvent * aEvent) const107 int32_t nsNumberControlFrame::GetSpinButtonForPointerEvent(
108     WidgetGUIEvent* aEvent) const {
109   MOZ_ASSERT(aEvent->mClass == eMouseEventClass, "Unexpected event type");
110 
111   if (!mSpinBox) {
112     // we don't have a spinner
113     return eSpinButtonNone;
114   }
115   if (aEvent->mOriginalTarget == mSpinUp) {
116     return eSpinButtonUp;
117   }
118   if (aEvent->mOriginalTarget == mSpinDown) {
119     return eSpinButtonDown;
120   }
121   if (aEvent->mOriginalTarget == mSpinBox) {
122     // In the case that the up/down buttons are hidden (display:none) we use
123     // just the spin box element, spinning up if the pointer is over the top
124     // half of the element, or down if it's over the bottom half. This is
125     // important to handle since this is the state things are in for the
126     // default UA style sheet. See the comment in forms.css for why.
127     LayoutDeviceIntPoint absPoint = aEvent->mRefPoint;
128     nsPoint point = nsLayoutUtils::GetEventCoordinatesRelativeTo(
129         aEvent, absPoint, RelativeTo{mSpinBox->GetPrimaryFrame()});
130     if (point != nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE)) {
131       if (point.y < mSpinBox->GetPrimaryFrame()->GetSize().height / 2) {
132         return eSpinButtonUp;
133       }
134       return eSpinButtonDown;
135     }
136   }
137   return eSpinButtonNone;
138 }
139 
SpinnerStateChanged() const140 void nsNumberControlFrame::SpinnerStateChanged() const {
141   MOZ_ASSERT(mSpinUp && mSpinDown,
142              "We should not be called when we have no spinner");
143 
144   nsIFrame* spinUpFrame = mSpinUp->GetPrimaryFrame();
145   if (spinUpFrame && spinUpFrame->IsThemed()) {
146     spinUpFrame->InvalidateFrame();
147   }
148   nsIFrame* spinDownFrame = mSpinDown->GetPrimaryFrame();
149   if (spinDownFrame && spinDownFrame->IsThemed()) {
150     spinDownFrame->InvalidateFrame();
151   }
152 }
153 
SpinnerUpButtonIsDepressed() const154 bool nsNumberControlFrame::SpinnerUpButtonIsDepressed() const {
155   return HTMLInputElement::FromNode(mContent)
156       ->NumberSpinnerUpButtonIsDepressed();
157 }
158 
SpinnerDownButtonIsDepressed() const159 bool nsNumberControlFrame::SpinnerDownButtonIsDepressed() const {
160   return HTMLInputElement::FromNode(mContent)
161       ->NumberSpinnerDownButtonIsDepressed();
162 }
163 
AppendAnonymousContentTo(nsTArray<nsIContent * > & aElements,uint32_t aFilter)164 void nsNumberControlFrame::AppendAnonymousContentTo(
165     nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
166   nsTextControlFrame::AppendAnonymousContentTo(aElements, aFilter);
167   if (mSpinBox) {
168     aElements.AppendElement(mSpinBox);
169   }
170 }
171 
172 #ifdef ACCESSIBILITY
AccessibleType()173 a11y::AccType nsNumberControlFrame::AccessibleType() {
174   return a11y::eHTMLSpinnerType;
175 }
176 #endif
177