Home | History | Annotate | Download | only in menu
      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 UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_
      6 #define UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_
      7 
      8 #include <map>
      9 
     10 #include "ui/views/controls/menu/menu_delegate.h"
     11 
     12 namespace ui {
     13 class MenuModel;
     14 }
     15 
     16 namespace views {
     17 class MenuItemView;
     18 
     19 // This class wraps an instance of ui::MenuModel with the
     20 // views::MenuDelegate interface required by views::MenuItemView.
     21 class VIEWS_EXPORT MenuModelAdapter : public MenuDelegate {
     22  public:
     23   // The caller retains ownership of the ui::MenuModel instance and
     24   // must ensure it exists for the lifetime of the adapter.
     25   explicit MenuModelAdapter(ui::MenuModel* menu_model);
     26   virtual ~MenuModelAdapter();
     27 
     28   // Populate a MenuItemView menu with the ui::MenuModel items
     29   // (including submenus).
     30   virtual void BuildMenu(MenuItemView* menu);
     31 
     32   // Convenience for creating and populating a menu. The caller owns the
     33   // returned MenuItemView.
     34   MenuItemView* CreateMenu();
     35 
     36   void set_triggerable_event_flags(int triggerable_event_flags) {
     37     triggerable_event_flags_ = triggerable_event_flags;
     38   }
     39   int triggerable_event_flags() const { return triggerable_event_flags_; }
     40 
     41  protected:
     42   // Create and add a menu item to |menu| for the item at index |index| in
     43   // |model|. Subclasses override this to allow custom items to be added to the
     44   // menu.
     45   virtual MenuItemView* AppendMenuItem(MenuItemView* menu,
     46                                        ui::MenuModel* model,
     47                                        int index);
     48 
     49   // views::MenuDelegate implementation.
     50   virtual void ExecuteCommand(int id) OVERRIDE;
     51   virtual void ExecuteCommand(int id, int mouse_event_flags) OVERRIDE;
     52   virtual bool IsTriggerableEvent(MenuItemView* source,
     53                                   const ui::Event& e) OVERRIDE;
     54   virtual bool GetAccelerator(int id,
     55                               ui::Accelerator* accelerator) OVERRIDE;
     56   virtual string16 GetLabel(int id) const OVERRIDE;
     57   virtual const gfx::Font* GetLabelFont(int id) const OVERRIDE;
     58   virtual bool IsCommandEnabled(int id) const OVERRIDE;
     59   virtual bool IsItemChecked(int id) const OVERRIDE;
     60   virtual void SelectionChanged(MenuItemView* menu) OVERRIDE;
     61   virtual void WillShowMenu(MenuItemView* menu) OVERRIDE;
     62   virtual void WillHideMenu(MenuItemView* menu) OVERRIDE;
     63 
     64  private:
     65   // Implementation of BuildMenu().
     66   void BuildMenuImpl(MenuItemView* menu, ui::MenuModel* model);
     67 
     68   // Container of ui::MenuModel pointers as encountered by preorder
     69   // traversal.  The first element is always the top-level model
     70   // passed to the constructor.
     71   ui::MenuModel* menu_model_;
     72 
     73   // Mouse event flags which can trigger menu actions.
     74   int triggerable_event_flags_;
     75 
     76   // Map MenuItems to MenuModels.  Used to implement WillShowMenu().
     77   std::map<MenuItemView*, ui::MenuModel*> menu_map_;
     78 
     79   DISALLOW_COPY_AND_ASSIGN(MenuModelAdapter);
     80 };
     81 
     82 }  // namespace views
     83 
     84 #endif  // UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_
     85