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.
5 #include "ui/views/controls/menu/menu_model_adapter.h"
7 #include "base/logging.h"
8 #include "ui/base/models/menu_model.h"
9 #include "ui/gfx/image/image.h"
10 #include "ui/views/controls/menu/menu_item_view.h"
11 #include "ui/views/controls/menu/submenu_view.h"
15 MenuModelAdapter::MenuModelAdapter(ui::MenuModel
* menu_model
)
16 : menu_model_(menu_model
),
17 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON
|
18 ui::EF_RIGHT_MOUSE_BUTTON
) {
22 MenuModelAdapter::~MenuModelAdapter() {
25 void MenuModelAdapter::BuildMenu(MenuItemView
* menu
) {
29 if (menu
->HasSubmenu()) {
30 const int subitem_count
= menu
->GetSubmenu()->child_count();
31 for (int i
= 0; i
< subitem_count
; ++i
)
32 menu
->RemoveMenuItemAt(0);
35 // Leave entries in the map if the menu is being shown. This
36 // allows the map to find the menu model of submenus being closed
37 // so ui::MenuModel::MenuClosed() can be called.
38 if (!menu
->GetMenuController())
40 menu_map_
[menu
] = menu_model_
;
42 // Repopulate the menu.
43 BuildMenuImpl(menu
, menu_model_
);
44 menu
->ChildrenChanged();
47 MenuItemView
* MenuModelAdapter::CreateMenu() {
48 MenuItemView
* item
= new MenuItemView(this);
54 MenuItemView
* MenuModelAdapter::AddMenuItemFromModelAt(ui::MenuModel
* model
,
60 model
->GetIconAt(model_index
, &icon
);
61 base::string16 label
, sublabel
, minor_text
;
62 ui::MenuSeparatorType separator_style
= ui::NORMAL_SEPARATOR
;
63 MenuItemView::Type type
;
64 ui::MenuModel::ItemType menu_type
= model
->GetTypeAt(model_index
);
67 case ui::MenuModel::TYPE_COMMAND
:
68 case ui::MenuModel::TYPE_BUTTON_ITEM
:
69 type
= MenuItemView::NORMAL
;
70 label
= model
->GetLabelAt(model_index
);
71 sublabel
= model
->GetSublabelAt(model_index
);
72 minor_text
= model
->GetMinorTextAt(model_index
);
74 case ui::MenuModel::TYPE_CHECK
:
75 type
= MenuItemView::CHECKBOX
;
76 label
= model
->GetLabelAt(model_index
);
77 sublabel
= model
->GetSublabelAt(model_index
);
78 minor_text
= model
->GetMinorTextAt(model_index
);
80 case ui::MenuModel::TYPE_RADIO
:
81 type
= MenuItemView::RADIO
;
82 label
= model
->GetLabelAt(model_index
);
83 sublabel
= model
->GetSublabelAt(model_index
);
84 minor_text
= model
->GetMinorTextAt(model_index
);
86 case ui::MenuModel::TYPE_SEPARATOR
:
88 type
= MenuItemView::SEPARATOR
;
89 separator_style
= model
->GetSeparatorTypeAt(model_index
);
91 case ui::MenuModel::TYPE_SUBMENU
:
92 type
= MenuItemView::SUBMENU
;
93 label
= model
->GetLabelAt(model_index
);
94 sublabel
= model
->GetSublabelAt(model_index
);
95 minor_text
= model
->GetMinorTextAt(model_index
);
99 type
= MenuItemView::NORMAL
;
103 return menu
->AddMenuItemAt(
109 icon
.IsEmpty() ? gfx::ImageSkia() : *icon
.ToImageSkia(),
115 MenuItemView
* MenuModelAdapter::AppendMenuItemFromModel(ui::MenuModel
* model
,
119 const int menu_index
= menu
->HasSubmenu() ?
120 menu
->GetSubmenu()->child_count() : 0;
121 return AddMenuItemFromModelAt(model
, model_index
, menu
, menu_index
, item_id
);
125 MenuItemView
* MenuModelAdapter::AppendMenuItem(MenuItemView
* menu
,
126 ui::MenuModel
* model
,
128 return AppendMenuItemFromModel(model
, index
, menu
,
129 model
->GetCommandIdAt(index
));
132 // MenuModelAdapter, MenuDelegate implementation:
134 void MenuModelAdapter::ExecuteCommand(int id
) {
135 ui::MenuModel
* model
= menu_model_
;
137 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
)) {
138 model
->ActivatedAt(index
);
145 void MenuModelAdapter::ExecuteCommand(int id
, int mouse_event_flags
) {
146 ui::MenuModel
* model
= menu_model_
;
148 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
)) {
149 model
->ActivatedAt(index
, mouse_event_flags
);
156 bool MenuModelAdapter::IsTriggerableEvent(MenuItemView
* source
,
157 const ui::Event
& e
) {
158 return e
.type() == ui::ET_GESTURE_TAP
||
159 e
.type() == ui::ET_GESTURE_TAP_DOWN
||
160 (e
.IsMouseEvent() && (triggerable_event_flags_
& e
.flags()) != 0);
163 bool MenuModelAdapter::GetAccelerator(int id
,
164 ui::Accelerator
* accelerator
) const {
165 ui::MenuModel
* model
= menu_model_
;
167 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
))
168 return model
->GetAcceleratorAt(index
, accelerator
);
174 base::string16
MenuModelAdapter::GetLabel(int id
) const {
175 ui::MenuModel
* model
= menu_model_
;
177 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
))
178 return model
->GetLabelAt(index
);
181 return base::string16();
184 const gfx::FontList
* MenuModelAdapter::GetLabelFontList(int id
) const {
185 ui::MenuModel
* model
= menu_model_
;
187 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
)) {
188 const gfx::FontList
* font_list
= model
->GetLabelFontListAt(index
);
193 // This line may be reached for the empty menu item.
194 return MenuDelegate::GetLabelFontList(id
);
197 bool MenuModelAdapter::IsCommandEnabled(int id
) const {
198 ui::MenuModel
* model
= menu_model_
;
200 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
))
201 return model
->IsEnabledAt(index
);
207 bool MenuModelAdapter::IsCommandVisible(int id
) const {
208 ui::MenuModel
* model
= menu_model_
;
210 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
))
211 return model
->IsVisibleAt(index
);
217 bool MenuModelAdapter::IsItemChecked(int id
) const {
218 ui::MenuModel
* model
= menu_model_
;
220 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
))
221 return model
->IsItemCheckedAt(index
);
227 void MenuModelAdapter::SelectionChanged(MenuItemView
* menu
) {
228 // Ignore selection of the root menu.
229 if (menu
== menu
->GetRootMenuItem())
232 const int id
= menu
->GetCommand();
233 ui::MenuModel
* model
= menu_model_
;
235 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
)) {
236 model
->HighlightChangedTo(index
);
243 void MenuModelAdapter::WillShowMenu(MenuItemView
* menu
) {
244 // Look up the menu model for this menu.
245 const std::map
<MenuItemView
*, ui::MenuModel
*>::const_iterator map_iterator
=
246 menu_map_
.find(menu
);
247 if (map_iterator
!= menu_map_
.end()) {
248 map_iterator
->second
->MenuWillShow();
255 void MenuModelAdapter::WillHideMenu(MenuItemView
* menu
) {
256 // Look up the menu model for this menu.
257 const std::map
<MenuItemView
*, ui::MenuModel
*>::const_iterator map_iterator
=
258 menu_map_
.find(menu
);
259 if (map_iterator
!= menu_map_
.end()) {
260 map_iterator
->second
->MenuClosed();
267 // MenuModelAdapter, private:
269 void MenuModelAdapter::BuildMenuImpl(MenuItemView
* menu
, ui::MenuModel
* model
) {
272 bool has_icons
= model
->HasIcons();
273 const int item_count
= model
->GetItemCount();
274 for (int i
= 0; i
< item_count
; ++i
) {
275 MenuItemView
* item
= AppendMenuItem(menu
, model
, i
);
277 if (model
->GetTypeAt(i
) == ui::MenuModel::TYPE_SUBMENU
) {
279 DCHECK_EQ(MenuItemView::SUBMENU
, item
->GetType());
280 ui::MenuModel
* submodel
= model
->GetSubmenuModelAt(i
);
282 BuildMenuImpl(item
, submodel
);
283 has_icons
= has_icons
|| item
->has_icons();
285 menu_map_
[item
] = submodel
;
289 menu
->set_has_icons(has_icons
);