1/*
2 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "PlatformWheelEvent.h"
28
29#include "FloatPoint.h"
30#include "FloatSize.h"
31#include <windows.h>
32#include <windowsx.h>
33
34namespace WebCore {
35
36#define HIGH_BIT_MASK_SHORT 0x8000
37#define SPI_GETWHEELSCROLLCHARS 0x006C
38
39static IntPoint positionForEvent(HWND hWnd, LPARAM lParam)
40{
41    POINT point = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
42    ScreenToClient(hWnd, &point);
43    return point;
44}
45
46static IntPoint globalPositionForEvent(HWND hWnd, LPARAM lParam)
47{
48    POINT point = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
49    return point;
50}
51
52static int horizontalScrollChars()
53{
54    static ULONG scrollChars;
55    if (!scrollChars && !SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &scrollChars, 0))
56        scrollChars = 1;
57    return scrollChars;
58}
59
60static int verticalScrollLines()
61{
62    static ULONG scrollLines;
63    if (!scrollLines && !SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0))
64        scrollLines = 3;
65    return scrollLines;
66}
67
68PlatformWheelEvent::PlatformWheelEvent(HWND hWnd, const FloatSize& delta, const FloatPoint& location)
69    : PlatformEvent(PlatformEvent::Wheel, false, false, false, false, ::GetTickCount() * 0.001)
70    , m_directionInvertedFromDevice(false)
71{
72    m_deltaX = delta.width();
73    m_deltaY = delta.height();
74
75    m_wheelTicksX = m_deltaX;
76    m_wheelTicksY = m_deltaY;
77
78    // Global Position is just x, y location of event
79    POINT point = {location.x(), location.y()};
80    m_globalPosition = point;
81
82    // Position needs to be translated to our client
83    ScreenToClient(hWnd, &point);
84    m_position = point;
85}
86
87PlatformWheelEvent::PlatformWheelEvent(HWND hWnd, WPARAM wParam, LPARAM lParam, bool isMouseHWheel)
88    : PlatformEvent(PlatformEvent::Wheel, wParam & MK_SHIFT, wParam & MK_CONTROL, GetKeyState(VK_MENU) & HIGH_BIT_MASK_SHORT, GetKeyState(VK_MENU) & HIGH_BIT_MASK_SHORT, ::GetTickCount() * 0.001)
89    , m_position(positionForEvent(hWnd, lParam))
90    , m_globalPosition(globalPositionForEvent(hWnd, lParam))
91    , m_directionInvertedFromDevice(false)
92{
93    // How many pixels should we scroll per line?  Gecko uses the height of the
94    // current line, which means scroll distance changes as you go through the
95    // page or go to different pages.  IE 7 is ~50 px/line, although the value
96    // seems to vary slightly by page and zoom level.  Since IE 7 has a
97    // smoothing algorithm on scrolling, it can get away with slightly larger
98    // scroll values without feeling jerky.  Here we use 100 px per three lines
99    // (the default scroll amount on Windows is three lines per wheel tick).
100    static const float cScrollbarPixelsPerLine = 100.0f / 3.0f;
101    float delta = GET_WHEEL_DELTA_WPARAM(wParam) / static_cast<float>(WHEEL_DELTA);
102    if (isMouseHWheel) {
103        // Windows is <-- -/+ -->, WebKit wants <-- +/- -->, so we negate
104        // |delta| after saving the original value on the wheel tick member.
105        m_wheelTicksX = delta;
106        m_wheelTicksY = 0;
107        delta = -delta;
108    } else {
109        // Even though we use shift + vertical wheel to scroll horizontally in
110        // WebKit, we still note it as a vertical scroll on the wheel tick
111        // member, so that the DOM event we later construct will match the real
112        // hardware event better.
113        m_wheelTicksX = 0;
114        m_wheelTicksY = delta;
115    }
116    if (isMouseHWheel || shiftKey()) {
117        m_deltaX = delta * static_cast<float>(horizontalScrollChars()) * cScrollbarPixelsPerLine;
118        m_deltaY = 0;
119        m_granularity = ScrollByPixelWheelEvent;
120    } else {
121        m_deltaX = 0;
122        m_deltaY = delta;
123        int verticalMultiplier = verticalScrollLines();
124        m_granularity = (verticalMultiplier == WHEEL_PAGESCROLL) ? ScrollByPageWheelEvent : ScrollByPixelWheelEvent;
125        if (m_granularity == ScrollByPixelWheelEvent)
126            m_deltaY *= static_cast<float>(verticalMultiplier) * cScrollbarPixelsPerLine;
127    }
128}
129
130}
131