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 * 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 "core/events/WheelEvent.h"
26
27#include "core/clipboard/DataTransfer.h"
28#include "platform/PlatformMouseEvent.h"
29#include "platform/PlatformWheelEvent.h"
30
31namespace blink {
32
33WheelEventInit::WheelEventInit()
34    : deltaX(0)
35    , deltaY(0)
36    , deltaZ(0)
37    , wheelDeltaX(0)
38    , wheelDeltaY(0)
39    , deltaMode(WheelEvent::DOM_DELTA_PIXEL)
40{
41}
42
43WheelEvent::WheelEvent()
44    : m_deltaX(0)
45    , m_deltaY(0)
46    , m_deltaZ(0)
47    , m_deltaMode(DOM_DELTA_PIXEL)
48{
49}
50
51WheelEvent::WheelEvent(const AtomicString& type, const WheelEventInit& initializer)
52    : MouseEvent(type, initializer)
53    , m_wheelDelta(initializer.wheelDeltaX ? initializer.wheelDeltaX : -initializer.deltaX, initializer.wheelDeltaY ? initializer.wheelDeltaY : -initializer.deltaY)
54    , m_deltaX(initializer.deltaX ? initializer.deltaX : -initializer.wheelDeltaX)
55    , m_deltaY(initializer.deltaY ? initializer.deltaY : -initializer.wheelDeltaY)
56    , m_deltaZ(initializer.deltaZ)
57    , m_deltaMode(initializer.deltaMode)
58{
59}
60
61WheelEvent::WheelEvent(const FloatPoint& wheelTicks, const FloatPoint& rawDelta, unsigned deltaMode,
62    PassRefPtrWillBeRawPtr<AbstractView> view, const IntPoint& screenLocation, const IntPoint& pageLocation,
63    bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
64    : MouseEvent(EventTypeNames::wheel, true, true, view, 0, screenLocation.x(), screenLocation.y(),
65        pageLocation.x(), pageLocation.y(), 0, 0, ctrlKey, altKey, shiftKey, metaKey, 0, nullptr,
66        nullptr, false, PlatformMouseEvent::RealOrIndistinguishable)
67    , m_wheelDelta(wheelTicks.x() * TickMultiplier, wheelTicks.y() * TickMultiplier)
68    , m_deltaX(-rawDelta.x())
69    , m_deltaY(-rawDelta.y())
70    , m_deltaZ(0)
71    , m_deltaMode(deltaMode)
72{
73}
74
75const AtomicString& WheelEvent::interfaceName() const
76{
77    return EventNames::WheelEvent;
78}
79
80bool WheelEvent::isMouseEvent() const
81{
82    return false;
83}
84
85bool WheelEvent::isWheelEvent() const
86{
87    return true;
88}
89
90void WheelEvent::trace(Visitor* visitor)
91{
92    MouseEvent::trace(visitor);
93}
94
95inline static unsigned deltaMode(const PlatformWheelEvent& event)
96{
97    return event.granularity() == ScrollByPageWheelEvent ? WheelEvent::DOM_DELTA_PAGE : WheelEvent::DOM_DELTA_PIXEL;
98}
99
100PassRefPtrWillBeRawPtr<WheelEventDispatchMediator> WheelEventDispatchMediator::create(const PlatformWheelEvent& event, PassRefPtrWillBeRawPtr<AbstractView> view)
101{
102    return adoptRefWillBeNoop(new WheelEventDispatchMediator(event, view));
103}
104
105WheelEventDispatchMediator::WheelEventDispatchMediator(const PlatformWheelEvent& event, PassRefPtrWillBeRawPtr<AbstractView> view)
106{
107    if (!(event.deltaX() || event.deltaY()))
108        return;
109
110    setEvent(WheelEvent::create(FloatPoint(event.wheelTicksX(), event.wheelTicksY()), FloatPoint(event.deltaX(), event.deltaY()),
111        deltaMode(event), view, event.globalPosition(), event.position(),
112        event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey()));
113}
114
115WheelEvent* WheelEventDispatchMediator::event() const
116{
117    return toWheelEvent(EventDispatchMediator::event());
118}
119
120bool WheelEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
121{
122    ASSERT(event());
123    return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();
124}
125
126} // namespace blink
127