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