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