Have is<>(T*) function do a null check on the pointer argument
[WebKit-https.git] / Source / WebCore / html / shadow / SpinButtonElement.cpp
1 /*
2  * Copyright (C) 2006, 2008, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE 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 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.
25  */
26
27 #include "config.h"
28 #include "SpinButtonElement.h"
29
30 #include "Chrome.h"
31 #include "EventHandler.h"
32 #include "EventNames.h"
33 #include "Frame.h"
34 #include "HTMLNames.h"
35 #include "MouseEvent.h"
36 #include "Page.h"
37 #include "RenderBox.h"
38 #include "ScrollbarTheme.h"
39 #include "WheelEvent.h"
40 #include <wtf/Ref.h>
41
42 namespace WebCore {
43
44 using namespace HTMLNames;
45
46 inline SpinButtonElement::SpinButtonElement(Document& document, SpinButtonOwner& spinButtonOwner)
47     : HTMLDivElement(divTag, document)
48     , m_spinButtonOwner(&spinButtonOwner)
49     , m_capturing(false)
50     , m_upDownState(Indeterminate)
51     , m_pressStartingState(Indeterminate)
52     , m_repeatingTimer(this, &SpinButtonElement::repeatingTimerFired)
53 {
54     setHasCustomStyleResolveCallbacks();
55     setPseudo(AtomicString("-webkit-inner-spin-button", AtomicString::ConstructFromLiteral));
56 }
57
58 PassRefPtr<SpinButtonElement> SpinButtonElement::create(Document& document, SpinButtonOwner& spinButtonOwner)
59 {
60     return adoptRef(new SpinButtonElement(document, spinButtonOwner));
61 }
62
63 void SpinButtonElement::willDetachRenderers()
64 {
65     releaseCapture();
66 }
67
68 void SpinButtonElement::defaultEventHandler(Event* event)
69 {
70     if (!is<MouseEvent>(*event)) {
71         if (!event->defaultHandled())
72             HTMLDivElement::defaultEventHandler(event);
73         return;
74     }
75
76     RenderBox* box = renderBox();
77     if (!box) {
78         if (!event->defaultHandled())
79             HTMLDivElement::defaultEventHandler(event);
80         return;
81     }
82
83     if (!shouldRespondToMouseEvents()) {
84         if (!event->defaultHandled())
85             HTMLDivElement::defaultEventHandler(event);
86         return;
87     }
88
89     MouseEvent& mouseEvent = downcast<MouseEvent>(*event);
90     IntPoint local = roundedIntPoint(box->absoluteToLocal(mouseEvent.absoluteLocation(), UseTransforms));
91     if (mouseEvent.type() == eventNames().mousedownEvent && mouseEvent.button() == LeftButton) {
92         if (box->pixelSnappedBorderBoxRect().contains(local)) {
93             // The following functions of HTMLInputElement may run JavaScript
94             // code which detaches this shadow node. We need to take a reference
95             // and check renderer() after such function calls.
96             Ref<SpinButtonElement> protect(*this);
97             if (m_spinButtonOwner)
98                 m_spinButtonOwner->focusAndSelectSpinButtonOwner();
99             if (renderer()) {
100                 if (m_upDownState != Indeterminate) {
101                     // A JavaScript event handler called in doStepAction() below
102                     // might change the element state and we might need to
103                     // cancel the repeating timer by the state change. If we
104                     // started the timer after doStepAction(), we would have no
105                     // chance to cancel the timer.
106                     startRepeatingTimer();
107                     doStepAction(m_upDownState == Up ? 1 : -1);
108                 }
109             }
110             mouseEvent.setDefaultHandled();
111         }
112     } else if (mouseEvent.type() == eventNames().mouseupEvent && mouseEvent.button() == LeftButton)
113         stopRepeatingTimer();
114     else if (mouseEvent.type() == eventNames().mousemoveEvent) {
115         if (box->pixelSnappedBorderBoxRect().contains(local)) {
116             if (!m_capturing) {
117                 if (Frame* frame = document().frame()) {
118                     frame->eventHandler().setCapturingMouseEventsElement(this);
119                     m_capturing = true;
120                     if (Page* page = document().page())
121                         page->chrome().registerPopupOpeningObserver(this);
122                 }
123             }
124             UpDownState oldUpDownState = m_upDownState;
125             m_upDownState = local.y() < box->height() / 2 ? Up : Down;
126             if (m_upDownState != oldUpDownState)
127                 renderer()->repaint();
128         } else {
129             releaseCapture();
130             m_upDownState = Indeterminate;
131         }
132     }
133
134     if (!mouseEvent.defaultHandled())
135         HTMLDivElement::defaultEventHandler(&mouseEvent);
136 }
137
138 void SpinButtonElement::willOpenPopup()
139 {
140     releaseCapture();
141     m_upDownState = Indeterminate;
142 }
143
144 void SpinButtonElement::forwardEvent(Event* event)
145 {
146     if (!renderBox())
147         return;
148
149     if (!is<WheelEvent>(*event))
150         return;
151
152     if (!m_spinButtonOwner)
153         return;
154
155     if (!m_spinButtonOwner->shouldSpinButtonRespondToWheelEvents())
156         return;
157
158     doStepAction(downcast<WheelEvent>(*event).wheelDeltaY());
159     event->setDefaultHandled();
160 }
161
162 bool SpinButtonElement::willRespondToMouseMoveEvents()
163 {
164     if (renderBox() && shouldRespondToMouseEvents())
165         return true;
166
167     return HTMLDivElement::willRespondToMouseMoveEvents();
168 }
169
170 bool SpinButtonElement::willRespondToMouseClickEvents()
171 {
172     if (renderBox() && shouldRespondToMouseEvents())
173         return true;
174
175     return HTMLDivElement::willRespondToMouseClickEvents();
176 }
177
178 void SpinButtonElement::doStepAction(int amount)
179 {
180     if (!m_spinButtonOwner)
181         return;
182
183     if (amount > 0)
184         m_spinButtonOwner->spinButtonStepUp();
185     else if (amount < 0)
186         m_spinButtonOwner->spinButtonStepDown();
187 }
188
189 void SpinButtonElement::releaseCapture()
190 {
191     stopRepeatingTimer();
192     if (m_capturing) {
193         if (Frame* frame = document().frame()) {
194             frame->eventHandler().setCapturingMouseEventsElement(nullptr);
195             m_capturing = false;
196             if (Page* page = document().page())
197                 page->chrome().unregisterPopupOpeningObserver(this);
198         }
199     }
200 }
201
202 bool SpinButtonElement::matchesReadWritePseudoClass() const
203 {
204     return shadowHost()->matchesReadWritePseudoClass();
205 }
206
207 void SpinButtonElement::startRepeatingTimer()
208 {
209     m_pressStartingState = m_upDownState;
210     ScrollbarTheme* theme = ScrollbarTheme::theme();
211     m_repeatingTimer.start(theme->initialAutoscrollTimerDelay(), theme->autoscrollTimerDelay());
212 }
213
214 void SpinButtonElement::stopRepeatingTimer()
215 {
216     m_repeatingTimer.stop();
217 }
218
219 void SpinButtonElement::step(int amount)
220 {
221     if (!shouldRespondToMouseEvents())
222         return;
223     // On Mac OS, NSStepper updates the value for the button under the mouse
224     // cursor regardless of the button pressed at the beginning. So the
225     // following check is not needed for Mac OS.
226 #if !OS(MAC_OS_X)
227     if (m_upDownState != m_pressStartingState)
228         return;
229 #endif
230     doStepAction(amount);
231 }
232     
233 void SpinButtonElement::repeatingTimerFired(Timer<SpinButtonElement>*)
234 {
235     if (m_upDownState != Indeterminate)
236         step(m_upDownState == Up ? 1 : -1);
237 }
238
239 void SpinButtonElement::setHovered(bool flag)
240 {
241     if (!flag)
242         m_upDownState = Indeterminate;
243     HTMLDivElement::setHovered(flag);
244 }
245
246 bool SpinButtonElement::shouldRespondToMouseEvents()
247 {
248     return !m_spinButtonOwner || m_spinButtonOwner->shouldSpinButtonRespondToMouseEvents();
249 }
250
251 }