Home | History | Annotate | Download | only in views
      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_VIEWS_TOOLBAR_VIEW_H_
      6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_VIEW_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/observer_list.h"
     13 #include "base/prefs/pref_member.h"
     14 #include "chrome/browser/command_observer.h"
     15 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h"
     16 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
     17 #include "chrome/browser/ui/views/reload_button.h"
     18 #include "ui/base/accelerators/accelerator.h"
     19 #include "ui/base/animation/slide_animation.h"
     20 #include "ui/views/accessible_pane_view.h"
     21 #include "ui/views/controls/button/menu_button.h"
     22 #include "ui/views/controls/button/menu_button_listener.h"
     23 #include "ui/views/view.h"
     24 
     25 class BrowserActionsContainer;
     26 class Browser;
     27 class HomeImageButton;
     28 class WrenchMenu;
     29 class WrenchMenuModel;
     30 class WrenchToolbarButton;
     31 
     32 namespace views {
     33 class MenuListener;
     34 }
     35 
     36 // The Browser Window's toolbar.
     37 class ToolbarView : public views::AccessiblePaneView,
     38                     public views::MenuButtonListener,
     39                     public ui::AcceleratorProvider,
     40                     public LocationBarView::Delegate,
     41                     public content::NotificationObserver,
     42                     public CommandObserver,
     43                     public views::ButtonListener,
     44                     public views::WidgetObserver {
     45  public:
     46   // The view class name.
     47   static const char kViewClassName[];
     48 
     49   explicit ToolbarView(Browser* browser);
     50   virtual ~ToolbarView();
     51 
     52   // Create the contents of the Browser Toolbar.
     53   void Init();
     54 
     55   // Updates the toolbar (and transitively the location bar) with the states of
     56   // the specified |tab|.  If |should_restore_state| is true, we're switching
     57   // (back?) to this tab and should restore any previous location bar state
     58   // (such as user editing) as well.
     59   void Update(content::WebContents* tab, bool should_restore_state);
     60 
     61   // Set focus to the toolbar with complete keyboard access, with the
     62   // focus initially set to the app menu. Focus will be restored
     63   // to the last focused view if the user escapes.
     64   void SetPaneFocusAndFocusAppMenu();
     65 
     66   // Returns true if the app menu is focused.
     67   bool IsAppMenuFocused();
     68 
     69   // Add a listener to receive a callback when the menu opens.
     70   void AddMenuListener(views::MenuListener* listener);
     71 
     72   // Remove a menu listener.
     73   void RemoveMenuListener(views::MenuListener* listener);
     74 
     75   virtual bool GetAcceleratorInfo(int id, ui::Accelerator* accel);
     76 
     77   // Returns the view to which the bookmark bubble should be anchored.
     78   views::View* GetBookmarkBubbleAnchor();
     79 
     80   // Accessors...
     81   Browser* browser() const { return browser_; }
     82   BrowserActionsContainer* browser_actions() const { return browser_actions_; }
     83   ReloadButton* reload_button() const { return reload_; }
     84   LocationBarView* location_bar() const { return location_bar_; }
     85   views::MenuButton* app_menu() const;
     86 
     87   // Overridden from AccessiblePaneView
     88   virtual bool SetPaneFocus(View* initial_focus) OVERRIDE;
     89   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
     90 
     91   // Overridden from views::MenuButtonListener:
     92   virtual void OnMenuButtonClicked(views::View* source,
     93                                    const gfx::Point& point) OVERRIDE;
     94 
     95   // Overridden from LocationBarView::Delegate:
     96   virtual content::WebContents* GetWebContents() const OVERRIDE;
     97   virtual InstantController* GetInstant() OVERRIDE;
     98   virtual views::Widget* CreateViewsBubble(
     99       views::BubbleDelegateView* bubble_delegate) OVERRIDE;
    100   virtual PageActionImageView* CreatePageActionImageView(
    101       LocationBarView* owner, ExtensionAction* action) OVERRIDE;
    102   virtual ContentSettingBubbleModelDelegate*
    103       GetContentSettingBubbleModelDelegate() OVERRIDE;
    104   virtual void ShowWebsiteSettings(content::WebContents* web_contents,
    105                                    const GURL& url,
    106                                    const content::SSLStatus& ssl) OVERRIDE;
    107   virtual void OnInputInProgress(bool in_progress) OVERRIDE;
    108 
    109   // Overridden from CommandObserver:
    110   virtual void EnabledStateChangedForCommand(int id, bool enabled) OVERRIDE;
    111 
    112   // Overridden from views::ButtonListener:
    113   virtual void ButtonPressed(views::Button* sender,
    114                              const ui::Event& event) OVERRIDE;
    115 
    116   // Overridden from content::NotificationObserver:
    117   virtual void Observe(int type,
    118                        const content::NotificationSource& source,
    119                        const content::NotificationDetails& details) OVERRIDE;
    120 
    121   // Overridden from ui::AcceleratorProvider:
    122   virtual bool GetAcceleratorForCommandId(
    123       int command_id, ui::Accelerator* accelerator) OVERRIDE;
    124 
    125   // Overridden from views::View:
    126   virtual gfx::Size GetPreferredSize() OVERRIDE;
    127   virtual void Layout() OVERRIDE;
    128   virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
    129   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
    130   virtual bool GetDropFormats(
    131       int* formats,
    132       std::set<OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
    133   virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
    134   virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
    135   virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
    136   virtual void OnThemeChanged() OVERRIDE;
    137   virtual const char* GetClassName() const OVERRIDE;
    138   virtual bool AcceleratorPressed(const ui::Accelerator& acc) OVERRIDE;
    139 
    140   // Whether the wrench/hotdogs menu is currently showing.
    141   bool IsWrenchMenuShowing() const;
    142 
    143   // The apparent horizontal space between most items, and the vertical padding
    144   // above and below them.
    145   static const int kStandardSpacing;
    146   // The top of the toolbar has an edge we have to skip over in addition to the
    147   // standard spacing.
    148   static const int kVertSpacing;
    149 
    150  protected:
    151   // Overridden from AccessiblePaneView
    152   virtual bool SetPaneFocusAndFocusDefault() OVERRIDE;
    153   virtual void RemovePaneFocus() OVERRIDE;
    154 
    155  private:
    156   // Types of display mode this toolbar can have.
    157   enum DisplayMode {
    158     DISPLAYMODE_NORMAL,       // Normal toolbar with buttons, etc.
    159     DISPLAYMODE_LOCATION      // Slimline toolbar showing only compact location
    160                               // bar, used for popups.
    161   };
    162 
    163   // Returns true if we should show the upgrade recommended dot.
    164   bool ShouldShowUpgradeRecommended();
    165 
    166   // Returns true if we should show the background page badge.
    167   bool ShouldShowBackgroundPageBadge();
    168 
    169   // Returns true if we should show the warning for incompatible software.
    170   bool ShouldShowIncompatibilityWarning();
    171 
    172   // Returns the number of pixels above the location bar in non-normal display.
    173   int PopupTopSpacing() const;
    174 
    175   // Loads the images for all the child views.
    176   void LoadImages();
    177 
    178   bool is_display_mode_normal() const {
    179     return display_mode_ == DISPLAYMODE_NORMAL;
    180   }
    181 
    182   // Shows the critical notification bubble against the wrench menu.
    183   void ShowCriticalNotification();
    184 
    185   // Shows the outdated install notification bubble against the wrench menu.
    186   void ShowOutdatedInstallNotification();
    187 
    188   // Updates the badge and the accessible name of the app menu (Wrench).
    189   void UpdateAppMenuState();
    190 
    191   // Updates the severity level on the wrench menu button.
    192   void UpdateWrenchButtonSeverity();
    193 
    194   void OnShowHomeButtonChanged();
    195 
    196   int content_shadow_height() const;
    197 
    198   // The model that contains the security level, text, icon to display...
    199   ToolbarModel* model_;
    200 
    201   // Controls
    202   views::ImageButton* back_;
    203   views::ImageButton* forward_;
    204   ReloadButton* reload_;
    205   HomeImageButton* home_;
    206   LocationBarView* location_bar_;
    207   BrowserActionsContainer* browser_actions_;
    208   WrenchToolbarButton* app_menu_;
    209   Browser* browser_;
    210 
    211   // Controls whether or not a home button should be shown on the toolbar.
    212   BooleanPrefMember show_home_button_;
    213 
    214   // The display mode used when laying out the toolbar.
    215   DisplayMode display_mode_;
    216 
    217   // Wrench model and menu.
    218   // Note that the menu should be destroyed before the model it uses, so the
    219   // menu should be listed later.
    220   scoped_ptr<WrenchMenuModel> wrench_menu_model_;
    221   scoped_ptr<WrenchMenu> wrench_menu_;
    222 
    223   // A list of listeners to call when the menu opens.
    224   ObserverList<views::MenuListener> menu_listeners_;
    225 
    226   content::NotificationRegistrar registrar_;
    227 
    228   DISALLOW_IMPLICIT_CONSTRUCTORS(ToolbarView);
    229 };
    230 
    231 #endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_VIEW_H_
    232