| 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/examples/menu_example.h" | |
| 6 | |
| 7 #include <set> | |
| 8 | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "ui/base/models/simple_menu_model.h" | |
| 11 #include "views/controls/button/menu_button.h" | |
| 12 #include "views/controls/button/text_button.h" | |
| 13 #include "views/controls/menu/menu_2.h" | |
| 14 #include "views/controls/menu/view_menu_delegate.h" | |
| 15 #include "views/layout/fill_layout.h" | |
| 16 #include "views/view.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class ExampleMenuModel : public ui::SimpleMenuModel, | |
| 21 public ui::SimpleMenuModel::Delegate { | |
| 22 public: | |
| 23 ExampleMenuModel(); | |
| 24 | |
| 25 void RunMenuAt(const gfx::Point& point); | |
| 26 | |
| 27 // Overridden from ui::SimpleMenuModel::Delegate: | |
| 28 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | |
| 29 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | |
| 30 virtual bool GetAcceleratorForCommandId( | |
| 31 int command_id, | |
| 32 ui::Accelerator* accelerator) OVERRIDE; | |
| 33 virtual void ExecuteCommand(int command_id) OVERRIDE; | |
| 34 | |
| 35 private: | |
| 36 enum { | |
| 37 kGroupMakeDecision, | |
| 38 }; | |
| 39 | |
| 40 enum { | |
| 41 kCommandDoSomething, | |
| 42 kCommandSelectAscii, | |
| 43 kCommandSelectUtf8, | |
| 44 kCommandSelectUtf16, | |
| 45 kCommandCheckApple, | |
| 46 kCommandCheckOrange, | |
| 47 kCommandCheckKiwi, | |
| 48 kCommandGoHome, | |
| 49 }; | |
| 50 | |
| 51 scoped_ptr<views::Menu2> menu_; | |
| 52 scoped_ptr<ui::SimpleMenuModel> submenu_; | |
| 53 std::set<int> checked_fruits_; | |
| 54 int current_encoding_command_id_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(ExampleMenuModel); | |
| 57 }; | |
| 58 | |
| 59 class ExampleMenuButton : public views::MenuButton, | |
| 60 public views::ViewMenuDelegate { | |
| 61 public: | |
| 62 ExampleMenuButton(const string16& test, bool show_menu_marker); | |
| 63 virtual ~ExampleMenuButton(); | |
| 64 | |
| 65 private: | |
| 66 // Overridden from views::ViewMenuDelegate: | |
| 67 virtual void RunMenu(views::View* source, const gfx::Point& point) OVERRIDE; | |
| 68 | |
| 69 scoped_ptr<ExampleMenuModel> menu_model_; | |
| 70 DISALLOW_COPY_AND_ASSIGN(ExampleMenuButton); | |
| 71 }; | |
| 72 | |
| 73 // ExampleMenuModel --------------------------------------------------------- | |
| 74 | |
| 75 ExampleMenuModel::ExampleMenuModel() | |
| 76 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | |
| 77 current_encoding_command_id_(kCommandSelectAscii) { | |
| 78 AddItem(kCommandDoSomething, WideToUTF16(L"Do Something")); | |
| 79 AddSeparator(); | |
| 80 AddRadioItem(kCommandSelectAscii, | |
| 81 WideToUTF16(L"ASCII"), kGroupMakeDecision); | |
| 82 AddRadioItem(kCommandSelectUtf8, | |
| 83 WideToUTF16(L"UTF-8"), kGroupMakeDecision); | |
| 84 AddRadioItem(kCommandSelectUtf16, | |
| 85 WideToUTF16(L"UTF-16"), kGroupMakeDecision); | |
| 86 AddSeparator(); | |
| 87 AddCheckItem(kCommandCheckApple, WideToUTF16(L"Apple")); | |
| 88 AddCheckItem(kCommandCheckOrange, WideToUTF16(L"Orange")); | |
| 89 AddCheckItem(kCommandCheckKiwi, WideToUTF16(L"Kiwi")); | |
| 90 AddSeparator(); | |
| 91 AddItem(kCommandGoHome, WideToUTF16(L"Go Home")); | |
| 92 | |
| 93 submenu_.reset(new ui::SimpleMenuModel(this)); | |
| 94 submenu_->AddItem(kCommandDoSomething, WideToUTF16(L"Do Something 2")); | |
| 95 AddSubMenu(0, ASCIIToUTF16("Submenu"), submenu_.get()); | |
| 96 menu_.reset(new views::Menu2(this)); | |
| 97 } | |
| 98 | |
| 99 void ExampleMenuModel::RunMenuAt(const gfx::Point& point) { | |
| 100 menu_->RunMenuAt(point, views::Menu2::ALIGN_TOPRIGHT); | |
| 101 } | |
| 102 | |
| 103 bool ExampleMenuModel::IsCommandIdChecked(int command_id) const { | |
| 104 // Radio items. | |
| 105 if (command_id == current_encoding_command_id_) | |
| 106 return true; | |
| 107 | |
| 108 // Check items. | |
| 109 if (checked_fruits_.find(command_id) != checked_fruits_.end()) | |
| 110 return true; | |
| 111 | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 bool ExampleMenuModel::IsCommandIdEnabled(int command_id) const { | |
| 116 // All commands are enabled except for kCommandGoHome. | |
| 117 return command_id != kCommandGoHome; | |
| 118 } | |
| 119 | |
| 120 bool ExampleMenuModel::GetAcceleratorForCommandId( | |
| 121 int command_id, | |
| 122 ui::Accelerator* accelerator) { | |
| 123 // We don't use this in the example. | |
| 124 return false; | |
| 125 } | |
| 126 | |
| 127 void ExampleMenuModel::ExecuteCommand(int command_id) { | |
| 128 switch (command_id) { | |
| 129 case kCommandDoSomething: { | |
| 130 LOG(INFO) << "Done something"; | |
| 131 break; | |
| 132 } | |
| 133 | |
| 134 // Radio items. | |
| 135 case kCommandSelectAscii: { | |
| 136 current_encoding_command_id_ = kCommandSelectAscii; | |
| 137 LOG(INFO) << "Selected ASCII"; | |
| 138 break; | |
| 139 } | |
| 140 case kCommandSelectUtf8: { | |
| 141 current_encoding_command_id_ = kCommandSelectUtf8; | |
| 142 LOG(INFO) << "Selected UTF-8"; | |
| 143 break; | |
| 144 } | |
| 145 case kCommandSelectUtf16: { | |
| 146 current_encoding_command_id_ = kCommandSelectUtf16; | |
| 147 LOG(INFO) << "Selected UTF-16"; | |
| 148 break; | |
| 149 } | |
| 150 | |
| 151 // Check items. | |
| 152 case kCommandCheckApple: | |
| 153 case kCommandCheckOrange: | |
| 154 case kCommandCheckKiwi: { | |
| 155 // Print what fruit is checked. | |
| 156 const char* checked_fruit = ""; | |
| 157 if (command_id == kCommandCheckApple) { | |
| 158 checked_fruit = "Apple"; | |
| 159 } else if (command_id == kCommandCheckOrange) { | |
| 160 checked_fruit = "Orange"; | |
| 161 } else if (command_id == kCommandCheckKiwi) { | |
| 162 checked_fruit = "Kiwi"; | |
| 163 } | |
| 164 LOG(INFO) << "Checked " << checked_fruit; | |
| 165 | |
| 166 // Update the check status. | |
| 167 std::set<int>::iterator iter = checked_fruits_.find(command_id); | |
| 168 if (iter == checked_fruits_.end()) | |
| 169 checked_fruits_.insert(command_id); | |
| 170 else | |
| 171 checked_fruits_.erase(iter); | |
| 172 break; | |
| 173 } | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 // ExampleMenuButton ----------------------------------------------------------- | |
| 178 | |
| 179 ExampleMenuButton::ExampleMenuButton(const string16& test, | |
| 180 bool show_menu_marker) | |
| 181 : ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 182 views::MenuButton(NULL, test, this, show_menu_marker)) { | |
| 183 } | |
| 184 | |
| 185 ExampleMenuButton::~ExampleMenuButton() { | |
| 186 } | |
| 187 | |
| 188 void ExampleMenuButton::RunMenu(views::View* source, const gfx::Point& point) { | |
| 189 if (!menu_model_.get()) | |
| 190 menu_model_.reset(new ExampleMenuModel); | |
| 191 | |
| 192 menu_model_->RunMenuAt(point); | |
| 193 } | |
| 194 | |
| 195 } // namespace | |
| 196 | |
| 197 namespace examples { | |
| 198 | |
| 199 MenuExample::MenuExample(ExamplesMain* main) | |
| 200 : ExampleBase(main, "Menu") { | |
| 201 } | |
| 202 | |
| 203 MenuExample::~MenuExample() { | |
| 204 } | |
| 205 | |
| 206 void MenuExample::CreateExampleView(views::View* container) { | |
| 207 // views::Menu2 is not a sub class of View, hence we cannot directly | |
| 208 // add to the container. Instead, we add a button to open a menu. | |
| 209 const bool show_menu_marker = true; | |
| 210 ExampleMenuButton* menu_button = new ExampleMenuButton( | |
| 211 ASCIIToUTF16("Open a menu"), show_menu_marker); | |
| 212 container->SetLayoutManager(new views::FillLayout); | |
| 213 container->AddChildView(menu_button); | |
| 214 } | |
| 215 | |
| 216 } // namespace examples | |
| OLD | NEW |