- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / accelerator_utils_cocoa.mm
1 // Copyright (c) 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 #include "chrome/browser/profiles/profile.h"
6 #include "chrome/browser/ui/cocoa/accelerators_cocoa.h"
7 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
8 #include "content/public/browser/native_web_keyboard_event.h"
9 #include "ui/base/accelerators/accelerator.h"
10 #import "ui/base/accelerators/platform_accelerator_cocoa.h"
11 #import "ui/events/keycodes/keyboard_code_conversion_mac.h"
12
13 namespace chrome {
14
15 bool IsChromeAccelerator(const ui::Accelerator& accelerator, Profile* profile) {
16   // The |accelerator| passed in contains a Windows key code but no platform
17   // accelerator info. The Accelerator list is the opposite: It has accelerators
18   // that have key_code() == VKEY_UNKNOWN but they contain a platform
19   // accelerator. We find common ground by converting the passed in Windows key
20   // code to a character and use that when comparing against the Accelerator
21   // list.
22   unichar character;
23   unichar characterIgnoringModifiers;
24   ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(),
25                                   0,
26                                   &character,
27                                   &characterIgnoringModifiers);
28   NSString* characters =
29       [[[NSString alloc] initWithCharacters:&character length:1] autorelease];
30
31   NSUInteger modifiers =
32       (accelerator.IsCtrlDown() ? NSControlKeyMask : 0) |
33       (accelerator.IsCmdDown() ? NSCommandKeyMask : 0) |
34       (accelerator.IsAltDown() ? NSAlternateKeyMask : 0) |
35       (accelerator.IsShiftDown() ? NSShiftKeyMask : 0);
36
37   NSEvent* event = [NSEvent keyEventWithType:NSKeyDown
38                                     location:NSZeroPoint
39                                modifierFlags:modifiers
40                                    timestamp:0
41                                 windowNumber:0
42                                      context:nil
43                                   characters:characters
44                  charactersIgnoringModifiers:characters
45                                    isARepeat:NO
46                                      keyCode:accelerator.key_code()];
47
48   content::NativeWebKeyboardEvent keyboard_event(event);
49   int id = [BrowserWindowUtils getCommandId:keyboard_event];
50   return id != -1;
51 }
52
53 }  // namespace chrome