1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #import "ui/base/cocoa/cocoa_event_utils.h" 6 7 #include "ui/base/events/event_constants.h" 8 #include "ui/base/window_open_disposition.h" 9 10 namespace { 11 12 bool isLeftButtonEvent(NSEvent* event) { 13 NSEventType type = [event type]; 14 return type == NSLeftMouseDown || 15 type == NSLeftMouseDragged || 16 type == NSLeftMouseUp; 17 } 18 19 bool isRightButtonEvent(NSEvent* event) { 20 NSEventType type = [event type]; 21 return type == NSRightMouseDown || 22 type == NSRightMouseDragged || 23 type == NSRightMouseUp; 24 } 25 26 bool isMiddleButtonEvent(NSEvent* event) { 27 if ([event buttonNumber] != 2) 28 return false; 29 30 NSEventType type = [event type]; 31 return type == NSOtherMouseDown || 32 type == NSOtherMouseDragged || 33 type == NSOtherMouseUp; 34 } 35 36 } // namespace 37 38 namespace ui { 39 40 // Retrieves a bitsum of ui::EventFlags from NSEvent. 41 int EventFlagsFromNSEvent(NSEvent* event) { 42 NSUInteger modifiers = [event modifierFlags]; 43 return EventFlagsFromNSEventWithModifiers(event, modifiers); 44 } 45 46 int EventFlagsFromNSEventWithModifiers(NSEvent* event, NSUInteger modifiers) { 47 int flags = 0; 48 flags |= (modifiers & NSAlphaShiftKeyMask) ? ui::EF_CAPS_LOCK_DOWN : 0; 49 flags |= (modifiers & NSShiftKeyMask) ? ui::EF_SHIFT_DOWN : 0; 50 flags |= (modifiers & NSControlKeyMask) ? ui::EF_CONTROL_DOWN : 0; 51 flags |= (modifiers & NSAlternateKeyMask) ? ui::EF_ALT_DOWN : 0; 52 flags |= (modifiers & NSCommandKeyMask) ? ui::EF_COMMAND_DOWN : 0; 53 flags |= isLeftButtonEvent(event) ? ui::EF_LEFT_MOUSE_BUTTON : 0; 54 flags |= isRightButtonEvent(event) ? ui::EF_RIGHT_MOUSE_BUTTON : 0; 55 flags |= isMiddleButtonEvent(event) ? ui::EF_MIDDLE_MOUSE_BUTTON : 0; 56 return flags; 57 } 58 59 WindowOpenDisposition WindowOpenDispositionFromNSEvent(NSEvent* event) { 60 NSUInteger modifiers = [event modifierFlags]; 61 return WindowOpenDispositionFromNSEventWithFlags(event, modifiers); 62 } 63 64 WindowOpenDisposition WindowOpenDispositionFromNSEventWithFlags( 65 NSEvent* event, NSUInteger modifiers) { 66 int event_flags = EventFlagsFromNSEventWithModifiers(event, modifiers); 67 return ui::DispositionFromEventFlags(event_flags); 68 } 69 70 } // namespace ui 71