1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_UI_COCOA_TOOLBAR_TOOLBAR_CONTROLLER_H_ 6 #define CHROME_BROWSER_UI_COCOA_TOOLBAR_TOOLBAR_CONTROLLER_H_ 7 8 #import <Cocoa/Cocoa.h> 9 10 #include "base/mac/scoped_nsobject.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/prefs/pref_member.h" 13 #import "chrome/browser/ui/cocoa/command_observer_bridge.h" 14 #import "chrome/browser/ui/cocoa/url_drop_target.h" 15 #import "chrome/browser/ui/cocoa/view_resizer.h" 16 #import "ui/base/cocoa/tracking_area.h" 17 18 @class AutocompleteTextField; 19 @class AutocompleteTextFieldEditor; 20 @class BackForwardMenuController; 21 class Browser; 22 @class BrowserActionsContainerView; 23 @class BrowserActionsController; 24 class CommandUpdater; 25 class LocationBarViewMac; 26 @class MenuButton; 27 class Profile; 28 @class ReloadButton; 29 @class ToolbarButton; 30 class ToolbarModel; 31 @class WrenchMenuController; 32 33 namespace content { 34 class WebContents; 35 } 36 37 namespace ToolbarControllerInternal { 38 class NotificationBridge; 39 } 40 41 // A controller for the toolbar in the browser window. Manages 42 // updating the state for location bar and back/fwd/reload/go buttons. 43 // Manages the bookmark bar and its position in the window relative to 44 // the web content view. 45 46 @interface ToolbarController : NSViewController<CommandObserverProtocol, 47 URLDropTargetController> { 48 @protected 49 // The ordering is important for unit tests. If new items are added or the 50 // ordering is changed, make sure to update |-toolbarViews| and the 51 // corresponding enum in the unit tests. 52 IBOutlet MenuButton* backButton_; 53 IBOutlet MenuButton* forwardButton_; 54 IBOutlet ReloadButton* reloadButton_; 55 IBOutlet ToolbarButton* homeButton_; 56 IBOutlet MenuButton* wrenchButton_; 57 IBOutlet AutocompleteTextField* locationBar_; 58 IBOutlet BrowserActionsContainerView* browserActionsContainerView_; 59 60 @private 61 ToolbarModel* toolbarModel_; // weak, one per window 62 CommandUpdater* commands_; // weak, one per window 63 Profile* profile_; // weak, one per window 64 Browser* browser_; // weak, one per window 65 scoped_ptr<CommandObserverBridge> commandObserver_; 66 scoped_ptr<LocationBarViewMac> locationBarView_; 67 base::scoped_nsobject<AutocompleteTextFieldEditor> 68 autocompleteTextFieldEditor_; 69 id<ViewResizer> resizeDelegate_; // weak 70 base::scoped_nsobject<BackForwardMenuController> backMenuController_; 71 base::scoped_nsobject<BackForwardMenuController> forwardMenuController_; 72 base::scoped_nsobject<BrowserActionsController> browserActionsController_; 73 74 // Lazily-instantiated menu controller. 75 base::scoped_nsobject<WrenchMenuController> wrenchMenuController_; 76 77 // Used for monitoring the optional toolbar button prefs. 78 scoped_ptr<ToolbarControllerInternal::NotificationBridge> notificationBridge_; 79 BooleanPrefMember showHomeButton_; 80 BOOL hasToolbar_; // If NO, we may have only the location bar. 81 BOOL hasLocationBar_; // If |hasToolbar_| is YES, this must also be YES. 82 BOOL locationBarAtMinSize_; // If the location bar is at the minimum size. 83 84 // We have an extra retain in the locationBar_. 85 // See comments in awakeFromNib for more info. 86 base::scoped_nsobject<AutocompleteTextField> locationBarRetainer_; 87 88 // Tracking area for mouse enter/exit/moved in the toolbar. 89 ui::ScopedCrTrackingArea trackingArea_; 90 91 // We retain/release the hover button since interaction with the 92 // button may make it go away (e.g. delete menu option over a 93 // bookmark button). Thus this variable is not weak. The 94 // hoveredButton_ is required to have an NSCell that responds to 95 // setMouseInside:animate:. 96 NSButton* hoveredButton_; 97 } 98 99 // Initialize the toolbar and register for command updates. The profile is 100 // needed for initializing the location bar. The browser is needed for 101 // initializing the back/forward menus. 102 - (id)initWithModel:(ToolbarModel*)model 103 commands:(CommandUpdater*)commands 104 profile:(Profile*)profile 105 browser:(Browser*)browser 106 resizeDelegate:(id<ViewResizer>)resizeDelegate; 107 108 // Get the C++ bridge object representing the location bar for this tab. 109 - (LocationBarViewMac*)locationBarBridge; 110 111 // Called by the Window delegate so we can provide a custom field editor if 112 // needed. 113 // Note that this may be called for objects unrelated to the toolbar. 114 // returns nil if we don't want to override the custom field editor for |obj|. 115 - (id)customFieldEditorForObject:(id)obj; 116 117 // Make the location bar the first responder, if possible. 118 - (void)focusLocationBar:(BOOL)selectAll; 119 120 // Updates the toolbar (and transitively the location bar) with the states of 121 // the specified |tab|. If |shouldRestore| is true, we're switching 122 // (back?) to this tab and should restore any previous location bar state 123 // (such as user editing) as well. 124 - (void)updateToolbarWithContents:(content::WebContents*)tabForRestoring 125 shouldRestoreState:(BOOL)shouldRestore; 126 127 // Sets whether or not the current page in the frontmost tab is bookmarked. 128 - (void)setStarredState:(BOOL)isStarred; 129 130 // Happens when the zoom for the active tab changes, the active tab switches, or 131 // a new tab or browser window is created. |canShowBubble| indicates if it is 132 // appropriate to show a zoom bubble for the change. 133 - (void)zoomChangedForActiveTab:(BOOL)canShowBubble; 134 135 // Called to update the loading state. Handles updating the go/stop 136 // button state. |force| is set if the update is due to changing 137 // tabs, as opposed to the page-load finishing. See comment in 138 // reload_button.h. 139 - (void)setIsLoading:(BOOL)isLoading force:(BOOL)force; 140 141 // Allow turning off the toolbar (but we may keep the location bar without a 142 // surrounding toolbar). If |toolbar| is YES, the value of |hasLocationBar| is 143 // ignored. This changes the behavior of other methods, like |-view|. 144 - (void)setHasToolbar:(BOOL)toolbar hasLocationBar:(BOOL)locBar; 145 146 // Point on the star icon for the bookmark bubble to be - in the 147 // associated window's coordinate system. 148 - (NSPoint)bookmarkBubblePoint; 149 150 // Returns the desired toolbar height for the given compression factor. 151 - (CGFloat)desiredHeightForCompression:(CGFloat)compressByHeight; 152 153 // Set the opacity of the divider (the line at the bottom) *if* we have a 154 // |ToolbarView| (0 means don't show it); no-op otherwise. 155 - (void)setDividerOpacity:(CGFloat)opacity; 156 157 // Create and add the Browser Action buttons to the toolbar view. 158 - (void)createBrowserActionButtons; 159 160 // Return the BrowserActionsController for this toolbar. 161 - (BrowserActionsController*)browserActionsController; 162 163 // Returns the wrench button. 164 - (NSView*)wrenchButton; 165 166 @end 167 168 // A set of private methods used by subclasses. Do not call these directly 169 // unless a subclass of ToolbarController. 170 @interface ToolbarController(ProtectedMethods) 171 // Designated initializer which takes a nib name in order to allow subclasses 172 // to load a different nib file. 173 - (id)initWithModel:(ToolbarModel*)model 174 commands:(CommandUpdater*)commands 175 profile:(Profile*)profile 176 browser:(Browser*)browser 177 resizeDelegate:(id<ViewResizer>)resizeDelegate 178 nibFileNamed:(NSString*)nibName; 179 @end 180 181 // A set of private methods used by tests, in the absence of "friends" in ObjC. 182 @interface ToolbarController(PrivateTestMethods) 183 // Returns an array of views in the order of the outlets above. 184 - (NSArray*)toolbarViews; 185 - (void)showOptionalHomeButton; 186 - (void)installWrenchMenu; 187 - (WrenchMenuController*)wrenchMenuController; 188 // Return a hover button for the current event. 189 - (NSButton*)hoverButtonForEvent:(NSEvent*)theEvent; 190 @end 191 192 #endif // CHROME_BROWSER_UI_COCOA_TOOLBAR_TOOLBAR_CONTROLLER_H_ 193