OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/gfx/rect.h" | 5 #include "base/gfx/rect.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
8 #include "chrome/browser/bookmarks/bookmark_utils.h" | 8 #include "chrome/browser/bookmarks/bookmark_utils.h" |
9 #include "chrome/browser/cocoa/browser_window_cocoa.h" | 9 #include "chrome/browser/cocoa/browser_window_cocoa.h" |
10 #import "chrome/browser/cocoa/browser_window_controller.h" | 10 #import "chrome/browser/cocoa/browser_window_controller.h" |
11 #import "chrome/browser/cocoa/clear_browsing_data_controller.h" | 11 #import "chrome/browser/cocoa/clear_browsing_data_controller.h" |
12 #import "chrome/browser/cocoa/download_shelf_controller.h" | 12 #import "chrome/browser/cocoa/download_shelf_controller.h" |
13 #import "chrome/browser/cocoa/keyword_editor_cocoa_controller.h" | 13 #import "chrome/browser/cocoa/keyword_editor_cocoa_controller.h" |
| 14 #import "chrome/browser/cocoa/nsmenuitem_additions.h" |
14 #include "chrome/browser/cocoa/page_info_window_mac.h" | 15 #include "chrome/browser/cocoa/page_info_window_mac.h" |
15 #include "chrome/browser/cocoa/status_bubble_mac.h" | 16 #include "chrome/browser/cocoa/status_bubble_mac.h" |
16 #include "chrome/browser/cocoa/task_manager_mac.h" | 17 #include "chrome/browser/cocoa/task_manager_mac.h" |
17 #import "chrome/browser/cocoa/theme_install_bubble_view.h" | 18 #import "chrome/browser/cocoa/theme_install_bubble_view.h" |
18 #include "chrome/browser/browser.h" | 19 #include "chrome/browser/browser.h" |
19 #include "chrome/browser/download/download_shelf.h" | 20 #include "chrome/browser/download/download_shelf.h" |
| 21 #include "chrome/browser/global_keyboard_shortcuts_mac.h" |
20 #include "chrome/common/notification_service.h" | 22 #include "chrome/common/notification_service.h" |
21 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
22 #include "chrome/common/pref_service.h" | 24 #include "chrome/common/pref_service.h" |
23 #include "chrome/common/temp_scaffolding_stubs.h" | 25 #include "chrome/common/temp_scaffolding_stubs.h" |
24 #include "chrome/browser/profile.h" | 26 #include "chrome/browser/profile.h" |
25 | 27 |
26 BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser, | 28 BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser, |
27 BrowserWindowController* controller, | 29 BrowserWindowController* controller, |
28 NSWindow* window) | 30 NSWindow* window) |
29 : window_(window), | 31 : window_(window), |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 } | 318 } |
317 | 319 |
318 void BrowserWindowCocoa::ShowPageMenu() { | 320 void BrowserWindowCocoa::ShowPageMenu() { |
319 // No-op. Mac doesn't support showing the menus via alt keys. | 321 // No-op. Mac doesn't support showing the menus via alt keys. |
320 } | 322 } |
321 | 323 |
322 void BrowserWindowCocoa::ShowAppMenu() { | 324 void BrowserWindowCocoa::ShowAppMenu() { |
323 // No-op. Mac doesn't support showing the menus via alt keys. | 325 // No-op. Mac doesn't support showing the menus via alt keys. |
324 } | 326 } |
325 | 327 |
| 328 @interface MenuWalker : NSObject |
| 329 + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key |
| 330 menu:(NSMenu*)menu; |
| 331 @end |
| 332 |
| 333 @implementation MenuWalker |
| 334 + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key |
| 335 menu:(NSMenu*)menu { |
| 336 NSMenuItem* result = nil; |
| 337 |
| 338 for (NSMenuItem *item in [menu itemArray]) { |
| 339 NSMenu* submenu = [item submenu]; |
| 340 if (submenu) { |
| 341 if (submenu != [NSApp servicesMenu]) |
| 342 result = [self itemForKeyEquivalent:key |
| 343 menu:submenu]; |
| 344 } else if ([item cr_firesForKeyEvent:key]) { |
| 345 result = item; |
| 346 } |
| 347 |
| 348 if (result) |
| 349 break; |
| 350 } |
| 351 |
| 352 return result; |
| 353 } |
| 354 @end |
| 355 |
326 int BrowserWindowCocoa::GetCommandId(const NativeWebKeyboardEvent& event) { | 356 int BrowserWindowCocoa::GetCommandId(const NativeWebKeyboardEvent& event) { |
327 // TODO(port): return the command id if this is a keyboard accelerator. | 357 if ([event.os_event type] != NSKeyDown) |
328 // CommandForKeyboardShortcut() doesn't have the full list. | 358 return -1; |
| 359 |
| 360 // Look in menu. |
| 361 NSMenuItem* item = [MenuWalker itemForKeyEquivalent:event.os_event |
| 362 menu:[NSApp mainMenu]]; |
| 363 |
| 364 if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0) |
| 365 return [item tag]; |
| 366 |
| 367 // Look in secondary keyboard shortcuts. |
| 368 NSUInteger modifiers = [event.os_event modifierFlags]; |
| 369 const bool cmdKey = (modifiers & NSCommandKeyMask) != 0; |
| 370 const bool shiftKey = (modifiers & NSShiftKeyMask) != 0; |
| 371 const bool cntrlKey = (modifiers & NSControlKeyMask) != 0; |
| 372 const bool optKey = (modifiers & NSAlternateKeyMask) != 0; |
| 373 const int keyCode = [event.os_event keyCode]; |
| 374 |
| 375 int cmdNum = CommandForWindowKeyboardShortcut( |
| 376 cmdKey, shiftKey, cntrlKey, optKey, keyCode); |
| 377 if (cmdNum != -1) |
| 378 return cmdNum; |
| 379 |
| 380 cmdNum = CommandForBrowserKeyboardShortcut( |
| 381 cmdKey, shiftKey, cntrlKey, optKey, keyCode); |
| 382 if (cmdNum != -1) |
| 383 return cmdNum; |
| 384 |
329 return -1; | 385 return -1; |
330 } | 386 } |
331 | 387 |
332 void BrowserWindowCocoa::Observe(NotificationType type, | 388 void BrowserWindowCocoa::Observe(NotificationType type, |
333 const NotificationSource& source, | 389 const NotificationSource& source, |
334 const NotificationDetails& details) { | 390 const NotificationDetails& details) { |
335 switch (type.value) { | 391 switch (type.value) { |
336 // Only the key window gets a direct toggle from the menu. | 392 // Only the key window gets a direct toggle from the menu. |
337 // Other windows hear about it from the notification. | 393 // Other windows hear about it from the notification. |
338 case NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED: | 394 case NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED: |
339 [controller_ updateBookmarkBarVisibility]; | 395 [controller_ updateBookmarkBarVisibility]; |
340 break; | 396 break; |
341 default: | 397 default: |
342 NOTREACHED(); // we don't ask for anything else! | 398 NOTREACHED(); // we don't ask for anything else! |
343 break; | 399 break; |
344 } | 400 } |
345 } | 401 } |
346 | 402 |
347 void BrowserWindowCocoa::DestroyBrowser() { | 403 void BrowserWindowCocoa::DestroyBrowser() { |
348 [controller_ destroyBrowser]; | 404 [controller_ destroyBrowser]; |
349 | 405 |
350 // at this point the controller is dead (autoreleased), so | 406 // at this point the controller is dead (autoreleased), so |
351 // make sure we don't try to reference it any more. | 407 // make sure we don't try to reference it any more. |
352 } | 408 } |
OLD | NEW |