Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: views/controls/menu/menu_model_adapter.cc

Issue 7067032: Add MenuModelAdapter to wrap ui::MenuModel with views::MenuDelegate interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Implemented reviewer recommendations. Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #include "views/controls/menu/menu_model_adapter.h"
6
7 #include "base/logging.h"
8 #include "ui/base/l10n/l10n_util.h"
9 #include "ui/base/models/menu_model.h"
10 #include "views/controls/menu/submenu_view.h"
11 #include "views/views_delegate.h"
12
13 namespace views {
14
15 MenuModelAdapter::MenuModelAdapter(ui::MenuModel* menu_model)
16 : menu_model_(menu_model) {
17 DCHECK(menu_model);
18 }
19
20 void MenuModelAdapter::BuildMenu(MenuItemView* menu) {
21 DCHECK(menu);
22
23 // Clear the menu.
24 if (menu->HasSubmenu()) {
25 const int subitem_count = menu->GetSubmenu()->child_count();
26 for (int i = 0; i < subitem_count; ++i)
27 menu->RemoveMenuItemAt(0);
28 }
29
30 menu_map_.clear();
31 menu_map_[menu] = menu_model_;
32
33 // Repopulate the menu.
34 BuildMenuImpl(menu, menu_model_);
35 menu->ChildrenChanged();
36 }
37
38 // MenuModelAdapter, MenuDelegate implementation:
39
40 void MenuModelAdapter::ExecuteCommand(int id) {
41 ui::MenuModel* model = menu_model_;
42 int index = 0;
43 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) {
44 model->ActivatedAt(index);
45 return;
46 }
47
48 NOTREACHED();
49 }
50
51 void MenuModelAdapter::ExecuteCommand(int id, int mouse_event_flags) {
52 ui::MenuModel* model = menu_model_;
53 int index = 0;
54 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) {
55 const int disposition =
56 ViewsDelegate::views_delegate->GetDispositionForEvent(
57 mouse_event_flags);
58 model->ActivatedAtWithDisposition(index, disposition);
59 return;
60 }
61
62 NOTREACHED();
63 }
64
65 bool MenuModelAdapter::GetAccelerator(int id,
66 views::Accelerator* accelerator) {
67 ui::MenuModel* model = menu_model_;
68 int index = 0;
69 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
70 return model->GetAcceleratorAt(index, accelerator);
71
72 NOTREACHED();
73 return false;
74 }
75
76 std::wstring MenuModelAdapter::GetLabel(int id) const {
77 ui::MenuModel* model = menu_model_;
78 int index = 0;
79 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
80 return UTF16ToWide(model->GetLabelAt(index));
81
82 NOTREACHED();
83 return std::wstring();
84 }
85
86 const gfx::Font& MenuModelAdapter::GetLabelFont(int id) const {
87 ui::MenuModel* model = menu_model_;
88 int index = 0;
89 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) {
90 const gfx::Font* font = model->GetLabelFontAt(index);
91 return font ? *font : MenuDelegate::GetLabelFont(id);
92 }
93
94 NOTREACHED();
95 return MenuDelegate::GetLabelFont(id);
96 }
97
98 bool MenuModelAdapter::IsCommandEnabled(int id) const {
99 ui::MenuModel* model = menu_model_;
100 int index = 0;
101 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
102 return model->IsEnabledAt(index);
103
104 NOTREACHED();
105 return false;
106 }
107
108 bool MenuModelAdapter::IsItemChecked(int id) const {
109 ui::MenuModel* model = menu_model_;
110 int index = 0;
111 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
112 return model->IsItemCheckedAt(index);
113
114 NOTREACHED();
115 return false;
116 }
117
118 void MenuModelAdapter::SelectionChanged(MenuItemView* menu) {
119 const int id = menu->GetCommand();
120 ui::MenuModel* model = menu_model_;
121 int index = 0;
122 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) {
123 model->HighlightChangedTo(index);
124 return;
125 }
126
127 NOTREACHED();
128 }
129
130 void MenuModelAdapter::WillShowMenu(MenuItemView* menu) {
131 // Look up the menu model for this menu.
132 const std::map<MenuItemView*, ui::MenuModel*>::const_iterator map_iterator =
133 menu_map_.find(menu);
134 if (map_iterator != menu_map_.end()) {
135 map_iterator->second->MenuWillShow();
136 return;
137 }
138
139 NOTREACHED();
140 }
141
142 // MenuModelAdapter, private:
143
144 void MenuModelAdapter::BuildMenuImpl(MenuItemView* menu, ui::MenuModel* model) {
145 DCHECK(menu);
146 DCHECK(model);
147 const int item_count = model->GetItemCount();
148 for (int i = 0; i < item_count; ++i) {
149 const int index = i + model->GetFirstItemIndex(NULL);
150 MenuItemView* item = menu->AppendMenuItemFromModel(
151 model, index, model->GetCommandIdAt(index));
152
153 if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SUBMENU) {
154 DCHECK(item);
155 DCHECK_EQ(MenuItemView::SUBMENU, item->GetType());
156 ui::MenuModel* submodel = model->GetSubmenuModelAt(index);
157 DCHECK(submodel);
158 BuildMenuImpl(item, submodel);
159
160 menu_map_[item] = submodel;
161 }
162 }
163
164 menu->set_has_icons(model->HasIcons());
165 }
166
167 } // views
OLDNEW
« no previous file with comments | « views/controls/menu/menu_model_adapter.h ('k') | views/controls/menu/menu_model_adapter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698