1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_
#define UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_
#include <map>
#include "base/callback.h"
#include "base/macros.h"
#include "ui/views/controls/menu/menu_delegate.h"
namespace ui {
class MenuModel;
}
namespace views {
class MenuItemView;
// This class wraps an instance of ui::MenuModel with the
// views::MenuDelegate interface required by views::MenuItemView.
class VIEWS_EXPORT MenuModelAdapter : public MenuDelegate {
public:
// The caller retains ownership of the ui::MenuModel instance and
// must ensure it exists for the lifetime of the adapter.
explicit MenuModelAdapter(ui::MenuModel* menu_model);
MenuModelAdapter(ui::MenuModel* menu_model,
const base::Closure& on_menu_closed_callback);
~MenuModelAdapter() override;
// Populate a MenuItemView menu with the ui::MenuModel items
// (including submenus).
virtual void BuildMenu(MenuItemView* menu);
// Convenience for creating and populating a menu. The caller owns the
// returned MenuItemView.
MenuItemView* CreateMenu();
void set_triggerable_event_flags(int triggerable_event_flags) {
triggerable_event_flags_ = triggerable_event_flags;
}
int triggerable_event_flags() const { return triggerable_event_flags_; }
// Creates a menu item for the specified entry in the model and adds it as
// a child to |menu| at the specified |menu_index|.
static MenuItemView* AddMenuItemFromModelAt(ui::MenuModel* model,
int model_index,
MenuItemView* menu,
int menu_index,
int item_id);
// Creates a menu item for the specified entry in the model and appends it as
// a child to |menu|.
static MenuItemView* AppendMenuItemFromModel(ui::MenuModel* model,
int model_index,
MenuItemView* menu,
int item_id);
protected:
// Create and add a menu item to |menu| for the item at index |index| in
// |model|. Subclasses override this to allow custom items to be added to the
// menu.
virtual MenuItemView* AppendMenuItem(MenuItemView* menu,
ui::MenuModel* model,
int index);
// views::MenuDelegate implementation.
void ExecuteCommand(int id) override;
void ExecuteCommand(int id, int mouse_event_flags) override;
bool IsTriggerableEvent(MenuItemView* source, const ui::Event& e) override;
bool GetAccelerator(int id, ui::Accelerator* accelerator) const override;
base::string16 GetLabel(int id) const override;
const gfx::FontList* GetLabelFontList(int id) const override;
bool IsCommandEnabled(int id) const override;
bool IsCommandVisible(int id) const override;
bool IsItemChecked(int id) const override;
void SelectionChanged(MenuItemView* menu) override;
void WillShowMenu(MenuItemView* menu) override;
void WillHideMenu(MenuItemView* menu) override;
void OnMenuClosed(MenuItemView* menu, MenuRunner::RunResult result) override;
private:
// Implementation of BuildMenu().
void BuildMenuImpl(MenuItemView* menu, ui::MenuModel* model);
// Container of ui::MenuModel pointers as encountered by preorder
// traversal. The first element is always the top-level model
// passed to the constructor.
ui::MenuModel* menu_model_;
// Mouse event flags which can trigger menu actions.
int triggerable_event_flags_;
// Map MenuItems to MenuModels. Used to implement WillShowMenu().
std::map<MenuItemView*, ui::MenuModel*> menu_map_;
// Optional callback triggered during OnMenuClosed().
base::Closure on_menu_closed_callback_;
DISALLOW_COPY_AND_ASSIGN(MenuModelAdapter);
};
} // namespace views
#endif // UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_
|