1 | /* |
2 | * Copyright (C) 2006 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * |
15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | #include "config.h" |
29 | #include "PlatformWheelEvent.h" |
30 | |
31 | #include "FloatPoint.h" |
32 | #include "GtkUtilities.h" |
33 | #include "PlatformKeyboardEvent.h" |
34 | #include "Scrollbar.h" |
35 | #include <gdk/gdk.h> |
36 | #include <gtk/gtk.h> |
37 | #include <wtf/WallTime.h> |
38 | |
39 | namespace WebCore { |
40 | |
41 | // Keep this in sync with the other platform event constructors |
42 | PlatformWheelEvent::PlatformWheelEvent(GdkEventScroll* event) |
43 | { |
44 | static const float delta = 1; |
45 | |
46 | m_type = PlatformEvent::Wheel; |
47 | m_timestamp = wallTimeForEvent(event); |
48 | |
49 | if (event->state & GDK_SHIFT_MASK) |
50 | m_modifiers.add(Modifier::ShiftKey); |
51 | if (event->state & GDK_CONTROL_MASK) |
52 | m_modifiers.add(Modifier::ControlKey); |
53 | if (event->state & GDK_MOD1_MASK) |
54 | m_modifiers.add(Modifier::AltKey); |
55 | if (event->state & GDK_META_MASK) |
56 | m_modifiers.add(Modifier::MetaKey); |
57 | if (PlatformKeyboardEvent::modifiersContainCapsLock(event->state)) |
58 | m_modifiers.add(PlatformEvent::Modifier::CapsLockKey); |
59 | |
60 | m_deltaX = 0; |
61 | m_deltaY = 0; |
62 | |
63 | // Docs say an upwards scroll (away from the user) has a positive delta |
64 | switch (event->direction) { |
65 | case GDK_SCROLL_UP: |
66 | m_deltaY = delta; |
67 | break; |
68 | case GDK_SCROLL_DOWN: |
69 | m_deltaY = -delta; |
70 | break; |
71 | case GDK_SCROLL_LEFT: |
72 | m_deltaX = delta; |
73 | break; |
74 | case GDK_SCROLL_RIGHT: |
75 | m_deltaX = -delta; |
76 | break; |
77 | #if GTK_CHECK_VERSION(3, 3, 18) |
78 | case GDK_SCROLL_SMOOTH: { |
79 | gdouble deltaX, deltaY; |
80 | gdk_event_get_scroll_deltas(reinterpret_cast<GdkEvent*>(event), &deltaX, &deltaY); |
81 | m_deltaX = -deltaX; |
82 | m_deltaY = -deltaY; |
83 | } |
84 | break; |
85 | #endif |
86 | } |
87 | m_wheelTicksX = m_deltaX; |
88 | m_wheelTicksY = m_deltaY; |
89 | |
90 | #if ENABLE(ASYNC_SCROLLING) |
91 | #ifndef GTK_API_VERSION_2 |
92 | #if GTK_CHECK_VERSION(3, 20, 0) |
93 | m_phase = event->is_stop ? |
94 | PlatformWheelEventPhaseEnded : |
95 | PlatformWheelEventPhaseChanged; |
96 | #else |
97 | m_phase = event->direction == GDK_SCROLL_SMOOTH && !m_deltaX && !m_deltaY ? |
98 | PlatformWheelEventPhaseEnded : |
99 | PlatformWheelEventPhaseChanged; |
100 | #endif |
101 | #else |
102 | m_phase = PlatformWheelEventPhaseChanged; |
103 | #endif // GTK_API_VERSION_2 |
104 | #endif // ENABLE(ASYNC_SCROLLING) |
105 | |
106 | m_position = IntPoint(static_cast<int>(event->x), static_cast<int>(event->y)); |
107 | m_globalPosition = IntPoint(static_cast<int>(event->x_root), static_cast<int>(event->y_root)); |
108 | m_granularity = ScrollByPixelWheelEvent; |
109 | m_directionInvertedFromDevice = false; |
110 | |
111 | // FIXME: retrieve the user setting for the number of lines to scroll on each wheel event |
112 | m_deltaX *= static_cast<float>(Scrollbar::pixelsPerLineStep()); |
113 | m_deltaY *= static_cast<float>(Scrollbar::pixelsPerLineStep()); |
114 | } |
115 | |
116 | } // namespace WebCore |
117 | |