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 COMPUTER, 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 COMPUTER, 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    : m_isAccepted(false)
70    , m_shiftKey(false)
71    , m_ctrlKey(false)
72    , m_altKey(false)
73    , m_metaKey(false)
74{
75    m_deltaX = delta.width();
76    m_deltaY = delta.height();
77
78    m_wheelTicksX = m_deltaX;
79    m_wheelTicksY = m_deltaY;
80
81    // Global Position is just x, y location of event
82    POINT point = {location.x(), location.y()};
83    m_globalPosition = point;
84
85    // Position needs to be translated to our client
86    ScreenToClient(hWnd, &point);
87    m_position = point;
88}
89
90PlatformWheelEvent::PlatformWheelEvent(HWND hWnd, WPARAM wParam, LPARAM lParam, bool isMouseHWheel)
91    : m_position(positionForEvent(hWnd, lParam))
92    , m_globalPosition(globalPositionForEvent(hWnd, lParam))
93    , m_isAccepted(false)
94    , m_shiftKey(wParam & MK_SHIFT)
95    , m_ctrlKey(wParam & MK_CONTROL)
96    , m_altKey(GetKeyState(VK_MENU) & HIGH_BIT_MASK_SHORT)
97    , m_metaKey(m_altKey) // FIXME: We'll have to test other browsers
98{
99    // How many pixels should we scroll per line?  Gecko uses the height of the
100    // current line, which means scroll distance changes as you go through the
101    // page or go to different pages.  IE 7 is ~50 px/line, although the value
102    // seems to vary slightly by page and zoom level.  Since IE 7 has a
103    // smoothing algorithm on scrolling, it can get away with slightly larger
104    // scroll values without feeling jerky.  Here we use 100 px per three lines
105    // (the default scroll amount on Windows is three lines per wheel tick).
106    static const float cScrollbarPixelsPerLine = 100.0f / 3.0f;
107    float delta = GET_WHEEL_DELTA_WPARAM(wParam) / static_cast<float>(WHEEL_DELTA);
108    if (isMouseHWheel) {
109        // Windows is <-- -/+ -->, WebKit wants <-- +/- -->, so we negate
110        // |delta| after saving the original value on the wheel tick member.
111        m_wheelTicksX = delta;
112        m_wheelTicksY = 0;
113        delta = -delta;
114    } else {
115        // Even though we use shift + vertical wheel to scroll horizontally in
116        // WebKit, we still note it as a vertical scroll on the wheel tick
117        // member, so that the DOM event we later construct will match the real
118        // hardware event better.
119        m_wheelTicksX = 0;
120        m_wheelTicksY = delta;
121    }
122    if (isMouseHWheel || m_shiftKey) {
123        m_deltaX = delta * static_cast<float>(horizontalScrollChars()) * cScrollbarPixelsPerLine;
124        m_deltaY = 0;
125        m_granularity = ScrollByPixelWheelEvent;
126    } else {
127        m_deltaX = 0;
128        m_deltaY = delta;
129        int verticalMultiplier = verticalScrollLines();
130        m_granularity = (verticalMultiplier == WHEEL_PAGESCROLL) ? ScrollByPageWheelEvent : ScrollByPixelWheelEvent;
131        if (m_granularity == ScrollByPixelWheelEvent)
132            m_deltaY *= static_cast<float>(verticalMultiplier) * cScrollbarPixelsPerLine;
133    }
134}
135
136}
137