2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "PlatformMouseEvent.h"
29 #import "PlatformScreen.h"
33 static MouseButton mouseButtonForEvent(NSEvent *event)
35 switch ([event type]) {
38 case NSLeftMouseDragged:
40 case NSRightMouseDown:
42 case NSRightMouseDragged:
44 case NSOtherMouseDown:
46 case NSOtherMouseDragged:
53 static int clickCountForEvent(NSEvent *event)
55 switch ([event type]) {
58 case NSLeftMouseDragged:
59 case NSRightMouseDown:
61 case NSRightMouseDragged:
62 case NSOtherMouseDown:
64 case NSOtherMouseDragged:
65 return [event clickCount];
71 LayoutPoint globalPoint(const NSPoint& windowPoint, NSWindow *window)
73 return LayoutPoint(flipScreenPoint([window convertBaseToScreen:windowPoint], screenForWindow(window)));
76 LayoutPoint pointForEvent(NSEvent *event, NSView *windowView)
78 switch ([event type]) {
81 case NSLeftMouseDragged:
82 case NSRightMouseDown:
84 case NSRightMouseDragged:
85 case NSOtherMouseDown:
87 case NSOtherMouseDragged:
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];
94 location = [windowView convertPoint:location fromView:nil];
95 return LayoutPoint(location);
102 LayoutPoint globalPointForEvent(NSEvent *event)
104 switch ([event type]) {
105 case NSLeftMouseDown:
107 case NSLeftMouseDragged:
108 case NSRightMouseDown:
110 case NSRightMouseDragged:
111 case NSOtherMouseDown:
113 case NSOtherMouseDragged:
116 return globalPoint([event locationInWindow], [event window]);
118 return LayoutPoint();
122 static MouseEventType mouseEventForNSEvent(NSEvent* event)
124 switch ([event type]) {
126 return MouseEventScroll;
127 case NSLeftMouseDragged:
128 case NSRightMouseDragged:
129 case NSOtherMouseDragged:
131 return MouseEventMoved;
132 case NSLeftMouseDown:
133 case NSRightMouseDown:
134 case NSOtherMouseDown:
135 return MouseEventPressed;
139 return MouseEventReleased;
141 return MouseEventMoved;
145 PlatformMouseEvent::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])
161 PlatformMouseEvent::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))
167 , m_eventType(eventType)
168 , m_clickCount(clickCount)
169 , m_shiftKey(shiftKey)
173 , m_timestamp(timestamp)
174 , m_modifierFlags(modifierFlags)
175 , m_eventNumber(eventNumber)