1/*
2 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5 * Copyright (C) 2003, 2005, 2006, 2008, 2010 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#include "config.h"
24#include "core/dom/WheelEvent.h"
25
26#include "core/dom/Clipboard.h"
27#include "core/dom/EventNames.h"
28#include "core/platform/PlatformWheelEvent.h"
29
30namespace WebCore {
31
32WheelEventInit::WheelEventInit()
33    : wheelDeltaX(0)
34    , wheelDeltaY(0)
35    , deltaMode(WheelEvent::DOM_DELTA_PIXEL)
36{
37}
38
39WheelEvent::WheelEvent()
40    : m_deltaMode(DOM_DELTA_PIXEL)
41    , m_directionInvertedFromDevice(false)
42{
43    ScriptWrappable::init(this);
44}
45
46WheelEvent::WheelEvent(const AtomicString& type, const WheelEventInit& initializer)
47    : MouseEvent(type, initializer)
48    , m_wheelDelta(IntPoint(initializer.wheelDeltaX, initializer.wheelDeltaY))
49    , m_deltaMode(initializer.deltaMode)
50{
51    ScriptWrappable::init(this);
52}
53
54WheelEvent::WheelEvent(const FloatPoint& wheelTicks, const FloatPoint& rawDelta, unsigned deltaMode,
55    PassRefPtr<AbstractView> view, const IntPoint& screenLocation, const IntPoint& pageLocation,
56    bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool directionInvertedFromDevice)
57    : MouseEvent(eventNames().mousewheelEvent,
58                 true, true, view, 0, screenLocation.x(), screenLocation.y(),
59                 pageLocation.x(), pageLocation.y(),
60                 0, 0,
61                 ctrlKey, altKey, shiftKey, metaKey, 0, 0, 0, false)
62    , m_wheelDelta(IntPoint(static_cast<int>(wheelTicks.x() * TickMultiplier), static_cast<int>(wheelTicks.y() * TickMultiplier)))
63    , m_rawDelta(roundedIntPoint(rawDelta))
64    , m_deltaMode(deltaMode)
65    , m_directionInvertedFromDevice(directionInvertedFromDevice)
66{
67    ScriptWrappable::init(this);
68}
69
70void WheelEvent::initWheelEvent(int rawDeltaX, int rawDeltaY, PassRefPtr<AbstractView> view,
71                                int screenX, int screenY, int pageX, int pageY,
72                                bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
73{
74    if (dispatched())
75        return;
76
77    initUIEvent(eventNames().mousewheelEvent, true, true, view, 0);
78
79    m_screenLocation = IntPoint(screenX, screenY);
80    m_ctrlKey = ctrlKey;
81    m_altKey = altKey;
82    m_shiftKey = shiftKey;
83    m_metaKey = metaKey;
84
85    // Normalize to the Windows 120 multiple
86    m_wheelDelta = IntPoint(rawDeltaX * TickMultiplier, rawDeltaY * TickMultiplier);
87
88    m_rawDelta = IntPoint(rawDeltaX, rawDeltaY);
89    m_deltaMode = DOM_DELTA_PIXEL;
90    m_directionInvertedFromDevice = false;
91
92    initCoordinates(IntPoint(pageX, pageY));
93}
94
95void WheelEvent::initWebKitWheelEvent(int rawDeltaX, int rawDeltaY, PassRefPtr<AbstractView> view,
96                                      int screenX, int screenY, int pageX, int pageY,
97                                      bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
98{
99    initWheelEvent(rawDeltaX, rawDeltaY, view, screenX, screenY, pageX, pageY,
100                   ctrlKey, altKey, shiftKey, metaKey);
101}
102
103const AtomicString& WheelEvent::interfaceName() const
104{
105    return eventNames().interfaceForWheelEvent;
106}
107
108bool WheelEvent::isMouseEvent() const
109{
110    return false;
111}
112
113inline static unsigned deltaMode(const PlatformWheelEvent& event)
114{
115    return event.granularity() == ScrollByPageWheelEvent ? WheelEvent::DOM_DELTA_PAGE : WheelEvent::DOM_DELTA_PIXEL;
116}
117
118PassRefPtr<WheelEventDispatchMediator> WheelEventDispatchMediator::create(const PlatformWheelEvent& event, PassRefPtr<AbstractView> view)
119{
120    return adoptRef(new WheelEventDispatchMediator(event, view));
121}
122
123WheelEventDispatchMediator::WheelEventDispatchMediator(const PlatformWheelEvent& event, PassRefPtr<AbstractView> view)
124{
125    if (!(event.deltaX() || event.deltaY()))
126        return;
127
128    setEvent(WheelEvent::create(FloatPoint(event.wheelTicksX(), event.wheelTicksY()), FloatPoint(event.deltaX(), event.deltaY()),
129        deltaMode(event), view, event.globalPosition(), event.position(),
130        event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(), event.directionInvertedFromDevice()));
131}
132
133WheelEvent* WheelEventDispatchMediator::event() const
134{
135    return static_cast<WheelEvent*>(EventDispatchMediator::event());
136}
137
138bool WheelEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
139{
140    ASSERT(event());
141    return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();
142}
143
144} // namespace WebCore
145