OLD | NEW |
| (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 "ui/views/controls/menu/native_menu_views.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "ui/base/models/menu_model.h" | |
10 #include "ui/gfx/canvas_skia.h" | |
11 #include "ui/gfx/skia_util.h" | |
12 #include "ui/views/controls/menu/menu_2.h" | |
13 #include "ui/views/controls/menu/menu_item_view.h" | |
14 #include "ui/views/controls/menu/menu_listener.h" | |
15 #include "ui/views/controls/menu/menu_runner.h" | |
16 #include "ui/views/controls/menu/submenu_view.h" | |
17 | |
18 namespace views { | |
19 | |
20 NativeMenuViews::NativeMenuViews(Menu2* menu) | |
21 : model_(menu->model()), | |
22 ALLOW_THIS_IN_INITIALIZER_LIST(root_(new MenuItemView(this))), | |
23 menu_runner_(new MenuRunner(root_)) { | |
24 } | |
25 | |
26 NativeMenuViews::~NativeMenuViews() { | |
27 } | |
28 | |
29 // MenuWrapper implementation: | |
30 void NativeMenuViews::RunMenuAt(const gfx::Point& point, int alignment) { | |
31 // TODO: this should really return the value from MenuRunner. | |
32 UpdateStates(); | |
33 if (menu_runner_->RunMenuAt(NULL, NULL, gfx::Rect(point, gfx::Size()), | |
34 alignment == Menu2::ALIGN_TOPLEFT ? MenuItemView::TOPLEFT : | |
35 MenuItemView::TOPRIGHT, MenuRunner::HAS_MNEMONICS) == | |
36 MenuRunner::MENU_DELETED) | |
37 return; | |
38 } | |
39 | |
40 void NativeMenuViews::CancelMenu() { | |
41 NOTIMPLEMENTED(); | |
42 } | |
43 | |
44 void NativeMenuViews::Rebuild() { | |
45 if (SubmenuView* submenu = root_->GetSubmenu()) | |
46 submenu->RemoveAllChildViews(true); | |
47 AddMenuItemsFromModel(root_, model_); | |
48 } | |
49 | |
50 void NativeMenuViews::UpdateStates() { | |
51 SubmenuView* submenu = root_->CreateSubmenu(); | |
52 UpdateMenuFromModel(submenu, model_); | |
53 } | |
54 | |
55 gfx::NativeMenu NativeMenuViews::GetNativeMenu() const { | |
56 NOTIMPLEMENTED(); | |
57 return NULL; | |
58 } | |
59 | |
60 MenuWrapper::MenuAction NativeMenuViews::GetMenuAction() const { | |
61 NOTIMPLEMENTED(); | |
62 return MENU_ACTION_NONE; | |
63 } | |
64 | |
65 void NativeMenuViews::AddMenuListener(MenuListener* listener) { | |
66 NOTIMPLEMENTED(); | |
67 } | |
68 | |
69 void NativeMenuViews::RemoveMenuListener(MenuListener* listener) { | |
70 NOTIMPLEMENTED(); | |
71 } | |
72 | |
73 void NativeMenuViews::SetMinimumWidth(int width) { | |
74 NOTIMPLEMENTED(); | |
75 } | |
76 | |
77 // MenuDelegate implementation | |
78 | |
79 bool NativeMenuViews::IsItemChecked(int cmd) const { | |
80 int index; | |
81 ui::MenuModel* model = model_; | |
82 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) | |
83 return false; | |
84 return model->IsItemCheckedAt(index); | |
85 } | |
86 | |
87 bool NativeMenuViews::IsCommandEnabled(int cmd) const { | |
88 int index; | |
89 ui::MenuModel* model = model_; | |
90 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) | |
91 return false; | |
92 return model->IsEnabledAt(index); | |
93 } | |
94 | |
95 void NativeMenuViews::ExecuteCommand(int cmd) { | |
96 int index; | |
97 ui::MenuModel* model = model_; | |
98 if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index)) | |
99 return; | |
100 model->ActivatedAt(index); | |
101 } | |
102 | |
103 bool NativeMenuViews::GetAccelerator(int id, ui::Accelerator* accelerator) { | |
104 int index; | |
105 ui::MenuModel* model = model_; | |
106 if (!ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) | |
107 return false; | |
108 | |
109 ui::Accelerator menu_accelerator; | |
110 if (!model->GetAcceleratorAt(index, &menu_accelerator)) | |
111 return false; | |
112 | |
113 *accelerator = ui::Accelerator(menu_accelerator.key_code(), | |
114 menu_accelerator.modifiers()); | |
115 return true; | |
116 } | |
117 | |
118 // private | |
119 void NativeMenuViews::AddMenuItemsFromModel(MenuItemView* parent, | |
120 ui::MenuModel* model) { | |
121 for (int i = 0; i < model->GetItemCount(); ++i) { | |
122 int index = i + model->GetFirstItemIndex(NULL); | |
123 MenuItemView* child = parent->AppendMenuItemFromModel(model, index, | |
124 model->GetCommandIdAt(index)); | |
125 | |
126 if (child && child->GetType() == MenuItemView::SUBMENU) { | |
127 AddMenuItemsFromModel(child, model->GetSubmenuModelAt(index)); | |
128 } | |
129 } | |
130 } | |
131 | |
132 void NativeMenuViews::UpdateMenuFromModel(SubmenuView* menu, | |
133 ui::MenuModel* model) { | |
134 for (int i = 0, sep = 0; i < model->GetItemCount(); ++i) { | |
135 int index = i + model->GetFirstItemIndex(NULL); | |
136 if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SEPARATOR) { | |
137 ++sep; | |
138 continue; | |
139 } | |
140 | |
141 // The submenu excludes the separators when counting the menu-items | |
142 // in it. So exclude the number of separators to get the correct index. | |
143 MenuItemView* mitem = menu->GetMenuItemAt(index - sep); | |
144 mitem->SetVisible(model->IsVisibleAt(index)); | |
145 mitem->SetEnabled(model->IsEnabledAt(index)); | |
146 if (model->IsItemDynamicAt(index)) { | |
147 mitem->SetTitle(model->GetLabelAt(index)); | |
148 } | |
149 | |
150 SkBitmap icon; | |
151 if (model->GetIconAt(index, &icon)) { | |
152 // TODO(atwilson): Support removing the icon dynamically | |
153 // (http://crbug.com/66508). | |
154 mitem->SetIcon(icon); | |
155 } | |
156 | |
157 if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SUBMENU) { | |
158 DCHECK(mitem->HasSubmenu()); | |
159 UpdateMenuFromModel(mitem->GetSubmenu(), model->GetSubmenuModelAt(index)); | |
160 } | |
161 } | |
162 } | |
163 | |
164 // MenuWrapper, public: | |
165 | |
166 // static | |
167 MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) { | |
168 return new NativeMenuViews(menu); | |
169 } | |
170 | |
171 } // namespace views | |
OLD | NEW |