Home | History | Annotate | Download | only in views
      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_VIEWS_TOOLBAR_VIEW_H_
      6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_VIEW_H_
      7 #pragma once
      8 
      9 #include <vector>
     10 
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "chrome/browser/command_updater.h"
     14 #include "chrome/browser/prefs/pref_member.h"
     15 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h"
     16 #include "chrome/browser/ui/views/accessible_pane_view.h"
     17 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
     18 #include "chrome/browser/ui/views/reload_button.h"
     19 #include "ui/base/animation/slide_animation.h"
     20 #include "ui/base/models/accelerator.h"
     21 #include "views/controls/button/menu_button.h"
     22 #include "views/controls/menu/menu.h"
     23 #include "views/controls/menu/menu_wrapper.h"
     24 #include "views/controls/menu/view_menu_delegate.h"
     25 #include "views/view.h"
     26 
     27 class BrowserActionsContainer;
     28 class Browser;
     29 class Profile;
     30 #if defined(OS_CHROMEOS)
     31 namespace views {
     32 class Menu2;
     33 }  // namespace views
     34 #endif
     35 class WrenchMenu;
     36 
     37 // The Browser Window's toolbar.
     38 class ToolbarView : public AccessiblePaneView,
     39                     public views::ViewMenuDelegate,
     40                     public ui::AcceleratorProvider,
     41                     public LocationBarView::Delegate,
     42                     public NotificationObserver,
     43                     public CommandUpdater::CommandObserver,
     44                     public views::ButtonListener {
     45  public:
     46   explicit ToolbarView(Browser* browser);
     47   virtual ~ToolbarView();
     48 
     49   // Create the contents of the Browser Toolbar
     50   void Init(Profile* profile);
     51 
     52   // Sets the profile which is active on the currently-active tab.
     53   void SetProfile(Profile* profile);
     54   Profile* profile() { return profile_; }
     55 
     56   // Updates the toolbar (and transitively the location bar) with the states of
     57   // the specified |tab|.  If |should_restore_state| is true, we're switching
     58   // (back?) to this tab and should restore any previous location bar state
     59   // (such as user editing) as well.
     60   void Update(TabContents* tab, bool should_restore_state);
     61 
     62   // Set focus to the toolbar with complete keyboard access, with the
     63   // focus initially set to the location bar. Focus will be restored
     64   // to the ViewStorage with id |view_storage_id| if the user escapes.
     65   void SetPaneFocusAndFocusLocationBar(int view_storage_id);
     66 
     67   // Set focus to the toolbar with complete keyboard access, with the
     68   // focus initially set to the app menu. Focus will be restored
     69   // to the ViewStorage with id |view_storage_id| if the user escapes.
     70   void SetPaneFocusAndFocusAppMenu(int view_storage_id);
     71 
     72   // Returns true if the app menu is focused.
     73   bool IsAppMenuFocused();
     74 
     75   // Add a listener to receive a callback when the menu opens.
     76   void AddMenuListener(views::MenuListener* listener);
     77 
     78   // Remove a menu listener.
     79   void RemoveMenuListener(views::MenuListener* listener);
     80 
     81   virtual bool GetAcceleratorInfo(int id, ui::Accelerator* accel);
     82 
     83   // Accessors...
     84   Browser* browser() const { return browser_; }
     85   BrowserActionsContainer* browser_actions() const { return browser_actions_; }
     86   ReloadButton* reload_button() const { return reload_; }
     87   LocationBarView* location_bar() const { return location_bar_; }
     88   views::MenuButton* app_menu() const { return app_menu_; }
     89 
     90   // Overridden from AccessiblePaneView
     91   virtual bool SetPaneFocus(int view_storage_id, View* initial_focus) OVERRIDE;
     92   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
     93 
     94   // Overridden from views::MenuDelegate:
     95   virtual void RunMenu(views::View* source, const gfx::Point& pt) OVERRIDE;
     96 
     97   // Overridden from LocationBarView::Delegate:
     98   virtual TabContentsWrapper* GetTabContentsWrapper() const OVERRIDE;
     99   virtual InstantController* GetInstant() OVERRIDE;
    100   virtual void OnInputInProgress(bool in_progress) OVERRIDE;
    101 
    102   // Overridden from CommandUpdater::CommandObserver:
    103   virtual void EnabledStateChangedForCommand(int id, bool enabled) OVERRIDE;
    104 
    105   // Overridden from views::BaseButton::ButtonListener:
    106   virtual void ButtonPressed(views::Button* sender, const views::Event& event)
    107       OVERRIDE;
    108 
    109   // Overridden from NotificationObserver:
    110   virtual void Observe(NotificationType type,
    111                        const NotificationSource& source,
    112                        const NotificationDetails& details) OVERRIDE;
    113 
    114   // Overridden from ui::AcceleratorProvider:
    115   virtual bool GetAcceleratorForCommandId(
    116       int command_id, ui::Accelerator* accelerator) OVERRIDE;
    117 
    118   // Overridden from views::View:
    119   virtual gfx::Size GetPreferredSize() OVERRIDE;
    120   virtual void Layout() OVERRIDE;
    121   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
    122   virtual bool GetDropFormats(
    123       int* formats,
    124       std::set<OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
    125   virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
    126   virtual int OnDragUpdated(const views::DropTargetEvent& event) OVERRIDE;
    127   virtual int OnPerformDrop(const views::DropTargetEvent& event) OVERRIDE;
    128   virtual void OnThemeChanged() OVERRIDE;
    129 
    130   // The apparent horizontal space between most items, and the vertical padding
    131   // above and below them.
    132   static const int kStandardSpacing;
    133   // The top of the toolbar has an edge we have to skip over in addition to the
    134   // standard spacing.
    135   static const int kVertSpacing;
    136 
    137  protected:
    138 
    139   // Overridden from AccessiblePaneView
    140   virtual views::View* GetDefaultFocusableChild() OVERRIDE;
    141   virtual void RemovePaneFocus() OVERRIDE;
    142 
    143  private:
    144   // Returns true if we should show the upgrade recommended dot.
    145   bool IsUpgradeRecommended();
    146 
    147   // Retrieve which badge we should show when recommending an upgrade.
    148   int GetUpgradeRecommendedBadge() const;
    149 
    150   // Returns true if we should show the background page badge.
    151   bool ShouldShowBackgroundPageBadge();
    152 
    153   // Returns true if we should show the warning for incompatible software.
    154   bool ShouldShowIncompatibilityWarning();
    155 
    156   // Returns the number of pixels above the location bar in non-normal display.
    157   int PopupTopSpacing() const;
    158 
    159   // Loads the images for all the child views.
    160   void LoadImages();
    161 
    162   // Types of display mode this toolbar can have.
    163   enum DisplayMode {
    164     DISPLAYMODE_NORMAL,       // Normal toolbar with buttons, etc.
    165     DISPLAYMODE_LOCATION      // Slimline toolbar showing only compact location
    166                               // bar, used for popups.
    167   };
    168   bool IsDisplayModeNormal() const {
    169     return display_mode_ == DISPLAYMODE_NORMAL;
    170   }
    171 
    172   // Updates the badge on the app menu (Wrench).
    173   void UpdateAppMenuBadge();
    174 
    175   // Gets a bitmap with the icon for the app menu and any overlaid notification
    176   // badge.
    177   SkBitmap GetAppMenuIcon(views::CustomButton::ButtonState state);
    178 
    179   // Gets a badge for the wrench icon corresponding to the number of
    180   // unacknowledged background pages in the system.
    181   SkBitmap GetBackgroundPageBadge();
    182 
    183   scoped_ptr<BackForwardMenuModel> back_menu_model_;
    184   scoped_ptr<BackForwardMenuModel> forward_menu_model_;
    185 
    186   // The model that contains the security level, text, icon to display...
    187   ToolbarModel* model_;
    188 
    189   // Controls
    190   views::ImageButton* back_;
    191   views::ImageButton* forward_;
    192   ReloadButton* reload_;
    193   views::ImageButton* home_;
    194   LocationBarView* location_bar_;
    195   BrowserActionsContainer* browser_actions_;
    196   views::MenuButton* app_menu_;
    197   Profile* profile_;
    198   Browser* browser_;
    199 
    200   // Contents of the profiles menu to populate with profile names.
    201   scoped_ptr<ui::SimpleMenuModel> profiles_menu_contents_;
    202 
    203   // Controls whether or not a home button should be shown on the toolbar.
    204   BooleanPrefMember show_home_button_;
    205 
    206   // The display mode used when laying out the toolbar.
    207   DisplayMode display_mode_;
    208 
    209   // The contents of the wrench menu.
    210   scoped_ptr<ui::SimpleMenuModel> wrench_menu_model_;
    211 
    212 #if defined(OS_CHROMEOS)
    213   // Wrench menu using WebUI menu.
    214   // MenuLister is managed by Menu2.
    215   scoped_ptr<views::Menu2> wrench_menu_2_;
    216 #endif
    217 
    218   // Wrench menu.
    219   scoped_refptr<WrenchMenu> wrench_menu_;
    220 
    221   // Vector of listeners to receive callbacks when the menu opens.
    222   std::vector<views::MenuListener*> menu_listeners_;
    223 
    224   NotificationRegistrar registrar_;
    225 
    226   // If non-null the destructor sets this to true. This is set to a non-null
    227   // while the menu is showing and used to detect if the menu was deleted while
    228   // running.
    229   bool* destroyed_flag_;
    230 
    231   DISALLOW_IMPLICIT_CONSTRUCTORS(ToolbarView);
    232 };
    233 
    234 #endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_VIEW_H_
    235