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