| 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/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "ui/base/models/menu_model.h" | 8 #include "ui/base/models/menu_model.h" |
| 9 #include "ui/base/models/menu_model_delegate.h" | 9 #include "ui/base/models/menu_model_delegate.h" |
| 10 #include "ui/views/controls/menu/menu_item_view.h" | 10 #include "ui/views/controls/menu/menu_item_view.h" |
| 11 #include "ui/views/controls/menu/menu_runner.h" | 11 #include "ui/views/controls/menu/menu_runner.h" |
| 12 #include "ui/views/controls/menu/submenu_view.h" | 12 #include "ui/views/controls/menu/submenu_view.h" |
| 13 #include "ui/views/test/views_test_base.h" | 13 #include "ui/views/test/views_test_base.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Base command id for test menu and its submenu. | 17 // Base command id for test menu and its submenu. |
| 18 const int kRootIdBase = 100; | 18 const int kRootIdBase = 100; |
| 19 const int kSubmenuIdBase = 200; | 19 const int kSubmenuIdBase = 200; |
| 20 | 20 |
| 21 class MenuModelBase : public ui::MenuModel { | 21 class MenuModelBase : public ui::MenuModel { |
| 22 public: | 22 public: |
| 23 explicit MenuModelBase(int command_id_base) | 23 explicit MenuModelBase(int command_id_base) |
| 24 : command_id_base_(command_id_base), | 24 : command_id_base_(command_id_base), |
| 25 last_activation_(-1) { | 25 last_activation_(-1) { |
| 26 } | 26 } |
| 27 | 27 |
| 28 virtual ~MenuModelBase() { | 28 ~MenuModelBase() override {} |
| 29 } | |
| 30 | 29 |
| 31 // ui::MenuModel implementation: | 30 // ui::MenuModel implementation: |
| 32 | 31 |
| 33 virtual bool HasIcons() const override { | 32 bool HasIcons() const override { return false; } |
| 33 |
| 34 int GetItemCount() const override { return static_cast<int>(items_.size()); } |
| 35 |
| 36 ItemType GetTypeAt(int index) const override { return items_[index].type; } |
| 37 |
| 38 ui::MenuSeparatorType GetSeparatorTypeAt(int index) const override { |
| 39 return ui::NORMAL_SEPARATOR; |
| 40 } |
| 41 |
| 42 int GetCommandIdAt(int index) const override { |
| 43 return index + command_id_base_; |
| 44 } |
| 45 |
| 46 base::string16 GetLabelAt(int index) const override { |
| 47 return items_[index].label; |
| 48 } |
| 49 |
| 50 bool IsItemDynamicAt(int index) const override { return false; } |
| 51 |
| 52 const gfx::FontList* GetLabelFontListAt(int index) const override { |
| 53 return NULL; |
| 54 } |
| 55 |
| 56 bool GetAcceleratorAt(int index, |
| 57 ui::Accelerator* accelerator) const override { |
| 34 return false; | 58 return false; |
| 35 } | 59 } |
| 36 | 60 |
| 37 virtual int GetItemCount() const override { | 61 bool IsItemCheckedAt(int index) const override { return false; } |
| 38 return static_cast<int>(items_.size()); | |
| 39 } | |
| 40 | 62 |
| 41 virtual ItemType GetTypeAt(int index) const override { | 63 int GetGroupIdAt(int index) const override { return 0; } |
| 42 return items_[index].type; | |
| 43 } | |
| 44 | 64 |
| 45 virtual ui::MenuSeparatorType GetSeparatorTypeAt( | 65 bool GetIconAt(int index, gfx::Image* icon) override { return false; } |
| 46 int index) const override { | |
| 47 return ui::NORMAL_SEPARATOR; | |
| 48 } | |
| 49 | 66 |
| 50 virtual int GetCommandIdAt(int index) const override { | 67 ui::ButtonMenuItemModel* GetButtonMenuItemAt(int index) const override { |
| 51 return index + command_id_base_; | |
| 52 } | |
| 53 | |
| 54 virtual base::string16 GetLabelAt(int index) const override { | |
| 55 return items_[index].label; | |
| 56 } | |
| 57 | |
| 58 virtual bool IsItemDynamicAt(int index) const override { | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 62 virtual const gfx::FontList* GetLabelFontListAt(int index) const override { | |
| 63 return NULL; | 68 return NULL; |
| 64 } | 69 } |
| 65 | 70 |
| 66 virtual bool GetAcceleratorAt(int index, | 71 bool IsEnabledAt(int index) const override { return true; } |
| 67 ui::Accelerator* accelerator) const override { | |
| 68 return false; | |
| 69 } | |
| 70 | 72 |
| 71 virtual bool IsItemCheckedAt(int index) const override { | 73 bool IsVisibleAt(int index) const override { return true; } |
| 72 return false; | |
| 73 } | |
| 74 | 74 |
| 75 virtual int GetGroupIdAt(int index) const override { | 75 MenuModel* GetSubmenuModelAt(int index) const override { |
| 76 return 0; | |
| 77 } | |
| 78 | |
| 79 virtual bool GetIconAt(int index, gfx::Image* icon) override { | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt( | |
| 84 int index) const override { | |
| 85 return NULL; | |
| 86 } | |
| 87 | |
| 88 virtual bool IsEnabledAt(int index) const override { | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 virtual bool IsVisibleAt(int index) const override { | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 virtual MenuModel* GetSubmenuModelAt(int index) const override { | |
| 97 return items_[index].submenu; | 76 return items_[index].submenu; |
| 98 } | 77 } |
| 99 | 78 |
| 100 virtual void HighlightChangedTo(int index) override { | 79 void HighlightChangedTo(int index) override {} |
| 101 } | |
| 102 | 80 |
| 103 virtual void ActivatedAt(int index) override { | 81 void ActivatedAt(int index) override { set_last_activation(index); } |
| 104 set_last_activation(index); | |
| 105 } | |
| 106 | 82 |
| 107 virtual void ActivatedAt(int index, int event_flags) override { | 83 void ActivatedAt(int index, int event_flags) override { ActivatedAt(index); } |
| 108 ActivatedAt(index); | |
| 109 } | |
| 110 | 84 |
| 111 virtual void MenuWillShow() override { | 85 void MenuWillShow() override {} |
| 112 } | |
| 113 | 86 |
| 114 virtual void MenuClosed() override { | 87 void MenuClosed() override {} |
| 115 } | |
| 116 | 88 |
| 117 virtual void SetMenuModelDelegate( | 89 void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) override {} |
| 118 ui::MenuModelDelegate* delegate) override { | |
| 119 } | |
| 120 | 90 |
| 121 virtual ui::MenuModelDelegate* GetMenuModelDelegate() const override { | 91 ui::MenuModelDelegate* GetMenuModelDelegate() const override { return NULL; } |
| 122 return NULL; | |
| 123 } | |
| 124 | 92 |
| 125 // Item definition. | 93 // Item definition. |
| 126 struct Item { | 94 struct Item { |
| 127 Item(ItemType item_type, | 95 Item(ItemType item_type, |
| 128 const std::string& item_label, | 96 const std::string& item_label, |
| 129 ui::MenuModel* item_submenu) | 97 ui::MenuModel* item_submenu) |
| 130 : type(item_type), | 98 : type(item_type), |
| 131 label(base::ASCIIToUTF16(item_label)), | 99 label(base::ASCIIToUTF16(item_label)), |
| 132 submenu(item_submenu) { | 100 submenu(item_submenu) { |
| 133 } | 101 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 157 DISALLOW_COPY_AND_ASSIGN(MenuModelBase); | 125 DISALLOW_COPY_AND_ASSIGN(MenuModelBase); |
| 158 }; | 126 }; |
| 159 | 127 |
| 160 class SubmenuModel : public MenuModelBase { | 128 class SubmenuModel : public MenuModelBase { |
| 161 public: | 129 public: |
| 162 SubmenuModel() : MenuModelBase(kSubmenuIdBase) { | 130 SubmenuModel() : MenuModelBase(kSubmenuIdBase) { |
| 163 items_.push_back(Item(TYPE_COMMAND, "submenu item 0", NULL)); | 131 items_.push_back(Item(TYPE_COMMAND, "submenu item 0", NULL)); |
| 164 items_.push_back(Item(TYPE_COMMAND, "submenu item 1", NULL)); | 132 items_.push_back(Item(TYPE_COMMAND, "submenu item 1", NULL)); |
| 165 } | 133 } |
| 166 | 134 |
| 167 virtual ~SubmenuModel() { | 135 ~SubmenuModel() override {} |
| 168 } | |
| 169 | 136 |
| 170 private: | 137 private: |
| 171 DISALLOW_COPY_AND_ASSIGN(SubmenuModel); | 138 DISALLOW_COPY_AND_ASSIGN(SubmenuModel); |
| 172 }; | 139 }; |
| 173 | 140 |
| 174 class RootModel : public MenuModelBase { | 141 class RootModel : public MenuModelBase { |
| 175 public: | 142 public: |
| 176 RootModel() : MenuModelBase(kRootIdBase) { | 143 RootModel() : MenuModelBase(kRootIdBase) { |
| 177 submenu_model_.reset(new SubmenuModel); | 144 submenu_model_.reset(new SubmenuModel); |
| 178 | 145 |
| 179 items_.push_back(Item(TYPE_COMMAND, "command 0", NULL)); | 146 items_.push_back(Item(TYPE_COMMAND, "command 0", NULL)); |
| 180 items_.push_back(Item(TYPE_CHECK, "check 1", NULL)); | 147 items_.push_back(Item(TYPE_CHECK, "check 1", NULL)); |
| 181 items_.push_back(Item(TYPE_SEPARATOR, "", NULL)); | 148 items_.push_back(Item(TYPE_SEPARATOR, "", NULL)); |
| 182 items_.push_back(Item(TYPE_SUBMENU, "submenu 3", submenu_model_.get())); | 149 items_.push_back(Item(TYPE_SUBMENU, "submenu 3", submenu_model_.get())); |
| 183 items_.push_back(Item(TYPE_RADIO, "radio 4", NULL)); | 150 items_.push_back(Item(TYPE_RADIO, "radio 4", NULL)); |
| 184 } | 151 } |
| 185 | 152 |
| 186 virtual ~RootModel() { | 153 ~RootModel() override {} |
| 187 } | |
| 188 | 154 |
| 189 private: | 155 private: |
| 190 scoped_ptr<MenuModel> submenu_model_; | 156 scoped_ptr<MenuModel> submenu_model_; |
| 191 | 157 |
| 192 DISALLOW_COPY_AND_ASSIGN(RootModel); | 158 DISALLOW_COPY_AND_ASSIGN(RootModel); |
| 193 }; | 159 }; |
| 194 | 160 |
| 195 } // namespace | 161 } // namespace |
| 196 | 162 |
| 197 namespace views { | 163 namespace views { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 submodel->set_last_activation(-1); | 266 submodel->set_last_activation(-1); |
| 301 } | 267 } |
| 302 | 268 |
| 303 // Check that selecting the root item is safe. The MenuModel does | 269 // Check that selecting the root item is safe. The MenuModel does |
| 304 // not care about the root so MenuModelAdapter should do nothing | 270 // not care about the root so MenuModelAdapter should do nothing |
| 305 // (not hit the NOTREACHED check) when the root is selected. | 271 // (not hit the NOTREACHED check) when the root is selected. |
| 306 static_cast<views::MenuDelegate*>(&delegate)->SelectionChanged(menu); | 272 static_cast<views::MenuDelegate*>(&delegate)->SelectionChanged(menu); |
| 307 } | 273 } |
| 308 | 274 |
| 309 } // namespace views | 275 } // namespace views |
| OLD | NEW |