Home | History | Annotate | Download | only in extensions
      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_EXTENSIONS_BROWSER_ACTION_OVERFLOW_MENU_CONTROLLER_H_
      6 #define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_BROWSER_ACTION_OVERFLOW_MENU_CONTROLLER_H_
      7 
      8 #include <set>
      9 #include <vector>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "base/sequenced_task_runner_helpers.h"
     15 #include "ui/views/controls/menu/menu_delegate.h"
     16 
     17 class Browser;
     18 class BrowserActionsContainer;
     19 class BrowserActionView;
     20 
     21 class IconUpdater;
     22 
     23 namespace views {
     24 class MenuRunner;
     25 class Widget;
     26 }
     27 
     28 // This class handles the overflow menu for browser actions (showing the menu,
     29 // drag and drop, etc). This class manages its own lifetime.
     30 class BrowserActionOverflowMenuController : public views::MenuDelegate {
     31  public:
     32   // The observer is notified prior to the menu being deleted.
     33   class Observer {
     34    public:
     35     virtual void NotifyMenuDeleted(
     36         BrowserActionOverflowMenuController* controller) = 0;
     37   };
     38 
     39   BrowserActionOverflowMenuController(
     40       BrowserActionsContainer* owner,
     41       Browser* browser,
     42       views::MenuButton* menu_button,
     43       const std::vector<BrowserActionView*>& views,
     44       int start_index);
     45 
     46   void set_observer(Observer* observer) { observer_ = observer; }
     47 
     48   // Shows the overflow menu.
     49   bool RunMenu(views::Widget* widget, bool for_drop);
     50 
     51   // Closes the overflow menu (and its context menu if open as well).
     52   void CancelMenu();
     53 
     54   // Overridden from views::MenuDelegate:
     55   virtual bool IsCommandEnabled(int id) const OVERRIDE;
     56   virtual void ExecuteCommand(int id) OVERRIDE;
     57   virtual bool ShowContextMenu(views::MenuItemView* source,
     58                                int id,
     59                                const gfx::Point& p,
     60                                ui::MenuSourceType source_type) OVERRIDE;
     61   virtual void DropMenuClosed(views::MenuItemView* menu) OVERRIDE;
     62   // These drag functions offer support for dragging icons into the overflow
     63   // menu.
     64   virtual bool GetDropFormats(
     65       views::MenuItemView* menu,
     66       int* formats,
     67       std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
     68   virtual bool AreDropTypesRequired(views::MenuItemView* menu) OVERRIDE;
     69   virtual bool CanDrop(views::MenuItemView* menu,
     70                        const ui::OSExchangeData& data) OVERRIDE;
     71   virtual int GetDropOperation(views::MenuItemView* item,
     72                                const ui::DropTargetEvent& event,
     73                                DropPosition* position) OVERRIDE;
     74   virtual int OnPerformDrop(views::MenuItemView* menu,
     75                             DropPosition position,
     76                             const ui::DropTargetEvent& event) OVERRIDE;
     77   // These three drag functions offer support for dragging icons out of the
     78   // overflow menu.
     79   virtual bool CanDrag(views::MenuItemView* menu) OVERRIDE;
     80   virtual void WriteDragData(views::MenuItemView* sender,
     81                              ui::OSExchangeData* data) OVERRIDE;
     82   virtual int GetDragOperations(views::MenuItemView* sender) OVERRIDE;
     83 
     84  private:
     85   // This class manages its own lifetime.
     86   virtual ~BrowserActionOverflowMenuController();
     87 
     88   // Converts a menu item |id| into a BrowserActionView by adding the |id| value
     89   // to the number of visible views (according to the container owner). If
     90   // |index| is specified, it will point to the absolute index of the view.
     91   BrowserActionView* ViewForId(int id, size_t* index);
     92 
     93   // A pointer to the browser action container that owns the overflow menu.
     94   BrowserActionsContainer* owner_;
     95 
     96   Browser* browser_;
     97 
     98   // The observer, may be null.
     99   Observer* observer_;
    100 
    101   // A pointer to the overflow menu button that we are showing the menu for.
    102   views::MenuButton* menu_button_;
    103 
    104   // The overflow menu for the menu button. Owned by |menu_runner_|.
    105   views::MenuItemView* menu_;
    106 
    107   // Resposible for running the menu.
    108   scoped_ptr<views::MenuRunner> menu_runner_;
    109 
    110   // The views vector of all the browser actions the container knows about. We
    111   // won't show all items, just the one starting at |start_index| and above.
    112   const std::vector<BrowserActionView*>* views_;
    113 
    114   // The index into the BrowserActionView vector, indicating where to start
    115   // picking browser actions to draw.
    116   int start_index_;
    117 
    118   // Whether this controller is being used for drop.
    119   bool for_drop_;
    120 
    121   // The vector keeps all icon updaters associated with menu item views in the
    122   // controller. The icon updater will update the menu item view's icon when
    123   // the browser action view's icon has been updated.
    124   ScopedVector<IconUpdater> icon_updaters_;
    125 
    126   friend class base::DeleteHelper<BrowserActionOverflowMenuController>;
    127 
    128   DISALLOW_COPY_AND_ASSIGN(BrowserActionOverflowMenuController);
    129 };
    130 
    131 #endif  // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_BROWSER_ACTION_OVERFLOW_MENU_CONTROLLER_H_
    132