Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2011 Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "WebInputEventFactory.h"
     33 
     34 #include "WebInputEvent.h"
     35 #include "core/platform/chromium/KeyCodeConversion.h"
     36 #include "platform/KeyboardCodes.h"
     37 #include "wtf/Assertions.h"
     38 
     39 namespace blink {
     40 
     41 WebKeyboardEvent WebInputEventFactory::keyboardEvent(WebInputEvent::Type type,
     42                                                      int modifiers,
     43                                                      double timeStampSeconds,
     44                                                      int keycode,
     45                                                      WebUChar unicodeCharacter,
     46                                                      bool isSystemKey)
     47 {
     48     WebKeyboardEvent result;
     49 
     50     result.type = type;
     51     result.modifiers = modifiers;
     52     result.timeStampSeconds = timeStampSeconds;
     53     int windowsKeyCode = WebCore::windowsKeyCodeForKeyEvent(keycode);
     54     result.windowsKeyCode = WebKeyboardEvent::windowsKeyCodeWithoutLocation(windowsKeyCode);
     55     result.modifiers |= WebKeyboardEvent::locationModifiersFromWindowsKeyCode(windowsKeyCode);
     56     result.nativeKeyCode = keycode;
     57     result.unmodifiedText[0] = unicodeCharacter;
     58     if (result.windowsKeyCode == WebCore::VKEY_RETURN) {
     59         // This is the same behavior as GTK:
     60         // We need to treat the enter key as a key press of character \r. This
     61         // is apparently just how webkit handles it and what it expects.
     62         result.unmodifiedText[0] = '\r';
     63     }
     64     result.text[0] = result.unmodifiedText[0];
     65     result.setKeyIdentifierFromWindowsKeyCode();
     66     result.isSystemKey = isSystemKey;
     67 
     68     return result;
     69 }
     70 
     71 WebMouseEvent WebInputEventFactory::mouseEvent(MouseEventType type,
     72                                                WebMouseEvent::Button button,
     73                                                double timeStampSeconds,
     74                                                int windowX,
     75                                                int windowY,
     76                                                int modifiers,
     77                                                int clickCount)
     78 {
     79     WebMouseEvent result;
     80 
     81     result.x = windowX;
     82     result.y = windowY;
     83     result.windowX = windowX;
     84     result.windowY = windowY;
     85     result.timeStampSeconds = timeStampSeconds;
     86     result.clickCount = clickCount;
     87     result.modifiers = modifiers;
     88 
     89     switch (type) {
     90     case MouseEventTypeDown:
     91         result.type = WebInputEvent::MouseDown;
     92         result.button = button;
     93         break;
     94     case MouseEventTypeUp:
     95         result.type = WebInputEvent::MouseUp;
     96         result.button = button;
     97         break;
     98     case MouseEventTypeMove:
     99         result.type = WebInputEvent::MouseMove;
    100         result.button = WebMouseEvent::ButtonNone;
    101         break;
    102     };
    103 
    104     return result;
    105 }
    106 
    107 bool WebInputEventFactory::isSystemKeyEvent(const WebKeyboardEvent& event)
    108 {
    109     // On Windows all keys with Alt modifier will be marked as system key.
    110     // We keep the same behavior on Linux and everywhere non-Mac.
    111     return event.modifiers & WebInputEvent::AltKey;
    112 }
    113 
    114 // WebMouseWheelEvent ------------------------------------------------------------
    115 
    116 WebMouseWheelEvent WebInputEventFactory::mouseWheelEvent(MouseWheelDirectionType direction,
    117                                                          double timeStampSeconds,
    118                                                          int windowX,
    119                                                          int windowY)
    120 {
    121     WebMouseWheelEvent result;
    122 
    123     result.type = WebInputEvent::MouseWheel;
    124     result.x = windowX;
    125     result.y = windowY;
    126     result.windowX = windowX;
    127     result.windowY = windowY;
    128     result.timeStampSeconds = timeStampSeconds;
    129     result.button = WebMouseEvent::ButtonNone;
    130 
    131     // The below choices are matched from GTK.
    132     static const float scrollbarPixelsPerTick = 160.0f / 3.0f;
    133 
    134     switch (direction) {
    135     case MouseWheelDirectionTypeUp:
    136         result.deltaY = scrollbarPixelsPerTick;
    137         result.wheelTicksY = 1;
    138         break;
    139     case MouseWheelDirectionTypeDown:
    140         result.deltaY = -scrollbarPixelsPerTick;
    141         result.wheelTicksY = -1;
    142         break;
    143     case MouseWheelDirectionTypeLeft:
    144         result.deltaX = scrollbarPixelsPerTick;
    145         result.wheelTicksX = 1;
    146         break;
    147     case MouseWheelDirectionTypeRight:
    148         result.deltaX = -scrollbarPixelsPerTick;
    149         result.wheelTicksX = -1;
    150         break;
    151     }
    152 
    153     return result;
    154 }
    155 
    156 // WebGestureEvent ------------------------------------------------------------
    157 
    158 // FIXME: remove this obsolete version
    159 WebGestureEvent WebInputEventFactory::gestureEvent(WebInputEvent::Type type,
    160                                                    double timeStampSeconds,
    161                                                    int x,
    162                                                    int y,
    163                                                    float,
    164                                                    float,
    165                                                    int modifiers) {
    166     return gestureEvent(type, timeStampSeconds, x, y, modifiers);
    167 }
    168 
    169 WebGestureEvent WebInputEventFactory::gestureEvent(WebInputEvent::Type type,
    170                                                    double timeStampSeconds,
    171                                                    int x,
    172                                                    int y,
    173                                                    int modifiers)
    174 {
    175     WebGestureEvent result;
    176 
    177     result.type = type;
    178     result.x = x;
    179     result.y = y;
    180     result.timeStampSeconds = timeStampSeconds;
    181     result.modifiers = modifiers;
    182 
    183     return result;
    184 }
    185 
    186 } // namespace blink
    187