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/l10n/l10n_util.h"
9 #include "ui/base/models/menu_model.h"
10 #include "ui/gfx/image/image.h"
11 #include "ui/views/controls/menu/submenu_view.h"
12 #include "ui/views/views_delegate.h"
16 MenuModelAdapter::MenuModelAdapter(ui::MenuModel
* menu_model
)
17 : menu_model_(menu_model
),
18 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON
|
19 ui::EF_RIGHT_MOUSE_BUTTON
) {
23 MenuModelAdapter::~MenuModelAdapter() {
26 void MenuModelAdapter::BuildMenu(MenuItemView
* menu
) {
30 if (menu
->HasSubmenu()) {
31 const int subitem_count
= menu
->GetSubmenu()->child_count();
32 for (int i
= 0; i
< subitem_count
; ++i
)
33 menu
->RemoveMenuItemAt(0);
36 // Leave entries in the map if the menu is being shown. This
37 // allows the map to find the menu model of submenus being closed
38 // so ui::MenuModel::MenuClosed() can be called.
39 if (!menu
->GetMenuController())
41 menu_map_
[menu
] = menu_model_
;
43 // Repopulate the menu.
44 BuildMenuImpl(menu
, menu_model_
);
45 menu
->ChildrenChanged();
48 MenuItemView
* MenuModelAdapter::CreateMenu() {
49 MenuItemView
* item
= new MenuItemView(this);
55 MenuItemView
* MenuModelAdapter::AddMenuItemFromModelAt(ui::MenuModel
* model
,
61 model
->GetIconAt(model_index
, &icon
);
62 base::string16 label
, sublabel
, minor_text
;
63 ui::MenuSeparatorType separator_style
= ui::NORMAL_SEPARATOR
;
64 MenuItemView::Type type
;
65 ui::MenuModel::ItemType menu_type
= model
->GetTypeAt(model_index
);
68 case ui::MenuModel::TYPE_COMMAND
:
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
) {
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::IsItemChecked(int id
) const {
208 ui::MenuModel
* model
= menu_model_
;
210 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
))
211 return model
->IsItemCheckedAt(index
);
217 void MenuModelAdapter::SelectionChanged(MenuItemView
* menu
) {
218 // Ignore selection of the root menu.
219 if (menu
== menu
->GetRootMenuItem())
222 const int id
= menu
->GetCommand();
223 ui::MenuModel
* model
= menu_model_
;
225 if (ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
)) {
226 model
->HighlightChangedTo(index
);
233 void MenuModelAdapter::WillShowMenu(MenuItemView
* menu
) {
234 // Look up the menu model for this menu.
235 const std::map
<MenuItemView
*, ui::MenuModel
*>::const_iterator map_iterator
=
236 menu_map_
.find(menu
);
237 if (map_iterator
!= menu_map_
.end()) {
238 map_iterator
->second
->MenuWillShow();
245 void MenuModelAdapter::WillHideMenu(MenuItemView
* menu
) {
246 // Look up the menu model for this menu.
247 const std::map
<MenuItemView
*, ui::MenuModel
*>::const_iterator map_iterator
=
248 menu_map_
.find(menu
);
249 if (map_iterator
!= menu_map_
.end()) {
250 map_iterator
->second
->MenuClosed();
257 // MenuModelAdapter, private:
259 void MenuModelAdapter::BuildMenuImpl(MenuItemView
* menu
, ui::MenuModel
* model
) {
262 bool has_icons
= model
->HasIcons();
263 const int item_count
= model
->GetItemCount();
264 for (int i
= 0; i
< item_count
; ++i
) {
265 MenuItemView
* item
= AppendMenuItem(menu
, model
, i
);
268 item
->SetVisible(model
->IsVisibleAt(i
));
270 if (model
->GetTypeAt(i
) == ui::MenuModel::TYPE_SUBMENU
) {
272 DCHECK_EQ(MenuItemView::SUBMENU
, item
->GetType());
273 ui::MenuModel
* submodel
= model
->GetSubmenuModelAt(i
);
275 BuildMenuImpl(item
, submodel
);
276 has_icons
= has_icons
|| item
->has_icons();
278 menu_map_
[item
] = submodel
;
282 menu
->set_has_icons(has_icons
);