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