[email protected] | 35d0615 | 2011-01-10 22:19:42 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 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/ui/cocoa/browser_window_cocoa.h" |
| 6 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 7 | #include "base/command_line.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/message_loop.h" |
| 10 | #include "base/sys_string_conversions.h" |
| 11 | #include "chrome/app/chrome_command_ids.h" |
| 12 | #include "chrome/browser/bookmarks/bookmark_utils.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" |
| 16 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 18 | #include "chrome/browser/sidebar/sidebar_container.h" |
| 19 | #include "chrome/browser/sidebar/sidebar_manager.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 20 | #include "chrome/browser/ui/browser.h" |
| 21 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 464f001 | 2011-07-07 02:12:51 | [diff] [blame] | 22 | #import "chrome/browser/ui/cocoa/browser/edit_search_engine_cocoa_controller.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 23 | #import "chrome/browser/ui/cocoa/browser_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" |
[email protected] | fa810229 | 2011-01-20 16:21:23 | [diff] [blame] | 26 | #import "chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 27 | #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h" |
[email protected] | 632983f | 2011-08-08 22:51:24 | [diff] [blame^] | 28 | #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 29 | #import "chrome/browser/ui/cocoa/html_dialog_window_controller.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 30 | #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
[email protected] | a7d83ca | 2011-03-06 14:41:07 | [diff] [blame] | 31 | #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 32 | #include "chrome/browser/ui/cocoa/repost_form_warning_mac.h" |
| 33 | #include "chrome/browser/ui/cocoa/restart_browser.h" |
| 34 | #include "chrome/browser/ui/cocoa/status_bubble_mac.h" |
| 35 | #include "chrome/browser/ui/cocoa/task_manager_mac.h" |
| 36 | #import "chrome/browser/ui/cocoa/theme_install_bubble_view.h" |
[email protected] | 8450c4f | 2011-01-19 22:16:22 | [diff] [blame] | 37 | #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" |
[email protected] | 6a3ec231 | 2010-12-02 19:30:19 | [diff] [blame] | 38 | #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 39 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 40 | #include "chrome/common/pref_names.h" |
[email protected] | 21f1168 | 2011-03-02 16:45:42 | [diff] [blame] | 41 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | 4dd5793 | 2011-03-17 06:06:12 | [diff] [blame] | 42 | #include "content/common/native_web_keyboard_event.h" |
[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 43 | #include "content/common/notification_service.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 44 | #include "grit/chromium_strings.h" |
| 45 | #include "grit/generated_resources.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 46 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 47 | #include "ui/gfx/rect.h" |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 48 | |
| 49 | BrowserWindowCocoa::BrowserWindowCocoa(Browser* browser, |
| 50 | BrowserWindowController* controller, |
| 51 | NSWindow* window) |
| 52 | : browser_(browser), |
| 53 | controller_(controller), |
| 54 | confirm_close_factory_(browser) { |
| 55 | // This pref applies to all windows, so all must watch for it. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 56 | registrar_.Add(this, |
| 57 | chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 58 | NotificationService::AllSources()); |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 59 | registrar_.Add(this, chrome::NOTIFICATION_SIDEBAR_CHANGED, |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 60 | NotificationService::AllSources()); |
| 61 | } |
| 62 | |
| 63 | BrowserWindowCocoa::~BrowserWindowCocoa() { |
| 64 | } |
| 65 | |
| 66 | void BrowserWindowCocoa::Show() { |
| 67 | // The Browser associated with this browser window must become the active |
| 68 | // browser at the time |Show()| is called. This is the natural behaviour under |
| 69 | // Windows, but |-makeKeyAndOrderFront:| won't send |-windowDidBecomeMain:| |
| 70 | // until we return to the runloop. Therefore any calls to |
| 71 | // |BrowserList::GetLastActive()| (for example, in bookmark_util), will return |
| 72 | // the previous browser instead if we don't explicitly set it here. |
| 73 | BrowserList::SetLastActive(browser_); |
| 74 | |
| 75 | [window() makeKeyAndOrderFront:controller_]; |
| 76 | } |
| 77 | |
[email protected] | d4db6c70 | 2011-03-28 21:49:14 | [diff] [blame] | 78 | void BrowserWindowCocoa::ShowInactive() { |
| 79 | [window() orderFront:controller_]; |
| 80 | } |
| 81 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 82 | void BrowserWindowCocoa::SetBounds(const gfx::Rect& bounds) { |
[email protected] | ccb5895f | 2011-06-09 21:16:14 | [diff] [blame] | 83 | gfx::Rect real_bounds = [controller_ enforceMinWindowSize:bounds]; |
| 84 | |
[email protected] | d479b8e2 | 2011-02-09 05:19:49 | [diff] [blame] | 85 | SetFullscreen(false); |
[email protected] | ccb5895f | 2011-06-09 21:16:14 | [diff] [blame] | 86 | NSRect cocoa_bounds = NSMakeRect(real_bounds.x(), 0, |
| 87 | real_bounds.width(), |
| 88 | real_bounds.height()); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 89 | // Flip coordinates based on the primary screen. |
| 90 | NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 91 | cocoa_bounds.origin.y = |
[email protected] | ccb5895f | 2011-06-09 21:16:14 | [diff] [blame] | 92 | [screen frame].size.height - real_bounds.height() - real_bounds.y(); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 93 | |
| 94 | [window() setFrame:cocoa_bounds display:YES]; |
| 95 | } |
| 96 | |
| 97 | // Callers assume that this doesn't immediately delete the Browser object. |
| 98 | // The controller implementing the window delegate methods called from |
| 99 | // |-performClose:| must take precautions to ensure that. |
| 100 | void BrowserWindowCocoa::Close() { |
| 101 | // If there is an overlay window, we contain a tab being dragged between |
| 102 | // windows. Don't hide the window as it makes the UI extra confused. We can |
| 103 | // still close the window, as that will happen when the drag completes. |
| 104 | if ([controller_ overlayWindow]) { |
| 105 | [controller_ deferPerformClose]; |
| 106 | } else { |
| 107 | // Make sure we hide the window immediately. Even though performClose: |
| 108 | // calls orderOut: eventually, it leaves the window on-screen long enough |
| 109 | // that we start to see tabs shutting down. http://crbug.com/23959 |
| 110 | // TODO(viettrungluu): This is kind of bad, since |-performClose:| calls |
| 111 | // |-windowShouldClose:| (on its delegate, which is probably the |
| 112 | // controller) which may return |NO| causing the window to not be closed, |
| 113 | // thereby leaving a hidden window. In fact, our window-closing procedure |
| 114 | // involves a (indirect) recursion on |-performClose:|, which is also bad. |
| 115 | [window() orderOut:controller_]; |
| 116 | [window() performClose:controller_]; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void BrowserWindowCocoa::Activate() { |
| 121 | [controller_ activate]; |
| 122 | } |
| 123 | |
| 124 | void BrowserWindowCocoa::Deactivate() { |
| 125 | // TODO(jcivelli): http://crbug.com/51364 Implement me. |
| 126 | NOTIMPLEMENTED(); |
| 127 | } |
| 128 | |
| 129 | void BrowserWindowCocoa::FlashFrame() { |
| 130 | [NSApp requestUserAttention:NSInformationalRequest]; |
| 131 | } |
| 132 | |
| 133 | bool BrowserWindowCocoa::IsActive() const { |
| 134 | return [window() isKeyWindow]; |
| 135 | } |
| 136 | |
| 137 | gfx::NativeWindow BrowserWindowCocoa::GetNativeHandle() { |
| 138 | return window(); |
| 139 | } |
| 140 | |
| 141 | BrowserWindowTesting* BrowserWindowCocoa::GetBrowserWindowTesting() { |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | StatusBubble* BrowserWindowCocoa::GetStatusBubble() { |
| 146 | return [controller_ statusBubble]; |
| 147 | } |
| 148 | |
[email protected] | c9bd2e8 | 2011-04-15 23:28:19 | [diff] [blame] | 149 | void BrowserWindowCocoa::ToolbarSizeChanged(bool is_animating) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 150 | // According to beng, this is an ugly method that comes from the days when the |
| 151 | // download shelf was a ChromeView attached to the TabContents, and as its |
| 152 | // size changed via animation it notified through TCD/etc to the browser view |
| 153 | // to relayout for each tick of the animation. We don't need anything of the |
| 154 | // sort on Mac. |
| 155 | } |
| 156 | |
| 157 | void BrowserWindowCocoa::UpdateTitleBar() { |
| 158 | NSString* newTitle = |
| 159 | base::SysUTF16ToNSString(browser_->GetWindowTitleForCurrentTab()); |
| 160 | |
| 161 | // Work around Cocoa bug: if a window changes title during the tracking of the |
| 162 | // Window menu it doesn't display well and the constant re-sorting of the list |
| 163 | // makes it difficult for the user to pick the desired window. Delay window |
| 164 | // title updates until the default run-loop mode. |
| 165 | |
| 166 | if (pending_window_title_.get()) |
| 167 | [[NSRunLoop currentRunLoop] |
| 168 | cancelPerformSelector:@selector(setTitle:) |
| 169 | target:window() |
| 170 | argument:pending_window_title_.get()]; |
| 171 | |
| 172 | pending_window_title_.reset([newTitle copy]); |
| 173 | [[NSRunLoop currentRunLoop] |
| 174 | performSelector:@selector(setTitle:) |
| 175 | target:window() |
| 176 | argument:newTitle |
| 177 | order:0 |
| 178 | modes:[NSArray arrayWithObject:NSDefaultRunLoopMode]]; |
| 179 | } |
| 180 | |
[email protected] | 09b2934 | 2011-06-24 19:18:48 | [diff] [blame] | 181 | void BrowserWindowCocoa::BookmarkBarStateChanged( |
| 182 | BookmarkBar::AnimateChangeType change_type) { |
| 183 | // TODO: route changes to state through this. |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void BrowserWindowCocoa::UpdateDevTools() { |
| 187 | [controller_ updateDevToolsForContents: |
| 188 | browser_->GetSelectedTabContents()]; |
| 189 | } |
| 190 | |
| 191 | void BrowserWindowCocoa::UpdateLoadingAnimations(bool should_animate) { |
| 192 | // Do nothing on Mac. |
| 193 | } |
| 194 | |
| 195 | void BrowserWindowCocoa::SetStarredState(bool is_starred) { |
| 196 | [controller_ setStarredState:is_starred ? YES : NO]; |
| 197 | } |
| 198 | |
| 199 | gfx::Rect BrowserWindowCocoa::GetRestoredBounds() const { |
| 200 | // Flip coordinates based on the primary screen. |
| 201 | NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 202 | NSRect frame = [controller_ regularWindowFrame]; |
| 203 | gfx::Rect bounds(frame.origin.x, 0, frame.size.width, frame.size.height); |
| 204 | bounds.set_y([screen frame].size.height - frame.origin.y - frame.size.height); |
| 205 | return bounds; |
| 206 | } |
| 207 | |
[email protected] | d479b8e2 | 2011-02-09 05:19:49 | [diff] [blame] | 208 | gfx::Rect BrowserWindowCocoa::GetBounds() const { |
| 209 | return GetRestoredBounds(); |
| 210 | } |
| 211 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 212 | bool BrowserWindowCocoa::IsMaximized() const { |
| 213 | return [window() isZoomed]; |
| 214 | } |
| 215 | |
| 216 | void BrowserWindowCocoa::SetFullscreen(bool fullscreen) { |
| 217 | [controller_ setFullscreen:fullscreen]; |
| 218 | } |
| 219 | |
| 220 | bool BrowserWindowCocoa::IsFullscreen() const { |
| 221 | return !![controller_ isFullscreen]; |
| 222 | } |
| 223 | |
| 224 | bool BrowserWindowCocoa::IsFullscreenBubbleVisible() const { |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | void BrowserWindowCocoa::ConfirmAddSearchProvider( |
| 229 | const TemplateURL* template_url, |
| 230 | Profile* profile) { |
[email protected] | 464f001 | 2011-07-07 02:12:51 | [diff] [blame] | 231 | // The controller will release itself when the window closes. |
| 232 | EditSearchEngineCocoaController* editor = |
| 233 | [[EditSearchEngineCocoaController alloc] initWithProfile:profile |
| 234 | delegate:NULL |
| 235 | templateURL:template_url]; |
| 236 | [NSApp beginSheet:[editor window] |
| 237 | modalForWindow:window() |
| 238 | modalDelegate:controller_ |
| 239 | didEndSelector:@selector(sheetDidEnd:returnCode:context:) |
| 240 | contextInfo:NULL]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | LocationBar* BrowserWindowCocoa::GetLocationBar() const { |
| 244 | return [controller_ locationBarBridge]; |
| 245 | } |
| 246 | |
| 247 | void BrowserWindowCocoa::SetFocusToLocationBar(bool select_all) { |
| 248 | [controller_ focusLocationBar:select_all ? YES : NO]; |
| 249 | } |
| 250 | |
| 251 | void BrowserWindowCocoa::UpdateReloadStopState(bool is_loading, bool force) { |
| 252 | [controller_ setIsLoading:is_loading force:force]; |
| 253 | } |
| 254 | |
| 255 | void BrowserWindowCocoa::UpdateToolbar(TabContentsWrapper* contents, |
| 256 | bool should_restore_state) { |
| 257 | [controller_ updateToolbarWithContents:contents->tab_contents() |
| 258 | shouldRestoreState:should_restore_state ? YES : NO]; |
| 259 | } |
| 260 | |
| 261 | void BrowserWindowCocoa::FocusToolbar() { |
| 262 | // Not needed on the Mac. |
| 263 | } |
| 264 | |
| 265 | void BrowserWindowCocoa::FocusAppMenu() { |
| 266 | // Chrome uses the standard Mac OS X menu bar, so this isn't needed. |
| 267 | } |
| 268 | |
| 269 | void BrowserWindowCocoa::RotatePaneFocus(bool forwards) { |
| 270 | // Not needed on the Mac. |
| 271 | } |
| 272 | |
| 273 | void BrowserWindowCocoa::FocusBookmarksToolbar() { |
| 274 | // Not needed on the Mac. |
| 275 | } |
| 276 | |
| 277 | void BrowserWindowCocoa::FocusChromeOSStatus() { |
| 278 | // Not needed on the Mac. |
| 279 | } |
| 280 | |
| 281 | bool BrowserWindowCocoa::IsBookmarkBarVisible() const { |
[email protected] | a007e73 | 2011-08-05 13:32:19 | [diff] [blame] | 282 | return browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | bool BrowserWindowCocoa::IsBookmarkBarAnimating() const { |
| 286 | return [controller_ isBookmarkBarAnimating]; |
| 287 | } |
| 288 | |
[email protected] | 95bf8a5b | 2010-12-22 16:04:07 | [diff] [blame] | 289 | bool BrowserWindowCocoa::IsTabStripEditable() const { |
| 290 | return ![controller_ isDragSessionActive]; |
| 291 | } |
| 292 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 293 | bool BrowserWindowCocoa::IsToolbarVisible() const { |
| 294 | return browser_->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) || |
| 295 | browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR); |
| 296 | } |
| 297 | |
| 298 | // This is called from Browser, which in turn is called directly from |
| 299 | // a menu option. All we do here is set a preference. The act of |
| 300 | // setting the preference sends notifications to all windows who then |
| 301 | // know what to do. |
| 302 | void BrowserWindowCocoa::ToggleBookmarkBar() { |
| 303 | bookmark_utils::ToggleWhenVisible(browser_->profile()); |
| 304 | } |
| 305 | |
| 306 | void BrowserWindowCocoa::AddFindBar( |
| 307 | FindBarCocoaController* find_bar_cocoa_controller) { |
[email protected] | 632983f | 2011-08-08 22:51:24 | [diff] [blame^] | 308 | [controller_ addFindBar:find_bar_cocoa_controller]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 4c6b474b7 | 2011-02-24 21:35:21 | [diff] [blame] | 311 | void BrowserWindowCocoa::ShowAboutChromeDialog() { |
[email protected] | 287b9060 | 2011-03-02 22:43:27 | [diff] [blame] | 312 | // Go through AppController's implementation to bring up the branded panel. |
| 313 | [[NSApp delegate] orderFrontStandardAboutPanel:nil]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void BrowserWindowCocoa::ShowUpdateChromeDialog() { |
[email protected] | 678dae8 | 2011-02-11 20:03:08 | [diff] [blame] | 317 | restart_browser::RequestRestart(window()); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void BrowserWindowCocoa::ShowTaskManager() { |
[email protected] | adb6a84d | 2011-02-07 16:58:40 | [diff] [blame] | 321 | TaskManagerMac::Show(false); |
| 322 | } |
| 323 | |
| 324 | void BrowserWindowCocoa::ShowBackgroundPages() { |
| 325 | TaskManagerMac::Show(true); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void BrowserWindowCocoa::ShowBookmarkBubble(const GURL& url, |
| 329 | bool already_bookmarked) { |
| 330 | [controller_ showBookmarkBubbleForURL:url |
| 331 | alreadyBookmarked:(already_bookmarked ? YES : NO)]; |
| 332 | } |
| 333 | |
| 334 | bool BrowserWindowCocoa::IsDownloadShelfVisible() const { |
| 335 | return [controller_ isDownloadShelfVisible] != NO; |
| 336 | } |
| 337 | |
| 338 | DownloadShelf* BrowserWindowCocoa::GetDownloadShelf() { |
| 339 | DownloadShelfController* shelfController = [controller_ downloadShelf]; |
| 340 | return [shelfController bridge]; |
| 341 | } |
| 342 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 343 | void BrowserWindowCocoa::ShowRepostFormWarningDialog( |
| 344 | TabContents* tab_contents) { |
| 345 | RepostFormWarningMac::Create(GetNativeHandle(), tab_contents); |
| 346 | } |
| 347 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 348 | void BrowserWindowCocoa::ShowCollectedCookiesDialog(TabContents* tab_contents) { |
| 349 | // Deletes itself on close. |
| 350 | new CollectedCookiesMac(GetNativeHandle(), tab_contents); |
| 351 | } |
| 352 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 353 | void BrowserWindowCocoa::ShowThemeInstallBubble() { |
| 354 | ThemeInstallBubbleView::Show(window()); |
| 355 | } |
| 356 | |
| 357 | // We allow closing the window here since the real quit decision on Mac is made |
| 358 | // in [AppController quit:]. |
| 359 | void BrowserWindowCocoa::ConfirmBrowserCloseWithPendingDownloads() { |
| 360 | // Call InProgressDownloadResponse asynchronously to avoid a crash when the |
| 361 | // browser window is closed here (http://crbug.com/44454). |
| 362 | MessageLoop::current()->PostTask( |
| 363 | FROM_HERE, |
| 364 | confirm_close_factory_.NewRunnableMethod( |
| 365 | &Browser::InProgressDownloadResponse, |
| 366 | true)); |
| 367 | } |
| 368 | |
| 369 | void BrowserWindowCocoa::ShowHTMLDialog(HtmlDialogUIDelegate* delegate, |
| 370 | gfx::NativeWindow parent_window) { |
| 371 | [HtmlDialogWindowController showHtmlDialog:delegate |
| 372 | profile:browser_->profile()]; |
| 373 | } |
| 374 | |
| 375 | void BrowserWindowCocoa::UserChangedTheme() { |
| 376 | [controller_ userChangedTheme]; |
| 377 | } |
| 378 | |
| 379 | int BrowserWindowCocoa::GetExtraRenderViewHeight() const { |
| 380 | // Currently this is only used on linux. |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | void BrowserWindowCocoa::TabContentsFocused(TabContents* tab_contents) { |
| 385 | NOTIMPLEMENTED(); |
| 386 | } |
| 387 | |
| 388 | void BrowserWindowCocoa::ShowPageInfo(Profile* profile, |
| 389 | const GURL& url, |
| 390 | const NavigationEntry::SSLStatus& ssl, |
| 391 | bool show_history) { |
| 392 | browser::ShowPageInfoBubble(window(), profile, url, ssl, show_history); |
| 393 | } |
| 394 | |
| 395 | void BrowserWindowCocoa::ShowAppMenu() { |
| 396 | // No-op. Mac doesn't support showing the menus via alt keys. |
| 397 | } |
| 398 | |
| 399 | bool BrowserWindowCocoa::PreHandleKeyboardEvent( |
| 400 | const NativeWebKeyboardEvent& event, bool* is_keyboard_shortcut) { |
| 401 | if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) |
| 402 | return false; |
| 403 | |
| 404 | DCHECK(event.os_event != NULL); |
| 405 | int id = GetCommandId(event); |
| 406 | if (id == -1) |
| 407 | return false; |
| 408 | |
[email protected] | 0618142 | 2011-02-09 17:44:21 | [diff] [blame] | 409 | if (browser_->IsReservedCommandOrKey(id, event)) |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 410 | return HandleKeyboardEventInternal(event.os_event); |
| 411 | |
| 412 | DCHECK(is_keyboard_shortcut != NULL); |
| 413 | *is_keyboard_shortcut = true; |
| 414 | |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | void BrowserWindowCocoa::HandleKeyboardEvent( |
| 419 | const NativeWebKeyboardEvent& event) { |
| 420 | if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) |
| 421 | return; |
| 422 | |
| 423 | DCHECK(event.os_event != NULL); |
| 424 | HandleKeyboardEventInternal(event.os_event); |
| 425 | } |
| 426 | |
| 427 | @interface MenuWalker : NSObject |
| 428 | + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key |
| 429 | menu:(NSMenu*)menu; |
| 430 | @end |
| 431 | |
| 432 | @implementation MenuWalker |
| 433 | + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key |
| 434 | menu:(NSMenu*)menu { |
| 435 | NSMenuItem* result = nil; |
| 436 | |
| 437 | for (NSMenuItem *item in [menu itemArray]) { |
| 438 | NSMenu* submenu = [item submenu]; |
| 439 | if (submenu) { |
| 440 | if (submenu != [NSApp servicesMenu]) |
| 441 | result = [self itemForKeyEquivalent:key |
| 442 | menu:submenu]; |
[email protected] | 0f72a2dc | 2010-12-06 19:40:23 | [diff] [blame] | 443 | } else if ([item cr_firesForKeyEventIfEnabled:key]) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 444 | result = item; |
| 445 | } |
| 446 | |
| 447 | if (result) |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | return result; |
| 452 | } |
| 453 | @end |
| 454 | |
| 455 | int BrowserWindowCocoa::GetCommandId(const NativeWebKeyboardEvent& event) { |
| 456 | if ([event.os_event type] != NSKeyDown) |
| 457 | return -1; |
| 458 | |
| 459 | // Look in menu. |
| 460 | NSMenuItem* item = [MenuWalker itemForKeyEquivalent:event.os_event |
| 461 | menu:[NSApp mainMenu]]; |
| 462 | |
| 463 | if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0) |
| 464 | return [item tag]; |
| 465 | |
| 466 | // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items |
| 467 | // that do not correspond to IDC_ constants need no special treatment however, |
[email protected] | 0618142 | 2011-02-09 17:44:21 | [diff] [blame] | 468 | // as they can't be blacklisted in |Browser::IsReservedCommandOrKey()| anyhow. |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 469 | if (item && [item action] == @selector(performClose:)) |
| 470 | return IDC_CLOSE_WINDOW; |
| 471 | |
| 472 | // "Exit" doesn't use the |commandDispatch:| mechanism either. |
| 473 | if (item && [item action] == @selector(terminate:)) |
| 474 | return IDC_EXIT; |
| 475 | |
| 476 | // Look in secondary keyboard shortcuts. |
| 477 | NSUInteger modifiers = [event.os_event modifierFlags]; |
| 478 | const bool cmdKey = (modifiers & NSCommandKeyMask) != 0; |
| 479 | const bool shiftKey = (modifiers & NSShiftKeyMask) != 0; |
| 480 | const bool cntrlKey = (modifiers & NSControlKeyMask) != 0; |
| 481 | const bool optKey = (modifiers & NSAlternateKeyMask) != 0; |
| 482 | const int keyCode = [event.os_event keyCode]; |
| 483 | const unichar keyChar = KeyCharacterForEvent(event.os_event); |
| 484 | |
| 485 | int cmdNum = CommandForWindowKeyboardShortcut( |
| 486 | cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); |
| 487 | if (cmdNum != -1) |
| 488 | return cmdNum; |
| 489 | |
| 490 | cmdNum = CommandForBrowserKeyboardShortcut( |
| 491 | cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); |
| 492 | if (cmdNum != -1) |
| 493 | return cmdNum; |
| 494 | |
| 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | bool BrowserWindowCocoa::HandleKeyboardEventInternal(NSEvent* event) { |
| 499 | ChromeEventProcessingWindow* event_window = |
| 500 | static_cast<ChromeEventProcessingWindow*>(window()); |
| 501 | DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]); |
| 502 | |
| 503 | // Do not fire shortcuts on key up. |
| 504 | if ([event type] == NSKeyDown) { |
| 505 | // Send the event to the menu before sending it to the browser/window |
| 506 | // shortcut handling, so that if a user configures cmd-left to mean |
| 507 | // "previous tab", it takes precedence over the built-in "history back" |
| 508 | // binding. Other than that, the |-redispatchKeyEvent:| call would take care |
| 509 | // of invoking the original menu item shortcut as well. |
| 510 | |
| 511 | if ([[NSApp mainMenu] performKeyEquivalent:event]) |
| 512 | return true; |
| 513 | |
| 514 | if ([event_window handleExtraBrowserKeyboardShortcut:event]) |
| 515 | return true; |
| 516 | |
| 517 | if ([event_window handleExtraWindowKeyboardShortcut:event]) |
| 518 | return true; |
| 519 | |
| 520 | if ([event_window handleDelayedWindowKeyboardShortcut:event]) |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | return [event_window redispatchKeyEvent:event]; |
| 525 | } |
| 526 | |
| 527 | void BrowserWindowCocoa::ShowCreateWebAppShortcutsDialog( |
[email protected] | f847e608 | 2011-03-24 00:08:26 | [diff] [blame] | 528 | TabContentsWrapper* tab_contents) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 529 | NOTIMPLEMENTED(); |
| 530 | } |
| 531 | |
| 532 | void BrowserWindowCocoa::ShowCreateChromeAppShortcutsDialog( |
| 533 | Profile* profile, const Extension* app) { |
| 534 | NOTIMPLEMENTED(); |
| 535 | } |
| 536 | |
| 537 | void BrowserWindowCocoa::Cut() { |
| 538 | [NSApp sendAction:@selector(cut:) to:nil from:nil]; |
| 539 | } |
| 540 | |
| 541 | void BrowserWindowCocoa::Copy() { |
| 542 | [NSApp sendAction:@selector(copy:) to:nil from:nil]; |
| 543 | } |
| 544 | |
| 545 | void BrowserWindowCocoa::Paste() { |
| 546 | [NSApp sendAction:@selector(paste:) to:nil from:nil]; |
| 547 | } |
| 548 | |
| 549 | void BrowserWindowCocoa::ToggleTabStripMode() { |
| 550 | [controller_ toggleTabStripDisplayMode]; |
| 551 | } |
| 552 | |
| 553 | void BrowserWindowCocoa::OpenTabpose() { |
| 554 | [controller_ openTabpose]; |
| 555 | } |
| 556 | |
[email protected] | d376693 | 2011-08-04 22:18:23 | [diff] [blame] | 557 | void BrowserWindowCocoa::SetPresentationMode(bool presentation_mode) { |
| 558 | [controller_ setPresentationMode:presentation_mode]; |
| 559 | } |
| 560 | |
| 561 | bool BrowserWindowCocoa::InPresentationMode() { |
| 562 | return [controller_ inPresentationMode]; |
| 563 | } |
| 564 | |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 565 | void BrowserWindowCocoa::PrepareForInstant() { |
| 566 | // TODO: implement fade as done on windows. |
| 567 | } |
| 568 | |
[email protected] | e3690ed | 2011-03-25 20:25:14 | [diff] [blame] | 569 | void BrowserWindowCocoa::ShowInstant(TabContentsWrapper* preview) { |
| 570 | [controller_ showInstant:preview->tab_contents()]; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 571 | } |
| 572 | |
[email protected] | 1946c93 | 2010-12-15 00:07:38 | [diff] [blame] | 573 | void BrowserWindowCocoa::HideInstant(bool instant_is_active) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 574 | [controller_ hideInstant]; |
[email protected] | 1946c93 | 2010-12-15 00:07:38 | [diff] [blame] | 575 | |
| 576 | // TODO: add support for |instant_is_active|. |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | gfx::Rect BrowserWindowCocoa::GetInstantBounds() { |
| 580 | // Flip coordinates based on the primary screen. |
| 581 | NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; |
| 582 | NSRect monitorFrame = [screen frame]; |
| 583 | NSRect frame = [controller_ instantFrame]; |
| 584 | gfx::Rect bounds(NSRectToCGRect(frame)); |
| 585 | bounds.set_y(NSHeight(monitorFrame) - bounds.y() - bounds.height()); |
| 586 | return bounds; |
| 587 | } |
| 588 | |
[email protected] | 588300d | 2011-04-28 21:06:35 | [diff] [blame] | 589 | WindowOpenDisposition BrowserWindowCocoa::GetDispositionForPopupBounds( |
| 590 | const gfx::Rect& bounds) { |
| 591 | return NEW_POPUP; |
| 592 | } |
| 593 | |
[email protected] | 632983f | 2011-08-08 22:51:24 | [diff] [blame^] | 594 | FindBar* BrowserWindowCocoa::CreateFindBar() { |
| 595 | // We could push the AddFindBar() call into the FindBarBridge |
| 596 | // constructor or the FindBarCocoaController init, but that makes |
| 597 | // unit testing difficult, since we would also require a |
| 598 | // BrowserWindow object. |
| 599 | FindBarBridge* bridge = new FindBarBridge(); |
| 600 | AddFindBar(bridge->find_bar_cocoa_controller()); |
| 601 | return bridge; |
| 602 | } |
| 603 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 604 | void BrowserWindowCocoa::Observe(int type, |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 605 | const NotificationSource& source, |
| 606 | const NotificationDetails& details) { |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 607 | switch (type) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 608 | // Only the key window gets a direct toggle from the menu. |
| 609 | // Other windows hear about it from the notification. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 610 | case chrome::NOTIFICATION_BOOKMARK_BAR_VISIBILITY_PREF_CHANGED: |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 611 | [controller_ updateBookmarkBarVisibilityWithAnimation:YES]; |
| 612 | break; |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 613 | case chrome::NOTIFICATION_SIDEBAR_CHANGED: |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 614 | UpdateSidebarForContents( |
| 615 | Details<SidebarContainer>(details)->tab_contents()); |
| 616 | break; |
| 617 | default: |
| 618 | NOTREACHED(); // we don't ask for anything else! |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | void BrowserWindowCocoa::DestroyBrowser() { |
| 624 | [controller_ destroyBrowser]; |
| 625 | |
| 626 | // at this point the controller is dead (autoreleased), so |
| 627 | // make sure we don't try to reference it any more. |
| 628 | } |
| 629 | |
| 630 | NSWindow* BrowserWindowCocoa::window() const { |
| 631 | return [controller_ window]; |
| 632 | } |
| 633 | |
| 634 | void BrowserWindowCocoa::UpdateSidebarForContents(TabContents* tab_contents) { |
| 635 | if (tab_contents == browser_->GetSelectedTabContents()) { |
| 636 | [controller_ updateSidebarForContents:tab_contents]; |
| 637 | } |
| 638 | } |