2 * Copyright (C) 2006, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "core/html/shadow/SpinButtonElement.h"
30 #include "core/HTMLNames.h"
31 #include "core/events/MouseEvent.h"
32 #include "core/events/WheelEvent.h"
33 #include "core/frame/LocalFrame.h"
34 #include "core/html/shadow/ShadowElementNames.h"
35 #include "core/input/EventHandler.h"
36 #include "core/layout/LayoutBox.h"
37 #include "core/page/ChromeClient.h"
38 #include "core/page/Page.h"
39 #include "platform/scroll/ScrollbarTheme.h"
43 using namespace HTMLNames
;
45 inline SpinButtonElement::SpinButtonElement(Document
& document
, SpinButtonOwner
& spinButtonOwner
)
46 : HTMLDivElement(document
)
47 , m_spinButtonOwner(&spinButtonOwner
)
49 , m_upDownState(Indeterminate
)
50 , m_pressStartingState(Indeterminate
)
51 , m_repeatingTimer(this, &SpinButtonElement::repeatingTimerFired
)
55 PassRefPtrWillBeRawPtr
<SpinButtonElement
> SpinButtonElement::create(Document
& document
, SpinButtonOwner
& spinButtonOwner
)
57 RefPtrWillBeRawPtr
<SpinButtonElement
> element
= adoptRefWillBeNoop(new SpinButtonElement(document
, spinButtonOwner
));
58 element
->setShadowPseudoId(AtomicString("-webkit-inner-spin-button", AtomicString::ConstructFromLiteral
));
59 element
->setAttribute(idAttr
, ShadowElementNames::spinButton());
60 return element
.release();
63 void SpinButtonElement::detach(const AttachContext
& context
)
65 releaseCapture(EventDispatchDisallowed
);
66 HTMLDivElement::detach(context
);
69 void SpinButtonElement::defaultEventHandler(Event
* event
)
71 if (!event
->isMouseEvent()) {
72 if (!event
->defaultHandled())
73 HTMLDivElement::defaultEventHandler(event
);
77 LayoutBox
* box
= layoutBox();
79 if (!event
->defaultHandled())
80 HTMLDivElement::defaultEventHandler(event
);
84 if (!shouldRespondToMouseEvents()) {
85 if (!event
->defaultHandled())
86 HTMLDivElement::defaultEventHandler(event
);
90 MouseEvent
* mouseEvent
= toMouseEvent(event
);
91 IntPoint local
= roundedIntPoint(box
->absoluteToLocal(FloatPoint(mouseEvent
->absoluteLocation()), UseTransforms
));
92 if (mouseEvent
->type() == EventTypeNames::mousedown
&& mouseEvent
->button() == LeftButton
) {
93 if (box
->pixelSnappedBorderBoxRect().contains(local
)) {
94 // The following functions of HTMLInputElement may run JavaScript
95 // code which detaches this shadow node. We need to take a reference
96 // and check layoutObject() after such function calls.
97 RefPtrWillBeRawPtr
<Node
> protector(this);
98 if (m_spinButtonOwner
)
99 m_spinButtonOwner
->focusAndSelectSpinButtonOwner();
100 if (layoutObject()) {
101 if (m_upDownState
!= Indeterminate
) {
102 // A JavaScript event handler called in doStepAction() below
103 // might change the element state and we might need to
104 // cancel the repeating timer by the state change. If we
105 // started the timer after doStepAction(), we would have no
106 // chance to cancel the timer.
107 startRepeatingTimer();
108 doStepAction(m_upDownState
== Up
? 1 : -1);
111 event
->setDefaultHandled();
113 } else if (mouseEvent
->type() == EventTypeNames::mouseup
&& mouseEvent
->button() == LeftButton
) {
115 } else if (event
->type() == EventTypeNames::mousemove
) {
116 if (box
->pixelSnappedBorderBoxRect().contains(local
)) {
118 if (LocalFrame
* frame
= document().frame()) {
119 frame
->eventHandler().setCapturingMouseEventsNode(this);
121 if (Page
* page
= document().page())
122 page
->chromeClient().registerPopupOpeningObserver(this);
125 UpDownState oldUpDownState
= m_upDownState
;
126 m_upDownState
= (local
.y() < box
->size().height() / 2) ? Up
: Down
;
127 if (m_upDownState
!= oldUpDownState
)
128 layoutObject()->setShouldDoFullPaintInvalidation();
131 m_upDownState
= Indeterminate
;
135 if (!event
->defaultHandled())
136 HTMLDivElement::defaultEventHandler(event
);
139 void SpinButtonElement::willOpenPopup()
142 m_upDownState
= Indeterminate
;
145 void SpinButtonElement::forwardEvent(Event
* event
)
150 if (!event
->hasInterface(EventNames::WheelEvent
))
153 if (!m_spinButtonOwner
)
156 if (!m_spinButtonOwner
->shouldSpinButtonRespondToWheelEvents())
159 doStepAction(toWheelEvent(event
)->wheelDeltaY());
160 event
->setDefaultHandled();
163 bool SpinButtonElement::willRespondToMouseMoveEvents()
165 if (layoutBox() && shouldRespondToMouseEvents())
168 return HTMLDivElement::willRespondToMouseMoveEvents();
171 bool SpinButtonElement::willRespondToMouseClickEvents()
173 if (layoutBox() && shouldRespondToMouseEvents())
176 return HTMLDivElement::willRespondToMouseClickEvents();
179 void SpinButtonElement::doStepAction(int amount
)
181 if (!m_spinButtonOwner
)
185 m_spinButtonOwner
->spinButtonStepUp();
187 m_spinButtonOwner
->spinButtonStepDown();
190 void SpinButtonElement::releaseCapture(EventDispatch eventDispatch
)
192 stopRepeatingTimer();
194 if (LocalFrame
* frame
= document().frame()) {
195 frame
->eventHandler().setCapturingMouseEventsNode(nullptr);
197 if (Page
* page
= document().page())
198 page
->chromeClient().unregisterPopupOpeningObserver(this);
201 if (m_spinButtonOwner
)
202 m_spinButtonOwner
->spinButtonDidReleaseMouseCapture(eventDispatch
);
206 bool SpinButtonElement::matchesReadOnlyPseudoClass() const
208 return shadowHost()->matchesReadOnlyPseudoClass();
211 bool SpinButtonElement::matchesReadWritePseudoClass() const
213 return shadowHost()->matchesReadWritePseudoClass();
216 void SpinButtonElement::startRepeatingTimer()
218 m_pressStartingState
= m_upDownState
;
219 ScrollbarTheme
* theme
= ScrollbarTheme::theme();
220 m_repeatingTimer
.start(theme
->initialAutoscrollTimerDelay(), theme
->autoscrollTimerDelay(), FROM_HERE
);
223 void SpinButtonElement::stopRepeatingTimer()
225 m_repeatingTimer
.stop();
228 void SpinButtonElement::step(int amount
)
230 if (!shouldRespondToMouseEvents())
232 // On Mac OS, NSStepper updates the value for the button under the mouse
233 // cursor regardless of the button pressed at the beginning. So the
234 // following check is not needed for Mac OS.
236 if (m_upDownState
!= m_pressStartingState
)
239 doStepAction(amount
);
242 void SpinButtonElement::repeatingTimerFired(Timer
<SpinButtonElement
>*)
244 if (m_upDownState
!= Indeterminate
)
245 step(m_upDownState
== Up
? 1 : -1);
248 void SpinButtonElement::setHovered(bool flag
)
251 m_upDownState
= Indeterminate
;
252 HTMLDivElement::setHovered(flag
);
255 bool SpinButtonElement::shouldRespondToMouseEvents()
257 return !m_spinButtonOwner
|| m_spinButtonOwner
->shouldSpinButtonRespondToMouseEvents();
260 DEFINE_TRACE(SpinButtonElement
)
262 visitor
->trace(m_spinButtonOwner
);
263 HTMLDivElement::trace(visitor
);