| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/menu/menu_model_adapter.h" | 5 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ui/base/l10n/l10n_util.h" | 8 #include "ui/base/l10n/l10n_util.h" |
| 9 #include "ui/base/models/menu_model.h" | 9 #include "ui/base/models/menu_model.h" |
| 10 #include "ui/gfx/image/image.h" |
| 10 #include "ui/views/controls/menu/submenu_view.h" | 11 #include "ui/views/controls/menu/submenu_view.h" |
| 11 #include "ui/views/views_delegate.h" | 12 #include "ui/views/views_delegate.h" |
| 12 | 13 |
| 13 namespace views { | 14 namespace views { |
| 14 | 15 |
| 15 MenuModelAdapter::MenuModelAdapter(ui::MenuModel* menu_model) | 16 MenuModelAdapter::MenuModelAdapter(ui::MenuModel* menu_model) |
| 16 : menu_model_(menu_model), | 17 : menu_model_(menu_model), |
| 17 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON | | 18 triggerable_event_flags_(ui::EF_LEFT_MOUSE_BUTTON | |
| 18 ui::EF_RIGHT_MOUSE_BUTTON) { | 19 ui::EF_RIGHT_MOUSE_BUTTON) { |
| 19 DCHECK(menu_model); | 20 DCHECK(menu_model); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 43 BuildMenuImpl(menu, menu_model_); | 44 BuildMenuImpl(menu, menu_model_); |
| 44 menu->ChildrenChanged(); | 45 menu->ChildrenChanged(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 MenuItemView* MenuModelAdapter::CreateMenu() { | 48 MenuItemView* MenuModelAdapter::CreateMenu() { |
| 48 MenuItemView* item = new MenuItemView(this); | 49 MenuItemView* item = new MenuItemView(this); |
| 49 BuildMenu(item); | 50 BuildMenu(item); |
| 50 return item; | 51 return item; |
| 51 } | 52 } |
| 52 | 53 |
| 54 // Static. |
| 55 MenuItemView* MenuModelAdapter::AddMenuItemFromModelAt(ui::MenuModel* model, |
| 56 int model_index, |
| 57 MenuItemView* menu, |
| 58 int menu_index, |
| 59 int item_id) { |
| 60 gfx::Image icon; |
| 61 model->GetIconAt(model_index, &icon); |
| 62 string16 label, sublabel, minor_text; |
| 63 ui::MenuSeparatorType separator_style = ui::NORMAL_SEPARATOR; |
| 64 MenuItemView::Type type; |
| 65 ui::MenuModel::ItemType menu_type = model->GetTypeAt(model_index); |
| 66 |
| 67 switch (menu_type) { |
| 68 case ui::MenuModel::TYPE_COMMAND: |
| 69 type = MenuItemView::NORMAL; |
| 70 label = model->GetLabelAt(model_index); |
| 71 sublabel = model->GetSublabelAt(model_index); |
| 72 minor_text = model->GetMinorTextAt(model_index); |
| 73 break; |
| 74 case ui::MenuModel::TYPE_CHECK: |
| 75 type = MenuItemView::CHECKBOX; |
| 76 label = model->GetLabelAt(model_index); |
| 77 sublabel = model->GetSublabelAt(model_index); |
| 78 minor_text = model->GetMinorTextAt(model_index); |
| 79 break; |
| 80 case ui::MenuModel::TYPE_RADIO: |
| 81 type = MenuItemView::RADIO; |
| 82 label = model->GetLabelAt(model_index); |
| 83 sublabel = model->GetSublabelAt(model_index); |
| 84 minor_text = model->GetMinorTextAt(model_index); |
| 85 break; |
| 86 case ui::MenuModel::TYPE_SEPARATOR: |
| 87 icon = gfx::Image(); |
| 88 type = MenuItemView::SEPARATOR; |
| 89 separator_style = model->GetSeparatorTypeAt(model_index); |
| 90 break; |
| 91 case ui::MenuModel::TYPE_SUBMENU: |
| 92 type = MenuItemView::SUBMENU; |
| 93 label = model->GetLabelAt(model_index); |
| 94 sublabel = model->GetSublabelAt(model_index); |
| 95 minor_text = model->GetMinorTextAt(model_index); |
| 96 break; |
| 97 default: |
| 98 NOTREACHED(); |
| 99 type = MenuItemView::NORMAL; |
| 100 break; |
| 101 } |
| 102 |
| 103 return menu->AddMenuItemAt( |
| 104 menu_index, |
| 105 item_id, |
| 106 label, |
| 107 sublabel, |
| 108 minor_text, |
| 109 icon.IsEmpty() ? gfx::ImageSkia() : *icon.ToImageSkia(), |
| 110 type, |
| 111 separator_style); |
| 112 } |
| 113 |
| 114 // Static. |
| 115 MenuItemView* MenuModelAdapter::AppendMenuItemFromModel(ui::MenuModel* model, |
| 116 int model_index, |
| 117 MenuItemView* menu, |
| 118 int item_id) { |
| 119 const int menu_index = menu->HasSubmenu() ? |
| 120 menu->GetSubmenu()->child_count() : 0; |
| 121 return AddMenuItemFromModelAt(model, model_index, menu, menu_index, item_id); |
| 122 } |
| 123 |
| 124 |
| 53 MenuItemView* MenuModelAdapter::AppendMenuItem(MenuItemView* menu, | 125 MenuItemView* MenuModelAdapter::AppendMenuItem(MenuItemView* menu, |
| 54 ui::MenuModel* model, | 126 ui::MenuModel* model, |
| 55 int index) { | 127 int index) { |
| 56 return menu->AppendMenuItemFromModel(model, index, | 128 return AppendMenuItemFromModel(model, index, menu, |
| 57 model->GetCommandIdAt(index)); | 129 model->GetCommandIdAt(index)); |
| 58 } | 130 } |
| 59 | 131 |
| 60 // MenuModelAdapter, MenuDelegate implementation: | 132 // MenuModelAdapter, MenuDelegate implementation: |
| 61 | 133 |
| 62 void MenuModelAdapter::ExecuteCommand(int id) { | 134 void MenuModelAdapter::ExecuteCommand(int id) { |
| 63 ui::MenuModel* model = menu_model_; | 135 ui::MenuModel* model = menu_model_; |
| 64 int index = 0; | 136 int index = 0; |
| 65 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) { | 137 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) { |
| 66 model->ActivatedAt(index); | 138 model->ActivatedAt(index); |
| 67 return; | 139 return; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 has_icons = has_icons || item->has_icons(); | 276 has_icons = has_icons || item->has_icons(); |
| 205 | 277 |
| 206 menu_map_[item] = submodel; | 278 menu_map_[item] = submodel; |
| 207 } | 279 } |
| 208 } | 280 } |
| 209 | 281 |
| 210 menu->set_has_icons(has_icons); | 282 menu->set_has_icons(has_icons); |
| 211 } | 283 } |
| 212 | 284 |
| 213 } // namespace views | 285 } // namespace views |
| OLD | NEW |