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