• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2004, 2006 Apple Computer, 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#import "config.h"
27#import "PlatformMouseEvent.h"
28
29#import "PlatformScreen.h"
30
31namespace WebCore {
32
33static MouseButton mouseButtonForEvent(NSEvent *event)
34{
35    switch ([event type]) {
36        case NSLeftMouseDown:
37        case NSLeftMouseUp:
38        case NSLeftMouseDragged:
39            return LeftButton;
40        case NSRightMouseDown:
41        case NSRightMouseUp:
42        case NSRightMouseDragged:
43            return RightButton;
44        case NSOtherMouseDown:
45        case NSOtherMouseUp:
46        case NSOtherMouseDragged:
47            return MiddleButton;
48        default:
49            return NoButton;
50    }
51}
52
53static int clickCountForEvent(NSEvent *event)
54{
55    switch ([event type]) {
56        case NSLeftMouseDown:
57        case NSLeftMouseUp:
58        case NSLeftMouseDragged:
59        case NSRightMouseDown:
60        case NSRightMouseUp:
61        case NSRightMouseDragged:
62        case NSOtherMouseDown:
63        case NSOtherMouseUp:
64        case NSOtherMouseDragged:
65            return [event clickCount];
66        default:
67            return 0;
68    }
69}
70
71IntPoint globalPoint(const NSPoint& windowPoint, NSWindow *window)
72{
73    return IntPoint(flipScreenPoint([window convertBaseToScreen:windowPoint], screenForWindow(window)));
74}
75
76IntPoint pointForEvent(NSEvent *event, NSView *windowView)
77{
78    switch ([event type]) {
79        case NSLeftMouseDown:
80        case NSLeftMouseUp:
81        case NSLeftMouseDragged:
82        case NSRightMouseDown:
83        case NSRightMouseUp:
84        case NSRightMouseDragged:
85        case NSOtherMouseDown:
86        case NSOtherMouseUp:
87        case NSOtherMouseDragged:
88        case NSMouseMoved:
89        case NSScrollWheel: {
90            // Note: This will have its origin at the bottom left of the window unless windowView is flipped.
91            // In those cases, the Y coordinate gets flipped by Widget::convertFromContainingWindow.
92            NSPoint location = [event locationInWindow];
93            if (windowView)
94                location = [windowView convertPoint:location fromView:nil];
95            return IntPoint(location);
96        }
97        default:
98            return IntPoint();
99    }
100}
101
102IntPoint globalPointForEvent(NSEvent *event)
103{
104    switch ([event type]) {
105        case NSLeftMouseDown:
106        case NSLeftMouseUp:
107        case NSLeftMouseDragged:
108        case NSRightMouseDown:
109        case NSRightMouseUp:
110        case NSRightMouseDragged:
111        case NSOtherMouseDown:
112        case NSOtherMouseUp:
113        case NSOtherMouseDragged:
114        case NSMouseMoved:
115        case NSScrollWheel:
116            return globalPoint([event locationInWindow], [event window]);
117        default:
118            return IntPoint();
119    }
120}
121
122static MouseEventType mouseEventForNSEvent(NSEvent* event)
123{
124    switch ([event type]) {
125    case NSScrollWheel:
126        return MouseEventScroll;
127    case NSLeftMouseDragged:
128    case NSRightMouseDragged:
129    case NSOtherMouseDragged:
130    case NSMouseMoved:
131        return MouseEventMoved;
132    case NSLeftMouseDown:
133    case NSRightMouseDown:
134    case NSOtherMouseDown:
135        return MouseEventPressed;
136    case NSLeftMouseUp:
137    case NSRightMouseUp:
138    case NSOtherMouseUp:
139        return MouseEventReleased;
140    default:
141        return MouseEventMoved;
142    }
143}
144
145PlatformMouseEvent::PlatformMouseEvent(NSEvent* event, NSView *windowView)
146    : m_position(pointForEvent(event, windowView))
147    , m_globalPosition(globalPointForEvent(event))
148    , m_button(mouseButtonForEvent(event))
149    , m_eventType(mouseEventForNSEvent(event))
150    , m_clickCount(clickCountForEvent(event))
151    , m_shiftKey([event modifierFlags] & NSShiftKeyMask)
152    , m_ctrlKey([event modifierFlags] & NSControlKeyMask)
153    , m_altKey([event modifierFlags] & NSAlternateKeyMask)
154    , m_metaKey([event modifierFlags] & NSCommandKeyMask)
155    , m_timestamp([event timestamp])
156    , m_modifierFlags([event modifierFlags])
157    , m_eventNumber([event eventNumber])
158{
159}
160
161PlatformMouseEvent::PlatformMouseEvent(int x, int y, int globalX, int globalY, MouseButton button, MouseEventType eventType,
162                   int clickCount, bool shiftKey, bool ctrlKey, bool altKey, bool metaKey, double timestamp,
163                   unsigned modifierFlags, int eventNumber)
164    : m_position(IntPoint(x, y))
165    , m_globalPosition(IntPoint(globalX, globalY))
166    , m_button(button)
167    , m_eventType(eventType)
168    , m_clickCount(clickCount)
169    , m_shiftKey(shiftKey)
170    , m_ctrlKey(ctrlKey)
171    , m_altKey(altKey)
172    , m_metaKey(metaKey)
173    , m_timestamp(timestamp)
174    , m_modifierFlags(modifierFlags)
175    , m_eventNumber(eventNumber)
176{
177}
178
179}
180