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