OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/ui/cocoa/browser_window_cocoa.h" | 5 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
11 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/bookmarks/bookmark_utils.h" | 12 #include "chrome/browser/bookmarks/bookmark_utils.h" |
13 #include "chrome/browser/download/download_shelf.h" | 13 #include "chrome/browser/download/download_shelf.h" |
14 #include "chrome/browser/global_keyboard_shortcuts_mac.h" | |
15 #include "chrome/browser/page_info_window.h" | 14 #include "chrome/browser/page_info_window.h" |
16 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/sidebar/sidebar_container.h" | 17 #include "chrome/browser/sidebar/sidebar_container.h" |
19 #include "chrome/browser/sidebar/sidebar_manager.h" | 18 #include "chrome/browser/sidebar/sidebar_manager.h" |
20 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/ui/browser_list.h" | 20 #include "chrome/browser/ui/browser_list.h" |
22 #import "chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.h" | 21 #import "chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.h" |
23 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 22 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 23 #import "chrome/browser/ui/cocoa/browser_window_utils.h" |
24 #import "chrome/browser/ui/cocoa/bug_report_window_controller.h" | 24 #import "chrome/browser/ui/cocoa/bug_report_window_controller.h" |
25 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" | 25 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" |
26 #import "chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.h" | 26 #import "chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.h" |
27 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h" | 27 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h" |
28 #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h" | 28 #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h" |
29 #import "chrome/browser/ui/cocoa/html_dialog_window_controller.h" | 29 #import "chrome/browser/ui/cocoa/html_dialog_window_controller.h" |
30 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | 30 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
31 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" | 31 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" |
32 #include "chrome/browser/ui/cocoa/repost_form_warning_mac.h" | 32 #include "chrome/browser/ui/cocoa/repost_form_warning_mac.h" |
33 #include "chrome/browser/ui/cocoa/restart_browser.h" | 33 #include "chrome/browser/ui/cocoa/restart_browser.h" |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 bool show_history) { | 442 bool show_history) { |
443 browser::ShowPageInfoBubble(window(), profile, url, ssl, show_history); | 443 browser::ShowPageInfoBubble(window(), profile, url, ssl, show_history); |
444 } | 444 } |
445 | 445 |
446 void BrowserWindowCocoa::ShowAppMenu() { | 446 void BrowserWindowCocoa::ShowAppMenu() { |
447 // No-op. Mac doesn't support showing the menus via alt keys. | 447 // No-op. Mac doesn't support showing the menus via alt keys. |
448 } | 448 } |
449 | 449 |
450 bool BrowserWindowCocoa::PreHandleKeyboardEvent( | 450 bool BrowserWindowCocoa::PreHandleKeyboardEvent( |
451 const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) { | 451 const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) { |
452 if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) | 452 if (![BrowserWindowUtils shouldHandleKeyboardEvent:event]) |
453 return false; | 453 return false; |
454 | 454 |
455 DCHECK(event.os_event != NULL); | 455 int id = [BrowserWindowUtils getCommandId:event]; |
456 int id = GetCommandId(event); | |
457 if (id == -1) | 456 if (id == -1) |
458 return false; | 457 return false; |
459 | 458 |
460 if (browser_->IsReservedCommandOrKey(id, event)) | 459 if (browser_->IsReservedCommandOrKey(id, event)) { |
461 return HandleKeyboardEventInternal(event.os_event); | 460 return [BrowserWindowUtils handleKeyboardEvent:event.os_event |
| 461 inWindow:window()]; |
| 462 } |
462 | 463 |
463 DCHECK(is_keyboard_shortcut != NULL); | 464 DCHECK(is_keyboard_shortcut); |
464 *is_keyboard_shortcut = true; | 465 *is_keyboard_shortcut = true; |
465 | |
466 return false; | 466 return false; |
467 } | 467 } |
468 | 468 |
469 void BrowserWindowCocoa::HandleKeyboardEvent( | 469 void BrowserWindowCocoa::HandleKeyboardEvent( |
470 const NativeWebKeyboardEvent& event) { | 470 const NativeWebKeyboardEvent& event) { |
471 if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) | 471 if ([BrowserWindowUtils shouldHandleKeyboardEvent:event]) |
472 return; | 472 [BrowserWindowUtils handleKeyboardEvent:event.os_event inWindow:window()]; |
473 | |
474 DCHECK(event.os_event != NULL); | |
475 HandleKeyboardEventInternal(event.os_event); | |
476 } | |
477 | |
478 @interface MenuWalker : NSObject | |
479 + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key | |
480 menu:(NSMenu*)menu; | |
481 @end | |
482 | |
483 @implementation MenuWalker | |
484 + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key | |
485 menu:(NSMenu*)menu { | |
486 NSMenuItem* result = nil; | |
487 | |
488 for (NSMenuItem *item in [menu itemArray]) { | |
489 NSMenu* submenu = [item submenu]; | |
490 if (submenu) { | |
491 if (submenu != [NSApp servicesMenu]) | |
492 result = [self itemForKeyEquivalent:key | |
493 menu:submenu]; | |
494 } else if ([item cr_firesForKeyEventIfEnabled:key]) { | |
495 result = item; | |
496 } | |
497 | |
498 if (result) | |
499 break; | |
500 } | |
501 | |
502 return result; | |
503 } | |
504 @end | |
505 | |
506 int BrowserWindowCocoa::GetCommandId(const NativeWebKeyboardEvent& event) { | |
507 if ([event.os_event type] != NSKeyDown) | |
508 return -1; | |
509 | |
510 // Look in menu. | |
511 NSMenuItem* item = [MenuWalker itemForKeyEquivalent:event.os_event | |
512 menu:[NSApp mainMenu]]; | |
513 | |
514 if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0) | |
515 return [item tag]; | |
516 | |
517 // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items | |
518 // that do not correspond to IDC_ constants need no special treatment however, | |
519 // as they can't be blacklisted in |Browser::IsReservedCommandOrKey()| anyhow. | |
520 if (item && [item action] == @selector(performClose:)) | |
521 return IDC_CLOSE_WINDOW; | |
522 | |
523 // "Exit" doesn't use the |commandDispatch:| mechanism either. | |
524 if (item && [item action] == @selector(terminate:)) | |
525 return IDC_EXIT; | |
526 | |
527 // Look in secondary keyboard shortcuts. | |
528 NSUInteger modifiers = [event.os_event modifierFlags]; | |
529 const bool cmdKey = (modifiers & NSCommandKeyMask) != 0; | |
530 const bool shiftKey = (modifiers & NSShiftKeyMask) != 0; | |
531 const bool cntrlKey = (modifiers & NSControlKeyMask) != 0; | |
532 const bool optKey = (modifiers & NSAlternateKeyMask) != 0; | |
533 const int keyCode = [event.os_event keyCode]; | |
534 const unichar keyChar = KeyCharacterForEvent(event.os_event); | |
535 | |
536 int cmdNum = CommandForWindowKeyboardShortcut( | |
537 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); | |
538 if (cmdNum != -1) | |
539 return cmdNum; | |
540 | |
541 cmdNum = CommandForBrowserKeyboardShortcut( | |
542 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); | |
543 if (cmdNum != -1) | |
544 return cmdNum; | |
545 | |
546 return -1; | |
547 } | |
548 | |
549 bool BrowserWindowCocoa::HandleKeyboardEventInternal(NSEvent* event) { | |
550 ChromeEventProcessingWindow* event_window = | |
551 static_cast<ChromeEventProcessingWindow*>(window()); | |
552 DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]); | |
553 | |
554 // Do not fire shortcuts on key up. | |
555 if ([event type] == NSKeyDown) { | |
556 // Send the event to the menu before sending it to the browser/window | |
557 // shortcut handling, so that if a user configures cmd-left to mean | |
558 // "previous tab", it takes precedence over the built-in "history back" | |
559 // binding. Other than that, the |-redispatchKeyEvent:| call would take care | |
560 // of invoking the original menu item shortcut as well. | |
561 | |
562 if ([[NSApp mainMenu] performKeyEquivalent:event]) | |
563 return true; | |
564 | |
565 if ([event_window handleExtraBrowserKeyboardShortcut:event]) | |
566 return true; | |
567 | |
568 if ([event_window handleExtraWindowKeyboardShortcut:event]) | |
569 return true; | |
570 | |
571 if ([event_window handleDelayedWindowKeyboardShortcut:event]) | |
572 return true; | |
573 } | |
574 | |
575 return [event_window redispatchKeyEvent:event]; | |
576 } | 473 } |
577 | 474 |
578 void BrowserWindowCocoa::ShowCreateWebAppShortcutsDialog( | 475 void BrowserWindowCocoa::ShowCreateWebAppShortcutsDialog( |
579 TabContentsWrapper* tab_contents) { | 476 TabContentsWrapper* tab_contents) { |
580 NOTIMPLEMENTED(); | 477 NOTIMPLEMENTED(); |
581 } | 478 } |
582 | 479 |
583 void BrowserWindowCocoa::ShowCreateChromeAppShortcutsDialog( | 480 void BrowserWindowCocoa::ShowCreateChromeAppShortcutsDialog( |
584 Profile* profile, const Extension* app) { | 481 Profile* profile, const Extension* app) { |
585 NOTIMPLEMENTED(); | 482 NOTIMPLEMENTED(); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 | 578 |
682 NSWindow* BrowserWindowCocoa::window() const { | 579 NSWindow* BrowserWindowCocoa::window() const { |
683 return [controller_ window]; | 580 return [controller_ window]; |
684 } | 581 } |
685 | 582 |
686 void BrowserWindowCocoa::UpdateSidebarForContents(TabContents* tab_contents) { | 583 void BrowserWindowCocoa::UpdateSidebarForContents(TabContents* tab_contents) { |
687 if (tab_contents == browser_->GetSelectedTabContents()) { | 584 if (tab_contents == browser_->GetSelectedTabContents()) { |
688 [controller_ updateSidebarForContents:tab_contents]; | 585 [controller_ updateSidebarForContents:tab_contents]; |
689 } | 586 } |
690 } | 587 } |
OLD | NEW |