Home | History | Annotate | Download | only in toolbar
      1 // Copyright 2013 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_WRENCH_MENU_H_
      6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
      7 
      8 #include <map>
      9 #include <utility>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/observer_list.h"
     13 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
     14 #include "content/public/browser/notification_observer.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "ui/base/models/menu_model.h"
     17 #include "ui/views/controls/menu/menu_delegate.h"
     18 
     19 class BookmarkMenuDelegate;
     20 class Browser;
     21 class WrenchMenuObserver;
     22 
     23 namespace ui {
     24 class NativeTheme;
     25 }
     26 
     27 namespace views {
     28 class MenuButton;
     29 struct MenuConfig;
     30 class MenuItemView;
     31 class MenuRunner;
     32 class View;
     33 }  // namespace views
     34 
     35 // WrenchMenu adapts the WrenchMenuModel to view's menu related classes.
     36 class WrenchMenu : public views::MenuDelegate,
     37                    public BaseBookmarkModelObserver,
     38                    public content::NotificationObserver {
     39  public:
     40   // TODO: remove |use_new_menu| and |supports_new_separators|.
     41   WrenchMenu(Browser* browser,
     42              bool use_new_menu,
     43              bool supports_new_separators);
     44   virtual ~WrenchMenu();
     45 
     46   void Init(ui::MenuModel* model);
     47 
     48   // Shows the menu relative to the specified view.
     49   void RunMenu(views::MenuButton* host);
     50 
     51   // Whether the menu is currently visible to the user.
     52   bool IsShowing();
     53 
     54   const views::MenuConfig& GetMenuConfig() const;
     55 
     56   bool use_new_menu() const { return use_new_menu_; }
     57 
     58   void AddObserver(WrenchMenuObserver* observer);
     59   void RemoveObserver(WrenchMenuObserver* observer);
     60 
     61   // MenuDelegate overrides:
     62   virtual const gfx::Font* GetLabelFont(int command_id) const OVERRIDE;
     63   virtual bool GetForegroundColor(int command_id,
     64                                   bool is_hovered,
     65                                   SkColor* override_color) const OVERRIDE;
     66   virtual base::string16 GetTooltipText(int command_id,
     67                                   const gfx::Point& p) const OVERRIDE;
     68   virtual bool IsTriggerableEvent(views::MenuItemView* menu,
     69                                   const ui::Event& e) OVERRIDE;
     70   virtual bool GetDropFormats(
     71       views::MenuItemView* menu,
     72       int* formats,
     73       std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
     74   virtual bool AreDropTypesRequired(views::MenuItemView* menu) OVERRIDE;
     75   virtual bool CanDrop(views::MenuItemView* menu,
     76                        const ui::OSExchangeData& data) OVERRIDE;
     77   virtual int GetDropOperation(views::MenuItemView* item,
     78                                const ui::DropTargetEvent& event,
     79                                DropPosition* position) OVERRIDE;
     80   virtual int OnPerformDrop(views::MenuItemView* menu,
     81                             DropPosition position,
     82                             const ui::DropTargetEvent& event) OVERRIDE;
     83   virtual bool ShowContextMenu(views::MenuItemView* source,
     84                                int command_id,
     85                                const gfx::Point& p,
     86                                ui::MenuSourceType source_type) OVERRIDE;
     87   virtual bool CanDrag(views::MenuItemView* menu) OVERRIDE;
     88   virtual void WriteDragData(views::MenuItemView* sender,
     89                              ui::OSExchangeData* data) OVERRIDE;
     90   virtual int GetDragOperations(views::MenuItemView* sender) OVERRIDE;
     91   virtual int GetMaxWidthForMenu(views::MenuItemView* menu) OVERRIDE;
     92   virtual bool IsItemChecked(int command_id) const OVERRIDE;
     93   virtual bool IsCommandEnabled(int command_id) const OVERRIDE;
     94   virtual void ExecuteCommand(int command_id, int mouse_event_flags) OVERRIDE;
     95   virtual bool GetAccelerator(int command_id,
     96                               ui::Accelerator* accelerator) OVERRIDE;
     97   virtual void WillShowMenu(views::MenuItemView* menu) OVERRIDE;
     98   virtual void WillHideMenu(views::MenuItemView* menu) OVERRIDE;
     99 
    100   // BaseBookmarkModelObserver overrides:
    101   virtual void BookmarkModelChanged() OVERRIDE;
    102 
    103   // content::NotificationObserver overrides:
    104   virtual void Observe(int type,
    105                        const content::NotificationSource& source,
    106                        const content::NotificationDetails& details) OVERRIDE;
    107 
    108  private:
    109   class CutCopyPasteView;
    110   class RecentTabsMenuModelDelegate;
    111   class ZoomView;
    112 
    113   typedef std::pair<ui::MenuModel*,int> Entry;
    114   typedef std::map<int,Entry> CommandIDToEntry;
    115 
    116   const ui::NativeTheme* GetNativeTheme() const;
    117 
    118   // Populates |parent| with all the child menus in |model|. Recursively invokes
    119   // |PopulateMenu| for any submenu.
    120   void PopulateMenu(views::MenuItemView* parent,
    121                     ui::MenuModel* model);
    122 
    123   // Adds a new menu item to |parent| at |menu_index| to represent the item in
    124   // |model| at |model_index|:
    125   // - |menu_index|: position in |parent| to add the new item.
    126   // - |model_index|: position in |model| to retrieve information about the
    127   //   new menu item.
    128   // - |height|: For button containing menu items, a |height| override can be
    129   //   specified with a number bigger then 0.
    130   // The returned item's MenuItemView::GetCommand() is the same as that of
    131   // |model|->GetCommandIdAt(|model_index|).
    132   views::MenuItemView* AddMenuItem(views::MenuItemView* parent,
    133                                    int menu_index,
    134                                    ui::MenuModel* model,
    135                                    int model_index,
    136                                    ui::MenuModel::ItemType menu_type,
    137                                    int height);
    138 
    139   // Invoked from the cut/copy/paste menus. Cancels the current active menu and
    140   // activates the menu item in |model| at |index|.
    141   void CancelAndEvaluate(ui::MenuModel* model, int index);
    142 
    143   // Creates the bookmark menu if necessary. Does nothing if already created or
    144   // the bookmark model isn't loaded.
    145   void CreateBookmarkMenu();
    146 
    147   // Returns the index of the MenuModel/index pair representing the |command_id|
    148   // in |command_id_to_entry_|.
    149   int ModelIndexFromCommandId(int command_id) const;
    150 
    151   // The views menu. Owned by |menu_runner_|.
    152   views::MenuItemView* root_;
    153 
    154   scoped_ptr<views::MenuRunner> menu_runner_;
    155 
    156   // Maps from the command ID in model to the model/index pair the item came
    157   // from.
    158   CommandIDToEntry command_id_to_entry_;
    159 
    160   // Browser the menu is being shown for.
    161   Browser* browser_;
    162 
    163   // |CancelAndEvaluate| sets |selected_menu_model_| and |selected_index_|.
    164   // If |selected_menu_model_| is non-null after the menu completes
    165   // ActivatedAt is invoked. This is done so that ActivatedAt isn't invoked
    166   // while the message loop is nested.
    167   ui::MenuModel* selected_menu_model_;
    168   int selected_index_;
    169 
    170   // Used for managing the bookmark menu items.
    171   scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_;
    172 
    173   // Menu corresponding to IDC_BOOKMARKS_MENU.
    174   views::MenuItemView* bookmark_menu_;
    175 
    176   // Menu corresponding to IDC_FEEDBACK.
    177   views::MenuItemView* feedback_menu_item_;
    178 
    179   // Used for managing "Recent tabs" menu items.
    180   scoped_ptr<RecentTabsMenuModelDelegate> recent_tabs_menu_model_delegate_;
    181 
    182   content::NotificationRegistrar registrar_;
    183 
    184   const bool use_new_menu_;
    185 
    186   const bool supports_new_separators_;
    187 
    188   ObserverList<WrenchMenuObserver> observer_list_;
    189 
    190   DISALLOW_COPY_AND_ASSIGN(WrenchMenu);
    191 };
    192 
    193 #endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
    194