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, 2013 Apple Inc. All rights reserved.
6 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB.  If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24#include "config.h"
25#include "WheelEvent.h"
26
27#include "DataTransfer.h"
28#include "EventNames.h"
29#include <wtf/MathExtras.h>
30
31namespace WebCore {
32
33inline static unsigned determineDeltaMode(const PlatformWheelEvent& event)
34{
35    return event.granularity() == ScrollByPageWheelEvent ? WheelEvent::DOM_DELTA_PAGE : WheelEvent::DOM_DELTA_PIXEL;
36}
37
38WheelEventInit::WheelEventInit()
39    : deltaX(0)
40    , deltaY(0)
41    , deltaZ(0)
42    , deltaMode(WheelEvent::DOM_DELTA_PIXEL)
43    , wheelDeltaX(0)
44    , wheelDeltaY(0)
45{
46}
47
48WheelEvent::WheelEvent()
49    : m_deltaX(0)
50    , m_deltaY(0)
51    , m_deltaZ(0)
52    , m_deltaMode(DOM_DELTA_PIXEL)
53    , m_directionInvertedFromDevice(false)
54{
55}
56
57WheelEvent::WheelEvent(const AtomicString& type, const WheelEventInit& initializer)
58    : MouseEvent(type, initializer)
59    , m_wheelDelta(initializer.wheelDeltaX ? initializer.wheelDeltaX : -initializer.deltaX, initializer.wheelDeltaY ? initializer.wheelDeltaY : -initializer.deltaY)
60    , m_deltaX(initializer.deltaX ? initializer.deltaX : -initializer.wheelDeltaX)
61    , m_deltaY(initializer.deltaY ? initializer.deltaY : -initializer.wheelDeltaY)
62    , m_deltaZ(initializer.deltaZ)
63    , m_deltaMode(initializer.deltaMode)
64{
65}
66
67WheelEvent::WheelEvent(const PlatformWheelEvent& event, PassRefPtr<AbstractView> view)
68    : MouseEvent(eventNames().wheelEvent, true, true, event.timestamp(), view, 0, event.globalPosition().x(), event.globalPosition().y(), event.position().x(), event.position().y()
69#if ENABLE(POINTER_LOCK)
70                , 0, 0
71#endif
72                , event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(), 0, 0, 0, false)
73    , m_wheelDelta(event.wheelTicksX() * TickMultiplier, event.wheelTicksY() * TickMultiplier)
74    , m_deltaX(-event.deltaX())
75    , m_deltaY(-event.deltaY())
76    , m_deltaZ(0)
77    , m_deltaMode(determineDeltaMode(event))
78    , m_directionInvertedFromDevice(event.directionInvertedFromDevice())
79#if PLATFORM(MAC)
80    , m_phase(event.phase())
81    , m_momentumPhase(event.momentumPhase())
82#endif
83{
84}
85
86void WheelEvent::initWheelEvent(int rawDeltaX, int rawDeltaY, PassRefPtr<AbstractView> view, int screenX, int screenY, int pageX, int pageY, bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
87{
88    if (dispatched())
89        return;
90
91    initUIEvent(eventNames().wheelEvent, true, true, view, 0);
92
93    m_screenLocation = IntPoint(screenX, screenY);
94    m_ctrlKey = ctrlKey;
95    m_altKey = altKey;
96    m_shiftKey = shiftKey;
97    m_metaKey = metaKey;
98
99    // Normalize to 120 multiple for compatibility with IE.
100    m_wheelDelta = IntPoint(rawDeltaX * TickMultiplier, rawDeltaY * TickMultiplier);
101    m_deltaX = -rawDeltaX;
102    m_deltaY = -rawDeltaY;
103
104    m_deltaMode = DOM_DELTA_PIXEL;
105    m_directionInvertedFromDevice = false;
106
107    initCoordinates(IntPoint(pageX, pageY));
108}
109
110void WheelEvent::initWebKitWheelEvent(int rawDeltaX, int rawDeltaY, PassRefPtr<AbstractView> view, int screenX, int screenY, int pageX, int pageY, bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
111{
112    initWheelEvent(rawDeltaX, rawDeltaY, view, screenX, screenY, pageX, pageY, ctrlKey, altKey, shiftKey, metaKey);
113}
114
115EventInterface WheelEvent::eventInterface() const
116{
117    return WheelEventInterfaceType;
118}
119
120bool WheelEvent::isMouseEvent() const
121{
122    return false;
123}
124
125bool WheelEvent::isWheelEvent() const
126{
127    return true;
128}
129
130} // namespace WebCore
131