| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "views/controls/menu/menu_model_adapter.h" | 5 #include "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 "views/controls/menu/submenu_view.h" | 10 #include "views/controls/menu/submenu_view.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 87 } |
| 88 | 88 |
| 89 const gfx::Font& MenuModelAdapter::GetLabelFont(int id) const { | 89 const gfx::Font& MenuModelAdapter::GetLabelFont(int id) const { |
| 90 ui::MenuModel* model = menu_model_; | 90 ui::MenuModel* model = menu_model_; |
| 91 int index = 0; | 91 int index = 0; |
| 92 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) { | 92 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) { |
| 93 const gfx::Font* font = model->GetLabelFontAt(index); | 93 const gfx::Font* font = model->GetLabelFontAt(index); |
| 94 return font ? *font : MenuDelegate::GetLabelFont(id); | 94 return font ? *font : MenuDelegate::GetLabelFont(id); |
| 95 } | 95 } |
| 96 | 96 |
| 97 NOTREACHED(); | 97 // This line may be reached for the empty menu item. |
| 98 return MenuDelegate::GetLabelFont(id); | 98 return MenuDelegate::GetLabelFont(id); |
| 99 } | 99 } |
| 100 | 100 |
| 101 bool MenuModelAdapter::IsCommandEnabled(int id) const { | 101 bool MenuModelAdapter::IsCommandEnabled(int id) const { |
| 102 ui::MenuModel* model = menu_model_; | 102 ui::MenuModel* model = menu_model_; |
| 103 int index = 0; | 103 int index = 0; |
| 104 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) | 104 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) |
| 105 return model->IsEnabledAt(index); | 105 return model->IsEnabledAt(index); |
| 106 | 106 |
| 107 NOTREACHED(); | 107 NOTREACHED(); |
| 108 return false; | 108 return false; |
| 109 } | 109 } |
| 110 | 110 |
| 111 bool MenuModelAdapter::IsItemChecked(int id) const { | 111 bool MenuModelAdapter::IsItemChecked(int id) const { |
| 112 ui::MenuModel* model = menu_model_; | 112 ui::MenuModel* model = menu_model_; |
| 113 int index = 0; | 113 int index = 0; |
| 114 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) | 114 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) |
| 115 return model->IsItemCheckedAt(index); | 115 return model->IsItemCheckedAt(index); |
| 116 | 116 |
| 117 NOTREACHED(); | 117 NOTREACHED(); |
| 118 return false; | 118 return false; |
| 119 } | 119 } |
| 120 | 120 |
| 121 void MenuModelAdapter::SelectionChanged(MenuItemView* menu) { | 121 void MenuModelAdapter::SelectionChanged(MenuItemView* menu) { |
| 122 // Ignore selection of the root menu. |
| 123 if (menu == menu->GetRootMenuItem()) |
| 124 return; |
| 125 |
| 122 const int id = menu->GetCommand(); | 126 const int id = menu->GetCommand(); |
| 123 ui::MenuModel* model = menu_model_; | 127 ui::MenuModel* model = menu_model_; |
| 124 int index = 0; | 128 int index = 0; |
| 125 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) { | 129 if (ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) { |
| 126 model->HighlightChangedTo(index); | 130 model->HighlightChangedTo(index); |
| 127 return; | 131 return; |
| 128 } | 132 } |
| 129 | 133 |
| 130 NOTREACHED(); | 134 NOTREACHED(); |
| 131 } | 135 } |
| 132 | 136 |
| 133 void MenuModelAdapter::WillShowMenu(MenuItemView* menu) { | 137 void MenuModelAdapter::WillShowMenu(MenuItemView* menu) { |
| 134 // Look up the menu model for this menu. | 138 // Look up the menu model for this menu. |
| 135 const std::map<MenuItemView*, ui::MenuModel*>::const_iterator map_iterator = | 139 const std::map<MenuItemView*, ui::MenuModel*>::const_iterator map_iterator = |
| 136 menu_map_.find(menu); | 140 menu_map_.find(menu); |
| 137 if (map_iterator != menu_map_.end()) { | 141 if (map_iterator != menu_map_.end()) { |
| 138 map_iterator->second->MenuWillShow(); | 142 map_iterator->second->MenuWillShow(); |
| 139 return; | 143 return; |
| 140 } | 144 } |
| 141 | 145 |
| 142 NOTREACHED(); | 146 NOTREACHED(); |
| 143 } | 147 } |
| 144 | 148 |
| 149 void MenuModelAdapter::WillHideMenu(MenuItemView* menu) { |
| 150 // Look up the menu model for this menu. |
| 151 const std::map<MenuItemView*, ui::MenuModel*>::const_iterator map_iterator = |
| 152 menu_map_.find(menu); |
| 153 if (map_iterator != menu_map_.end()) { |
| 154 map_iterator->second->MenuClosed(); |
| 155 return; |
| 156 } |
| 157 |
| 158 NOTREACHED(); |
| 159 } |
| 160 |
| 145 // MenuModelAdapter, private: | 161 // MenuModelAdapter, private: |
| 146 | 162 |
| 147 void MenuModelAdapter::BuildMenuImpl(MenuItemView* menu, ui::MenuModel* model) { | 163 void MenuModelAdapter::BuildMenuImpl(MenuItemView* menu, ui::MenuModel* model) { |
| 148 DCHECK(menu); | 164 DCHECK(menu); |
| 149 DCHECK(model); | 165 DCHECK(model); |
| 150 const int item_count = model->GetItemCount(); | 166 const int item_count = model->GetItemCount(); |
| 151 for (int i = 0; i < item_count; ++i) { | 167 for (int i = 0; i < item_count; ++i) { |
| 152 const int index = i + model->GetFirstItemIndex(NULL); | 168 const int index = i + model->GetFirstItemIndex(NULL); |
| 153 MenuItemView* item = menu->AppendMenuItemFromModel( | 169 MenuItemView* item = menu->AppendMenuItemFromModel( |
| 154 model, index, model->GetCommandIdAt(index)); | 170 model, index, model->GetCommandIdAt(index)); |
| 155 | 171 |
| 156 if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SUBMENU) { | 172 if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SUBMENU) { |
| 157 DCHECK(item); | 173 DCHECK(item); |
| 158 DCHECK_EQ(MenuItemView::SUBMENU, item->GetType()); | 174 DCHECK_EQ(MenuItemView::SUBMENU, item->GetType()); |
| 159 ui::MenuModel* submodel = model->GetSubmenuModelAt(index); | 175 ui::MenuModel* submodel = model->GetSubmenuModelAt(index); |
| 160 DCHECK(submodel); | 176 DCHECK(submodel); |
| 161 BuildMenuImpl(item, submodel); | 177 BuildMenuImpl(item, submodel); |
| 162 | 178 |
| 163 menu_map_[item] = submodel; | 179 menu_map_[item] = submodel; |
| 164 } | 180 } |
| 165 } | 181 } |
| 166 | 182 |
| 167 menu->set_has_icons(model->HasIcons()); | 183 menu->set_has_icons(model->HasIcons()); |
| 168 } | 184 } |
| 169 | 185 |
| 170 } // views | 186 } // views |
| OLD | NEW |