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