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