initial import
[vuplus_webkit] / Source / WebCore / dom / WheelEvent.cpp
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 "WheelEvent.h"
25
26 #include "EventDispatcher.h"
27 #include "EventNames.h"
28 #include "PlatformWheelEvent.h"
29
30 #include <wtf/MathExtras.h>
31
32 namespace WebCore {
33
34 WheelEvent::WheelEvent()
35     : m_granularity(Pixel)
36 {
37 }
38
39 WheelEvent::WheelEvent(const FloatPoint& wheelTicks, const FloatPoint& rawDelta,
40                        Granularity granularity, PassRefPtr<AbstractView> view,
41                        const IntPoint& screenLocation, const IntPoint& pageLocation,
42                        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
43     : MouseRelatedEvent(eventNames().mousewheelEvent,
44                         true, true, view, 0, screenLocation, pageLocation,
45                         ctrlKey, altKey, shiftKey, metaKey)
46     , m_wheelDelta(IntPoint(static_cast<int>(wheelTicks.x() * tickMultiplier), static_cast<int>(wheelTicks.y() * tickMultiplier)))
47     , m_rawDelta(roundedIntPoint(rawDelta))
48     , m_granularity(granularity)
49 {
50 }
51
52 void WheelEvent::initWheelEvent(int rawDeltaX, int rawDeltaY, PassRefPtr<AbstractView> view,
53                                 int screenX, int screenY, int pageX, int pageY,
54                                 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
55 {
56     if (dispatched())
57         return;
58     
59     initUIEvent(eventNames().mousewheelEvent, true, true, view, 0);
60     
61     m_screenLocation = IntPoint(screenX, screenY);
62     m_ctrlKey = ctrlKey;
63     m_altKey = altKey;
64     m_shiftKey = shiftKey;
65     m_metaKey = metaKey;
66     
67     // Normalize to the Windows 120 multiple
68     m_wheelDelta = IntPoint(rawDeltaX * tickMultiplier, rawDeltaY * tickMultiplier);
69     
70     m_rawDelta = IntPoint(rawDeltaX, rawDeltaY);
71     m_granularity = Pixel;
72     
73     initCoordinates(IntPoint(pageX, pageY));
74 }
75
76 void WheelEvent::initWebKitWheelEvent(int rawDeltaX, int rawDeltaY, PassRefPtr<AbstractView> view,
77                                       int screenX, int screenY, int pageX, int pageY,
78                                       bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
79 {
80     initWheelEvent(rawDeltaX, rawDeltaY, view, screenX, screenY, pageX, pageY,
81                    ctrlKey, altKey, shiftKey, metaKey);
82 }
83
84 bool WheelEvent::isWheelEvent() const
85 {
86     return true;
87 }
88
89 inline static WheelEvent::Granularity granularity(const PlatformWheelEvent& event)
90 {
91     return event.granularity() == ScrollByPageWheelEvent ? WheelEvent::Page : WheelEvent::Pixel;
92 }
93
94 PassRefPtr<WheelEventDispatchMediator> WheelEventDispatchMediator::create(const PlatformWheelEvent& event, PassRefPtr<AbstractView> view)
95 {
96     return adoptRef(new WheelEventDispatchMediator(event, view));
97 }
98
99 WheelEventDispatchMediator::WheelEventDispatchMediator(const PlatformWheelEvent& event, PassRefPtr<AbstractView> view)
100 {
101     if (!(event.deltaX() || event.deltaY()))
102         return;
103
104     setEvent(WheelEvent::create(FloatPoint(event.wheelTicksX(), event.wheelTicksY()), FloatPoint(event.deltaX(), event.deltaY()), granularity(event),
105         view, IntPoint(event.globalX(), event.globalY()), IntPoint(event.x(), event.y()), event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey()));
106
107 }
108
109 WheelEvent* WheelEventDispatchMediator::event() const
110 {
111     return static_cast<WheelEvent*>(EventDispatchMediator::event());
112 }
113
114 bool WheelEventDispatchMediator::dispatchEvent(EventDispatcher* dispatcher) const
115 {
116     if (!event())
117         return true;
118
119     return EventDispatchMediator::dispatchEvent(dispatcher) && !event()->defaultHandled();
120 }
121
122 } // namespace WebCore